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 version 2 or (at your option) any later version.
7 * This module contains code for generating tags for the PHP scripting
14 #include "general.h" /* must always come first */
25 #define SCOPE_SEPARATOR "::"
117 static kindOption PhpKinds
[COUNT_KIND
] = {
118 { TRUE
, 'c', "class", "classes" },
119 { TRUE
, 'd', "define", "constant definitions" },
120 { TRUE
, 'f', "function", "functions" },
121 { TRUE
, 'i', "interface", "interfaces" },
122 { FALSE
, 'l', "local", "local variables" },
123 { TRUE
, 'n', "namespace", "namespaces" },
124 { TRUE
, 't', "trait", "traits" },
125 { TRUE
, 'v', "variable", "variables" }
133 static const keywordDesc PhpKeywordTable
[] = {
134 /* keyword keyword ID */
135 { "abstract", KEYWORD_abstract
},
136 { "and", KEYWORD_and
},
137 { "as", KEYWORD_as
},
138 { "break", KEYWORD_break
},
139 { "callable", KEYWORD_callable
},
140 { "case", KEYWORD_case
},
141 { "catch", KEYWORD_catch
},
142 { "cfunction", KEYWORD_function
}, /* nobody knows what the hell this is, but it seems to behave much like "function" so bind it to it */
143 { "class", KEYWORD_class
},
144 { "clone", KEYWORD_clone
},
145 { "const", KEYWORD_const
},
146 { "continue", KEYWORD_continue
},
147 { "declare", KEYWORD_declare
},
148 { "define", KEYWORD_define
}, /* this isn't really a keyword but we handle it so it's easier this way */
149 { "default", KEYWORD_default
},
150 { "do", KEYWORD_do
},
151 { "echo", KEYWORD_echo
},
152 { "else", KEYWORD_else
},
153 { "elseif", KEYWORD_elif
},
154 { "enddeclare", KEYWORD_enddeclare
},
155 { "endfor", KEYWORD_endfor
},
156 { "endforeach", KEYWORD_endforeach
},
157 { "endif", KEYWORD_endif
},
158 { "endswitch", KEYWORD_endswitch
},
159 { "endwhile", KEYWORD_endwhile
},
160 { "extends", KEYWORD_extends
},
161 { "final", KEYWORD_final
},
162 { "finally", KEYWORD_finally
},
163 { "for", KEYWORD_for
},
164 { "foreach", KEYWORD_foreach
},
165 { "function", KEYWORD_function
},
166 { "global", KEYWORD_global
},
167 { "goto", KEYWORD_goto
},
168 { "if", KEYWORD_if
},
169 { "implements", KEYWORD_implements
},
170 { "include", KEYWORD_include
},
171 { "include_once", KEYWORD_include_once
},
172 { "instanceof", KEYWORD_instanceof
},
173 { "insteadof", KEYWORD_insteadof
},
174 { "interface", KEYWORD_interface
},
175 { "namespace", KEYWORD_namespace
},
176 { "new", KEYWORD_new
},
177 { "or", KEYWORD_or
},
178 { "print", KEYWORD_print
},
179 { "private", KEYWORD_private
},
180 { "protected", KEYWORD_protected
},
181 { "public", KEYWORD_public
},
182 { "require", KEYWORD_require
},
183 { "require_once", KEYWORD_require_once
},
184 { "return", KEYWORD_return
},
185 { "static", KEYWORD_static
},
186 { "switch", KEYWORD_switch
},
187 { "throw", KEYWORD_throw
},
188 { "trait", KEYWORD_trait
},
189 { "try", KEYWORD_try
},
190 { "use", KEYWORD_use
},
191 { "var", KEYWORD_var
},
192 { "while", KEYWORD_while
},
193 { "xor", KEYWORD_xor
},
194 { "yield", KEYWORD_yield
}
198 typedef enum eTokenType
{
226 unsigned long lineNumber
;
228 int parentKind
; /* -1 if none */
231 static langType Lang_php
;
232 static langType Lang_zephir
;
234 static boolean InPhp
= FALSE
; /* whether we are between <? ?> */
236 /* current statement details */
242 /* Current namespace */
243 static vString
*CurrentNamespace
;
246 static void buildPhpKeywordHash (const langType language
)
248 const size_t count
= sizeof (PhpKeywordTable
) / sizeof (PhpKeywordTable
[0]);
250 for (i
= 0; i
< count
; i
++)
252 const keywordDesc
* const p
= &PhpKeywordTable
[i
];
253 addKeyword (p
->name
, language
, (int) p
->id
);
257 static const char *accessToString (const accessType access
)
259 static const char *const names
[COUNT_ACCESS
] = {
266 Assert (access
< COUNT_ACCESS
);
268 return names
[access
];
271 static const char *implToString (const implType impl
)
273 static const char *const names
[COUNT_IMPL
] = {
278 Assert (impl
< COUNT_IMPL
);
283 static void initPhpEntry (tagEntryInfo
*const e
, const tokenInfo
*const token
,
284 const phpKind kind
, const accessType access
)
286 static vString
*fullScope
= NULL
;
289 if (fullScope
== NULL
)
290 fullScope
= vStringNew ();
292 vStringClear (fullScope
);
294 if (vStringLength (CurrentNamespace
) > 0)
296 vStringCopy (fullScope
, CurrentNamespace
);
297 parentKind
= K_NAMESPACE
;
300 initTagEntry (e
, vStringValue (token
->string
));
302 e
->lineNumber
= token
->lineNumber
;
303 e
->filePosition
= token
->filePosition
;
304 e
->kindName
= PhpKinds
[kind
].name
;
305 e
->kind
= (char) PhpKinds
[kind
].letter
;
307 if (access
!= ACCESS_UNDEFINED
)
308 e
->extensionFields
.access
= accessToString (access
);
309 if (vStringLength (token
->scope
) > 0)
311 parentKind
= token
->parentKind
;
312 if (vStringLength (fullScope
) > 0)
313 vStringCatS (fullScope
, SCOPE_SEPARATOR
);
314 vStringCat (fullScope
, token
->scope
);
316 if (vStringLength (fullScope
) > 0)
318 Assert (parentKind
>= 0);
320 vStringTerminate (fullScope
);
321 e
->extensionFields
.scope
[0] = PhpKinds
[parentKind
].name
;
322 e
->extensionFields
.scope
[1] = vStringValue (fullScope
);
326 static void makeSimplePhpTag (const tokenInfo
*const token
, const phpKind kind
,
327 const accessType access
)
329 if (PhpKinds
[kind
].enabled
)
333 initPhpEntry (&e
, token
, kind
, access
);
338 static void makeNamespacePhpTag (const tokenInfo
*const token
, const vString
*const name
)
340 if (PhpKinds
[K_NAMESPACE
].enabled
)
344 initTagEntry (&e
, vStringValue (name
));
346 e
.lineNumber
= token
->lineNumber
;
347 e
.filePosition
= token
->filePosition
;
348 e
.kindName
= PhpKinds
[K_NAMESPACE
].name
;
349 e
.kind
= (char) PhpKinds
[K_NAMESPACE
].letter
;
355 static void makeClassOrIfaceTag (const phpKind kind
, const tokenInfo
*const token
,
356 vString
*const inheritance
, const implType impl
)
358 if (PhpKinds
[kind
].enabled
)
362 initPhpEntry (&e
, token
, kind
, ACCESS_UNDEFINED
);
364 if (impl
!= IMPL_UNDEFINED
)
365 e
.extensionFields
.implementation
= implToString (impl
);
366 if (vStringLength (inheritance
) > 0)
367 e
.extensionFields
.inheritance
= vStringValue (inheritance
);
373 static void makeFunctionTag (const tokenInfo
*const token
,
374 const vString
*const arglist
,
375 const accessType access
, const implType impl
)
377 if (PhpKinds
[K_FUNCTION
].enabled
)
381 initPhpEntry (&e
, token
, K_FUNCTION
, access
);
383 if (impl
!= IMPL_UNDEFINED
)
384 e
.extensionFields
.implementation
= implToString (impl
);
386 e
.extensionFields
.signature
= vStringValue (arglist
);
392 static tokenInfo
*newToken (void)
394 tokenInfo
*const token
= xMalloc (1, tokenInfo
);
396 token
->type
= TOKEN_UNDEFINED
;
397 token
->keyword
= KEYWORD_NONE
;
398 token
->string
= vStringNew ();
399 token
->scope
= vStringNew ();
400 token
->lineNumber
= getSourceLineNumber ();
401 token
->filePosition
= getInputFilePosition ();
402 token
->parentKind
= -1;
407 static void deleteToken (tokenInfo
*const token
)
409 vStringDelete (token
->string
);
410 vStringDelete (token
->scope
);
414 static void copyToken (tokenInfo
*const dest
, const tokenInfo
*const src
,
417 dest
->lineNumber
= src
->lineNumber
;
418 dest
->filePosition
= src
->filePosition
;
419 dest
->type
= src
->type
;
420 dest
->keyword
= src
->keyword
;
421 vStringCopy(dest
->string
, src
->string
);
422 dest
->parentKind
= src
->parentKind
;
424 vStringCopy(dest
->scope
, src
->scope
);
430 static const char *tokenTypeName (const tokenType type
)
434 case TOKEN_UNDEFINED
: return "undefined";
435 case TOKEN_EOF
: return "EOF";
436 case TOKEN_CHARACTER
: return "character";
437 case TOKEN_CLOSE_PAREN
: return "')'";
438 case TOKEN_SEMICOLON
: return "';'";
439 case TOKEN_COLON
: return "':'";
440 case TOKEN_COMMA
: return "','";
441 case TOKEN_OPEN_PAREN
: return "'('";
442 case TOKEN_OPERATOR
: return "operator";
443 case TOKEN_IDENTIFIER
: return "identifier";
444 case TOKEN_KEYWORD
: return "keyword";
445 case TOKEN_STRING
: return "string";
446 case TOKEN_PERIOD
: return "'.'";
447 case TOKEN_OPEN_CURLY
: return "'{'";
448 case TOKEN_CLOSE_CURLY
: return "'}'";
449 case TOKEN_EQUAL_SIGN
: return "'='";
450 case TOKEN_OPEN_SQUARE
: return "'['";
451 case TOKEN_CLOSE_SQUARE
: return "']'";
452 case TOKEN_VARIABLE
: return "variable";
457 static void printToken (const tokenInfo
*const token
)
459 fprintf (stderr
, "%p:\n\ttype:\t%s\n\tline:\t%lu\n\tscope:\t%s\n", (void *) token
,
460 tokenTypeName (token
->type
),
462 vStringValue (token
->scope
));
465 case TOKEN_IDENTIFIER
:
468 fprintf (stderr
, "\tcontent:\t%s\n", vStringValue (token
->string
));
473 size_t n
= sizeof PhpKeywordTable
/ sizeof PhpKeywordTable
[0];
476 fprintf (stderr
, "\tkeyword:\t");
477 for (i
= 0; i
< n
; i
++)
479 if (PhpKeywordTable
[i
].id
== token
->keyword
)
481 fprintf (stderr
, "%s\n", PhpKeywordTable
[i
].name
);
486 fprintf (stderr
, "(unknown)\n");
494 static void addToScope (tokenInfo
*const token
, const vString
*const extra
)
496 if (vStringLength (token
->scope
) > 0)
497 vStringCatS (token
->scope
, SCOPE_SEPARATOR
);
498 vStringCatS (token
->scope
, vStringValue (extra
));
499 vStringTerminate(token
->scope
);
502 static boolean
isIdentChar (const int c
)
504 return (isalnum (c
) || c
== '_' || c
>= 0x80);
507 static int skipToCharacter (const int c
)
513 } while (d
!= EOF
&& d
!= c
);
517 static void parseString (vString
*const string
, const int delimiter
)
523 if (c
== '\\' && (c
= fileGetc ()) != EOF
)
524 vStringPut (string
, (char) c
);
525 else if (c
== EOF
|| c
== delimiter
)
528 vStringPut (string
, (char) c
);
530 vStringTerminate (string
);
533 /* reads an HereDoc or a NowDoc (the part after the <<<).
534 * <<<[ \t]*(ID|'ID'|"ID")
539 * 1) starting ID must be immediately followed by a newline;
540 * 2) closing ID is the same as opening one;
541 * 3) closing ID must be immediately followed by a newline or a semicolon
544 * Example of a *single* valid heredoc:
548 * FOO this is not an end
549 * FOO; this isn't either
550 * FOO; # neither this is
552 * # previous line was the end, but the semicolon wasn't required
554 static void parseHeredoc (vString
*const string
)
558 char delimiter
[64]; /* arbitrary limit, but more is crazy anyway */
565 while (c
== ' ' || c
== '\t');
567 if (c
== '\'' || c
== '"')
572 for (len
= 0; len
< (sizeof delimiter
/ sizeof delimiter
[0]) - 1; len
++)
574 if (! isIdentChar (c
))
576 delimiter
[len
] = (char) c
;
581 if (len
== 0) /* no delimiter, give up */
585 if (c
!= quote
) /* no closing quote for quoted identifier, give up */
589 if (c
!= '\r' && c
!= '\n') /* missing newline, give up */
596 if (c
!= '\r' && c
!= '\n')
597 vStringPut (string
, (char) c
);
600 /* new line, check for a delimiter right after */
605 for (len
= 0; c
!= 0 && (c
- delimiter
[len
]) == 0; len
++)
608 if (delimiter
[len
] != 0)
612 /* line start matched the delimiter, now check whether there
613 * is anything after it */
614 if (c
== '\r' || c
== '\n')
622 if (d
== '\r' || d
== '\n')
624 /* put back the semicolon since it's not part of the
625 * string. we can't put back the newline, but it's a
626 * whitespace character nobody cares about it anyway */
632 /* put semicolon in the string and continue */
638 /* if we are here it wasn't a delimiter, so put everything in the
640 vStringPut (string
, (char) nl
);
641 vStringNCatS (string
, delimiter
, len
);
643 vStringPut (string
, (char) extra
);
648 vStringTerminate (string
);
656 static void parseIdentifier (vString
*const string
, const int firstChar
)
661 vStringPut (string
, (char) c
);
663 } while (isIdentChar (c
));
665 vStringTerminate (string
);
668 static keywordId
analyzeToken (vString
*const name
, langType language
)
670 vString
*keyword
= vStringNew ();
672 vStringCopyToLower (keyword
, name
);
673 result
= lookupKeyword (vStringValue (keyword
), language
);
674 vStringDelete (keyword
);
678 static boolean
isSpace (int c
)
680 return (c
== '\t' || c
== ' ' || c
== '\v' ||
681 c
== '\n' || c
== '\r' || c
== '\f');
684 static int skipWhitespaces (int c
)
691 /* <script[:white:]+language[:white:]*=[:white:]*(php|'php'|"php")[:white:]*>
693 * This is ugly, but the whole "<script language=php>" tag is and we can't
694 * really do better without adding a lot of code only for this */
695 static boolean
isOpenScriptLanguagePhp (int c
)
699 /* <script[:white:]+language[:white:]*= */
701 tolower ((c
= fileGetc ())) != 's' ||
702 tolower ((c
= fileGetc ())) != 'c' ||
703 tolower ((c
= fileGetc ())) != 'r' ||
704 tolower ((c
= fileGetc ())) != 'i' ||
705 tolower ((c
= fileGetc ())) != 'p' ||
706 tolower ((c
= fileGetc ())) != 't' ||
707 ! isSpace ((c
= fileGetc ())) ||
708 tolower ((c
= skipWhitespaces (c
))) != 'l' ||
709 tolower ((c
= fileGetc ())) != 'a' ||
710 tolower ((c
= fileGetc ())) != 'n' ||
711 tolower ((c
= fileGetc ())) != 'g' ||
712 tolower ((c
= fileGetc ())) != 'u' ||
713 tolower ((c
= fileGetc ())) != 'a' ||
714 tolower ((c
= fileGetc ())) != 'g' ||
715 tolower ((c
= fileGetc ())) != 'e' ||
716 (c
= skipWhitespaces (fileGetc ())) != '=')
719 /* (php|'php'|"php")> */
720 c
= skipWhitespaces (fileGetc ());
721 if (c
== '"' || c
== '\'')
726 if (tolower (c
) != 'p' ||
727 tolower ((c
= fileGetc ())) != 'h' ||
728 tolower ((c
= fileGetc ())) != 'p' ||
729 (quote
!= 0 && (c
= fileGetc ()) != quote
) ||
730 (c
= skipWhitespaces (fileGetc ())) != '>')
736 static int findPhpStart (void)
741 if ((c
= fileGetc ()) == '<')
744 /* <? and <?php, but not <?xml */
747 /* don't enter PHP mode on "<?xml", yet still support short open tags (<?) */
748 if (tolower ((c
= fileGetc ())) != 'x' ||
749 tolower ((c
= fileGetc ())) != 'm' ||
750 tolower ((c
= fileGetc ())) != 'l')
755 /* <script language="php"> */
759 if (isOpenScriptLanguagePhp ('<'))
769 static int skipSingleComment (void)
777 int next
= fileGetc ();
783 /* ?> in single-line comments leaves PHP mode */
786 int next
= fileGetc ();
792 } while (InPhp
&& c
!= EOF
&& c
!= '\n' && c
!= '\r');
796 static void readToken (tokenInfo
*const token
)
800 token
->type
= TOKEN_UNDEFINED
;
801 token
->keyword
= KEYWORD_NONE
;
802 vStringClear (token
->string
);
815 c
= skipWhitespaces (c
);
817 token
->lineNumber
= getSourceLineNumber ();
818 token
->filePosition
= getInputFilePosition ();
822 case EOF
: token
->type
= TOKEN_EOF
; break;
823 case '(': token
->type
= TOKEN_OPEN_PAREN
; break;
824 case ')': token
->type
= TOKEN_CLOSE_PAREN
; break;
825 case ';': token
->type
= TOKEN_SEMICOLON
; break;
826 case ',': token
->type
= TOKEN_COMMA
; break;
827 case '.': token
->type
= TOKEN_PERIOD
; break;
828 case ':': token
->type
= TOKEN_COLON
; break;
829 case '{': token
->type
= TOKEN_OPEN_CURLY
; break;
830 case '}': token
->type
= TOKEN_CLOSE_CURLY
; break;
831 case '[': token
->type
= TOKEN_OPEN_SQUARE
; break;
832 case ']': token
->type
= TOKEN_CLOSE_SQUARE
; break;
833 case '&': token
->type
= TOKEN_AMPERSAND
; break;
838 if (d
== '=' || d
== '>')
839 token
->type
= TOKEN_OPERATOR
;
843 token
->type
= TOKEN_EQUAL_SIGN
;
850 token
->type
= TOKEN_STRING
;
851 parseString (token
->string
, c
);
852 token
->lineNumber
= getSourceLineNumber ();
853 token
->filePosition
= getInputFilePosition ();
861 /* </script[:white:]*> */
862 if (tolower ((d
= fileGetc ())) == 's' &&
863 tolower ((d
= fileGetc ())) == 'c' &&
864 tolower ((d
= fileGetc ())) == 'r' &&
865 tolower ((d
= fileGetc ())) == 'i' &&
866 tolower ((d
= fileGetc ())) == 'p' &&
867 tolower ((d
= fileGetc ())) == 't' &&
868 (d
= skipWhitespaces (fileGetc ())) == '>')
876 token
->type
= TOKEN_UNDEFINED
;
879 else if (d
== '<' && (d
= fileGetc ()) == '<')
881 token
->type
= TOKEN_STRING
;
882 parseHeredoc (token
->string
);
887 token
->type
= TOKEN_UNDEFINED
;
892 case '#': /* comment */
893 skipSingleComment ();
905 token
->type
= TOKEN_OPERATOR
;
909 case '/': /* division or comment start */
912 if (d
== '/') /* single-line comment */
914 skipSingleComment ();
921 c
= skipToCharacter ('*');
930 } while (c
!= EOF
&& c
!= '\0');
937 token
->type
= TOKEN_OPERATOR
;
942 case '$': /* variable start */
945 if (! isIdentChar (d
))
948 token
->type
= TOKEN_UNDEFINED
;
952 parseIdentifier (token
->string
, d
);
953 token
->type
= TOKEN_VARIABLE
;
958 case '?': /* maybe the end of the PHP chunk */
969 token
->type
= TOKEN_UNDEFINED
;
975 if (! isIdentChar (c
))
976 token
->type
= TOKEN_UNDEFINED
;
979 parseIdentifier (token
->string
, c
);
980 token
->keyword
= analyzeToken (token
->string
, getSourceLanguage ());
981 if (token
->keyword
== KEYWORD_NONE
)
982 token
->type
= TOKEN_IDENTIFIER
;
984 token
->type
= TOKEN_KEYWORD
;
989 if (token
->type
== TOKEN_SEMICOLON
||
990 token
->type
== TOKEN_OPEN_CURLY
||
991 token
->type
== TOKEN_CLOSE_CURLY
)
993 /* reset current statement details on statement end, and when entering
995 * it is a bit ugly to do this in readToken(), but it makes everything
997 CurrentStatement
.access
= ACCESS_UNDEFINED
;
998 CurrentStatement
.impl
= IMPL_UNDEFINED
;
1002 static void enterScope (tokenInfo
*const parentToken
,
1003 const vString
*const extraScope
,
1004 const int parentKind
);
1006 /* parses a class or an interface:
1008 * class Foo extends Bar {}
1009 * class Foo extends Bar implements iFoo, iBar {}
1011 * interface iBar extends iFoo {} */
1012 static boolean
parseClassOrIface (tokenInfo
*const token
, const phpKind kind
)
1014 boolean readNext
= TRUE
;
1015 implType impl
= CurrentStatement
.impl
;
1017 vString
*inheritance
= NULL
;
1020 if (token
->type
!= TOKEN_IDENTIFIER
)
1024 copyToken (name
, token
, TRUE
);
1026 inheritance
= vStringNew ();
1027 /* skip until the open bracket and assume every identifier (not keyword)
1028 * is an inheritance (like in "class Foo extends Bar implements iA, iB") */
1033 if (token
->type
== TOKEN_IDENTIFIER
)
1035 if (vStringLength (inheritance
) > 0)
1036 vStringPut (inheritance
, ',');
1037 vStringCat (inheritance
, token
->string
);
1040 while (token
->type
!= TOKEN_EOF
&&
1041 token
->type
!= TOKEN_OPEN_CURLY
);
1043 makeClassOrIfaceTag (kind
, name
, inheritance
, impl
);
1045 if (token
->type
== TOKEN_OPEN_CURLY
)
1046 enterScope (token
, name
->string
, K_CLASS
);
1051 vStringDelete (inheritance
);
1058 static boolean
parseTrait (tokenInfo
*const token
)
1060 boolean readNext
= TRUE
;
1064 if (token
->type
!= TOKEN_IDENTIFIER
)
1068 copyToken (name
, token
, TRUE
);
1070 makeSimplePhpTag (name
, K_TRAIT
, ACCESS_UNDEFINED
);
1073 if (token
->type
== TOKEN_OPEN_CURLY
)
1074 enterScope (token
, name
->string
, K_TRAIT
);
1085 * if @name is NULL, parses a normal function
1086 * function myfunc($foo, $bar) {}
1087 * function &myfunc($foo, $bar) {}
1089 * if @name is not NULL, parses an anonymous function with name @name
1090 * $foo = function($foo, $bar) {}
1091 * $foo = function&($foo, $bar) {}
1092 * $foo = function($foo, $bar) use ($x, &$y) {} */
1093 static boolean
parseFunction (tokenInfo
*const token
, const tokenInfo
*name
)
1095 boolean readNext
= TRUE
;
1096 accessType access
= CurrentStatement
.access
;
1097 implType impl
= CurrentStatement
.impl
;
1098 tokenInfo
*nameFree
= NULL
;
1101 /* skip a possible leading ampersand (return by reference) */
1102 if (token
->type
== TOKEN_AMPERSAND
)
1107 if (token
->type
!= TOKEN_IDENTIFIER
)
1110 name
= nameFree
= newToken ();
1111 copyToken (nameFree
, token
, TRUE
);
1115 if (token
->type
== TOKEN_OPEN_PAREN
)
1117 vString
*arglist
= vStringNew ();
1120 vStringPut (arglist
, '(');
1125 switch (token
->type
)
1127 case TOKEN_OPEN_PAREN
: depth
++; break;
1128 case TOKEN_CLOSE_PAREN
: depth
--; break;
1132 switch (token
->type
)
1134 case TOKEN_AMPERSAND
: vStringPut (arglist
, '&'); break;
1135 case TOKEN_CLOSE_CURLY
: vStringPut (arglist
, '}'); break;
1136 case TOKEN_CLOSE_PAREN
: vStringPut (arglist
, ')'); break;
1137 case TOKEN_CLOSE_SQUARE
: vStringPut (arglist
, ']'); break;
1138 case TOKEN_COLON
: vStringPut (arglist
, ':'); break;
1139 case TOKEN_COMMA
: vStringCatS (arglist
, ", "); break;
1140 case TOKEN_EQUAL_SIGN
: vStringCatS (arglist
, " = "); break;
1141 case TOKEN_OPEN_CURLY
: vStringPut (arglist
, '{'); break;
1142 case TOKEN_OPEN_PAREN
: vStringPut (arglist
, '('); break;
1143 case TOKEN_OPEN_SQUARE
: vStringPut (arglist
, '['); break;
1144 case TOKEN_PERIOD
: vStringPut (arglist
, '.'); break;
1145 case TOKEN_SEMICOLON
: vStringPut (arglist
, ';'); break;
1146 case TOKEN_STRING
: vStringCatS (arglist
, "'...'"); break;
1148 case TOKEN_IDENTIFIER
:
1150 case TOKEN_VARIABLE
:
1152 switch (vStringLast (arglist
))
1160 /* no need for a space between those and the identifier */
1164 vStringPut (arglist
, ' ');
1167 if (token
->type
== TOKEN_VARIABLE
)
1168 vStringPut (arglist
, '$');
1169 vStringCat (arglist
, token
->string
);
1176 while (token
->type
!= TOKEN_EOF
&& depth
> 0);
1178 vStringTerminate (arglist
);
1180 makeFunctionTag (name
, arglist
, access
, impl
);
1181 vStringDelete (arglist
);
1183 readToken (token
); /* normally it's an open brace or "use" keyword */
1186 /* if parsing Zephir, skip function return type hint */
1187 if (getSourceLanguage () == Lang_zephir
&& token
->type
== TOKEN_OPERATOR
)
1191 while (token
->type
!= TOKEN_EOF
&&
1192 token
->type
!= TOKEN_OPEN_CURLY
&&
1193 token
->type
!= TOKEN_CLOSE_CURLY
&&
1194 token
->type
!= TOKEN_SEMICOLON
);
1198 if (token
->type
== TOKEN_KEYWORD
&& token
->keyword
== KEYWORD_use
)
1201 if (token
->type
== TOKEN_OPEN_PAREN
)
1208 switch (token
->type
)
1210 case TOKEN_OPEN_PAREN
: depth
++; break;
1211 case TOKEN_CLOSE_PAREN
: depth
--; break;
1215 while (token
->type
!= TOKEN_EOF
&& depth
> 0);
1221 if (token
->type
== TOKEN_OPEN_CURLY
)
1222 enterScope (token
, name
->string
, K_FUNCTION
);
1227 deleteToken (nameFree
);
1232 /* parses declarations of the form
1233 * const NAME = VALUE */
1234 static boolean
parseConstant (tokenInfo
*const token
)
1238 readToken (token
); /* skip const keyword */
1239 if (token
->type
!= TOKEN_IDENTIFIER
)
1243 copyToken (name
, token
, TRUE
);
1246 if (token
->type
== TOKEN_EQUAL_SIGN
)
1247 makeSimplePhpTag (name
, K_DEFINE
, ACCESS_UNDEFINED
);
1251 return token
->type
== TOKEN_EQUAL_SIGN
;
1254 /* parses declarations of the form
1255 * define('NAME', 'VALUE')
1256 * define(NAME, 'VALUE) */
1257 static boolean
parseDefine (tokenInfo
*const token
)
1261 readToken (token
); /* skip "define" identifier */
1262 if (token
->type
!= TOKEN_OPEN_PAREN
)
1266 if (token
->type
== TOKEN_STRING
||
1267 token
->type
== TOKEN_IDENTIFIER
)
1269 makeSimplePhpTag (token
, K_DEFINE
, ACCESS_UNDEFINED
);
1273 /* skip until the close parenthesis.
1274 * no need to handle nested blocks since they would be invalid
1275 * in this context anyway (the VALUE may only be a scalar, like
1279 while (token
->type
!= TOKEN_EOF
&& depth
> 0)
1281 switch (token
->type
)
1283 case TOKEN_OPEN_PAREN
: depth
++; break;
1284 case TOKEN_CLOSE_PAREN
: depth
--; break;
1293 /* parses declarations of the form
1296 static boolean
parseVariable (tokenInfo
*const token
)
1299 boolean readNext
= TRUE
;
1300 accessType access
= CurrentStatement
.access
;
1303 copyToken (name
, token
, TRUE
);
1306 if (token
->type
== TOKEN_EQUAL_SIGN
)
1308 phpKind kind
= K_VARIABLE
;
1310 if (token
->parentKind
== K_FUNCTION
)
1311 kind
= K_LOCAL_VARIABLE
;
1314 if (token
->type
== TOKEN_KEYWORD
&&
1315 token
->keyword
== KEYWORD_function
&&
1316 PhpKinds
[kind
].enabled
)
1318 if (parseFunction (token
, name
))
1320 readNext
= (boolean
) (token
->type
== TOKEN_SEMICOLON
);
1324 makeSimplePhpTag (name
, kind
, access
);
1328 else if (token
->type
== TOKEN_SEMICOLON
)
1330 /* generate tags for variable declarations in classes
1334 * but don't get fooled by stuff like $foo = $bar; */
1335 if (token
->parentKind
== K_CLASS
|| token
->parentKind
== K_INTERFACE
)
1336 makeSimplePhpTag (name
, K_VARIABLE
, access
);
1346 /* parses namespace declarations
1348 * namespace Foo\Bar {}
1350 * namespace Foo\Bar;
1353 static boolean
parseNamespace (tokenInfo
*const token
)
1355 tokenInfo
*nsToken
= newToken ();
1357 vStringClear (CurrentNamespace
);
1358 copyToken (nsToken
, token
, FALSE
);
1363 if (token
->type
== TOKEN_IDENTIFIER
)
1365 if (vStringLength (CurrentNamespace
) > 0)
1366 vStringPut (CurrentNamespace
, '\\');
1367 vStringCat (CurrentNamespace
, token
->string
);
1370 while (token
->type
!= TOKEN_EOF
&&
1371 token
->type
!= TOKEN_SEMICOLON
&&
1372 token
->type
!= TOKEN_OPEN_CURLY
);
1374 vStringTerminate (CurrentNamespace
);
1375 if (vStringLength (CurrentNamespace
) > 0)
1376 makeNamespacePhpTag (nsToken
, CurrentNamespace
);
1378 if (token
->type
== TOKEN_OPEN_CURLY
)
1379 enterScope (token
, NULL
, -1);
1381 deleteToken (nsToken
);
1386 static void enterScope (tokenInfo
*const parentToken
,
1387 const vString
*const extraScope
,
1388 const int parentKind
)
1390 tokenInfo
*token
= newToken ();
1391 int origParentKind
= parentToken
->parentKind
;
1393 copyToken (token
, parentToken
, TRUE
);
1397 addToScope (token
, extraScope
);
1398 token
->parentKind
= parentKind
;
1402 while (token
->type
!= TOKEN_EOF
&&
1403 token
->type
!= TOKEN_CLOSE_CURLY
)
1405 boolean readNext
= TRUE
;
1407 switch (token
->type
)
1409 case TOKEN_OPEN_CURLY
:
1410 enterScope (token
, NULL
, -1);
1414 switch (token
->keyword
)
1416 case KEYWORD_class
: readNext
= parseClassOrIface (token
, K_CLASS
); break;
1417 case KEYWORD_interface
: readNext
= parseClassOrIface (token
, K_INTERFACE
); break;
1418 case KEYWORD_trait
: readNext
= parseTrait (token
); break;
1419 case KEYWORD_function
: readNext
= parseFunction (token
, NULL
); break;
1420 case KEYWORD_const
: readNext
= parseConstant (token
); break;
1421 case KEYWORD_define
: readNext
= parseDefine (token
); break;
1423 case KEYWORD_namespace
: readNext
= parseNamespace (token
); break;
1425 case KEYWORD_private
: CurrentStatement
.access
= ACCESS_PRIVATE
; break;
1426 case KEYWORD_protected
: CurrentStatement
.access
= ACCESS_PROTECTED
; break;
1427 case KEYWORD_public
: CurrentStatement
.access
= ACCESS_PUBLIC
; break;
1428 case KEYWORD_var
: CurrentStatement
.access
= ACCESS_PUBLIC
; break;
1430 case KEYWORD_abstract
: CurrentStatement
.impl
= IMPL_ABSTRACT
; break;
1436 case TOKEN_VARIABLE
:
1437 readNext
= parseVariable (token
);
1447 copyToken (parentToken
, token
, FALSE
);
1448 parentToken
->parentKind
= origParentKind
;
1449 deleteToken (token
);
1452 static void findTags (void)
1454 tokenInfo
*const token
= newToken ();
1456 CurrentStatement
.access
= ACCESS_UNDEFINED
;
1457 CurrentStatement
.impl
= IMPL_UNDEFINED
;
1458 CurrentNamespace
= vStringNew ();
1462 enterScope (token
, NULL
, -1);
1464 while (token
->type
!= TOKEN_EOF
); /* keep going even with unmatched braces */
1466 vStringDelete (CurrentNamespace
);
1467 deleteToken (token
);
1470 static void findPhpTags (void)
1476 static void findZephirTags (void)
1482 static void initializePhpParser (const langType language
)
1484 Lang_php
= language
;
1485 buildPhpKeywordHash (language
);
1488 static void initializeZephirParser (const langType language
)
1490 Lang_zephir
= language
;
1491 buildPhpKeywordHash (language
);
1494 extern parserDefinition
* PhpParser (void)
1496 static const char *const extensions
[] = { "php", "php3", "php4", "php5", "phtml", NULL
};
1497 parserDefinition
* def
= parserNew ("PHP");
1498 def
->kinds
= PhpKinds
;
1499 def
->kindCount
= KIND_COUNT (PhpKinds
);
1500 def
->extensions
= extensions
;
1501 def
->parser
= findPhpTags
;
1502 def
->initialize
= initializePhpParser
;
1506 extern parserDefinition
* ZephirParser (void)
1508 static const char *const extensions
[] = { "zep", NULL
};
1509 parserDefinition
* def
= parserNew ("Zephir");
1510 def
->kinds
= PhpKinds
;
1511 def
->kindCount
= KIND_COUNT (PhpKinds
);
1512 def
->extensions
= extensions
;
1513 def
->parser
= findZephirTags
;
1514 def
->initialize
= initializeZephirParser
;
1518 /* vi:set tabstop=4 shiftwidth=4: */