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.util.Enumeration;
33 import java.util.ArrayList;
34 import java.io.StringReader;
36 import java.text.MessageFormat;
38 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
39 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
40 import net.sourceforge.phpdt.internal.compiler.ast.*;
41 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
42 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
46 * This php parser is inspired by the Java 1.2 grammar example
47 * given with JavaCC. You can get JavaCC at http://www.webgain.com
48 * You can test the parser with the PHPParserTestCase2.java
49 * @author Matthieu Casanova
51 public final class PHPParser extends PHPParserSuperclass {
53 /** The file that is parsed. */
54 private static IFile fileToParse;
56 /** The current segment. */
57 private static OutlineableWithChildren currentSegment;
59 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
60 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
61 static PHPOutlineInfo outlineInfo;
63 private static boolean assigning;
65 /** The error level of the current ParseException. */
66 private static int errorLevel = ERROR;
67 /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
68 private static String errorMessage;
70 private static int errorStart = -1;
71 private static int errorEnd = -1;
72 private static PHPDocument phpDocument;
74 * The point where html starts.
75 * It will be used by the token manager to create HTMLCode objects
77 public static int htmlStart;
80 private final static int AstStackIncrement = 100;
81 /** The stack of node. */
82 private static AstNode[] nodes;
83 /** The cursor in expression stack. */
84 private static int nodePtr;
86 public final void setFileToParse(final IFile fileToParse) {
87 this.fileToParse = fileToParse;
93 public PHPParser(final IFile fileToParse) {
94 this(new StringReader(""));
95 this.fileToParse = fileToParse;
99 * Reinitialize the parser.
101 private static final void init() {
102 nodes = new AstNode[AstStackIncrement];
108 * Add an php node on the stack.
109 * @param node the node that will be added to the stack
111 private static final void pushOnAstNodes(AstNode node) {
113 nodes[++nodePtr] = node;
114 } catch (IndexOutOfBoundsException e) {
115 int oldStackLength = nodes.length;
116 AstNode[] oldStack = nodes;
117 nodes = new AstNode[oldStackLength + AstStackIncrement];
118 System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
119 nodePtr = oldStackLength;
120 nodes[nodePtr] = node;
124 public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
125 phpDocument = new PHPDocument(parent,"_root".toCharArray());
126 currentSegment = phpDocument;
127 outlineInfo = new PHPOutlineInfo(parent, currentSegment);
128 final StringReader stream = new StringReader(s);
129 if (jj_input_stream == null) {
130 jj_input_stream = new SimpleCharStream(stream, 1, 1);
136 phpDocument.nodes = new AstNode[nodes.length];
137 System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
138 if (PHPeclipsePlugin.DEBUG) {
139 PHPeclipsePlugin.log(1,phpDocument.toString());
141 } catch (ParseException e) {
142 processParseException(e);
148 * This method will process the parse exception.
149 * If the error message is null, the parse exception wasn't catched and a trace is written in the log
150 * @param e the ParseException
152 private static void processParseException(final ParseException e) {
153 if (errorMessage == null) {
154 PHPeclipsePlugin.log(e);
155 errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
156 errorStart = SimpleCharStream.getPosition();
157 errorEnd = errorStart + 1;
164 * Create marker for the parse error
165 * @param e the ParseException
167 private static void setMarker(final ParseException e) {
169 if (errorStart == -1) {
170 setMarker(fileToParse,
172 SimpleCharStream.tokenBegin,
173 SimpleCharStream.tokenBegin + e.currentToken.image.length(),
175 "Line " + e.currentToken.beginLine);
177 setMarker(fileToParse,
182 "Line " + e.currentToken.beginLine);
186 } catch (CoreException e2) {
187 PHPeclipsePlugin.log(e2);
192 * Create markers according to the external parser output
194 private static void createMarkers(final String output, final IFile file) throws CoreException {
195 // delete all markers
196 file.deleteMarkers(IMarker.PROBLEM, false, 0);
201 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
202 // newer php error output (tested with 4.2.3)
203 scanLine(output, file, indx, brIndx);
208 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
209 // older php error output (tested with 4.2.3)
210 scanLine(output, file, indx, brIndx);
216 private static void scanLine(final String output,
219 final int brIndx) throws CoreException {
221 StringBuffer lineNumberBuffer = new StringBuffer(10);
223 current = output.substring(indx, brIndx);
225 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
226 int onLine = current.indexOf("on line <b>");
228 lineNumberBuffer.delete(0, lineNumberBuffer.length());
229 for (int i = onLine; i < current.length(); i++) {
230 ch = current.charAt(i);
231 if ('0' <= ch && '9' >= ch) {
232 lineNumberBuffer.append(ch);
236 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
238 Hashtable attributes = new Hashtable();
240 current = current.replaceAll("\n", "");
241 current = current.replaceAll("<b>", "");
242 current = current.replaceAll("</b>", "");
243 MarkerUtilities.setMessage(attributes, current);
245 if (current.indexOf(PARSE_ERROR_STRING) != -1)
246 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
247 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
248 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
250 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
251 MarkerUtilities.setLineNumber(attributes, lineNumber);
252 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
257 public final void parse(final String s) throws CoreException {
258 final StringReader stream = new StringReader(s);
259 if (jj_input_stream == null) {
260 jj_input_stream = new SimpleCharStream(stream, 1, 1);
266 } catch (ParseException e) {
267 processParseException(e);
272 * Call the php parse command ( php -l -f <filename> )
273 * and create markers according to the external parser output
275 public static void phpExternalParse(final IFile file) {
276 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
277 final String filename = file.getLocation().toString();
279 final String[] arguments = { filename };
280 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
281 final String command = form.format(arguments);
283 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
286 // parse the buffer to find the errors and warnings
287 createMarkers(parserResult, file);
288 } catch (CoreException e) {
289 PHPeclipsePlugin.log(e);
294 * Put a new html block in the stack.
296 public static final void createNewHTMLCode() {
297 final int currentPosition = SimpleCharStream.getPosition();
298 if (currentPosition == htmlStart) {
301 final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
302 pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
305 private static final void parse() throws ParseException {
310 PARSER_END(PHPParser)
314 <PHPSTARTSHORT : "<?"> {PHPParser.createNewHTMLCode();} : PHPPARSING
315 | <PHPSTARTLONG : "<?php"> {PHPParser.createNewHTMLCode();} : PHPPARSING
316 | <PHPECHOSTART : "<?="> {PHPParser.createNewHTMLCode();} : PHPPARSING
321 <PHPEND :"?>"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
324 /* Skip any character if we are not in php mode */
342 <PHPPARSING> SPECIAL_TOKEN :
344 "//" : IN_SINGLE_LINE_COMMENT
346 "#" : IN_SINGLE_LINE_COMMENT
348 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
350 "/*" : IN_MULTI_LINE_COMMENT
353 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
355 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
358 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
360 <SINGLE_LINE_COMMENT_PHPEND : "?>" > : DEFAULT
366 <FORMAL_COMMENT: "*/" > : PHPPARSING
369 <IN_MULTI_LINE_COMMENT>
372 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
375 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
385 | <FUNCTION : "function">
388 | <ELSEIF : "elseif">
395 /* LANGUAGE CONSTRUCT */
400 | <INCLUDE : "include">
401 | <REQUIRE : "require">
402 | <INCLUDE_ONCE : "include_once">
403 | <REQUIRE_ONCE : "require_once">
404 | <GLOBAL : "global">
405 | <STATIC : "static">
406 | <CLASSACCESS : "->">
407 | <STATICCLASSACCESS : "::">
408 | <ARRAYASSIGN : "=>">
411 /* RESERVED WORDS AND LITERALS */
417 | <CONTINUE : "continue">
418 | <_DEFAULT : "default">
420 | <EXTENDS : "extends">
425 | <RETURN : "return">
427 | <SWITCH : "switch">
432 | <ENDWHILE : "endwhile">
433 | <ENDSWITCH: "endswitch">
435 | <ENDFOR : "endfor">
436 | <FOREACH : "foreach">
444 | <OBJECT : "object">
446 | <BOOLEAN : "boolean">
448 | <DOUBLE : "double">
451 | <INTEGER : "integer">
481 | <RSIGNEDSHIFT : ">>">
482 | <RUNSIGNEDSHIFT : ">>>">
491 <DECIMAL_LITERAL> (["l","L"])?
492 | <HEX_LITERAL> (["l","L"])?
493 | <OCTAL_LITERAL> (["l","L"])?
496 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
498 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
500 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
502 <FLOATING_POINT_LITERAL:
503 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
504 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
505 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
506 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
509 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
511 <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
544 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
547 ["a"-"z"] | ["A"-"Z"]
555 "_" | ["\u007f"-"\u00ff"]
580 | <EQUAL_EQUAL : "==">
585 | <BANGDOUBLEEQUAL : "!==">
586 | <TRIPLEEQUAL : "===">
593 | <PLUSASSIGN : "+=">
594 | <MINUSASSIGN : "-=">
595 | <STARASSIGN : "*=">
596 | <SLASHASSIGN : "/=">
602 | <TILDEEQUAL : "~=">
603 | <LSHIFTASSIGN : "<<=">
604 | <RSIGNEDSHIFTASSIGN : ">>=">
609 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
618 } catch (TokenMgrError e) {
619 PHPeclipsePlugin.log(e);
620 errorStart = SimpleCharStream.getPosition();
621 errorEnd = errorStart + 1;
622 errorMessage = e.getMessage();
624 throw generateParseException();
629 * A php block is a <?= expression [;]?>
630 * or <?php somephpcode ?>
631 * or <? somephpcode ?>
635 final int start = SimpleCharStream.getPosition();
643 setMarker(fileToParse,
644 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
646 SimpleCharStream.getPosition(),
648 "Line " + token.beginLine);
649 } catch (CoreException e) {
650 PHPeclipsePlugin.log(e);
656 } catch (ParseException e) {
657 errorMessage = "'?>' expected";
659 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
660 errorEnd = SimpleCharStream.getPosition() + 1;
665 PHPEchoBlock phpEchoBlock() :
667 final Expression expr;
668 final int pos = SimpleCharStream.getPosition();
669 PHPEchoBlock echoBlock;
672 <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] <PHPEND>
674 echoBlock = new PHPEchoBlock(expr,pos,SimpleCharStream.getPosition());
675 pushOnAstNodes(echoBlock);
685 ClassDeclaration ClassDeclaration() :
687 final ClassDeclaration classDeclaration;
688 final Token className;
689 Token superclassName = null;
695 {pos = SimpleCharStream.getPosition();}
696 className = <IDENTIFIER>
697 } catch (ParseException e) {
698 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
700 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
701 errorEnd = SimpleCharStream.getPosition() + 1;
707 superclassName = <IDENTIFIER>
708 } catch (ParseException e) {
709 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
711 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
712 errorEnd = SimpleCharStream.getPosition() + 1;
717 if (superclassName == null) {
718 classDeclaration = new ClassDeclaration(currentSegment,
719 className.image.toCharArray(),
723 classDeclaration = new ClassDeclaration(currentSegment,
724 className.image.toCharArray(),
725 superclassName.image.toCharArray(),
729 currentSegment.add(classDeclaration);
730 currentSegment = classDeclaration;
732 ClassBody(classDeclaration)
733 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
734 classDeclaration.sourceEnd = SimpleCharStream.getPosition();
735 pushOnAstNodes(classDeclaration);
736 return classDeclaration;}
739 void ClassBody(ClassDeclaration classDeclaration) :
744 } catch (ParseException e) {
745 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected";
747 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
748 errorEnd = SimpleCharStream.getPosition() + 1;
751 ( ClassBodyDeclaration(classDeclaration) )*
754 } catch (ParseException e) {
755 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected";
757 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
758 errorEnd = SimpleCharStream.getPosition() + 1;
764 * A class can contain only methods and fields.
766 void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
768 MethodDeclaration method;
769 FieldDeclaration field;
772 method = MethodDeclaration() {method.setParent(classDeclaration);}
773 | field = FieldDeclaration()
777 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
779 FieldDeclaration FieldDeclaration() :
781 VariableDeclaration variableDeclaration;
782 VariableDeclaration[] list;
783 final ArrayList arrayList = new ArrayList();
784 final int pos = SimpleCharStream.getPosition();
787 <VAR> variableDeclaration = VariableDeclarator()
788 {arrayList.add(variableDeclaration);
789 outlineInfo.addVariable(new String(variableDeclaration.name));
790 currentSegment.add(variableDeclaration);}
791 ( <COMMA> variableDeclaration = VariableDeclarator()
792 {arrayList.add(variableDeclaration);
793 outlineInfo.addVariable(new String(variableDeclaration.name));
794 currentSegment.add(variableDeclaration);}
798 } catch (ParseException e) {
799 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
801 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
802 errorEnd = SimpleCharStream.getPosition() + 1;
803 processParseException(e);
806 {list = new VariableDeclaration[arrayList.size()];
807 arrayList.toArray(list);
808 return new FieldDeclaration(list,
810 SimpleCharStream.getPosition(),
814 VariableDeclaration VariableDeclarator() :
816 final String varName;
817 Expression initializer = null;
818 final int pos = SimpleCharStream.getPosition();
821 varName = VariableDeclaratorId()
825 initializer = VariableInitializer()
826 } catch (ParseException e) {
827 errorMessage = "Literal expression expected in variable initializer";
829 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
830 errorEnd = SimpleCharStream.getPosition() + 1;
835 if (initializer == null) {
836 return new VariableDeclaration(currentSegment,
837 varName.toCharArray(),
839 SimpleCharStream.getPosition());
841 return new VariableDeclaration(currentSegment,
842 varName.toCharArray(),
850 * @return the variable name (with suffix)
852 String VariableDeclaratorId() :
855 Expression expression;
856 final StringBuffer buff = new StringBuffer();
857 final int pos = SimpleCharStream.getPosition();
858 ConstantIdentifier ex;
862 expr = Variable() {buff.append(expr);}
864 {ex = new ConstantIdentifier(expr.toCharArray(),
866 SimpleCharStream.getPosition());}
867 expression = VariableSuffix(ex)
868 {buff.append(expression.toStringExpression());}
870 {return buff.toString();}
871 } catch (ParseException e) {
872 errorMessage = "'$' expected for variable identifier";
874 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
875 errorEnd = SimpleCharStream.getPosition() + 1;
882 final StringBuffer buff;
883 Expression expression = null;
888 token = <DOLLAR_ID> [<LBRACE> expression = Expression() <RBRACE>]
890 if (expression == null && !assigning) {
891 return token.image.substring(1);
893 buff = new StringBuffer(token.image);
895 buff.append(expression.toStringExpression());
897 return buff.toString();
900 <DOLLAR> expr = VariableName()
904 String VariableName():
906 final StringBuffer buff;
908 Expression expression = null;
912 <LBRACE> expression = Expression() <RBRACE>
913 {buff = new StringBuffer("{");
914 buff.append(expression.toStringExpression());
916 return buff.toString();}
918 token = <IDENTIFIER> [<LBRACE> expression = Expression() <RBRACE>]
920 if (expression == null) {
923 buff = new StringBuffer(token.image);
925 buff.append(expression.toStringExpression());
927 return buff.toString();
930 <DOLLAR> expr = VariableName()
932 buff = new StringBuffer("$");
934 return buff.toString();
937 token = <DOLLAR_ID> {return token.image;}
940 Expression VariableInitializer() :
942 final Expression expr;
944 final int pos = SimpleCharStream.getPosition();
950 <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
951 {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
953 SimpleCharStream.getPosition()),
957 <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
958 {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
960 SimpleCharStream.getPosition()),
964 expr = ArrayDeclarator()
968 {return new ConstantIdentifier(token.image.toCharArray(),pos,SimpleCharStream.getPosition());}
971 ArrayVariableDeclaration ArrayVariable() :
973 Expression expr,expr2;
977 [<ARRAYASSIGN> expr2 = Expression()
978 {return new ArrayVariableDeclaration(expr,expr2);}
980 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
983 ArrayVariableDeclaration[] ArrayInitializer() :
985 ArrayVariableDeclaration expr;
986 final ArrayList list = new ArrayList();
989 <LPAREN> [ expr = ArrayVariable()
991 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
995 [<COMMA> {list.add(null);}]
998 ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1004 * A Method Declaration.
1005 * <b>function</b> MetodDeclarator() Block()
1007 MethodDeclaration MethodDeclaration() :
1009 final MethodDeclaration functionDeclaration;
1015 functionDeclaration = MethodDeclarator()
1016 {outlineInfo.addVariable(new String(functionDeclaration.name));}
1017 } catch (ParseException e) {
1018 if (errorMessage != null) throw e;
1019 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1021 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1022 errorEnd = SimpleCharStream.getPosition() + 1;
1026 if (currentSegment != null) {
1027 currentSegment.add(functionDeclaration);
1028 currentSegment = functionDeclaration;
1033 functionDeclaration.statements = block.statements;
1034 if (currentSegment != null) {
1035 currentSegment = (OutlineableWithChildren) currentSegment.getParent();
1037 return functionDeclaration;
1042 * A MethodDeclarator.
1043 * [&] IDENTIFIER(parameters ...).
1044 * @return a function description for the outline
1046 MethodDeclaration MethodDeclarator() :
1048 final Token identifier;
1049 Token reference = null;
1050 final Hashtable formalParameters;
1051 final int pos = SimpleCharStream.getPosition();
1054 [reference = <BIT_AND>] identifier = <IDENTIFIER>
1055 formalParameters = FormalParameters()
1056 {return new MethodDeclaration(currentSegment,
1057 identifier.image.toCharArray(),
1061 SimpleCharStream.getPosition());}
1065 * FormalParameters follows method identifier.
1066 * (FormalParameter())
1068 Hashtable FormalParameters() :
1070 VariableDeclaration var;
1071 final Hashtable parameters = new Hashtable();
1076 } catch (ParseException e) {
1077 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1079 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1080 errorEnd = SimpleCharStream.getPosition() + 1;
1083 [ var = FormalParameter()
1084 {parameters.put(new String(var.name),var);}
1086 <COMMA> var = FormalParameter()
1087 {parameters.put(new String(var.name),var);}
1092 } catch (ParseException e) {
1093 errorMessage = "')' expected";
1095 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1096 errorEnd = SimpleCharStream.getPosition() + 1;
1099 {return parameters;}
1103 * A formal parameter.
1104 * $varname[=value] (,$varname[=value])
1106 VariableDeclaration FormalParameter() :
1108 final VariableDeclaration variableDeclaration;
1112 [token = <BIT_AND>] variableDeclaration = VariableDeclarator()
1114 if (token != null) {
1115 variableDeclaration.setReference(true);
1117 return variableDeclaration;}
1120 ConstantIdentifier Type() :
1123 <STRING> {pos = SimpleCharStream.getPosition();
1124 return new ConstantIdentifier(Types.STRING,pos,pos-6);}
1125 | <BOOL> {pos = SimpleCharStream.getPosition();
1126 return new ConstantIdentifier(Types.BOOL,pos,pos-4);}
1127 | <BOOLEAN> {pos = SimpleCharStream.getPosition();
1128 return new ConstantIdentifier(Types.BOOLEAN,pos,pos-7);}
1129 | <REAL> {pos = SimpleCharStream.getPosition();
1130 return new ConstantIdentifier(Types.REAL,pos,pos-4);}
1131 | <DOUBLE> {pos = SimpleCharStream.getPosition();
1132 return new ConstantIdentifier(Types.DOUBLE,pos,pos-5);}
1133 | <FLOAT> {pos = SimpleCharStream.getPosition();
1134 return new ConstantIdentifier(Types.FLOAT,pos,pos-5);}
1135 | <INT> {pos = SimpleCharStream.getPosition();
1136 return new ConstantIdentifier(Types.INT,pos,pos-3);}
1137 | <INTEGER> {pos = SimpleCharStream.getPosition();
1138 return new ConstantIdentifier(Types.INTEGER,pos,pos-7);}
1139 | <OBJECT> {pos = SimpleCharStream.getPosition();
1140 return new ConstantIdentifier(Types.OBJECT,pos,pos-6);}
1143 Expression Expression() :
1145 final Expression expr;
1148 expr = PrintExpression() {return expr;}
1149 | expr = ListExpression() {return expr;}
1150 | LOOKAHEAD(varAssignation())
1151 expr = varAssignation() {return expr;}
1152 | expr = ConditionalExpression() {return expr;}
1156 * A Variable assignation.
1157 * varName (an assign operator) any expression
1159 VarAssignation varAssignation() :
1162 final Expression expression;
1163 final int assignOperator;
1164 final int pos = SimpleCharStream.getPosition();
1167 varName = VariableDeclaratorId()
1168 assignOperator = AssignmentOperator()
1170 expression = Expression()
1171 } catch (ParseException e) {
1172 if (errorMessage != null) {
1175 errorMessage = "expression expected";
1177 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1178 errorEnd = SimpleCharStream.getPosition() + 1;
1181 {return new VarAssignation(varName.toCharArray(),
1185 SimpleCharStream.getPosition());}
1188 int AssignmentOperator() :
1191 <ASSIGN> {return VarAssignation.EQUAL;}
1192 | <STARASSIGN> {return VarAssignation.STAR_EQUAL;}
1193 | <SLASHASSIGN> {return VarAssignation.SLASH_EQUAL;}
1194 | <REMASSIGN> {return VarAssignation.REM_EQUAL;}
1195 | <PLUSASSIGN> {return VarAssignation.PLUS_EQUAL;}
1196 | <MINUSASSIGN> {return VarAssignation.MINUS_EQUAL;}
1197 | <LSHIFTASSIGN> {return VarAssignation.LSHIFT_EQUAL;}
1198 | <RSIGNEDSHIFTASSIGN> {return VarAssignation.RSIGNEDSHIFT_EQUAL;}
1199 | <ANDASSIGN> {return VarAssignation.AND_EQUAL;}
1200 | <XORASSIGN> {return VarAssignation.XOR_EQUAL;}
1201 | <ORASSIGN> {return VarAssignation.OR_EQUAL;}
1202 | <DOTASSIGN> {return VarAssignation.DOT_EQUAL;}
1203 | <TILDEEQUAL> {return VarAssignation.TILDE_EQUAL;}
1206 Expression ConditionalExpression() :
1208 final Expression expr;
1209 Expression expr2 = null;
1210 Expression expr3 = null;
1213 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1215 if (expr3 == null) {
1218 return new ConditionalExpression(expr,expr2,expr3);
1222 Expression ConditionalOrExpression() :
1224 Expression expr,expr2;
1228 expr = ConditionalAndExpression()
1231 <OR_OR> {operator = OperatorIds.OR_OR;}
1232 | <_ORL> {operator = OperatorIds.ORL;}
1233 ) expr2 = ConditionalAndExpression()
1235 expr = new BinaryExpression(expr,expr2,operator);
1241 Expression ConditionalAndExpression() :
1243 Expression expr,expr2;
1247 expr = ConcatExpression()
1249 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1250 | <_ANDL> {operator = OperatorIds.ANDL;})
1251 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1256 Expression ConcatExpression() :
1258 Expression expr,expr2;
1261 expr = InclusiveOrExpression()
1263 <DOT> expr2 = InclusiveOrExpression()
1264 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1269 Expression InclusiveOrExpression() :
1271 Expression expr,expr2;
1274 expr = ExclusiveOrExpression()
1275 (<BIT_OR> expr2 = ExclusiveOrExpression()
1276 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1281 Expression ExclusiveOrExpression() :
1283 Expression expr,expr2;
1286 expr = AndExpression()
1288 <XOR> expr2 = AndExpression()
1289 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1294 Expression AndExpression() :
1296 Expression expr,expr2;
1299 expr = EqualityExpression()
1301 <BIT_AND> expr2 = EqualityExpression()
1302 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1307 Expression EqualityExpression() :
1309 Expression expr,expr2;
1313 expr = RelationalExpression()
1315 ( <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1316 | <DIF> {operator = OperatorIds.DIF;}
1317 | <NOT_EQUAL> {operator = OperatorIds.DIF;}
1318 | <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1319 | <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1322 expr2 = RelationalExpression()
1323 } catch (ParseException e) {
1324 if (errorMessage != null) {
1327 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1329 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1330 errorEnd = SimpleCharStream.getPosition() + 1;
1334 expr = new BinaryExpression(expr,expr2,operator);
1340 Expression RelationalExpression() :
1342 Expression expr,expr2;
1346 expr = ShiftExpression()
1348 ( <LT> {operator = OperatorIds.LESS;}
1349 | <GT> {operator = OperatorIds.GREATER;}
1350 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1351 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1352 expr2 = ShiftExpression()
1353 {expr = new BinaryExpression(expr,expr2,operator);}
1358 Expression ShiftExpression() :
1360 Expression expr,expr2;
1364 expr = AdditiveExpression()
1366 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1367 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1368 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1369 expr2 = AdditiveExpression()
1370 {expr = new BinaryExpression(expr,expr2,operator);}
1375 Expression AdditiveExpression() :
1377 Expression expr,expr2;
1381 expr = MultiplicativeExpression()
1383 ( <PLUS> {operator = OperatorIds.PLUS;}
1384 | <MINUS> {operator = OperatorIds.MINUS;} )
1385 expr2 = MultiplicativeExpression()
1386 {expr = new BinaryExpression(expr,expr2,operator);}
1391 Expression MultiplicativeExpression() :
1393 Expression expr,expr2;
1398 expr = UnaryExpression()
1399 } catch (ParseException e) {
1400 if (errorMessage != null) throw e;
1401 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1403 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1404 errorEnd = SimpleCharStream.getPosition() + 1;
1408 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1409 | <SLASH> {operator = OperatorIds.DIVIDE;}
1410 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1411 expr2 = UnaryExpression()
1412 {expr = new BinaryExpression(expr,expr2,operator);}
1418 * An unary expression starting with @, & or nothing
1420 Expression UnaryExpression() :
1423 final int pos = SimpleCharStream.getPosition();
1426 <BIT_AND> expr = UnaryExpressionNoPrefix()
1427 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1429 expr = AtUnaryExpression() {return expr;}
1432 Expression AtUnaryExpression() :
1435 final int pos = SimpleCharStream.getPosition();
1439 expr = AtUnaryExpression()
1440 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,pos);}
1442 expr = UnaryExpressionNoPrefix()
1447 Expression UnaryExpressionNoPrefix() :
1451 final int pos = SimpleCharStream.getPosition();
1454 ( <PLUS> {operator = OperatorIds.PLUS;}
1455 | <MINUS> {operator = OperatorIds.MINUS;})
1456 expr = UnaryExpression()
1457 {return new PrefixedUnaryExpression(expr,operator,pos);}
1459 expr = PreIncDecExpression()
1462 expr = UnaryExpressionNotPlusMinus()
1467 Expression PreIncDecExpression() :
1469 final Expression expr;
1471 final int pos = SimpleCharStream.getPosition();
1474 ( <INCR> {operator = OperatorIds.PLUS_PLUS;}
1475 | <DECR> {operator = OperatorIds.MINUS_MINUS;})
1476 expr = PrimaryExpression()
1477 {return new PrefixedUnaryExpression(expr,operator,pos);}
1480 Expression UnaryExpressionNotPlusMinus() :
1483 final int pos = SimpleCharStream.getPosition();
1486 <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
1487 | LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1488 expr = CastExpression() {return expr;}
1489 | expr = PostfixExpression() {return expr;}
1490 | expr = Literal() {return expr;}
1491 | <LPAREN> expr = Expression()
1494 } catch (ParseException e) {
1495 errorMessage = "')' expected";
1497 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1498 errorEnd = SimpleCharStream.getPosition() + 1;
1504 CastExpression CastExpression() :
1506 final ConstantIdentifier type;
1507 final Expression expr;
1508 final int pos = SimpleCharStream.getPosition();
1513 | <ARRAY> {type = new ConstantIdentifier(Types.ARRAY,pos,SimpleCharStream.getPosition());})
1514 <RPAREN> expr = UnaryExpression()
1515 {return new CastExpression(type,expr,pos,SimpleCharStream.getPosition());}
1518 Expression PostfixExpression() :
1522 final int pos = SimpleCharStream.getPosition();
1525 expr = PrimaryExpression()
1526 [ <INCR> {operator = OperatorIds.PLUS_PLUS;}
1527 | <DECR> {operator = OperatorIds.MINUS_MINUS;}]
1529 if (operator == -1) {
1532 return new PostfixedUnaryExpression(expr,operator,pos);
1536 Expression PrimaryExpression() :
1538 final Token identifier;
1540 final int pos = SimpleCharStream.getPosition();
1544 identifier = <IDENTIFIER> <STATICCLASSACCESS> expr = ClassIdentifier()
1545 {expr = new ClassAccess(new ConstantIdentifier(identifier.image.toCharArray(),
1547 SimpleCharStream.getPosition()),
1549 ClassAccess.STATIC);}
1550 (expr = PrimarySuffix(expr))*
1553 expr = PrimaryPrefix()
1554 (expr = PrimarySuffix(expr))*
1557 expr = ArrayDeclarator()
1561 ArrayInitializer ArrayDeclarator() :
1563 final ArrayVariableDeclaration[] vars;
1564 final int pos = SimpleCharStream.getPosition();
1567 <ARRAY> vars = ArrayInitializer()
1568 {return new ArrayInitializer(vars,pos,SimpleCharStream.getPosition());}
1571 Expression PrimaryPrefix() :
1573 final Expression expr;
1576 final int pos = SimpleCharStream.getPosition();
1579 token = <IDENTIFIER> {return new ConstantIdentifier(token.image.toCharArray(),
1581 SimpleCharStream.getPosition());}
1582 | <NEW> expr = ClassIdentifier() {return new PrefixedUnaryExpression(expr,
1585 | var = VariableDeclaratorId() {return new ConstantIdentifier(var.toCharArray(),
1587 SimpleCharStream.getPosition());}
1590 PrefixedUnaryExpression classInstantiation() :
1593 final StringBuffer buff;
1594 final int pos = SimpleCharStream.getPosition();
1597 <NEW> expr = ClassIdentifier()
1599 {buff = new StringBuffer(expr.toStringExpression());}
1600 expr = PrimaryExpression()
1601 {buff.append(expr.toStringExpression());
1602 expr = new ConstantIdentifier(buff.toString().toCharArray(),
1604 SimpleCharStream.getPosition());}
1606 {return new PrefixedUnaryExpression(expr,
1611 ConstantIdentifier ClassIdentifier():
1615 final int pos = SimpleCharStream.getPosition();
1618 token = <IDENTIFIER> {return new ConstantIdentifier(token.image.toCharArray(),
1620 SimpleCharStream.getPosition());}
1621 | expr = VariableDeclaratorId() {return new ConstantIdentifier(expr.toCharArray(),
1623 SimpleCharStream.getPosition());}
1626 AbstractSuffixExpression PrimarySuffix(Expression prefix) :
1628 final AbstractSuffixExpression expr;
1631 expr = Arguments(prefix) {return expr;}
1632 | expr = VariableSuffix(prefix) {return expr;}
1635 AbstractSuffixExpression VariableSuffix(Expression prefix) :
1638 final int pos = SimpleCharStream.getPosition();
1639 Expression expression = null;
1644 expr = VariableName()
1645 } catch (ParseException e) {
1646 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1648 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1649 errorEnd = SimpleCharStream.getPosition() + 1;
1652 {return new ClassAccess(prefix,
1653 new ConstantIdentifier(expr.toCharArray(),pos,SimpleCharStream.getPosition()),
1654 ClassAccess.NORMAL);}
1656 <LBRACKET> [ expression = Expression() | expression = Type() ] //Not good
1659 } catch (ParseException e) {
1660 errorMessage = "']' expected";
1662 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1663 errorEnd = SimpleCharStream.getPosition() + 1;
1666 {return new ArrayDeclarator(prefix,expression,SimpleCharStream.getPosition());}
1675 token = <INTEGER_LITERAL> {pos = SimpleCharStream.getPosition();
1676 return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
1677 | token = <FLOATING_POINT_LITERAL> {pos = SimpleCharStream.getPosition();
1678 return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
1679 | token = <STRING_LITERAL> {pos = SimpleCharStream.getPosition();
1680 return new StringLiteral(token.image.toCharArray(),pos-token.image.length());}
1681 | <TRUE> {pos = SimpleCharStream.getPosition();
1682 return new TrueLiteral(pos-4,pos);}
1683 | <FALSE> {pos = SimpleCharStream.getPosition();
1684 return new FalseLiteral(pos-4,pos);}
1685 | <NULL> {pos = SimpleCharStream.getPosition();
1686 return new NullLiteral(pos-4,pos);}
1689 FunctionCall Arguments(Expression func) :
1691 Expression[] args = null;
1694 <LPAREN> [ args = ArgumentList() ]
1697 } catch (ParseException e) {
1698 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
1700 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1701 errorEnd = SimpleCharStream.getPosition() + 1;
1704 {return new FunctionCall(func,args,SimpleCharStream.getPosition());}
1708 * An argument list is a list of arguments separated by comma :
1709 * argumentDeclaration() (, argumentDeclaration)*
1710 * @return an array of arguments
1712 Expression[] ArgumentList() :
1715 final ArrayList list = new ArrayList();
1724 } catch (ParseException e) {
1725 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
1727 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1728 errorEnd = SimpleCharStream.getPosition() + 1;
1733 Expression[] arguments = new Expression[list.size()];
1734 list.toArray(arguments);
1739 * A Statement without break.
1741 Statement StatementNoBreak() :
1743 final Statement statement;
1748 statement = Expression()
1751 } catch (ParseException e) {
1752 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
1753 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1755 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1756 errorEnd = SimpleCharStream.getPosition() + 1;
1762 statement = LabeledStatement() {return statement;}
1763 | statement = Block() {return statement;}
1764 | statement = EmptyStatement() {return statement;}
1765 | statement = StatementExpression()
1768 } catch (ParseException e) {
1769 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1771 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1772 errorEnd = SimpleCharStream.getPosition() + 1;
1776 | statement = SwitchStatement() {return statement;}
1777 | statement = IfStatement() {return statement;}
1778 | statement = WhileStatement() {return statement;}
1779 | statement = DoStatement() {return statement;}
1780 | statement = ForStatement() {return statement;}
1781 | statement = ForeachStatement() {return statement;}
1782 | statement = ContinueStatement() {return statement;}
1783 | statement = ReturnStatement() {return statement;}
1784 | statement = EchoStatement() {return statement;}
1785 | [token=<AT>] statement = IncludeStatement()
1786 {if (token != null) {
1787 ((InclusionStatement)statement).silent = true;
1790 | statement = StaticStatement() {return statement;}
1791 | statement = GlobalStatement() {return statement;}
1795 * A Normal statement.
1797 Statement Statement() :
1799 final Statement statement;
1802 statement = StatementNoBreak() {return statement;}
1803 | statement = BreakStatement() {return statement;}
1807 * An html block inside a php syntax.
1809 HTMLBlock htmlBlock() :
1811 final int startIndex = nodePtr;
1812 AstNode[] blockNodes;
1816 <PHPEND> (phpEchoBlock())*
1818 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
1819 } catch (ParseException e) {
1820 errorMessage = "End of file unexpected, '<?php' expected";
1822 errorStart = SimpleCharStream.getPosition();
1823 errorEnd = SimpleCharStream.getPosition();
1827 nbNodes = nodePtr - startIndex;
1828 blockNodes = new AstNode[nbNodes];
1829 System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes);
1830 nodePtr = startIndex;
1831 return new HTMLBlock(blockNodes);}
1835 * An include statement. It's "include" an expression;
1837 InclusionStatement IncludeStatement() :
1839 final Expression expr;
1841 final int pos = SimpleCharStream.getPosition();
1842 final InclusionStatement inclusionStatement;
1845 ( <REQUIRE> {keyword = InclusionStatement.REQUIRE;}
1846 | <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;}
1847 | <INCLUDE> {keyword = InclusionStatement.INCLUDE;}
1848 | <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;})
1851 } catch (ParseException e) {
1852 if (errorMessage != null) {
1855 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
1857 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1858 errorEnd = SimpleCharStream.getPosition() + 1;
1861 {inclusionStatement = new InclusionStatement(currentSegment,
1865 currentSegment.add(inclusionStatement);
1869 } catch (ParseException e) {
1870 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1872 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1873 errorEnd = SimpleCharStream.getPosition() + 1;
1876 {return inclusionStatement;}
1879 PrintExpression PrintExpression() :
1881 final Expression expr;
1882 final int pos = SimpleCharStream.getPosition();
1885 <PRINT> expr = Expression() {return new PrintExpression(expr,pos,SimpleCharStream.getPosition());}
1888 ListExpression ListExpression() :
1891 Expression expression = null;
1892 ArrayList list = new ArrayList();
1893 final int pos = SimpleCharStream.getPosition();
1899 } catch (ParseException e) {
1900 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
1902 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1903 errorEnd = SimpleCharStream.getPosition() + 1;
1907 expr = VariableDeclaratorId()
1910 {if (expr == null) list.add(null);}
1914 } catch (ParseException e) {
1915 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
1917 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1918 errorEnd = SimpleCharStream.getPosition() + 1;
1921 expr = VariableDeclaratorId()
1926 } catch (ParseException e) {
1927 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
1929 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1930 errorEnd = SimpleCharStream.getPosition() + 1;
1933 [ <ASSIGN> expression = Expression()
1935 String[] strings = new String[list.size()];
1936 list.toArray(strings);
1937 return new ListExpression(strings,
1940 SimpleCharStream.getPosition());}
1943 String[] strings = new String[list.size()];
1944 list.toArray(strings);
1945 return new ListExpression(strings,pos,SimpleCharStream.getPosition());}
1949 * An echo statement.
1950 * echo anyexpression (, otherexpression)*
1952 EchoStatement EchoStatement() :
1954 final ArrayList expressions = new ArrayList();
1956 final int pos = SimpleCharStream.getPosition();
1959 <ECHO> expr = Expression()
1960 {expressions.add(expr);}
1962 <COMMA> expr = Expression()
1963 {expressions.add(expr);}
1967 } catch (ParseException e) {
1968 if (e.currentToken.next.kind != 4) {
1969 errorMessage = "';' expected after 'echo' statement";
1971 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1972 errorEnd = SimpleCharStream.getPosition() + 1;
1976 {Expression[] exprs = new Expression[expressions.size()];
1977 expressions.toArray(exprs);
1978 return new EchoStatement(exprs,pos);}
1981 GlobalStatement GlobalStatement() :
1983 final int pos = SimpleCharStream.getPosition();
1985 ArrayList vars = new ArrayList();
1986 GlobalStatement global;
1990 expr = VariableDeclaratorId()
1993 expr = VariableDeclaratorId()
1999 String[] strings = new String[vars.size()];
2000 vars.toArray(strings);
2001 global = new GlobalStatement(currentSegment,
2004 SimpleCharStream.getPosition());
2005 currentSegment.add(global);
2007 } catch (ParseException e) {
2008 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2010 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2011 errorEnd = SimpleCharStream.getPosition() + 1;
2016 StaticStatement StaticStatement() :
2018 final int pos = SimpleCharStream.getPosition();
2019 final ArrayList vars = new ArrayList();
2020 VariableDeclaration expr;
2023 <STATIC> expr = VariableDeclarator() {vars.add(new String(expr.name));}
2024 (<COMMA> expr = VariableDeclarator() {vars.add(new String(expr.name));})*
2028 String[] strings = new String[vars.size()];
2029 vars.toArray(strings);
2030 return new StaticStatement(strings,
2032 SimpleCharStream.getPosition());}
2033 } catch (ParseException e) {
2034 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2036 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2037 errorEnd = SimpleCharStream.getPosition() + 1;
2042 LabeledStatement LabeledStatement() :
2044 final int pos = SimpleCharStream.getPosition();
2046 final Statement statement;
2049 label = <IDENTIFIER> <COLON> statement = Statement()
2050 {return new LabeledStatement(label.image.toCharArray(),statement,pos,SimpleCharStream.getPosition());}
2062 final int pos = SimpleCharStream.getPosition();
2063 final ArrayList list = new ArrayList();
2064 Statement statement;
2069 } catch (ParseException e) {
2070 errorMessage = "'{' expected";
2072 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2073 errorEnd = SimpleCharStream.getPosition() + 1;
2076 ( statement = BlockStatement() {list.add(statement);}
2077 | statement = htmlBlock() {list.add(statement);})*
2080 } catch (ParseException e) {
2081 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2083 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2084 errorEnd = SimpleCharStream.getPosition() + 1;
2088 Statement[] statements = new Statement[list.size()];
2089 list.toArray(statements);
2090 return new Block(statements,pos,SimpleCharStream.getPosition());}
2093 Statement BlockStatement() :
2095 final Statement statement;
2099 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2101 } catch (ParseException e) {
2102 errorMessage = "statement expected";
2104 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2105 errorEnd = SimpleCharStream.getPosition() + 1;
2108 | statement = ClassDeclaration() {return statement;}
2109 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2114 * A Block statement that will not contain any 'break'
2116 Statement BlockStatementNoBreak() :
2118 final Statement statement;
2121 statement = StatementNoBreak() {return statement;}
2122 | statement = ClassDeclaration() {return statement;}
2123 | statement = MethodDeclaration() {return statement;}
2126 VariableDeclaration[] LocalVariableDeclaration() :
2128 final ArrayList list = new ArrayList();
2129 VariableDeclaration var;
2132 var = LocalVariableDeclarator()
2134 ( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
2136 VariableDeclaration[] vars = new VariableDeclaration[list.size()];
2141 VariableDeclaration LocalVariableDeclarator() :
2143 final String varName;
2144 Expression initializer = null;
2145 final int pos = SimpleCharStream.getPosition();
2148 varName = VariableDeclaratorId() [ <ASSIGN> initializer = Expression() ]
2150 if (initializer == null) {
2151 return new VariableDeclaration(currentSegment,
2152 varName.toCharArray(),
2154 SimpleCharStream.getPosition());
2156 return new VariableDeclaration(currentSegment,
2157 varName.toCharArray(),
2163 EmptyStatement EmptyStatement() :
2169 {pos = SimpleCharStream.getPosition();
2170 return new EmptyStatement(pos-1,pos);}
2173 Statement StatementExpression() :
2175 Expression expr,expr2;
2179 expr = PreIncDecExpression() {return expr;}
2181 expr = PrimaryExpression()
2182 [ <INCR> {return new PostfixedUnaryExpression(expr,
2183 OperatorIds.PLUS_PLUS,
2184 SimpleCharStream.getPosition());}
2185 | <DECR> {return new PostfixedUnaryExpression(expr,
2186 OperatorIds.MINUS_MINUS,
2187 SimpleCharStream.getPosition());}
2188 | operator = AssignmentOperator() expr2 = Expression()
2189 {return new BinaryExpression(expr,expr2,operator);}
2194 SwitchStatement SwitchStatement() :
2196 final Expression variable;
2197 final AbstractCase[] cases;
2198 final int pos = SimpleCharStream.getPosition();
2204 } catch (ParseException e) {
2205 errorMessage = "'(' expected after 'switch'";
2207 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2208 errorEnd = SimpleCharStream.getPosition() + 1;
2212 variable = Expression()
2213 } catch (ParseException e) {
2214 if (errorMessage != null) {
2217 errorMessage = "expression expected";
2219 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2220 errorEnd = SimpleCharStream.getPosition() + 1;
2225 } catch (ParseException e) {
2226 errorMessage = "')' expected";
2228 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2229 errorEnd = SimpleCharStream.getPosition() + 1;
2232 (cases = switchStatementBrace() | cases = switchStatementColon(pos, pos + 6))
2233 {return new SwitchStatement(variable,cases,pos,SimpleCharStream.getPosition());}
2236 AbstractCase[] switchStatementBrace() :
2239 final ArrayList cases = new ArrayList();
2243 ( cas = switchLabel0() {cases.add(cas);})*
2247 AbstractCase[] abcase = new AbstractCase[cases.size()];
2248 cases.toArray(abcase);
2250 } catch (ParseException e) {
2251 errorMessage = "'}' expected";
2253 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2254 errorEnd = SimpleCharStream.getPosition() + 1;
2259 * A Switch statement with : ... endswitch;
2260 * @param start the begin offset of the switch
2261 * @param end the end offset of the switch
2263 AbstractCase[] switchStatementColon(final int start, final int end) :
2266 final ArrayList cases = new ArrayList();
2271 setMarker(fileToParse,
2272 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2276 "Line " + token.beginLine);
2277 } catch (CoreException e) {
2278 PHPeclipsePlugin.log(e);
2280 ( cas = switchLabel0() {cases.add(cas);})*
2283 } catch (ParseException e) {
2284 errorMessage = "'endswitch' expected";
2286 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2287 errorEnd = SimpleCharStream.getPosition() + 1;
2293 AbstractCase[] abcase = new AbstractCase[cases.size()];
2294 cases.toArray(abcase);
2296 } catch (ParseException e) {
2297 errorMessage = "';' expected after 'endswitch' keyword";
2299 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2300 errorEnd = SimpleCharStream.getPosition() + 1;
2305 AbstractCase switchLabel0() :
2307 final Expression expr;
2308 Statement statement;
2309 final ArrayList stmts = new ArrayList();
2310 final int pos = SimpleCharStream.getPosition();
2313 expr = SwitchLabel()
2314 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2315 | statement = htmlBlock() {stmts.add(statement);})*
2316 [ statement = BreakStatement() {stmts.add(statement);}]
2318 Statement[] stmtsArray = new Statement[stmts.size()];
2319 stmts.toArray(stmtsArray);
2320 if (expr == null) {//it's a default
2321 return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition());
2323 return new Case(expr,stmtsArray,pos,SimpleCharStream.getPosition());}
2328 * case Expression() :
2330 * @return the if it was a case and null if not
2332 Expression SwitchLabel() :
2334 final Expression expr;
2340 } catch (ParseException e) {
2341 if (errorMessage != null) throw e;
2342 errorMessage = "expression expected after 'case' keyword";
2344 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2345 errorEnd = SimpleCharStream.getPosition() + 1;
2351 } catch (ParseException e) {
2352 errorMessage = "':' expected after case expression";
2354 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2355 errorEnd = SimpleCharStream.getPosition() + 1;
2363 } catch (ParseException e) {
2364 errorMessage = "':' expected after 'default' keyword";
2366 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2367 errorEnd = SimpleCharStream.getPosition() + 1;
2372 Break BreakStatement() :
2374 Expression expression = null;
2375 final int start = SimpleCharStream.getPosition();
2378 <BREAK> [ expression = Expression() ]
2381 } catch (ParseException e) {
2382 errorMessage = "';' expected after 'break' keyword";
2384 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2385 errorEnd = SimpleCharStream.getPosition() + 1;
2388 {return new Break(expression, start, SimpleCharStream.getPosition());}
2391 IfStatement IfStatement() :
2393 final int pos = SimpleCharStream.getPosition();
2394 Expression condition;
2395 IfStatement ifStatement;
2398 <IF> condition = Condition("if") ifStatement = IfStatement0(condition, pos,pos+2)
2399 {return ifStatement;}
2403 Expression Condition(final String keyword) :
2405 final Expression condition;
2410 } catch (ParseException e) {
2411 errorMessage = "'(' expected after " + keyword + " keyword";
2413 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length();
2414 errorEnd = errorStart +1;
2415 processParseException(e);
2417 condition = Expression()
2421 } catch (ParseException e) {
2422 errorMessage = "')' expected after " + keyword + " keyword";
2424 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2425 errorEnd = SimpleCharStream.getPosition() + 1;
2430 IfStatement IfStatement0(Expression condition, final int start,final int end) :
2432 Statement statement;
2434 final Statement[] statementsArray;
2435 ElseIf elseifStatement;
2436 Else elseStatement = null;
2438 final ArrayList elseIfList = new ArrayList();
2440 int pos = SimpleCharStream.getPosition();
2445 {stmts = new ArrayList();}
2446 ( statement = Statement() {stmts.add(statement);}
2447 | statement = htmlBlock() {stmts.add(statement);})*
2448 {endStatements = SimpleCharStream.getPosition();}
2449 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2450 [elseStatement = ElseStatementColon()]
2453 setMarker(fileToParse,
2454 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2458 "Line " + token.beginLine);
2459 } catch (CoreException e) {
2460 PHPeclipsePlugin.log(e);
2464 } catch (ParseException e) {
2465 errorMessage = "'endif' expected";
2467 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2468 errorEnd = SimpleCharStream.getPosition() + 1;
2473 } catch (ParseException e) {
2474 errorMessage = "';' expected after 'endif' keyword";
2476 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2477 errorEnd = SimpleCharStream.getPosition() + 1;
2481 elseIfs = new ElseIf[elseIfList.size()];
2482 elseIfList.toArray(elseIfs);
2483 if (stmts.size() == 1) {
2484 return new IfStatement(condition,
2485 (Statement) stmts.get(0),
2489 SimpleCharStream.getPosition());
2491 statementsArray = new Statement[stmts.size()];
2492 stmts.toArray(statementsArray);
2493 return new IfStatement(condition,
2494 new Block(statementsArray,pos,endStatements),
2498 SimpleCharStream.getPosition());
2503 (stmt = Statement() | stmt = htmlBlock())
2504 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
2508 {pos = SimpleCharStream.getPosition();}
2509 statement = Statement()
2510 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
2511 } catch (ParseException e) {
2512 if (errorMessage != null) {
2515 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
2517 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2518 errorEnd = SimpleCharStream.getPosition() + 1;
2523 elseIfs = new ElseIf[elseIfList.size()];
2524 elseIfList.toArray(elseIfs);
2525 return new IfStatement(condition,
2530 SimpleCharStream.getPosition());}
2533 ElseIf ElseIfStatementColon() :
2535 Expression condition;
2536 Statement statement;
2537 final ArrayList list = new ArrayList();
2538 final int pos = SimpleCharStream.getPosition();
2541 <ELSEIF> condition = Condition("elseif")
2542 <COLON> ( statement = Statement() {list.add(statement);}
2543 | statement = htmlBlock() {list.add(statement);})*
2545 Statement[] stmtsArray = new Statement[list.size()];
2546 list.toArray(stmtsArray);
2547 return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());}
2550 Else ElseStatementColon() :
2552 Statement statement;
2553 final ArrayList list = new ArrayList();
2554 final int pos = SimpleCharStream.getPosition();
2557 <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
2558 | statement = htmlBlock() {list.add(statement);})*
2560 Statement[] stmtsArray = new Statement[list.size()];
2561 list.toArray(stmtsArray);
2562 return new Else(stmtsArray,pos,SimpleCharStream.getPosition());}
2565 ElseIf ElseIfStatement() :
2567 Expression condition;
2568 Statement statement;
2569 final ArrayList list = new ArrayList();
2570 final int pos = SimpleCharStream.getPosition();
2573 <ELSEIF> condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/}
2575 Statement[] stmtsArray = new Statement[list.size()];
2576 list.toArray(stmtsArray);
2577 return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());}
2580 WhileStatement WhileStatement() :
2582 final Expression condition;
2583 final Statement action;
2584 final int pos = SimpleCharStream.getPosition();
2588 condition = Condition("while")
2589 action = WhileStatement0(pos,pos + 5)
2590 {return new WhileStatement(condition,action,pos,SimpleCharStream.getPosition());}
2593 Statement WhileStatement0(final int start, final int end) :
2595 Statement statement;
2596 final ArrayList stmts = new ArrayList();
2597 final int pos = SimpleCharStream.getPosition();
2600 <COLON> (statement = Statement() {stmts.add(statement);})*
2602 setMarker(fileToParse,
2603 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
2607 "Line " + token.beginLine);
2608 } catch (CoreException e) {
2609 PHPeclipsePlugin.log(e);
2613 } catch (ParseException e) {
2614 errorMessage = "'endwhile' expected";
2616 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2617 errorEnd = SimpleCharStream.getPosition() + 1;
2623 Statement[] stmtsArray = new Statement[stmts.size()];
2624 stmts.toArray(stmtsArray);
2625 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
2626 } catch (ParseException e) {
2627 errorMessage = "';' expected after 'endwhile' keyword";
2629 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2630 errorEnd = SimpleCharStream.getPosition() + 1;
2634 statement = Statement()
2638 DoStatement DoStatement() :
2640 final Statement action;
2641 final Expression condition;
2642 final int pos = SimpleCharStream.getPosition();
2645 <DO> action = Statement() <WHILE> condition = Condition("while")
2648 {return new DoStatement(condition,action,pos,SimpleCharStream.getPosition());}
2649 } catch (ParseException e) {
2650 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2652 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2653 errorEnd = SimpleCharStream.getPosition() + 1;
2658 ForeachStatement ForeachStatement() :
2660 Statement statement;
2661 Expression expression;
2662 final int pos = SimpleCharStream.getPosition();
2663 ArrayVariableDeclaration variable;
2669 } catch (ParseException e) {
2670 errorMessage = "'(' expected after 'foreach' keyword";
2672 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2673 errorEnd = SimpleCharStream.getPosition() + 1;
2677 expression = Expression()
2678 } catch (ParseException e) {
2679 errorMessage = "variable expected";
2681 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2682 errorEnd = SimpleCharStream.getPosition() + 1;
2687 } catch (ParseException e) {
2688 errorMessage = "'as' expected";
2690 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2691 errorEnd = SimpleCharStream.getPosition() + 1;
2695 variable = ArrayVariable()
2696 } catch (ParseException e) {
2697 errorMessage = "variable expected";
2699 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2700 errorEnd = SimpleCharStream.getPosition() + 1;
2705 } catch (ParseException e) {
2706 errorMessage = "')' expected after 'foreach' keyword";
2708 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2709 errorEnd = SimpleCharStream.getPosition() + 1;
2713 statement = Statement()
2714 } catch (ParseException e) {
2715 if (errorMessage != null) throw e;
2716 errorMessage = "statement expected";
2718 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2719 errorEnd = SimpleCharStream.getPosition() + 1;
2722 {return new ForeachStatement(expression,
2726 SimpleCharStream.getPosition());}
2730 ForStatement ForStatement() :
2733 final int pos = SimpleCharStream.getPosition();
2734 Statement[] initializations = null;
2735 Expression condition = null;
2736 Statement[] increments = null;
2738 final ArrayList list = new ArrayList();
2739 final int startBlock, endBlock;
2745 } catch (ParseException e) {
2746 errorMessage = "'(' expected after 'for' keyword";
2748 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2749 errorEnd = SimpleCharStream.getPosition() + 1;
2752 [ initializations = ForInit() ] <SEMICOLON>
2753 [ condition = Expression() ] <SEMICOLON>
2754 [ increments = StatementExpressionList() ] <RPAREN>
2756 action = Statement()
2757 {return new ForStatement(initializations,condition,increments,action,pos,SimpleCharStream.getPosition());}
2760 {startBlock = SimpleCharStream.getPosition();}
2761 (action = Statement() {list.add(action);})*
2764 setMarker(fileToParse,
2765 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
2767 pos+token.image.length(),
2769 "Line " + token.beginLine);
2770 } catch (CoreException e) {
2771 PHPeclipsePlugin.log(e);
2774 {endBlock = SimpleCharStream.getPosition();}
2777 } catch (ParseException e) {
2778 errorMessage = "'endfor' expected";
2780 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2781 errorEnd = SimpleCharStream.getPosition() + 1;
2787 Statement[] stmtsArray = new Statement[list.size()];
2788 list.toArray(stmtsArray);
2789 return new ForStatement(initializations,condition,increments,new Block(stmtsArray,startBlock,endBlock),pos,SimpleCharStream.getPosition());}
2790 } catch (ParseException e) {
2791 errorMessage = "';' expected after 'endfor' keyword";
2793 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2794 errorEnd = SimpleCharStream.getPosition() + 1;
2800 Statement[] ForInit() :
2802 Statement[] statements;
2805 LOOKAHEAD(LocalVariableDeclaration())
2806 statements = LocalVariableDeclaration()
2807 {return statements;}
2809 statements = StatementExpressionList()
2810 {return statements;}
2813 Statement[] StatementExpressionList() :
2815 final ArrayList list = new ArrayList();
2819 expr = StatementExpression() {list.add(expr);}
2820 (<COMMA> StatementExpression() {list.add(expr);})*
2822 Statement[] stmtsArray = new Statement[list.size()];
2823 list.toArray(stmtsArray);
2827 Continue ContinueStatement() :
2829 Expression expr = null;
2830 final int pos = SimpleCharStream.getPosition();
2833 <CONTINUE> [ expr = Expression() ]
2836 {return new Continue(expr,pos,SimpleCharStream.getPosition());}
2837 } catch (ParseException e) {
2838 errorMessage = "';' expected after 'continue' statement";
2840 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2841 errorEnd = SimpleCharStream.getPosition() + 1;
2846 ReturnStatement ReturnStatement() :
2848 Expression expr = null;
2849 final int pos = SimpleCharStream.getPosition();
2852 <RETURN> [ expr = Expression() ]
2855 {return new ReturnStatement(expr,pos,SimpleCharStream.getPosition());}
2856 } catch (ParseException e) {
2857 errorMessage = "';' expected after 'return' statement";
2859 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2860 errorEnd = SimpleCharStream.getPosition() + 1;