3 CHOICE_AMBIGUITY_CHECK = 2;
4 OTHER_AMBIGUITY_CHECK = 1;
7 DEBUG_LOOKAHEAD = false;
8 DEBUG_TOKEN_MANAGER = false;
9 OPTIMIZE_TOKEN_MANAGER = false;
10 ERROR_REPORTING = true;
11 JAVA_UNICODE_ESCAPE = false;
12 UNICODE_INPUT = false;
14 USER_TOKEN_MANAGER = false;
15 USER_CHAR_STREAM = false;
17 BUILD_TOKEN_MANAGER = true;
19 FORCE_LA_CHECK = false;
22 PARSER_BEGIN(PHPParser)
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IMarker;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.ui.texteditor.MarkerUtilities;
29 import org.eclipse.jface.preference.IPreferenceStore;
31 import java.util.Hashtable;
32 import java.io.StringReader;
33 import java.text.MessageFormat;
35 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
36 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
37 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
38 import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren;
39 import net.sourceforge.phpdt.internal.compiler.parser.PHPFunctionDeclaration;
40 import net.sourceforge.phpdt.internal.compiler.parser.PHPClassDeclaration;
44 * This php parser is inspired by the Java 1.2 grammar example
45 * given with JavaCC. You can get JavaCC at http://www.webgain.com
46 * You can test the parser with the PHPParserTestCase2.java
47 * @author Matthieu Casanova
49 public class PHPParser extends PHPParserSuperclass {
51 private static IFile fileToParse;
53 /** The current segment */
54 private static PHPSegmentWithChildren currentSegment;
56 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
57 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
58 public static final int ERROR = 2;
59 public static final int WARNING = 1;
60 public static final int INFO = 0;
61 PHPOutlineInfo outlineInfo;
62 private static int errorLevel = ERROR;
63 private static String errorMessage;
68 public void setFileToParse(IFile fileToParse) {
69 this.fileToParse = fileToParse;
72 public PHPParser(IFile fileToParse) {
73 this(new StringReader(""));
74 this.fileToParse = fileToParse;
77 public void phpParserTester(String strEval) throws CoreException, ParseException {
78 PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
79 StringReader stream = new StringReader(strEval);
80 if (jj_input_stream == null) {
81 jj_input_stream = new SimpleCharStream(stream, 1, 1);
83 ReInit(new StringReader(strEval));
87 public void htmlParserTester(String strEval) throws CoreException, ParseException {
88 StringReader stream = new StringReader(strEval);
89 if (jj_input_stream == null) {
90 jj_input_stream = new SimpleCharStream(stream, 1, 1);
96 public PHPOutlineInfo parseInfo(Object parent, String s) {
97 outlineInfo = new PHPOutlineInfo(parent);
98 currentSegment = outlineInfo.getDeclarations();
99 StringReader stream = new StringReader(s);
100 if (jj_input_stream == null) {
101 jj_input_stream = new SimpleCharStream(stream, 1, 1);
106 } catch (ParseException e) {
107 if (errorMessage == null) {
108 PHPeclipsePlugin.log(e);
110 setMarker(errorMessage, e.currentToken.beginLine, errorLevel);
119 * Create marker for the parse error
121 private static void setMarker(String message, int lineNumber, int errorLevel) {
123 setMarker(fileToParse, message, lineNumber, errorLevel);
124 } catch (CoreException e) {
125 PHPeclipsePlugin.log(e);
129 public static void setMarker(IFile file, String message, int lineNumber, int errorLevel) throws CoreException {
131 Hashtable attributes = new Hashtable();
132 MarkerUtilities.setMessage(attributes, message);
133 switch (errorLevel) {
135 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
138 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
141 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
144 MarkerUtilities.setLineNumber(attributes, lineNumber);
145 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
150 * Create markers according to the external parser output
152 private static void createMarkers(String output, IFile file) throws CoreException {
153 // delete all markers
154 file.deleteMarkers(IMarker.PROBLEM, false, 0);
159 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
160 // newer php error output (tested with 4.2.3)
161 scanLine(output, file, indx, brIndx);
166 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
167 // older php error output (tested with 4.2.3)
168 scanLine(output, file, indx, brIndx);
174 private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
176 StringBuffer lineNumberBuffer = new StringBuffer(10);
178 current = output.substring(indx, brIndx);
180 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
181 int onLine = current.indexOf("on line <b>");
183 lineNumberBuffer.delete(0, lineNumberBuffer.length());
184 for (int i = onLine; i < current.length(); i++) {
185 ch = current.charAt(i);
186 if ('0' <= ch && '9' >= ch) {
187 lineNumberBuffer.append(ch);
191 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
193 Hashtable attributes = new Hashtable();
195 current = current.replaceAll("\n", "");
196 current = current.replaceAll("<b>", "");
197 current = current.replaceAll("</b>", "");
198 MarkerUtilities.setMessage(attributes, current);
200 if (current.indexOf(PARSE_ERROR_STRING) != -1)
201 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
202 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
203 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
205 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
206 MarkerUtilities.setLineNumber(attributes, lineNumber);
207 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
212 public void parse(String s) throws CoreException {
213 ReInit(new StringReader(s));
216 } catch (ParseException e) {
217 if (errorMessage == null) {
218 PHPeclipsePlugin.log(e);
220 setMarker(errorMessage, e.currentToken.beginLine, errorLevel);
227 * Call the php parse command ( php -l -f <filename> )
228 * and create markers according to the external parser output
230 public static void phpExternalParse(IFile file) {
231 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
232 String filename = file.getLocation().toString();
234 String[] arguments = { filename };
235 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
236 String command = form.format(arguments);
238 String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
241 // parse the buffer to find the errors and warnings
242 createMarkers(parserResult, file);
243 } catch (CoreException e) {
244 PHPeclipsePlugin.log(e);
248 public void parse() throws ParseException {
253 PARSER_END(PHPParser)
257 <PHPSTART : "<?php" | "<?"> : PHPPARSING
262 <PHPEND :"?>"> : DEFAULT
286 "//" : IN_SINGLE_LINE_COMMENT
288 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
290 "/*" : IN_MULTI_LINE_COMMENT
293 <IN_SINGLE_LINE_COMMENT>
296 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" | "?>" > : PHPPARSING
302 <FORMAL_COMMENT: "*/" > : PHPPARSING
305 <IN_MULTI_LINE_COMMENT>
308 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
311 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
321 | <FUNCTION : "function">
324 | <ELSEIF : "elseif">
329 /* LANGUAGE CONSTRUCT */
334 | <INCLUDE : "include">
335 | <REQUIRE : "require">
336 | <INCLUDE_ONCE : "include_once">
337 | <REQUIRE_ONCE : "require_once">
338 | <GLOBAL : "global">
339 | <STATIC : "static">
340 | <CLASSACCESS: "->">
341 | <STATICCLASSACCESS: "::">
342 | <ARRAYASSIGN: "=>">
345 /* RESERVED WORDS AND LITERALS */
352 | < CONTINUE: "continue" >
353 | < _DEFAULT: "default" >
355 | < EXTENDS: "extends" >
361 | < RETURN: "return" >
363 | < SWITCH: "switch" >
367 | < ENDWHILE : "endwhile" >
375 | <OBJECT : "object">
377 | <BOOLEAN : "boolean">
379 | <DOUBLE : "double">
382 | <INTEGER : "integer">
396 <DECIMAL_LITERAL> (["l","L"])?
397 | <HEX_LITERAL> (["l","L"])?
398 | <OCTAL_LITERAL> (["l","L"])?
401 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
403 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
405 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
407 < FLOATING_POINT_LITERAL:
408 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
409 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
410 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
411 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
414 < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
416 < STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
441 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
444 ["a"-"z"] | ["A"-"Z"]
500 | < RSIGNEDSHIFT: ">>" >
501 | < RUNSIGNEDSHIFT: ">>>" >
502 | < PLUSASSIGN: "+=" >
503 | < MINUSASSIGN: "-=" >
504 | < STARASSIGN: "*=" >
505 | < SLASHASSIGN: "/=" >
506 | < ANDASSIGN: "&=" >
508 | < XORASSIGN: "^=" >
509 | < DOTASSIGN: ".=" >
510 | < REMASSIGN: "%=" >
511 | < LSHIFTASSIGN: "<<=" >
512 | < RSIGNEDSHIFTASSIGN: ">>=" >
513 | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
518 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
521 /*****************************************
522 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
523 *****************************************/
526 * Program structuring syntax follows.
539 (<PHPSTART> Php() <PHPEND>)*
549 void ClassDeclaration() :
551 PHPClassDeclaration classDeclaration;
553 int pos = jj_input_stream.bufpos;
556 <CLASS> className = <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
558 classDeclaration = new PHPClassDeclaration(currentSegment,className.image,pos);
559 currentSegment.add(classDeclaration);
560 currentSegment = classDeclaration;
564 currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
571 <LBRACE> ( ClassBodyDeclaration() )* <RBRACE>
574 void ClassBodyDeclaration() :
582 void FieldDeclaration() :
585 <VAR> VariableDeclarator() ( <COMMA> VariableDeclarator() )* <SEMICOLON>
588 void VariableDeclarator() :
591 VariableDeclaratorId() [ <ASSIGN> VariableInitializer() ]
594 void VariableDeclaratorId() :
597 Variable() ( LOOKAHEAD(2) VariableSuffix() )*
603 <DOLLAR_ID> (<LBRACE> Expression() <RBRACE>) *
605 <DOLLAR> VariableName()
611 <LBRACE> Expression() <RBRACE>
613 <IDENTIFIER> (<LBRACE> Expression() <RBRACE>) *
615 <DOLLAR> VariableName()
618 void VariableInitializer() :
624 void ArrayVariable() :
627 Expression() (<ARRAYASSIGN> Expression())*
630 void ArrayInitializer() :
633 <LPAREN> [ ArrayVariable() ( LOOKAHEAD(2) <COMMA> ArrayVariable() )* ]<RPAREN>
636 void MethodDeclaration() :
638 PHPFunctionDeclaration functionDeclaration;
641 <FUNCTION> functionDeclaration = MethodDeclarator()
643 currentSegment.add(functionDeclaration);
644 currentSegment = functionDeclaration;
646 ( Block() | <SEMICOLON> )
648 currentSegment = (PHPSegmentWithChildren) currentSegment.getParent();
652 PHPFunctionDeclaration MethodDeclarator() :
654 Token bit_and = null;
656 StringBuffer methodDeclaration = new StringBuffer();
657 String formalParameters;
658 int pos = jj_input_stream.bufpos;
661 [ bit_and = <BIT_AND>]
662 identifier = <IDENTIFIER> FormalParameters()
664 if (bit_and != null) {
665 methodDeclaration.append("&");
667 methodDeclaration.append(identifier);
668 return new PHPFunctionDeclaration(currentSegment,methodDeclaration.toString(),pos);
672 void FormalParameters() :
675 <LPAREN> [ FormalParameter() ( <COMMA> FormalParameter() )* ] <RPAREN>
678 void FormalParameter() :
681 [<BIT_AND>] VariableDeclarator()
705 * Expression syntax follows.
710 * This expansion has been written this way instead of:
711 * Assignment() | ConditionalExpression()
712 * for performance reasons.
713 * However, it is a weakening of the grammar for it allows the LHS of
714 * assignments to be any conditional expression whereas it can only be
715 * a primary expression. Consider adding a semantic predicate to work
722 ConditionalExpression()
724 AssignmentOperator() Expression()
728 void AssignmentOperator() :
731 <ASSIGN> | <STARASSIGN> | <SLASHASSIGN> | <REMASSIGN> | <PLUSASSIGN> | <MINUSASSIGN> | <LSHIFTASSIGN> | <RSIGNEDSHIFTASSIGN> | <RUNSIGNEDSHIFTASSIGN> | <ANDASSIGN> | <XORASSIGN> | <ORASSIGN> | <DOTASSIGN>
734 void ConditionalExpression() :
737 ConditionalOrExpression() [ <HOOK> Expression() <COLON> ConditionalExpression() ]
740 void ConditionalOrExpression() :
743 ConditionalAndExpression() ( (<SC_OR> | <_ORL>) ConditionalAndExpression() )*
746 void ConditionalAndExpression() :
749 ConcatExpression() ( (<SC_AND> | <_ANDL>) ConcatExpression() )*
752 void ConcatExpression() :
755 InclusiveOrExpression() ( <DOT> InclusiveOrExpression() )*
758 void InclusiveOrExpression() :
761 ExclusiveOrExpression() ( <BIT_OR> ExclusiveOrExpression() )*
764 void ExclusiveOrExpression() :
767 AndExpression() ( <XOR> AndExpression() )*
770 void AndExpression() :
773 EqualityExpression() ( <BIT_AND> EqualityExpression() )*
776 void EqualityExpression() :
779 RelationalExpression() ( ( <EQ> | <NE> ) RelationalExpression() )*
782 void RelationalExpression() :
785 ShiftExpression() ( ( <LT> | <GT> | <LE> | <GE> ) ShiftExpression() )*
788 void ShiftExpression() :
791 AdditiveExpression() ( ( <LSHIFT> | <RSIGNEDSHIFT> | <RUNSIGNEDSHIFT> ) AdditiveExpression() )*
794 void AdditiveExpression() :
797 MultiplicativeExpression() ( ( <PLUS> | <MINUS> ) MultiplicativeExpression() )*
800 void MultiplicativeExpression() :
803 UnaryExpression() ( ( <STAR> | <SLASH> | <REM> ) UnaryExpression() )*
806 void UnaryExpression() :
809 <AT> UnaryExpression()
811 ( <PLUS> | <MINUS> ) UnaryExpression()
813 PreIncrementExpression()
815 PreDecrementExpression()
817 UnaryExpressionNotPlusMinus()
820 void PreIncrementExpression() :
823 <INCR> PrimaryExpression()
826 void PreDecrementExpression() :
829 <DECR> PrimaryExpression()
832 void UnaryExpressionNotPlusMinus() :
835 <BANG> UnaryExpression()
837 LOOKAHEAD( <LPAREN> Type() <RPAREN> )
844 <LPAREN>Expression()<RPAREN>
847 void CastExpression() :
850 <LPAREN> Type() <RPAREN> UnaryExpression()
853 void PostfixExpression() :
856 PrimaryExpression() [ <INCR> | <DECR> ]
859 void PrimaryExpression() :
863 <IDENTIFIER> <STATICCLASSACCESS> ClassIdentifier() (PrimarySuffix())*
865 PrimaryPrefix() ( PrimarySuffix() )*
867 <ARRAY> ArrayInitializer()
870 void PrimaryPrefix() :
875 <NEW> ClassIdentifier()
877 VariableDeclaratorId()
880 void ClassIdentifier():
885 VariableDeclaratorId()
888 void PrimarySuffix() :
896 void VariableSuffix() :
899 <CLASSACCESS> VariableName()
901 <LBRACKET> [ Expression() ] <RBRACKET>
909 <FLOATING_POINT_LITERAL>
918 void BooleanLiteral() :
935 <LPAREN> [ ArgumentList() ]
938 } catch (ParseException e) {
939 errorMessage = "')' expected to close the argument list";
945 void ArgumentList() :
952 } catch (ParseException e) {
953 errorMessage = "expression expected after a comma in argument list";
961 * Statement syntax follows.
968 Expression() (<SEMICOLON> | "?>")
977 StatementExpression()
980 } catch (ParseException e) {
981 errorMessage = "';' expected after expression";
1011 void IncludeStatement() :
1014 <REQUIRE> Expression() (<SEMICOLON> | "?>")
1016 <REQUIRE_ONCE> Expression() (<SEMICOLON> | "?>")
1018 <INCLUDE> Expression() (<SEMICOLON> | "?>")
1020 <INCLUDE_ONCE> Expression() (<SEMICOLON> | "?>")
1023 void PrintExpression() :
1026 <PRINT> Expression()
1029 void EchoStatement() :
1032 <ECHO> Expression() (<COMMA> Expression())*
1034 (<SEMICOLON> | "?>")
1035 } catch (ParseException e) {
1036 errorMessage = "';' expected after 'echo' statement";
1042 void GlobalStatement() :
1045 <GLOBAL> VariableDeclaratorId() (<COMMA> VariableDeclaratorId())* (<SEMICOLON> | "?>")
1048 void StaticStatement() :
1051 <STATIC> VariableDeclarator() (<COMMA> VariableDeclarator())* (<SEMICOLON> | "?>")
1054 void LabeledStatement() :
1057 <IDENTIFIER> <COLON> Statement()
1063 <LBRACE> ( BlockStatement() )* <RBRACE>
1066 void BlockStatement() :
1076 void LocalVariableDeclaration() :
1079 VariableDeclarator() ( <COMMA> VariableDeclarator() )*
1082 void EmptyStatement() :
1088 void StatementExpression() :
1090 * The last expansion of this production accepts more than the legal
1091 * Java expansions for StatementExpression. This expansion does not
1092 * use PostfixExpression for performance reasons.
1096 PreIncrementExpression()
1098 PreDecrementExpression()
1106 AssignmentOperator() Expression()
1110 void SwitchStatement() :
1113 <SWITCH> <LPAREN> Expression() <RPAREN> <LBRACE>
1114 ( SwitchLabel() ( BlockStatement() )* )*
1118 void SwitchLabel() :
1121 <CASE> Expression() <COLON>
1126 void IfStatement() :
1128 * The disambiguating algorithm of JavaCC automatically binds dangling
1129 * else's to the innermost if statement. The LOOKAHEAD specification
1130 * is to tell JavaCC that we know what we are doing.
1134 <IF> Condition("if") Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) <ELSE> Statement() ]
1137 void Condition(String keyword) :
1142 } catch (ParseException e) {
1143 errorMessage = "'(' expected after " + keyword + " keyword";
1150 } catch (ParseException e) {
1151 errorMessage = "')' expected after " + keyword + " keyword";
1157 void ElseIfStatement() :
1160 <ELSEIF> Condition("elseif") Statement()
1163 void WhileStatement() :
1166 <WHILE> Condition("while") WhileStatement0()
1169 void WhileStatement0() :
1172 <COLON> (Statement())* <ENDWHILE> (<SEMICOLON> | "?>")
1177 void DoStatement() :
1180 <DO> Statement() <WHILE> Condition("while") (<SEMICOLON> | "?>")
1183 void ForStatement() :
1186 <FOR> <LPAREN> [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ ForUpdate() ] <RPAREN> Statement()
1192 LOOKAHEAD(LocalVariableDeclaration())
1193 LocalVariableDeclaration()
1195 StatementExpressionList()
1198 void StatementExpressionList() :
1201 StatementExpression() ( <COMMA> StatementExpression() )*
1207 StatementExpressionList()
1210 void BreakStatement() :
1213 <BREAK> [ <IDENTIFIER> ] <SEMICOLON>
1216 void ContinueStatement() :
1219 <CONTINUE> [ <IDENTIFIER> ] <SEMICOLON>
1222 void ReturnStatement() :
1225 <RETURN> [ Expression() ] <SEMICOLON>