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 PHPeclipsePlugin.log(1,phpDocument.toString());
139 } catch (ParseException e) {
140 processParseException(e);
146 * This method will process the parse exception.
147 * If the error message is null, the parse exception wasn't catched and a trace is written in the log
148 * @param e the ParseException
150 private static void processParseException(final ParseException e) {
151 if (errorMessage == null) {
152 PHPeclipsePlugin.log(e);
153 errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
154 errorStart = SimpleCharStream.getPosition();
155 errorEnd = errorStart + 1;
162 * Create marker for the parse error
163 * @param e the ParseException
165 private static void setMarker(final ParseException e) {
167 if (errorStart == -1) {
168 setMarker(fileToParse,
170 SimpleCharStream.tokenBegin,
171 SimpleCharStream.tokenBegin + e.currentToken.image.length(),
173 "Line " + e.currentToken.beginLine);
175 setMarker(fileToParse,
180 "Line " + e.currentToken.beginLine);
184 } catch (CoreException e2) {
185 PHPeclipsePlugin.log(e2);
190 * Create markers according to the external parser output
192 private static void createMarkers(final String output, final IFile file) throws CoreException {
193 // delete all markers
194 file.deleteMarkers(IMarker.PROBLEM, false, 0);
199 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
200 // newer php error output (tested with 4.2.3)
201 scanLine(output, file, indx, brIndx);
206 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
207 // older php error output (tested with 4.2.3)
208 scanLine(output, file, indx, brIndx);
214 private static void scanLine(final String output,
217 final int brIndx) throws CoreException {
219 StringBuffer lineNumberBuffer = new StringBuffer(10);
221 current = output.substring(indx, brIndx);
223 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
224 int onLine = current.indexOf("on line <b>");
226 lineNumberBuffer.delete(0, lineNumberBuffer.length());
227 for (int i = onLine; i < current.length(); i++) {
228 ch = current.charAt(i);
229 if ('0' <= ch && '9' >= ch) {
230 lineNumberBuffer.append(ch);
234 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
236 Hashtable attributes = new Hashtable();
238 current = current.replaceAll("\n", "");
239 current = current.replaceAll("<b>", "");
240 current = current.replaceAll("</b>", "");
241 MarkerUtilities.setMessage(attributes, current);
243 if (current.indexOf(PARSE_ERROR_STRING) != -1)
244 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
245 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
246 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
248 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
249 MarkerUtilities.setLineNumber(attributes, lineNumber);
250 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
255 public final void parse(final String s) throws CoreException {
256 final StringReader stream = new StringReader(s);
257 if (jj_input_stream == null) {
258 jj_input_stream = new SimpleCharStream(stream, 1, 1);
264 } catch (ParseException e) {
265 processParseException(e);
270 * Call the php parse command ( php -l -f <filename> )
271 * and create markers according to the external parser output
273 public static void phpExternalParse(final IFile file) {
274 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
275 final String filename = file.getLocation().toString();
277 final String[] arguments = { filename };
278 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
279 final String command = form.format(arguments);
281 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
284 // parse the buffer to find the errors and warnings
285 createMarkers(parserResult, file);
286 } catch (CoreException e) {
287 PHPeclipsePlugin.log(e);
292 * Put a new html block in the stack.
294 public static final void createNewHTMLCode() {
295 final int currentPosition = SimpleCharStream.getPosition();
296 if (currentPosition == htmlStart) {
299 final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
300 pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
303 private static final void parse() throws ParseException {
308 PARSER_END(PHPParser)
312 <PHPSTARTSHORT : "<?"> {PHPParser.createNewHTMLCode();} : PHPPARSING
313 | <PHPSTARTLONG : "<?php"> {PHPParser.createNewHTMLCode();} : PHPPARSING
314 | <PHPECHOSTART : "<?="> {PHPParser.createNewHTMLCode();} : PHPPARSING
319 <PHPEND :"?>"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
322 /* Skip any character if we are not in php mode */
340 <PHPPARSING> SPECIAL_TOKEN :
342 "//" : IN_SINGLE_LINE_COMMENT
344 "#" : IN_SINGLE_LINE_COMMENT
346 <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
348 "/*" : IN_MULTI_LINE_COMMENT
351 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
353 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
356 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
358 <SINGLE_LINE_COMMENT_PHPEND : "?>" > : DEFAULT
364 <FORMAL_COMMENT: "*/" > : PHPPARSING
367 <IN_MULTI_LINE_COMMENT>
370 <MULTI_LINE_COMMENT: "*/" > : PHPPARSING
373 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
383 | <FUNCTION : "function">
386 | <ELSEIF : "elseif">
393 /* LANGUAGE CONSTRUCT */
398 | <INCLUDE : "include">
399 | <REQUIRE : "require">
400 | <INCLUDE_ONCE : "include_once">
401 | <REQUIRE_ONCE : "require_once">
402 | <GLOBAL : "global">
403 | <STATIC : "static">
404 | <CLASSACCESS : "->">
405 | <STATICCLASSACCESS : "::">
406 | <ARRAYASSIGN : "=>">
409 /* RESERVED WORDS AND LITERALS */
415 | <CONTINUE : "continue">
416 | <_DEFAULT : "default">
418 | <EXTENDS : "extends">
423 | <RETURN : "return">
425 | <SWITCH : "switch">
430 | <ENDWHILE : "endwhile">
431 | <ENDSWITCH: "endswitch">
433 | <ENDFOR : "endfor">
434 | <FOREACH : "foreach">
442 | <OBJECT : "object">
444 | <BOOLEAN : "boolean">
446 | <DOUBLE : "double">
449 | <INTEGER : "integer">
479 | <RSIGNEDSHIFT : ">>">
480 | <RUNSIGNEDSHIFT : ">>>">
489 <DECIMAL_LITERAL> (["l","L"])?
490 | <HEX_LITERAL> (["l","L"])?
491 | <OCTAL_LITERAL> (["l","L"])?
494 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
496 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
498 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
500 <FLOATING_POINT_LITERAL:
501 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
502 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
503 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
504 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
507 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
509 <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
542 < IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
545 ["a"-"z"] | ["A"-"Z"]
553 "_" | ["\u007f"-"\u00ff"]
578 | <EQUAL_EQUAL : "==">
583 | <BANGDOUBLEEQUAL : "!==">
584 | <TRIPLEEQUAL : "===">
591 | <PLUSASSIGN : "+=">
592 | <MINUSASSIGN : "-=">
593 | <STARASSIGN : "*=">
594 | <SLASHASSIGN : "/=">
600 | <TILDEEQUAL : "~=">
601 | <LSHIFTASSIGN : "<<=">
602 | <RSIGNEDSHIFTASSIGN : ">>=">
607 < DOLLAR_ID: <DOLLAR> <IDENTIFIER> >
616 } catch (TokenMgrError e) {
617 PHPeclipsePlugin.log(e);
618 errorStart = SimpleCharStream.getPosition();
619 errorEnd = errorStart + 1;
620 errorMessage = e.getMessage();
622 throw generateParseException();
627 * A php block is a <?= expression [;]?>
628 * or <?php somephpcode ?>
629 * or <? somephpcode ?>
633 final int start = SimpleCharStream.getPosition();
641 setMarker(fileToParse,
642 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
644 SimpleCharStream.getPosition(),
646 "Line " + token.beginLine);
647 } catch (CoreException e) {
648 PHPeclipsePlugin.log(e);
654 } catch (ParseException e) {
655 errorMessage = "'?>' expected";
657 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
658 errorEnd = SimpleCharStream.getPosition() + 1;
663 PHPEchoBlock phpEchoBlock() :
665 final Expression expr;
666 final int pos = SimpleCharStream.getPosition();
667 PHPEchoBlock echoBlock;
670 <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] <PHPEND>
672 echoBlock = new PHPEchoBlock(expr,pos,SimpleCharStream.getPosition());
673 pushOnAstNodes(echoBlock);
683 ClassDeclaration ClassDeclaration() :
685 final ClassDeclaration classDeclaration;
686 final Token className;
687 Token superclassName = null;
693 {pos = SimpleCharStream.getPosition();}
694 className = <IDENTIFIER>
695 } catch (ParseException e) {
696 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
698 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
699 errorEnd = SimpleCharStream.getPosition() + 1;
705 superclassName = <IDENTIFIER>
706 } catch (ParseException e) {
707 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
709 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
710 errorEnd = SimpleCharStream.getPosition() + 1;
715 if (superclassName == null) {
716 classDeclaration = new ClassDeclaration(currentSegment,
717 className.image.toCharArray(),
721 classDeclaration = new ClassDeclaration(currentSegment,
722 className.image.toCharArray(),
723 superclassName.image.toCharArray(),
727 currentSegment.add(classDeclaration);
728 currentSegment = classDeclaration;
730 ClassBody(classDeclaration)
731 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
732 classDeclaration.sourceEnd = SimpleCharStream.getPosition();
733 pushOnAstNodes(classDeclaration);
734 return classDeclaration;}
737 void ClassBody(ClassDeclaration classDeclaration) :
742 } catch (ParseException e) {
743 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected";
745 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
746 errorEnd = SimpleCharStream.getPosition() + 1;
749 ( ClassBodyDeclaration(classDeclaration) )*
752 } catch (ParseException e) {
753 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected";
755 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
756 errorEnd = SimpleCharStream.getPosition() + 1;
762 * A class can contain only methods and fields.
764 void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
766 MethodDeclaration method;
767 FieldDeclaration field;
770 method = MethodDeclaration() {method.setParent(classDeclaration);}
771 | field = FieldDeclaration()
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;
801 processParseException(e);
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;
1835 blockNodes = new AstNode[nbNodes];
1836 System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes);
1837 nodePtr = startIndex;
1838 return new HTMLBlock(blockNodes);}
1842 * An include statement. It's "include" an expression;
1844 InclusionStatement IncludeStatement() :
1846 final Expression expr;
1848 final int pos = SimpleCharStream.getPosition();
1849 final InclusionStatement inclusionStatement;
1852 ( <REQUIRE> {keyword = InclusionStatement.REQUIRE;}
1853 | <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;}
1854 | <INCLUDE> {keyword = InclusionStatement.INCLUDE;}
1855 | <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;})
1858 } catch (ParseException e) {
1859 if (errorMessage != null) {
1862 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
1864 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1865 errorEnd = SimpleCharStream.getPosition() + 1;
1868 {inclusionStatement = new InclusionStatement(currentSegment,
1872 currentSegment.add(inclusionStatement);
1876 } catch (ParseException e) {
1877 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
1879 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1880 errorEnd = SimpleCharStream.getPosition() + 1;
1883 {return inclusionStatement;}
1886 PrintExpression PrintExpression() :
1888 final Expression expr;
1889 final int pos = SimpleCharStream.getPosition();
1892 <PRINT> expr = Expression() {return new PrintExpression(expr,pos,SimpleCharStream.getPosition());}
1895 ListExpression ListExpression() :
1898 Expression expression = null;
1899 ArrayList list = new ArrayList();
1900 final int pos = SimpleCharStream.getPosition();
1906 } catch (ParseException e) {
1907 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
1909 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1910 errorEnd = SimpleCharStream.getPosition() + 1;
1914 expr = VariableDeclaratorId()
1917 {if (expr == null) list.add(null);}
1921 } catch (ParseException e) {
1922 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
1924 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1925 errorEnd = SimpleCharStream.getPosition() + 1;
1928 expr = VariableDeclaratorId()
1933 } catch (ParseException e) {
1934 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
1936 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1937 errorEnd = SimpleCharStream.getPosition() + 1;
1940 [ <ASSIGN> expression = Expression()
1942 String[] strings = new String[list.size()];
1943 list.toArray(strings);
1944 return new ListExpression(strings,
1947 SimpleCharStream.getPosition());}
1950 String[] strings = new String[list.size()];
1951 list.toArray(strings);
1952 return new ListExpression(strings,pos,SimpleCharStream.getPosition());}
1956 * An echo statement.
1957 * echo anyexpression (, otherexpression)*
1959 EchoStatement EchoStatement() :
1961 final ArrayList expressions = new ArrayList();
1963 final int pos = SimpleCharStream.getPosition();
1966 <ECHO> expr = Expression()
1967 {expressions.add(expr);}
1969 <COMMA> expr = Expression()
1970 {expressions.add(expr);}
1975 Expression[] exprs = new Expression[expressions.size()];
1976 expressions.toArray(exprs);
1977 return new EchoStatement(exprs,pos);}
1978 } catch (ParseException e) {
1979 if (e.currentToken.next.kind != 4) {
1980 errorMessage = "';' expected after 'echo' statement";
1982 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1983 errorEnd = SimpleCharStream.getPosition() + 1;
1989 GlobalStatement GlobalStatement() :
1991 final int pos = SimpleCharStream.getPosition();
1993 ArrayList vars = new ArrayList();
1994 GlobalStatement global;
1998 expr = VariableDeclaratorId()
2001 expr = VariableDeclaratorId()
2007 String[] strings = new String[vars.size()];
2008 vars.toArray(strings);
2009 global = new GlobalStatement(currentSegment,
2012 SimpleCharStream.getPosition());
2013 currentSegment.add(global);
2015 } catch (ParseException e) {
2016 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2018 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2019 errorEnd = SimpleCharStream.getPosition() + 1;
2024 StaticStatement StaticStatement() :
2026 final int pos = SimpleCharStream.getPosition();
2027 final ArrayList vars = new ArrayList();
2028 VariableDeclaration expr;
2031 <STATIC> expr = VariableDeclarator() {vars.add(new String(expr.name));}
2032 (<COMMA> expr = VariableDeclarator() {vars.add(new String(expr.name));})*
2036 String[] strings = new String[vars.size()];
2037 vars.toArray(strings);
2038 return new StaticStatement(strings,
2040 SimpleCharStream.getPosition());}
2041 } catch (ParseException e) {
2042 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2044 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2045 errorEnd = SimpleCharStream.getPosition() + 1;
2050 LabeledStatement LabeledStatement() :
2052 final int pos = SimpleCharStream.getPosition();
2054 final Statement statement;
2057 label = <IDENTIFIER> <COLON> statement = Statement()
2058 {return new LabeledStatement(label.image.toCharArray(),statement,pos,SimpleCharStream.getPosition());}
2070 final int pos = SimpleCharStream.getPosition();
2071 final ArrayList list = new ArrayList();
2072 Statement statement;
2077 } catch (ParseException e) {
2078 errorMessage = "'{' expected";
2080 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2081 errorEnd = SimpleCharStream.getPosition() + 1;
2084 ( statement = BlockStatement() {list.add(statement);}
2085 | statement = htmlBlock() {list.add(statement);})*
2088 } catch (ParseException e) {
2089 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2091 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2092 errorEnd = SimpleCharStream.getPosition() + 1;
2096 Statement[] statements = new Statement[list.size()];
2097 list.toArray(statements);
2098 return new Block(statements,pos,SimpleCharStream.getPosition());}
2101 Statement BlockStatement() :
2103 final Statement statement;
2106 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
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;