3 CHOICE_AMBIGUITY_CHECK = 2;
4 OTHER_AMBIGUITY_CHECK = 1;
7 DEBUG_LOOKAHEAD = false;
8 DEBUG_TOKEN_MANAGER = false;
9 ERROR_REPORTING = true;
10 JAVA_UNICODE_ESCAPE = false;
11 UNICODE_INPUT = false;
13 USER_TOKEN_MANAGER = false;
14 USER_CHAR_STREAM = false;
16 BUILD_TOKEN_MANAGER = true;
18 FORCE_LA_CHECK = false;
21 PARSER_BEGIN(PHPParser2)
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.runtime.CoreException;
29 * This php parser is inspired by the Java 1.2 grammar example
30 * given with JavaCC. You can get JavaCC at http://www.webgain.com
31 * You can test the parser with the PHPParserTestCase2.java
32 * @author Matthieu Casanova
34 public class PHPParser2 {
36 public PHPParser2(IFile fileToParse) throws CoreException {
37 this(fileToParse.getContents());
40 public void parse() throws ParseException {
43 public static void main(String args[]) throws ParseException {
44 PHPParser2 parser = new PHPParser2(System.in);
49 PARSER_END(PHPParser2)
67 "//" : IN_SINGLE_LINE_COMMENT
69 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
71 "/*" : IN_MULTI_LINE_COMMENT
74 <IN_SINGLE_LINE_COMMENT>
77 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT
83 <FORMAL_COMMENT: "*/" > : DEFAULT
86 <IN_MULTI_LINE_COMMENT>
89 <MULTI_LINE_COMMENT: "*/" > : DEFAULT
92 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
102 | <FUNCTION : "function">
105 | <ELSEIF : "elseif">
110 /* LANGUAGE CONSTRUCT */
114 | <GLOBAL : "global">
115 | <STATIC : "static">
118 /* RESERVED WORDS AND LITERALS */
125 | < CONTINUE: "continue" >
126 | < _DEFAULT: "default" >
128 | < EXTENDS: "extends" >
134 | < RETURN: "return" >
136 | < SWITCH: "switch" >
147 | <OBJECT : "object">
149 | <BOOLEAN : "boolean">
151 | <DOUBLE : "double">
154 | <INTEGER : "integer">
168 <DECIMAL_LITERAL> (["l","L"])?
169 | <HEX_LITERAL> (["l","L"])?
170 | <OCTAL_LITERAL> (["l","L"])?
173 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
175 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
177 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
179 < FLOATING_POINT_LITERAL:
180 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
181 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
182 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
183 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
186 < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
188 < STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
213 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
216 ["a"-"z"] | ["A"-"Z"]
271 | < RSIGNEDSHIFT: ">>" >
272 | < RUNSIGNEDSHIFT: ">>>" >
273 | < PLUSASSIGN: "+=" >
274 | < MINUSASSIGN: "-=" >
275 | < STARASSIGN: "*=" >
276 | < SLASHASSIGN: "/=" >
277 | < ANDASSIGN: "&=" >
279 | < XORASSIGN: "^=" >
280 | < REMASSIGN: "%=" >
281 | < LSHIFTASSIGN: "<<=" >
282 | < RSIGNEDSHIFTASSIGN: ">>=" >
283 | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
287 /*****************************************
288 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
289 *****************************************/
292 * Program structuring syntax follows.
302 void ClassDeclaration() :
305 "class" <IDENTIFIER> [ "extends" <IDENTIFIER> ]
312 "{" ( ClassBodyDeclaration() )* "}"
315 void ClassBodyDeclaration() :
323 void FieldDeclaration() :
326 "var" VariableDeclarator() ( "," VariableDeclarator() )* ";"
329 void VariableDeclarator() :
332 VariableDeclaratorId() [ "=" VariableInitializer() ]
335 void VariableDeclaratorId() :
338 "$" VariableName() ( LOOKAHEAD(2) VariableSuffix() )*
348 <IDENTIFIER> ("{" Expression() "}") *
353 void VariableInitializer() :
359 void ArrayVariable() :
362 Expression() ("=>" Expression())*
365 void ArrayInitializer() :
368 "(" [ ArrayVariable() ( LOOKAHEAD(2) "," ArrayVariable() )* ] [ "," ] ")"
371 void MethodDeclaration() :
374 "function" MethodDeclarator()
378 void MethodDeclarator() :
381 ["&"] <IDENTIFIER> FormalParameters()
384 void FormalParameters() :
387 "(" [ FormalParameter() ( "," FormalParameter() )* ] ")"
390 void FormalParameter() :
393 ["&"] VariableDeclarator()
417 * Expression syntax follows.
422 * This expansion has been written this way instead of:
423 * Assignment() | ConditionalExpression()
424 * for performance reasons.
425 * However, it is a weakening of the grammar for it allows the LHS of
426 * assignments to be any conditional expression whereas it can only be
427 * a primary expression. Consider adding a semantic predicate to work
432 ConditionalExpression()
434 AssignmentOperator() Expression()
438 void AssignmentOperator() :
441 "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|=" | ".="
444 void ConditionalExpression() :
447 ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression() ]
450 void ConditionalOrExpression() :
453 ConditionalAndExpression() ( ("||" | "OR") ConditionalAndExpression() )*
456 void ConditionalAndExpression() :
459 ConcatExpression() ( ("&&" | "AND") ConcatExpression() )*
462 void ConcatExpression() :
465 InclusiveOrExpression() ( "." InclusiveOrExpression() )*
468 void InclusiveOrExpression() :
471 ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )*
474 void ExclusiveOrExpression() :
477 AndExpression() ( "^" AndExpression() )*
480 void AndExpression() :
483 EqualityExpression() ( "&" EqualityExpression() )*
486 void EqualityExpression() :
489 RelationalExpression() ( ( "==" | "!=" ) RelationalExpression() )*
492 void RelationalExpression() :
495 ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )*
498 void ShiftExpression() :
501 AdditiveExpression() ( ( "<<" | ">>" | ">>>" ) AdditiveExpression() )*
504 void AdditiveExpression() :
507 MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )*
510 void MultiplicativeExpression() :
513 UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )*
516 void UnaryExpression() :
519 "@" UnaryExpression()
521 ( "+" | "-" ) UnaryExpression()
523 PreIncrementExpression()
525 PreDecrementExpression()
527 UnaryExpressionNotPlusMinus()
530 void PreIncrementExpression() :
533 "++" PrimaryExpression()
536 void PreDecrementExpression() :
539 "--" PrimaryExpression()
542 void UnaryExpressionNotPlusMinus() :
545 ( "~" | "!" ) UnaryExpression()
547 LOOKAHEAD( "(" Type() ")" )
557 void CastExpression() :
560 "(" Type() ")" UnaryExpression()
563 void PostfixExpression() :
566 PrimaryExpression() [ "++" | "--" ]
569 void PrimaryExpression() :
573 <IDENTIFIER> "::" ClassIdentifier() (PrimarySuffix())*
575 PrimaryPrefix() ( PrimarySuffix() )*
577 "array" ArrayInitializer()
580 void PrimaryPrefix() :
587 "new" ClassIdentifier()
589 VariableDeclaratorId()
592 void ClassIdentifier():
597 VariableDeclaratorId()
602 void PrimarySuffix() :
610 void VariableSuffix() :
623 <FLOATING_POINT_LITERAL>
632 void BooleanLiteral() :
649 "(" [ ArgumentList() ] ")"
652 void ArgumentList() :
655 Expression() ( "," Expression() )*
659 * Statement syntax follows.
675 StatementExpression() ";"
700 void EchoStatement() :
703 "echo" Expression() ("," Expression())* ";"
706 void GlobalStatement() :
709 "global" VariableDeclaratorId() ("," VariableDeclaratorId())* ";"
712 void StaticStatement() :
715 "static" VariableDeclarator() ("," VariableDeclarator())* ";"
718 void LabeledStatement() :
721 <IDENTIFIER> ":" Statement()
727 "{" ( BlockStatement() )* "}"
730 void BlockStatement() :
740 void LocalVariableDeclaration() :
743 VariableDeclarator() ( "," VariableDeclarator() )*
746 void EmptyStatement() :
752 void StatementExpression() :
754 * The last expansion of this production accepts more than the legal
755 * Java expansions for StatementExpression. This expansion does not
756 * use PostfixExpression for performance reasons.
760 PreIncrementExpression()
762 PreDecrementExpression()
770 AssignmentOperator() Expression()
774 void SwitchStatement() :
777 "switch" "(" Expression() ")" "{"
778 ( SwitchLabel() ( BlockStatement() )* )*
785 "case" Expression() ":"
792 * The disambiguating algorithm of JavaCC automatically binds dangling
793 * else's to the innermost if statement. The LOOKAHEAD specification
794 * is to tell JavaCC that we know what we are doing.
798 "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) "else" Statement() ]
801 void ElseIfStatement() :
804 "elseif" "(" Expression() ")" Statement()
807 void WhileStatement() :
810 "while" "(" Expression() ")" Statement()
813 void WhileStatement0() :
816 ":" Statement() "endwhile;"
824 "do" Statement() "while" "(" Expression() ")" ";"
827 void ForStatement() :
830 "for" "(" [ ForInit() ] ";" [ Expression() ] ";" [ ForUpdate() ] ")" Statement()
836 LOOKAHEAD(LocalVariableDeclaration())
837 LocalVariableDeclaration()
839 StatementExpressionList()
842 void StatementExpressionList() :
845 StatementExpression() ( "," StatementExpression() )*
851 StatementExpressionList()
854 void BreakStatement() :
857 "break" [ <IDENTIFIER> ] ";"
860 void ContinueStatement() :
863 "continue" [ <IDENTIFIER> ] ";"
866 void ReturnStatement() :
869 "return" [ Expression() ] ";"