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.io.CharArrayReader;
32 import java.util.Hashtable;
33 import java.io.StringReader;
34 import java.text.MessageFormat;
36 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
37 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
38 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
42 * This php parser is inspired by the Java 1.2 grammar example
43 * given with JavaCC. You can get JavaCC at http://www.webgain.com
44 * You can test the parser with the PHPParserTestCase2.java
45 * @author Matthieu Casanova
47 public class PHPParser extends PHPParserSuperclass {
49 private static PHPParser me;
51 private static IFile fileToParse;
53 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
54 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
55 public static final int ERROR = 2;
56 public static final int WARNING = 1;
57 public static final int INFO = 0;
58 PHPOutlineInfo outlineInfo;
59 private static int errorLevel = ERROR;
60 private static String errorMessage;
65 public static PHPParser getInstance(IFile fileToParse) {
67 me = new PHPParser(fileToParse);
69 me.setFileToParse(fileToParse);
74 public void setFileToParse(IFile fileToParse) {
75 this.fileToParse = fileToParse;
78 public static PHPParser getInstance(java.io.Reader stream) {
80 me = new PHPParser(stream);
87 public PHPParser(IFile fileToParse) {
88 this(new StringReader(""));
89 this.fileToParse = fileToParse;
92 public void phpParserTester(String strEval) throws CoreException, ParseException {
93 PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
94 StringReader stream = new StringReader(strEval);
95 if (jj_input_stream == null) {
96 jj_input_stream = new SimpleCharStream(stream, 1, 1);
98 ReInit(new StringReader(strEval));
102 public void htmlParserTester(String strEval) throws CoreException, ParseException {
103 StringReader stream = new StringReader(strEval);
104 if (jj_input_stream == null) {
105 jj_input_stream = new SimpleCharStream(stream, 1, 1);
111 public PHPOutlineInfo parseInfo(Object parent, String s) {
112 outlineInfo = new PHPOutlineInfo(parent);
113 StringReader stream = new StringReader(s);
114 if (jj_input_stream == null) {
115 jj_input_stream = new SimpleCharStream(stream, 1, 1);
120 } catch (ParseException e) {
121 if (errorMessage == null) {
122 PHPeclipsePlugin.log(e);
124 setMarker(errorMessage, e.currentToken.beginLine, errorLevel);
133 * Create marker for the parse error
135 private static void setMarker(String message, int lineNumber, int errorLevel) {
137 setMarker(fileToParse, message, lineNumber, errorLevel);
138 } catch (CoreException e) {
139 PHPeclipsePlugin.log(e);
143 public static void setMarker(IFile file, String message, int lineNumber, int errorLevel) throws CoreException {
145 Hashtable attributes = new Hashtable();
146 MarkerUtilities.setMessage(attributes, message);
147 switch (errorLevel) {
149 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
152 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
155 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
158 MarkerUtilities.setLineNumber(attributes, lineNumber);
159 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
164 * Create markers according to the external parser output
166 private static void createMarkers(String output, IFile file) throws CoreException {
167 // delete all markers
168 file.deleteMarkers(IMarker.PROBLEM, false, 0);
173 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
174 // newer php error output (tested with 4.2.3)
175 scanLine(output, file, indx, brIndx);
180 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
181 // older php error output (tested with 4.2.3)
182 scanLine(output, file, indx, brIndx);
188 private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
190 StringBuffer lineNumberBuffer = new StringBuffer(10);
192 current = output.substring(indx, brIndx);
194 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
195 int onLine = current.indexOf("on line <b>");
197 lineNumberBuffer.delete(0, lineNumberBuffer.length());
198 for (int i = onLine; i < current.length(); i++) {
199 ch = current.charAt(i);
200 if ('0' <= ch && '9' >= ch) {
201 lineNumberBuffer.append(ch);
205 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
207 Hashtable attributes = new Hashtable();
209 current = current.replaceAll("\n", "");
210 current = current.replaceAll("<b>", "");
211 current = current.replaceAll("</b>", "");
212 MarkerUtilities.setMessage(attributes, current);
214 if (current.indexOf(PARSE_ERROR_STRING) != -1)
215 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
216 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
217 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
219 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
220 MarkerUtilities.setLineNumber(attributes, lineNumber);
221 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
226 public void parse(String s) throws CoreException {
227 ReInit(new StringReader(s));
230 } catch (ParseException e) {
231 PHPeclipsePlugin.log(e);
236 * Call the php parse command ( php -l -f <filename> )
237 * and create markers according to the external parser output
239 public static void phpExternalParse(IFile file) {
240 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
241 String filename = file.getLocation().toString();
243 String[] arguments = { filename };
244 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
245 String command = form.format(arguments);
247 String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
250 // parse the buffer to find the errors and warnings
251 createMarkers(parserResult, file);
252 } catch (CoreException e) {
253 PHPeclipsePlugin.log(e);
257 public void parse() throws ParseException {
262 PARSER_END(PHPParser)
295 "//" : IN_SINGLE_LINE_COMMENT
297 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
299 "/*" : IN_MULTI_LINE_COMMENT
302 <IN_SINGLE_LINE_COMMENT>
305 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" | "?>" > : PHPPARSING
311 <FORMAL_COMMENT: "*/" > : PHPPARSING
314 <IN_MULTI_LINE_COMMENT>
317 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
320 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
330 | <FUNCTION : "function">
333 | <ELSEIF : "elseif">
338 /* LANGUAGE CONSTRUCT */
343 | <INCLUDE : "include">
344 | <REQUIRE : "require">
345 | <INCLUDE_ONCE : "include_once">
346 | <REQUIRE_ONCE : "require_once">
347 | <GLOBAL : "global">
348 | <STATIC : "static">
349 | <CLASSACCESS: "->">
350 | <STATICCLASSACCESS: "::">
351 | <ARRAYASSIGN: "=>">
354 /* RESERVED WORDS AND LITERALS */
361 | < CONTINUE: "continue" >
362 | < _DEFAULT: "default" >
364 | < EXTENDS: "extends" >
370 | < RETURN: "return" >
372 | < SWITCH: "switch" >
376 | < ENDWHILE : "endwhile" >
384 | <OBJECT : "object">
386 | <BOOLEAN : "boolean">
388 | <DOUBLE : "double">
391 | <INTEGER : "integer">
405 <DECIMAL_LITERAL> (["l","L"])?
406 | <HEX_LITERAL> (["l","L"])?
407 | <OCTAL_LITERAL> (["l","L"])?
410 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
412 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
414 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
416 < FLOATING_POINT_LITERAL:
417 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
418 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
419 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
420 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
423 < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
425 < STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
450 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
453 ["a"-"z"] | ["A"-"Z"]
509 | < RSIGNEDSHIFT: ">>" >
510 | < RUNSIGNEDSHIFT: ">>>" >
511 | < PLUSASSIGN: "+=" >
512 | < MINUSASSIGN: "-=" >
513 | < STARASSIGN: "*=" >
514 | < SLASHASSIGN: "/=" >
515 | < ANDASSIGN: "&=" >
517 | < XORASSIGN: "^=" >
518 | < DOTASSIGN: ".=" >
519 | < REMASSIGN: "%=" >
520 | < LSHIFTASSIGN: "<<=" >
521 | < RSIGNEDSHIFTASSIGN: ">>=" >
522 | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
527 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
530 /*****************************************
531 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
532 *****************************************/
535 * Program structuring syntax follows.
548 ("<?php" Php() "?>")*
558 void ClassDeclaration() :
561 <CLASS> <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
568 <LBRACE> ( ClassBodyDeclaration() )* <RBRACE>
571 void ClassBodyDeclaration() :
579 void FieldDeclaration() :
582 <VAR> VariableDeclarator() ( <COMMA> VariableDeclarator() )* <SEMICOLON>
585 void VariableDeclarator() :
588 VariableDeclaratorId() [ <ASSIGN> VariableInitializer() ]
591 void VariableDeclaratorId() :
594 Variable() ( LOOKAHEAD(2) VariableSuffix() )*
600 <DOLLAR_ID> (<LBRACE> Expression() <RBRACE>) *
602 <DOLLAR> VariableName()
608 <LBRACE> Expression() <RBRACE>
610 <IDENTIFIER> (<LBRACE> Expression() <RBRACE>) *
612 <DOLLAR> VariableName()
615 void VariableInitializer() :
621 void ArrayVariable() :
624 Expression() (<ARRAYASSIGN> Expression())*
627 void ArrayInitializer() :
630 <LPAREN> [ ArrayVariable() ( LOOKAHEAD(2) <COMMA> ArrayVariable() )* ]<RPAREN>
633 void MethodDeclaration() :
636 <FUNCTION> MethodDeclarator()
637 ( Block() | <SEMICOLON> )
640 void MethodDeclarator() :
643 [<BIT_AND>] <IDENTIFIER> FormalParameters()
646 void FormalParameters() :
649 <LPAREN> [ FormalParameter() ( <COMMA> FormalParameter() )* ] <RPAREN>
652 void FormalParameter() :
655 [<BIT_AND>] VariableDeclarator()
679 * Expression syntax follows.
684 * This expansion has been written this way instead of:
685 * Assignment() | ConditionalExpression()
686 * for performance reasons.
687 * However, it is a weakening of the grammar for it allows the LHS of
688 * assignments to be any conditional expression whereas it can only be
689 * a primary expression. Consider adding a semantic predicate to work
696 ConditionalExpression()
698 AssignmentOperator() Expression()
702 void AssignmentOperator() :
705 <ASSIGN> | <STARASSIGN> | <SLASHASSIGN> | <REMASSIGN> | <PLUSASSIGN> | <MINUSASSIGN> | <LSHIFTASSIGN> | <RSIGNEDSHIFTASSIGN> | <RUNSIGNEDSHIFTASSIGN> | <ANDASSIGN> | <XORASSIGN> | <ORASSIGN> | <DOTASSIGN>
708 void ConditionalExpression() :
711 ConditionalOrExpression() [ <HOOK> Expression() <COLON> ConditionalExpression() ]
714 void ConditionalOrExpression() :
717 ConditionalAndExpression() ( (<SC_OR> | <_ORL>) ConditionalAndExpression() )*
720 void ConditionalAndExpression() :
723 ConcatExpression() ( (<SC_AND> | <_ANDL>) ConcatExpression() )*
726 void ConcatExpression() :
729 InclusiveOrExpression() ( <DOT> InclusiveOrExpression() )*
732 void InclusiveOrExpression() :
735 ExclusiveOrExpression() ( <BIT_OR> ExclusiveOrExpression() )*
738 void ExclusiveOrExpression() :
741 AndExpression() ( <XOR> AndExpression() )*
744 void AndExpression() :
747 EqualityExpression() ( <BIT_AND> EqualityExpression() )*
750 void EqualityExpression() :
753 RelationalExpression() ( ( <EQ> | <NE> ) RelationalExpression() )*
756 void RelationalExpression() :
759 ShiftExpression() ( ( <LT> | <GT> | <LE> | <GE> ) ShiftExpression() )*
762 void ShiftExpression() :
765 AdditiveExpression() ( ( <LSHIFT> | <RSIGNEDSHIFT> | <RUNSIGNEDSHIFT> ) AdditiveExpression() )*
768 void AdditiveExpression() :
771 MultiplicativeExpression() ( ( <PLUS> | <MINUS> ) MultiplicativeExpression() )*
774 void MultiplicativeExpression() :
777 UnaryExpression() ( ( <STAR> | <SLASH> | <REM> ) UnaryExpression() )*
780 void UnaryExpression() :
783 <AT> UnaryExpression()
785 ( <PLUS> | <MINUS> ) UnaryExpression()
787 PreIncrementExpression()
789 PreDecrementExpression()
791 UnaryExpressionNotPlusMinus()
794 void PreIncrementExpression() :
797 <INCR> PrimaryExpression()
800 void PreDecrementExpression() :
803 <DECR> PrimaryExpression()
806 void UnaryExpressionNotPlusMinus() :
809 <BANG> UnaryExpression()
811 LOOKAHEAD( <LPAREN> Type() <RPAREN> )
818 <LPAREN>Expression()<RPAREN>
821 void CastExpression() :
824 <LPAREN> Type() <RPAREN> UnaryExpression()
827 void PostfixExpression() :
830 PrimaryExpression() [ <INCR> | <DECR> ]
833 void PrimaryExpression() :
837 <IDENTIFIER> <STATICCLASSACCESS> ClassIdentifier() (PrimarySuffix())*
839 PrimaryPrefix() ( PrimarySuffix() )*
841 <ARRAY> ArrayInitializer()
844 void PrimaryPrefix() :
849 <NEW> ClassIdentifier()
851 VariableDeclaratorId()
854 void ClassIdentifier():
859 VariableDeclaratorId()
862 void PrimarySuffix() :
870 void VariableSuffix() :
873 <CLASSACCESS> VariableName()
875 <LBRACKET> [ Expression() ] <RBRACKET>
883 <FLOATING_POINT_LITERAL>
892 void BooleanLiteral() :
909 <LPAREN> [ ArgumentList() ] <RPAREN>
912 void ArgumentList() :
915 Expression() ( <COMMA> Expression() )*
919 * Statement syntax follows.
926 Expression() (<SEMICOLON> | "?>")
935 StatementExpression()
938 } catch (ParseException e) {
939 errorMessage = "';' expected after expression";
969 void IncludeStatement() :
972 <REQUIRE> Expression() (<SEMICOLON> | "?>")
974 <REQUIRE_ONCE> Expression() (<SEMICOLON> | "?>")
976 <INCLUDE> Expression() (<SEMICOLON> | "?>")
978 <INCLUDE_ONCE> Expression() (<SEMICOLON> | "?>")
981 void PrintExpression() :
987 void EchoStatement() :
990 <ECHO> Expression() (<COMMA> Expression())*
993 } catch (ParseException e) {
994 errorMessage = "';' expected after 'echo' statement";
1000 void GlobalStatement() :
1003 <GLOBAL> VariableDeclaratorId() (<COMMA> VariableDeclaratorId())* (<SEMICOLON> | "?>")
1006 void StaticStatement() :
1009 <STATIC> VariableDeclarator() (<COMMA> VariableDeclarator())* (<SEMICOLON> | "?>")
1012 void LabeledStatement() :
1015 <IDENTIFIER> <COLON> Statement()
1021 <LBRACE> ( BlockStatement() )* <RBRACE>
1024 void BlockStatement() :
1034 void LocalVariableDeclaration() :
1037 VariableDeclarator() ( <COMMA> VariableDeclarator() )*
1040 void EmptyStatement() :
1046 void StatementExpression() :
1048 * The last expansion of this production accepts more than the legal
1049 * Java expansions for StatementExpression. This expansion does not
1050 * use PostfixExpression for performance reasons.
1054 PreIncrementExpression()
1056 PreDecrementExpression()
1064 AssignmentOperator() Expression()
1068 void SwitchStatement() :
1071 <SWITCH> <LPAREN> Expression() <RPAREN> <LBRACE>
1072 ( SwitchLabel() ( BlockStatement() )* )*
1076 void SwitchLabel() :
1079 <CASE> Expression() <COLON>
1084 void IfStatement() :
1086 * The disambiguating algorithm of JavaCC automatically binds dangling
1087 * else's to the innermost if statement. The LOOKAHEAD specification
1088 * is to tell JavaCC that we know what we are doing.
1092 <IF> Condition("if") Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) <ELSE> Statement() ]
1095 void Condition(String keyword) :
1100 } catch (ParseException e) {
1101 errorMessage = "'(' expected after " + keyword + " keyword";
1108 } catch (ParseException e) {
1109 errorMessage = "')' expected after " + keyword + " keyword";
1115 void ElseIfStatement() :
1118 <ELSEIF> Condition("elseif") Statement()
1121 void WhileStatement() :
1124 <WHILE> Condition("while") WhileStatement0()
1127 void WhileStatement0() :
1130 <COLON> (Statement())* <ENDWHILE> (<SEMICOLON> | "?>")
1135 void DoStatement() :
1138 <DO> Statement() <WHILE> Condition("while") (<SEMICOLON> | "?>")
1141 void ForStatement() :
1144 <FOR> <LPAREN> [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ ForUpdate() ] <RPAREN> Statement()
1150 LOOKAHEAD(LocalVariableDeclaration())
1151 LocalVariableDeclaration()
1153 StatementExpressionList()
1156 void StatementExpressionList() :
1159 StatementExpression() ( <COMMA> StatementExpression() )*
1165 StatementExpressionList()
1168 void BreakStatement() :
1171 <BREAK> [ <IDENTIFIER> ] <SEMICOLON>
1174 void ContinueStatement() :
1177 <CONTINUE> [ <IDENTIFIER> ] <SEMICOLON>
1180 void ReturnStatement() :
1183 <RETURN> [ Expression() ] <SEMICOLON>