2 * Copyright (c) 2013, Colomban Wendling <ban@herbesfolles.org>
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License.
7 * This module contains code for generating tags for the PHP scripting
14 #include "general.h" /* must always come first */
23 #define SCOPE_SEPARATOR "::"
115 static kindOption PhpKinds
[COUNT_KIND
] = {
116 { TRUE
, 'c', "class", "classes" },
117 { TRUE
, 'd', "define", "constant definitions" },
118 { TRUE
, 'f', "function", "functions" },
119 { TRUE
, 'i', "interface", "interfaces" },
120 { FALSE
, 'l', "local", "local variables" },
121 { TRUE
, 'n', "namespace", "namespaces" },
122 { TRUE
, 't', "trait", "traits" },
123 { TRUE
, 'v', "variable", "variables" }
131 static const keywordDesc PhpKeywordTable
[] = {
132 /* keyword keyword ID */
133 { "abstract", KEYWORD_abstract
},
134 { "and", KEYWORD_and
},
135 { "as", KEYWORD_as
},
136 { "break", KEYWORD_break
},
137 { "callable", KEYWORD_callable
},
138 { "case", KEYWORD_case
},
139 { "catch", KEYWORD_catch
},
140 { "cfunction", KEYWORD_function
}, /* nobody knows what the hell this is, but it seems to behave much like "function" so bind it to it */
141 { "class", KEYWORD_class
},
142 { "clone", KEYWORD_clone
},
143 { "const", KEYWORD_const
},
144 { "continue", KEYWORD_continue
},
145 { "declare", KEYWORD_declare
},
146 { "define", KEYWORD_define
}, /* this isn't really a keyword but we handle it so it's easier this way */
147 { "default", KEYWORD_default
},
148 { "do", KEYWORD_do
},
149 { "echo", KEYWORD_echo
},
150 { "else", KEYWORD_else
},
151 { "elseif", KEYWORD_elif
},
152 { "enddeclare", KEYWORD_enddeclare
},
153 { "endfor", KEYWORD_endfor
},
154 { "endforeach", KEYWORD_endforeach
},
155 { "endif", KEYWORD_endif
},
156 { "endswitch", KEYWORD_endswitch
},
157 { "endwhile", KEYWORD_endwhile
},
158 { "extends", KEYWORD_extends
},
159 { "final", KEYWORD_final
},
160 { "finally", KEYWORD_finally
},
161 { "for", KEYWORD_for
},
162 { "foreach", KEYWORD_foreach
},
163 { "function", KEYWORD_function
},
164 { "global", KEYWORD_global
},
165 { "goto", KEYWORD_goto
},
166 { "if", KEYWORD_if
},
167 { "implements", KEYWORD_implements
},
168 { "include", KEYWORD_include
},
169 { "include_once", KEYWORD_include_once
},
170 { "instanceof", KEYWORD_instanceof
},
171 { "insteadof", KEYWORD_insteadof
},
172 { "interface", KEYWORD_interface
},
173 { "namespace", KEYWORD_namespace
},
174 { "new", KEYWORD_new
},
175 { "or", KEYWORD_or
},
176 { "print", KEYWORD_print
},
177 { "private", KEYWORD_private
},
178 { "protected", KEYWORD_protected
},
179 { "public", KEYWORD_public
},
180 { "require", KEYWORD_require
},
181 { "require_once", KEYWORD_require_once
},
182 { "return", KEYWORD_return
},
183 { "static", KEYWORD_static
},
184 { "switch", KEYWORD_switch
},
185 { "throw", KEYWORD_throw
},
186 { "trait", KEYWORD_trait
},
187 { "try", KEYWORD_try
},
188 { "use", KEYWORD_use
},
189 { "var", KEYWORD_var
},
190 { "while", KEYWORD_while
},
191 { "xor", KEYWORD_xor
},
192 { "yield", KEYWORD_yield
}
196 typedef enum eTokenType
{
224 unsigned long lineNumber
;
226 int parentKind
; /* -1 if none */
229 static langType Lang_php
;
230 static langType Lang_zephir
;
232 static boolean InPhp
= FALSE
; /* whether we are between <? ?> */
234 /* current statement details */
240 /* Current namespace */
241 static vString
*CurrentNamespace
;
244 static void buildPhpKeywordHash (const langType language
)
246 const size_t count
= sizeof (PhpKeywordTable
) / sizeof (PhpKeywordTable
[0]);
248 for (i
= 0; i
< count
; i
++)
250 const keywordDesc
* const p
= &PhpKeywordTable
[i
];
251 addKeyword (p
->name
, language
, (int) p
->id
);
255 static const char *accessToString (const accessType access
)
257 static const char *const names
[COUNT_ACCESS
] = {
264 Assert (access
< COUNT_ACCESS
);
266 return names
[access
];
269 static const char *implToString (const implType impl
)
271 static const char *const names
[COUNT_IMPL
] = {
276 Assert (impl
< COUNT_IMPL
);
281 static void initPhpEntry (tagEntryInfo
*const e
, const tokenInfo
*const token
,
282 const phpKind kind
, const accessType access
)
284 static vString
*fullScope
= NULL
;
287 if (fullScope
== NULL
)
288 fullScope
= vStringNew ();
290 vStringClear (fullScope
);
292 if (vStringLength (CurrentNamespace
) > 0)
294 vStringCopy (fullScope
, CurrentNamespace
);
295 parentKind
= K_NAMESPACE
;
298 initTagEntry (e
, vStringValue (token
->string
));
300 e
->lineNumber
= token
->lineNumber
;
301 e
->filePosition
= token
->filePosition
;
302 e
->kindName
= PhpKinds
[kind
].name
;
303 e
->kind
= (char) PhpKinds
[kind
].letter
;
305 if (access
!= ACCESS_UNDEFINED
)
306 e
->extensionFields
.access
= accessToString (access
);
307 if (vStringLength (token
->scope
) > 0)
309 parentKind
= token
->parentKind
;
310 if (vStringLength (fullScope
) > 0)
311 vStringCatS (fullScope
, SCOPE_SEPARATOR
);
312 vStringCat (fullScope
, token
->scope
);
314 if (vStringLength (fullScope
) > 0)
316 Assert (parentKind
>= 0);
318 vStringTerminate (fullScope
);
319 e
->extensionFields
.scope
[0] = PhpKinds
[parentKind
].name
;
320 e
->extensionFields
.scope
[1] = vStringValue (fullScope
);
324 static void makeSimplePhpTag (const tokenInfo
*const token
, const phpKind kind
,
325 const accessType access
)
327 if (PhpKinds
[kind
].enabled
)
331 initPhpEntry (&e
, token
, kind
, access
);
336 static void makeNamespacePhpTag (const tokenInfo
*const token
, const vString
*const name
)
338 if (PhpKinds
[K_NAMESPACE
].enabled
)
342 initTagEntry (&e
, vStringValue (name
));
344 e
.lineNumber
= token
->lineNumber
;
345 e
.filePosition
= token
->filePosition
;
346 e
.kindName
= PhpKinds
[K_NAMESPACE
].name
;
347 e
.kind
= (char) PhpKinds
[K_NAMESPACE
].letter
;
353 static void makeClassOrIfaceTag (const phpKind kind
, const tokenInfo
*const token
,
354 vString
*const inheritance
, const implType impl
)
356 if (PhpKinds
[kind
].enabled
)
360 initPhpEntry (&e
, token
, kind
, ACCESS_UNDEFINED
);
362 if (impl
!= IMPL_UNDEFINED
)
363 e
.extensionFields
.implementation
= implToString (impl
);
364 if (vStringLength (inheritance
) > 0)
365 e
.extensionFields
.inheritance
= vStringValue (inheritance
);
371 static void makeFunctionTag (const tokenInfo
*const token
,
372 const vString
*const arglist
,
373 const accessType access
, const implType impl
)
375 if (PhpKinds
[K_FUNCTION
].enabled
)
379 initPhpEntry (&e
, token
, K_FUNCTION
, access
);
381 if (impl
!= IMPL_UNDEFINED
)
382 e
.extensionFields
.implementation
= implToString (impl
);
384 e
.extensionFields
.signature
= vStringValue (arglist
);
390 static tokenInfo
*newToken (void)
392 tokenInfo
*const token
= xMalloc (1, tokenInfo
);
394 token
->type
= TOKEN_UNDEFINED
;
395 token
->keyword
= KEYWORD_NONE
;
396 token
->string
= vStringNew ();
397 token
->scope
= vStringNew ();
398 token
->lineNumber
= getSourceLineNumber ();
399 token
->filePosition
= getInputFilePosition ();
400 token
->parentKind
= -1;
405 static void deleteToken (tokenInfo
*const token
)
407 vStringDelete (token
->string
);
408 vStringDelete (token
->scope
);
412 static void copyToken (tokenInfo
*const dest
, const tokenInfo
*const src
,
415 dest
->lineNumber
= src
->lineNumber
;
416 dest
->filePosition
= src
->filePosition
;
417 dest
->type
= src
->type
;
418 dest
->keyword
= src
->keyword
;
419 vStringCopy(dest
->string
, src
->string
);
420 dest
->parentKind
= src
->parentKind
;
422 vStringCopy(dest
->scope
, src
->scope
);
428 static const char *tokenTypeName (const tokenType type
)
432 case TOKEN_UNDEFINED
: return "undefined";
433 case TOKEN_EOF
: return "EOF";
434 case TOKEN_CHARACTER
: return "character";
435 case TOKEN_CLOSE_PAREN
: return "')'";
436 case TOKEN_SEMICOLON
: return "';'";
437 case TOKEN_COLON
: return "':'";
438 case TOKEN_COMMA
: return "','";
439 case TOKEN_OPEN_PAREN
: return "'('";
440 case TOKEN_OPERATOR
: return "operator";
441 case TOKEN_IDENTIFIER
: return "identifier";
442 case TOKEN_KEYWORD
: return "keyword";
443 case TOKEN_STRING
: return "string";
444 case TOKEN_PERIOD
: return "'.'";
445 case TOKEN_OPEN_CURLY
: return "'{'";
446 case TOKEN_CLOSE_CURLY
: return "'}'";
447 case TOKEN_EQUAL_SIGN
: return "'='";
448 case TOKEN_OPEN_SQUARE
: return "'['";
449 case TOKEN_CLOSE_SQUARE
: return "']'";
450 case TOKEN_VARIABLE
: return "variable";
455 static void printToken (const tokenInfo
*const token
)
457 fprintf (stderr
, "%p:\n\ttype:\t%s\n\tline:\t%lu\n\tscope:\t%s\n", (void *) token
,
458 tokenTypeName (token
->type
),
460 vStringValue (token
->scope
));
463 case TOKEN_IDENTIFIER
:
466 fprintf (stderr
, "\tcontent:\t%s\n", vStringValue (token
->string
));
471 size_t n
= sizeof PhpKeywordTable
/ sizeof PhpKeywordTable
[0];
474 fprintf (stderr
, "\tkeyword:\t");
475 for (i
= 0; i
< n
; i
++)
477 if (PhpKeywordTable
[i
].id
== token
->keyword
)
479 fprintf (stderr
, "%s\n", PhpKeywordTable
[i
].name
);
484 fprintf (stderr
, "(unknown)\n");
492 static void addToScope (tokenInfo
*const token
, const vString
*const extra
)
494 if (vStringLength (token
->scope
) > 0)
495 vStringCatS (token
->scope
, SCOPE_SEPARATOR
);
496 vStringCatS (token
->scope
, vStringValue (extra
));
497 vStringTerminate(token
->scope
);
500 static boolean
isIdentChar (const int c
)
502 return (isalnum (c
) || c
== '_' || c
>= 0x80);
505 static int skipToCharacter (const int c
)
511 } while (d
!= EOF
&& d
!= c
);
515 static void parseString (vString
*const string
, const int delimiter
)
521 if (c
== '\\' && (c
= fileGetc ()) != EOF
)
522 vStringPut (string
, (char) c
);
523 else if (c
== EOF
|| c
== delimiter
)
526 vStringPut (string
, (char) c
);
528 vStringTerminate (string
);
531 /* reads an HereDoc or a NowDoc (the part after the <<<).
532 * <<<[ \t]*(ID|'ID'|"ID")
537 * 1) starting ID must be immediately followed by a newline;
538 * 2) closing ID is the same as opening one;
539 * 3) closing ID must be immediately followed by a newline or a semicolon
542 * Example of a *single* valid heredoc:
546 * FOO this is not an end
547 * FOO; this isn't either
548 * FOO; # neither this is
550 * # previous line was the end, but the semicolon wasn't required
552 static void parseHeredoc (vString
*const string
)
556 char delimiter
[64]; /* arbitrary limit, but more is crazy anyway */
563 while (c
== ' ' || c
== '\t');
565 if (c
== '\'' || c
== '"')
570 for (len
= 0; len
< (sizeof delimiter
/ sizeof delimiter
[0]) - 1; len
++)
572 if (! isIdentChar (c
))
574 delimiter
[len
] = (char) c
;
579 if (len
== 0) /* no delimiter, give up */
583 if (c
!= quote
) /* no closing quote for quoted identifier, give up */
587 if (c
!= '\r' && c
!= '\n') /* missing newline, give up */
594 if (c
!= '\r' && c
!= '\n')
595 vStringPut (string
, (char) c
);
598 /* new line, check for a delimiter right after */
603 for (len
= 0; c
!= 0 && (c
- delimiter
[len
]) == 0; len
++)
606 if (delimiter
[len
] != 0)
610 /* line start matched the delimiter, now check whether there
611 * is anything after it */
612 if (c
== '\r' || c
== '\n')
620 if (d
== '\r' || d
== '\n')
622 /* put back the semicolon since it's not part of the
623 * string. we can't put back the newline, but it's a
624 * whitespace character nobody cares about it anyway */
630 /* put semicolon in the string and continue */
636 /* if we are here it wasn't a delimiter, so put everything in the
638 vStringPut (string
, (char) nl
);
639 vStringNCatS (string
, delimiter
, len
);
641 vStringPut (string
, (char) extra
);
646 vStringTerminate (string
);
654 static void parseIdentifier (vString
*const string
, const int firstChar
)
659 vStringPut (string
, (char) c
);
661 } while (isIdentChar (c
));
663 vStringTerminate (string
);
666 static keywordId
analyzeToken (vString
*const name
, langType language
)
668 vString
*keyword
= vStringNew ();
670 vStringCopyToLower (keyword
, name
);
671 result
= lookupKeyword (vStringValue (keyword
), language
);
672 vStringDelete (keyword
);
676 static boolean
isSpace (int c
)
678 return (c
== '\t' || c
== ' ' || c
== '\v' ||
679 c
== '\n' || c
== '\r' || c
== '\f');
682 static int skipWhitespaces (int c
)
689 /* <script[:white:]+language[:white:]*=[:white:]*(php|'php'|"php")[:white:]*>
691 * This is ugly, but the whole "<script language=php>" tag is and we can't
692 * really do better without adding a lot of code only for this */
693 static boolean
isOpenScriptLanguagePhp (int c
)
697 /* <script[:white:]+language[:white:]*= */
699 tolower ((c
= fileGetc ())) != 's' ||
700 tolower ((c
= fileGetc ())) != 'c' ||
701 tolower ((c
= fileGetc ())) != 'r' ||
702 tolower ((c
= fileGetc ())) != 'i' ||
703 tolower ((c
= fileGetc ())) != 'p' ||
704 tolower ((c
= fileGetc ())) != 't' ||
705 ! isSpace ((c
= fileGetc ())) ||
706 tolower ((c
= skipWhitespaces (c
))) != 'l' ||
707 tolower ((c
= fileGetc ())) != 'a' ||
708 tolower ((c
= fileGetc ())) != 'n' ||
709 tolower ((c
= fileGetc ())) != 'g' ||
710 tolower ((c
= fileGetc ())) != 'u' ||
711 tolower ((c
= fileGetc ())) != 'a' ||
712 tolower ((c
= fileGetc ())) != 'g' ||
713 tolower ((c
= fileGetc ())) != 'e' ||
714 (c
= skipWhitespaces (fileGetc ())) != '=')
717 /* (php|'php'|"php")> */
718 c
= skipWhitespaces (fileGetc ());
719 if (c
== '"' || c
== '\'')
724 if (tolower (c
) != 'p' ||
725 tolower ((c
= fileGetc ())) != 'h' ||
726 tolower ((c
= fileGetc ())) != 'p' ||
727 (quote
!= 0 && (c
= fileGetc ()) != quote
) ||
728 (c
= skipWhitespaces (fileGetc ())) != '>')
734 static int findPhpStart (void)
739 if ((c
= fileGetc ()) == '<')
742 /* <? and <?php, but not <?xml */
745 /* don't enter PHP mode on "<?xml", yet still support short open tags (<?) */
746 if (tolower ((c
= fileGetc ())) != 'x' ||
747 tolower ((c
= fileGetc ())) != 'm' ||
748 tolower ((c
= fileGetc ())) != 'l')
753 /* <script language="php"> */
757 if (isOpenScriptLanguagePhp ('<'))
767 static int skipSingleComment (void)
775 int next
= fileGetc ();
781 /* ?> in single-line comments leaves PHP mode */
784 int next
= fileGetc ();
790 } while (InPhp
&& c
!= EOF
&& c
!= '\n' && c
!= '\r');
794 static void readToken (tokenInfo
*const token
)
798 token
->type
= TOKEN_UNDEFINED
;
799 token
->keyword
= KEYWORD_NONE
;
800 vStringClear (token
->string
);
813 c
= skipWhitespaces (c
);
815 token
->lineNumber
= getSourceLineNumber ();
816 token
->filePosition
= getInputFilePosition ();
820 case EOF
: token
->type
= TOKEN_EOF
; break;
821 case '(': token
->type
= TOKEN_OPEN_PAREN
; break;
822 case ')': token
->type
= TOKEN_CLOSE_PAREN
; break;
823 case ';': token
->type
= TOKEN_SEMICOLON
; break;
824 case ',': token
->type
= TOKEN_COMMA
; break;
825 case '.': token
->type
= TOKEN_PERIOD
; break;
826 case ':': token
->type
= TOKEN_COLON
; break;
827 case '{': token
->type
= TOKEN_OPEN_CURLY
; break;
828 case '}': token
->type
= TOKEN_CLOSE_CURLY
; break;
829 case '[': token
->type
= TOKEN_OPEN_SQUARE
; break;
830 case ']': token
->type
= TOKEN_CLOSE_SQUARE
; break;
831 case '&': token
->type
= TOKEN_AMPERSAND
; break;
836 if (d
== '=' || d
== '>')
837 token
->type
= TOKEN_OPERATOR
;
841 token
->type
= TOKEN_EQUAL_SIGN
;
848 token
->type
= TOKEN_STRING
;
849 parseString (token
->string
, c
);
850 token
->lineNumber
= getSourceLineNumber ();
851 token
->filePosition
= getInputFilePosition ();
859 /* </script[:white:]*> */
860 if (tolower ((d
= fileGetc ())) == 's' &&
861 tolower ((d
= fileGetc ())) == 'c' &&
862 tolower ((d
= fileGetc ())) == 'r' &&
863 tolower ((d
= fileGetc ())) == 'i' &&
864 tolower ((d
= fileGetc ())) == 'p' &&
865 tolower ((d
= fileGetc ())) == 't' &&
866 (d
= skipWhitespaces (fileGetc ())) == '>')
874 token
->type
= TOKEN_UNDEFINED
;
877 else if (d
== '<' && (d
= fileGetc ()) == '<')
879 token
->type
= TOKEN_STRING
;
880 parseHeredoc (token
->string
);
885 token
->type
= TOKEN_UNDEFINED
;
890 case '#': /* comment */
891 skipSingleComment ();
903 token
->type
= TOKEN_OPERATOR
;
907 case '/': /* division or comment start */
910 if (d
== '/') /* single-line comment */
912 skipSingleComment ();
919 c
= skipToCharacter ('*');
928 } while (c
!= EOF
&& c
!= '\0');
935 token
->type
= TOKEN_OPERATOR
;
940 case '$': /* variable start */
943 if (! isIdentChar (d
))
946 token
->type
= TOKEN_UNDEFINED
;
950 parseIdentifier (token
->string
, d
);
951 token
->type
= TOKEN_VARIABLE
;
956 case '?': /* maybe the end of the PHP chunk */
967 token
->type
= TOKEN_UNDEFINED
;
973 if (! isIdentChar (c
))
974 token
->type
= TOKEN_UNDEFINED
;
977 parseIdentifier (token
->string
, c
);
978 token
->keyword
= analyzeToken (token
->string
, getSourceLanguage ());
979 if (token
->keyword
== KEYWORD_NONE
)
980 token
->type
= TOKEN_IDENTIFIER
;
982 token
->type
= TOKEN_KEYWORD
;
987 if (token
->type
== TOKEN_SEMICOLON
||
988 token
->type
== TOKEN_OPEN_CURLY
||
989 token
->type
== TOKEN_CLOSE_CURLY
)
991 /* reset current statement details on statement end, and when entering
993 * it is a bit ugly to do this in readToken(), but it makes everything
995 CurrentStatement
.access
= ACCESS_UNDEFINED
;
996 CurrentStatement
.impl
= IMPL_UNDEFINED
;
1000 static void enterScope (tokenInfo
*const parentToken
,
1001 const vString
*const extraScope
,
1002 const int parentKind
);
1004 /* parses a class or an interface:
1006 * class Foo extends Bar {}
1007 * class Foo extends Bar implements iFoo, iBar {}
1009 * interface iBar extends iFoo {} */
1010 static boolean
parseClassOrIface (tokenInfo
*const token
, const phpKind kind
)
1012 boolean readNext
= TRUE
;
1013 implType impl
= CurrentStatement
.impl
;
1015 vString
*inheritance
= NULL
;
1018 if (token
->type
!= TOKEN_IDENTIFIER
)
1022 copyToken (name
, token
, TRUE
);
1024 inheritance
= vStringNew ();
1025 /* skip until the open bracket and assume every identifier (not keyword)
1026 * is an inheritance (like in "class Foo extends Bar implements iA, iB") */
1031 if (token
->type
== TOKEN_IDENTIFIER
)
1033 if (vStringLength (inheritance
) > 0)
1034 vStringPut (inheritance
, ',');
1035 vStringCat (inheritance
, token
->string
);
1038 while (token
->type
!= TOKEN_EOF
&&
1039 token
->type
!= TOKEN_OPEN_CURLY
);
1041 makeClassOrIfaceTag (kind
, name
, inheritance
, impl
);
1043 if (token
->type
== TOKEN_OPEN_CURLY
)
1044 enterScope (token
, name
->string
, K_CLASS
);
1049 vStringDelete (inheritance
);
1056 static boolean
parseTrait (tokenInfo
*const token
)
1058 boolean readNext
= TRUE
;
1062 if (token
->type
!= TOKEN_IDENTIFIER
)
1066 copyToken (name
, token
, TRUE
);
1068 makeSimplePhpTag (name
, K_TRAIT
, ACCESS_UNDEFINED
);
1071 if (token
->type
== TOKEN_OPEN_CURLY
)
1072 enterScope (token
, name
->string
, K_TRAIT
);
1083 * if @name is NULL, parses a normal function
1084 * function myfunc($foo, $bar) {}
1085 * function &myfunc($foo, $bar) {}
1087 * if @name is not NULL, parses an anonymous function with name @name
1088 * $foo = function($foo, $bar) {}
1089 * $foo = function&($foo, $bar) {}
1090 * $foo = function($foo, $bar) use ($x, &$y) {} */
1091 static boolean
parseFunction (tokenInfo
*const token
, const tokenInfo
*name
)
1093 boolean readNext
= TRUE
;
1094 accessType access
= CurrentStatement
.access
;
1095 implType impl
= CurrentStatement
.impl
;
1096 tokenInfo
*nameFree
= NULL
;
1099 /* skip a possible leading ampersand (return by reference) */
1100 if (token
->type
== TOKEN_AMPERSAND
)
1105 if (token
->type
!= TOKEN_IDENTIFIER
)
1108 name
= nameFree
= newToken ();
1109 copyToken (nameFree
, token
, TRUE
);
1113 if (token
->type
== TOKEN_OPEN_PAREN
)
1115 vString
*arglist
= vStringNew ();
1118 vStringPut (arglist
, '(');
1123 switch (token
->type
)
1125 case TOKEN_OPEN_PAREN
: depth
++; break;
1126 case TOKEN_CLOSE_PAREN
: depth
--; break;
1130 switch (token
->type
)
1132 case TOKEN_AMPERSAND
: vStringPut (arglist
, '&'); break;
1133 case TOKEN_CLOSE_CURLY
: vStringPut (arglist
, '}'); break;
1134 case TOKEN_CLOSE_PAREN
: vStringPut (arglist
, ')'); break;
1135 case TOKEN_CLOSE_SQUARE
: vStringPut (arglist
, ']'); break;
1136 case TOKEN_COLON
: vStringPut (arglist
, ':'); break;
1137 case TOKEN_COMMA
: vStringCatS (arglist
, ", "); break;
1138 case TOKEN_EQUAL_SIGN
: vStringCatS (arglist
, " = "); break;
1139 case TOKEN_OPEN_CURLY
: vStringPut (arglist
, '{'); break;
1140 case TOKEN_OPEN_PAREN
: vStringPut (arglist
, '('); break;
1141 case TOKEN_OPEN_SQUARE
: vStringPut (arglist
, '['); break;
1142 case TOKEN_PERIOD
: vStringPut (arglist
, '.'); break;
1143 case TOKEN_SEMICOLON
: vStringPut (arglist
, ';'); break;
1144 case TOKEN_STRING
: vStringCatS (arglist
, "'...'"); break;
1146 case TOKEN_IDENTIFIER
:
1148 case TOKEN_VARIABLE
:
1150 switch (vStringLast (arglist
))
1158 /* no need for a space between those and the identifier */
1162 vStringPut (arglist
, ' ');
1165 if (token
->type
== TOKEN_VARIABLE
)
1166 vStringPut (arglist
, '$');
1167 vStringCat (arglist
, token
->string
);
1174 while (token
->type
!= TOKEN_EOF
&& depth
> 0);
1176 vStringTerminate (arglist
);
1178 makeFunctionTag (name
, arglist
, access
, impl
);
1179 vStringDelete (arglist
);
1181 readToken (token
); /* normally it's an open brace or "use" keyword */
1184 /* if parsing Zephir, skip function return type hint */
1185 if (getSourceLanguage () == Lang_zephir
&& token
->type
== TOKEN_OPERATOR
)
1189 while (token
->type
!= TOKEN_EOF
&&
1190 token
->type
!= TOKEN_OPEN_CURLY
&&
1191 token
->type
!= TOKEN_CLOSE_CURLY
&&
1192 token
->type
!= TOKEN_SEMICOLON
);
1196 if (token
->type
== TOKEN_KEYWORD
&& token
->keyword
== KEYWORD_use
)
1199 if (token
->type
== TOKEN_OPEN_PAREN
)
1206 switch (token
->type
)
1208 case TOKEN_OPEN_PAREN
: depth
++; break;
1209 case TOKEN_CLOSE_PAREN
: depth
--; break;
1213 while (token
->type
!= TOKEN_EOF
&& depth
> 0);
1219 if (token
->type
== TOKEN_OPEN_CURLY
)
1220 enterScope (token
, name
->string
, K_FUNCTION
);
1225 deleteToken (nameFree
);
1230 /* parses declarations of the form
1231 * const NAME = VALUE */
1232 static boolean
parseConstant (tokenInfo
*const token
)
1236 readToken (token
); /* skip const keyword */
1237 if (token
->type
!= TOKEN_IDENTIFIER
)
1241 copyToken (name
, token
, TRUE
);
1244 if (token
->type
== TOKEN_EQUAL_SIGN
)
1245 makeSimplePhpTag (name
, K_DEFINE
, ACCESS_UNDEFINED
);
1249 return token
->type
== TOKEN_EQUAL_SIGN
;
1252 /* parses declarations of the form
1253 * define('NAME', 'VALUE')
1254 * define(NAME, 'VALUE) */
1255 static boolean
parseDefine (tokenInfo
*const token
)
1259 readToken (token
); /* skip "define" identifier */
1260 if (token
->type
!= TOKEN_OPEN_PAREN
)
1264 if (token
->type
== TOKEN_STRING
||
1265 token
->type
== TOKEN_IDENTIFIER
)
1267 makeSimplePhpTag (token
, K_DEFINE
, ACCESS_UNDEFINED
);
1271 /* skip until the close parenthesis.
1272 * no need to handle nested blocks since they would be invalid
1273 * in this context anyway (the VALUE may only be a scalar, like
1277 while (token
->type
!= TOKEN_EOF
&& depth
> 0)
1279 switch (token
->type
)
1281 case TOKEN_OPEN_PAREN
: depth
++; break;
1282 case TOKEN_CLOSE_PAREN
: depth
--; break;
1291 /* parses declarations of the form
1294 static boolean
parseVariable (tokenInfo
*const token
)
1297 boolean readNext
= TRUE
;
1298 accessType access
= CurrentStatement
.access
;
1301 copyToken (name
, token
, TRUE
);
1304 if (token
->type
== TOKEN_EQUAL_SIGN
)
1306 phpKind kind
= K_VARIABLE
;
1308 if (token
->parentKind
== K_FUNCTION
)
1309 kind
= K_LOCAL_VARIABLE
;
1312 if (token
->type
== TOKEN_KEYWORD
&&
1313 token
->keyword
== KEYWORD_function
&&
1314 PhpKinds
[kind
].enabled
)
1316 if (parseFunction (token
, name
))
1318 readNext
= (boolean
) (token
->type
== TOKEN_SEMICOLON
);
1322 makeSimplePhpTag (name
, kind
, access
);
1326 else if (token
->type
== TOKEN_SEMICOLON
)
1328 /* generate tags for variable declarations in classes
1332 * but don't get fooled by stuff like $foo = $bar; */
1333 if (token
->parentKind
== K_CLASS
|| token
->parentKind
== K_INTERFACE
)
1334 makeSimplePhpTag (name
, K_VARIABLE
, access
);
1344 /* parses namespace declarations
1346 * namespace Foo\Bar {}
1348 * namespace Foo\Bar;
1351 static boolean
parseNamespace (tokenInfo
*const token
)
1353 tokenInfo
*nsToken
= newToken ();
1355 vStringClear (CurrentNamespace
);
1356 copyToken (nsToken
, token
, FALSE
);
1361 if (token
->type
== TOKEN_IDENTIFIER
)
1363 if (vStringLength (CurrentNamespace
) > 0)
1364 vStringPut (CurrentNamespace
, '\\');
1365 vStringCat (CurrentNamespace
, token
->string
);
1368 while (token
->type
!= TOKEN_EOF
&&
1369 token
->type
!= TOKEN_SEMICOLON
&&
1370 token
->type
!= TOKEN_OPEN_CURLY
);
1372 vStringTerminate (CurrentNamespace
);
1373 if (vStringLength (CurrentNamespace
) > 0)
1374 makeNamespacePhpTag (nsToken
, CurrentNamespace
);
1376 if (token
->type
== TOKEN_OPEN_CURLY
)
1377 enterScope (token
, NULL
, -1);
1379 deleteToken (nsToken
);
1384 static void enterScope (tokenInfo
*const parentToken
,
1385 const vString
*const extraScope
,
1386 const int parentKind
)
1388 tokenInfo
*token
= newToken ();
1389 int origParentKind
= parentToken
->parentKind
;
1391 copyToken (token
, parentToken
, TRUE
);
1395 addToScope (token
, extraScope
);
1396 token
->parentKind
= parentKind
;
1400 while (token
->type
!= TOKEN_EOF
&&
1401 token
->type
!= TOKEN_CLOSE_CURLY
)
1403 boolean readNext
= TRUE
;
1405 switch (token
->type
)
1407 case TOKEN_OPEN_CURLY
:
1408 enterScope (token
, NULL
, -1);
1412 switch (token
->keyword
)
1414 case KEYWORD_class
: readNext
= parseClassOrIface (token
, K_CLASS
); break;
1415 case KEYWORD_interface
: readNext
= parseClassOrIface (token
, K_INTERFACE
); break;
1416 case KEYWORD_trait
: readNext
= parseTrait (token
); break;
1417 case KEYWORD_function
: readNext
= parseFunction (token
, NULL
); break;
1418 case KEYWORD_const
: readNext
= parseConstant (token
); break;
1419 case KEYWORD_define
: readNext
= parseDefine (token
); break;
1421 case KEYWORD_namespace
: readNext
= parseNamespace (token
); break;
1423 case KEYWORD_private
: CurrentStatement
.access
= ACCESS_PRIVATE
; break;
1424 case KEYWORD_protected
: CurrentStatement
.access
= ACCESS_PROTECTED
; break;
1425 case KEYWORD_public
: CurrentStatement
.access
= ACCESS_PUBLIC
; break;
1426 case KEYWORD_var
: CurrentStatement
.access
= ACCESS_PUBLIC
; break;
1428 case KEYWORD_abstract
: CurrentStatement
.impl
= IMPL_ABSTRACT
; break;
1434 case TOKEN_VARIABLE
:
1435 readNext
= parseVariable (token
);
1445 copyToken (parentToken
, token
, FALSE
);
1446 parentToken
->parentKind
= origParentKind
;
1447 deleteToken (token
);
1450 static void findTags (void)
1452 tokenInfo
*const token
= newToken ();
1454 CurrentStatement
.access
= ACCESS_UNDEFINED
;
1455 CurrentStatement
.impl
= IMPL_UNDEFINED
;
1456 CurrentNamespace
= vStringNew ();
1460 enterScope (token
, NULL
, -1);
1462 while (token
->type
!= TOKEN_EOF
); /* keep going even with unmatched braces */
1464 vStringDelete (CurrentNamespace
);
1465 deleteToken (token
);
1468 static void findPhpTags (void)
1474 static void findZephirTags (void)
1480 static void initializePhpParser (const langType language
)
1482 Lang_php
= language
;
1483 buildPhpKeywordHash (language
);
1486 static void initializeZephirParser (const langType language
)
1488 Lang_zephir
= language
;
1489 buildPhpKeywordHash (language
);
1492 extern parserDefinition
* PhpParser (void)
1494 static const char *const extensions
[] = { "php", "php3", "php4", "php5", "phtml", NULL
};
1495 parserDefinition
* def
= parserNew ("PHP");
1496 def
->kinds
= PhpKinds
;
1497 def
->kindCount
= KIND_COUNT (PhpKinds
);
1498 def
->extensions
= extensions
;
1499 def
->parser
= findPhpTags
;
1500 def
->initialize
= initializePhpParser
;
1504 extern parserDefinition
* ZephirParser (void)
1506 static const char *const extensions
[] = { "zep", NULL
};
1507 parserDefinition
* def
= parserNew ("Zephir");
1508 def
->kinds
= PhpKinds
;
1509 def
->kindCount
= KIND_COUNT (PhpKinds
);
1510 def
->extensions
= extensions
;
1511 def
->parser
= findZephirTags
;
1512 def
->initialize
= initializeZephirParser
;
1516 /* vi:set tabstop=4 shiftwidth=4: */