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>)>
191 ( (~["\"","\\","\n","\r"])
193 ( ["n","t","b","r","f","\\","'","\""]
194 | ["0"-"7"] ( ["0"-"7"] )?
195 | ["0"-"3"] ["0"-"7"] ["0"-"7"]
203 ( (~["\"","\\","\n","\r"])
205 ( ["n","t","b","r","f","\\","'","\""]
206 | ["0"-"7"] ( ["0"-"7"] )?
207 | ["0"-"3"] ["0"-"7"] ["0"-"7"]
215 ( (~["\"","\\","\n","\r"])
217 ( ["n","t","b","r","f","\\","'","\""]
218 | ["0"-"7"] ( ["0"-"7"] )?
219 | ["0"-"3"] ["0"-"7"] ["0"-"7"]
231 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
234 ["a"-"z"] | ["A"-"Z"]
289 | < RSIGNEDSHIFT: ">>" >
290 | < RUNSIGNEDSHIFT: ">>>" >
291 | < PLUSASSIGN: "+=" >
292 | < MINUSASSIGN: "-=" >
293 | < STARASSIGN: "*=" >
294 | < SLASHASSIGN: "/=" >
295 | < ANDASSIGN: "&=" >
297 | < XORASSIGN: "^=" >
298 | < REMASSIGN: "%=" >
299 | < LSHIFTASSIGN: "<<=" >
300 | < RSIGNEDSHIFTASSIGN: ">>=" >
301 | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
305 /*****************************************
306 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
307 *****************************************/
310 * Program structuring syntax follows.
320 void ClassDeclaration() :
323 "class" <IDENTIFIER> [ "extends" <IDENTIFIER> ]
330 "{" ( ClassBodyDeclaration() )* "}"
333 void ClassBodyDeclaration() :
341 void FieldDeclaration() :
344 "var" VariableDeclarator() ( "," VariableDeclarator() )* ";"
347 void VariableDeclarator() :
350 VariableDeclaratorId() [ "=" VariableInitializer() ]
353 void VariableDeclaratorId() :
356 "$" VariableName() ( LOOKAHEAD(2) VariableSuffix() )*
369 void VariableInitializer() :
375 void ArrayVariable() :
378 Expression() ("=>" Expression())*
381 void ArrayInitializer() :
384 "(" [ ArrayVariable() ( LOOKAHEAD(2) "," ArrayVariable() )* ] [ "," ] ")"
387 void MethodDeclaration() :
390 "function" MethodDeclarator()
394 void MethodDeclarator() :
397 ["&"] <IDENTIFIER> FormalParameters()
400 void FormalParameters() :
403 "(" [ FormalParameter() ( "," FormalParameter() )* ] ")"
406 void FormalParameter() :
409 ["&"] VariableDeclarator()
433 * Expression syntax follows.
438 * This expansion has been written this way instead of:
439 * Assignment() | ConditionalExpression()
440 * for performance reasons.
441 * However, it is a weakening of the grammar for it allows the LHS of
442 * assignments to be any conditional expression whereas it can only be
443 * a primary expression. Consider adding a semantic predicate to work
448 ConditionalExpression()
450 AssignmentOperator() Expression()
454 void AssignmentOperator() :
457 "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|=" | ".="
460 void ConditionalExpression() :
463 ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression() ]
466 void ConditionalOrExpression() :
469 ConditionalAndExpression() ( ("||" | "OR") ConditionalAndExpression() )*
472 void ConditionalAndExpression() :
475 ConcatExpression() ( ("&&" | "AND") ConcatExpression() )*
478 void ConcatExpression() :
481 InclusiveOrExpression() ( "." InclusiveOrExpression() )*
484 void InclusiveOrExpression() :
487 ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )*
490 void ExclusiveOrExpression() :
493 AndExpression() ( "^" AndExpression() )*
496 void AndExpression() :
499 EqualityExpression() ( "&" EqualityExpression() )*
502 void EqualityExpression() :
505 RelationalExpression() ( ( "==" | "!=" ) RelationalExpression() )*
508 void RelationalExpression() :
511 ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )*
514 void ShiftExpression() :
517 AdditiveExpression() ( ( "<<" | ">>" | ">>>" ) AdditiveExpression() )*
520 void AdditiveExpression() :
523 MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )*
526 void MultiplicativeExpression() :
529 UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )*
532 void UnaryExpression() :
535 "@" UnaryExpression()
537 ( "+" | "-" ) UnaryExpression()
539 PreIncrementExpression()
541 PreDecrementExpression()
543 UnaryExpressionNotPlusMinus()
546 void PreIncrementExpression() :
549 "++" PrimaryExpression()
552 void PreDecrementExpression() :
555 "--" PrimaryExpression()
558 void UnaryExpressionNotPlusMinus() :
561 ( "~" | "!" ) UnaryExpression()
563 LOOKAHEAD( "(" Type() ")" )
573 void CastExpression() :
576 "(" Type() ")" UnaryExpression()
579 void PostfixExpression() :
582 PrimaryExpression() [ "++" | "--" ]
585 void PrimaryExpression() :
589 <IDENTIFIER> "::" ClassIdentifier() (PrimarySuffix())*
591 PrimaryPrefix() ( PrimarySuffix() )*
593 "array" ArrayInitializer()
596 void PrimaryPrefix() :
603 "new" ClassIdentifier()
605 VariableDeclaratorId()
608 void ClassIdentifier():
613 VariableDeclaratorId()
618 void PrimarySuffix() :
626 void VariableSuffix() :
639 <FLOATING_POINT_LITERAL>
648 void BooleanLiteral() :
665 "(" [ ArgumentList() ] ")"
668 void ArgumentList() :
671 Expression() ( "," Expression() )*
675 * Statement syntax follows.
691 StatementExpression() ";"
714 void EchoStatement() :
717 "echo" Expression() ("," Expression())* ";"
720 void GlobalStatement() :
723 "global" VariableDeclaratorId() ("," VariableDeclaratorId())* ";"
726 void StaticStatement() :
729 "static" VariableDeclarator() ("," VariableDeclarator())* ";"
732 void LabeledStatement() :
735 <IDENTIFIER> ":" Statement()
741 "{" ( BlockStatement() )* "}"
744 void BlockStatement() :
754 void LocalVariableDeclaration() :
757 VariableDeclarator() ( "," VariableDeclarator() )*
760 void EmptyStatement() :
766 void StatementExpression() :
768 * The last expansion of this production accepts more than the legal
769 * Java expansions for StatementExpression. This expansion does not
770 * use PostfixExpression for performance reasons.
774 PreIncrementExpression()
776 PreDecrementExpression()
784 AssignmentOperator() Expression()
788 void SwitchStatement() :
791 "switch" "(" Expression() ")" "{"
792 ( SwitchLabel() ( BlockStatement() )* )*
799 "case" Expression() ":"
806 * The disambiguating algorithm of JavaCC automatically binds dangling
807 * else's to the innermost if statement. The LOOKAHEAD specification
808 * is to tell JavaCC that we know what we are doing.
812 "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) "else" Statement() ]
815 void ElseIfStatement() :
818 "elseif" "(" Expression() ")" Statement()
821 void WhileStatement() :
824 "while" "(" Expression() ")" Statement()
830 "do" Statement() "while" "(" Expression() ")" ";"
833 void ForStatement() :
836 "for" "(" [ ForInit() ] ";" [ Expression() ] ";" [ ForUpdate() ] ")" Statement()
842 LOOKAHEAD(LocalVariableDeclaration())
843 LocalVariableDeclaration()
845 StatementExpressionList()
848 void StatementExpressionList() :
851 StatementExpression() ( "," StatementExpression() )*
857 StatementExpressionList()
860 void BreakStatement() :
863 "break" [ <IDENTIFIER> ] ";"
866 void ContinueStatement() :
869 "continue" [ <IDENTIFIER> ] ";"
872 void ReturnStatement() :
875 "return" [ Expression() ] ";"