Debian Codegraph @

plugins/symbolbrowser/symbols/parsers/parserex.cpp

raw symbols SCIP
1 /***************************************************************************
2 A SymbolBrowser plugin for JuffEd editor.
3
4 Copyright: 2010 Aleksander Sokoloff <[email protected]>
5
6 Some of the code based on a ctags project http://ctags.sourceforge.net/
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License.
10 version 2 as published by the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have rceived a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 ***************************************************************************/
21
22 #include "parserex.h"
23
24 #define isspacetab(c) ((c) == SPACE || (c) == TAB)
25 #define stringMatch(s1,s2) (strcmp (s1,s2) == 0)
26
27
28 /*****************************************************************************
29 *****************************************************************************/
30 ParserEx::ParserEx() : Parser() {
31 BraceFormat = false;
32 ungetch = '\0'; /* ungetch characters */
33 ungetch2 = '\0'; /* ungetch characters */
34 resolveRequired = false; /* resolveRequired */
35 hasAtLiteralStrings = false; /* hasAtLiteralStrings */
36
37
38 directive.state = DRCTV_NONE; /* current directive being processed */
39 directive.accept = false; /* is a directive syntactically permitted? */
40 directive.name = NULL; /* macro name */
41 directive.nestLevel = 0; /* level 0 is not used */
42
43 for (int i=0; i<MaxCppNestingLevel; ++i ) {
44 directive.ifdef[i].ignoreAllBranches = false;
45 directive.ifdef[i].singleBranch = false;
46 directive.ifdef[i].branchChosen = false;
47 directive.ifdef[i].ignoring = false;
48 }
49 }
50
51
52 /*****************************************************************************
53 *****************************************************************************/
54 ParserEx::~ParserEx() {
55
56 }
57
58 /*****************************************************************************
59 *****************************************************************************/
60 bool ParserEx::isBraceFormat (void) {
61 return BraceFormat;
62 }
63
64
65 /*****************************************************************************
66 *****************************************************************************/
67 unsigned int ParserEx::getDirectiveNestLevel (void) {
68 return directive.nestLevel;
69 }
70
71
72 /*****************************************************************************
73 *****************************************************************************/
74 void ParserEx::cppInit (const bool state, const bool hasAtLiteralStrings) {
75 BraceFormat = state;
76
77 ungetch = '\0';
78 ungetch2 = '\0';
79 resolveRequired = false;
80 this->hasAtLiteralStrings = hasAtLiteralStrings;
81
82 directive.state = DRCTV_NONE;
83 directive.accept = true;
84 directive.nestLevel = 0;
85
86 directive.ifdef [0].ignoreAllBranches = false;
87 directive.ifdef [0].singleBranch = false;
88 directive.ifdef [0].branchChosen = false;
89 directive.ifdef [0].ignoring = false;
90
91 if (directive.name == NULL)
92 directive.name = vStringNew ();
93 else
94 vStringClear (directive.name);
95 }
96
97
98 /*****************************************************************************
99 *****************************************************************************/
100 void ParserEx::cppTerminate (void) {
101 if (directive.name != NULL)
102 {
103 vStringDelete (directive.name);
104 directive.name = NULL;
105 }
106 }
107
108
109 /*****************************************************************************
110 *****************************************************************************/
111 void ParserEx::cppBeginStatement (void){
112 resolveRequired = true;
113 }
114
115
116 /*****************************************************************************
117 *****************************************************************************/
118 void ParserEx::cppEndStatement (void) {
119 resolveRequired = false;
120 }
121
122
123 /*
124 * Scanning functions
125 *
126 * This section handles preprocessor directives. It strips out all
127 * directives and may emit a tag for #define directives.
128 */
129
130 /*****************************************************************************
131 This puts a character back into the input queue for the source File.
132 Up to two characters may be ungotten.
133 *****************************************************************************/
134 void ParserEx::cppUngetc (const int c)
135 {
136 Assert (ungetch2 == '\0');
137 ungetch2 = ungetch;
138 ungetch = c;
139 }
140
141 /*****************************************************************************
142 Reads a directive, whose first character is given by "c", into "name".
143 *****************************************************************************/
144 bool ParserEx::readDirective (int c, char *const name, unsigned int maxLength)
145 {
146 unsigned int i;
147
148 for (i = 0 ; i < maxLength - 1 ; ++i)
149 {
150 if (i > 0)
151 {
152 c = fileGetc ();
153 if (c == EOF || ! isalpha (c))
154 {
155 fileUngetc (c);
156 break;
157 }
158 }
159 name [i] = c;
160 }
161 name [i] = '\0'; /* null terminate */
162
163 return (bool) isspacetab(c);
164 }
165
166
167 /*****************************************************************************
168 Reads an identifier, whose first character is given by "c", into "tag",
169 together with the file location and corresponding line number.
170 *****************************************************************************/
171 void ParserEx::readIdentifier (int c, vString *const name)
172 {
173 vStringClear (name);
174 do
175 {
176 vStringPut (name, c);
177 } while (c = fileGetc (), (c != EOF && isident(c)));
178 fileUngetc (c);
179 vStringTerminate (name);
180 }
181
182
183 /*****************************************************************************
184 *****************************************************************************/
185 ParserEx::conditionalInfo *ParserEx::currentConditional (void)
186 {
187 return &directive.ifdef [directive.nestLevel];
188 }
189
190
191 /*****************************************************************************
192 *****************************************************************************/
193 bool ParserEx::isIgnore (void)
194 {
195 return directive.ifdef [directive.nestLevel].ignoring;
196 }
197
198
199 /*****************************************************************************
200 *****************************************************************************/
201 bool ParserEx::setIgnore (const bool ignore)
202 {
203 return directive.ifdef [directive.nestLevel].ignoring = ignore;
204 }
205
206
207 /*****************************************************************************
208 *****************************************************************************/
209 bool ParserEx::isIgnoreBranch (void)
210 {
211 conditionalInfo *const ifdef = currentConditional ();
212
213 /* Force a single branch if an incomplete statement is discovered
214 * en route. This may have allowed earlier branches containing complete
215 * statements to be followed, but we must follow no further branches.
216 */
217 if (resolveRequired && ! BraceFormat)
218 ifdef->singleBranch = true;
219
220 /* We will ignore this branch in the following cases:
221 *
222 * 1. We are ignoring all branches (conditional was within an ignored
223 * branch of the parent conditional)
224 * 2. A branch has already been chosen and either of:
225 * a. A statement was incomplete upon entering the conditional
226 * b. A statement is incomplete upon encountering a branch
227 */
228 return (bool) (ifdef->ignoreAllBranches ||
229 (ifdef->branchChosen && ifdef->singleBranch));
230 }
231
232
233 /*****************************************************************************
234 *****************************************************************************/
235 void ParserEx::chooseBranch (void)
236 {
237 if (! BraceFormat)
238 {
239 conditionalInfo *const ifdef = currentConditional ();
240
241 ifdef->branchChosen = (bool) (ifdef->singleBranch ||
242 resolveRequired);
243 }
244 }
245
246
247 /*****************************************************************************
248 Pushes one nesting level for an #if directive, indicating whether or not
249 the branch should be ignored and whether a branch has already been chosen.
250 *****************************************************************************/
251 bool ParserEx::pushConditional (const bool firstBranchChosen)
252 {
253 const bool ignoreAllBranches = isIgnore (); /* current ignore */
254 bool ignoreBranch = false;
255
256 if (directive.nestLevel < (unsigned int) MaxCppNestingLevel - 1)
257 {
258 conditionalInfo *ifdef;
259
260 ++directive.nestLevel;
261 ifdef = currentConditional ();
262
263 /* We take a snapshot of whether there is an incomplete statement in
264 * progress upon encountering the preprocessor conditional. If so,
265 * then we will flag that only a single branch of the conditional
266 * should be followed.
267 */
268 ifdef->ignoreAllBranches = ignoreAllBranches;
269 ifdef->singleBranch = resolveRequired;
270 ifdef->branchChosen = firstBranchChosen;
271 ifdef->ignoring = (bool) (ignoreAllBranches || (
272 ! firstBranchChosen && ! BraceFormat &&
273 (ifdef->singleBranch /*|| !Option.if0*/)));
274
275 ignoreBranch = ifdef->ignoring;
276 }
277 return ignoreBranch;
278 }
279
280
281 /*****************************************************************************
282 Pops one nesting level for an #endif directive.
283 *****************************************************************************/
284 bool ParserEx::popConditional (void)
285 {
286 if (directive.nestLevel > 0)
287 --directive.nestLevel;
288
289 return isIgnore ();
290 }
291
292
293 /*****************************************************************************
294 *****************************************************************************/
295 void ParserEx::makeDefineTag (const char *const name)
296 {
297 Q_UNUSED(name)
298 // qDebug() << "ParserEx::makeDefineTag" << name;
299 // const bool isFileScope = (bool) (! isHeaderFile ());
300 //
301 // if (includingDefineTags () &&
302 // (! isFileScope || Option.include.fileScope))
303 // {
304 // tagEntryInfo e;
305 // initTagEntry (&e, name);
306 // e.lineNumberEntry = (bool) (Option.locate != EX_PATTERN);
307 // e.isFileScope = isFileScope;
308 // e.truncateLine = true;
309 // e.kindName = "macro";
310 // e.kind = 'd';
311 // makeTagEntry (&e);
312 // }
313 }
314
315
316 /*****************************************************************************
317 *****************************************************************************/
318 void ParserEx::directiveDefine (const int c)
319 {
320 if (isident1 (c))
321 {
322 readIdentifier (c, directive.name);
323 if (! isIgnore ())
324 makeDefineTag (vStringValue (directive.name));
325 }
326 directive.state = DRCTV_NONE;
327 }
328
329
330 /*****************************************************************************
331 *****************************************************************************/
332 void ParserEx::directivePragma (int c)
333 {
334 if (isident1 (c))
335 {
336 readIdentifier (c, directive.name);
337 if (stringMatch (vStringValue (directive.name), "weak"))
338 {
339 /* generate macro tag for weak name */
340 do
341 {
342 c = fileGetc ();
343 } while (c == SPACE);
344 if (isident1 (c))
345 {
346 readIdentifier (c, directive.name);
347 makeDefineTag (vStringValue (directive.name));
348 }
349 }
350 }
351 directive.state = DRCTV_NONE;
352 }
353
354
355 /*****************************************************************************
356 *****************************************************************************/
357 bool ParserEx::directiveIf (const int c)
358 {
359 DebugStatement ( const bool ignore0 = isIgnore (); )
360 const bool ignore = pushConditional ((bool) (c != '0'));
361
362 directive.state = DRCTV_NONE;
363 DebugStatement ( debugCppNest (true, directive.nestLevel);
364 if (ignore != ignore0) debugCppIgnore (ignore); )
365
366 return ignore;
367 }
368
369
370 /*****************************************************************************
371 *****************************************************************************/
372 bool ParserEx::directiveHash (const int c)
373 {
374 bool ignore = false;
375 char directiveStr [MaxDirectiveName];
376 DebugStatement( const bool ignore0 = isIgnore (); )
377
378 readDirective (c, directiveStr, MaxDirectiveName);
379 if (stringMatch (directiveStr, "define"))
380 directive.state = DRCTV_DEFINE;
381 else if (stringMatch (directiveStr, "undef"))
382 directive.state = DRCTV_UNDEF;
383 else if (strncmp (directiveStr, "if", (size_t) 2) == 0)
384 directive.state = DRCTV_IF;
385 else if (stringMatch (directiveStr, "elif") ||
386 stringMatch (directiveStr, "else"))
387 {
388 ignore = setIgnore (isIgnoreBranch ());
389 if (! ignore && stringMatch (directiveStr, "else"))
390 chooseBranch ();
391 directive.state = DRCTV_NONE;
392 DebugStatement ( if (ignore != ignore0) debugCppIgnore (ignore); )
393 }
394 else if (stringMatch (directiveStr, "endif"))
395 {
396 DebugStatement ( debugCppNest (false, directive.nestLevel); )
397 ignore = popConditional ();
398 directive.state = DRCTV_NONE;
399 DebugStatement ( if (ignore != ignore0) debugCppIgnore (ignore); )
400 }
401 else if (stringMatch (directiveStr, "pragma"))
402 directive.state = DRCTV_PRAGMA;
403 else
404 directive.state = DRCTV_NONE;
405
406 return ignore;
407 }
408
409
410 /*****************************************************************************
411 Handles a pre-processor directive whose first character is given by "c".
412 *****************************************************************************/
413 bool ParserEx::handleDirective (const int c)
414 {
415 bool ignore = isIgnore ();
416
417 switch (directive.state)
418 {
419 case DRCTV_NONE: ignore = isIgnore (); break;
420 case DRCTV_DEFINE: directiveDefine (c); break;
421 case DRCTV_HASH: ignore = directiveHash (c); break;
422 case DRCTV_IF: ignore = directiveIf (c); break;
423 case DRCTV_PRAGMA: directivePragma (c); break;
424 case DRCTV_UNDEF: directiveDefine (c); break;
425 }
426 return ignore;
427 }
428
429
430 /*****************************************************************************
431 Called upon reading of a slash ('/') characters, determines whether a
432 comment is encountered, and its type.
433 *****************************************************************************/
434 ParserEx::Comment ParserEx::isComment (void)
435 {
436 Comment comment;
437 const int next = fileGetc ();
438
439 if (next == '*')
440 comment = COMMENT_C;
441 else if (next == '/')
442 comment = COMMENT_CPLUS;
443 else
444 {
445 fileUngetc (next);
446 comment = COMMENT_NONE;
447 }
448 return comment;
449 }
450
451
452 /*****************************************************************************
453 Skips over a C style comment. According to ANSI specification a comment
454 is treated as white space, so we perform this substitution.
455 *****************************************************************************/
456 int ParserEx::skipOverCComment (void)
457 {
458 int c = fileGetc ();
459
460 while (c != EOF)
461 {
462 if (c != '*')
463 c = fileGetc ();
464 else
465 {
466 const int next = fileGetc ();
467
468 if (next != '/')
469 c = next;
470 else
471 {
472 c = SPACE; /* replace comment with space */
473 break;
474 }
475 }
476 }
477 return c;
478 }
479
480 /*****************************************************************************
481 Skips over a C++ style comment.
482 *****************************************************************************/
483 int ParserEx::skipOverCplusComment (void)
484 {
485 int c;
486
487 while ((c = fileGetc ()) != EOF)
488 {
489 if (c == BACKSLASH)
490 fileGetc (); /* throw away next character, too */
491 else if (c == NEWLINE)
492 break;
493 }
494 return c;
495 }
496
497
498 /*****************************************************************************
499 Skips to the end of a string, returning a special character to
500 symbolically represent a generic string.
501 *****************************************************************************/
502 int ParserEx::skipToEndOfString (bool ignoreBackslash)
503 {
504 int c;
505
506 while ((c = fileGetc ()) != EOF)
507 {
508 if (c == BACKSLASH && ! ignoreBackslash)
509 fileGetc (); /* throw away next character, too */
510 else if (c == DOUBLE_QUOTE)
511 break;
512 }
513 return STRING_SYMBOL; /* symbolic representation of string */
514 }
515
516
517 /*****************************************************************************
518 Skips to the end of the three (possibly four) 'c' sequence, returning a
519 special character to symbolically represent a generic character.
520 Also detects Vera numbers that include a base specifier (ie. 'b1010).
521 *****************************************************************************/
522 int ParserEx::skipToEndOfChar (void)
523 {
524 int c;
525 int count = 0, veraBase = '\0';
526
527 while ((c = fileGetc ()) != EOF)
528 {
529 ++count;
530 if (c == BACKSLASH)
531 fileGetc (); /* throw away next character, too */
532 else if (c == SINGLE_QUOTE)
533 break;
534 else if (c == NEWLINE)
535 {
536 fileUngetc (c);
537 break;
538 }
539 else if (count == 1 && strchr ("DHOB", toupper (c)) != NULL)
540 veraBase = c;
541 else if (veraBase != '\0' && ! isalnum (c))
542 {
543 fileUngetc (c);
544 break;
545 }
546 }
547 return CHAR_SYMBOL; /* symbolic representation of character */
548 }
549
550
551 /*****************************************************************************
552 This function returns the next character, stripping out comments,
553 C pre-processor directives, and the contents of single and double
554 quoted strings. In short, strip anything which places a burden upon
555 the tokenizer.
556 *****************************************************************************/
557 extern int ParserEx::cppGetc (void)
558 {
559 boolean bDirective = false;
560 boolean ignore = false;
561 int c;
562
563 if (ungetch != '\0')
564 {
565 c = ungetch;
566 ungetch = ungetch2;
567 ungetch2 = '\0';
568 return c; /* return here to avoid re-calling debugPutc () */
569 }
570 else do
571 {
572 c = fileGetc ();
573 process:
574 switch (c)
575 {
576 case EOF:
577 ignore = false;
578 bDirective = false;
579 break;
580
581 case TAB:
582 case SPACE:
583 break; /* ignore most white space */
584
585 case NEWLINE:
586 if (bDirective && ! ignore)
587 bDirective = false;
588 directive.accept = true;
589 break;
590
591 case DOUBLE_QUOTE:
592 directive.accept = false;
593 c = skipToEndOfString (false);
594 break;
595
596 case '#':
597 if (directive.accept)
598 {
599 bDirective = true;
600 directive.state = DRCTV_HASH;
601 directive.accept = false;
602 }
603 break;
604
605 case SINGLE_QUOTE:
606 directive.accept = false;
607 c = skipToEndOfChar ();
608 break;
609
610 case '/':
611 {
612 const Comment comment = isComment ();
613
614 if (comment == COMMENT_C)
615 c = skipOverCComment ();
616 else if (comment == COMMENT_CPLUS)
617 {
618 c = skipOverCplusComment ();
619 if (c == NEWLINE)
620 fileUngetc (c);
621 }
622 else
623 directive.accept = false;
624 break;
625 }
626
627 case BACKSLASH:
628 {
629 int next = fileGetc ();
630
631 if (next == NEWLINE)
632 continue;
633 else if (next == '?')
634 cppUngetc (next);
635 else
636 fileUngetc (next);
637 break;
638 }
639
640 case '?':
641 {
642 int next = fileGetc ();
643 if (next != '?')
644 fileUngetc (next);
645 else
646 {
647 next = fileGetc ();
648 switch (next)
649 {
650 case '(': c = '['; break;
651 case ')': c = ']'; break;
652 case '<': c = '{'; break;
653 case '>': c = '}'; break;
654 case '/': c = BACKSLASH; goto process;
655 case '!': c = '|'; break;
656 case SINGLE_QUOTE: c = '^'; break;
657 case '-': c = '~'; break;
658 case '=': c = '#'; goto process;
659 default:
660 fileUngetc (next);
661 cppUngetc ('?');
662 break;
663 }
664 }
665 } break;
666
667 default:
668 if (c == '@' && hasAtLiteralStrings)
669 {
670 int next = fileGetc ();
671 if (next == DOUBLE_QUOTE)
672 {
673 directive.accept = false;
674 c = skipToEndOfString (true);
675 break;
676 }
677 }
678 directive.accept = false;
679 if (bDirective)
680 ignore = handleDirective (c);
681 break;
682 }
683 } while (bDirective || ignore);
684
685 DebugStatement ( debugPutc (DEBUG_CPP, c); )
686 DebugStatement ( if (c == NEWLINE)
687 debugPrintf (DEBUG_CPP, "%6ld: ", getInputLineNumber () + 1); )
688
689 return c;
690 }
691