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()
608 String 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()
709 ConditionalExpression()
711 AssignmentOperator() Expression()
715 void AssignmentOperator() :
718 <ASSIGN> | <STARASSIGN> | <SLASHASSIGN> | <REMASSIGN> | <PLUSASSIGN> | <MINUSASSIGN> | <LSHIFTASSIGN> | <RSIGNEDSHIFTASSIGN> | <RUNSIGNEDSHIFTASSIGN> | <ANDASSIGN> | <XORASSIGN> | <ORASSIGN> | <DOTASSIGN>
721 void ConditionalExpression() :
724 ConditionalOrExpression() [ <HOOK> Expression() <COLON> ConditionalExpression() ]
727 void ConditionalOrExpression() :
730 ConditionalAndExpression() ( (<SC_OR> | <_ORL>) ConditionalAndExpression() )*
733 void ConditionalAndExpression() :
736 ConcatExpression() ( (<SC_AND> | <_ANDL>) ConcatExpression() )*
739 void ConcatExpression() :
742 InclusiveOrExpression() ( <DOT> InclusiveOrExpression() )*
745 void InclusiveOrExpression() :
748 ExclusiveOrExpression() ( <BIT_OR> ExclusiveOrExpression() )*
751 void ExclusiveOrExpression() :
754 AndExpression() ( <XOR> AndExpression() )*
757 void AndExpression() :
760 EqualityExpression() ( <BIT_AND> EqualityExpression() )*
763 void EqualityExpression() :
766 RelationalExpression() ( ( <EQ> | <NE> ) RelationalExpression() )*
769 void RelationalExpression() :
772 ShiftExpression() ( ( <LT> | <GT> | <LE> | <GE> ) ShiftExpression() )*
775 void ShiftExpression() :
778 AdditiveExpression() ( ( <LSHIFT> | <RSIGNEDSHIFT> | <RUNSIGNEDSHIFT> ) AdditiveExpression() )*
781 void AdditiveExpression() :
784 MultiplicativeExpression() ( ( <PLUS> | <MINUS> ) MultiplicativeExpression() )*
787 void MultiplicativeExpression() :
790 UnaryExpression() ( ( <STAR> | <SLASH> | <REM> ) UnaryExpression() )*
793 void UnaryExpression() :
796 <AT> UnaryExpression()
798 ( <PLUS> | <MINUS> ) UnaryExpression()
800 PreIncrementExpression()
802 PreDecrementExpression()
804 UnaryExpressionNotPlusMinus()
807 void PreIncrementExpression() :
810 <INCR> PrimaryExpression()
813 void PreDecrementExpression() :
816 <DECR> PrimaryExpression()
819 void UnaryExpressionNotPlusMinus() :
822 <BANG> UnaryExpression()
824 LOOKAHEAD( <LPAREN> Type() <RPAREN> )
831 <LPAREN>Expression()<RPAREN>
834 void CastExpression() :
837 <LPAREN> Type() <RPAREN> UnaryExpression()
840 void PostfixExpression() :
843 PrimaryExpression() [ <INCR> | <DECR> ]
846 void PrimaryExpression() :
850 <IDENTIFIER> <STATICCLASSACCESS> ClassIdentifier() (PrimarySuffix())*
852 PrimaryPrefix() ( PrimarySuffix() )*
854 <ARRAY> ArrayInitializer()
857 void PrimaryPrefix() :
862 [<BIT_AND>] <NEW> ClassIdentifier()
864 VariableDeclaratorId()
867 void ClassIdentifier():
872 VariableDeclaratorId()
875 void PrimarySuffix() :
883 void VariableSuffix() :
886 <CLASSACCESS> VariableName()
888 <LBRACKET> [ Expression() ] <RBRACKET>
896 <FLOATING_POINT_LITERAL>
900 } catch (TokenMgrError e) {
901 errorMessage = "unterminated string";
903 throw generateParseException();
911 void BooleanLiteral() :
928 <LPAREN> [ ArgumentList() ]
931 } catch (ParseException e) {
932 errorMessage = "')' expected to close the argument list";
938 void ArgumentList() :
945 } catch (ParseException e) {
946 errorMessage = "expression expected after a comma in argument list";
954 * Statement syntax follows.
961 Expression() (<SEMICOLON> | "?>")
970 StatementExpression()
973 } catch (ParseException e) {
974 errorMessage = "';' expected after expression";
1004 void IncludeStatement() :
1007 <REQUIRE> Expression() (<SEMICOLON> | "?>")
1009 <REQUIRE_ONCE> Expression() (<SEMICOLON> | "?>")
1011 <INCLUDE> Expression() (<SEMICOLON> | "?>")
1013 <INCLUDE_ONCE> Expression() (<SEMICOLON> | "?>")
1016 void PrintExpression() :
1019 <PRINT> Expression()
1022 void EchoStatement() :
1025 <ECHO> Expression() (<COMMA> Expression())*
1027 (<SEMICOLON> | "?>")
1028 } catch (ParseException e) {
1029 errorMessage = "';' expected after 'echo' statement";
1035 void GlobalStatement() :
1038 <GLOBAL> VariableDeclaratorId() (<COMMA> VariableDeclaratorId())* (<SEMICOLON> | "?>")
1041 void StaticStatement() :
1044 <STATIC> VariableDeclarator() (<COMMA> VariableDeclarator())* (<SEMICOLON> | "?>")
1047 void LabeledStatement() :
1050 <IDENTIFIER> <COLON> Statement()
1056 <LBRACE> ( BlockStatement() )* <RBRACE>
1059 void BlockStatement() :
1069 void LocalVariableDeclaration() :
1072 VariableDeclarator() ( <COMMA> VariableDeclarator() )*
1075 void EmptyStatement() :
1081 void StatementExpression() :
1083 * The last expansion of this production accepts more than the legal
1084 * Java expansions for StatementExpression. This expansion does not
1085 * use PostfixExpression for performance reasons.
1089 PreIncrementExpression()
1091 PreDecrementExpression()
1099 AssignmentOperator() Expression()
1103 void SwitchStatement() :
1106 <SWITCH> <LPAREN> Expression() <RPAREN> <LBRACE>
1107 ( SwitchLabel() ( BlockStatement() )* )*
1111 void SwitchLabel() :
1114 <CASE> Expression() <COLON>
1119 void IfStatement() :
1121 * The disambiguating algorithm of JavaCC automatically binds dangling
1122 * else's to the innermost if statement. The LOOKAHEAD specification
1123 * is to tell JavaCC that we know what we are doing.
1127 <IF> Condition("if") Statement() ( LOOKAHEAD(1) ElseIfStatement() )* [ LOOKAHEAD(1) <ELSE> Statement() ]
1130 void Condition(String keyword) :
1135 } catch (ParseException e) {
1136 errorMessage = "'(' expected after " + keyword + " keyword";
1143 } catch (ParseException e) {
1144 errorMessage = "')' expected after " + keyword + " keyword";
1150 void ElseIfStatement() :
1153 <ELSEIF> Condition("elseif") Statement()
1156 void WhileStatement() :
1159 <WHILE> Condition("while") WhileStatement0()
1162 void WhileStatement0() :
1165 <COLON> (Statement())* <ENDWHILE> (<SEMICOLON> | "?>")
1170 void DoStatement() :
1173 <DO> Statement() <WHILE> Condition("while") (<SEMICOLON> | "?>")
1176 void ForStatement() :
1179 <FOR> <LPAREN> [ ForInit() ] <SEMICOLON> [ Expression() ] <SEMICOLON> [ ForUpdate() ] <RPAREN> Statement()
1185 LOOKAHEAD(LocalVariableDeclaration())
1186 LocalVariableDeclaration()
1188 StatementExpressionList()
1191 void StatementExpressionList() :
1194 StatementExpression() ( <COMMA> StatementExpression() )*
1200 StatementExpressionList()
1203 void BreakStatement() :
1206 <BREAK> [ <IDENTIFIER> ] <SEMICOLON>
1209 void ContinueStatement() :
1212 <CONTINUE> [ <IDENTIFIER> ] <SEMICOLON>
1215 void ReturnStatement() :
1218 <RETURN> [ Expression() ] <SEMICOLON>