Debian Codegraph @

debian/patches/go.patch

raw
1 From 18e7bce7b0a8c8761c0b911f3039a7f949703fb3 Mon Sep 17 00:00:00 2001
2 From: Alexey Marinichev <[email protected]>
3 Date: Sat, 15 Feb 2014 22:47:04 +0000
4 Subject: Add Go support
5
6 Bug-Debian: http://bugs.debian.org/634166
7 Origin: other, https://github.com/lyosha/ctags-go/commit/ca097bd639e35470a9abccbf348016b7cc44f811
8 Last-Update: 2011-07-20
9
10 Patch-Name: go.patch
11 ---
12 go.c | 670 +++++++++++++++++++++++++++++++++++++++++++++++++++++
13 parsers.h | 1 +
14 source.mak | 2 +
15 3 files changed, 673 insertions(+)
16 create mode 100644 go.c
17
18 diff --git a/go.c b/go.c
19 new file mode 100644
20 index 000000000..6bd3a369a
21 --- /dev/null
22 +++ b/go.c
23 @@ -0,0 +1,670 @@
24 +/*
25 +* INCLUDE FILES
26 +*/
27 +#include "general.h" /* must always come first */
28 +#include <setjmp.h>
29 +
30 +#include "debug.h"
31 +#include "entry.h"
32 +#include "keyword.h"
33 +#include "read.h"
34 +#include "main.h"
35 +#include "routines.h"
36 +#include "vstring.h"
37 +#include "options.h"
38 +
39 +/*
40 + * MACROS
41 + */
42 +#define isType(token,t) (boolean) ((token)->type == (t))
43 +#define isKeyword(token,k) (boolean) ((token)->keyword == (k))
44 +
45 +/*
46 + * DATA DECLARATIONS
47 + */
48 +
49 +typedef enum eException { ExceptionNone, ExceptionEOF } exception_t;
50 +
51 +typedef enum eKeywordId {
52 + KEYWORD_NONE = -1,
53 + KEYWORD_package,
54 + KEYWORD_import,
55 + KEYWORD_const,
56 + KEYWORD_type,
57 + KEYWORD_var,
58 + KEYWORD_func,
59 + KEYWORD_struct,
60 + KEYWORD_interface,
61 + KEYWORD_map,
62 + KEYWORD_chan
63 +} keywordId;
64 +
65 +/* Used to determine whether keyword is valid for the current language and
66 + * what its ID is.
67 + */
68 +typedef struct sKeywordDesc {
69 + const char *name;
70 + keywordId id;
71 +} keywordDesc;
72 +
73 +typedef enum eTokenType {
74 + TOKEN_NONE = -1,
75 + TOKEN_CHARACTER,
76 + // Don't need TOKEN_FORWARD_SLASH
77 + TOKEN_FORWARD_SLASH,
78 + TOKEN_KEYWORD,
79 + TOKEN_IDENTIFIER,
80 + TOKEN_STRING,
81 + TOKEN_OPEN_PAREN,
82 + TOKEN_CLOSE_PAREN,
83 + TOKEN_OPEN_CURLY,
84 + TOKEN_CLOSE_CURLY,
85 + TOKEN_OPEN_SQUARE,
86 + TOKEN_CLOSE_SQUARE,
87 + TOKEN_SEMICOLON,
88 + TOKEN_STAR,
89 + TOKEN_LEFT_ARROW,
90 + TOKEN_DOT,
91 + TOKEN_COMMA
92 +} tokenType;
93 +
94 +typedef struct sTokenInfo {
95 + tokenType type;
96 + keywordId keyword;
97 + vString *string; /* the name of the token */
98 + unsigned long lineNumber; /* line number of tag */
99 + fpos_t filePosition; /* file position of line containing name */
100 +} tokenInfo;
101 +
102 +/*
103 +* DATA DEFINITIONS
104 +*/
105 +
106 +static int Lang_go;
107 +static jmp_buf Exception;
108 +static vString *scope;
109 +
110 +typedef enum {
111 + GOTAG_UNDEFINED = -1,
112 + GOTAG_PACKAGE,
113 + GOTAG_FUNCTION,
114 + GOTAG_CONST,
115 + GOTAG_TYPE,
116 + GOTAG_VAR,
117 +} goKind;
118 +
119 +static kindOption GoKinds[] = {
120 + {TRUE, 'p', "package", "packages"},
121 + {TRUE, 'f', "func", "functions"},
122 + {TRUE, 'c', "const", "constants"},
123 + {TRUE, 't', "type", "types"},
124 + {TRUE, 'v', "var", "variables"}
125 +};
126 +
127 +static keywordDesc GoKeywordTable[] = {
128 + {"package", KEYWORD_package},
129 + {"import", KEYWORD_import},
130 + {"const", KEYWORD_const},
131 + {"type", KEYWORD_type},
132 + {"var", KEYWORD_var},
133 + {"func", KEYWORD_func},
134 + {"struct", KEYWORD_struct},
135 + {"interface", KEYWORD_interface},
136 + {"map", KEYWORD_map},
137 + {"chan", KEYWORD_chan}
138 +};
139 +
140 +/*
141 +* FUNCTION DEFINITIONS
142 +*/
143 +
144 +// XXX UTF-8
145 +static boolean isIdentChar (const int c)
146 +{
147 + return (boolean)
148 + (isalpha (c) || isdigit (c) || c == '$' ||
149 + c == '@' || c == '_' || c == '#' || c > 128);
150 +}
151 +
152 +static void initialize (const langType language)
153 +{
154 + size_t i;
155 + const size_t count =
156 + sizeof (GoKeywordTable) / sizeof (GoKeywordTable[0]);
157 + Lang_go = language;
158 + for (i = 0; i < count; ++i)
159 + {
160 + const keywordDesc *const p = &GoKeywordTable[i];
161 + addKeyword (p->name, language, (int) p->id);
162 + }
163 +}
164 +
165 +static tokenInfo *newToken (void)
166 +{
167 + tokenInfo *const token = xMalloc (1, tokenInfo);
168 + token->type = TOKEN_NONE;
169 + token->keyword = KEYWORD_NONE;
170 + token->string = vStringNew ();
171 + token->lineNumber = getSourceLineNumber ();
172 + token->filePosition = getInputFilePosition ();
173 + return token;
174 +}
175 +
176 +static void deleteToken (tokenInfo * const token)
177 +{
178 + if (token != NULL)
179 + {
180 + vStringDelete (token->string);
181 + eFree (token);
182 + }
183 +}
184 +
185 +/*
186 + * Parsing functions
187 + */
188 +
189 +static void parseString (vString *const string, const int delimiter)
190 +{
191 + boolean end = FALSE;
192 + while (!end)
193 + {
194 + int c = fileGetc ();
195 + if (c == EOF)
196 + end = TRUE;
197 + else if (c == '\\' && delimiter != '`')
198 + {
199 + c = fileGetc (); /* This maybe a ' or ". */
200 + vStringPut (string, c);
201 + }
202 + else if (c == delimiter)
203 + end = TRUE;
204 + else
205 + vStringPut (string, c);
206 + }
207 + vStringTerminate (string);
208 +}
209 +
210 +static void parseIdentifier (vString *const string, const int firstChar)
211 +{
212 + int c = firstChar;
213 + //Assert (isIdentChar (c));
214 + do
215 + {
216 + vStringPut (string, c);
217 + c = fileGetc ();
218 + } while (isIdentChar (c));
219 + vStringTerminate (string);
220 + fileUngetc (c); /* always unget, LF might add a semicolon */
221 +}
222 +
223 +static void readToken (tokenInfo *const token)
224 +{
225 + int c;
226 + static tokenType lastTokenType = TOKEN_NONE;
227 +
228 + token->type = TOKEN_NONE;
229 + token->keyword = KEYWORD_NONE;
230 + vStringClear (token->string);
231 +
232 +getNextChar:
233 + do
234 + {
235 + c = fileGetc ();
236 + token->lineNumber = getSourceLineNumber ();
237 + token->filePosition = getInputFilePosition ();
238 + if (c == '\n' && (lastTokenType == TOKEN_IDENTIFIER ||
239 + lastTokenType == TOKEN_STRING ||
240 + lastTokenType == TOKEN_CLOSE_PAREN ||
241 + lastTokenType == TOKEN_CLOSE_CURLY ||
242 + lastTokenType == TOKEN_CLOSE_SQUARE))
243 + {
244 + token->type = TOKEN_SEMICOLON;
245 + goto done;
246 + }
247 + }
248 + while (c == '\t' || c == ' ' || c == '\r' || c == '\n');
249 +
250 + switch (c)
251 + {
252 + case EOF:
253 + longjmp (Exception, (int)ExceptionEOF);
254 + break;
255 +
256 + case '/':
257 + {
258 + boolean hasNewline = FALSE;
259 + int d = fileGetc ();
260 + switch (d)
261 + {
262 + case '/':
263 + fileSkipToCharacter ('\n');
264 + /* Line comments start with the
265 + * character sequence // and
266 + * continue through the next
267 + * newline. A line comment acts
268 + * like a newline. */
269 + fileUngetc ('\n');
270 + goto getNextChar;
271 + case '*':
272 + do
273 + {
274 + int d;
275 + do
276 + {
277 + d = fileGetc ();
278 + if (d == '\n')
279 + {
280 + hasNewline = TRUE;
281 + }
282 + } while (d != EOF && d != '*');
283 +
284 + c = fileGetc ();
285 + if (c == '/')
286 + break;
287 + else
288 + fileUngetc (c);
289 + } while (c != EOF && c != '\0');
290 +
291 + fileUngetc (hasNewline ? '\n' : ' ');
292 + goto getNextChar;
293 + default:
294 + token->type = TOKEN_FORWARD_SLASH;
295 + fileUngetc (d);
296 + break;
297 + }
298 + }
299 + break;
300 +
301 + case '"':
302 + case '\'':
303 + case '`':
304 + token->type = TOKEN_STRING;
305 + parseString (token->string, c);
306 + token->lineNumber = getSourceLineNumber ();
307 + token->filePosition = getInputFilePosition ();
308 + break;
309 +
310 + case '<':
311 + {
312 + int d = fileGetc ();
313 + if (d == '-')
314 + {
315 + token->type = TOKEN_LEFT_ARROW;
316 + break;
317 + }
318 + else
319 + goto getNextChar;
320 + }
321 +
322 + case '(':
323 + token->type = TOKEN_OPEN_PAREN;
324 + break;
325 +
326 + case ')':
327 + token->type = TOKEN_CLOSE_PAREN;
328 + break;
329 +
330 + case '{':
331 + token->type = TOKEN_OPEN_CURLY;
332 + break;
333 +
334 + case '}':
335 + token->type = TOKEN_CLOSE_CURLY;
336 + break;
337 +
338 + case '[':
339 + token->type = TOKEN_OPEN_SQUARE;
340 + break;
341 +
342 + case ']':
343 + token->type = TOKEN_CLOSE_SQUARE;
344 + break;
345 +
346 + case '*':
347 + token->type = TOKEN_STAR;
348 + break;
349 +
350 + case '.':
351 + token->type = TOKEN_DOT;
352 + break;
353 +
354 + case ',':
355 + token->type = TOKEN_COMMA;
356 + break;
357 +
358 + default:
359 + parseIdentifier (token->string, c);
360 + token->lineNumber = getSourceLineNumber ();
361 + token->filePosition = getInputFilePosition ();
362 + token->keyword = lookupKeyword (vStringValue (token->string), Lang_go);
363 + if (isKeyword (token, KEYWORD_NONE))
364 + token->type = TOKEN_IDENTIFIER;
365 + else
366 + token->type = TOKEN_KEYWORD;
367 + break;
368 + }
369 +
370 +done:
371 + lastTokenType = token->type;
372 +}
373 +
374 +static void skipToMatched (tokenInfo *const token)
375 +{
376 + int nest_level = 0;
377 + tokenType open_token;
378 + tokenType close_token;
379 +
380 + switch (token->type)
381 + {
382 + case TOKEN_OPEN_PAREN:
383 + open_token = TOKEN_OPEN_PAREN;
384 + close_token = TOKEN_CLOSE_PAREN;
385 + break;
386 + case TOKEN_OPEN_CURLY:
387 + open_token = TOKEN_OPEN_CURLY;
388 + close_token = TOKEN_CLOSE_CURLY;
389 + break;
390 + case TOKEN_OPEN_SQUARE:
391 + open_token = TOKEN_OPEN_SQUARE;
392 + close_token = TOKEN_CLOSE_SQUARE;
393 + break;
394 + default:
395 + return;
396 + }
397 +
398 + /*
399 + * This routine will skip to a matching closing token.
400 + * It will also handle nested tokens like the (, ) below.
401 + * ( name varchar(30), text binary(10) )
402 + */
403 + if (isType (token, open_token))
404 + {
405 + nest_level++;
406 + while (!(isType (token, close_token) && (nest_level == 0)))
407 + {
408 + readToken (token);
409 + if (isType (token, open_token))
410 + {
411 + nest_level++;
412 + }
413 + if (isType (token, close_token))
414 + {
415 + if (nest_level > 0)
416 + {
417 + nest_level--;
418 + }
419 + }
420 + }
421 + readToken (token);
422 + }
423 +}
424 +
425 +static void skipType (tokenInfo *const token)
426 +{
427 +again:
428 + // Type = TypeName | TypeLit | "(" Type ")" .
429 + if (isType (token, TOKEN_OPEN_PAREN))
430 + {
431 + skipToMatched (token);
432 + return;
433 + }
434 +
435 + // TypeName = QualifiedIdent.
436 + // QualifiedIdent = [ PackageName "." ] identifier .
437 + // PackageName = identifier .
438 + if (isType (token, TOKEN_IDENTIFIER))
439 + {
440 + readToken (token);
441 + if (isType (token, TOKEN_DOT))
442 + {
443 + readToken (token);
444 + Assert (isType (token, TOKEN_IDENTIFIER));
445 + readToken (token);
446 + }
447 + return;
448 + }
449 +
450 + // StructType = "struct" "{" { FieldDecl ";" } "}"
451 + // InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
452 + if (isKeyword (token, KEYWORD_struct) || isKeyword (token, KEYWORD_interface))
453 + {
454 + readToken (token);
455 + Assert (isType (token, TOKEN_OPEN_CURLY));
456 + skipToMatched (token);
457 + return;
458 + }
459 +
460 + // ArrayType = "[" ArrayLength "]" ElementType .
461 + // SliceType = "[" "]" ElementType .
462 + // ElementType = Type .
463 + if (isType (token, TOKEN_OPEN_SQUARE))
464 + {
465 + skipToMatched (token);
466 + goto again;
467 + }
468 +
469 + // PointerType = "*" BaseType .
470 + // BaseType = Type .
471 + // ChannelType = ( "chan" [ "<-" ] | "<-" "chan" ) ElementType .
472 + if (isType (token, TOKEN_STAR) || isKeyword (token, KEYWORD_chan) || isType (token, TOKEN_LEFT_ARROW))
473 + {
474 + readToken (token);
475 + goto again;
476 + }
477 +
478 + // MapType = "map" "[" KeyType "]" ElementType .
479 + // KeyType = Type .
480 + if (isKeyword (token, KEYWORD_map))
481 + {
482 + readToken (token);
483 + Assert (isType (token, TOKEN_OPEN_SQUARE));
484 + skipToMatched (token);
485 + goto again;
486 + }
487 +
488 + // FunctionType = "func" Signature .
489 + // Signature = Parameters [ Result ] .
490 + // Result = Parameters | Type .
491 + // Parameters = "(" [ ParameterList [ "," ] ] ")" .
492 + if (isKeyword (token, KEYWORD_func))
493 + {
494 + readToken (token);
495 + Assert (isType (token, TOKEN_OPEN_PAREN));
496 + // Parameters
497 + skipToMatched (token);
498 + // Result is parameters or type or nothing. skipType treats anything
499 + // surrounded by parentheses as a type, and does nothing if what
500 + // follows is not a type.
501 + goto again;
502 + }
503 +}
504 +
505 +// Skip to the next semicolon, skipping over matching brackets.
506 +static void skipToTopLevelSemicolon (tokenInfo *const token)
507 +{
508 + while (!isType (token, TOKEN_SEMICOLON))
509 + {
510 + readToken (token);
511 + skipToMatched (token);
512 + }
513 +}
514 +
515 +static void makeTag (tokenInfo *const token, const goKind kind)
516 +{
517 + const char *const name = vStringValue (token->string);
518 +
519 + tagEntryInfo e;
520 + initTagEntry (&e, name);
521 +
522 + if (!GoKinds [kind].enabled)
523 + return;
524 +
525 + e.lineNumber = token->lineNumber;
526 + e.filePosition = token->filePosition;
527 + e.kindName = GoKinds [kind].name;
528 + e.kind = GoKinds [kind].letter;
529 +
530 + makeTagEntry (&e);
531 +
532 + if (scope && Option.include.qualifiedTags)
533 + {
534 + vString *qualifiedName = vStringNew ();
535 + vStringCopy (qualifiedName, scope);
536 + vStringCatS (qualifiedName, ".");
537 + vStringCat (qualifiedName, token->string);
538 + e.name = vStringValue (qualifiedName);
539 + makeTagEntry (&e);
540 + vStringDelete (qualifiedName);
541 + }
542 +}
543 +
544 +static void parsePackage (tokenInfo *const token)
545 +{
546 + tokenInfo *const name = newToken ();
547 +
548 + readToken (name);
549 + Assert (isType (name, TOKEN_IDENTIFIER));
550 + makeTag (name, GOTAG_PACKAGE);
551 + if (!scope && Option.include.qualifiedTags)
552 + {
553 + scope = vStringNew ();
554 + vStringCopy (scope, name->string);
555 + }
556 +
557 + deleteToken (name);
558 +}
559 +
560 +static void parseFunctionOrMethod (tokenInfo *const token)
561 +{
562 + // FunctionDecl = "func" identifier Signature [ Body ] .
563 + // Body = Block.
564 + //
565 + // MethodDecl = "func" Receiver MethodName Signature [ Body ] .
566 + // Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
567 + // BaseTypeName = identifier .
568 + tokenInfo *const name = newToken ();
569 +
570 + // Skip over receiver.
571 + readToken (name);
572 + if (isType (name, TOKEN_OPEN_PAREN))
573 + skipToMatched (name);
574 +
575 + Assert (isType (name, TOKEN_IDENTIFIER));
576 +
577 + // Skip over parameters.
578 + readToken (token);
579 + skipToMatched (token);
580 +
581 + // Skip over result.
582 + skipType (token);
583 +
584 + // Skip over function body.
585 + if (isType (token, TOKEN_OPEN_CURLY))
586 + skipToMatched (token);
587 +
588 + makeTag (name, GOTAG_FUNCTION);
589 +
590 + deleteToken (name);
591 +}
592 +
593 +static void parseConstTypeVar (tokenInfo *const token, goKind kind)
594 +{
595 + // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
596 + // ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
597 + // IdentifierList = identifier { "," identifier } .
598 + // ExpressionList = Expression { "," Expression } .
599 + // TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
600 + // TypeSpec = identifier Type .
601 + // VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
602 + // VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
603 + tokenInfo *const name = newToken ();
604 + boolean usesParens = FALSE;
605 +
606 + readToken (name);
607 +
608 + if (isType (name, TOKEN_OPEN_PAREN))
609 + {
610 + usesParens = TRUE;
611 + readToken (name);
612 + }
613 +
614 +again:
615 + while (1)
616 + {
617 + makeTag (name, kind);
618 + readToken (token);
619 + if (!isType (token, TOKEN_COMMA) && !isType (token, TOKEN_CLOSE_PAREN))
620 + break;
621 + readToken (name);
622 + }
623 +
624 + skipType (token);
625 + skipToTopLevelSemicolon (token);
626 +
627 + if (usesParens)
628 + {
629 + readToken (name);
630 + if (!isType (name, TOKEN_CLOSE_PAREN))
631 + goto again;
632 + }
633 +
634 + deleteToken (name);
635 +}
636 +
637 +static void parseGoFile (tokenInfo *const token)
638 +{
639 + do
640 + {
641 + readToken (token);
642 +
643 + if (isType (token, TOKEN_KEYWORD))
644 + {
645 + switch (token->keyword)
646 + {
647 + case KEYWORD_package:
648 + parsePackage (token);
649 + break;
650 + case KEYWORD_func:
651 + parseFunctionOrMethod (token);
652 + break;
653 + case KEYWORD_const:
654 + parseConstTypeVar (token, GOTAG_CONST);
655 + break;
656 + case KEYWORD_type:
657 + parseConstTypeVar (token, GOTAG_TYPE);
658 + break;
659 + case KEYWORD_var:
660 + parseConstTypeVar (token, GOTAG_VAR);
661 + break;
662 + default:
663 + break;
664 + }
665 + }
666 + } while (TRUE);
667 +}
668 +
669 +static void findGoTags (void)
670 +{
671 + tokenInfo *const token = newToken ();
672 + exception_t exception;
673 +
674 + exception = (exception_t) (setjmp (Exception));
675 + while (exception == ExceptionNone)
676 + parseGoFile (token);
677 +
678 + deleteToken (token);
679 + vStringDelete (scope);
680 + scope = NULL;
681 +}
682 +
683 +extern parserDefinition *GoParser (void)
684 +{
685 + static const char *const extensions[] = { "go", NULL };
686 + parserDefinition *def = parserNew ("Go");
687 + def->kinds = GoKinds;
688 + def->kindCount = KIND_COUNT (GoKinds);
689 + def->extensions = extensions;
690 + def->parser = findGoTags;
691 + def->initialize = initialize;
692 + return def;
693 +}
694 diff --git a/parsers.h b/parsers.h
695 index 600f63614..3a24d6e09 100644
696 --- a/parsers.h
697 +++ b/parsers.h
698 @@ -31,6 +31,7 @@
699 ErlangParser, \
700 FlexParser, \
701 FortranParser, \
702 + GoParser, \
703 HtmlParser, \
704 JavaParser, \
705 JavaScriptParser, \
706 diff --git a/source.mak b/source.mak
707 index c97617f34..985d56cfc 100644
708 --- a/source.mak
709 +++ b/source.mak
710 @@ -24,6 +24,7 @@ SOURCES = \
711 flex.c \
712 fortran.c \
713 get.c \
714 + go.c \
715 html.c \
716 jscript.c \
717 keyword.c \
718 @@ -87,6 +88,7 @@ OBJECTS = \
719 flex.$(OBJEXT) \
720 fortran.$(OBJEXT) \
721 get.$(OBJEXT) \
722 + go.$(OBJEXT) \
723 html.$(OBJEXT) \
724 jscript.$(OBJEXT) \
725 keyword.$(OBJEXT) \