Debian Codegraph @

objc.c

raw ◀ ▶
1 Debian patch touch this file
1
2 /*
3 * Copyright (c) 2010, Vincent Berthoux
4 *
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
7 *
8 * This module contains functions for generating tags for Objective C
9 * language files.
10 */
11 /*
12 * INCLUDE FILES
13 */
14 #include "general.h" /* must always come first */
15
16 #include <string.h>
17
18 #include "keyword.h"
19 #include "entry.h"
20 #include "options.h"
21 #include "read.h"
22 #include "routines.h"
23 #include "vstring.h"
24
25 /* To get rid of unused parameter warning in
26 * -Wextra */
27 #ifdef UNUSED
28 #elif defined(__GNUC__)
29 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
30 #elif defined(__LCLINT__)
31 # define UNUSED(x) /*@unused@*/ x
32 #else
33 # define UNUSED(x) x
34 #endif
35
36 typedef enum {
37 K_INTERFACE,
38 K_IMPLEMENTATION,
39 K_PROTOCOL,
40 K_METHOD,
41 K_CLASSMETHOD,
42 K_VAR,
43 K_FIELD,
44 K_FUNCTION,
45 K_PROPERTY,
46 K_TYPEDEF,
47 K_STRUCT,
48 K_ENUM,
49 K_MACRO
50 } objcKind;
51
52 static kindOption ObjcKinds[] = {
53 {TRUE, 'i', "interface", "class interface"},
54 {TRUE, 'I', "implementation", "class implementation"},
55 {TRUE, 'p', "protocol", "Protocol"},
56 {TRUE, 'm', "method", "Object's method"},
57 {TRUE, 'c', "class", "Class' method"},
58 {TRUE, 'v', "var", "Global variable"},
59 {TRUE, 'F', "field", "Object field"},
60 {TRUE, 'f', "function", "A function"},
61 {TRUE, 'p', "property", "A property"},
62 {TRUE, 't', "typedef", "A type alias"},
63 {TRUE, 's', "struct", "A type structure"},
64 {TRUE, 'e', "enum", "An enumeration"},
65 {TRUE, 'M', "macro", "A preprocessor macro"},
66 };
67
68 typedef enum {
69 ObjcTYPEDEF,
70 ObjcSTRUCT,
71 ObjcENUM,
72 ObjcIMPLEMENTATION,
73 ObjcINTERFACE,
74 ObjcPROTOCOL,
75 ObjcENCODE,
76 ObjcSYNCHRONIZED,
77 ObjcSELECTOR,
78 ObjcPROPERTY,
79 ObjcEND,
80 ObjcDEFS,
81 ObjcCLASS,
82 ObjcPRIVATE,
83 ObjcPACKAGE,
84 ObjcPUBLIC,
85 ObjcPROTECTED,
86 ObjcSYNTHESIZE,
87 ObjcDYNAMIC,
88 ObjcOPTIONAL,
89 ObjcREQUIRED,
90 ObjcSTRING,
91 ObjcIDENTIFIER,
92
93 Tok_COMA, /* ',' */
94 Tok_PLUS, /* '+' */
95 Tok_MINUS, /* '-' */
96 Tok_PARL, /* '(' */
97 Tok_PARR, /* ')' */
98 Tok_CurlL, /* '{' */
99 Tok_CurlR, /* '}' */
100 Tok_SQUAREL, /* '[' */
101 Tok_SQUARER, /* ']' */
102 Tok_semi, /* ';' */
103 Tok_dpoint, /* ':' */
104 Tok_Sharp, /* '#' */
105 Tok_Backslash, /* '\\' */
106 Tok_EOL, /* '\r''\n' */
107 Tok_any,
108
109 Tok_EOF /* END of file */
110 } objcKeyword;
111
112 typedef objcKeyword objcToken;
113
114 typedef struct sOBjcKeywordDesc {
115 const char *name;
116 objcKeyword id;
117 } objcKeywordDesc;
118
119
120 static const objcKeywordDesc objcKeywordTable[] = {
121 {"typedef", ObjcTYPEDEF},
122 {"struct", ObjcSTRUCT},
123 {"enum", ObjcENUM},
124 {"@implementation", ObjcIMPLEMENTATION},
125 {"@interface", ObjcINTERFACE},
126 {"@protocol", ObjcPROTOCOL},
127 {"@encode", ObjcENCODE},
128 {"@property", ObjcPROPERTY},
129 {"@synchronized", ObjcSYNCHRONIZED},
130 {"@selector", ObjcSELECTOR},
131 {"@end", ObjcEND},
132 {"@defs", ObjcDEFS},
133 {"@class", ObjcCLASS},
134 {"@private", ObjcPRIVATE},
135 {"@package", ObjcPACKAGE},
136 {"@public", ObjcPUBLIC},
137 {"@protected", ObjcPROTECTED},
138 {"@synthesize", ObjcSYNTHESIZE},
139 {"@dynamic", ObjcDYNAMIC},
140 {"@optional", ObjcOPTIONAL},
141 {"@required", ObjcREQUIRED},
142 };
143
144 static langType Lang_ObjectiveC;
145
146 /*//////////////////////////////////////////////////////////////////
147 //// lexingInit */
148 typedef struct _lexingState {
149 vString *name; /* current parsed identifier/operator */
150 const unsigned char *cp; /* position in stream */
151 } lexingState;
152
153 static void initKeywordHash (void)
154 {
155 const size_t count = sizeof (objcKeywordTable) / sizeof (objcKeywordDesc);
156 size_t i;
157
158 for (i = 0; i < count; ++i)
159 {
160 addKeyword (objcKeywordTable[i].name, Lang_ObjectiveC,
161 (int) objcKeywordTable[i].id);
162 }
163 }
164
165 /*//////////////////////////////////////////////////////////////////////
166 //// Lexing */
167 static boolean isNum (char c)
168 {
169 return c >= '0' && c <= '9';
170 }
171
172 static boolean isLowerAlpha (char c)
173 {
174 return c >= 'a' && c <= 'z';
175 }
176
177 static boolean isUpperAlpha (char c)
178 {
179 return c >= 'A' && c <= 'Z';
180 }
181
182 static boolean isAlpha (char c)
183 {
184 return isLowerAlpha (c) || isUpperAlpha (c);
185 }
186
187 static boolean isIdent (char c)
188 {
189 return isNum (c) || isAlpha (c) || c == '_';
190 }
191
192 static boolean isSpace (char c)
193 {
194 return c == ' ' || c == '\t';
195 }
196
197 /* return true if it end with an end of line */
198 static void eatWhiteSpace (lexingState * st)
199 {
200 const unsigned char *cp = st->cp;
201 while (isSpace (*cp))
202 cp++;
203
204 st->cp = cp;
205 }
206
207 static void eatString (lexingState * st)
208 {
209 boolean lastIsBackSlash = FALSE;
210 boolean unfinished = TRUE;
211 const unsigned char *c = st->cp + 1;
212
213 while (unfinished)
214 {
215 /* end of line should never happen.
216 * we tolerate it */
217 if (c == NULL || c[0] == '\0')
218 break;
219 else if (*c == '"' && !lastIsBackSlash)
220 unfinished = FALSE;
221 else
222 lastIsBackSlash = *c == '\\';
223
224 c++;
225 }
226
227 st->cp = c;
228 }
229
230 static void eatComment (lexingState * st)
231 {
232 boolean unfinished = TRUE;
233 boolean lastIsStar = FALSE;
234 const unsigned char *c = st->cp + 2;
235
236 while (unfinished)
237 {
238 /* we've reached the end of the line..
239 * so we have to reload a line... */
240 if (c == NULL || *c == '\0')
241 {
242 st->cp = fileReadLine ();
243 /* WOOPS... no more input...
244 * we return, next lexing read
245 * will be null and ok */
246 if (st->cp == NULL)
247 return;
248 c = st->cp;
249 }
250 /* we've reached the end of the comment */
251 else if (*c == '/' && lastIsStar)
252 unfinished = FALSE;
253 else
254 {
255 lastIsStar = '*' == *c;
256 c++;
257 }
258 }
259
260 st->cp = c;
261 }
262
263 static void readIdentifier (lexingState * st)
264 {
265 const unsigned char *p;
266 vStringClear (st->name);
267
268 /* first char is a simple letter */
269 if (isAlpha (*st->cp) || *st->cp == '_')
270 vStringPut (st->name, (int) *st->cp);
271
272 /* Go till you get identifier chars */
273 for (p = st->cp + 1; isIdent (*p); p++)
274 vStringPut (st->name, (int) *p);
275
276 st->cp = p;
277
278 vStringTerminate (st->name);
279 }
280
281 /* read the @something directives */
282 static void readIdentifierObjcDirective (lexingState * st)
283 {
284 const unsigned char *p;
285 vStringClear (st->name);
286
287 /* first char is a simple letter */
288 if (*st->cp == '@')
289 vStringPut (st->name, (int) *st->cp);
290
291 /* Go till you get identifier chars */
292 for (p = st->cp + 1; isIdent (*p); p++)
293 vStringPut (st->name, (int) *p);
294
295 st->cp = p;
296
297 vStringTerminate (st->name);
298 }
299
300 /* The lexer is in charge of reading the file.
301 * Some of sub-lexer (like eatComment) also read file.
302 * lexing is finished when the lexer return Tok_EOF */
303 static objcKeyword lex (lexingState * st)
304 {
305 int retType;
306
307 /* handling data input here */
308 while (st->cp == NULL || st->cp[0] == '\0')
309 {
310 st->cp = fileReadLine ();
311 if (st->cp == NULL)
312 return Tok_EOF;
313
314 return Tok_EOL;
315 }
316
317 if (isAlpha (*st->cp))
318 {
319 readIdentifier (st);
320 retType = lookupKeyword (vStringValue (st->name), Lang_ObjectiveC);
321
322 if (retType == -1) /* If it's not a keyword */
323 {
324 return ObjcIDENTIFIER;
325 }
326 else
327 {
328 return retType;
329 }
330 }
331 else if (*st->cp == '@')
332 {
333 readIdentifierObjcDirective (st);
334 retType = lookupKeyword (vStringValue (st->name), Lang_ObjectiveC);
335
336 if (retType == -1) /* If it's not a keyword */
337 {
338 return Tok_any;
339 }
340 else
341 {
342 return retType;
343 }
344 }
345 else if (isSpace (*st->cp))
346 {
347 eatWhiteSpace (st);
348 return lex (st);
349 }
350 else
351 switch (*st->cp)
352 {
353 case '(':
354 st->cp++;
355 return Tok_PARL;
356
357 case '\\':
358 st->cp++;
359 return Tok_Backslash;
360
361 case '#':
362 st->cp++;
363 return Tok_Sharp;
364
365 case '/':
366 if (st->cp[1] == '*') /* ergl, a comment */
367 {
368 eatComment (st);
369 return lex (st);
370 }
371 else if (st->cp[1] == '/')
372 {
373 st->cp = NULL;
374 return lex (st);
375 }
376 else
377 {
378 st->cp++;
379 return Tok_any;
380 }
381 break;
382
383 case ')':
384 st->cp++;
385 return Tok_PARR;
386 case '{':
387 st->cp++;
388 return Tok_CurlL;
389 case '}':
390 st->cp++;
391 return Tok_CurlR;
392 case '[':
393 st->cp++;
394 return Tok_SQUAREL;
395 case ']':
396 st->cp++;
397 return Tok_SQUARER;
398 case ',':
399 st->cp++;
400 return Tok_COMA;
401 case ';':
402 st->cp++;
403 return Tok_semi;
404 case ':':
405 st->cp++;
406 return Tok_dpoint;
407 case '"':
408 eatString (st);
409 return Tok_any;
410 case '+':
411 st->cp++;
412 return Tok_PLUS;
413 case '-':
414 st->cp++;
415 return Tok_MINUS;
416
417 default:
418 st->cp++;
419 break;
420 }
421
422 /* default return if nothing is recognized,
423 * shouldn't happen, but at least, it will
424 * be handled without destroying the parsing. */
425 return Tok_any;
426 }
427
428 /*//////////////////////////////////////////////////////////////////////
429 //// Parsing */
430 typedef void (*parseNext) (vString * const ident, objcToken what);
431
432 /********** Helpers */
433 /* This variable hold the 'parser' which is going to
434 * handle the next token */
•435 static parseNext toDoNext;
436
437 /* Special variable used by parser eater to
438 * determine which action to put after their
439 * job is finished. */
•440 static parseNext comeAfter;
441
442 /* Used by some parsers detecting certain token
443 * to revert to previous parser. */
•444 static parseNext fallback;
445
446
447 /********** Grammar */
448 static void globalScope (vString * const ident, objcToken what);
449 static void parseMethods (vString * const ident, objcToken what);
450 static void parseImplemMethods (vString * const ident, objcToken what);
451 static vString *tempName = NULL;
452 static vString *parentName = NULL;
453 static objcKind parentType = K_INTERFACE;
454
455 /* used to prepare tag for OCaml, just in case their is a need to
456 * add additional information to the tag. */
457 static void prepareTag (tagEntryInfo * tag, vString const *name, objcKind kind)
458 {
459 initTagEntry (tag, vStringValue (name));
460 tag->kindName = ObjcKinds[kind].name;
461 tag->kind = ObjcKinds[kind].letter;
462
463 if (parentName != NULL)
464 {
465 tag->extensionFields.scope[0] = ObjcKinds[parentType].name;
466 tag->extensionFields.scope[1] = vStringValue (parentName);
467 }
468 }
469
470 void pushEnclosingContext (const vString * parent, objcKind type)
471 {
472 vStringCopy (parentName, parent);
473 parentType = type;
474 }
475
476 void popEnclosingContext ()
477 {
478 vStringClear (parentName);
479 }
480
481 /* Used to centralise tag creation, and be able to add
482 * more information to it in the future */
483 static void addTag (vString * const ident, int kind)
484 {
485 tagEntryInfo toCreate;
486 prepareTag (&toCreate, ident, kind);
487 makeTagEntry (&toCreate);
488 }
489
490 objcToken waitedToken, fallBackToken;
491
492 /* Ignore everything till waitedToken and jump to comeAfter.
493 * If the "end" keyword is encountered break, doesn't remember
494 * why though. */
495 static void tillToken (vString * const UNUSED (ident), objcToken what)
496 {
497 if (what == waitedToken)
498 toDoNext = comeAfter;
499 }
500
501 static void tillTokenOrFallBack (vString * const UNUSED (ident), objcToken what)
502 {
503 if (what == waitedToken)
504 toDoNext = comeAfter;
505 else if (what == fallBackToken)
506 {
507 toDoNext = fallback;
508 }
509 }
510
511 static void ignoreBalanced (vString * const UNUSED (ident), objcToken what)
512 {
513 static int count = 0;
514
515 switch (what)
516 {
517 case Tok_PARL:
518 case Tok_CurlL:
519 case Tok_SQUAREL:
520 count++;
521 break;
522
523 case Tok_PARR:
524 case Tok_CurlR:
525 case Tok_SQUARER:
526 count--;
527 break;
528
529 default:
530 /* don't care */
531 break;
532 }
533
534 if (count == 0)
535 toDoNext = comeAfter;
536 }
537
538 static void parseFields (vString * const ident, objcToken what)
539 {
540 switch (what)
541 {
542 case Tok_CurlR:
543 toDoNext = &parseMethods;
544 break;
545
546 case Tok_SQUAREL:
547 case Tok_PARL:
548 toDoNext = &ignoreBalanced;
549 comeAfter = &parseFields;
550 break;
551
552 // we got an identifier, keep track
553 // of it
554 case ObjcIDENTIFIER:
555 vStringCopy (tempName, ident);
556 break;
557
558 // our last kept identifier must be our
559 // variable name =)
560 case Tok_semi:
561 addTag (tempName, K_FIELD);
562 vStringClear (tempName);
563 break;
564
565 default:
566 /* NOTHING */
567 break;
568 }
569 }
570
571 objcKind methodKind;
572
573
574 static vString *fullMethodName;
575 static vString *prevIdent;
576
577 static void parseMethodsName (vString * const ident, objcToken what)
578 {
579 switch (what)
580 {
581 case Tok_PARL:
582 toDoNext = &tillToken;
583 comeAfter = &parseMethodsName;
584 waitedToken = Tok_PARR;
585 break;
586
587 case Tok_dpoint:
588 vStringCat (fullMethodName, prevIdent);
589 vStringCatS (fullMethodName, ":");
590 vStringClear (prevIdent);
591 break;
592
593 case ObjcIDENTIFIER:
594 vStringCopy (prevIdent, ident);
595 break;
596
597 case Tok_CurlL:
598 case Tok_semi:
599 // method name is not simple
600 if (vStringLength (fullMethodName) != '\0')
601 {
602 addTag (fullMethodName, methodKind);
603 vStringClear (fullMethodName);
604 }
605 else
606 addTag (prevIdent, methodKind);
607
608 toDoNext = &parseMethods;
609 parseImplemMethods (ident, what);
610 vStringClear (prevIdent);
611 break;
612
613 default:
614 break;
615 }
616 }
617
618 static void parseMethodsImplemName (vString * const ident, objcToken what)
619 {
620 switch (what)
621 {
622 case Tok_PARL:
623 toDoNext = &tillToken;
624 comeAfter = &parseMethodsImplemName;
625 waitedToken = Tok_PARR;
626 break;
627
628 case Tok_dpoint:
629 vStringCat (fullMethodName, prevIdent);
630 vStringCatS (fullMethodName, ":");
631 vStringClear (prevIdent);
632 break;
633
634 case ObjcIDENTIFIER:
635 vStringCopy (prevIdent, ident);
636 break;
637
638 case Tok_CurlL:
639 case Tok_semi:
640 // method name is not simple
641 if (vStringLength (fullMethodName) != '\0')
642 {
643 addTag (fullMethodName, methodKind);
644 vStringClear (fullMethodName);
645 }
646 else
647 addTag (prevIdent, methodKind);
648
649 toDoNext = &parseImplemMethods;
650 parseImplemMethods (ident, what);
651 vStringClear (prevIdent);
652 break;
653
654 default:
655 break;
656 }
657 }
658
659 static void parseImplemMethods (vString * const ident, objcToken what)
660 {
661 switch (what)
662 {
663 case Tok_PLUS: /* + */
664 toDoNext = &parseMethodsImplemName;
665 methodKind = K_CLASSMETHOD;
666 break;
667
668 case Tok_MINUS: /* - */
669 toDoNext = &parseMethodsImplemName;
670 methodKind = K_METHOD;
671 break;
672
673 case ObjcEND: /* @end */
674 popEnclosingContext ();
675 toDoNext = &globalScope;
676 break;
677
678 case Tok_CurlL: /* { */
679 toDoNext = &ignoreBalanced;
680 ignoreBalanced (ident, what);
681 comeAfter = &parseImplemMethods;
682 break;
683
684 default:
685 break;
686 }
687 }
688
689 static void parseProperty (vString * const ident, objcToken what)
690 {
691 switch (what)
692 {
693 case Tok_PARL:
694 toDoNext = &tillToken;
695 comeAfter = &parseProperty;
696 waitedToken = Tok_PARR;
697 break;
698
699 // we got an identifier, keep track
700 // of it
701 case ObjcIDENTIFIER:
702 vStringCopy (tempName, ident);
703 break;
704
705 // our last kept identifier must be our
706 // variable name =)
707 case Tok_semi:
708 addTag (tempName, K_PROPERTY);
709 vStringClear (tempName);
710 break;
711
712 default:
713 break;
714 }
715 }
716
717 static void parseMethods (vString * const UNUSED (ident), objcToken what)
718 {
719 switch (what)
720 {
721 case Tok_PLUS: /* + */
722 toDoNext = &parseMethodsName;
723 methodKind = K_CLASSMETHOD;
724 break;
725
726 case Tok_MINUS: /* - */
727 toDoNext = &parseMethodsName;
728 methodKind = K_METHOD;
729 break;
730
731 case ObjcPROPERTY:
732 toDoNext = &parseProperty;
733 break;
734
735 case ObjcEND: /* @end */
736 popEnclosingContext ();
737 toDoNext = &globalScope;
738 break;
739
740 case Tok_CurlL: /* { */
741 toDoNext = &parseFields;
742 break;
743
744 default:
745 break;
746 }
747 }
748
749
750 static void parseProtocol (vString * const ident, objcToken what)
751 {
752 if (what == ObjcIDENTIFIER)
753 {
754 pushEnclosingContext (ident, K_PROTOCOL);
755 addTag (ident, K_PROTOCOL);
756 }
757 toDoNext = &parseMethods;
758 }
759
760 static void parseImplementation (vString * const ident, objcToken what)
761 {
762 if (what == ObjcIDENTIFIER)
763 {
764 addTag (ident, K_IMPLEMENTATION);
765 pushEnclosingContext (ident, K_IMPLEMENTATION);
766 }
767 toDoNext = &parseImplemMethods;
768 }
769
770 static void parseInterface (vString * const ident, objcToken what)
771 {
772 if (what == ObjcIDENTIFIER)
773 {
774 addTag (ident, K_INTERFACE);
775 pushEnclosingContext (ident, K_INTERFACE);
776 }
777
778 toDoNext = &parseMethods;
779 }
780
781 static void parseStructMembers (vString * const ident, objcToken what)
782 {
783 static parseNext prev = NULL;
784
785 if (prev != NULL)
786 {
787 comeAfter = prev;
788 prev = NULL;
789 }
790
791 switch (what)
792 {
793 case ObjcIDENTIFIER:
794 vStringCopy (tempName, ident);
795 break;
796
797 case Tok_semi: /* ';' */
798 addTag (tempName, K_FIELD);
799 vStringClear (tempName);
800 break;
801
802 // some types are complex, the only one
803 // we will loose is the function type.
804 case Tok_CurlL: /* '{' */
805 case Tok_PARL: /* '(' */
806 case Tok_SQUAREL: /* '[' */
807 toDoNext = &ignoreBalanced;
808 prev = comeAfter;
809 comeAfter = &parseStructMembers;
810 ignoreBalanced (ident, what);
811 break;
812
813 case Tok_CurlR:
814 toDoNext = comeAfter;
815 break;
816
817 default:
818 /* don't care */
819 break;
820 }
821 }
822
823 /* Called just after the struct keyword */
824 static void parseStruct (vString * const ident, objcToken what)
825 {
826 static boolean gotName = FALSE;
827
828 switch (what)
829 {
830 case ObjcIDENTIFIER:
831 if (!gotName)
832 {
833 addTag (ident, K_STRUCT);
834 pushEnclosingContext (ident, K_STRUCT);
835 gotName = TRUE;
836 }
837 else
838 {
839 gotName = FALSE;
840 popEnclosingContext ();
841 toDoNext = comeAfter;
842 comeAfter (ident, what);
843 }
844 break;
845
846 case Tok_CurlL:
847 toDoNext = &parseStructMembers;
848 break;
849
850 /* maybe it was just a forward declaration
851 * in which case, we pop the context */
852 case Tok_semi:
853 if (gotName)
854 popEnclosingContext ();
855
856 toDoNext = comeAfter;
857 comeAfter (ident, what);
858 break;
859
860 default:
861 /* we don't care */
862 break;
863 }
864 }
865
866 /* Parse enumeration members, ignoring potential initialization */
867 static void parseEnumFields (vString * const ident, objcToken what)
868 {
869 static parseNext prev = NULL;
870
871 if (prev != NULL)
872 {
873 comeAfter = prev;
874 prev = NULL;
875 }
876
877 switch (what)
878 {
879 case ObjcIDENTIFIER:
880 addTag (ident, K_ENUM);
881 prev = comeAfter;
882 waitedToken = Tok_COMA;
883 /* last item might not have a coma */
884 fallBackToken = Tok_CurlR;
885 fallback = comeAfter;
886 comeAfter = parseEnumFields;
887 toDoNext = &tillTokenOrFallBack;
888 break;
889
890 case Tok_CurlR:
891 toDoNext = comeAfter;
892 popEnclosingContext ();
893 break;
894
895 default:
896 /* don't care */
897 break;
898 }
899 }
900
901 /* parse enum ... { ... */
902 static void parseEnum (vString * const ident, objcToken what)
903 {
904 static boolean named = FALSE;
905
906 switch (what)
907 {
908 case ObjcIDENTIFIER:
909 if (!named)
910 {
911 addTag (ident, K_ENUM);
912 pushEnclosingContext (ident, K_ENUM);
913 named = TRUE;
914 }
915 else
916 {
917 named = FALSE;
918 popEnclosingContext ();
919 toDoNext = comeAfter;
920 comeAfter (ident, what);
921 }
922 break;
923
924 case Tok_CurlL: /* '{' */
925 toDoNext = &parseEnumFields;
926 named = FALSE;
927 break;
928
929 case Tok_semi: /* ';' */
930 if (named)
931 popEnclosingContext ();
932 toDoNext = comeAfter;
933 comeAfter (ident, what);
934 break;
935
936 default:
937 /* don't care */
938 break;
939 }
940 }
941
942 /* Parse something like
943 * typedef .... ident ;
944 * ignoring the defined type but in the case of struct,
945 * in which case struct are parsed.
946 */
947 static void parseTypedef (vString * const ident, objcToken what)
948 {
949 switch (what)
950 {
951 case ObjcSTRUCT:
952 toDoNext = &parseStruct;
953 comeAfter = &parseTypedef;
954 break;
955
956 case ObjcENUM:
957 toDoNext = &parseEnum;
958 comeAfter = &parseTypedef;
959 break;
960
961 case ObjcIDENTIFIER:
962 vStringCopy (tempName, ident);
963 break;
964
965 case Tok_semi: /* ';' */
966 addTag (tempName, K_TYPEDEF);
967 vStringClear (tempName);
968 toDoNext = &globalScope;
969 break;
970
971 default:
972 /* we don't care */
973 break;
974 }
975 }
976
977 static void ignorePreprocStuff (vString * const UNUSED (ident), objcToken what)
978 {
979 static boolean escaped = FALSE;
980
981 switch (what)
982 {
983 case Tok_Backslash:
984 escaped = TRUE;
985 break;
986
987 case Tok_EOL:
988 if (escaped)
989 {
990 escaped = FALSE;
991 }
992 else
993 {
994 toDoNext = &globalScope;
995 }
996 break;
997
998 default:
999 escaped = FALSE;
1000 break;
1001 }
1002 }
1003
1004 static void parseMacroName (vString * const ident, objcToken what)
1005 {
1006 if (what == ObjcIDENTIFIER)
1007 addTag (ident, K_MACRO);
1008
1009 toDoNext = &ignorePreprocStuff;
1010 }
1011
1012 static void parsePreproc (vString * const ident, objcToken what)
1013 {
1014 switch (what)
1015 {
1016 case ObjcIDENTIFIER:
1017 if (strcmp (vStringValue (ident), "define") == 0)
1018 toDoNext = &parseMacroName;
1019 else
1020 toDoNext = &ignorePreprocStuff;
1021 break;
1022
1023 default:
1024 toDoNext = &ignorePreprocStuff;
1025 break;
1026 }
1027 }
1028
1029 /* Handle the "strong" top levels, all 'big' declarations
1030 * happen here */
1031 static void globalScope (vString * const ident, objcToken what)
1032 {
1033 switch (what)
1034 {
1035 case Tok_Sharp:
1036 toDoNext = &parsePreproc;
1037 break;
1038
1039 case ObjcSTRUCT:
1040 toDoNext = &parseStruct;
1041 comeAfter = &globalScope;
1042 break;
1043
1044 case ObjcIDENTIFIER:
1045 /* we keep track of the identifier if we
1046 * come across a function. */
1047 vStringCopy (tempName, ident);
1048 break;
1049
1050 case Tok_PARL:
1051 /* if we find an opening parenthesis it means we
1052 * found a function (or a macro...) */
1053 addTag (tempName, K_FUNCTION);
1054 vStringClear (tempName);
1055 comeAfter = &globalScope;
1056 toDoNext = &ignoreBalanced;
1057 ignoreBalanced (ident, what);
1058 break;
1059
1060 case ObjcINTERFACE:
1061 toDoNext = &parseInterface;
1062 break;
1063
1064 case ObjcIMPLEMENTATION:
1065 toDoNext = &parseImplementation;
1066 break;
1067
1068 case ObjcPROTOCOL:
1069 toDoNext = &parseProtocol;
1070 break;
1071
1072 case ObjcTYPEDEF:
1073 toDoNext = parseTypedef;
1074 comeAfter = &globalScope;
1075 break;
1076
1077 case Tok_CurlL:
1078 comeAfter = &globalScope;
1079 toDoNext = &ignoreBalanced;
1080 ignoreBalanced (ident, what);
1081 break;
1082
1083 case ObjcEND:
1084 case ObjcPUBLIC:
1085 case ObjcPROTECTED:
1086 case ObjcPRIVATE:
1087
1088 default:
1089 /* we don't care */
1090 break;
1091 }
1092 }
1093
1094 /*////////////////////////////////////////////////////////////////
1095 //// Deal with the system */
1096
1097 static void findObjcTags (void)
1098 {
1099 vString *name = vStringNew ();
1100 lexingState st;
1101 objcToken tok;
1102
1103 parentName = vStringNew ();
1104 tempName = vStringNew ();
1105 fullMethodName = vStringNew ();
1106 prevIdent = vStringNew ();
1107
1108 st.name = vStringNew ();
1109 st.cp = fileReadLine ();
1110 toDoNext = &globalScope;
1111 tok = lex (&st);
1112 while (tok != Tok_EOF)
1113 {
1114 (*toDoNext) (st.name, tok);
1115 tok = lex (&st);
1116 }
1117
1118 vStringDelete (name);
1119 vStringDelete (parentName);
1120 vStringDelete (tempName);
1121 vStringDelete (fullMethodName);
1122 vStringDelete (prevIdent);
1123 parentName = NULL;
1124 tempName = NULL;
1125 prevIdent = NULL;
1126 fullMethodName = NULL;
1127 }
1128
1129 static void objcInitialize (const langType language)
1130 {
1131 Lang_ObjectiveC = language;
1132
1133 initKeywordHash ();
1134 }
1135
1136 extern parserDefinition *ObjcParser (void)
1137 {
1138 static const char *const extensions[] = { "m", "h", NULL };
1139 parserDefinition *def = parserNew ("ObjectiveC");
1140 def->kinds = ObjcKinds;
1141 def->kindCount = KIND_COUNT (ObjcKinds);
1142 def->extensions = extensions;
1143 def->parser = findObjcTags;
1144 def->initialize = objcInitialize;
1145
1146 return def;
1147 }