Debian Codegraph @

go.c

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