Debian Codegraph @

jscript.c

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