Debian Codegraph @

flex.c

raw
1 /*
2 * $Id: flex.c 666 2008-05-15 17:47:31Z dfishburn $
3 *
4 * Copyright (c) 2008, David Fishburn
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * This module contains functions for generating tags for Adobe languages.
10 * There are a number of different ones, but this will begin with:
11 * Flex
12 * MXML files (*.mMacromedia XML)
13 * ActionScript files (*.as)
14 *
15 * Flex 3 language reference
16 * http://livedocs.adobe.com/flex/3/langref/index.html
17 */
18
19 /*
20 * INCLUDE FILES
21 */
22 #include "general.h" /* must always come first */
23 #include <ctype.h> /* to define isalpha () */
24 #include <setjmp.h>
25 #ifdef DEBUG
26 #include <stdio.h>
27 #endif
28
29 #include "debug.h"
30 #include "entry.h"
31 #include "keyword.h"
32 #include "parse.h"
33 #include "read.h"
34 #include "routines.h"
35 #include "vstring.h"
36
37 /*
38 * MACROS
39 */
40 #define isType(token,t) (boolean) ((token)->type == (t))
41 #define isKeyword(token,k) (boolean) ((token)->keyword == (k))
42
43 /*
44 * DATA DECLARATIONS
45 */
46
47 typedef enum eException { ExceptionNone, ExceptionEOF } exception_t;
48
49 /*
50 * Tracks class and function names already created
51 */
52 static stringList *ClassNames;
53 static stringList *FunctionNames;
54
55 /* Used to specify type of keyword.
56 */
57 typedef enum eKeywordId {
58 KEYWORD_NONE = -1,
59 KEYWORD_function,
60 KEYWORD_capital_function,
61 KEYWORD_object,
62 KEYWORD_capital_object,
63 KEYWORD_prototype,
64 KEYWORD_var,
65 KEYWORD_new,
66 KEYWORD_this,
67 KEYWORD_for,
68 KEYWORD_while,
69 KEYWORD_do,
70 KEYWORD_if,
71 KEYWORD_else,
72 KEYWORD_switch,
73 KEYWORD_try,
74 KEYWORD_catch,
75 KEYWORD_finally,
76 KEYWORD_public,
77 KEYWORD_private,
78 KEYWORD_static,
79 KEYWORD_class,
80 KEYWORD_id,
81 KEYWORD_name,
82 KEYWORD_script,
83 KEYWORD_cdata,
84 KEYWORD_mx,
85 KEYWORD_fx,
86 KEYWORD_override
87 } keywordId;
88
89 /* Used to determine whether keyword is valid for the token language and
90 * what its ID is.
91 */
92 typedef struct sKeywordDesc {
93 const char *name;
94 keywordId id;
95 } keywordDesc;
96
97 typedef enum eTokenType {
98 TOKEN_UNDEFINED,
99 TOKEN_CHARACTER,
100 TOKEN_CLOSE_PAREN,
101 TOKEN_SEMICOLON,
102 TOKEN_COLON,
103 TOKEN_COMMA,
104 TOKEN_KEYWORD,
105 TOKEN_OPEN_PAREN,
106 TOKEN_OPERATOR,
107 TOKEN_IDENTIFIER,
108 TOKEN_STRING,
109 TOKEN_PERIOD,
110 TOKEN_OPEN_CURLY,
111 TOKEN_CLOSE_CURLY,
112 TOKEN_EQUAL_SIGN,
113 TOKEN_EXCLAMATION,
114 TOKEN_FORWARD_SLASH,
115 TOKEN_OPEN_SQUARE,
116 TOKEN_CLOSE_SQUARE,
117 TOKEN_OPEN_MXML,
118 TOKEN_CLOSE_MXML,
119 TOKEN_CLOSE_SGML,
120 TOKEN_LESS_THAN,
121 TOKEN_GREATER_THAN,
122 TOKEN_QUESTION_MARK,
123 TOKEN_OPEN_NAMESPACE
124 } tokenType;
125
126 typedef struct sTokenInfo {
127 tokenType type;
128 keywordId keyword;
129 vString * string;
130 vString * scope;
131 unsigned long lineNumber;
132 fpos_t filePosition;
133 int nestLevel;
134 boolean ignoreTag;
135 boolean isClass;
136 } tokenInfo;
137
138 /*
139 * DATA DEFINITIONS
140 */
141
142 static langType Lang_js;
143
144 static jmp_buf Exception;
145
146 typedef enum {
147 FLEXTAG_FUNCTION,
148 FLEXTAG_CLASS,
149 FLEXTAG_METHOD,
150 FLEXTAG_PROPERTY,
151 FLEXTAG_VARIABLE,
152 FLEXTAG_MXTAG,
153 FLEXTAG_COUNT
154 } flexKind;
155
156 static kindOption FlexKinds [] = {
157 { TRUE, 'f', "function", "functions" },
158 { TRUE, 'c', "class", "classes" },
159 { TRUE, 'm', "method", "methods" },
160 { TRUE, 'p', "property", "properties" },
161 { TRUE, 'v', "variable", "global variables" },
162 { TRUE, 'x', "mxtag", "mxtags" }
163 };
164
165 static const keywordDesc FlexKeywordTable [] = {
166 /* keyword keyword ID */
167 { "function", KEYWORD_function },
168 { "Function", KEYWORD_capital_function },
169 { "object", KEYWORD_object },
170 { "Object", KEYWORD_capital_object },
171 { "prototype", KEYWORD_prototype },
172 { "var", KEYWORD_var },
173 { "new", KEYWORD_new },
174 { "this", KEYWORD_this },
175 { "for", KEYWORD_for },
176 { "while", KEYWORD_while },
177 { "do", KEYWORD_do },
178 { "if", KEYWORD_if },
179 { "else", KEYWORD_else },
180 { "switch", KEYWORD_switch },
181 { "try", KEYWORD_try },
182 { "catch", KEYWORD_catch },
183 { "finally", KEYWORD_finally },
184 { "public", KEYWORD_public },
185 { "private", KEYWORD_private },
186 { "static", KEYWORD_static },
187 { "class", KEYWORD_class },
188 { "id", KEYWORD_id },
189 { "name", KEYWORD_name },
190 { "script", KEYWORD_script },
191 { "cdata", KEYWORD_cdata },
192 { "mx", KEYWORD_mx },
193 { "fx", KEYWORD_fx },
194 { "override", KEYWORD_override }
195 };
196
197 /*
198 * FUNCTION DEFINITIONS
199 */
200
201 /* Recursive functions */
202 static void parseFunction (tokenInfo *const token);
203 static boolean parseBlock (tokenInfo *const token, tokenInfo *const parent);
204 static boolean parseLine (tokenInfo *const token);
205 static boolean parseActionScript (tokenInfo *const token);
206 static boolean parseMXML (tokenInfo *const token);
207
208 static boolean isIdentChar (const int c)
209 {
210 return (boolean)
211 (isalpha (c) || isdigit (c) || c == '$' ||
212 c == '@' || c == '_' || c == '#');
213 }
214
215 static void buildFlexKeywordHash (void)
216 {
217 const size_t count = sizeof (FlexKeywordTable) /
218 sizeof (FlexKeywordTable [0]);
219 size_t i;
220 for (i = 0 ; i < count ; ++i)
221 {
222 const keywordDesc* const p = &FlexKeywordTable [i];
223 addKeyword (p->name, Lang_js, (int) p->id);
224 }
225 }
226
227 static tokenInfo *newToken (void)
228 {
229 tokenInfo *const token = xMalloc (1, tokenInfo);
230
231 token->type = TOKEN_UNDEFINED;
232 token->keyword = KEYWORD_NONE;
233 token->string = vStringNew ();
234 token->scope = vStringNew ();
235 token->nestLevel = 0;
236 token->isClass = FALSE;
237 token->ignoreTag = FALSE;
238 token->lineNumber = getSourceLineNumber ();
239 token->filePosition = getInputFilePosition ();
240
241 return token;
242 }
243
244 static void deleteToken (tokenInfo *const token)
245 {
246 vStringDelete (token->string);
247 vStringDelete (token->scope);
248 eFree (token);
249 }
250
251 /*
252 * Tag generation functions
253 */
254
255 static void makeConstTag (tokenInfo *const token, const flexKind kind)
256 {
257 if (FlexKinds [kind].enabled && ! token->ignoreTag )
258 {
259 const char *const name = vStringValue (token->string);
260 tagEntryInfo e;
261 initTagEntry (&e, name);
262
263 e.lineNumber = token->lineNumber;
264 e.filePosition = token->filePosition;
265 e.kindName = FlexKinds [kind].name;
266 e.kind = FlexKinds [kind].letter;
267
268 makeTagEntry (&e);
269 }
270 }
271
272 static void makeFlexTag (tokenInfo *const token, flexKind kind)
273 {
274 vString * fulltag;
275
276 if (FlexKinds [kind].enabled && ! token->ignoreTag )
277 {
278 DebugStatement (
279 debugPrintf (DEBUG_PARSE
280 , "\n makeFlexTag start: token isClass:%d scope:%s name:%s\n"
281 , token->isClass
282 , vStringValue(token->scope)
283 , vStringValue(token->string)
284 );
285 );
286 if (kind == FLEXTAG_FUNCTION && token->isClass )
287 {
288 kind = FLEXTAG_METHOD;
289 }
290 /*
291 * If a scope has been added to the token, change the token
292 * string to include the scope when making the tag.
293 */
294 if ( vStringLength(token->scope) > 0 )
295 {
296 fulltag = vStringNew ();
297 vStringCopy(fulltag, token->scope);
298 vStringCatS (fulltag, ".");
299 vStringCatS (fulltag, vStringValue(token->string));
300 vStringTerminate(fulltag);
301 vStringCopy(token->string, fulltag);
302 vStringDelete (fulltag);
303 }
304 makeConstTag (token, kind);
305 }
306 }
307
308 static void makeClassTag (tokenInfo *const token)
309 {
310 vString * fulltag;
311
312 if ( ! token->ignoreTag )
313 {
314 fulltag = vStringNew ();
315 if (vStringLength (token->scope) > 0)
316 {
317 vStringCopy(fulltag, token->scope);
318 vStringCatS (fulltag, ".");
319 vStringCatS (fulltag, vStringValue(token->string));
320 }
321 else
322 {
323 vStringCopy(fulltag, token->string);
324 }
325 vStringTerminate(fulltag);
326 if ( ! stringListHas(ClassNames, vStringValue (fulltag)) )
327 {
328 stringListAdd (ClassNames, vStringNewCopy (fulltag));
329 makeFlexTag (token, FLEXTAG_CLASS);
330 }
331 vStringDelete (fulltag);
332 }
333 }
334
335 static void makeMXTag (tokenInfo *const token)
336 {
337 vString * fulltag;
338
339 if ( ! token->ignoreTag )
340 {
341 fulltag = vStringNew ();
342 if (vStringLength (token->scope) > 0)
343 {
344 vStringCopy(fulltag, token->scope);
345 vStringCatS (fulltag, ".");
346 vStringCatS (fulltag, vStringValue(token->string));
347 }
348 else
349 {
350 vStringCopy(fulltag, token->string);
351 }
352 vStringTerminate(fulltag);
353 makeFlexTag (token, FLEXTAG_MXTAG);
354 vStringDelete (fulltag);
355 }
356 }
357
358 static void makeFunctionTag (tokenInfo *const token)
359 {
360 vString * fulltag;
361
362 if ( ! token->ignoreTag )
363 {
364 fulltag = vStringNew ();
365 if (vStringLength (token->scope) > 0)
366 {
367 vStringCopy(fulltag, token->scope);
368 vStringCatS (fulltag, ".");
369 vStringCatS (fulltag, vStringValue(token->string));
370 }
371 else
372 {
373 vStringCopy(fulltag, token->string);
374 }
375 vStringTerminate(fulltag);
376 if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) )
377 {
378 stringListAdd (FunctionNames, vStringNewCopy (fulltag));
379 makeFlexTag (token, FLEXTAG_FUNCTION);
380 }
381 vStringDelete (fulltag);
382 }
383 }
384
385 /*
386 * Parsing functions
387 */
388
389 static void parseString (vString *const string, const int delimiter)
390 {
391 boolean end = FALSE;
392 while (! end)
393 {
394 int c = fileGetc ();
395 if (c == EOF)
396 end = TRUE;
397 else if (c == '\\')
398 {
399 c = fileGetc(); /* This maybe a ' or ". */
400 vStringPut(string, c);
401 }
402 else if (c == delimiter)
403 end = TRUE;
404 else
405 vStringPut (string, c);
406 }
407 vStringTerminate (string);
408 }
409
410 /* Read a C identifier beginning with "firstChar" and places it into
411 * "name".
412 */
413 static void parseIdentifier (vString *const string, const int firstChar)
414 {
415 int c = firstChar;
416 Assert (isIdentChar (c));
417 do
418 {
419 vStringPut (string, c);
420 c = fileGetc ();
421 } while (isIdentChar (c));
422 vStringTerminate (string);
423 if (!isspace (c))
424 fileUngetc (c); /* unget non-identifier character */
425 }
426
427 static void readToken (tokenInfo *const token)
428 {
429 int c;
430
431 token->type = TOKEN_UNDEFINED;
432 token->keyword = KEYWORD_NONE;
433 vStringClear (token->string);
434
435 getNextChar:
436 do
437 {
438 c = fileGetc ();
439 token->lineNumber = getSourceLineNumber ();
440 token->filePosition = getInputFilePosition ();
441 }
442 while (c == '\t' || c == ' ' || c == '\n');
443
444 switch (c)
445 {
446 case EOF: longjmp (Exception, (int)ExceptionEOF); break;
447 case '(': token->type = TOKEN_OPEN_PAREN; break;
448 case ')': token->type = TOKEN_CLOSE_PAREN; break;
449 case ';': token->type = TOKEN_SEMICOLON; break;
450 case ',': token->type = TOKEN_COMMA; break;
451 case '.': token->type = TOKEN_PERIOD; break;
452 case ':': token->type = TOKEN_COLON; break;
453 case '{': token->type = TOKEN_OPEN_CURLY; break;
454 case '}': token->type = TOKEN_CLOSE_CURLY; break;
455 case '=': token->type = TOKEN_EQUAL_SIGN; break;
456 case '[': token->type = TOKEN_OPEN_SQUARE; break;
457 case ']': token->type = TOKEN_CLOSE_SQUARE; break;
458 case '?': token->type = TOKEN_QUESTION_MARK; break;
459
460 case '\'':
461 case '"':
462 token->type = TOKEN_STRING;
463 parseString (token->string, c);
464 token->lineNumber = getSourceLineNumber ();
465 token->filePosition = getInputFilePosition ();
466 break;
467
468 case '\\':
469 c = fileGetc ();
470 if (c != '\\' && c != '"' && !isspace (c))
471 fileUngetc (c);
472 token->type = TOKEN_CHARACTER;
473 token->lineNumber = getSourceLineNumber ();
474 token->filePosition = getInputFilePosition ();
475 break;
476
477 case '/':
478 {
479 int d = fileGetc ();
480 if ( (d != '*') && /* is this the start of a comment? */
481 (d != '/') && /* is a one line comment? */
482 (d != '>') ) /* is this a close XML tag? */
483 {
484 fileUngetc (d);
485 token->type = TOKEN_FORWARD_SLASH;
486 token->lineNumber = getSourceLineNumber ();
487 token->filePosition = getInputFilePosition ();
488 }
489 else
490 {
491 if (d == '*')
492 {
493 do
494 {
495 fileSkipToCharacter ('*');
496 c = fileGetc ();
497 if (c == '/')
498 break;
499 else
500 fileUngetc (c);
501 } while (c != EOF && c != '\0');
502 goto getNextChar;
503 }
504 else if (d == '/') /* is this the start of a comment? */
505 {
506 fileSkipToCharacter ('\n');
507 goto getNextChar;
508 }
509 else if (d == '>') /* is this the start of a comment? */
510 {
511 token->type = TOKEN_CLOSE_SGML;
512 token->lineNumber = getSourceLineNumber ();
513 token->filePosition = getInputFilePosition ();
514 }
515 }
516 break;
517 }
518
519 case '<':
520 {
521 /*
522 * An XML comment looks like this
523 * <!-- anything over multiple lines -->
524 */
525 int d = fileGetc ();
526
527 if ( (d != '!' ) && /* is this the start of a comment? */
528 (d != '/' ) && /* is this the start of a closing mx tag */
529 (d != 'm' ) && /* is this the start of a mx tag */
530 (d != 'f' ) && /* is this the start of a fx tag */
531 (d != 's' ) ) /* is this the start of a spark tag */
532 {
533 fileUngetc (d);
534 token->type = TOKEN_LESS_THAN;
535 token->lineNumber = getSourceLineNumber ();
536 token->filePosition = getInputFilePosition ();
537 break;
538 }
539 else
540 {
541 if (d == '!')
542 {
543 int e = fileGetc ();
544 if ( e != '-' ) /* is this the start of a comment? */
545 {
546 fileUngetc (e);
547 fileUngetc (d);
548 token->type = TOKEN_LESS_THAN;
549 token->lineNumber = getSourceLineNumber ();
550 token->filePosition = getInputFilePosition ();
551 }
552 else
553 {
554 if (e == '-')
555 {
556 int f = fileGetc ();
557 if ( f != '-' ) /* is this the start of a comment? */
558 {
559 fileUngetc (f);
560 fileUngetc (e);
561 fileUngetc (d);
562 token->type = TOKEN_LESS_THAN;
563 token->lineNumber = getSourceLineNumber ();
564 token->filePosition = getInputFilePosition ();
565 }
566 else
567 {
568 if (f == '-')
569 {
570 do
571 {
572 fileSkipToCharacter ('-');
573 c = fileGetc ();
574 if (c == '-')
575 {
576 d = fileGetc ();
577 if (d == '>')
578 break;
579 else
580 {
581 fileUngetc (d);
582 fileUngetc (c);
583 }
584 break;
585 }
586 else
587 fileUngetc (c);
588 } while (c != EOF && c != '\0');
589 goto getNextChar;
590 }
591 }
592 }
593 }
594 }
595 else if (d == 'm' || d == 'f' || d == 's' )
596 {
597 int e = fileGetc ();
598 if ( (d == 'm' || d == 'f') && e != 'x' ) /* continuing an mx or fx tag */
599 {
600 fileUngetc (e);
601 fileUngetc (d);
602 token->type = TOKEN_LESS_THAN;
603 token->lineNumber = getSourceLineNumber ();
604 token->filePosition = getInputFilePosition ();
605 break;
606 }
607 else
608 {
609 if ( (d == 'm' || d == 'f') && e == 'x' )
610 {
611 int f = fileGetc ();
612 if ( f != ':' ) /* start of the tag */
613 {
614 fileUngetc (f);
615 fileUngetc (e);
616 fileUngetc (d);
617 token->type = TOKEN_LESS_THAN;
618 token->lineNumber = getSourceLineNumber ();
619 token->filePosition = getInputFilePosition ();
620 break;
621 }
622 else
623 {
624 token->type = TOKEN_OPEN_MXML;
625 token->lineNumber = getSourceLineNumber ();
626 token->filePosition = getInputFilePosition ();
627 break;
628 }
629 }
630 if ( d == 's' && e == ':') /* continuing a spark tag */
631 {
632 token->type = TOKEN_OPEN_MXML;
633 token->lineNumber = getSourceLineNumber ();
634 token->filePosition = getInputFilePosition ();
635 break;
636 }
637 else
638 {
639 fileUngetc (e);
640 fileUngetc (d);
641 token->type = TOKEN_LESS_THAN;
642 token->lineNumber = getSourceLineNumber ();
643 token->filePosition = getInputFilePosition ();
644 break;
645 }
646 }
647 }
648 else if (d == '/')
649 {
650 int e = fileGetc ();
651 if ( !(e == 'm' || e == 'f' || e == 's' ))
652 {
653 fileUngetc (e);
654 fileUngetc (d);
655 token->type = TOKEN_LESS_THAN;
656 token->lineNumber = getSourceLineNumber ();
657 token->filePosition = getInputFilePosition ();
658 break;
659 }
660 else
661 {
662 int f = fileGetc ();
663 if ( (e == 'm' || e == 'f') && f != 'x' ) /* continuing an mx or fx tag */
664 {
665 fileUngetc (f);
666 fileUngetc (e);
667 token->type = TOKEN_LESS_THAN;
668 token->lineNumber = getSourceLineNumber ();
669 token->filePosition = getInputFilePosition ();
670 break;
671 }
672 else
673 {
674 if (f == 'x')
675 {
676 int g = fileGetc ();
677 if ( g != ':' ) /* is this the start of a comment? */
678 {
679 fileUngetc (g);
680 fileUngetc (f);
681 fileUngetc (e);
682 token->type = TOKEN_LESS_THAN;
683 token->lineNumber = getSourceLineNumber ();
684 token->filePosition = getInputFilePosition ();
685 break;
686 }
687 else
688 {
689 token->type = TOKEN_CLOSE_MXML;
690 token->lineNumber = getSourceLineNumber ();
691 token->filePosition = getInputFilePosition ();
692 break;
693 }
694 }
695 if ( e == 's' && f == ':') /* continuing a spark tag */
696 {
697 token->type = TOKEN_CLOSE_MXML;
698 token->lineNumber = getSourceLineNumber ();
699 token->filePosition = getInputFilePosition ();
700 break;
701 }
702 else
703 {
704 fileUngetc (f);
705 fileUngetc (e);
706 token->type = TOKEN_LESS_THAN;
707 token->lineNumber = getSourceLineNumber ();
708 token->filePosition = getInputFilePosition ();
709 break;
710 }
711 }
712 }
713 }
714 }
715 break;
716 }
717
718 case '>':
719 token->type = TOKEN_GREATER_THAN;
720 token->lineNumber = getSourceLineNumber ();
721 token->filePosition = getInputFilePosition ();
722 break;
723
724 case '!':
725 token->type = TOKEN_EXCLAMATION;
726 /*token->lineNumber = getSourceLineNumber ();
727 token->filePosition = getInputFilePosition ();*/
728 break;
729
730 default:
731 if (! isIdentChar (c))
732 token->type = TOKEN_UNDEFINED;
733 else
734 {
735 parseIdentifier (token->string, c);
736 token->lineNumber = getSourceLineNumber ();
737 token->filePosition = getInputFilePosition ();
738 token->keyword = analyzeToken (token->string, Lang_js);
739 if (isKeyword (token, KEYWORD_NONE))
740 token->type = TOKEN_IDENTIFIER;
741 else
742 token->type = TOKEN_KEYWORD;
743 }
744 break;
745 }
746 }
747
748 static void copyToken (tokenInfo *const dest, tokenInfo *const src)
749 {
750 dest->nestLevel = src->nestLevel;
751 dest->lineNumber = src->lineNumber;
752 dest->filePosition = src->filePosition;
753 dest->type = src->type;
754 dest->keyword = src->keyword;
755 dest->isClass = src->isClass;
756 vStringCopy(dest->string, src->string);
757 vStringCopy(dest->scope, src->scope);
758 }
759
760 /*
761 * Token parsing functions
762 */
763
764 static void skipArgumentList (tokenInfo *const token)
765 {
766 int nest_level = 0;
767
768 /*
769 * Other databases can have arguments with fully declared
770 * datatypes:
771 * ( name varchar(30), text binary(10) )
772 * So we must check for nested open and closing parantheses
773 */
774
775 if (isType (token, TOKEN_OPEN_PAREN)) /* arguments? */
776 {
777 nest_level++;
778 while (! (isType (token, TOKEN_CLOSE_PAREN) && (nest_level == 0)))
779 {
780 readToken (token);
781 if (isType (token, TOKEN_OPEN_PAREN))
782 {
783 nest_level++;
784 }
785 if (isType (token, TOKEN_CLOSE_PAREN))
786 {
787 if (nest_level > 0)
788 {
789 nest_level--;
790 }
791 }
792 }
793 readToken (token);
794 }
795 }
796
797 static void skipArrayList (tokenInfo *const token)
798 {
799 int nest_level = 0;
800
801 /*
802 * Handle square brackets
803 * var name[1]
804 * So we must check for nested open and closing square brackets
805 */
806
807 if (isType (token, TOKEN_OPEN_SQUARE)) /* arguments? */
808 {
809 nest_level++;
810 while (! (isType (token, TOKEN_CLOSE_SQUARE) && (nest_level == 0)))
811 {
812 readToken (token);
813 if (isType (token, TOKEN_OPEN_SQUARE))
814 {
815 nest_level++;
816 }
817 if (isType (token, TOKEN_CLOSE_SQUARE))
818 {
819 if (nest_level > 0)
820 {
821 nest_level--;
822 }
823 }
824 }
825 readToken (token);
826 }
827 }
828
829 static void addContext (tokenInfo* const parent, const tokenInfo* const child)
830 {
831 if (vStringLength (parent->string) > 0)
832 {
833 vStringCatS (parent->string, ".");
834 }
835 vStringCatS (parent->string, vStringValue(child->string));
836 vStringTerminate(parent->string);
837 }
838
839 static void addToScope (tokenInfo* const token, vString* const extra)
840 {
841 if (vStringLength (token->scope) > 0)
842 {
843 vStringCatS (token->scope, ".");
844 }
845 vStringCatS (token->scope, vStringValue(extra));
846 vStringTerminate(token->scope);
847 }
848
849 /*
850 * Scanning functions
851 */
852
853 static void findCmdTerm (tokenInfo *const token)
854 {
855 /*
856 * Read until we find either a semicolon or closing brace.
857 * Any nested braces will be handled within.
858 */
859 while (! ( isType (token, TOKEN_SEMICOLON) ||
860 isType (token, TOKEN_CLOSE_CURLY) ) )
861 {
862 /* Handle nested blocks */
863 if ( isType (token, TOKEN_OPEN_CURLY))
864 {
865 parseBlock (token, token);
866 }
867 else if ( isType (token, TOKEN_OPEN_PAREN) )
868 {
869 skipArgumentList(token);
870 }
871 else
872 {
873 readToken (token);
874 }
875 }
876 }
877
878 static void parseSwitch (tokenInfo *const token)
879 {
880 /*
881 * switch (expression){
882 * case value1:
883 * statement;
884 * break;
885 * case value2:
886 * statement;
887 * break;
888 * default : statement;
889 * }
890 */
891
892 readToken (token);
893
894 if (isType (token, TOKEN_OPEN_PAREN))
895 {
896 skipArgumentList(token);
897 }
898
899 if (isType (token, TOKEN_OPEN_CURLY))
900 {
901 do
902 {
903 readToken (token);
904 } while (! (isType (token, TOKEN_CLOSE_SGML) ||
905 isType (token, TOKEN_CLOSE_MXML) ||
906 isType (token, TOKEN_CLOSE_CURLY) ||
907 isType (token, TOKEN_GREATER_THAN)) );
908 }
909
910 }
911
912 static void parseLoop (tokenInfo *const token)
913 {
914 /*
915 * Handles these statements
916 * for (x=0; x<3; x++)
917 * document.write("This text is repeated three times<br>");
918 *
919 * for (x=0; x<3; x++)
920 * {
921 * document.write("This text is repeated three times<br>");
922 * }
923 *
924 * while (number<5){
925 * document.write(number+"<br>");
926 * number++;
927 * }
928 *
929 * do{
930 * document.write(number+"<br>");
931 * number++;
932 * }
933 * while (number<5);
934 */
935
936 if (isKeyword (token, KEYWORD_for) || isKeyword (token, KEYWORD_while))
937 {
938 readToken(token);
939
940 if (isType (token, TOKEN_OPEN_PAREN))
941 {
942 /*
943 * Handle nameless functions, these will only
944 * be considered methods.
945 */
946 skipArgumentList(token);
947 }
948
949 if (isType (token, TOKEN_OPEN_CURLY))
950 {
951 /*
952 * This will be either a function or a class.
953 * We can only determine this by checking the body
954 * of the function. If we find a "this." we know
955 * it is a class, otherwise it is a function.
956 */
957 parseBlock (token, token);
958 }
959 else
960 {
961 parseLine(token);
962 }
963 }
964 else if (isKeyword (token, KEYWORD_do))
965 {
966 readToken(token);
967
968 if (isType (token, TOKEN_OPEN_CURLY))
969 {
970 /*
971 * This will be either a function or a class.
972 * We can only determine this by checking the body
973 * of the function. If we find a "this." we know
974 * it is a class, otherwise it is a function.
975 */
976 parseBlock (token, token);
977 }
978 else
979 {
980 parseLine(token);
981 }
982
983 readToken(token);
984
985 if (isKeyword (token, KEYWORD_while))
986 {
987 readToken(token);
988
989 if (isType (token, TOKEN_OPEN_PAREN))
990 {
991 /*
992 * Handle nameless functions, these will only
993 * be considered methods.
994 */
995 skipArgumentList(token);
996 }
997 }
998 }
999 }
1000
1001 static boolean parseIf (tokenInfo *const token)
1002 {
1003 boolean read_next_token = TRUE;
1004 /*
1005 * If statements have two forms
1006 * if ( ... )
1007 * one line;
1008 *
1009 * if ( ... )
1010 * statement;
1011 * else
1012 * statement
1013 *
1014 * if ( ... ) {
1015 * multiple;
1016 * statements;
1017 * }
1018 *
1019 *
1020 * if ( ... ) {
1021 * return elem
1022 * }
1023 *
1024 * This example if correctly written, but the
1025 * else contains only 1 statement without a terminator
1026 * since the function finishes with the closing brace.
1027 *
1028 * function a(flag){
1029 * if(flag)
1030 * test(1);
1031 * else
1032 * test(2)
1033 * }
1034 *
1035 * TODO: Deal with statements that can optional end
1036 * without a semi-colon. Currently this messes up
1037 * the parsing of blocks.
1038 * Need to somehow detect this has happened, and either
1039 * backup a token, or skip reading the next token if
1040 * that is possible from all code locations.
1041 *
1042 */
1043
1044 readToken (token);
1045
1046 if (isKeyword (token, KEYWORD_if))
1047 {
1048 /*
1049 * Check for an "else if" and consume the "if"
1050 */
1051 readToken (token);
1052 }
1053
1054 if (isType (token, TOKEN_OPEN_PAREN))
1055 {
1056 /*
1057 * Handle nameless functions, these will only
1058 * be considered methods.
1059 */
1060 skipArgumentList(token);
1061 }
1062
1063 if (isType (token, TOKEN_OPEN_CURLY))
1064 {
1065 /*
1066 * This will be either a function or a class.
1067 * We can only determine this by checking the body
1068 * of the function. If we find a "this." we know
1069 * it is a class, otherwise it is a function.
1070 */
1071 parseBlock (token, token);
1072 }
1073 else
1074 {
1075 findCmdTerm (token);
1076
1077 /*
1078 * The IF could be followed by an ELSE statement.
1079 * This too could have two formats, a curly braced
1080 * multiline section, or another single line.
1081 */
1082
1083 if (isType (token, TOKEN_CLOSE_CURLY))
1084 {
1085 /*
1086 * This statement did not have a line terminator.
1087 */
1088 read_next_token = FALSE;
1089 }
1090 else
1091 {
1092 readToken (token);
1093
1094 if (isType (token, TOKEN_CLOSE_CURLY))
1095 {
1096 /*
1097 * This statement did not have a line terminator.
1098 */
1099 read_next_token = FALSE;
1100 }
1101 else
1102 {
1103 if (isKeyword (token, KEYWORD_else))
1104 read_next_token = parseIf (token);
1105 }
1106 }
1107 }
1108 return read_next_token;
1109 }
1110
1111 static void parseFunction (tokenInfo *const token)
1112 {
1113 tokenInfo *const name = newToken ();
1114
1115 /*
1116 * This deals with these formats
1117 * private static function ioErrorHandler( event:IOErrorEvent ):void {
1118 */
1119
1120 if ( isKeyword(token, KEYWORD_function) )
1121 {
1122 readToken (token);
1123 }
1124
1125 copyToken (name, token);
1126 /* Add scope in case this is an INNER function
1127 addToScope(name, token->scope);
1128 */
1129
1130 DebugStatement (
1131 debugPrintf (DEBUG_PARSE
1132 , "\n parseFunction: token isClass:%d scope:%s name:%s\n"
1133 , token->isClass
1134 , vStringValue(token->scope)
1135 , vStringValue(token->string)
1136 );
1137 );
1138 DebugStatement (
1139 debugPrintf (DEBUG_PARSE
1140 , "\n parseFunction: name isClass:%d scope:%s name:%s\n"
1141 , name->isClass
1142 , vStringValue(name->scope)
1143 , vStringValue(name->string)
1144 );
1145 );
1146
1147 readToken (token);
1148
1149 if ( isType (token, TOKEN_OPEN_PAREN) )
1150 skipArgumentList(token);
1151
1152 if ( isType (token, TOKEN_COLON) )
1153 {
1154 /*
1155 * function fname ():ReturnType
1156 */
1157 readToken (token);
1158 readToken (token);
1159 }
1160
1161 if ( isType (token, TOKEN_OPEN_CURLY) )
1162 {
1163 DebugStatement (
1164 debugPrintf (DEBUG_PARSE
1165 , "\n parseFunction end: name isClass:%d scope:%s name:%s\n"
1166 , name->isClass
1167 , vStringValue(name->scope)
1168 , vStringValue(name->string)
1169 );
1170 );
1171 parseBlock (token, name);
1172 DebugStatement (
1173 debugPrintf (DEBUG_PARSE
1174 , "\n parseFunction end2: token isClass:%d scope:%s name:%s\n"
1175 , token->isClass
1176 , vStringValue(token->scope)
1177 , vStringValue(token->string)
1178 );
1179 );
1180 DebugStatement (
1181 debugPrintf (DEBUG_PARSE
1182 , "\n parseFunction end2: token isClass:%d scope:%s name:%s\n"
1183 , token->isClass
1184 , vStringValue(token->scope)
1185 , vStringValue(token->string)
1186 );
1187 );
1188 DebugStatement (
1189 debugPrintf (DEBUG_PARSE
1190 , "\n parseFunction end3: name isClass:%d scope:%s name:%s\n"
1191 , name->isClass
1192 , vStringValue(name->scope)
1193 , vStringValue(name->string)
1194 );
1195 );
1196 makeFunctionTag (name);
1197 }
1198
1199 findCmdTerm (token);
1200
1201 deleteToken (name);
1202 }
1203
1204 static boolean parseBlock (tokenInfo *const token, tokenInfo *const parent)
1205 {
1206 boolean read_next_token = TRUE;
1207 vString * saveScope = vStringNew ();
1208
1209 vStringClear(saveScope);
1210 vStringCopy (saveScope, token->scope);
1211 token->nestLevel++;
1212 DebugStatement (
1213 debugPrintf (DEBUG_PARSE
1214 , "\n parseBlock start: token isClass:%d scope:%s name:%s\n"
1215 , token->isClass
1216 , vStringValue(token->scope)
1217 , vStringValue(token->string)
1218 );
1219 );
1220 /*
1221 * Make this routine a bit more forgiving.
1222 * If called on an open_curly advance it
1223 */
1224 if ( isType (token, TOKEN_OPEN_CURLY) &&
1225 isKeyword(token, KEYWORD_NONE) )
1226 readToken(token);
1227
1228 if (! isType (token, TOKEN_CLOSE_CURLY))
1229 {
1230 /*
1231 * Read until we find the closing brace,
1232 * any nested braces will be handled within
1233 */
1234 do
1235 {
1236 if (isType (token, TOKEN_OPEN_CURLY))
1237 {
1238 /* Handle nested blocks */
1239 parseBlock (token, parent);
1240 }
1241 else
1242 {
1243 /*
1244 * It is possible for a line to have no terminator
1245 * if the following line is a closing brace.
1246 * parseLine will detect this case and indicate
1247 * whether we should read an additional token.
1248 */
1249 read_next_token = parseLine (token);
1250 }
1251
1252 /*
1253 * Always read a new token unless we find a statement without
1254 * a ending terminator
1255 */
1256 if( read_next_token )
1257 readToken(token);
1258
1259 /*
1260 * If we find a statement without a terminator consider the
1261 * block finished, otherwise the stack will be off by one.
1262 */
1263 } while (! isType (token, TOKEN_CLOSE_CURLY) && read_next_token );
1264 }
1265
1266 vStringDelete(saveScope);
1267 token->nestLevel--;
1268
1269 DebugStatement (
1270 debugPrintf (DEBUG_PARSE
1271 , "\n parseBlock end: token isClass:%d scope:%s name:%s\n"
1272 , token->isClass
1273 , vStringValue(token->scope)
1274 , vStringValue(token->string)
1275 );
1276 );
1277 return FALSE;
1278 }
1279
1280 static void parseMethods (tokenInfo *const token, tokenInfo *const class)
1281 {
1282 tokenInfo *const name = newToken ();
1283
1284 /*
1285 * This deals with these formats
1286 * validProperty : 2,
1287 * validMethod : function(a,b) {}
1288 * 'validMethod2' : function(a,b) {}
1289 * container.dirtyTab = {'url': false, 'title':false, 'snapshot':false, '*': false}
1290 */
1291
1292 do
1293 {
1294 readToken (token);
1295 if (isType (token, TOKEN_STRING) || isKeyword(token, KEYWORD_NONE))
1296 {
1297 copyToken (name, token);
1298
1299 readToken (token);
1300 if ( isType (token, TOKEN_COLON) )
1301 {
1302 readToken (token);
1303 if ( isKeyword (token, KEYWORD_function) )
1304 {
1305 readToken (token);
1306 if ( isType (token, TOKEN_OPEN_PAREN) )
1307 {
1308 skipArgumentList(token);
1309 }
1310
1311 if (isType (token, TOKEN_OPEN_CURLY))
1312 {
1313 addToScope (name, class->string);
1314 makeFlexTag (name, FLEXTAG_METHOD);
1315 parseBlock (token, name);
1316
1317 /*
1318 * Read to the closing curly, check next
1319 * token, if a comma, we must loop again
1320 */
1321 readToken (token);
1322 }
1323 }
1324 else
1325 {
1326 addToScope (name, class->string);
1327 makeFlexTag (name, FLEXTAG_PROPERTY);
1328
1329 /*
1330 * Read the next token, if a comma
1331 * we must loop again
1332 */
1333 readToken (token);
1334 }
1335 }
1336 }
1337 } while ( isType(token, TOKEN_COMMA) );
1338
1339 findCmdTerm (token);
1340
1341 deleteToken (name);
1342 }
1343
1344 static boolean parseVar (tokenInfo *const token, boolean is_public)
1345 {
1346 tokenInfo *const name = newToken ();
1347 tokenInfo *const secondary_name = newToken ();
1348 vString * saveScope = vStringNew ();
1349 boolean is_terminated = TRUE;
1350
1351 vStringClear(saveScope);
1352 vStringCopy (saveScope, token->scope);
1353 /*
1354 * Variables are defined as:
1355 * private static var lastFaultMessage:Date = new Date( 0 );
1356 * private static var webRequests:ArrayCollection = new ArrayCollection();
1357 */
1358
1359 if ( isKeyword(token, KEYWORD_var) )
1360 {
1361 readToken(token);
1362 }
1363
1364 /* Variable name */
1365 copyToken (name, token);
1366 readToken(token);
1367
1368 if ( isType (token, TOKEN_COLON) )
1369 {
1370 /*
1371 * var vname ():DataType = new Date();
1372 * var vname ():DataType;
1373 */
1374 readToken (token);
1375 readToken (token);
1376 }
1377
1378 while (! isType (token, TOKEN_SEMICOLON) )
1379 {
1380 readToken (token);
1381 }
1382
1383 if ( isType (token, TOKEN_SEMICOLON) )
1384 {
1385 /*
1386 * Only create variables for global scope
1387 */
1388 /* if ( token->nestLevel == 0 && is_global ) */
1389 if ( is_public )
1390 {
1391 if (isType (token, TOKEN_SEMICOLON))
1392 makeFlexTag (name, FLEXTAG_VARIABLE);
1393 }
1394 }
1395
1396 vStringCopy(token->scope, saveScope);
1397 deleteToken (name);
1398 deleteToken (secondary_name);
1399 vStringDelete(saveScope);
1400
1401 return is_terminated;
1402 }
1403
1404 static boolean parseClass (tokenInfo *const token)
1405 {
1406 tokenInfo *const name = newToken ();
1407 vString * saveScope = vStringNew ();
1408 boolean saveIsClass = token->isClass;
1409
1410 vStringClear(saveScope);
1411 vStringCopy (saveScope, token->scope);
1412 /*
1413 * Variables are defined as:
1414 * private static var lastFaultMessage:Date = new Date( 0 );
1415 * private static var webRequests:ArrayCollection = new ArrayCollection();
1416 */
1417
1418 if ( isKeyword(token, KEYWORD_class) )
1419 {
1420 readToken(token);
1421 }
1422
1423 token->isClass = TRUE;
1424 /* Add class name to scope */
1425 addToScope(token, token->string);
1426 /* Class name */
1427 copyToken (name, token);
1428 readToken(token);
1429
1430 DebugStatement (
1431 debugPrintf (DEBUG_PARSE
1432 , "\n parseClass start: token isClass:%d scope:%s name:%s\n"
1433 , token->isClass
1434 , vStringValue(token->scope)
1435 , vStringValue(token->string)
1436 );
1437 );
1438 if ( isType (token, TOKEN_OPEN_CURLY) )
1439 {
1440 makeClassTag (name);
1441 parseBlock (token, name);
1442 }
1443
1444 DebugStatement (
1445 debugPrintf (DEBUG_PARSE
1446 , "\n parseClass end: token isClass:%d scope:%s name:%s\n"
1447 , token->isClass
1448 , vStringValue(token->scope)
1449 , vStringValue(token->string)
1450 );
1451 );
1452 vStringCopy(token->scope, saveScope);
1453 token->isClass = saveIsClass;
1454 deleteToken (name);
1455 vStringDelete(saveScope);
1456
1457 return TRUE;
1458 }
1459
1460 static boolean parseStatement (tokenInfo *const token)
1461 {
1462 tokenInfo *const name = newToken ();
1463 tokenInfo *const secondary_name = newToken ();
1464 vString * saveScope = vStringNew ();
1465 boolean is_public = FALSE;
1466 boolean is_class = FALSE;
1467 boolean is_terminated = TRUE;
1468 boolean is_global = FALSE;
1469 boolean is_prototype = FALSE;
1470 vString * fulltag;
1471
1472 vStringClear(saveScope);
1473 vStringCopy (saveScope, token->scope);
1474 DebugStatement (
1475 debugPrintf (DEBUG_PARSE
1476 , "\n parseStatement: token isClass:%d scope:%s name:%s\n"
1477 , token->isClass
1478 , vStringValue(token->scope)
1479 , vStringValue(token->string)
1480 );
1481 );
1482 /*
1483 * Functions can be named or unnamed.
1484 * This deals with these formats:
1485 * Function
1486 * validFunctionOne = function(a,b) {}
1487 * testlib.validFunctionFive = function(a,b) {}
1488 * var innerThree = function(a,b) {}
1489 * var innerFour = (a,b) {}
1490 * var D2 = secondary_fcn_name(a,b) {}
1491 * var D3 = new Function("a", "b", "return a+b;");
1492 * Class
1493 * testlib.extras.ValidClassOne = function(a,b) {
1494 * this.a = a;
1495 * }
1496 * Class Methods
1497 * testlib.extras.ValidClassOne.prototype = {
1498 * 'validMethodOne' : function(a,b) {},
1499 * 'validMethodTwo' : function(a,b) {}
1500 * }
1501 * ValidClassTwo = function ()
1502 * {
1503 * this.validMethodThree = function() {}
1504 * // unnamed method
1505 * this.validMethodFour = () {}
1506 * }
1507 * Database.prototype.validMethodThree = Database_getTodaysDate;
1508 */
1509
1510 if ( isKeyword(token, KEYWORD_public) )
1511 {
1512 is_public = TRUE;
1513 readToken(token);
1514 }
1515
1516 if ( isKeyword(token, KEYWORD_private) )
1517 {
1518 readToken(token);
1519 }
1520
1521 if ( isKeyword(token, KEYWORD_static) )
1522 {
1523 readToken(token);
1524 }
1525
1526 if (isType(token, TOKEN_KEYWORD))
1527 {
1528 switch (token->keyword)
1529 {
1530 case KEYWORD_for:
1531 case KEYWORD_while:
1532 case KEYWORD_do:
1533 parseLoop (token);
1534 break;
1535 case KEYWORD_if:
1536 case KEYWORD_else:
1537 case KEYWORD_try:
1538 case KEYWORD_catch:
1539 case KEYWORD_finally:
1540 /* Common semantics */
1541 is_terminated = parseIf (token);
1542 break;
1543 case KEYWORD_switch:
1544 parseSwitch (token);
1545 break;
1546 case KEYWORD_class:
1547 parseClass (token);
1548 return is_terminated;
1549 break;
1550 case KEYWORD_function:
1551 parseFunction (token);
1552 return is_terminated;
1553 break;
1554 case KEYWORD_var:
1555 parseVar (token, is_public);
1556 return is_terminated;
1557 break;
1558 default:
1559 readToken(token);
1560 break;
1561 }
1562 }
1563
1564 copyToken (name, token);
1565
1566 while (! isType (token, TOKEN_CLOSE_CURLY) &&
1567 ! isType (token, TOKEN_SEMICOLON) &&
1568 ! isType (token, TOKEN_EQUAL_SIGN) )
1569 {
1570 /* Potentially the name of the function */
1571 readToken (token);
1572 if (isType (token, TOKEN_PERIOD))
1573 {
1574 /*
1575 * Cannot be a global variable is it has dot references in the name
1576 */
1577 is_global = FALSE;
1578 do
1579 {
1580 readToken (token);
1581 if ( isKeyword(token, KEYWORD_NONE) )
1582 {
1583 if ( is_class )
1584 {
1585 vStringCopy(saveScope, token->scope);
1586 addToScope(token, name->string);
1587 }
1588 else
1589 addContext (name, token);
1590 }
1591 else if ( isKeyword(token, KEYWORD_prototype) )
1592 {
1593 /*
1594 * When we reach the "prototype" tag, we infer:
1595 * "BindAgent" is a class
1596 * "build" is a method
1597 *
1598 * function BindAgent( repeatableIdName, newParentIdName ) {
1599 * }
1600 *
1601 * CASE 1
1602 * Specified function name: "build"
1603 * BindAgent.prototype.build = function( mode ) {
1604 * ignore everything within this function
1605 * }
1606 *
1607 * CASE 2
1608 * Prototype listing
1609 * ValidClassOne.prototype = {
1610 * 'validMethodOne' : function(a,b) {},
1611 * 'validMethodTwo' : function(a,b) {}
1612 * }
1613 *
1614 */
1615 makeClassTag (name);
1616 is_class = TRUE;
1617 is_prototype = TRUE;
1618
1619 /*
1620 * There should a ".function_name" next.
1621 */
1622 readToken (token);
1623 if (isType (token, TOKEN_PERIOD))
1624 {
1625 /*
1626 * Handle CASE 1
1627 */
1628 readToken (token);
1629 if ( isKeyword(token, KEYWORD_NONE) )
1630 {
1631 vStringCopy(saveScope, token->scope);
1632 addToScope(token, name->string);
1633
1634 makeFlexTag (token, FLEXTAG_METHOD);
1635 /*
1636 * We can read until the end of the block / statement.
1637 * We need to correctly parse any nested blocks, but
1638 * we do NOT want to create any tags based on what is
1639 * within the blocks.
1640 */
1641 token->ignoreTag = TRUE;
1642 /*
1643 * Find to the end of the statement
1644 */
1645 findCmdTerm (token);
1646 token->ignoreTag = FALSE;
1647 is_terminated = TRUE;
1648 goto cleanUp;
1649 }
1650 }
1651 else if (isType (token, TOKEN_EQUAL_SIGN))
1652 {
1653 readToken (token);
1654 if (isType (token, TOKEN_OPEN_CURLY))
1655 {
1656 /*
1657 * Handle CASE 2
1658 *
1659 * Creates tags for each of these class methods
1660 * ValidClassOne.prototype = {
1661 * 'validMethodOne' : function(a,b) {},
1662 * 'validMethodTwo' : function(a,b) {}
1663 * }
1664 */
1665 parseMethods(token, name);
1666 /*
1667 * Find to the end of the statement
1668 */
1669 findCmdTerm (token);
1670 token->ignoreTag = FALSE;
1671 is_terminated = TRUE;
1672 goto cleanUp;
1673 }
1674 }
1675 }
1676 readToken (token);
1677 } while (isType (token, TOKEN_PERIOD));
1678 }
1679
1680 if ( isType (token, TOKEN_OPEN_PAREN) )
1681 skipArgumentList(token);
1682
1683 if ( isType (token, TOKEN_COLON) )
1684 {
1685 /*
1686 * Functions are of this form:
1687 * function fname ():ReturnType {
1688 */
1689 readToken (token);
1690 readToken (token);
1691 }
1692
1693 if ( isType (token, TOKEN_OPEN_SQUARE) )
1694 skipArrayList(token);
1695
1696 }
1697
1698 if ( isType (token, TOKEN_CLOSE_CURLY) )
1699 {
1700 /*
1701 * Reaching this section without having
1702 * processed an open curly brace indicates
1703 * the statement is most likely not terminated.
1704 */
1705 is_terminated = FALSE;
1706 goto cleanUp;
1707 }
1708
1709 if ( isType (token, TOKEN_SEMICOLON) )
1710 {
1711 /*
1712 * Only create variables for global scope
1713 */
1714 if ( token->nestLevel == 0 && is_global )
1715 {
1716 /*
1717 * Handles this syntax:
1718 * var g_var2;
1719 */
1720 if (isType (token, TOKEN_SEMICOLON))
1721 makeFlexTag (name, FLEXTAG_VARIABLE);
1722 }
1723 /*
1724 * Statement has ended.
1725 * This deals with calls to functions, like:
1726 * alert(..);
1727 */
1728 goto cleanUp;
1729 }
1730
1731 if ( isType (token, TOKEN_EQUAL_SIGN) )
1732 {
1733 readToken (token);
1734
1735 if ( isKeyword (token, KEYWORD_function) )
1736 {
1737 readToken (token);
1738
1739 if ( isKeyword (token, KEYWORD_NONE) &&
1740 ! isType (token, TOKEN_OPEN_PAREN) )
1741 {
1742 /*
1743 * Functions of this format:
1744 * var D2A = function theAdd(a, b)
1745 * {
1746 * return a+b;
1747 * }
1748 * Are really two separate defined functions and
1749 * can be referenced in two ways:
1750 * alert( D2A(1,2) ); // produces 3
1751 * alert( theAdd(1,2) ); // also produces 3
1752 * So it must have two tags:
1753 * D2A
1754 * theAdd
1755 * Save the reference to the name for later use, once
1756 * we have established this is a valid function we will
1757 * create the secondary reference to it.
1758 */
1759 copyToken (secondary_name, token);
1760 readToken (token);
1761 }
1762
1763 if ( isType (token, TOKEN_OPEN_PAREN) )
1764 skipArgumentList(token);
1765
1766 if (isType (token, TOKEN_OPEN_CURLY))
1767 {
1768 /*
1769 * This will be either a function or a class.
1770 * We can only determine this by checking the body
1771 * of the function. If we find a "this." we know
1772 * it is a class, otherwise it is a function.
1773 */
1774 if ( token->isClass )
1775 {
1776 makeFlexTag (name, FLEXTAG_METHOD);
1777 if ( vStringLength(secondary_name->string) > 0 )
1778 makeFunctionTag (secondary_name);
1779 parseBlock (token, name);
1780 }
1781 else
1782 {
1783 parseBlock (token, name);
1784 makeFunctionTag (name);
1785
1786 if ( vStringLength(secondary_name->string) > 0 )
1787 makeFunctionTag (secondary_name);
1788
1789 /*
1790 * Find to the end of the statement
1791 */
1792 goto cleanUp;
1793 }
1794 }
1795 }
1796 else if (isType (token, TOKEN_OPEN_PAREN))
1797 {
1798 /*
1799 * Handle nameless functions
1800 * this.method_name = () {}
1801 */
1802 skipArgumentList(token);
1803
1804 if (isType (token, TOKEN_OPEN_CURLY))
1805 {
1806 /*
1807 * Nameless functions are only setup as methods.
1808 */
1809 makeFlexTag (name, FLEXTAG_METHOD);
1810 parseBlock (token, name);
1811 }
1812 }
1813 else if (isType (token, TOKEN_OPEN_CURLY))
1814 {
1815 /*
1816 * Creates tags for each of these class methods
1817 * ValidClassOne.prototype = {
1818 * 'validMethodOne' : function(a,b) {},
1819 * 'validMethodTwo' : function(a,b) {}
1820 * }
1821 */
1822 parseMethods(token, name);
1823 if (isType (token, TOKEN_CLOSE_CURLY))
1824 {
1825 /*
1826 * Assume the closing parantheses terminates
1827 * this statements.
1828 */
1829 is_terminated = TRUE;
1830 }
1831 }
1832 else if (isKeyword (token, KEYWORD_new))
1833 {
1834 readToken (token);
1835 if ( isKeyword (token, KEYWORD_function) ||
1836 isKeyword (token, KEYWORD_capital_function) ||
1837 isKeyword (token, KEYWORD_object) ||
1838 isKeyword (token, KEYWORD_capital_object) )
1839 {
1840 if ( isKeyword (token, KEYWORD_object) ||
1841 isKeyword (token, KEYWORD_capital_object) )
1842 is_class = TRUE;
1843
1844 readToken (token);
1845 if ( isType (token, TOKEN_OPEN_PAREN) )
1846 skipArgumentList(token);
1847
1848 if (isType (token, TOKEN_SEMICOLON))
1849 {
1850 if ( token->nestLevel == 0 )
1851 {
1852 if ( is_class )
1853 {
1854 makeClassTag (name);
1855 } else {
1856 makeFunctionTag (name);
1857 }
1858 }
1859 }
1860 }
1861 }
1862 else if (isKeyword (token, KEYWORD_NONE))
1863 {
1864 /*
1865 * Only create variables for global scope
1866 */
1867 if ( token->nestLevel == 0 && is_global )
1868 {
1869 /*
1870 * A pointer can be created to the function.
1871 * If we recognize the function/class name ignore the variable.
1872 * This format looks identical to a variable definition.
1873 * A variable defined outside of a block is considered
1874 * a global variable:
1875 * var g_var1 = 1;
1876 * var g_var2;
1877 * This is not a global variable:
1878 * var g_var = function;
1879 * This is a global variable:
1880 * var g_var = different_var_name;
1881 */
1882 fulltag = vStringNew ();
1883 if (vStringLength (token->scope) > 0)
1884 {
1885 vStringCopy(fulltag, token->scope);
1886 vStringCatS (fulltag, ".");
1887 vStringCatS (fulltag, vStringValue(token->string));
1888 }
1889 else
1890 {
1891 vStringCopy(fulltag, token->string);
1892 }
1893 vStringTerminate(fulltag);
1894 if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) &&
1895 ! stringListHas(ClassNames, vStringValue (fulltag)) )
1896 {
1897 findCmdTerm (token);
1898 if (isType (token, TOKEN_SEMICOLON))
1899 makeFlexTag (name, FLEXTAG_VARIABLE);
1900 }
1901 vStringDelete (fulltag);
1902 }
1903 }
1904 }
1905 findCmdTerm (token);
1906
1907 /*
1908 * Statements can be optionally terminated in the case of
1909 * statement prior to a close curly brace as in the
1910 * document.write line below:
1911 *
1912 * function checkForUpdate() {
1913 * if( 1==1 ) {
1914 * document.write("hello from checkForUpdate<br>")
1915 * }
1916 * return 1;
1917 * }
1918 */
1919 if ( ! is_terminated && isType (token, TOKEN_CLOSE_CURLY))
1920 is_terminated = FALSE;
1921
1922
1923 cleanUp:
1924 vStringCopy(token->scope, saveScope);
1925 deleteToken (name);
1926 deleteToken (secondary_name);
1927 vStringDelete(saveScope);
1928
1929 return is_terminated;
1930 }
1931
1932 static boolean parseLine (tokenInfo *const token)
1933 {
1934 boolean is_terminated = TRUE;
1935 /*
1936 * Detect the common statements, if, while, for, do, ...
1937 * This is necessary since the last statement within a block "{}"
1938 * can be optionally terminated.
1939 *
1940 * If the statement is not terminated, we need to tell
1941 * the calling routine to prevent reading an additional token
1942 * looking for the end of the statement.
1943 */
1944
1945 if (isType(token, TOKEN_KEYWORD))
1946 {
1947 switch (token->keyword)
1948 {
1949 case KEYWORD_for:
1950 case KEYWORD_while:
1951 case KEYWORD_do:
1952 parseLoop (token);
1953 break;
1954 case KEYWORD_if:
1955 case KEYWORD_else:
1956 case KEYWORD_try:
1957 case KEYWORD_catch:
1958 case KEYWORD_finally:
1959 /* Common semantics */
1960 is_terminated = parseIf (token);
1961 break;
1962 case KEYWORD_switch:
1963 parseSwitch (token);
1964 break;
1965 default:
1966 parseStatement (token);
1967 break;
1968 }
1969 }
1970 else
1971 {
1972 /*
1973 * Special case where single line statements may not be
1974 * SEMICOLON terminated. parseBlock needs to know this
1975 * so that it does not read the next token.
1976 */
1977 is_terminated = parseStatement (token);
1978 }
1979 return is_terminated;
1980 }
1981
1982 static boolean parseCDATA (tokenInfo *const token)
1983 {
1984 if (isType (token, TOKEN_LESS_THAN))
1985 {
1986 /*
1987 * Handle these tags
1988 * <![CDATA[
1989 * ...
1990 * ]]>
1991 */
1992 readToken (token);
1993 if (isType (token, TOKEN_EXCLAMATION))
1994 {
1995 /*
1996 * Not sure why I had to comment these out, but I did.
1997 * readToken (token);
1998 * if (isType (token, TOKEN_OPEN_SQUARE))
1999 * {
2000 */
2001 readToken (token);
2002 if (isKeyword (token, KEYWORD_cdata))
2003 {
2004 readToken (token);
2005 if (isType (token, TOKEN_OPEN_SQUARE))
2006 {
2007 parseActionScript (token);
2008 if (isType (token, TOKEN_CLOSE_SQUARE))
2009 {
2010 readToken (token);
2011 if (isType (token, TOKEN_CLOSE_SQUARE))
2012 {
2013 readToken (token);
2014 }
2015 }
2016 }
2017 }
2018 /*} Not sure */
2019 }
2020 }
2021 else
2022 {
2023 parseActionScript (token);
2024 }
2025 return TRUE;
2026 }
2027
2028 static boolean parseNamespace (tokenInfo *const token)
2029 {
2030 /*
2031 * If we have found a <, we know it is not a TOKEN_OPEN_MXML
2032 * but it could potentially be a different namespace.
2033 * This means it will also have a closing tag, which will
2034 * mess up the parser if we do not properly recurse
2035 * through these tags.
2036 */
2037
2038 if (isType (token, TOKEN_LESS_THAN))
2039 {
2040 readToken (token);
2041 }
2042
2043 /*
2044 * Check if we have reached a other namespace tag
2045 * <views:Object ... />
2046 * or
2047 * <views:Object ... >
2048 * </views:Object>
2049 */
2050 if (isType (token, TOKEN_IDENTIFIER))
2051 {
2052 readToken (token);
2053 if (isType (token, TOKEN_COLON))
2054 {
2055 readToken (token);
2056 if ( ! isType (token, TOKEN_IDENTIFIER))
2057 {
2058 return TRUE;
2059 }
2060 }
2061 else
2062 {
2063 return TRUE;
2064 }
2065 }
2066 else
2067 {
2068 return TRUE;
2069 }
2070
2071 /*
2072 * Confirmed we are inside a namespace tag, so
2073 * process it until the close tag.
2074 *
2075 * But also check for new tags, which will either
2076 * be recursive namespaces or MXML tags
2077 */
2078 do
2079 {
2080 if (isType (token, TOKEN_LESS_THAN))
2081 {
2082 parseNamespace (token);
2083 readToken (token);
2084 }
2085 if (isType (token, TOKEN_OPEN_MXML))
2086 {
2087 parseMXML (token);
2088 }
2089 else
2090 {
2091 readToken (token);
2092 }
2093 } while (! (isType (token, TOKEN_CLOSE_SGML) || isType (token, TOKEN_CLOSE_MXML)) );
2094
2095 return TRUE;
2096 }
2097
2098 static boolean parseMXML (tokenInfo *const token)
2099 {
2100 tokenInfo *const name = newToken ();
2101 tokenInfo *const type = newToken ();
2102 boolean inside_attributes = TRUE;
2103 /*
2104 * Detect the common statements, if, while, for, do, ...
2105 * This is necessary since the last statement within a block "{}"
2106 * can be optionally terminated.
2107 *
2108 * If the statement is not terminated, we need to tell
2109 * the calling routine to prevent reading an additional token
2110 * looking for the end of the statement.
2111 */
2112
2113 readToken (token);
2114
2115 if (isKeyword (token, KEYWORD_script))
2116 {
2117 /*
2118 * These tags can be of this form:
2119 * <mx:Script src="filename.as" />
2120 */
2121 do
2122 {
2123 readToken (token);
2124 } while (! (isType (token, TOKEN_CLOSE_SGML) ||
2125 isType (token, TOKEN_CLOSE_MXML) ||
2126 isType (token, TOKEN_GREATER_THAN)) );
2127
2128 if (isType (token, TOKEN_CLOSE_MXML))
2129 {
2130 /*
2131 * We have found a </mx:type> tag
2132 * Finish reading the "type" and ">"
2133 */
2134 readToken (token);
2135 readToken (token);
2136 goto cleanUp;
2137 }
2138 if (isType (token, TOKEN_CLOSE_SGML))
2139 {
2140 /*
2141 * We have found a <mx:Script src="filename.as" />
2142 */
2143 goto cleanUp;
2144 }
2145
2146 /*
2147 * This is a beginning of an embedded script.
2148 * These typically are of this format:
2149 * <mx:Script>
2150 * <![CDATA[
2151 * ... ActionScript ...
2152 * ]]>
2153 * </mx:Script>
2154 */
2155 readToken (token);
2156 parseCDATA (token);
2157
2158 readToken (token);
2159 if (isType (token, TOKEN_CLOSE_MXML))
2160 {
2161 /*
2162 * We have found a </mx:type> tag
2163 * Finish reading the "type" and ">"
2164 */
2165 readToken (token);
2166 readToken (token);
2167 }
2168 goto cleanUp;
2169 }
2170
2171 copyToken (type, token);
2172
2173 readToken (token);
2174 do
2175 {
2176 if (isType (token, TOKEN_GREATER_THAN))
2177 {
2178 inside_attributes = FALSE;
2179 }
2180 if (isType (token, TOKEN_LESS_THAN))
2181 {
2182 parseNamespace (token);
2183 readToken (token);
2184 }
2185 else if (isType (token, TOKEN_OPEN_MXML))
2186 {
2187 parseMXML (token);
2188 readToken (token);
2189 }
2190 else if (inside_attributes && (isKeyword (token, KEYWORD_id) || isKeyword (token, KEYWORD_name)))
2191 {
2192 if (vStringLength(name->string) == 0 )
2193 {
2194 /*
2195 * If we have already created the tag based on either "name"
2196 * or "id" do not do it again.
2197 */
2198 readToken (token);
2199 readToken (token);
2200
2201 copyToken (name, token);
2202 addToScope (name, type->string);
2203 makeMXTag (name);
2204 }
2205 else
2206 {
2207 readToken (token);
2208 }
2209 }
2210 else
2211 {
2212 readToken (token);
2213 }
2214 } while (! (isType (token, TOKEN_CLOSE_SGML) || isType (token, TOKEN_CLOSE_MXML)) );
2215
2216 if (isType (token, TOKEN_CLOSE_MXML))
2217 {
2218 /*
2219 * We have found a </mx:type> tag
2220 * Finish reading the "type" and ">"
2221 */
2222 readToken (token);
2223 readToken (token);
2224 }
2225
2226 cleanUp:
2227 deleteToken (name);
2228 deleteToken (type);
2229 return TRUE;
2230 }
2231
2232 static boolean parseActionScript (tokenInfo *const token)
2233 {
2234 do
2235 {
2236 readToken (token);
2237
2238 if (isType (token, TOKEN_LESS_THAN))
2239 {
2240 /*
2241 * Handle these tags
2242 * <![CDATA[
2243 * ...
2244 * ]]>
2245 */
2246 readToken (token);
2247 if (isType (token, TOKEN_EQUAL_SIGN))
2248 {
2249 if (isType (token, TOKEN_OPEN_SQUARE))
2250 {
2251 readToken (token);
2252 if (isKeyword (token, KEYWORD_cdata))
2253 {
2254 readToken (token);
2255 }
2256 }
2257 }
2258 }
2259 if (isType (token, TOKEN_CLOSE_SQUARE))
2260 {
2261 /*
2262 * Handle these tags
2263 * <![CDATA[
2264 * ...
2265 * ]]>
2266 */
2267 readToken (token);
2268 if (isType (token, TOKEN_CLOSE_SQUARE))
2269 {
2270 readToken (token);
2271 if (isType (token, TOKEN_GREATER_THAN))
2272 {
2273 return TRUE;
2274 }
2275 }
2276 }
2277 else if (isType (token, TOKEN_CLOSE_MXML))
2278 {
2279 /*
2280 * Read the Script> tags
2281 */
2282 readToken (token);
2283 readToken (token);
2284 return TRUE;
2285 }
2286 else if (isType (token, TOKEN_OPEN_MXML))
2287 {
2288 parseMXML (token);
2289 }
2290 else
2291 {
2292 if (isType(token, TOKEN_KEYWORD))
2293 {
2294 if (isKeyword (token, KEYWORD_private) ||
2295 isKeyword (token, KEYWORD_public) ||
2296 isKeyword (token, KEYWORD_override) )
2297 {
2298 /*
2299 * Methods can be defined as:
2300 * private function f_name
2301 * public override function f_name
2302 * override private function f_name
2303 * Ignore these keywords if present.
2304 */
2305 readToken (token);
2306 }
2307 if (isKeyword (token, KEYWORD_private) ||
2308 isKeyword (token, KEYWORD_public) ||
2309 isKeyword (token, KEYWORD_override) )
2310 {
2311 /*
2312 * Methods can be defined as:
2313 * private function f_name
2314 * public override function f_name
2315 * override private function f_name
2316 * Ignore these keywords if present.
2317 */
2318 readToken (token);
2319 }
2320
2321 switch (token->keyword)
2322 {
2323 case KEYWORD_function: parseFunction (token); break;
2324 default: parseLine (token); break;
2325 }
2326 }
2327 else
2328 {
2329 parseLine (token);
2330 }
2331 }
2332 } while (TRUE);
2333 }
2334
2335 static void parseFlexFile (tokenInfo *const token)
2336 {
2337 do
2338 {
2339 readToken (token);
2340
2341 if (isType (token, TOKEN_OPEN_MXML))
2342 {
2343 parseMXML (token);
2344 }
2345 else if (isType (token, TOKEN_LESS_THAN))
2346 {
2347 readToken (token);
2348 if (isType (token, TOKEN_QUESTION_MARK))
2349 {
2350 /*
2351 * <?xml version="1.0" encoding="utf-8"?>
2352 */
2353 readToken (token);
2354 while (! isType (token, TOKEN_QUESTION_MARK) )
2355 {
2356 readToken (token);
2357 }
2358 readToken (token);
2359 }
2360 else if (isKeyword (token, KEYWORD_NONE))
2361 {
2362 /*
2363 * This is a simple XML tag, read until the closing statement
2364 * <something .... >
2365 * </something>
2366 */
2367 readToken (token);
2368 while (! isType (token, TOKEN_GREATER_THAN) )
2369 {
2370 readToken (token);
2371 }
2372 }
2373 }
2374 else
2375 {
2376 parseActionScript (token);
2377 }
2378 } while (TRUE);
2379 }
2380
2381 static void initialize (const langType language)
2382 {
2383 Assert (sizeof (FlexKinds) / sizeof (FlexKinds [0]) == FLEXTAG_COUNT);
2384 Lang_js = language;
2385 buildFlexKeywordHash ();
2386 }
2387
2388 static void findFlexTags (void)
2389 {
2390 tokenInfo *const token = newToken ();
2391 exception_t exception;
2392
2393 ClassNames = stringListNew ();
2394 FunctionNames = stringListNew ();
2395
2396 exception = (exception_t) (setjmp (Exception));
2397 while (exception == ExceptionNone)
2398 parseFlexFile (token);
2399
2400 stringListDelete (ClassNames);
2401 stringListDelete (FunctionNames);
2402 ClassNames = NULL;
2403 FunctionNames = NULL;
2404 deleteToken (token);
2405 }
2406
2407 /* Create parser definition stucture */
2408 extern parserDefinition* FlexParser (void)
2409 {
2410 static const char *const extensions [] = { "as", "mxml", NULL };
2411 parserDefinition *const def = parserNew ("Flex");
2412 def->extensions = extensions;
2413 /*
2414 * New definitions for parsing instead of regex
2415 */
2416 def->kinds = FlexKinds;
2417 def->kindCount = KIND_COUNT (FlexKinds);
2418 def->parser = findFlexTags;
2419 def->initialize = initialize;
2420
2421 return def;
2422 }
2423 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */