options { LOOKAHEAD = 1; CHOICE_AMBIGUITY_CHECK = 2; OTHER_AMBIGUITY_CHECK = 1; STATIC = true; DEBUG_PARSER = false; DEBUG_LOOKAHEAD = false; DEBUG_TOKEN_MANAGER = false; ERROR_REPORTING = true; JAVA_UNICODE_ESCAPE = false; UNICODE_INPUT = false; IGNORE_CASE = true; USER_TOKEN_MANAGER = false; USER_CHAR_STREAM = false; BUILD_PARSER = true; BUILD_TOKEN_MANAGER = true; SANITY_CHECK = true; FORCE_LA_CHECK = false; } PARSER_BEGIN(PHPParser2) package test; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; /** * A new php parser. * This php parser is inspired by the Java 1.2 grammar example * given with JavaCC. You can get JavaCC at http://www.webgain.com * You can test the parser with the PHPParserTestCase2.java * @author Matthieu Casanova */ public class PHPParser2 { public PHPParser2(IFile fileToParse) throws CoreException { this(fileToParse.getContents()); } public void parse() throws ParseException { phpFile(); } public static void main(String args[]) throws ParseException { PHPParser2 parser = new PHPParser2(System.in); } } PARSER_END(PHPParser2) /* WHITE SPACE */ SKIP : { " " | "\t" | "\n" | "\r" | "\f" } /* COMMENTS */ MORE : { "//" : IN_SINGLE_LINE_COMMENT | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } MORE : { < ~[] > } /* KEYWORDS */ TOKEN : { | | | | | | } /* LANGUAGE CONSTRUCT */ TOKEN : { | | } /* RESERVED WORDS AND LITERALS */ TOKEN : { < BREAK: "break" > | < CASE: "case" > | < CONST: "const" > | < CONTINUE: "continue" > | < _DEFAULT: "default" > | < DO: "do" > | < EXTENDS: "extends" > | < FALSE: "false" > | < FOR: "for" > | < GOTO: "goto" > | < NEW: "new" > | < NULL: "null" > | < RETURN: "return" > | < SUPER: "super" > | < SWITCH: "switch" > | < THIS: "this" > | < TRUE: "true" > | < WHILE: "while" > } /* TYPES */ TOKEN : { | | | | | | | | } TOKEN : { < _ORL: "OR" > | < _ANDL: "AND" > } /* LITERALS */ TOKEN : { < INTEGER_LITERAL: (["l","L"])? | (["l","L"])? | (["l","L"])? > | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | < STRING_LITERAL: ( | | )> | < STRING_1: "\"" ( (~["\""]) | "\\\"" )* "\"" > | < STRING_2: "'" ( (~["'"]))* "'" > | < STRING_3: "`" ( (~["`"]))* "`" > } /* IDENTIFIERS */ TOKEN : { < IDENTIFIER: (|) (||)* > | < #LETTER: ["a"-"z"] | ["A"-"Z"] > | < #DIGIT: ["0"-"9"] > | < #SPECIAL: "_" > } /* SEPARATORS */ TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < LBRACKET: "[" > | < RBRACKET: "]" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > } /* OPERATORS */ TOKEN : { < ASSIGN: "=" > | < GT: ">" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < INCR: "++" > | < DECR: "--" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > | < LSHIFT: "<<" > | < RSIGNEDSHIFT: ">>" > | < RUNSIGNEDSHIFT: ">>>" > | < PLUSASSIGN: "+=" > | < MINUSASSIGN: "-=" > | < STARASSIGN: "*=" > | < SLASHASSIGN: "/=" > | < ANDASSIGN: "&=" > | < ORASSIGN: "|=" > | < XORASSIGN: "^=" > | < REMASSIGN: "%=" > | < LSHIFTASSIGN: "<<=" > | < RSIGNEDSHIFTASSIGN: ">>=" > | < RUNSIGNEDSHIFTASSIGN: ">>>=" > } /***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ /* * Program structuring syntax follows. */ void phpFile() : {} { (BlockStatement())* } void ClassDeclaration() : {} { "class" [ "extends" ] ClassBody() } void ClassBody() : {} { "{" ( ClassBodyDeclaration() )* "}" } void ClassBodyDeclaration() : {} { MethodDeclaration() | FieldDeclaration() } void FieldDeclaration() : {} { "var" VariableDeclarator() ( "," VariableDeclarator() )* ";" } void VariableDeclarator() : {} { VariableDeclaratorId() [ "=" VariableInitializer() ] } void VariableDeclaratorId() : {} { "$" VariableName() ( LOOKAHEAD(2) VariableSuffix() )* } void VariableName(): {} { "this" | "{" Expression() "}" | ("{" Expression() "}") * | "$" VariableName() } void VariableInitializer() : {} { Expression() } void ArrayVariable() : {} { Expression() ("=>" Expression())* } void ArrayInitializer() : {} { "(" [ ArrayVariable() ( LOOKAHEAD(2) "," ArrayVariable() )* ] [ "," ] ")" } void MethodDeclaration() : {} { "function" MethodDeclarator() ( Block() | ";" ) } void MethodDeclarator() : {} { ["&"] FormalParameters() } void FormalParameters() : {} { "(" [ FormalParameter() ( "," FormalParameter() )* ] ")" } void FormalParameter() : {} { ["&"] VariableDeclarator() } void Type() : {} { "string" | "bool" | "boolean" | "real" | "double" | "float" | "int" | "integer" } /* * Expression syntax follows. */ void Expression() : /* * This expansion has been written this way instead of: * Assignment() | ConditionalExpression() * for performance reasons. * However, it is a weakening of the grammar for it allows the LHS of * assignments to be any conditional expression whereas it can only be * a primary expression. Consider adding a semantic predicate to work * around this. */ {} { ConditionalExpression() [ AssignmentOperator() Expression() ] } void AssignmentOperator() : {} { "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "^=" | "|=" | ".=" } void ConditionalExpression() : {} { ConditionalOrExpression() [ "?" Expression() ":" ConditionalExpression() ] } void ConditionalOrExpression() : {} { ConditionalAndExpression() ( ("||" | "OR") ConditionalAndExpression() )* } void ConditionalAndExpression() : {} { ConcatExpression() ( ("&&" | "AND") ConcatExpression() )* } void ConcatExpression() : {} { InclusiveOrExpression() ( "." InclusiveOrExpression() )* } void InclusiveOrExpression() : {} { ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )* } void ExclusiveOrExpression() : {} { AndExpression() ( "^" AndExpression() )* } void AndExpression() : {} { EqualityExpression() ( "&" EqualityExpression() )* } void EqualityExpression() : {} { RelationalExpression() ( ( "==" | "!=" ) RelationalExpression() )* } void RelationalExpression() : {} { ShiftExpression() ( ( "<" | ">" | "<=" | ">=" ) ShiftExpression() )* } void ShiftExpression() : {} { AdditiveExpression() ( ( "<<" | ">>" | ">>>" ) AdditiveExpression() )* } void AdditiveExpression() : {} { MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )* } void MultiplicativeExpression() : {} { UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )* } void UnaryExpression() : {} { "@" UnaryExpression() | ( "+" | "-" ) UnaryExpression() | PreIncrementExpression() | PreDecrementExpression() | UnaryExpressionNotPlusMinus() } void PreIncrementExpression() : {} { "++" PrimaryExpression() } void PreDecrementExpression() : {} { "--" PrimaryExpression() } void UnaryExpressionNotPlusMinus() : {} { ( "~" | "!" ) UnaryExpression() | LOOKAHEAD( "(" Type() ")" ) CastExpression() | PostfixExpression() | Literal() | "("Expression()")" } void CastExpression() : {} { "(" Type() ")" UnaryExpression() } void PostfixExpression() : {} { PrimaryExpression() [ "++" | "--" ] } void PrimaryExpression() : {} { LOOKAHEAD(2) "::" ClassIdentifier() (PrimarySuffix())* | PrimaryPrefix() ( PrimarySuffix() )* | "array" ArrayInitializer() } void PrimaryPrefix() : {} { "$this" | | "new" ClassIdentifier() | VariableDeclaratorId() } void ClassIdentifier(): {} { | VariableDeclaratorId() | "$this" } void PrimarySuffix() : {} { Arguments() | VariableSuffix() } void VariableSuffix() : {} { "->" VariableName() | "[" Expression() "]" } void Literal() : {} { | | | BooleanLiteral() | NullLiteral() } void BooleanLiteral() : {} { "true" | "false" } void NullLiteral() : {} { "null" } void Arguments() : {} { "(" [ ArgumentList() ] ")" } void ArgumentList() : {} { Expression() ( "," Expression() )* } /* * Statement syntax follows. */ void Statement() : {} { LOOKAHEAD(2) LabeledStatement() | LOOKAHEAD(2) Expression() ";" | Block() | EmptyStatement() | StatementExpression() ";" | SwitchStatement() | IfStatement() | WhileStatement() | DoStatement() | ForStatement() | BreakStatement() | ContinueStatement() | ReturnStatement() | EchoStatement() | StaticStatement() | GlobalStatement() } void EchoStatement() : {} { "echo" Expression() ("," Expression())* ";" } void GlobalStatement() : {} { "global" VariableDeclaratorId() ("," VariableDeclaratorId())* ";" } void StaticStatement() : {} { "static" VariableDeclarator() ("," VariableDeclarator())* ";" } void LabeledStatement() : {} { ":" Statement() } void Block() : {} { "{" ( BlockStatement() )* "}" } void BlockStatement() : {} { Statement() | ClassDeclaration() | MethodDeclaration() } void LocalVariableDeclaration() : {} { VariableDeclarator() ( "," VariableDeclarator() )* } void EmptyStatement() : {} { ";" } void StatementExpression() : /* * The last expansion of this production accepts more than the legal * Java expansions for StatementExpression. This expansion does not * use PostfixExpression for performance reasons. */ {} { PreIncrementExpression() | PreDecrementExpression() | PrimaryExpression() [ "++" | "--" | AssignmentOperator() Expression() ] } void SwitchStatement() : {} { "switch" "(" Expression() ")" "{" ( SwitchLabel() ( BlockStatement() )* )* "}" } void SwitchLabel() : {} { "case" Expression() ":" | "default" ":" } void IfStatement() : /* * The disambiguating algorithm of JavaCC automatically binds dangling * else's to the innermost if statement. The LOOKAHEAD specification * is to tell JavaCC that we know what we are doing. */ {} { "if" "(" Expression() ")" Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) "else" Statement() ] } void ElseIfStatement() : {} { "elseif" "(" Expression() ")" Statement() } void WhileStatement() : {} { "while" "(" Expression() ")" Statement() } void WhileStatement0() : {} { ":" Statement() "endwhile;" | Statement() } void DoStatement() : {} { "do" Statement() "while" "(" Expression() ")" ";" } void ForStatement() : {} { "for" "(" [ ForInit() ] ";" [ Expression() ] ";" [ ForUpdate() ] ")" Statement() } void ForInit() : {} { LOOKAHEAD(LocalVariableDeclaration()) LocalVariableDeclaration() | StatementExpressionList() } void StatementExpressionList() : {} { StatementExpression() ( "," StatementExpression() )* } void ForUpdate() : {} { StatementExpressionList() } void BreakStatement() : {} { "break" [ ] ";" } void ContinueStatement() : {} { "continue" [ ] ";" } void ReturnStatement() : {} { "return" [ Expression() ] ";" }