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 currentSegment = new PHPDocument(parent);
126 outlineInfo = new PHPOutlineInfo(parent, currentSegment);
127 final StringReader stream = new StringReader(s);
128 if (jj_input_stream == null) {
129 jj_input_stream = new SimpleCharStream(stream, 1, 1);
135 phpDocument = new PHPDocument(null);
136 phpDocument.nodes = new AstNode[nodes.length];
137 System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
138 } catch (ParseException e) {
139 processParseException(e);
145 * This method will process the parse exception.
146 * If the error message is null, the parse exception wasn't catched and a trace is written in the log
147 * @param e the ParseException
149 private static void processParseException(final ParseException e) {
150 if (errorMessage == null) {
151 PHPeclipsePlugin.log(e);
152 errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
153 errorStart = SimpleCharStream.getPosition();
154 errorEnd = errorStart + 1;
161 * Create marker for the parse error
162 * @param e the ParseException
164 private static void setMarker(final ParseException e) {
166 if (errorStart == -1) {
167 setMarker(fileToParse,
169 SimpleCharStream.tokenBegin,
170 SimpleCharStream.tokenBegin + e.currentToken.image.length(),
172 "Line " + e.currentToken.beginLine);
174 setMarker(fileToParse,
179 "Line " + e.currentToken.beginLine);
183 } catch (CoreException e2) {
184 PHPeclipsePlugin.log(e2);
189 * Create markers according to the external parser output
191 private static void createMarkers(final String output, final IFile file) throws CoreException {
192 // delete all markers
193 file.deleteMarkers(IMarker.PROBLEM, false, 0);
198 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
199 // newer php error output (tested with 4.2.3)
200 scanLine(output, file, indx, brIndx);
205 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
206 // older php error output (tested with 4.2.3)
207 scanLine(output, file, indx, brIndx);
213 private static void scanLine(final String output,
216 final int brIndx) throws CoreException {
218 StringBuffer lineNumberBuffer = new StringBuffer(10);
220 current = output.substring(indx, brIndx);
222 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
223 int onLine = current.indexOf("on line <b>");
225 lineNumberBuffer.delete(0, lineNumberBuffer.length());
226 for (int i = onLine; i < current.length(); i++) {
227 ch = current.charAt(i);
228 if ('0' <= ch && '9' >= ch) {
229 lineNumberBuffer.append(ch);
233 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
235 Hashtable attributes = new Hashtable();
237 current = current.replaceAll("\n", "");
238 current = current.replaceAll("<b>", "");
239 current = current.replaceAll("</b>", "");
240 MarkerUtilities.setMessage(attributes, current);
242 if (current.indexOf(PARSE_ERROR_STRING) != -1)
243 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
244 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
245 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
247 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
248 MarkerUtilities.setLineNumber(attributes, lineNumber);
249 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
254 public final void parse(final String s) throws CoreException {
255 final StringReader stream = new StringReader(s);
256 if (jj_input_stream == null) {
257 jj_input_stream = new SimpleCharStream(stream, 1, 1);
263 } catch (ParseException e) {
264 processParseException(e);
269 * Call the php parse command ( php -l -f <filename> )
270 * and create markers according to the external parser output
272 public static void phpExternalParse(final IFile file) {
273 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
274 final String filename = file.getLocation().toString();
276 final String[] arguments = { filename };
277 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
278 final String command = form.format(arguments);
280 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
283 // parse the buffer to find the errors and warnings
284 createMarkers(parserResult, file);
285 } catch (CoreException e) {
286 PHPeclipsePlugin.log(e);
291 * Put a new html block in the stack.
293 public static final void createNewHTMLCode() {
294 final int currentPosition = SimpleCharStream.getPosition();
295 if (currentPosition == htmlStart) {
298 final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
299 pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
302 private static final void parse() throws ParseException {
307 PARSER_END(PHPParser)
311 <PHPSTARTSHORT : "<?"> {PHPParser.createNewHTMLCode();} : PHPPARSING
312 | <PHPSTARTLONG : "<?php"> {PHPParser.createNewHTMLCode();} : PHPPARSING
313 | <PHPECHOSTART : "<?="> {PHPParser.createNewHTMLCode();} : PHPPARSING
318 <PHPEND :"?>"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
321 /* Skip any character if we are not in php mode */
339 <PHPPARSING> SPECIAL_TOKEN :
341 "//" : IN_SINGLE_LINE_COMMENT
343 "#" : IN_SINGLE_LINE_COMMENT
345 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
347 "/*" : IN_MULTI_LINE_COMMENT
350 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
352 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
355 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
357 <SINGLE_LINE_COMMENT_PHPEND : "?>" > : DEFAULT
363 <FORMAL_COMMENT: "*/" > : PHPPARSING
366 <IN_MULTI_LINE_COMMENT>
369 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
372 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
382 | <FUNCTION : "function">
385 | <ELSEIF : "elseif">
392 /* LANGUAGE CONSTRUCT */
397 | <INCLUDE : "include">
398 | <REQUIRE : "require">
399 | <INCLUDE_ONCE : "include_once">
400 | <REQUIRE_ONCE : "require_once">
401 | <GLOBAL : "global">
402 | <STATIC : "static">
403 | <CLASSACCESS : "->">
404 | <STATICCLASSACCESS : "::">
405 | <ARRAYASSIGN : "=>">
408 /* RESERVED WORDS AND LITERALS */
414 | <CONTINUE : "continue">
415 | <_DEFAULT : "default">
417 | <EXTENDS : "extends">
422 | <RETURN : "return">
424 | <SWITCH : "switch">
429 | <ENDWHILE : "endwhile">
430 | <ENDSWITCH: "endswitch">
432 | <ENDFOR : "endfor">
433 | <FOREACH : "foreach">
441 | <OBJECT : "object">
443 | <BOOLEAN : "boolean">
445 | <DOUBLE : "double">
448 | <INTEGER : "integer">
478 | <RSIGNEDSHIFT : ">>">
479 | <RUNSIGNEDSHIFT : ">>>">
488 <DECIMAL_LITERAL> (["l","L"])?
489 | <HEX_LITERAL> (["l","L"])?
490 | <OCTAL_LITERAL> (["l","L"])?
493 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
495 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
497 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
499 <FLOATING_POINT_LITERAL:
500 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
501 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
502 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
503 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
506 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
508 <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
541 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
544 ["a"-"z"] | ["A"-"Z"]
552 "_" | ["\u007f"-"\u00ff"]
577 | <EQUAL_EQUAL : "==">
582 | <BANGDOUBLEEQUAL : "!==">
583 | <TRIPLEEQUAL : "===">
590 | <PLUSASSIGN : "+=">
591 | <MINUSASSIGN : "-=">
592 | <STARASSIGN : "*=">
593 | <SLASHASSIGN : "/=">
599 | <TILDEEQUAL : "~=">
600 | <LSHIFTASSIGN : "<<=">
601 | <RSIGNEDSHIFTASSIGN : ">>=">
606 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
615 } catch (TokenMgrError e) {
616 PHPeclipsePlugin.log(e);
617 errorStart = SimpleCharStream.getPosition();
618 errorEnd = errorStart + 1;
619 errorMessage = e.getMessage();
621 throw generateParseException();
626 * A php block is a <?= expression [;]?>
627 * or <?php somephpcode ?>
628 * or <? somephpcode ?>
632 final int start = SimpleCharStream.getPosition();
640 setMarker(fileToParse,
641 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
643 SimpleCharStream.getPosition(),
645 "Line " + token.beginLine);
646 } catch (CoreException e) {
647 PHPeclipsePlugin.log(e);
653 } catch (ParseException e) {
654 errorMessage = "'?>' expected";
656 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
657 errorEnd = SimpleCharStream.getPosition() + 1;
662 PHPEchoBlock phpEchoBlock() :
664 final Expression expr;
665 final int pos = SimpleCharStream.getPosition();
666 PHPEchoBlock echoBlock;
669 <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] <PHPEND>
671 echoBlock = new PHPEchoBlock(expr,pos,SimpleCharStream.getPosition());
672 pushOnAstNodes(echoBlock);
682 ClassDeclaration ClassDeclaration() :
684 final ClassDeclaration classDeclaration;
685 final Token className;
686 Token superclassName = null;
692 {pos = SimpleCharStream.getPosition();}
693 className = <IDENTIFIER>
694 } catch (ParseException e) {
695 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
697 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
698 errorEnd = SimpleCharStream.getPosition() + 1;
704 superclassName = <IDENTIFIER>
705 } catch (ParseException e) {
706 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
708 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
709 errorEnd = SimpleCharStream.getPosition() + 1;
714 if (superclassName == null) {
715 classDeclaration = new ClassDeclaration(currentSegment,
716 className.image.toCharArray(),
720 classDeclaration = new ClassDeclaration(currentSegment,
721 className.image.toCharArray(),
722 superclassName.image.toCharArray(),
726 currentSegment.add(classDeclaration);
727 currentSegment = classDeclaration;
729 ClassBody(classDeclaration)
730 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
731 classDeclaration.sourceEnd = SimpleCharStream.getPosition();
732 pushOnAstNodes(classDeclaration);
733 return classDeclaration;}
736 void ClassBody(ClassDeclaration classDeclaration) :
741 } catch (ParseException e) {
742 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected";
744 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
745 errorEnd = SimpleCharStream.getPosition() + 1;
748 ( ClassBodyDeclaration(classDeclaration) )*
751 } catch (ParseException e) {
752 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected";
754 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
755 errorEnd = SimpleCharStream.getPosition() + 1;
761 * A class can contain only methods and fields.
763 void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
765 MethodDeclaration method;
766 FieldDeclaration field;
769 method = MethodDeclaration() {method.setParent(classDeclaration);
770 classDeclaration.addMethod(method);}
771 | field = FieldDeclaration() {classDeclaration.addVariable(field);}
775 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
777 FieldDeclaration FieldDeclaration() :
779 VariableDeclaration variableDeclaration;
780 VariableDeclaration[] list;
781 final ArrayList arrayList = new ArrayList();
782 final int pos = SimpleCharStream.getPosition();
785 <VAR> variableDeclaration = VariableDeclarator()
786 {arrayList.add(variableDeclaration);
787 outlineInfo.addVariable(new String(variableDeclaration.name));
788 currentSegment.add(variableDeclaration);}
789 ( <COMMA> variableDeclaration = VariableDeclarator()
790 {arrayList.add(variableDeclaration);
791 outlineInfo.addVariable(new String(variableDeclaration.name));
792 currentSegment.add(variableDeclaration);}
796 } catch (ParseException e) {
797 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
799 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
800 errorEnd = SimpleCharStream.getPosition() + 1;
804 {list = new VariableDeclaration[arrayList.size()];
805 arrayList.toArray(list);
806 return new FieldDeclaration(list,
808 SimpleCharStream.getPosition(),
812 VariableDeclaration VariableDeclarator() :
814 final String varName;
815 Expression initializer = null;
816 final int pos = SimpleCharStream.getPosition();
819 varName = VariableDeclaratorId()
823 initializer = VariableInitializer()
824 } catch (ParseException e) {
825 errorMessage = "Literal expression expected in variable initializer";
827 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
828 errorEnd = SimpleCharStream.getPosition() + 1;
833 if (initializer == null) {
834 return new VariableDeclaration(currentSegment,
835 varName.toCharArray(),
837 SimpleCharStream.getPosition());
839 return new VariableDeclaration(currentSegment,
840 varName.toCharArray(),
848 * @return the variable name (with suffix)
850 String VariableDeclaratorId() :
853 Expression expression;
854 final StringBuffer buff = new StringBuffer();
855 final int pos = SimpleCharStream.getPosition();
856 ConstantIdentifier ex;
860 expr = Variable() {buff.append(expr);}
862 {ex = new ConstantIdentifier(expr.toCharArray(),
864 SimpleCharStream.getPosition());}
865 expression = VariableSuffix(ex)
866 {buff.append(expression.toStringExpression());}
868 {return buff.toString();}
869 } catch (ParseException e) {
870 errorMessage = "'$' expected for variable identifier";
872 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
873 errorEnd = SimpleCharStream.getPosition() + 1;
880 final StringBuffer buff;
881 Expression expression = null;
886 token = <DOLLAR_ID> [<LBRACE> expression = Expression() <RBRACE>]
888 if (expression == null && !assigning) {
889 return token.image.substring(1);
891 buff = new StringBuffer(token.image);
893 buff.append(expression.toStringExpression());
895 return buff.toString();
898 <DOLLAR> expr = VariableName()
902 String VariableName():
904 final StringBuffer buff;
906 Expression expression = null;
910 <LBRACE> expression = Expression() <RBRACE>
911 {buff = new StringBuffer("{");
912 buff.append(expression.toStringExpression());
914 return buff.toString();}
916 token = <IDENTIFIER> [<LBRACE> expression = Expression() <RBRACE>]
918 if (expression == null) {
921 buff = new StringBuffer(token.image);
923 buff.append(expression.toStringExpression());
925 return buff.toString();
928 <DOLLAR> expr = VariableName()
930 buff = new StringBuffer('$');
932 return buff.toString();
935 token = <DOLLAR_ID> {return token.image;}
938 Expression VariableInitializer() :
940 final Expression expr;
942 final int pos = SimpleCharStream.getPosition();
948 <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
949 {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
951 SimpleCharStream.getPosition()),
955 <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
956 {return new PrefixedUnaryExpression(new NumberLiteral(token.image.toCharArray(),
958 SimpleCharStream.getPosition()),
962 expr = ArrayDeclarator()
966 {return new ConstantIdentifier(token.image.toCharArray(),pos,SimpleCharStream.getPosition());}
969 ArrayVariableDeclaration ArrayVariable() :
971 Expression expr,expr2;
975 [<ARRAYASSIGN> expr2 = Expression()
976 {return new ArrayVariableDeclaration(expr,expr2);}
978 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
981 ArrayVariableDeclaration[] ArrayInitializer() :
983 ArrayVariableDeclaration expr;
984 final ArrayList list = new ArrayList();
987 <LPAREN> [ expr = ArrayVariable()
989 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
993 [<COMMA> {list.add(null);}]
996 ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1002 * A Method Declaration.
1003 * <b>function</b> MetodDeclarator() Block()
1005 MethodDeclaration MethodDeclaration() :
1007 final MethodDeclaration functionDeclaration;
1013 functionDeclaration = MethodDeclarator()
1014 {outlineInfo.addVariable(new String(functionDeclaration.name));}
1015 } catch (ParseException e) {
1016 if (errorMessage != null) throw e;
1017 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1019 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1020 errorEnd = SimpleCharStream.getPosition() + 1;
1024 if (currentSegment != null) {
1025 currentSegment.add(functionDeclaration);
1026 currentSegment = functionDeclaration;
1031 functionDeclaration.statements = block.statements;
1032 if (currentSegment != null) {
1033 currentSegment = (OutlineableWithChildren) currentSegment.getParent();
1035 return functionDeclaration;
1040 * A MethodDeclarator.
1041 * [&] IDENTIFIER(parameters ...).
1042 * @return a function description for the outline
1044 MethodDeclaration MethodDeclarator() :
1046 final Token identifier;
1047 Token reference = null;
1048 final Hashtable formalParameters;
1049 final int pos = SimpleCharStream.getPosition();
1052 [reference = <BIT_AND>] identifier = <IDENTIFIER>
1053 formalParameters = FormalParameters()
1054 {return new MethodDeclaration(currentSegment,
1055 identifier.image.toCharArray(),
1059 SimpleCharStream.getPosition());}
1063 * FormalParameters follows method identifier.
1064 * (FormalParameter())
1066 Hashtable FormalParameters() :
1068 VariableDeclaration var;
1069 final Hashtable parameters = new Hashtable();
1074 } catch (ParseException e) {
1075 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1077 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1078 errorEnd = SimpleCharStream.getPosition() + 1;
1081 [ var = FormalParameter()
1082 {parameters.put(new String(var.name),var);}
1084 <COMMA> var = FormalParameter()
1085 {parameters.put(new String(var.name),var);}
1090 } catch (ParseException e) {
1091 errorMessage = "')' expected";
1093 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1094 errorEnd = SimpleCharStream.getPosition() + 1;
1097 {return parameters;}
1101 * A formal parameter.
1102 * $varname[=value] (,$varname[=value])
1104 VariableDeclaration FormalParameter() :
1106 final VariableDeclaration variableDeclaration;
1110 [token = <BIT_AND>] variableDeclaration = VariableDeclarator()
1112 if (token != null) {
1113 variableDeclaration.setReference(true);
1115 return variableDeclaration;}
1118 ConstantIdentifier Type() :
1121 <STRING> {pos = SimpleCharStream.getPosition();
1122 return new ConstantIdentifier(Types.STRING,
1124 | <BOOL> {pos = SimpleCharStream.getPosition();
1125 return new ConstantIdentifier(Types.BOOL,
1127 | <BOOLEAN> {pos = SimpleCharStream.getPosition();
1128 return new ConstantIdentifier(Types.BOOLEAN,
1130 | <REAL> {pos = SimpleCharStream.getPosition();
1131 return new ConstantIdentifier(Types.REAL,
1133 | <DOUBLE> {pos = SimpleCharStream.getPosition();
1134 return new ConstantIdentifier(Types.DOUBLE,
1136 | <FLOAT> {pos = SimpleCharStream.getPosition();
1137 return new ConstantIdentifier(Types.FLOAT,
1139 | <INT> {pos = SimpleCharStream.getPosition();
1140 return new ConstantIdentifier(Types.INT,
1142 | <INTEGER> {pos = SimpleCharStream.getPosition();
1143 return new ConstantIdentifier(Types.INTEGER,
1145 | <OBJECT> {pos = SimpleCharStream.getPosition();
1146 return new ConstantIdentifier(Types.OBJECT,
1150 Expression Expression() :
1152 final Expression expr;
1155 expr = PrintExpression() {return expr;}
1156 | expr = ListExpression() {return expr;}
1157 | LOOKAHEAD(varAssignation())
1158 expr = varAssignation() {return expr;}
1159 | expr = ConditionalExpression() {return expr;}
1163 * A Variable assignation.
1164 * varName (an assign operator) any expression
1166 VarAssignation varAssignation() :
1169 final Expression expression;
1170 final int assignOperator;
1171 final int pos = SimpleCharStream.getPosition();
1174 varName = VariableDeclaratorId()
1175 assignOperator = AssignmentOperator()
1177 expression = Expression()
1178 } catch (ParseException e) {
1179 if (errorMessage != null) {
1182 errorMessage = "expression expected";
1184 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1185 errorEnd = SimpleCharStream.getPosition() + 1;
1188 {return new VarAssignation(varName.toCharArray(),
1192 SimpleCharStream.getPosition());}
1195 int AssignmentOperator() :
1198 <ASSIGN> {return VarAssignation.EQUAL;}
1199 | <STARASSIGN> {return VarAssignation.STAR_EQUAL;}
1200 | <SLASHASSIGN> {return VarAssignation.SLASH_EQUAL;}
1201 | <REMASSIGN> {return VarAssignation.REM_EQUAL;}
1202 | <PLUSASSIGN> {return VarAssignation.PLUS_EQUAL;}
1203 | <MINUSASSIGN> {return VarAssignation.MINUS_EQUAL;}
1204 | <LSHIFTASSIGN> {return VarAssignation.LSHIFT_EQUAL;}
1205 | <RSIGNEDSHIFTASSIGN> {return VarAssignation.RSIGNEDSHIFT_EQUAL;}
1206 | <ANDASSIGN> {return VarAssignation.AND_EQUAL;}
1207 | <XORASSIGN> {return VarAssignation.XOR_EQUAL;}
1208 | <ORASSIGN> {return VarAssignation.OR_EQUAL;}
1209 | <DOTASSIGN> {return VarAssignation.DOT_EQUAL;}
1210 | <TILDEEQUAL> {return VarAssignation.TILDE_EQUAL;}
1213 Expression ConditionalExpression() :
1215 final Expression expr;
1216 Expression expr2 = null;
1217 Expression expr3 = null;
1220 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1222 if (expr3 == null) {
1225 return new ConditionalExpression(expr,expr2,expr3);
1229 Expression ConditionalOrExpression() :
1231 Expression expr,expr2;
1235 expr = ConditionalAndExpression()
1238 <OR_OR> {operator = OperatorIds.OR_OR;}
1239 | <_ORL> {operator = OperatorIds.ORL;}
1240 ) expr2 = ConditionalAndExpression()
1242 expr = new BinaryExpression(expr,expr2,operator);
1248 Expression ConditionalAndExpression() :
1250 Expression expr,expr2;
1254 expr = ConcatExpression()
1256 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1257 | <_ANDL> {operator = OperatorIds.ANDL;})
1258 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1263 Expression ConcatExpression() :
1265 Expression expr,expr2;
1268 expr = InclusiveOrExpression()
1270 <DOT> expr2 = InclusiveOrExpression()
1271 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1276 Expression InclusiveOrExpression() :
1278 Expression expr,expr2;
1281 expr = ExclusiveOrExpression()
1282 (<BIT_OR> expr2 = ExclusiveOrExpression()
1283 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1288 Expression ExclusiveOrExpression() :
1290 Expression expr,expr2;
1293 expr = AndExpression()
1295 <XOR> expr2 = AndExpression()
1296 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1301 Expression AndExpression() :
1303 Expression expr,expr2;
1306 expr = EqualityExpression()
1308 <BIT_AND> expr2 = EqualityExpression()
1309 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1314 Expression EqualityExpression() :
1316 Expression expr,expr2;
1320 expr = RelationalExpression()
1322 ( <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1323 | <DIF> {operator = OperatorIds.DIF;}
1324 | <NOT_EQUAL> {operator = OperatorIds.DIF;}
1325 | <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1326 | <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1329 expr2 = RelationalExpression()
1330 } catch (ParseException e) {
1331 if (errorMessage != null) {
1334 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1336 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1337 errorEnd = SimpleCharStream.getPosition() + 1;
1341 expr = new BinaryExpression(expr,expr2,operator);
1347 Expression RelationalExpression() :
1349 Expression expr,expr2;
1353 expr = ShiftExpression()
1355 ( <LT> {operator = OperatorIds.LESS;}
1356 | <GT> {operator = OperatorIds.GREATER;}
1357 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1358 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1359 expr2 = ShiftExpression()
1360 {expr = new BinaryExpression(expr,expr2,operator);}
1365 Expression ShiftExpression() :
1367 Expression expr,expr2;
1371 expr = AdditiveExpression()
1373 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1374 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1375 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1376 expr2 = AdditiveExpression()
1377 {expr = new BinaryExpression(expr,expr2,operator);}
1382 Expression AdditiveExpression() :
1384 Expression expr,expr2;
1388 expr = MultiplicativeExpression()
1390 ( <PLUS> {operator = OperatorIds.PLUS;}
1391 | <MINUS> {operator = OperatorIds.MINUS;} )
1392 expr2 = MultiplicativeExpression()
1393 {expr = new BinaryExpression(expr,expr2,operator);}
1398 Expression MultiplicativeExpression() :
1400 Expression expr,expr2;
1405 expr = UnaryExpression()
1406 } catch (ParseException e) {
1407 if (errorMessage != null) throw e;
1408 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1410 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1411 errorEnd = SimpleCharStream.getPosition() + 1;
1415 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1416 | <SLASH> {operator = OperatorIds.DIVIDE;}
1417 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1418 expr2 = UnaryExpression()
1419 {expr = new BinaryExpression(expr,expr2,operator);}
1425 * An unary expression starting with @, & or nothing
1427 Expression UnaryExpression() :
1430 final int pos = SimpleCharStream.getPosition();
1433 <BIT_AND> expr = UnaryExpressionNoPrefix()
1434 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1436 expr = AtUnaryExpression() {return expr;}
1439 Expression AtUnaryExpression() :
1442 final int pos = SimpleCharStream.getPosition();
1446 expr = AtUnaryExpression()
1447 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,pos);}
1449 expr = UnaryExpressionNoPrefix()
1454 Expression UnaryExpressionNoPrefix() :
1458 final int pos = SimpleCharStream.getPosition();
1461 ( <PLUS> {operator = OperatorIds.PLUS;}
1462 | <MINUS> {operator = OperatorIds.MINUS;})
1463 expr = UnaryExpression()
1464 {return new PrefixedUnaryExpression(expr,operator,pos);}
1466 expr = PreIncDecExpression()
1469 expr = UnaryExpressionNotPlusMinus()
1474 Expression PreIncDecExpression() :
1476 final Expression expr;
1478 final int pos = SimpleCharStream.getPosition();
1481 ( <INCR> {operator = OperatorIds.PLUS_PLUS;}
1482 | <DECR> {operator = OperatorIds.MINUS_MINUS;})
1483 expr = PrimaryExpression()
1484 {return new PrefixedUnaryExpression(expr,operator,pos);}
1487 Expression UnaryExpressionNotPlusMinus() :
1490 final int pos = SimpleCharStream.getPosition();
1493 <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
1494 | LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1495 expr = CastExpression() {return expr;}
1496 | expr = PostfixExpression() {return expr;}
1497 | expr = Literal() {return expr;}
1498 | <LPAREN> expr = Expression()
1501 } catch (ParseException e) {
1502 errorMessage = "')' expected";
1504 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1505 errorEnd = SimpleCharStream.getPosition() + 1;
1511 CastExpression CastExpression() :
1513 final ConstantIdentifier type;
1514 final Expression expr;
1515 final int pos = SimpleCharStream.getPosition();
1520 | <ARRAY> {type = new ConstantIdentifier(Types.ARRAY,pos,SimpleCharStream.getPosition());})
1521 <RPAREN> expr = UnaryExpression()
1522 {return new CastExpression(type,expr,pos,SimpleCharStream.getPosition());}
1525 Expression PostfixExpression() :
1529 final int pos = SimpleCharStream.getPosition();
1532 expr = PrimaryExpression()
1533 [ <INCR> {operator = OperatorIds.PLUS_PLUS;}
1534 | <DECR> {operator = OperatorIds.MINUS_MINUS;}]
1536 if (operator == -1) {
1539 return new PostfixedUnaryExpression(expr,operator,pos);
1543 Expression PrimaryExpression() :
1545 final Token identifier;
1547 final int pos = SimpleCharStream.getPosition();
1551 identifier = <IDENTIFIER> <STATICCLASSACCESS> expr = ClassIdentifier()
1552 {expr = new ClassAccess(new ConstantIdentifier(identifier.image.toCharArray(),
1554 SimpleCharStream.getPosition()),
1556 ClassAccess.STATIC);}
1557 (expr = PrimarySuffix(expr))*
1560 expr = PrimaryPrefix()
1561 (expr = PrimarySuffix(expr))*
1564 expr = ArrayDeclarator()
1568 ArrayInitializer ArrayDeclarator() :
1570 final ArrayVariableDeclaration[] vars;
1571 final int pos = SimpleCharStream.getPosition();
1574 <ARRAY> vars = ArrayInitializer()
1575 {return new ArrayInitializer(vars,pos,SimpleCharStream.getPosition());}
1578 Expression PrimaryPrefix() :
1580 final Expression expr;
1583 final int pos = SimpleCharStream.getPosition();
1586 token = <IDENTIFIER> {return new ConstantIdentifier(token.image.toCharArray(),
1588 SimpleCharStream.getPosition());}
1589 | <NEW> expr = ClassIdentifier() {return new PrefixedUnaryExpression(expr,
1592 | var = VariableDeclaratorId() {return new ConstantIdentifier(var.toCharArray(),
1594 SimpleCharStream.getPosition());}
1597 PrefixedUnaryExpression classInstantiation() :
1600 final StringBuffer buff;
1601 final int pos = SimpleCharStream.getPosition();
1604 <NEW> expr = ClassIdentifier()
1606 {buff = new StringBuffer(expr.toStringExpression());}
1607 expr = PrimaryExpression()
1608 {buff.append(expr.toStringExpression());
1609 expr = new ConstantIdentifier(buff.toString().toCharArray(),
1611 SimpleCharStream.getPosition());}
1613 {return new PrefixedUnaryExpression(expr,
1618 ConstantIdentifier ClassIdentifier():
1622 final int pos = SimpleCharStream.getPosition();
1625 token = <IDENTIFIER> {return new ConstantIdentifier(token.image.toCharArray(),
1627 SimpleCharStream.getPosition());}
1628 | expr = VariableDeclaratorId() {return new ConstantIdentifier(expr.toCharArray(),
1630 SimpleCharStream.getPosition());}
1633 AbstractSuffixExpression PrimarySuffix(Expression prefix) :
1635 final AbstractSuffixExpression expr;
1638 expr = Arguments(prefix) {return expr;}
1639 | expr = VariableSuffix(prefix) {return expr;}
1642 AbstractSuffixExpression VariableSuffix(Expression prefix) :
1645 final int pos = SimpleCharStream.getPosition();
1646 Expression expression = null;
1651 expr = VariableName()
1652 } catch (ParseException e) {
1653 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1655 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1656 errorEnd = SimpleCharStream.getPosition() + 1;
1659 {return new ClassAccess(prefix,
1660 new ConstantIdentifier(expr.toCharArray(),pos,SimpleCharStream.getPosition()),
1661 ClassAccess.NORMAL);}
1663 <LBRACKET> [ expression = Expression() | expression = Type() ] //Not good
1666 } catch (ParseException e) {
1667 errorMessage = "']' expected";
1669 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1670 errorEnd = SimpleCharStream.getPosition() + 1;
1673 {return new ArrayDeclarator(prefix,expression,SimpleCharStream.getPosition());}
1682 token = <INTEGER_LITERAL> {pos = SimpleCharStream.getPosition();
1683 return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
1684 | token = <FLOATING_POINT_LITERAL> {pos = SimpleCharStream.getPosition();
1685 return new NumberLiteral(token.image.toCharArray(),pos-token.image.length(),pos);}
1686 | token = <STRING_LITERAL> {pos = SimpleCharStream.getPosition();
1687 return new StringLiteral(token.image.toCharArray(),pos-token.image.length());}
1688 | <TRUE> {pos = SimpleCharStream.getPosition();
1689 return new TrueLiteral(pos-4,pos);}
1690 | <FALSE> {pos = SimpleCharStream.getPosition();
1691 return new FalseLiteral(pos-4,pos);}
1692 | <NULL> {pos = SimpleCharStream.getPosition();
1693 return new NullLiteral(pos-4,pos);}
1696 FunctionCall Arguments(Expression func) :
1698 Expression[] args = null;
1701 <LPAREN> [ args = ArgumentList() ]
1704 } catch (ParseException e) {
1705 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
1707 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1708 errorEnd = SimpleCharStream.getPosition() + 1;
1711 {return new FunctionCall(func,args,SimpleCharStream.getPosition());}
1715 * An argument list is a list of arguments separated by comma :
1716 * argumentDeclaration() (, argumentDeclaration)*
1717 * @return an array of arguments
1719 Expression[] ArgumentList() :
1722 final ArrayList list = new ArrayList();
1731 } catch (ParseException e) {
1732 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
1734 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1735 errorEnd = SimpleCharStream.getPosition() + 1;
1740 Expression[] arguments = new Expression[list.size()];
1741 list.toArray(arguments);
1746 * A Statement without break.
1748 Statement StatementNoBreak() :
1750 final Statement statement;
1755 statement = Expression()
1758 } catch (ParseException e) {
1759 if (e.currentToken.next.kind != 4) {
1760 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1762 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1763 errorEnd = SimpleCharStream.getPosition() + 1;
1769 statement = LabeledStatement() {return statement;}
1770 | statement = Block() {return statement;}
1771 | statement = EmptyStatement() {return statement;}
1772 | statement = StatementExpression()
1775 } catch (ParseException e) {
1776 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1778 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1779 errorEnd = SimpleCharStream.getPosition() + 1;
1783 | statement = SwitchStatement() {return statement;}
1784 | statement = IfStatement() {return statement;}
1785 | statement = WhileStatement() {return statement;}
1786 | statement = DoStatement() {return statement;}
1787 | statement = ForStatement() {return statement;}
1788 | statement = ForeachStatement() {return statement;}
1789 | statement = ContinueStatement() {return statement;}
1790 | statement = ReturnStatement() {return statement;}
1791 | statement = EchoStatement() {return statement;}
1792 | [token=<AT>] statement = IncludeStatement()
1793 {if (token != null) {
1794 ((InclusionStatement)statement).silent = true;
1797 | statement = StaticStatement() {return statement;}
1798 | statement = GlobalStatement() {return statement;}
1802 * A Normal statement.
1804 Statement Statement() :
1806 final Statement statement;
1809 statement = StatementNoBreak() {return statement;}
1810 | statement = BreakStatement() {return statement;}
1814 * An html block inside a php syntax.
1816 HTMLBlock htmlBlock() :
1818 final int startIndex = nodePtr;
1819 AstNode[] blockNodes;
1823 <PHPEND> (phpEchoBlock())*
1825 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
1826 } catch (ParseException e) {
1827 errorMessage = "End of file unexpected, '<?php' expected";
1829 errorStart = SimpleCharStream.getPosition();
1830 errorEnd = SimpleCharStream.getPosition();
1834 nbNodes = nodePtr-startIndex - 1;
1835 blockNodes = new AstNode[nbNodes];
1836 System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes);
1837 return new HTMLBlock(nodes);}
1841 * An include statement. It's "include" an expression;
1843 InclusionStatement IncludeStatement() :
1845 final Expression expr;
1847 final int pos = SimpleCharStream.getPosition();
1848 final InclusionStatement inclusionStatement;
1851 ( <REQUIRE> {keyword = InclusionStatement.REQUIRE;}
1852 | <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;}
1853 | <INCLUDE> {keyword = InclusionStatement.INCLUDE;}
1854 | <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;})
1857 } catch (ParseException e) {
1858 if (errorMessage != null) {
1861 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
1863 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1864 errorEnd = SimpleCharStream.getPosition() + 1;
1867 {inclusionStatement = new InclusionStatement(currentSegment,
1871 currentSegment.add(inclusionStatement);
1875 } catch (ParseException e) {
1876 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1878 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1879 errorEnd = SimpleCharStream.getPosition() + 1;
1882 {return inclusionStatement;}
1885 PrintExpression PrintExpression() :
1887 final Expression expr;
1888 final int pos = SimpleCharStream.getPosition();
1891 <PRINT> expr = Expression() {return new PrintExpression(expr,pos,SimpleCharStream.getPosition());}
1894 ListExpression ListExpression() :
1897 Expression expression = null;
1898 ArrayList list = new ArrayList();
1899 final int pos = SimpleCharStream.getPosition();
1905 } catch (ParseException e) {
1906 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
1908 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1909 errorEnd = SimpleCharStream.getPosition() + 1;
1913 expr = VariableDeclaratorId()
1916 {if (expr == null) list.add(null);}
1920 } catch (ParseException e) {
1921 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
1923 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1924 errorEnd = SimpleCharStream.getPosition() + 1;
1927 expr = VariableDeclaratorId()
1932 } catch (ParseException e) {
1933 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
1935 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1936 errorEnd = SimpleCharStream.getPosition() + 1;
1939 [ <ASSIGN> expression = Expression()
1941 String[] strings = new String[list.size()];
1942 list.toArray(strings);
1943 return new ListExpression(strings,
1946 SimpleCharStream.getPosition());}
1949 String[] strings = new String[list.size()];
1950 list.toArray(strings);
1951 return new ListExpression(strings,pos,SimpleCharStream.getPosition());}
1955 * An echo statement.
1956 * echo anyexpression (, otherexpression)*
1958 EchoStatement EchoStatement() :
1960 final ArrayList expressions = new ArrayList();
1962 final int pos = SimpleCharStream.getPosition();
1965 <ECHO> expr = Expression()
1966 {expressions.add(expr);}
1968 <COMMA> expr = Expression()
1969 {expressions.add(expr);}
1974 Expression[] exprs = new Expression[expressions.size()];
1975 expressions.toArray(exprs);
1976 return new EchoStatement(exprs,pos);}
1977 } catch (ParseException e) {
1978 if (e.currentToken.next.kind != 4) {
1979 errorMessage = "';' expected after 'echo' statement";
1981 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1982 errorEnd = SimpleCharStream.getPosition() + 1;
1988 GlobalStatement GlobalStatement() :
1990 final int pos = SimpleCharStream.getPosition();
1992 ArrayList vars = new ArrayList();
1993 GlobalStatement global;
1997 expr = VariableDeclaratorId()
2000 expr = VariableDeclaratorId()
2006 String[] strings = new String[vars.size()];
2007 vars.toArray(strings);
2008 global = new GlobalStatement(currentSegment,
2011 SimpleCharStream.getPosition());
2012 currentSegment.add(global);
2014 } catch (ParseException e) {
2015 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2017 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2018 errorEnd = SimpleCharStream.getPosition() + 1;
2023 StaticStatement StaticStatement() :
2025 final int pos = SimpleCharStream.getPosition();
2026 final ArrayList vars = new ArrayList();
2027 VariableDeclaration expr;
2030 <STATIC> expr = VariableDeclarator() {vars.add(new String(expr.name));}
2031 (<COMMA> expr = VariableDeclarator() {vars.add(new String(expr.name));})*
2035 String[] strings = new String[vars.size()];
2036 vars.toArray(strings);
2037 return new StaticStatement(strings,
2039 SimpleCharStream.getPosition());}
2040 } catch (ParseException e) {
2041 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2043 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2044 errorEnd = SimpleCharStream.getPosition() + 1;
2049 LabeledStatement LabeledStatement() :
2051 final int pos = SimpleCharStream.getPosition();
2053 final Statement statement;
2056 label = <IDENTIFIER> <COLON> statement = Statement()
2057 {return new LabeledStatement(label.image.toCharArray(),statement,pos,SimpleCharStream.getPosition());}
2069 final int pos = SimpleCharStream.getPosition();
2070 final ArrayList list = new ArrayList();
2071 Statement statement;
2076 } catch (ParseException e) {
2077 errorMessage = "'{' expected";
2079 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2080 errorEnd = SimpleCharStream.getPosition() + 1;
2083 ( statement = BlockStatement() {list.add(statement);}
2084 | statement = htmlBlock() {list.add(statement);})*
2087 } catch (ParseException e) {
2088 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2090 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2091 errorEnd = SimpleCharStream.getPosition() + 1;
2095 Statement[] statements = new Statement[list.size()];
2096 list.toArray(statements);
2097 return new Block(statements,pos,SimpleCharStream.getPosition());}
2100 Statement BlockStatement() :
2102 final Statement statement;
2105 statement = Statement() {return statement;}
2106 | statement = ClassDeclaration() {return statement;}
2107 | statement = MethodDeclaration() {return statement;}
2111 * A Block statement that will not contain any 'break'
2113 Statement BlockStatementNoBreak() :
2115 final Statement statement;
2118 statement = StatementNoBreak() {return statement;}
2119 | statement = ClassDeclaration() {return statement;}
2120 | statement = MethodDeclaration() {return statement;}
2123 VariableDeclaration[] LocalVariableDeclaration() :
2125 final ArrayList list = new ArrayList();
2126 VariableDeclaration var;
2129 var = LocalVariableDeclarator()
2131 ( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
2133 VariableDeclaration[] vars = new VariableDeclaration[list.size()];
2138 VariableDeclaration LocalVariableDeclarator() :
2140 final String varName;
2141 Expression initializer = null;
2142 final int pos = SimpleCharStream.getPosition();
2145 varName = VariableDeclaratorId() [ <ASSIGN> initializer = Expression() ]
2147 if (initializer == null) {
2148 return new VariableDeclaration(currentSegment,
2149 varName.toCharArray(),
2151 SimpleCharStream.getPosition());
2153 return new VariableDeclaration(currentSegment,
2154 varName.toCharArray(),
2160 EmptyStatement EmptyStatement() :
2166 {pos = SimpleCharStream.getPosition();
2167 return new EmptyStatement(pos-1,pos);}
2170 Statement StatementExpression() :
2172 Expression expr,expr2;
2176 expr = PreIncDecExpression() {return expr;}
2178 expr = PrimaryExpression()
2179 [ <INCR> {return new PostfixedUnaryExpression(expr,
2180 OperatorIds.PLUS_PLUS,
2181 SimpleCharStream.getPosition());}
2182 | <DECR> {return new PostfixedUnaryExpression(expr,
2183 OperatorIds.MINUS_MINUS,
2184 SimpleCharStream.getPosition());}
2185 | operator = AssignmentOperator() expr2 = Expression()
2186 {return new BinaryExpression(expr,expr2,operator);}
2191 SwitchStatement SwitchStatement() :
2193 final Expression variable;
2194 final AbstractCase[] cases;
2195 final int pos = SimpleCharStream.getPosition();
2201 } catch (ParseException e) {
2202 errorMessage = "'(' expected after 'switch'";
2204 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2205 errorEnd = SimpleCharStream.getPosition() + 1;
2209 variable = Expression()
2210 } catch (ParseException e) {
2211 if (errorMessage != null) {
2214 errorMessage = "expression expected";
2216 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2217 errorEnd = SimpleCharStream.getPosition() + 1;
2222 } catch (ParseException e) {
2223 errorMessage = "')' expected";
2225 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2226 errorEnd = SimpleCharStream.getPosition() + 1;
2229 (cases = switchStatementBrace() | cases = switchStatementColon(pos, pos + 6))
2230 {return new SwitchStatement(variable,cases,pos,SimpleCharStream.getPosition());}
2233 AbstractCase[] switchStatementBrace() :
2236 final ArrayList cases = new ArrayList();
2240 ( cas = switchLabel0() {cases.add(cas);})*
2244 AbstractCase[] abcase = new AbstractCase[cases.size()];
2245 cases.toArray(abcase);
2247 } catch (ParseException e) {
2248 errorMessage = "'}' expected";
2250 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2251 errorEnd = SimpleCharStream.getPosition() + 1;
2256 * A Switch statement with : ... endswitch;
2257 * @param start the begin offset of the switch
2258 * @param end the end offset of the switch
2260 AbstractCase[] switchStatementColon(final int start, final int end) :
2263 final ArrayList cases = new ArrayList();
2268 setMarker(fileToParse,
2269 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2273 "Line " + token.beginLine);
2274 } catch (CoreException e) {
2275 PHPeclipsePlugin.log(e);
2277 ( cas = switchLabel0() {cases.add(cas);})*
2280 } catch (ParseException e) {
2281 errorMessage = "'endswitch' expected";
2283 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2284 errorEnd = SimpleCharStream.getPosition() + 1;
2290 AbstractCase[] abcase = new AbstractCase[cases.size()];
2291 cases.toArray(abcase);
2293 } catch (ParseException e) {
2294 errorMessage = "';' expected after 'endswitch' keyword";
2296 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2297 errorEnd = SimpleCharStream.getPosition() + 1;
2302 AbstractCase switchLabel0() :
2304 final Expression expr;
2305 Statement statement;
2306 final ArrayList stmts = new ArrayList();
2307 final int pos = SimpleCharStream.getPosition();
2310 expr = SwitchLabel()
2311 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2312 | statement = htmlBlock() {stmts.add(statement);})*
2313 [ statement = BreakStatement() {stmts.add(statement);}]
2315 Statement[] stmtsArray = new Statement[stmts.size()];
2316 stmts.toArray(stmtsArray);
2317 if (expr == null) {//it's a default
2318 return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition());
2320 return new Case(expr,stmtsArray,pos,SimpleCharStream.getPosition());}
2325 * case Expression() :
2327 * @return the if it was a case and null if not
2329 Expression SwitchLabel() :
2331 final Expression expr;
2337 } catch (ParseException e) {
2338 if (errorMessage != null) throw e;
2339 errorMessage = "expression expected after 'case' keyword";
2341 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2342 errorEnd = SimpleCharStream.getPosition() + 1;
2348 } catch (ParseException e) {
2349 errorMessage = "':' expected after case expression";
2351 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2352 errorEnd = SimpleCharStream.getPosition() + 1;
2360 } catch (ParseException e) {
2361 errorMessage = "':' expected after 'default' keyword";
2363 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2364 errorEnd = SimpleCharStream.getPosition() + 1;
2369 Break BreakStatement() :
2371 Expression expression = null;
2372 final int start = SimpleCharStream.getPosition();
2375 <BREAK> [ expression = Expression() ]
2378 } catch (ParseException e) {
2379 errorMessage = "';' expected after 'break' keyword";
2381 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2382 errorEnd = SimpleCharStream.getPosition() + 1;
2385 {return new Break(expression, start, SimpleCharStream.getPosition());}
2388 IfStatement IfStatement() :
2390 final int pos = SimpleCharStream.getPosition();
2391 Expression condition;
2392 IfStatement ifStatement;
2395 <IF> condition = Condition("if") ifStatement = IfStatement0(condition, pos,pos+2)
2396 {return ifStatement;}
2400 Expression Condition(final String keyword) :
2402 final Expression condition;
2407 } catch (ParseException e) {
2408 errorMessage = "'(' expected after " + keyword + " keyword";
2410 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length();
2411 errorEnd = errorStart +1;
2412 processParseException(e);
2414 condition = Expression()
2418 } catch (ParseException e) {
2419 errorMessage = "')' expected after " + keyword + " keyword";
2421 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2422 errorEnd = SimpleCharStream.getPosition() + 1;
2427 IfStatement IfStatement0(Expression condition, final int start,final int end) :
2429 Statement statement;
2431 final Statement[] statementsArray;
2432 ElseIf elseifStatement;
2433 Else elseStatement = null;
2435 final ArrayList elseIfList = new ArrayList();
2437 int pos = SimpleCharStream.getPosition();
2442 {stmts = new ArrayList();}
2443 ( statement = Statement() {stmts.add(statement);}
2444 | statement = htmlBlock() {stmts.add(statement);})*
2445 {endStatements = SimpleCharStream.getPosition();}
2446 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2447 [elseStatement = ElseStatementColon()]
2450 setMarker(fileToParse,
2451 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2455 "Line " + token.beginLine);
2456 } catch (CoreException e) {
2457 PHPeclipsePlugin.log(e);
2461 } catch (ParseException e) {
2462 errorMessage = "'endif' expected";
2464 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2465 errorEnd = SimpleCharStream.getPosition() + 1;
2470 } catch (ParseException e) {
2471 errorMessage = "';' expected after 'endif' keyword";
2473 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2474 errorEnd = SimpleCharStream.getPosition() + 1;
2478 elseIfs = new ElseIf[elseIfList.size()];
2479 elseIfList.toArray(elseIfs);
2480 if (stmts.size() == 1) {
2481 return new IfStatement(condition,
2482 (Statement) stmts.get(0),
2486 SimpleCharStream.getPosition());
2488 statementsArray = new Statement[stmts.size()];
2489 stmts.toArray(statementsArray);
2490 return new IfStatement(condition,
2491 new Block(statementsArray,pos,endStatements),
2495 SimpleCharStream.getPosition());
2500 (stmt = Statement() | stmt = htmlBlock())
2501 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
2505 {pos = SimpleCharStream.getPosition();}
2506 statement = Statement()
2507 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
2508 } catch (ParseException e) {
2509 if (errorMessage != null) {
2512 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
2514 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2515 errorEnd = SimpleCharStream.getPosition() + 1;
2520 elseIfs = new ElseIf[elseIfList.size()];
2521 elseIfList.toArray(elseIfs);
2522 return new IfStatement(condition,
2527 SimpleCharStream.getPosition());}
2530 ElseIf ElseIfStatementColon() :
2532 Expression condition;
2533 Statement statement;
2534 final ArrayList list = new ArrayList();
2535 final int pos = SimpleCharStream.getPosition();
2538 <ELSEIF> condition = Condition("elseif")
2539 <COLON> ( statement = Statement() {list.add(statement);}
2540 | statement = htmlBlock() {list.add(statement);})*
2542 Statement[] stmtsArray = new Statement[list.size()];
2543 list.toArray(stmtsArray);
2544 return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());}
2547 Else ElseStatementColon() :
2549 Statement statement;
2550 final ArrayList list = new ArrayList();
2551 final int pos = SimpleCharStream.getPosition();
2554 <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
2555 | statement = htmlBlock() {list.add(statement);})*
2557 Statement[] stmtsArray = new Statement[list.size()];
2558 list.toArray(stmtsArray);
2559 return new Else(stmtsArray,pos,SimpleCharStream.getPosition());}
2562 ElseIf ElseIfStatement() :
2564 Expression condition;
2565 Statement statement;
2566 final ArrayList list = new ArrayList();
2567 final int pos = SimpleCharStream.getPosition();
2570 <ELSEIF> condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/}
2572 Statement[] stmtsArray = new Statement[list.size()];
2573 list.toArray(stmtsArray);
2574 return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());}
2577 WhileStatement WhileStatement() :
2579 final Expression condition;
2580 final Statement action;
2581 final int pos = SimpleCharStream.getPosition();
2585 condition = Condition("while")
2586 action = WhileStatement0(pos,pos + 5)
2587 {return new WhileStatement(condition,action,pos,SimpleCharStream.getPosition());}
2590 Statement WhileStatement0(final int start, final int end) :
2592 Statement statement;
2593 final ArrayList stmts = new ArrayList();
2594 final int pos = SimpleCharStream.getPosition();
2597 <COLON> (statement = Statement() {stmts.add(statement);})*
2599 setMarker(fileToParse,
2600 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
2604 "Line " + token.beginLine);
2605 } catch (CoreException e) {
2606 PHPeclipsePlugin.log(e);
2610 } catch (ParseException e) {
2611 errorMessage = "'endwhile' expected";
2613 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2614 errorEnd = SimpleCharStream.getPosition() + 1;
2620 Statement[] stmtsArray = new Statement[stmts.size()];
2621 stmts.toArray(stmtsArray);
2622 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
2623 } catch (ParseException e) {
2624 errorMessage = "';' expected after 'endwhile' keyword";
2626 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2627 errorEnd = SimpleCharStream.getPosition() + 1;
2631 statement = Statement()
2635 DoStatement DoStatement() :
2637 final Statement action;
2638 final Expression condition;
2639 final int pos = SimpleCharStream.getPosition();
2642 <DO> action = Statement() <WHILE> condition = Condition("while")
2645 {return new DoStatement(condition,action,pos,SimpleCharStream.getPosition());}
2646 } catch (ParseException e) {
2647 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2649 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2650 errorEnd = SimpleCharStream.getPosition() + 1;
2655 ForeachStatement ForeachStatement() :
2657 Statement statement;
2658 Expression expression;
2659 final int pos = SimpleCharStream.getPosition();
2660 ArrayVariableDeclaration variable;
2666 } catch (ParseException e) {
2667 errorMessage = "'(' expected after 'foreach' keyword";
2669 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2670 errorEnd = SimpleCharStream.getPosition() + 1;
2674 expression = Expression()
2675 } catch (ParseException e) {
2676 errorMessage = "variable expected";
2678 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2679 errorEnd = SimpleCharStream.getPosition() + 1;
2684 } catch (ParseException e) {
2685 errorMessage = "'as' expected";
2687 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2688 errorEnd = SimpleCharStream.getPosition() + 1;
2692 variable = ArrayVariable()
2693 } catch (ParseException e) {
2694 errorMessage = "variable expected";
2696 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2697 errorEnd = SimpleCharStream.getPosition() + 1;
2702 } catch (ParseException e) {
2703 errorMessage = "')' expected after 'foreach' keyword";
2705 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2706 errorEnd = SimpleCharStream.getPosition() + 1;
2710 statement = Statement()
2711 } catch (ParseException e) {
2712 if (errorMessage != null) throw e;
2713 errorMessage = "statement expected";
2715 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2716 errorEnd = SimpleCharStream.getPosition() + 1;
2719 {return new ForeachStatement(expression,
2723 SimpleCharStream.getPosition());}
2727 ForStatement ForStatement() :
2730 final int pos = SimpleCharStream.getPosition();
2731 Statement[] initializations = null;
2732 Expression condition = null;
2733 Statement[] increments = null;
2735 final ArrayList list = new ArrayList();
2736 final int startBlock, endBlock;
2742 } catch (ParseException e) {
2743 errorMessage = "'(' expected after 'for' keyword";
2745 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2746 errorEnd = SimpleCharStream.getPosition() + 1;
2749 [ initializations = ForInit() ] <SEMICOLON>
2750 [ condition = Expression() ] <SEMICOLON>
2751 [ increments = StatementExpressionList() ] <RPAREN>
2753 action = Statement()
2754 {return new ForStatement(initializations,condition,increments,action,pos,SimpleCharStream.getPosition());}
2757 {startBlock = SimpleCharStream.getPosition();}
2758 (action = Statement() {list.add(action);})*
2761 setMarker(fileToParse,
2762 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
2764 pos+token.image.length(),
2766 "Line " + token.beginLine);
2767 } catch (CoreException e) {
2768 PHPeclipsePlugin.log(e);
2771 {endBlock = SimpleCharStream.getPosition();}
2774 } catch (ParseException e) {
2775 errorMessage = "'endfor' expected";
2777 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2778 errorEnd = SimpleCharStream.getPosition() + 1;
2784 Statement[] stmtsArray = new Statement[list.size()];
2785 list.toArray(stmtsArray);
2786 return new ForStatement(initializations,condition,increments,new Block(stmtsArray,startBlock,endBlock),pos,SimpleCharStream.getPosition());}
2787 } catch (ParseException e) {
2788 errorMessage = "';' expected after 'endfor' keyword";
2790 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2791 errorEnd = SimpleCharStream.getPosition() + 1;
2797 Statement[] ForInit() :
2799 Statement[] statements;
2802 LOOKAHEAD(LocalVariableDeclaration())
2803 statements = LocalVariableDeclaration()
2804 {return statements;}
2806 statements = StatementExpressionList()
2807 {return statements;}
2810 Statement[] StatementExpressionList() :
2812 final ArrayList list = new ArrayList();
2816 expr = StatementExpression() {list.add(expr);}
2817 (<COMMA> StatementExpression() {list.add(expr);})*
2819 Statement[] stmtsArray = new Statement[list.size()];
2820 list.toArray(stmtsArray);
2824 Continue ContinueStatement() :
2826 Expression expr = null;
2827 final int pos = SimpleCharStream.getPosition();
2830 <CONTINUE> [ expr = Expression() ]
2833 {return new Continue(expr,pos,SimpleCharStream.getPosition());}
2834 } catch (ParseException e) {
2835 errorMessage = "';' expected after 'continue' statement";
2837 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2838 errorEnd = SimpleCharStream.getPosition() + 1;
2843 ReturnStatement ReturnStatement() :
2845 Expression expr = null;
2846 final int pos = SimpleCharStream.getPosition();
2849 <RETURN> [ expr = Expression() ]
2852 {return new ReturnStatement(expr,pos,SimpleCharStream.getPosition());}
2853 } catch (ParseException e) {
2854 errorMessage = "';' expected after 'return' statement";
2856 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2857 errorEnd = SimpleCharStream.getPosition() + 1;