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;
41 * This php parser is inspired by the Java 1.2 grammar example
42 * given with JavaCC. You can get JavaCC at http://www.webgain.com
43 * You can test the parser with the PHPParserTestCase2.java
44 * @author Matthieu Casanova
46 public class PHPParser extends PHPParserSuperclass {
48 private static PHPParser me;
50 private static IFile fileToParse;
52 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
53 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
54 public static final int ERROR = 2;
55 public static final int WARNING = 1;
56 public static final int INFO = 0;
57 PHPOutlineInfo outlineInfo;
58 private static int errorLevel = ERROR;
59 private static String errorMessage;
64 public static PHPParser getInstance(IFile fileToParse) {
66 me = new PHPParser(fileToParse);
68 me.setFileToParse(fileToParse);
73 public void setFileToParse(IFile fileToParse) {
74 this.fileToParse = fileToParse;
77 public static PHPParser getInstance(java.io.Reader stream) {
79 me = new PHPParser(stream);
86 public PHPParser(IFile fileToParse) {
87 this(new StringReader(""));
88 this.fileToParse = fileToParse;
91 public void phpParserTester(String strEval) throws CoreException, ParseException {
92 PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
93 StringReader stream = new StringReader(strEval);
94 if (jj_input_stream == null) {
95 jj_input_stream = new SimpleCharStream(stream, 1, 1);
97 ReInit(new StringReader(strEval));
101 public void htmlParserTester(String strEval) throws CoreException, ParseException {
102 StringReader stream = new StringReader(strEval);
103 if (jj_input_stream == null) {
104 jj_input_stream = new SimpleCharStream(stream, 1, 1);
110 public PHPOutlineInfo parseInfo(Object parent, String s) {
111 outlineInfo = new PHPOutlineInfo(parent);
112 StringReader stream = new StringReader(s);
113 if (jj_input_stream == null) {
114 jj_input_stream = new SimpleCharStream(stream, 1, 1);
119 } catch (ParseException e) {
120 if (errorMessage == null) {
121 PHPeclipsePlugin.log(e);
123 setMarker(errorMessage, e.currentToken.beginLine, errorLevel);
132 * Create marker for the parse error
134 private static void setMarker(String message, int lineNumber, int errorLevel) {
136 setMarker(fileToParse, message, lineNumber, errorLevel);
137 } catch (CoreException e) {
138 PHPeclipsePlugin.log(e);
142 public static void setMarker(IFile file, String message, int lineNumber, int errorLevel) throws CoreException {
144 Hashtable attributes = new Hashtable();
145 MarkerUtilities.setMessage(attributes, message);
146 switch (errorLevel) {
148 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
151 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
154 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
157 MarkerUtilities.setLineNumber(attributes, lineNumber);
158 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
163 * Create markers according to the external parser output
165 private static void createMarkers(String output, IFile file) throws CoreException {
166 // delete all markers
167 file.deleteMarkers(IMarker.PROBLEM, false, 0);
172 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
173 // newer php error output (tested with 4.2.3)
174 scanLine(output, file, indx, brIndx);
179 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
180 // older php error output (tested with 4.2.3)
181 scanLine(output, file, indx, brIndx);
187 private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
189 StringBuffer lineNumberBuffer = new StringBuffer(10);
191 current = output.substring(indx, brIndx);
193 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
194 int onLine = current.indexOf("on line <b>");
196 lineNumberBuffer.delete(0, lineNumberBuffer.length());
197 for (int i = onLine; i < current.length(); i++) {
198 ch = current.charAt(i);
199 if ('0' <= ch && '9' >= ch) {
200 lineNumberBuffer.append(ch);
204 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
206 Hashtable attributes = new Hashtable();
208 current = current.replaceAll("\n", "");
209 current = current.replaceAll("<b>", "");
210 current = current.replaceAll("</b>", "");
211 MarkerUtilities.setMessage(attributes, current);
213 if (current.indexOf(PARSE_ERROR_STRING) != -1)
214 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
215 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
216 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
218 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
219 MarkerUtilities.setLineNumber(attributes, lineNumber);
220 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
225 public void parse(String s) throws CoreException {
226 ReInit(new StringReader(s));
229 } catch (ParseException e) {
230 PHPeclipsePlugin.log(e);
235 * Call the php parse command ( php -l -f <filename> )
236 * and create markers according to the external parser output
238 public static void phpExternalParse(IFile file) {
239 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
240 String filename = file.getLocation().toString();
242 String[] arguments = { filename };
243 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
244 String command = form.format(arguments);
246 String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
249 // parse the buffer to find the errors and warnings
250 createMarkers(parserResult, file);
251 } catch (CoreException e) {
252 PHPeclipsePlugin.log(e);
256 public void parse() throws ParseException {
261 PARSER_END(PHPParser)
265 <PHPSTART : "<?php" | "<?"> : PHPPARSING
270 <PHPEND :"?>"> : DEFAULT
294 "//" : IN_SINGLE_LINE_COMMENT
296 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
298 "/*" : IN_MULTI_LINE_COMMENT
301 <IN_SINGLE_LINE_COMMENT>
304 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" | "?>" > : PHPPARSING
310 <FORMAL_COMMENT: "*/" > : PHPPARSING
313 <IN_MULTI_LINE_COMMENT>
316 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
319 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
329 | <FUNCTION : "function">
332 | <ELSEIF : "elseif">
337 /* LANGUAGE CONSTRUCT */
342 | <INCLUDE : "include">
343 | <REQUIRE : "require">
344 | <INCLUDE_ONCE : "include_once">
345 | <REQUIRE_ONCE : "require_once">
346 | <GLOBAL : "global">
347 | <STATIC : "static">
348 | <CLASSACCESS: "->">
349 | <STATICCLASSACCESS: "::">
350 | <ARRAYASSIGN: "=>">
353 /* RESERVED WORDS AND LITERALS */
360 | < CONTINUE: "continue" >
361 | < _DEFAULT: "default" >
363 | < EXTENDS: "extends" >
369 | < RETURN: "return" >
371 | < SWITCH: "switch" >
375 | < ENDWHILE : "endwhile" >
383 | <OBJECT : "object">
385 | <BOOLEAN : "boolean">
387 | <DOUBLE : "double">
390 | <INTEGER : "integer">
404 <DECIMAL_LITERAL> (["l","L"])?
405 | <HEX_LITERAL> (["l","L"])?
406 | <OCTAL_LITERAL> (["l","L"])?
409 < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
411 < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
413 < #OCTAL_LITERAL: "0" (["0"-"7"])* >
415 < FLOATING_POINT_LITERAL:
416 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
417 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
418 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
419 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
422 < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
424 < STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
449 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
452 ["a"-"z"] | ["A"-"Z"]
508 | < RSIGNEDSHIFT: ">>" >
509 | < RUNSIGNEDSHIFT: ">>>" >
510 | < PLUSASSIGN: "+=" >
511 | < MINUSASSIGN: "-=" >
512 | < STARASSIGN: "*=" >
513 | < SLASHASSIGN: "/=" >
514 | < ANDASSIGN: "&=" >
516 | < XORASSIGN: "^=" >
517 | < DOTASSIGN: ".=" >
518 | < REMASSIGN: "%=" >
519 | < LSHIFTASSIGN: "<<=" >
520 | < RSIGNEDSHIFTASSIGN: ">>=" >
521 | < RUNSIGNEDSHIFTASSIGN: ">>>=" >
526 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
529 /*****************************************
530 * THE JAVA LANGUAGE GRAMMAR STARTS HERE *
531 *****************************************/
534 * Program structuring syntax follows.
547 (<PHPSTART> Php() <PHPEND>)*
557 void ClassDeclaration() :
560 <CLASS> <IDENTIFIER> [ <EXTENDS> <IDENTIFIER> ]
567 <LBRACE> ( ClassBodyDeclaration() )* <RBRACE>
570 void ClassBodyDeclaration() :
578 void FieldDeclaration() :
581 <VAR> VariableDeclarator() ( <COMMA> VariableDeclarator() )* <SEMICOLON>
584 void VariableDeclarator() :
587 VariableDeclaratorId() [ <ASSIGN> VariableInitializer() ]
590 void VariableDeclaratorId() :
593 Variable() ( LOOKAHEAD(2) VariableSuffix() )*
599 <DOLLAR_ID> (<LBRACE> Expression() <RBRACE>) *
601 <DOLLAR> VariableName()
607 <LBRACE> Expression() <RBRACE>
609 <IDENTIFIER> (<LBRACE> Expression() <RBRACE>) *
611 <DOLLAR> VariableName()
614 void VariableInitializer() :
620 void ArrayVariable() :
623 Expression() (<ARRAYASSIGN> Expression())*
626 void ArrayInitializer() :
629 <LPAREN> [ ArrayVariable() ( LOOKAHEAD(2) <COMMA> ArrayVariable() )* ]<RPAREN>
632 void MethodDeclaration() :
635 <FUNCTION> MethodDeclarator()
636 ( Block() | <SEMICOLON> )
639 void MethodDeclarator() :
642 [<BIT_AND>] <IDENTIFIER> FormalParameters()
645 void FormalParameters() :
648 <LPAREN> [ FormalParameter() ( <COMMA> FormalParameter() )* ] <RPAREN>
651 void FormalParameter() :
654 [<BIT_AND>] VariableDeclarator()
678 * Expression syntax follows.
683 * This expansion has been written this way instead of:
684 * Assignment() | ConditionalExpression()
685 * for performance reasons.
686 * However, it is a weakening of the grammar for it allows the LHS of
687 * assignments to be any conditional expression whereas it can only be
688 * a primary expression. Consider adding a semantic predicate to work
695 ConditionalExpression()
697 AssignmentOperator() Expression()
701 void AssignmentOperator() :
704 <ASSIGN> | <STARASSIGN> | <SLASHASSIGN> | <REMASSIGN> | <PLUSASSIGN> | <MINUSASSIGN> | <LSHIFTASSIGN> | <RSIGNEDSHIFTASSIGN> | <RUNSIGNEDSHIFTASSIGN> | <ANDASSIGN> | <XORASSIGN> | <ORASSIGN> | <DOTASSIGN>
707 void ConditionalExpression() :
710 ConditionalOrExpression() [ <HOOK> Expression() <COLON> ConditionalExpression() ]
713 void ConditionalOrExpression() :
716 ConditionalAndExpression() ( (<SC_OR> | <_ORL>) ConditionalAndExpression() )*
719 void ConditionalAndExpression() :
722 ConcatExpression() ( (<SC_AND> | <_ANDL>) ConcatExpression() )*
725 void ConcatExpression() :
728 InclusiveOrExpression() ( <DOT> InclusiveOrExpression() )*
731 void InclusiveOrExpression() :
734 ExclusiveOrExpression() ( <BIT_OR> ExclusiveOrExpression() )*
737 void ExclusiveOrExpression() :
740 AndExpression() ( <XOR> AndExpression() )*
743 void AndExpression() :
746 EqualityExpression() ( <BIT_AND> EqualityExpression() )*
749 void EqualityExpression() :
752 RelationalExpression() ( ( <EQ> | <NE> ) RelationalExpression() )*
755 void RelationalExpression() :
758 ShiftExpression() ( ( <LT> | <GT> | <LE> | <GE> ) ShiftExpression() )*
761 void ShiftExpression() :
764 AdditiveExpression() ( ( <LSHIFT> | <RSIGNEDSHIFT> | <RUNSIGNEDSHIFT> ) AdditiveExpression() )*
767 void AdditiveExpression() :
770 MultiplicativeExpression() ( ( <PLUS> | <MINUS> ) MultiplicativeExpression() )*
773 void MultiplicativeExpression() :
776 UnaryExpression() ( ( <STAR> | <SLASH> | <REM> ) UnaryExpression() )*
779 void UnaryExpression() :
782 <AT> UnaryExpression()
784 ( <PLUS> | <MINUS> ) UnaryExpression()
786 PreIncrementExpression()
788 PreDecrementExpression()
790 UnaryExpressionNotPlusMinus()
793 void PreIncrementExpression() :
796 <INCR> PrimaryExpression()
799 void PreDecrementExpression() :
802 <DECR> PrimaryExpression()
805 void UnaryExpressionNotPlusMinus() :
808 <BANG> UnaryExpression()
810 LOOKAHEAD( <LPAREN> Type() <RPAREN> )
817 <LPAREN>Expression()<RPAREN>
820 void CastExpression() :
823 <LPAREN> Type() <RPAREN> UnaryExpression()
826 void PostfixExpression() :
829 PrimaryExpression() [ <INCR> | <DECR> ]
832 void PrimaryExpression() :
836 <IDENTIFIER> <STATICCLASSACCESS> ClassIdentifier() (PrimarySuffix())*
838 PrimaryPrefix() ( PrimarySuffix() )*
840 <ARRAY> ArrayInitializer()
843 void PrimaryPrefix() :
848 <NEW> ClassIdentifier()
850 VariableDeclaratorId()
853 void ClassIdentifier():
858 VariableDeclaratorId()
861 void PrimarySuffix() :
869 void VariableSuffix() :
872 <CLASSACCESS> VariableName()
874 <LBRACKET> [ Expression() ] <RBRACKET>
882 <FLOATING_POINT_LITERAL>
891 void BooleanLiteral() :
908 <LPAREN> [ ArgumentList() ] <RPAREN>
911 void ArgumentList() :
914 Expression() ( <COMMA> Expression() )*
918 * Statement syntax follows.
925 Expression() (<SEMICOLON> | "?>")
934 StatementExpression()
937 } catch (ParseException e) {
938 errorMessage = "';' expected after expression";
968 void IncludeStatement() :
971 <REQUIRE> Expression() (<SEMICOLON> | "?>")
973 <REQUIRE_ONCE> Expression() (<SEMICOLON> | "?>")
975 <INCLUDE> Expression() (<SEMICOLON> | "?>")
977 <INCLUDE_ONCE> Expression() (<SEMICOLON> | "?>")
980 void PrintExpression() :
986 void EchoStatement() :
989 <ECHO> Expression() (<COMMA> Expression())*
992 } catch (ParseException e) {
993 errorMessage = "';' expected after 'echo' statement";
999 void GlobalStatement() :
1002 <GLOBAL> VariableDeclaratorId() (<COMMA> VariableDeclaratorId())* (<SEMICOLON> | "?>")
1005 void StaticStatement() :
1008 <STATIC> VariableDeclarator() (<COMMA> VariableDeclarator())* (<SEMICOLON> | "?>")
1011 void LabeledStatement() :
1014 <IDENTIFIER> <COLON> Statement()
1020 <LBRACE> ( BlockStatement() )* <RBRACE>
1023 void BlockStatement() :
1033 void LocalVariableDeclaration() :
1036 VariableDeclarator() ( <COMMA> VariableDeclarator() )*
1039 void EmptyStatement() :
1045 void StatementExpression() :
1047 * The last expansion of this production accepts more than the legal
1048 * Java expansions for StatementExpression. This expansion does not
1049 * use PostfixExpression for performance reasons.
1053 PreIncrementExpression()
1055 PreDecrementExpression()
1063 AssignmentOperator() Expression()
1067 void SwitchStatement() :
1070 <SWITCH> <LPAREN> Expression() <RPAREN> <LBRACE>
1071 ( SwitchLabel() ( BlockStatement() )* )*
1075 void SwitchLabel() :
1078 <CASE> Expression() <COLON>
1083 void IfStatement() :
1085 * The disambiguating algorithm of JavaCC automatically binds dangling
1086 * else's to the innermost if statement. The LOOKAHEAD specification
1087 * is to tell JavaCC that we know what we are doing.
1091 <IF> Condition("if") Statement() [ LOOKAHEAD(1) ElseIfStatement() ] [ LOOKAHEAD(1) <ELSE> Statement() ]
1094 void Condition(String keyword) :
1099 } catch (ParseException e) {
1100 errorMessage = "'(' expected after " + keyword + " keyword";
1107 } catch (ParseException e) {
1108 errorMessage = "')' expected after " + keyword + " keyword";
1114 void ElseIfStatement() :
1117 <ELSEIF> Condition("elseif") Statement()
1120 void WhileStatement() :
1123 <WHILE> Condition("while") WhileStatement0()
1126 void WhileStatement0() :
1129 <COLON> (Statement())* <ENDWHILE> (<SEMICOLON> | "?>")
1134 void DoStatement() :
1137 <DO> Statement() <WHILE> Condition("while") (<SEMICOLON> | "?>")
1140 void ForStatement() :
1143 <FOR> <LPAREN> [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ ForUpdate() ] <RPAREN> Statement()
1149 LOOKAHEAD(LocalVariableDeclaration())
1150 LocalVariableDeclaration()
1152 StatementExpressionList()
1155 void StatementExpressionList() :
1158 StatementExpression() ( <COMMA> StatementExpression() )*
1164 StatementExpressionList()
1167 void BreakStatement() :
1170 <BREAK> [ <IDENTIFIER> ] <SEMICOLON>
1173 void ContinueStatement() :
1176 <CONTINUE> [ <IDENTIFIER> ] <SEMICOLON>
1179 void ReturnStatement() :
1182 <RETURN> [ Expression() ] <SEMICOLON>