Debian Codegraph @

jscript.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * $Id: jscript.c 763 2010-07-28 14:22:42Z dfishburn $
3 *
4 * Copyright (c) 2003, Darren Hiebert
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 JavaScript language
10 * files.
11 *
12 * This is a good reference for different forms of the function statement:
13 * http://www.permadi.com/tutorial/jsFunc/
14 * Another good reference:
15 * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide
16 */
17
18 /*
19 * INCLUDE FILES
20 */
21 #include "general.h" /* must always come first */
22 #include <ctype.h> /* to define isalpha () */
23 #include <setjmp.h>
24 #ifdef DEBUG
25 #include <stdio.h>
26 #endif
27
28 #include "debug.h"
29 #include "entry.h"
30 #include "keyword.h"
31 #include "parse.h"
32 #include "read.h"
33 #include "routines.h"
34 #include "vstring.h"
35
36 /*
37 * MACROS
38 */
39 #define isType(token,t) (boolean) ((token)->type == (t))
40 #define isKeyword(token,k) (boolean) ((token)->keyword == (k))
41
42 /*
43 * DATA DECLARATIONS
44 */
45
46 typedef enum eException { ExceptionNone, ExceptionEOF } exception_t;
47
48 /*
49 * Tracks class and function names already created
50 */
51 static stringList *ClassNames;
52 static stringList *FunctionNames;
53
54 /* Used to specify type of keyword.
55 */
56 typedef enum eKeywordId {
57 KEYWORD_NONE = -1,
58 KEYWORD_function,
59 KEYWORD_capital_function,
60 KEYWORD_object,
61 KEYWORD_capital_object,
62 KEYWORD_prototype,
63 KEYWORD_var,
64 KEYWORD_new,
65 KEYWORD_this,
66 KEYWORD_for,
67 KEYWORD_while,
68 KEYWORD_do,
69 KEYWORD_if,
70 KEYWORD_else,
71 KEYWORD_switch,
72 KEYWORD_try,
73 KEYWORD_catch,
74 KEYWORD_finally
75 } keywordId;
76
77 /* Used to determine whether keyword is valid for the token language and
78 * what its ID is.
79 */
80 typedef struct sKeywordDesc {
81 const char *name;
82 keywordId id;
83 } keywordDesc;
84
85 typedef enum eTokenType {
86 TOKEN_UNDEFINED,
87 TOKEN_CHARACTER,
88 TOKEN_CLOSE_PAREN,
89 TOKEN_SEMICOLON,
90 TOKEN_COLON,
91 TOKEN_COMMA,
92 TOKEN_KEYWORD,
93 TOKEN_OPEN_PAREN,
94 TOKEN_OPERATOR,
95 TOKEN_IDENTIFIER,
96 TOKEN_STRING,
97 TOKEN_PERIOD,
98 TOKEN_OPEN_CURLY,
99 TOKEN_CLOSE_CURLY,
100 TOKEN_EQUAL_SIGN,
101 TOKEN_FORWARD_SLASH,
102 TOKEN_OPEN_SQUARE,
103 TOKEN_CLOSE_SQUARE
104 } tokenType;
105
106 typedef struct sTokenInfo {
107 tokenType type;
108 keywordId keyword;
109 vString * string;
110 vString * scope;
111 unsigned long lineNumber;
112 fpos_t filePosition;
113 int nestLevel;
114 boolean ignoreTag;
115 } tokenInfo;
116
117 /*
118 * DATA DEFINITIONS
119 */
120
121 static langType Lang_js;
122
123 static jmp_buf Exception;
124
125 typedef enum {
126 JSTAG_FUNCTION,
127 JSTAG_CLASS,
128 JSTAG_METHOD,
129 JSTAG_PROPERTY,
130 JSTAG_VARIABLE,
131 JSTAG_COUNT
132 } jsKind;
133
134 static kindOption JsKinds [] = {
135 { TRUE, 'f', "function", "functions" },
136 { TRUE, 'c', "class", "classes" },
137 { TRUE, 'm', "method", "methods" },
138 { TRUE, 'p', "property", "properties" },
139 { TRUE, 'v', "variable", "global variables" }
140 };
141
142 static const keywordDesc JsKeywordTable [] = {
143 /* keyword keyword ID */
144 { "function", KEYWORD_function },
145 { "Function", KEYWORD_capital_function },
146 { "object", KEYWORD_object },
147 { "Object", KEYWORD_capital_object },
148 { "prototype", KEYWORD_prototype },
149 { "var", KEYWORD_var },
150 { "new", KEYWORD_new },
151 { "this", KEYWORD_this },
152 { "for", KEYWORD_for },
153 { "while", KEYWORD_while },
154 { "do", KEYWORD_do },
155 { "if", KEYWORD_if },
156 { "else", KEYWORD_else },
157 { "switch", KEYWORD_switch },
158 { "try", KEYWORD_try },
159 { "catch", KEYWORD_catch },
160 { "finally", KEYWORD_finally }
161 };
162
163 /*
164 * FUNCTION DEFINITIONS
165 */
166
167 /* Recursive functions */
168 static void parseFunction (tokenInfo *const token);
169 static boolean parseBlock (tokenInfo *const token, tokenInfo *const parent);
170 static boolean parseLine (tokenInfo *const token, boolean is_inside_class);
171
172 static boolean isIdentChar (const int c)
173 {
174 return (boolean)
175 (isalpha (c) || isdigit (c) || c == '$' ||
176 c == '@' || c == '_' || c == '#');
177 }
178
179 static void buildJsKeywordHash (void)
180 {
181 const size_t count = sizeof (JsKeywordTable) /
182 sizeof (JsKeywordTable [0]);
183 size_t i;
184 for (i = 0 ; i < count ; ++i)
185 {
186 const keywordDesc* const p = &JsKeywordTable [i];
187 addKeyword (p->name, Lang_js, (int) p->id);
188 }
189 }
190
191 static tokenInfo *newToken (void)
192 {
193 tokenInfo *const token = xMalloc (1, tokenInfo);
194
195 token->type = TOKEN_UNDEFINED;
196 token->keyword = KEYWORD_NONE;
197 token->string = vStringNew ();
198 token->scope = vStringNew ();
199 token->nestLevel = 0;
200 token->ignoreTag = FALSE;
201 token->lineNumber = getSourceLineNumber ();
202 token->filePosition = getInputFilePosition ();
203
204 return token;
205 }
206
207 static void deleteToken (tokenInfo *const token)
208 {
209 vStringDelete (token->string);
210 vStringDelete (token->scope);
211 eFree (token);
212 }
213
214 /*
215 * Tag generation functions
216 */
217
218 static void makeConstTag (tokenInfo *const token, const jsKind kind)
219 {
220 if (JsKinds [kind].enabled && ! token->ignoreTag )
221 {
222 const char *const name = vStringValue (token->string);
223 tagEntryInfo e;
224 initTagEntry (&e, name);
225
226 e.lineNumber = token->lineNumber;
227 e.filePosition = token->filePosition;
228 e.kindName = JsKinds [kind].name;
229 e.kind = JsKinds [kind].letter;
230
231 makeTagEntry (&e);
232 }
233 }
234
235 static void makeJsTag (tokenInfo *const token, const jsKind kind)
236 {
237 vString * fulltag;
238
239 if (JsKinds [kind].enabled && ! token->ignoreTag )
240 {
241 /*
242 * If a scope has been added to the token, change the token
243 * string to include the scope when making the tag.
244 */
245 if ( vStringLength(token->scope) > 0 )
246 {
247 fulltag = vStringNew ();
248 vStringCopy(fulltag, token->scope);
249 vStringCatS (fulltag, ".");
250 vStringCatS (fulltag, vStringValue(token->string));
251 vStringTerminate(fulltag);
252 vStringCopy(token->string, fulltag);
253 vStringDelete (fulltag);
254 }
255 makeConstTag (token, kind);
256 }
257 }
258
259 static void makeClassTag (tokenInfo *const token)
260 {
261 vString * fulltag;
262
263 if ( ! token->ignoreTag )
264 {
265 fulltag = vStringNew ();
266 if (vStringLength (token->scope) > 0)
267 {
268 vStringCopy(fulltag, token->scope);
269 vStringCatS (fulltag, ".");
270 vStringCatS (fulltag, vStringValue(token->string));
271 }
272 else
273 {
274 vStringCopy(fulltag, token->string);
275 }
276 vStringTerminate(fulltag);
277 if ( ! stringListHas(ClassNames, vStringValue (fulltag)) )
278 {
279 stringListAdd (ClassNames, vStringNewCopy (fulltag));
280 makeJsTag (token, JSTAG_CLASS);
281 }
282 vStringDelete (fulltag);
283 }
284 }
285
286 static void makeFunctionTag (tokenInfo *const token)
287 {
288 vString * fulltag;
289
290 if ( ! token->ignoreTag )
291 {
292 fulltag = vStringNew ();
293 if (vStringLength (token->scope) > 0)
294 {
295 vStringCopy(fulltag, token->scope);
296 vStringCatS (fulltag, ".");
297 vStringCatS (fulltag, vStringValue(token->string));
298 }
299 else
300 {
301 vStringCopy(fulltag, token->string);
302 }
303 vStringTerminate(fulltag);
304 if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) )
305 {
306 stringListAdd (FunctionNames, vStringNewCopy (fulltag));
307 makeJsTag (token, JSTAG_FUNCTION);
308 }
309 vStringDelete (fulltag);
310 }
311 }
312
313 /*
314 * Parsing functions
315 */
316
317 static void parseString (vString *const string, const int delimiter)
318 {
319 boolean end = FALSE;
320 while (! end)
321 {
322 int c = fileGetc ();
323 if (c == EOF)
324 end = TRUE;
325 else if (c == '\\')
326 {
327 c = fileGetc(); /* This maybe a ' or ". */
328 vStringPut(string, c);
329 }
330 else if (c == delimiter)
331 end = TRUE;
332 else
333 vStringPut (string, c);
334 }
335 vStringTerminate (string);
336 }
337
338 /* Read a C identifier beginning with "firstChar" and places it into
339 * "name".
340 */
341 static void parseIdentifier (vString *const string, const int firstChar)
342 {
343 int c = firstChar;
344 Assert (isIdentChar (c));
345 do
346 {
347 vStringPut (string, c);
348 c = fileGetc ();
349 } while (isIdentChar (c));
350 vStringTerminate (string);
351 if (!isspace (c))
352 fileUngetc (c); /* unget non-identifier character */
353 }
354
355 static void readToken (tokenInfo *const token)
356 {
357 int c;
358
359 token->type = TOKEN_UNDEFINED;
360 token->keyword = KEYWORD_NONE;
361 vStringClear (token->string);
362
363 getNextChar:
364 do
365 {
366 c = fileGetc ();
367 token->lineNumber = getSourceLineNumber ();
368 token->filePosition = getInputFilePosition ();
369 }
370 while (c == '\t' || c == ' ' || c == '\n');
371
372 switch (c)
373 {
374 case EOF: longjmp (Exception, (int)ExceptionEOF); break;
375 case '(': token->type = TOKEN_OPEN_PAREN; break;
376 case ')': token->type = TOKEN_CLOSE_PAREN; break;
377 case ';': token->type = TOKEN_SEMICOLON; break;
378 case ',': token->type = TOKEN_COMMA; break;
379 case '.': token->type = TOKEN_PERIOD; break;
380 case ':': token->type = TOKEN_COLON; break;
381 case '{': token->type = TOKEN_OPEN_CURLY; break;
382 case '}': token->type = TOKEN_CLOSE_CURLY; break;
383 case '=': token->type = TOKEN_EQUAL_SIGN; break;
384 case '[': token->type = TOKEN_OPEN_SQUARE; break;
385 case ']': token->type = TOKEN_CLOSE_SQUARE; break;
386
387 case '\'':
388 case '"':
389 token->type = TOKEN_STRING;
390 parseString (token->string, c);
391 token->lineNumber = getSourceLineNumber ();
392 token->filePosition = getInputFilePosition ();
393 break;
394
395 case '\\':
396 c = fileGetc ();
397 if (c != '\\' && c != '"' && !isspace (c))
398 fileUngetc (c);
399 token->type = TOKEN_CHARACTER;
400 token->lineNumber = getSourceLineNumber ();
401 token->filePosition = getInputFilePosition ();
402 break;
403
404 case '/':
405 {
406 int d = fileGetc ();
407 if ( (d != '*') && /* is this the start of a comment? */
408 (d != '/') ) /* is a one line comment? */
409 {
410 token->type = TOKEN_FORWARD_SLASH;
411 fileUngetc (d);
412 }
413 else
414 {
415 if (d == '*')
416 {
417 do
418 {
419 fileSkipToCharacter ('*');
420 c = fileGetc ();
421 if (c == '/')
422 break;
423 else
424 fileUngetc (c);
425 } while (c != EOF && c != '\0');
426 goto getNextChar;
427 }
428 else if (d == '/') /* is this the start of a comment? */
429 {
430 fileSkipToCharacter ('\n');
431 goto getNextChar;
432 }
433 }
434 break;
435 }
436
437 default:
438 if (! isIdentChar (c))
439 token->type = TOKEN_UNDEFINED;
440 else
441 {
442 parseIdentifier (token->string, c);
443 token->lineNumber = getSourceLineNumber ();
444 token->filePosition = getInputFilePosition ();
445 token->keyword = analyzeToken (token->string, Lang_js);
446 if (isKeyword (token, KEYWORD_NONE))
447 token->type = TOKEN_IDENTIFIER;
448 else
449 token->type = TOKEN_KEYWORD;
450 }
451 break;
452 }
453 }
454
455 static void copyToken (tokenInfo *const dest, tokenInfo *const src)
456 {
457 dest->nestLevel = src->nestLevel;
458 dest->lineNumber = src->lineNumber;
459 dest->filePosition = src->filePosition;
460 dest->type = src->type;
461 dest->keyword = src->keyword;
462 vStringCopy(dest->string, src->string);
463 vStringCopy(dest->scope, src->scope);
464 }
465
466 /*
467 * Token parsing functions
468 */
469
470 static void skipArgumentList (tokenInfo *const token)
471 {
472 int nest_level = 0;
473
474 /*
475 * Other databases can have arguments with fully declared
476 * datatypes:
477 * ( name varchar(30), text binary(10) )
478 * So we must check for nested open and closing parantheses
479 */
480
481 if (isType (token, TOKEN_OPEN_PAREN)) /* arguments? */
482 {
483 nest_level++;
484 while (! (isType (token, TOKEN_CLOSE_PAREN) && (nest_level == 0)))
485 {
486 readToken (token);
487 if (isType (token, TOKEN_OPEN_PAREN))
488 {
489 nest_level++;
490 }
491 if (isType (token, TOKEN_CLOSE_PAREN))
492 {
493 if (nest_level > 0)
494 {
495 nest_level--;
496 }
497 }
498 }
499 readToken (token);
500 }
501 }
502
503 static void skipArrayList (tokenInfo *const token)
504 {
505 int nest_level = 0;
506
507 /*
508 * Handle square brackets
509 * var name[1]
510 * So we must check for nested open and closing square brackets
511 */
512
513 if (isType (token, TOKEN_OPEN_SQUARE)) /* arguments? */
514 {
515 nest_level++;
516 while (! (isType (token, TOKEN_CLOSE_SQUARE) && (nest_level == 0)))
517 {
518 readToken (token);
519 if (isType (token, TOKEN_OPEN_SQUARE))
520 {
521 nest_level++;
522 }
523 if (isType (token, TOKEN_CLOSE_SQUARE))
524 {
525 if (nest_level > 0)
526 {
527 nest_level--;
528 }
529 }
530 }
531 readToken (token);
532 }
533 }
534
535 static void addContext (tokenInfo* const parent, const tokenInfo* const child)
536 {
537 if (vStringLength (parent->string) > 0)
538 {
539 vStringCatS (parent->string, ".");
540 }
541 vStringCatS (parent->string, vStringValue(child->string));
542 vStringTerminate(parent->string);
543 }
544
545 static void addToScope (tokenInfo* const token, vString* const extra)
546 {
547 if (vStringLength (token->scope) > 0)
548 {
549 vStringCatS (token->scope, ".");
550 }
551 vStringCatS (token->scope, vStringValue(extra));
552 vStringTerminate(token->scope);
553 }
554
555 /*
556 * Scanning functions
557 */
558
559 static void findCmdTerm (tokenInfo *const token)
560 {
561 /*
562 * Read until we find either a semicolon or closing brace.
563 * Any nested braces will be handled within.
564 */
565 while (! ( isType (token, TOKEN_SEMICOLON) ||
566 isType (token, TOKEN_CLOSE_CURLY) ) )
567 {
568 /* Handle nested blocks */
569 if ( isType (token, TOKEN_OPEN_CURLY))
570 {
571 parseBlock (token, token);
572 }
573 else if ( isType (token, TOKEN_OPEN_PAREN) )
574 {
575 skipArgumentList(token);
576 }
577 else
578 {
579 readToken (token);
580 }
581 }
582 }
583
584 static void parseSwitch (tokenInfo *const token)
585 {
586 /*
587 * switch (expression){
588 * case value1:
589 * statement;
590 * break;
591 * case value2:
592 * statement;
593 * break;
594 * default : statement;
595 * }
596 */
597
598 readToken (token);
599
600 if (isType (token, TOKEN_OPEN_PAREN))
601 {
602 /*
603 * Handle nameless functions, these will only
604 * be considered methods.
605 */
606 skipArgumentList(token);
607 }
608
609 if (isType (token, TOKEN_OPEN_CURLY))
610 {
611 /*
612 * This will be either a function or a class.
613 * We can only determine this by checking the body
614 * of the function. If we find a "this." we know
615 * it is a class, otherwise it is a function.
616 */
617 parseBlock (token, token);
618 }
619
620 }
621
622 static void parseLoop (tokenInfo *const token)
623 {
624 /*
625 * Handles these statements
626 * for (x=0; x<3; x++)
627 * document.write("This text is repeated three times<br>");
628 *
629 * for (x=0; x<3; x++)
630 * {
631 * document.write("This text is repeated three times<br>");
632 * }
633 *
634 * while (number<5){
635 * document.write(number+"<br>");
636 * number++;
637 * }
638 *
639 * do{
640 * document.write(number+"<br>");
641 * number++;
642 * }
643 * while (number<5);
644 */
645
646 if (isKeyword (token, KEYWORD_for) || isKeyword (token, KEYWORD_while))
647 {
648 readToken(token);
649
650 if (isType (token, TOKEN_OPEN_PAREN))
651 {
652 /*
653 * Handle nameless functions, these will only
654 * be considered methods.
655 */
656 skipArgumentList(token);
657 }
658
659 if (isType (token, TOKEN_OPEN_CURLY))
660 {
661 /*
662 * This will be either a function or a class.
663 * We can only determine this by checking the body
664 * of the function. If we find a "this." we know
665 * it is a class, otherwise it is a function.
666 */
667 parseBlock (token, token);
668 }
669 else
670 {
671 parseLine(token, FALSE);
672 }
673 }
674 else if (isKeyword (token, KEYWORD_do))
675 {
676 readToken(token);
677
678 if (isType (token, TOKEN_OPEN_CURLY))
679 {
680 /*
681 * This will be either a function or a class.
682 * We can only determine this by checking the body
683 * of the function. If we find a "this." we know
684 * it is a class, otherwise it is a function.
685 */
686 parseBlock (token, token);
687 }
688 else
689 {
690 parseLine(token, FALSE);
691 }
692
693 readToken(token);
694
695 if (isKeyword (token, KEYWORD_while))
696 {
697 readToken(token);
698
699 if (isType (token, TOKEN_OPEN_PAREN))
700 {
701 /*
702 * Handle nameless functions, these will only
703 * be considered methods.
704 */
705 skipArgumentList(token);
706 }
707 }
708 }
709 }
710
711 static boolean parseIf (tokenInfo *const token)
712 {
713 boolean read_next_token = TRUE;
714 /*
715 * If statements have two forms
716 * if ( ... )
717 * one line;
718 *
719 * if ( ... )
720 * statement;
721 * else
722 * statement
723 *
724 * if ( ... ) {
725 * multiple;
726 * statements;
727 * }
728 *
729 *
730 * if ( ... ) {
731 * return elem
732 * }
733 *
734 * This example if correctly written, but the
735 * else contains only 1 statement without a terminator
736 * since the function finishes with the closing brace.
737 *
738 * function a(flag){
739 * if(flag)
740 * test(1);
741 * else
742 * test(2)
743 * }
744 *
745 * TODO: Deal with statements that can optional end
746 * without a semi-colon. Currently this messes up
747 * the parsing of blocks.
748 * Need to somehow detect this has happened, and either
749 * backup a token, or skip reading the next token if
750 * that is possible from all code locations.
751 *
752 */
753
754 readToken (token);
755
756 if (isKeyword (token, KEYWORD_if))
757 {
758 /*
759 * Check for an "else if" and consume the "if"
760 */
761 readToken (token);
762 }
763
764 if (isType (token, TOKEN_OPEN_PAREN))
765 {
766 /*
767 * Handle nameless functions, these will only
768 * be considered methods.
769 */
770 skipArgumentList(token);
771 }
772
773 if (isType (token, TOKEN_OPEN_CURLY))
774 {
775 /*
776 * This will be either a function or a class.
777 * We can only determine this by checking the body
778 * of the function. If we find a "this." we know
779 * it is a class, otherwise it is a function.
780 */
781 parseBlock (token, token);
782 }
783 else
784 {
785 findCmdTerm (token);
786
787 /*
788 * The IF could be followed by an ELSE statement.
789 * This too could have two formats, a curly braced
790 * multiline section, or another single line.
791 */
792
793 if (isType (token, TOKEN_CLOSE_CURLY))
794 {
795 /*
796 * This statement did not have a line terminator.
797 */
798 read_next_token = FALSE;
799 }
800 else
801 {
802 readToken (token);
803
804 if (isType (token, TOKEN_CLOSE_CURLY))
805 {
806 /*
807 * This statement did not have a line terminator.
808 */
809 read_next_token = FALSE;
810 }
811 else
812 {
813 if (isKeyword (token, KEYWORD_else))
814 read_next_token = parseIf (token);
815 }
816 }
817 }
818 return read_next_token;
819 }
820
821 static void parseFunction (tokenInfo *const token)
822 {
823 tokenInfo *const name = newToken ();
824 boolean is_class = FALSE;
825
826 /*
827 * This deals with these formats
828 * function validFunctionTwo(a,b) {}
829 */
830
831 readToken (name);
832 /* Add scope in case this is an INNER function */
833 addToScope(name, token->scope);
834
835 readToken (token);
836 if (isType (token, TOKEN_PERIOD))
837 {
838 do
839 {
840 readToken (token);
841 if ( isKeyword(token, KEYWORD_NONE) )
842 {
843 addContext (name, token);
844 readToken (token);
845 }
846 } while (isType (token, TOKEN_PERIOD));
847 }
848
849 if ( isType (token, TOKEN_OPEN_PAREN) )
850 skipArgumentList(token);
851
852 if ( isType (token, TOKEN_OPEN_CURLY) )
853 {
854 is_class = parseBlock (token, name);
855 if ( is_class )
856 makeClassTag (name);
857 else
858 makeFunctionTag (name);
859 }
860
861 findCmdTerm (token);
862
863 deleteToken (name);
864 }
865
866 static boolean parseBlock (tokenInfo *const token, tokenInfo *const parent)
867 {
868 boolean is_class = FALSE;
869 boolean read_next_token = TRUE;
870 vString * saveScope = vStringNew ();
871
872 token->nestLevel++;
873 /*
874 * Make this routine a bit more forgiving.
875 * If called on an open_curly advance it
876 */
877 if ( isType (token, TOKEN_OPEN_CURLY) &&
878 isKeyword(token, KEYWORD_NONE) )
879 readToken(token);
880
881 if (! isType (token, TOKEN_CLOSE_CURLY))
882 {
883 /*
884 * Read until we find the closing brace,
885 * any nested braces will be handled within
886 */
887 do
888 {
889 read_next_token = TRUE;
890 if (isKeyword (token, KEYWORD_this))
891 {
892 /*
893 * Means we are inside a class and have found
894 * a class, not a function
895 */
896 is_class = TRUE;
897 vStringCopy(saveScope, token->scope);
898 addToScope (token, parent->string);
899
900 /*
901 * Ignore the remainder of the line
902 * findCmdTerm(token);
903 */
904 parseLine (token, is_class);
905
906 vStringCopy(token->scope, saveScope);
907 }
908 else if (isKeyword (token, KEYWORD_var))
909 {
910 /*
911 * Potentially we have found an inner function.
912 * Set something to indicate the scope
913 */
914 vStringCopy(saveScope, token->scope);
915 addToScope (token, parent->string);
916 parseLine (token, is_class);
917 vStringCopy(token->scope, saveScope);
918 }
919 else if (isKeyword (token, KEYWORD_function))
920 {
921 vStringCopy(saveScope, token->scope);
922 addToScope (token, parent->string);
923 parseFunction (token);
924 vStringCopy(token->scope, saveScope);
925 }
926 else if (isType (token, TOKEN_OPEN_CURLY))
927 {
928 /* Handle nested blocks */
929 parseBlock (token, parent);
930 }
931 else
932 {
933 /*
934 * It is possible for a line to have no terminator
935 * if the following line is a closing brace.
936 * parseLine will detect this case and indicate
937 * whether we should read an additional token.
938 */
939 read_next_token = parseLine (token, is_class);
940 }
941
942 /*
943 * Always read a new token unless we find a statement without
944 * a ending terminator
945 */
946 if( read_next_token )
947 readToken(token);
948
949 /*
950 * If we find a statement without a terminator consider the
951 * block finished, otherwise the stack will be off by one.
952 */
953 } while (! isType (token, TOKEN_CLOSE_CURLY) && read_next_token );
954 }
955
956 vStringDelete(saveScope);
957 token->nestLevel--;
958
959 return is_class;
960 }
961
962 static boolean parseMethods (tokenInfo *const token, tokenInfo *const class)
963 {
964 tokenInfo *const name = newToken ();
965 boolean has_methods = FALSE;
966
967 /*
968 * This deals with these formats
969 * validProperty : 2,
970 * validMethod : function(a,b) {}
971 * 'validMethod2' : function(a,b) {}
972 * container.dirtyTab = {'url': false, 'title':false, 'snapshot':false, '*': false}
973 */
974
975 do
976 {
977 readToken (token);
978 if (isType (token, TOKEN_CLOSE_CURLY))
979 {
980 /*
981 * This was most likely a variable declaration of a hash table.
982 * indicate there were no methods and return.
983 */
984 has_methods = FALSE;
985 goto cleanUp;
986 }
987
988 if (isType (token, TOKEN_STRING) || isKeyword(token, KEYWORD_NONE))
989 {
990 copyToken(name, token);
991
992 readToken (token);
993 if ( isType (token, TOKEN_COLON) )
994 {
995 readToken (token);
996 if ( isKeyword (token, KEYWORD_function) )
997 {
998 readToken (token);
999 if ( isType (token, TOKEN_OPEN_PAREN) )
1000 {
1001 skipArgumentList(token);
1002 }
1003
1004 if (isType (token, TOKEN_OPEN_CURLY))
1005 {
1006 has_methods = TRUE;
1007 addToScope (name, class->string);
1008 makeJsTag (name, JSTAG_METHOD);
1009 parseBlock (token, name);
1010
1011 /*
1012 * Read to the closing curly, check next
1013 * token, if a comma, we must loop again
1014 */
1015 readToken (token);
1016 }
1017 }
1018 else
1019 {
1020 has_methods = TRUE;
1021 addToScope (name, class->string);
1022 makeJsTag (name, JSTAG_PROPERTY);
1023
1024 /*
1025 * Read the next token, if a comma
1026 * we must loop again
1027 */
1028 readToken (token);
1029 }
1030 }
1031 }
1032 } while ( isType(token, TOKEN_COMMA) );
1033
1034 findCmdTerm (token);
1035
1036 cleanUp:
1037 deleteToken (name);
1038
1039 return has_methods;
1040 }
1041
1042 static boolean parseStatement (tokenInfo *const token, boolean is_inside_class)
1043 {
1044 tokenInfo *const name = newToken ();
1045 tokenInfo *const secondary_name = newToken ();
1046 vString * saveScope = vStringNew ();
1047 boolean is_class = FALSE;
1048 boolean is_terminated = TRUE;
1049 boolean is_global = FALSE;
1050 boolean is_prototype = FALSE;
1051 boolean has_methods = FALSE;
1052 vString * fulltag;
1053
1054 vStringClear(saveScope);
1055 /*
1056 * Functions can be named or unnamed.
1057 * This deals with these formats:
1058 * Function
1059 * validFunctionOne = function(a,b) {}
1060 * testlib.validFunctionFive = function(a,b) {}
1061 * var innerThree = function(a,b) {}
1062 * var innerFour = (a,b) {}
1063 * var D2 = secondary_fcn_name(a,b) {}
1064 * var D3 = new Function("a", "b", "return a+b;");
1065 * Class
1066 * testlib.extras.ValidClassOne = function(a,b) {
1067 * this.a = a;
1068 * }
1069 * Class Methods
1070 * testlib.extras.ValidClassOne.prototype = {
1071 * 'validMethodOne' : function(a,b) {},
1072 * 'validMethodTwo' : function(a,b) {}
1073 * }
1074 * ValidClassTwo = function ()
1075 * {
1076 * this.validMethodThree = function() {}
1077 * // unnamed method
1078 * this.validMethodFour = () {}
1079 * }
1080 * Database.prototype.validMethodThree = Database_getTodaysDate;
1081 */
1082
1083 if ( is_inside_class )
1084 is_class = TRUE;
1085 /*
1086 * var can preceed an inner function
1087 */
1088 if ( isKeyword(token, KEYWORD_var) )
1089 {
1090 /*
1091 * Only create variables for global scope
1092 */
1093 if ( token->nestLevel == 0 )
1094 {
1095 is_global = TRUE;
1096 }
1097 readToken(token);
1098 }
1099
1100 if ( isKeyword(token, KEYWORD_this) )
1101 {
1102 readToken(token);
1103 if (isType (token, TOKEN_PERIOD))
1104 {
1105 readToken(token);
1106 }
1107 }
1108
1109 copyToken(name, token);
1110
1111 while (! isType (token, TOKEN_CLOSE_CURLY) &&
1112 ! isType (token, TOKEN_SEMICOLON) &&
1113 ! isType (token, TOKEN_EQUAL_SIGN) )
1114 {
1115 /* Potentially the name of the function */
1116 readToken (token);
1117 if (isType (token, TOKEN_PERIOD))
1118 {
1119 /*
1120 * Cannot be a global variable is it has dot references in the name
1121 */
1122 is_global = FALSE;
1123 do
1124 {
1125 readToken (token);
1126 if ( isKeyword(token, KEYWORD_NONE) )
1127 {
1128 if ( is_class )
1129 {
1130 vStringCopy(saveScope, token->scope);
1131 addToScope(token, name->string);
1132 }
1133 else
1134 addContext (name, token);
1135 }
1136 else if ( isKeyword(token, KEYWORD_prototype) )
1137 {
1138 /*
1139 * When we reach the "prototype" tag, we infer:
1140 * "BindAgent" is a class
1141 * "build" is a method
1142 *
1143 * function BindAgent( repeatableIdName, newParentIdName ) {
1144 * }
1145 *
1146 * CASE 1
1147 * Specified function name: "build"
1148 * BindAgent.prototype.build = function( mode ) {
1149 * ignore everything within this function
1150 * }
1151 *
1152 * CASE 2
1153 * Prototype listing
1154 * ValidClassOne.prototype = {
1155 * 'validMethodOne' : function(a,b) {},
1156 * 'validMethodTwo' : function(a,b) {}
1157 * }
1158 *
1159 */
1160 makeClassTag (name);
1161 is_class = TRUE;
1162 is_prototype = TRUE;
1163
1164 /*
1165 * There should a ".function_name" next.
1166 */
1167 readToken (token);
1168 if (isType (token, TOKEN_PERIOD))
1169 {
1170 /*
1171 * Handle CASE 1
1172 */
1173 readToken (token);
1174 if ( isKeyword(token, KEYWORD_NONE) )
1175 {
1176 vStringCopy(saveScope, token->scope);
1177 addToScope(token, name->string);
1178
1179 makeJsTag (token, JSTAG_METHOD);
1180 /*
1181 * We can read until the end of the block / statement.
1182 * We need to correctly parse any nested blocks, but
1183 * we do NOT want to create any tags based on what is
1184 * within the blocks.
1185 */
1186 token->ignoreTag = TRUE;
1187 /*
1188 * Find to the end of the statement
1189 */
1190 findCmdTerm (token);
1191 token->ignoreTag = FALSE;
1192 is_terminated = TRUE;
1193 goto cleanUp;
1194 }
1195 }
1196 else if (isType (token, TOKEN_EQUAL_SIGN))
1197 {
1198 readToken (token);
1199 if (isType (token, TOKEN_OPEN_CURLY))
1200 {
1201 /*
1202 * Handle CASE 2
1203 *
1204 * Creates tags for each of these class methods
1205 * ValidClassOne.prototype = {
1206 * 'validMethodOne' : function(a,b) {},
1207 * 'validMethodTwo' : function(a,b) {}
1208 * }
1209 */
1210 parseMethods(token, name);
1211 /*
1212 * Find to the end of the statement
1213 */
1214 findCmdTerm (token);
1215 token->ignoreTag = FALSE;
1216 is_terminated = TRUE;
1217 goto cleanUp;
1218 }
1219 }
1220 }
1221 readToken (token);
1222 } while (isType (token, TOKEN_PERIOD));
1223 }
1224
1225 if ( isType (token, TOKEN_OPEN_PAREN) )
1226 skipArgumentList(token);
1227
1228 if ( isType (token, TOKEN_OPEN_SQUARE) )
1229 skipArrayList(token);
1230
1231 /*
1232 if ( isType (token, TOKEN_OPEN_CURLY) )
1233 {
1234 is_class = parseBlock (token, name);
1235 }
1236 */
1237 }
1238
1239 if ( isType (token, TOKEN_CLOSE_CURLY) )
1240 {
1241 /*
1242 * Reaching this section without having
1243 * processed an open curly brace indicates
1244 * the statement is most likely not terminated.
1245 */
1246 is_terminated = FALSE;
1247 goto cleanUp;
1248 }
1249
1250 if ( isType (token, TOKEN_SEMICOLON) )
1251 {
1252 /*
1253 * Only create variables for global scope
1254 */
1255 if ( token->nestLevel == 0 && is_global )
1256 {
1257 /*
1258 * Handles this syntax:
1259 * var g_var2;
1260 */
1261 if (isType (token, TOKEN_SEMICOLON))
1262 makeJsTag (name, JSTAG_VARIABLE);
1263 }
1264 /*
1265 * Statement has ended.
1266 * This deals with calls to functions, like:
1267 * alert(..);
1268 */
1269 goto cleanUp;
1270 }
1271
1272 if ( isType (token, TOKEN_EQUAL_SIGN) )
1273 {
1274 readToken (token);
1275
1276 if ( isKeyword (token, KEYWORD_function) )
1277 {
1278 readToken (token);
1279
1280 if ( isKeyword (token, KEYWORD_NONE) &&
1281 ! isType (token, TOKEN_OPEN_PAREN) )
1282 {
1283 /*
1284 * Functions of this format:
1285 * var D2A = function theAdd(a, b)
1286 * {
1287 * return a+b;
1288 * }
1289 * Are really two separate defined functions and
1290 * can be referenced in two ways:
1291 * alert( D2A(1,2) ); // produces 3
1292 * alert( theAdd(1,2) ); // also produces 3
1293 * So it must have two tags:
1294 * D2A
1295 * theAdd
1296 * Save the reference to the name for later use, once
1297 * we have established this is a valid function we will
1298 * create the secondary reference to it.
1299 */
1300 copyToken(secondary_name, token);
1301 readToken (token);
1302 }
1303
1304 if ( isType (token, TOKEN_OPEN_PAREN) )
1305 skipArgumentList(token);
1306
1307 if (isType (token, TOKEN_OPEN_CURLY))
1308 {
1309 /*
1310 * This will be either a function or a class.
1311 * We can only determine this by checking the body
1312 * of the function. If we find a "this." we know
1313 * it is a class, otherwise it is a function.
1314 */
1315 if ( is_inside_class )
1316 {
1317 makeJsTag (name, JSTAG_METHOD);
1318 if ( vStringLength(secondary_name->string) > 0 )
1319 makeFunctionTag (secondary_name);
1320 parseBlock (token, name);
1321 }
1322 else
1323 {
1324 is_class = parseBlock (token, name);
1325 if ( is_class )
1326 makeClassTag (name);
1327 else
1328 makeFunctionTag (name);
1329
1330 if ( vStringLength(secondary_name->string) > 0 )
1331 makeFunctionTag (secondary_name);
1332
1333 /*
1334 * Find to the end of the statement
1335 */
1336 goto cleanUp;
1337 }
1338 }
1339 }
1340 else if (isType (token, TOKEN_OPEN_PAREN))
1341 {
1342 /*
1343 * Handle nameless functions
1344 * this.method_name = () {}
1345 */
1346 skipArgumentList(token);
1347
1348 if (isType (token, TOKEN_OPEN_CURLY))
1349 {
1350 /*
1351 * Nameless functions are only setup as methods.
1352 */
1353 makeJsTag (name, JSTAG_METHOD);
1354 parseBlock (token, name);
1355 }
1356 }
1357 else if (isType (token, TOKEN_OPEN_CURLY))
1358 {
1359 /*
1360 * Creates tags for each of these class methods
1361 * ValidClassOne.prototype = {
1362 * 'validMethodOne' : function(a,b) {},
1363 * 'validMethodTwo' : function(a,b) {}
1364 * }
1365 * Or checks if this is a hash variable.
1366 * var z = {};
1367 */
1368 has_methods = parseMethods(token, name);
1369 if ( ! has_methods )
1370 {
1371 /*
1372 * Only create variables for global scope
1373 */
1374 if ( token->nestLevel == 0 && is_global )
1375 {
1376 /*
1377 * A pointer can be created to the function.
1378 * If we recognize the function/class name ignore the variable.
1379 * This format looks identical to a variable definition.
1380 * A variable defined outside of a block is considered
1381 * a global variable:
1382 * var g_var1 = 1;
1383 * var g_var2;
1384 * This is not a global variable:
1385 * var g_var = function;
1386 * This is a global variable:
1387 * var g_var = different_var_name;
1388 */
1389 fulltag = vStringNew ();
1390 if (vStringLength (token->scope) > 0)
1391 {
1392 vStringCopy(fulltag, token->scope);
1393 vStringCatS (fulltag, ".");
1394 vStringCatS (fulltag, vStringValue(token->string));
1395 }
1396 else
1397 {
1398 vStringCopy(fulltag, token->string);
1399 }
1400 vStringTerminate(fulltag);
1401 if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) &&
1402 ! stringListHas(ClassNames, vStringValue (fulltag)) )
1403 {
1404 readToken (token);
1405 if ( ! isType (token, TOKEN_SEMICOLON))
1406 findCmdTerm (token);
1407 if (isType (token, TOKEN_SEMICOLON))
1408 makeJsTag (name, JSTAG_VARIABLE);
1409 }
1410 vStringDelete (fulltag);
1411 }
1412 }
1413 if (isType (token, TOKEN_CLOSE_CURLY))
1414 {
1415 /*
1416 * Assume the closing parantheses terminates
1417 * this statements.
1418 */
1419 is_terminated = TRUE;
1420 }
1421 }
1422 else if (isKeyword (token, KEYWORD_new))
1423 {
1424 readToken (token);
1425 if ( isKeyword (token, KEYWORD_function) ||
1426 isKeyword (token, KEYWORD_capital_function) ||
1427 isKeyword (token, KEYWORD_object) ||
1428 isKeyword (token, KEYWORD_capital_object) )
1429 {
1430 if ( isKeyword (token, KEYWORD_object) ||
1431 isKeyword (token, KEYWORD_capital_object) )
1432 is_class = TRUE;
1433
1434 readToken (token);
1435 if ( isType (token, TOKEN_OPEN_PAREN) )
1436 skipArgumentList(token);
1437
1438 if (isType (token, TOKEN_SEMICOLON))
1439 {
1440 if ( token->nestLevel == 0 )
1441 {
1442 if ( is_class )
1443 {
1444 makeClassTag (name);
1445 } else {
1446 makeFunctionTag (name);
1447 }
1448 }
1449 }
1450 }
1451 }
1452 else if (isKeyword (token, KEYWORD_NONE))
1453 {
1454 /*
1455 * Only create variables for global scope
1456 */
1457 if ( token->nestLevel == 0 && is_global )
1458 {
1459 /*
1460 * A pointer can be created to the function.
1461 * If we recognize the function/class name ignore the variable.
1462 * This format looks identical to a variable definition.
1463 * A variable defined outside of a block is considered
1464 * a global variable:
1465 * var g_var1 = 1;
1466 * var g_var2;
1467 * This is not a global variable:
1468 * var g_var = function;
1469 * This is a global variable:
1470 * var g_var = different_var_name;
1471 */
1472 fulltag = vStringNew ();
1473 if (vStringLength (token->scope) > 0)
1474 {
1475 vStringCopy(fulltag, token->scope);
1476 vStringCatS (fulltag, ".");
1477 vStringCatS (fulltag, vStringValue(token->string));
1478 }
1479 else
1480 {
1481 vStringCopy(fulltag, token->string);
1482 }
1483 vStringTerminate(fulltag);
1484 if ( ! stringListHas(FunctionNames, vStringValue (fulltag)) &&
1485 ! stringListHas(ClassNames, vStringValue (fulltag)) )
1486 {
1487 findCmdTerm (token);
1488 if (isType (token, TOKEN_SEMICOLON))
1489 makeJsTag (name, JSTAG_VARIABLE);
1490 }
1491 vStringDelete (fulltag);
1492 }
1493 }
1494 }
1495 findCmdTerm (token);
1496
1497 /*
1498 * Statements can be optionally terminated in the case of
1499 * statement prior to a close curly brace as in the
1500 * document.write line below:
1501 *
1502 * function checkForUpdate() {
1503 * if( 1==1 ) {
1504 * document.write("hello from checkForUpdate<br>")
1505 * }
1506 * return 1;
1507 * }
1508 */
1509 if ( ! is_terminated && isType (token, TOKEN_CLOSE_CURLY))
1510 is_terminated = FALSE;
1511
1512
1513 cleanUp:
1514 vStringCopy(token->scope, saveScope);
1515 deleteToken (name);
1516 deleteToken (secondary_name);
1517 vStringDelete(saveScope);
1518
1519 return is_terminated;
1520 }
1521
1522 static boolean parseLine (tokenInfo *const token, boolean is_inside_class)
1523 {
1524 boolean is_terminated = TRUE;
1525 /*
1526 * Detect the common statements, if, while, for, do, ...
1527 * This is necessary since the last statement within a block "{}"
1528 * can be optionally terminated.
1529 *
1530 * If the statement is not terminated, we need to tell
1531 * the calling routine to prevent reading an additional token
1532 * looking for the end of the statement.
1533 */
1534
1535 if (isType(token, TOKEN_KEYWORD))
1536 {
1537 switch (token->keyword)
1538 {
1539 case KEYWORD_for:
1540 case KEYWORD_while:
1541 case KEYWORD_do:
1542 parseLoop (token);
1543 break;
1544 case KEYWORD_if:
1545 case KEYWORD_else:
1546 case KEYWORD_try:
1547 case KEYWORD_catch:
1548 case KEYWORD_finally:
1549 /* Common semantics */
1550 is_terminated = parseIf (token);
1551 break;
1552 case KEYWORD_switch:
1553 parseSwitch (token);
1554 break;
1555 default:
1556 parseStatement (token, is_inside_class);
1557 break;
1558 }
1559 }
1560 else
1561 {
1562 /*
1563 * Special case where single line statements may not be
1564 * SEMICOLON terminated. parseBlock needs to know this
1565 * so that it does not read the next token.
1566 */
1567 is_terminated = parseStatement (token, is_inside_class);
1568 }
1569 return is_terminated;
1570 }
1571
1572 static void parseJsFile (tokenInfo *const token)
1573 {
1574 do
1575 {
1576 readToken (token);
1577
1578 if (isType(token, TOKEN_KEYWORD))
1579 {
1580 switch (token->keyword)
1581 {
1582 case KEYWORD_function: parseFunction (token); break;
1583 default: parseLine (token, FALSE); break;
1584 }
1585 }
1586 else
1587 {
1588 parseLine (token, FALSE);
1589 }
1590 } while (TRUE);
1591 }
1592
1593 static void initialize (const langType language)
1594 {
1595 Assert (sizeof (JsKinds) / sizeof (JsKinds [0]) == JSTAG_COUNT);
1596 Lang_js = language;
1597 buildJsKeywordHash ();
1598 }
1599
1600 static void findJsTags (void)
1601 {
1602 tokenInfo *const token = newToken ();
1603 exception_t exception;
1604
1605 ClassNames = stringListNew ();
1606 FunctionNames = stringListNew ();
1607
1608 exception = (exception_t) (setjmp (Exception));
1609 while (exception == ExceptionNone)
1610 parseJsFile (token);
1611
1612 stringListDelete (ClassNames);
1613 stringListDelete (FunctionNames);
1614 ClassNames = NULL;
1615 FunctionNames = NULL;
1616 deleteToken (token);
1617 }
1618
1619 /* Create parser definition stucture */
1620 extern parserDefinition* JavaScriptParser (void)
1621 {
1622 static const char *const extensions [] = { "js", NULL };
1623 parserDefinition *const def = parserNew ("JavaScript");
1624 def->extensions = extensions;
1625 /*
1626 * New definitions for parsing instead of regex
1627 */
1628 def->kinds = JsKinds;
1629 def->kindCount = KIND_COUNT (JsKinds);
1630 def->parser = findJsTags;
1631 def->initialize = initialize;
1632
1633 return def;
1634 }
1635 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */