4 CHOICE_AMBIGUITY_CHECK = 2;
5 OTHER_AMBIGUITY_CHECK = 1;
8 DEBUG_LOOKAHEAD = false;
9 DEBUG_TOKEN_MANAGER = false;
10 OPTIMIZE_TOKEN_MANAGER = false;
11 ERROR_REPORTING = true;
12 JAVA_UNICODE_ESCAPE = false;
13 UNICODE_INPUT = false;
15 USER_TOKEN_MANAGER = false;
16 USER_CHAR_STREAM = false;
18 BUILD_TOKEN_MANAGER = true;
20 FORCE_LA_CHECK = false;
21 COMMON_TOKEN_ACTION = true;
24 PARSER_BEGIN(PHPParser)
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IMarker;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.ui.texteditor.MarkerUtilities;
31 import org.eclipse.jface.preference.IPreferenceStore;
33 import java.util.Hashtable;
34 import java.util.ArrayList;
35 import java.io.StringReader;
37 import java.text.MessageFormat;
39 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
40 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
41 import net.sourceforge.phpdt.internal.compiler.ast.*;
42 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
43 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
44 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
45 import net.sourceforge.phpdt.internal.corext.Assert;
49 * This php parser is inspired by the Java 1.2 grammar example
50 * given with JavaCC. You can get JavaCC at http://www.webgain.com
51 * You can test the parser with the PHPParserTestCase2.java
52 * @author Matthieu Casanova
54 public final class PHPParser extends PHPParserSuperclass {
56 //todo : fix the variables names bug
57 //todo : handle tilde operator
60 /** The current segment. */
61 private static OutlineableWithChildren currentSegment;
63 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
64 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
65 static PHPOutlineInfo outlineInfo;
67 /** The error level of the current ParseException. */
68 private static int errorLevel = ERROR;
69 /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
70 private static String errorMessage;
72 private static int errorStart = -1;
73 private static int errorEnd = -1;
74 private static PHPDocument phpDocument;
76 private static final String SYNTAX_ERROR_CHAR = "syntax error";
78 * The point where html starts.
79 * It will be used by the token manager to create HTMLCode objects
81 public static int htmlStart;
84 private final static int AstStackIncrement = 100;
85 /** The stack of node. */
86 private static AstNode[] nodes;
87 /** The cursor in expression stack. */
88 private static int nodePtr;
90 public static final boolean PARSER_DEBUG = false;
92 public final void setFileToParse(final IFile fileToParse) {
93 PHPParser.fileToParse = fileToParse;
99 public PHPParser(final IFile fileToParse) {
100 this(new StringReader(""));
101 PHPParser.fileToParse = fileToParse;
104 public static final void phpParserTester(final String strEval) throws ParseException {
105 final StringReader stream = new StringReader(strEval);
106 if (jj_input_stream == null) {
107 jj_input_stream = new SimpleCharStream(stream, 1, 1);
109 ReInit(new StringReader(strEval));
111 phpDocument = new PHPDocument(null,"_root".toCharArray());
112 currentSegment = phpDocument;
113 outlineInfo = new PHPOutlineInfo(null, currentSegment);
114 PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
118 public static final void htmlParserTester(final File fileName) throws FileNotFoundException, ParseException {
119 final Reader stream = new FileReader(fileName);
120 if (jj_input_stream == null) {
121 jj_input_stream = new SimpleCharStream(stream, 1, 1);
125 phpDocument = new PHPDocument(null,"_root".toCharArray());
126 currentSegment = phpDocument;
127 outlineInfo = new PHPOutlineInfo(null, currentSegment);
131 public static final void htmlParserTester(final String strEval) throws ParseException {
132 final StringReader stream = new StringReader(strEval);
133 if (jj_input_stream == null) {
134 jj_input_stream = new SimpleCharStream(stream, 1, 1);
138 phpDocument = new PHPDocument(null,"_root".toCharArray());
139 currentSegment = phpDocument;
140 outlineInfo = new PHPOutlineInfo(null, currentSegment);
145 * Reinitialize the parser.
147 private static final void init() {
148 nodes = new AstNode[AstStackIncrement];
154 * Add an php node on the stack.
155 * @param node the node that will be added to the stack
157 private static final void pushOnAstNodes(final AstNode node) {
159 nodes[++nodePtr] = node;
160 } catch (IndexOutOfBoundsException e) {
161 final int oldStackLength = nodes.length;
162 final AstNode[] oldStack = nodes;
163 nodes = new AstNode[oldStackLength + AstStackIncrement];
164 System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
165 nodePtr = oldStackLength;
166 nodes[nodePtr] = node;
170 public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
171 phpDocument = new PHPDocument(parent,"_root".toCharArray());
172 currentSegment = phpDocument;
173 outlineInfo = new PHPOutlineInfo(parent, currentSegment);
174 final StringReader stream = new StringReader(s);
175 if (jj_input_stream == null) {
176 jj_input_stream = new SimpleCharStream(stream, 1, 1);
182 phpDocument.nodes = new AstNode[nodes.length];
183 System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
184 if (PHPeclipsePlugin.DEBUG) {
185 PHPeclipsePlugin.log(1,phpDocument.toString());
187 } catch (ParseException e) {
188 processParseException(e);
194 * This function will throw the exception if we are in debug mode
195 * and process it if we are in production mode.
196 * this should be fast since the PARSER_DEBUG is static final so the difference will be at compile time
197 * @param e the exception
198 * @throws ParseException the thrown exception
200 private static void processParseExceptionDebug(final ParseException e) throws ParseException {
204 processParseException(e);
207 * This method will process the parse exception.
208 * If the error message is null, the parse exception wasn't catched and a trace is written in the log
209 * @param e the ParseException
211 private static void processParseException(final ParseException e) {
212 if (errorMessage == null) {
213 PHPeclipsePlugin.log(e);
214 errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
215 errorStart = e.currentToken.sourceStart;
216 errorEnd = e.currentToken.sourceEnd;
220 // if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e);
224 * Create marker for the parse error.
225 * @param e the ParseException
227 private static void setMarker(final ParseException e) {
229 if (errorStart == -1) {
230 setMarker(fileToParse,
232 e.currentToken.sourceStart,
233 e.currentToken.sourceEnd,
235 "Line " + e.currentToken.beginLine+", "+e.currentToken.sourceStart+":"+e.currentToken.sourceEnd);
237 setMarker(fileToParse,
242 "Line " + e.currentToken.beginLine+", "+errorStart+":"+errorEnd);
246 } catch (CoreException e2) {
247 PHPeclipsePlugin.log(e2);
251 private static void scanLine(final String output,
254 final int brIndx) throws CoreException {
256 final StringBuffer lineNumberBuffer = new StringBuffer(10);
258 current = output.substring(indx, brIndx);
260 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
261 final int onLine = current.indexOf("on line <b>");
263 lineNumberBuffer.delete(0, lineNumberBuffer.length());
264 for (int i = onLine; i < current.length(); i++) {
265 ch = current.charAt(i);
266 if ('0' <= ch && '9' >= ch) {
267 lineNumberBuffer.append(ch);
271 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
273 final Hashtable attributes = new Hashtable();
275 current = current.replaceAll("\n", "");
276 current = current.replaceAll("<b>", "");
277 current = current.replaceAll("</b>", "");
278 MarkerUtilities.setMessage(attributes, current);
280 if (current.indexOf(PARSE_ERROR_STRING) != -1)
281 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
282 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
283 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
285 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
286 MarkerUtilities.setLineNumber(attributes, lineNumber);
287 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
292 public final void parse(final String s) {
293 final StringReader stream = new StringReader(s);
294 if (jj_input_stream == null) {
295 jj_input_stream = new SimpleCharStream(stream, 1, 1);
301 } catch (ParseException e) {
302 processParseException(e);
307 * Call the php parse command ( php -l -f <filename> )
308 * and create markers according to the external parser output
310 public static void phpExternalParse(final IFile file) {
311 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
312 final String filename = file.getLocation().toString();
314 final String[] arguments = { filename };
315 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
316 final String command = form.format(arguments);
318 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
321 // parse the buffer to find the errors and warnings
322 createMarkers(parserResult, file);
323 } catch (CoreException e) {
324 PHPeclipsePlugin.log(e);
329 * Put a new html block in the stack.
331 public static final void createNewHTMLCode() {
332 final int currentPosition = token.sourceStart;
333 if (currentPosition == htmlStart ||
334 currentPosition > SimpleCharStream.currentBuffer.length()) {
337 final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
338 pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
341 /** Create a new task. */
342 public static final void createNewTask() {
343 final int currentPosition = token.sourceStart;
344 final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3,
345 SimpleCharStream.currentBuffer.indexOf("\n",
349 setMarker(fileToParse,
351 SimpleCharStream.getBeginLine(),
353 "Line "+SimpleCharStream.getBeginLine());
354 } catch (CoreException e) {
355 PHPeclipsePlugin.log(e);
360 private static final void parse() throws ParseException {
365 PARSER_END(PHPParser)
369 // CommonTokenAction: use the begins/ends fields added to the Jack
370 // CharStream class to set corresponding fields in each Token (which was
371 // also extended with new fields). By default Jack doesn't supply absolute
372 // offsets, just line/column offsets
373 static void CommonTokenAction(Token t) {
374 t.sourceStart = input_stream.beginOffset;
375 t.sourceEnd = input_stream.endOffset;
376 } // CommonTokenAction
381 <PHPSTARTSHORT : "<?"> {PHPParser.createNewHTMLCode();} : PHPPARSING
382 | <PHPSTARTLONG : "<?php"> {PHPParser.createNewHTMLCode();} : PHPPARSING
383 | <PHPECHOSTART : "<?="> {PHPParser.createNewHTMLCode();} : PHPPARSING
386 <PHPPARSING, IN_SINGLE_LINE_COMMENT> TOKEN :
388 <PHPEND :"?>"> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT
391 /* Skip any character if we are not in php mode */
409 <PHPPARSING> SPECIAL_TOKEN :
411 "//" : IN_SINGLE_LINE_COMMENT
412 | "#" : IN_SINGLE_LINE_COMMENT
413 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
414 | "/*" : IN_MULTI_LINE_COMMENT
417 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
419 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
423 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
425 "todo" {PHPParser.createNewTask();}
428 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
433 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
438 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
448 | <FUNCTION : "function">
451 | <ELSEIF : "elseif">
458 /* LANGUAGE CONSTRUCT */
463 | <INCLUDE : "include">
464 | <REQUIRE : "require">
465 | <INCLUDE_ONCE : "include_once">
466 | <REQUIRE_ONCE : "require_once">
467 | <GLOBAL : "global">
468 | <DEFINE : "define">
469 | <STATIC : "static">
470 | <CLASSACCESS : "->">
471 | <STATICCLASSACCESS : "::">
472 | <ARRAYASSIGN : "=>">
475 /* RESERVED WORDS AND LITERALS */
481 | <CONTINUE : "continue">
482 | <_DEFAULT : "default">
484 | <EXTENDS : "extends">
489 | <RETURN : "return">
491 | <SWITCH : "switch">
496 | <ENDWHILE : "endwhile">
497 | <ENDSWITCH: "endswitch">
499 | <ENDFOR : "endfor">
500 | <FOREACH : "foreach">
508 | <OBJECT : "object">
510 | <BOOLEAN : "boolean">
512 | <DOUBLE : "double">
515 | <INTEGER : "integer">
535 | <MINUS_MINUS : "--">
545 | <RSIGNEDSHIFT : ">>">
546 | <RUNSIGNEDSHIFT : ">>>">
555 <DECIMAL_LITERAL> (["l","L"])?
556 | <HEX_LITERAL> (["l","L"])?
557 | <OCTAL_LITERAL> (["l","L"])?
560 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
562 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
564 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
566 <FLOATING_POINT_LITERAL:
567 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
568 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
569 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
570 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
573 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
575 <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
576 | <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
577 | <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
578 | <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
585 <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
588 ["a"-"z"] | ["A"-"Z"]
596 "_" | ["\u007f"-"\u00ff"]
621 | <EQUAL_EQUAL : "==">
626 | <BANGDOUBLEEQUAL : "!==">
627 | <TRIPLEEQUAL : "===">
634 | <PLUSASSIGN : "+=">
635 | <MINUSASSIGN : "-=">
636 | <STARASSIGN : "*=">
637 | <SLASHASSIGN : "/=">
643 | <TILDEEQUAL : "~=">
644 | <LSHIFTASSIGN : "<<=">
645 | <RSIGNEDSHIFTASSIGN : ">>=">
650 <DOLLAR_ID: <DOLLAR> <IDENTIFIER>>
665 {PHPParser.createNewHTMLCode();}
666 } catch (TokenMgrError e) {
667 PHPeclipsePlugin.log(e);
668 errorStart = SimpleCharStream.beginOffset;
669 errorEnd = SimpleCharStream.endOffset;
670 errorMessage = e.getMessage();
672 throw generateParseException();
677 * A php block is a <?= expression [;]?>
678 * or <?php somephpcode ?>
679 * or <? somephpcode ?>
683 final PHPEchoBlock phpEchoBlock;
687 phpEchoBlock = phpEchoBlock()
688 {pushOnAstNodes(phpEchoBlock);}
691 | token = <PHPSTARTSHORT>
693 setMarker(fileToParse,
694 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
698 "Line " + token.beginLine);
699 } catch (CoreException e) {
700 PHPeclipsePlugin.log(e);
706 } catch (ParseException e) {
707 errorMessage = "'?>' expected";
709 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
710 errorEnd = SimpleCharStream.getPosition() + 1;
711 processParseExceptionDebug(e);
715 PHPEchoBlock phpEchoBlock() :
717 final Expression expr;
718 final PHPEchoBlock echoBlock;
719 final Token token, token2;
722 token = <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
724 echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
725 pushOnAstNodes(echoBlock);
735 ClassDeclaration ClassDeclaration() :
737 final ClassDeclaration classDeclaration;
738 Token className = null;
739 final Token superclassName, token, extendsToken;
740 String classNameImage = SYNTAX_ERROR_CHAR;
741 String superclassNameImage = null;
746 className = <IDENTIFIER>
747 {classNameImage = className.image;}
748 } catch (ParseException e) {
749 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
751 errorStart = token.sourceEnd+1;
752 errorEnd = token.sourceEnd+1;
753 processParseExceptionDebug(e);
756 extendsToken = <EXTENDS>
758 superclassName = <IDENTIFIER>
759 {superclassNameImage = superclassName.image;}
760 } catch (ParseException e) {
761 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
763 errorStart = extendsToken.sourceEnd+1;
764 errorEnd = extendsToken.sourceEnd+1;
765 processParseExceptionDebug(e);
766 superclassNameImage = SYNTAX_ERROR_CHAR;
771 if (className == null) {
772 start = token.sourceStart;
773 end = token.sourceEnd;
775 start = className.sourceStart;
776 end = className.sourceEnd;
778 if (superclassNameImage == null) {
780 classDeclaration = new ClassDeclaration(currentSegment,
785 classDeclaration = new ClassDeclaration(currentSegment,
791 currentSegment.add(classDeclaration);
792 currentSegment = classDeclaration;
794 ClassBody(classDeclaration)
795 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
796 classDeclaration.sourceEnd = SimpleCharStream.getPosition();
797 pushOnAstNodes(classDeclaration);
798 return classDeclaration;}
801 void ClassBody(final ClassDeclaration classDeclaration) :
806 } catch (ParseException e) {
807 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
809 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
810 errorEnd = SimpleCharStream.getPosition() + 1;
811 processParseExceptionDebug(e);
813 ( ClassBodyDeclaration(classDeclaration) )*
816 } catch (ParseException e) {
817 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
819 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
820 errorEnd = SimpleCharStream.getPosition() + 1;
821 processParseExceptionDebug(e);
826 * A class can contain only methods and fields.
828 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
830 final MethodDeclaration method;
831 final FieldDeclaration field;
834 method = MethodDeclaration() {method.analyzeCode();
835 classDeclaration.addMethod(method);}
836 | field = FieldDeclaration() {classDeclaration.addField(field);}
840 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
841 * it is only used by ClassBodyDeclaration()
843 FieldDeclaration FieldDeclaration() :
845 VariableDeclaration variableDeclaration;
846 final VariableDeclaration[] list;
847 final ArrayList arrayList = new ArrayList();
853 token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
855 arrayList.add(variableDeclaration);
856 outlineInfo.addVariable(variableDeclaration.name());
857 pos = variableDeclaration.sourceEnd;
860 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
862 arrayList.add(variableDeclaration);
863 outlineInfo.addVariable(variableDeclaration.name());
864 pos = variableDeclaration.sourceEnd;
869 } catch (ParseException e) {
870 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
874 processParseExceptionDebug(e);
877 {list = new VariableDeclaration[arrayList.size()];
878 arrayList.toArray(list);
880 if (token2 == null) {
881 end = list[list.length-1].sourceEnd;
883 end = token2.sourceEnd;
885 return new FieldDeclaration(list,
892 * a strict variable declarator : there cannot be a suffix here.
893 * It will be used by fields and formal parameters
895 VariableDeclaration VariableDeclaratorNoSuffix() :
898 Expression initializer = null;
902 varName = <DOLLAR_ID>
904 assignToken = <ASSIGN>
906 initializer = VariableInitializer()
907 } catch (ParseException e) {
908 errorMessage = "Literal expression expected in variable initializer";
910 errorStart = assignToken.sourceEnd +1;
911 errorEnd = assignToken.sourceEnd +1;
912 processParseExceptionDebug(e);
916 if (initializer == null) {
917 return new VariableDeclaration(currentSegment,
918 new Variable(varName.image.substring(1),
919 varName.sourceStart+1,
920 varName.sourceEnd+1),
921 varName.sourceStart+1,
922 varName.sourceEnd+1);
924 return new VariableDeclaration(currentSegment,
925 new Variable(varName.image.substring(1),
926 varName.sourceStart+1,
927 varName.sourceEnd+1),
929 VariableDeclaration.EQUAL,
930 varName.sourceStart+1);
935 * this will be used by static statement
937 VariableDeclaration VariableDeclarator() :
939 final AbstractVariable variable;
940 Expression initializer = null;
944 variable = VariableDeclaratorId()
948 initializer = VariableInitializer()
949 } catch (ParseException e) {
950 errorMessage = "Literal expression expected in variable initializer";
952 errorStart = token.sourceEnd+1;
953 errorEnd = token.sourceEnd+1;
954 processParseExceptionDebug(e);
958 if (initializer == null) {
959 return new VariableDeclaration(currentSegment,
961 variable.sourceStart,
964 return new VariableDeclaration(currentSegment,
967 VariableDeclaration.EQUAL,
968 variable.sourceStart);
974 * @return the variable name (with suffix)
976 AbstractVariable VariableDeclaratorId() :
979 AbstractVariable expression = null;
986 expression = VariableSuffix(var)
989 if (expression == null) {
994 } catch (ParseException e) {
995 errorMessage = "'$' expected for variable identifier";
997 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
998 errorEnd = SimpleCharStream.getPosition() + 1;
1004 * Return a variablename without the $.
1005 * @return a variable name
1007 Variable Variable():
1009 final StringBuffer buff;
1010 Expression expression = null;
1017 [<LBRACE> expression = Expression() <RBRACE>]
1019 if (expression == null) {
1020 return new Variable(token.image.substring(1),
1021 token.sourceStart+1,
1024 String s = expression.toStringExpression();
1025 buff = new StringBuffer(token.image.length()+s.length()+2);
1026 buff.append(token.image);
1030 s = buff.toString();
1031 return new Variable(s,token.sourceStart+1,token.sourceEnd+1);
1035 expr = VariableName()
1036 {return new Variable(expr,token.sourceStart,expr.sourceEnd);}
1039 Variable Variable() :
1041 Variable variable = null;
1045 token = <DOLLAR_ID> [variable = Var(token)]
1047 if (variable == null) {
1048 return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
1050 final StringBuffer buff = new StringBuffer();
1051 buff.append(token.image.substring(1));
1052 buff.append(variable.toStringExpression());
1053 return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1);
1056 token = <DOLLAR> variable = Var(token)
1058 return new Variable(variable,token.sourceStart,variable.sourceEnd);
1062 Variable Var(final Token dollar) :
1064 Variable variable = null;
1066 ConstantIdentifier constant;
1069 token = <DOLLAR_ID> [variable = Var(token)]
1070 {if (variable == null) {
1071 return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
1073 final StringBuffer buff = new StringBuffer();
1074 buff.append(token.image.substring(1));
1075 buff.append(variable.toStringExpression());
1076 return new Variable(buff.toString(),dollar.sourceStart,variable.sourceEnd);
1079 LOOKAHEAD(<DOLLAR> <DOLLAR>)
1080 token = <DOLLAR> variable = Var(token)
1081 {return new Variable(variable,dollar.sourceStart,variable.sourceEnd);}
1083 constant = VariableName()
1084 {return new Variable(constant.name,dollar.sourceStart,constant.sourceEnd);}
1088 * A Variable name (without the $)
1089 * @return a variable name String
1091 ConstantIdentifier VariableName():
1093 final StringBuffer buff;
1095 Expression expression = null;
1097 Token token2 = null;
1100 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1101 {expr = expression.toStringExpression();
1102 buff = new StringBuffer(expr.length()+2);
1106 expr = buff.toString();
1107 return new ConstantIdentifier(expr,
1113 token = <IDENTIFIER>
1114 [<LBRACE> expression = Expression() token2 = <RBRACE>]
1116 if (expression == null) {
1117 return new ConstantIdentifier(token.image,
1121 expr = expression.toStringExpression();
1122 buff = new StringBuffer(token.image.length()+expr.length()+2);
1123 buff.append(token.image);
1127 expr = buff.toString();
1128 return new ConstantIdentifier(expr,
1134 var = VariableName()
1136 return new Variable(var,
1143 return new Variable(token.image,
1144 token.sourceStart+1,
1149 Expression VariableInitializer() :
1151 final Expression expr;
1152 final Token token, token2;
1158 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1159 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1161 token2.sourceStart);}
1163 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1164 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1166 token2.sourceStart);}
1168 expr = ArrayDeclarator()
1171 token = <IDENTIFIER>
1172 {return new ConstantIdentifier(token);}
1175 ArrayVariableDeclaration ArrayVariable() :
1177 final Expression expr,expr2;
1182 <ARRAYASSIGN> expr2 = Expression()
1183 {return new ArrayVariableDeclaration(expr,expr2);}
1185 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1188 ArrayVariableDeclaration[] ArrayInitializer() :
1190 ArrayVariableDeclaration expr;
1191 final ArrayList list = new ArrayList();
1196 expr = ArrayVariable()
1198 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1203 <COMMA> {list.add(null);}
1207 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1213 * A Method Declaration.
1214 * <b>function</b> MetodDeclarator() Block()
1216 MethodDeclaration MethodDeclaration() :
1218 final MethodDeclaration functionDeclaration;
1220 final OutlineableWithChildren seg = currentSegment;
1226 functionDeclaration = MethodDeclarator(token.sourceStart)
1227 {outlineInfo.addVariable(functionDeclaration.name);}
1228 } catch (ParseException e) {
1229 if (errorMessage != null) throw e;
1230 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1232 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1233 errorEnd = SimpleCharStream.getPosition() + 1;
1236 {currentSegment = functionDeclaration;}
1238 {functionDeclaration.statements = block.statements;
1239 currentSegment = seg;
1240 return functionDeclaration;}
1244 * A MethodDeclarator.
1245 * [&] IDENTIFIER(parameters ...).
1246 * @return a function description for the outline
1248 MethodDeclaration MethodDeclarator(final int start) :
1250 Token identifier = null;
1251 Token reference = null;
1252 final Hashtable formalParameters = new Hashtable();
1253 String identifierChar = SYNTAX_ERROR_CHAR;
1257 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1259 identifier = <IDENTIFIER>
1261 identifierChar = identifier.image;
1262 end = identifier.sourceEnd;
1264 } catch (ParseException e) {
1265 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1267 errorStart = e.currentToken.sourceEnd;
1268 errorEnd = e.currentToken.next.sourceStart;
1269 processParseExceptionDebug(e);
1271 end = FormalParameters(formalParameters)
1273 int nameStart, nameEnd;
1274 if (identifier == null) {
1275 if (reference == null) {
1276 nameStart = start + 9;
1277 nameEnd = start + 10;
1279 nameStart = reference.sourceEnd + 1;
1280 nameEnd = reference.sourceEnd + 2;
1283 nameStart = identifier.sourceStart;
1284 nameEnd = identifier.sourceEnd;
1286 return new MethodDeclaration(currentSegment,
1298 * FormalParameters follows method identifier.
1299 * (FormalParameter())
1301 int FormalParameters(final Hashtable parameters) :
1303 VariableDeclaration var;
1305 Token tok = PHPParser.token;
1306 int end = tok.sourceEnd;
1311 {end = tok.sourceEnd;}
1312 } catch (ParseException e) {
1313 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1315 errorStart = e.currentToken.next.sourceStart;
1316 errorEnd = e.currentToken.next.sourceEnd;
1317 processParseExceptionDebug(e);
1320 var = FormalParameter()
1321 {parameters.put(var.name(),var);end = var.sourceEnd;}
1323 <COMMA> var = FormalParameter()
1324 {parameters.put(var.name(),var);end = var.sourceEnd;}
1329 {end = token.sourceEnd;}
1330 } catch (ParseException e) {
1331 errorMessage = "')' expected";
1333 errorStart = e.currentToken.next.sourceStart;
1334 errorEnd = e.currentToken.next.sourceEnd;
1335 processParseExceptionDebug(e);
1341 * A formal parameter.
1342 * $varname[=value] (,$varname[=value])
1344 VariableDeclaration FormalParameter() :
1346 final VariableDeclaration variableDeclaration;
1350 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1352 if (token != null) {
1353 variableDeclaration.setReference(true);
1355 return variableDeclaration;}
1358 ConstantIdentifier Type() :
1359 {final Token token;}
1361 token = <STRING> {return new ConstantIdentifier(token);}
1362 | token = <BOOL> {return new ConstantIdentifier(token);}
1363 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1364 | token = <REAL> {return new ConstantIdentifier(token);}
1365 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1366 | token = <FLOAT> {return new ConstantIdentifier(token);}
1367 | token = <INT> {return new ConstantIdentifier(token);}
1368 | token = <INTEGER> {return new ConstantIdentifier(token);}
1369 | token = <OBJECT> {return new ConstantIdentifier(token);}
1372 Expression Expression() :
1374 final Expression expr;
1375 Expression initializer = null;
1376 int assignOperator = -1;
1380 expr = ConditionalExpression()
1382 assignOperator = AssignmentOperator()
1384 initializer = Expression()
1385 } catch (ParseException e) {
1386 if (errorMessage != null) {
1389 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1391 errorEnd = SimpleCharStream.getPosition();
1396 if (assignOperator != -1) {// todo : change this, very very bad :(
1397 if (expr instanceof AbstractVariable) {
1398 return new VariableDeclaration(currentSegment,
1399 (AbstractVariable) expr,
1402 initializer.sourceEnd);
1404 String varName = expr.toStringExpression().substring(1);
1405 return new VariableDeclaration(currentSegment,
1406 new Variable(varName,
1410 initializer.sourceEnd);
1414 | expr = ExpressionWBang() {return expr;}
1417 Expression ExpressionWBang() :
1419 final Expression expr;
1423 token = <BANG> expr = ExpressionWBang()
1424 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1425 | expr = ExpressionNoBang() {return expr;}
1428 Expression ExpressionNoBang() :
1433 expr = ListExpression() {return expr;}
1435 expr = PrintExpression() {return expr;}
1439 * Any assignement operator.
1440 * @return the assignement operator id
1442 int AssignmentOperator() :
1445 <ASSIGN> {return VariableDeclaration.EQUAL;}
1446 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1447 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1448 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1449 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1450 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1451 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1452 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1453 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1454 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1455 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1456 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1457 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1460 Expression ConditionalExpression() :
1462 final Expression expr;
1463 Expression expr2 = null;
1464 Expression expr3 = null;
1467 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1469 if (expr3 == null) {
1472 return new ConditionalExpression(expr,expr2,expr3);
1476 Expression ConditionalOrExpression() :
1478 Expression expr,expr2;
1482 expr = ConditionalAndExpression()
1485 <OR_OR> {operator = OperatorIds.OR_OR;}
1486 | <_ORL> {operator = OperatorIds.ORL;}
1488 expr2 = ConditionalAndExpression()
1490 expr = new BinaryExpression(expr,expr2,operator);
1496 Expression ConditionalAndExpression() :
1498 Expression expr,expr2;
1502 expr = ConcatExpression()
1504 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1505 | <_ANDL> {operator = OperatorIds.ANDL;})
1506 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1511 Expression ConcatExpression() :
1513 Expression expr,expr2;
1516 expr = InclusiveOrExpression()
1518 <DOT> expr2 = InclusiveOrExpression()
1519 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1524 Expression InclusiveOrExpression() :
1526 Expression expr,expr2;
1529 expr = ExclusiveOrExpression()
1530 (<BIT_OR> expr2 = ExclusiveOrExpression()
1531 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1536 Expression ExclusiveOrExpression() :
1538 Expression expr,expr2;
1541 expr = AndExpression()
1543 <XOR> expr2 = AndExpression()
1544 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1549 Expression AndExpression() :
1551 Expression expr,expr2;
1554 expr = EqualityExpression()
1557 <BIT_AND> expr2 = EqualityExpression()
1558 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1563 Expression EqualityExpression() :
1565 Expression expr,expr2;
1570 expr = RelationalExpression()
1572 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1573 | token = <DIF> {operator = OperatorIds.DIF;}
1574 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1575 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1576 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1579 expr2 = RelationalExpression()
1580 } catch (ParseException e) {
1581 if (errorMessage != null) {
1584 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1586 errorStart = token.sourceEnd +1;
1587 errorEnd = token.sourceEnd +1;
1588 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1589 processParseExceptionDebug(e);
1592 expr = new BinaryExpression(expr,expr2,operator);
1598 Expression RelationalExpression() :
1600 Expression expr,expr2;
1604 expr = ShiftExpression()
1606 ( <LT> {operator = OperatorIds.LESS;}
1607 | <GT> {operator = OperatorIds.GREATER;}
1608 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1609 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1610 expr2 = ShiftExpression()
1611 {expr = new BinaryExpression(expr,expr2,operator);}
1616 Expression ShiftExpression() :
1618 Expression expr,expr2;
1622 expr = AdditiveExpression()
1624 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1625 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1626 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1627 expr2 = AdditiveExpression()
1628 {expr = new BinaryExpression(expr,expr2,operator);}
1633 Expression AdditiveExpression() :
1635 Expression expr,expr2;
1639 expr = MultiplicativeExpression()
1642 ( <PLUS> {operator = OperatorIds.PLUS;}
1643 | <MINUS> {operator = OperatorIds.MINUS;}
1645 expr2 = MultiplicativeExpression()
1646 {expr = new BinaryExpression(expr,expr2,operator);}
1651 Expression MultiplicativeExpression() :
1653 Expression expr,expr2;
1658 expr = UnaryExpression()
1659 } catch (ParseException e) {
1660 if (errorMessage != null) throw e;
1661 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1663 errorStart = PHPParser.token.sourceStart;
1664 errorEnd = PHPParser.token.sourceEnd;
1668 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1669 | <SLASH> {operator = OperatorIds.DIVIDE;}
1670 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1671 expr2 = UnaryExpression()
1672 {expr = new BinaryExpression(expr,expr2,operator);}
1678 * An unary expression starting with @, & or nothing
1680 Expression UnaryExpression() :
1682 final Expression expr;
1685 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1686 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1688 expr = AtNotUnaryExpression() {return expr;}
1692 * An expression prefixed (or not) by one or more @ and !.
1693 * @return the expression
1695 Expression AtNotUnaryExpression() :
1697 final Expression expr;
1702 expr = AtNotUnaryExpression()
1703 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1706 expr = AtNotUnaryExpression()
1707 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1709 expr = UnaryExpressionNoPrefix()
1713 Expression UnaryExpressionNoPrefix() :
1715 final Expression expr;
1719 token = <PLUS> expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr,
1721 token.sourceStart);}
1723 token = <MINUS> expr = AtNotUnaryExpression() {return new PrefixedUnaryExpression(expr,
1725 token.sourceStart);}
1727 expr = PreIncDecExpression()
1730 expr = UnaryExpressionNotPlusMinus()
1735 Expression PreIncDecExpression() :
1737 final Expression expr;
1743 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1745 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1747 expr = PrimaryExpression()
1748 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1751 Expression UnaryExpressionNotPlusMinus() :
1753 final Expression expr;
1756 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1757 expr = CastExpression() {return expr;}
1758 | expr = PostfixExpression() {return expr;}
1759 | expr = Literal() {return expr;}
1760 | <LPAREN> expr = Expression()
1763 } catch (ParseException e) {
1764 errorMessage = "')' expected";
1766 errorStart = expr.sourceEnd +1;
1767 errorEnd = expr.sourceEnd +1;
1768 processParseExceptionDebug(e);
1773 CastExpression CastExpression() :
1775 final ConstantIdentifier type;
1776 final Expression expr;
1777 final Token token,token1;
1784 token = <ARRAY> {type = new ConstantIdentifier(token);}
1786 <RPAREN> expr = UnaryExpression()
1787 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1790 Expression PostfixExpression() :
1792 final Expression expr;
1797 expr = PrimaryExpression()
1799 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1801 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1804 if (operator == -1) {
1807 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1811 Expression PrimaryExpression() :
1817 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1820 expr = ArrayDeclarator()
1824 Expression refPrimaryExpression(final Token reference) :
1827 Expression expr2 = null;
1828 final Token identifier;
1831 identifier = <IDENTIFIER>
1833 expr = new ConstantIdentifier(identifier);
1836 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1837 {expr = new ClassAccess(expr,
1839 ClassAccess.STATIC);}
1841 [ expr2 = Arguments(expr) ]
1843 if (expr2 == null) {
1844 if (reference != null) {
1845 ParseException e = generateParseException();
1846 errorMessage = "you cannot use a constant by reference";
1848 errorStart = reference.sourceStart;
1849 errorEnd = reference.sourceEnd;
1850 processParseExceptionDebug(e);
1857 expr = VariableDeclaratorId() //todo use the reference parameter ...
1858 [ expr = Arguments(expr) ]
1862 expr = ClassIdentifier()
1865 if (reference == null) {
1866 start = token.sourceStart;
1868 start = reference.sourceStart;
1870 expr = new ClassInstantiation(expr,
1874 [ expr = Arguments(expr) ]
1879 * An array declarator.
1883 ArrayInitializer ArrayDeclarator() :
1885 final ArrayVariableDeclaration[] vars;
1889 token = <ARRAY> vars = ArrayInitializer()
1890 {return new ArrayInitializer(vars,
1892 PHPParser.token.sourceEnd);}
1895 Expression ClassIdentifier():
1897 final Expression expr;
1901 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1902 | expr = Type() {return expr;}
1903 | expr = VariableDeclaratorId() {return expr;}
1907 * Used by Variabledeclaratorid and primarysuffix
1909 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1911 Expression expression = null;
1912 final Token classAccessToken;
1917 classAccessToken = <CLASSACCESS>
1919 ( expression = VariableName() | expression = Variable() )
1920 } catch (ParseException e) {
1921 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1923 errorStart = classAccessToken.sourceEnd +1;
1924 errorEnd = classAccessToken.sourceEnd +1;
1925 processParseExceptionDebug(e);
1927 {return new ClassAccess(prefix,
1929 ClassAccess.NORMAL);}
1931 token = <LBRACKET> {pos = token.sourceEnd+1;}
1932 [ expression = Expression() {pos = expression.sourceEnd+1;}
1933 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1936 {pos = token.sourceEnd;}
1937 } catch (ParseException e) {
1938 errorMessage = "']' expected";
1942 processParseExceptionDebug(e);
1944 {return new ArrayDeclarator(prefix,expression,pos);}
1952 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1953 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1954 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1955 | token = <TRUE> {return new TrueLiteral(token);}
1956 | token = <FALSE> {return new FalseLiteral(token);}
1957 | token = <NULL> {return new NullLiteral(token);}
1960 FunctionCall Arguments(final Expression func) :
1962 Expression[] args = null;
1966 <LPAREN> [ args = ArgumentList() ]
1969 {return new FunctionCall(func,args,token.sourceEnd);}
1970 } catch (ParseException e) {
1971 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
1973 errorStart = args[args.length-1].sourceEnd+1;
1974 errorEnd = args[args.length-1].sourceEnd+1;
1975 processParseExceptionDebug(e);
1977 {return new FunctionCall(func,args,args[args.length-1].sourceEnd);}
1981 * An argument list is a list of arguments separated by comma :
1982 * argumentDeclaration() (, argumentDeclaration)*
1983 * @return an array of arguments
1985 Expression[] ArgumentList() :
1988 final ArrayList list = new ArrayList();
1994 {list.add(arg);pos = arg.sourceEnd;}
1995 ( token = <COMMA> {pos = token.sourceEnd;}
1999 pos = arg.sourceEnd;}
2000 } catch (ParseException e) {
2001 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2005 processParseException(e);
2009 final Expression[] arguments = new Expression[list.size()];
2010 list.toArray(arguments);
2015 * A Statement without break.
2016 * @return a statement
2018 Statement StatementNoBreak() :
2020 final Statement statement;
2025 statement = expressionStatement() {return statement;}
2027 statement = LabeledStatement() {return statement;}
2028 | statement = Block() {return statement;}
2029 | statement = EmptyStatement() {return statement;}
2030 | statement = SwitchStatement() {return statement;}
2031 | statement = IfStatement() {return statement;}
2032 | statement = WhileStatement() {return statement;}
2033 | statement = DoStatement() {return statement;}
2034 | statement = ForStatement() {return statement;}
2035 | statement = ForeachStatement() {return statement;}
2036 | statement = ContinueStatement() {return statement;}
2037 | statement = ReturnStatement() {return statement;}
2038 | statement = EchoStatement() {return statement;}
2039 | [token=<AT>] statement = IncludeStatement()
2040 {if (token != null) {
2041 ((InclusionStatement)statement).silent = true;
2042 statement.sourceStart = token.sourceStart;
2045 | statement = StaticStatement() {return statement;}
2046 | statement = GlobalStatement() {return statement;}
2047 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2051 * A statement expression.
2053 * @return an expression
2055 Statement expressionStatement() :
2057 final Statement statement;
2061 statement = Expression()
2064 {statement.sourceEnd = token.sourceEnd;}
2065 } catch (ParseException e) {
2066 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2067 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2069 errorStart = statement.sourceEnd+1;
2070 errorEnd = statement.sourceEnd+1;
2071 processParseExceptionDebug(e);
2077 Define defineStatement() :
2079 Expression defineName,defineValue;
2080 final Token defineToken;
2085 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2088 {pos = token.sourceEnd+1;}
2089 } catch (ParseException e) {
2090 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2094 processParseExceptionDebug(e);
2097 defineName = Expression()
2098 {pos = defineName.sourceEnd+1;}
2099 } catch (ParseException e) {
2100 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2104 processParseExceptionDebug(e);
2105 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2109 {pos = defineName.sourceEnd+1;}
2110 } catch (ParseException e) {
2111 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2115 processParseExceptionDebug(e);
2118 defineValue = Expression()
2119 {pos = defineValue.sourceEnd+1;}
2120 } catch (ParseException e) {
2121 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2125 processParseExceptionDebug(e);
2126 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2130 {pos = token.sourceEnd+1;}
2131 } catch (ParseException e) {
2132 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2136 processParseExceptionDebug(e);
2138 {return new Define(currentSegment,
2141 defineToken.sourceStart,
2146 * A Normal statement.
2148 Statement Statement() :
2150 final Statement statement;
2153 statement = StatementNoBreak() {return statement;}
2154 | statement = BreakStatement() {return statement;}
2158 * An html block inside a php syntax.
2160 HTMLBlock htmlBlock() :
2162 final int startIndex = nodePtr;
2163 final AstNode[] blockNodes;
2167 <PHPEND> (phpEchoBlock())*
2169 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2170 } catch (ParseException e) {
2171 errorMessage = "unexpected end of file , '<?php' expected";
2173 errorStart = SimpleCharStream.getPosition();
2174 errorEnd = SimpleCharStream.getPosition();
2178 nbNodes = nodePtr - startIndex;
2179 blockNodes = new AstNode[nbNodes];
2180 System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes);
2181 nodePtr = startIndex;
2182 return new HTMLBlock(blockNodes);}
2186 * An include statement. It's "include" an expression;
2188 InclusionStatement IncludeStatement() :
2192 final InclusionStatement inclusionStatement;
2193 final Token token, token2;
2197 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2198 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2199 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2200 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2203 {pos=expr.sourceEnd;}
2204 } catch (ParseException e) {
2205 if (errorMessage != null) {
2208 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2212 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2213 processParseExceptionDebug(e);
2215 {inclusionStatement = new InclusionStatement(currentSegment,
2219 currentSegment.add(inclusionStatement);
2222 token2 = <SEMICOLON>
2223 } catch (ParseException e) {
2224 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2226 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2227 errorEnd = SimpleCharStream.getPosition() + 1;
2230 {inclusionStatement.sourceEnd = token2.sourceEnd;
2231 return inclusionStatement;}
2234 PrintExpression PrintExpression() :
2236 final Expression expr;
2237 final Token printToken;
2240 token = <PRINT> expr = Expression()
2241 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2244 ListExpression ListExpression() :
2246 Expression expr = null;
2247 final Expression expression;
2248 final ArrayList list = new ArrayList();
2250 final Token listToken, rParen;
2254 listToken = <LIST> {pos = listToken.sourceEnd;}
2256 token = <LPAREN> {pos = token.sourceEnd;}
2257 } catch (ParseException e) {
2258 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2260 errorStart = listToken.sourceEnd+1;
2261 errorEnd = listToken.sourceEnd+1;
2262 processParseExceptionDebug(e);
2265 expr = VariableDeclaratorId()
2266 {list.add(expr);pos = expr.sourceEnd;}
2268 {if (expr == null) list.add(null);}
2272 {pos = token.sourceEnd;}
2273 } catch (ParseException e) {
2274 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2278 processParseExceptionDebug(e);
2280 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2284 {pos = rParen.sourceEnd;}
2285 } catch (ParseException e) {
2286 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2290 processParseExceptionDebug(e);
2292 [ <ASSIGN> expression = Expression()
2294 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2296 return new ListExpression(vars,
2298 listToken.sourceStart,
2299 expression.sourceEnd);}
2302 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2304 return new ListExpression(vars,listToken.sourceStart,pos);}
2308 * An echo statement.
2309 * echo anyexpression (, otherexpression)*
2311 EchoStatement EchoStatement() :
2313 final ArrayList expressions = new ArrayList();
2316 Token token2 = null;
2319 token = <ECHO> expr = Expression()
2320 {expressions.add(expr);}
2322 <COMMA> expr = Expression()
2323 {expressions.add(expr);}
2326 token2 = <SEMICOLON>
2327 } catch (ParseException e) {
2328 if (e.currentToken.next.kind != 4) {
2329 errorMessage = "';' expected after 'echo' statement";
2331 errorStart = e.currentToken.sourceEnd;
2332 errorEnd = e.currentToken.sourceEnd;
2333 processParseExceptionDebug(e);
2337 final Expression[] exprs = new Expression[expressions.size()];
2338 expressions.toArray(exprs);
2339 if (token2 == null) {
2340 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2342 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2346 GlobalStatement GlobalStatement() :
2349 final ArrayList vars = new ArrayList();
2350 final GlobalStatement global;
2351 final Token token, token2;
2357 {vars.add(expr);pos = expr.sourceEnd+1;}
2360 {vars.add(expr);pos = expr.sourceEnd+1;}
2363 token2 = <SEMICOLON>
2364 {pos = token2.sourceEnd+1;}
2365 } catch (ParseException e) {
2366 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2370 processParseExceptionDebug(e);
2373 final Variable[] variables = new Variable[vars.size()];
2374 vars.toArray(variables);
2375 global = new GlobalStatement(currentSegment,
2379 currentSegment.add(global);
2383 StaticStatement StaticStatement() :
2385 final ArrayList vars = new ArrayList();
2386 VariableDeclaration expr;
2387 final Token token, token2;
2391 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2393 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2396 token2 = <SEMICOLON>
2397 {pos = token2.sourceEnd+1;}
2398 } catch (ParseException e) {
2399 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2403 processParseException(e);
2406 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2407 vars.toArray(variables);
2408 return new StaticStatement(variables,
2413 LabeledStatement LabeledStatement() :
2416 final Statement statement;
2419 label = <IDENTIFIER> <COLON> statement = Statement()
2420 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2432 final ArrayList list = new ArrayList();
2433 Statement statement;
2434 final Token token, token2;
2440 {pos = token.sourceEnd+1;start=token.sourceStart;}
2441 } catch (ParseException e) {
2442 errorMessage = "'{' expected";
2444 pos = PHPParser.token.sourceEnd+1;
2448 processParseExceptionDebug(e);
2450 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2451 | statement = htmlBlock() {list.add(statement);pos = statement.sourceEnd+1;})*
2454 {pos = token2.sourceEnd+1;}
2455 } catch (ParseException e) {
2456 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2460 processParseExceptionDebug(e);
2463 final Statement[] statements = new Statement[list.size()];
2464 list.toArray(statements);
2465 return new Block(statements,start,pos);}
2468 Statement BlockStatement() :
2470 final Statement statement;
2474 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2476 } catch (ParseException e) {
2477 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2479 errorStart = e.currentToken.sourceStart;
2480 errorEnd = e.currentToken.sourceEnd;
2483 | statement = ClassDeclaration() {return statement;}
2484 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2485 currentSegment.add((MethodDeclaration) statement);
2486 ((MethodDeclaration) statement).analyzeCode();
2491 * A Block statement that will not contain any 'break'
2493 Statement BlockStatementNoBreak() :
2495 final Statement statement;
2498 statement = StatementNoBreak() {return statement;}
2499 | statement = ClassDeclaration() {return statement;}
2500 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2501 ((MethodDeclaration) statement).analyzeCode();
2506 * used only by ForInit()
2508 Expression[] LocalVariableDeclaration() :
2510 final ArrayList list = new ArrayList();
2516 ( <COMMA> var = Expression() {list.add(var);})*
2518 final Expression[] vars = new Expression[list.size()];
2525 * used only by LocalVariableDeclaration().
2527 VariableDeclaration LocalVariableDeclarator() :
2529 final Variable varName;
2530 Expression initializer = null;
2533 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2535 if (initializer == null) {
2536 return new VariableDeclaration(currentSegment,
2538 varName.sourceStart,
2541 return new VariableDeclaration(currentSegment,
2544 VariableDeclaration.EQUAL,
2545 varName.sourceStart);
2549 EmptyStatement EmptyStatement() :
2555 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2559 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2561 Expression StatementExpression() :
2563 final Expression expr;
2564 final Token operator;
2567 expr = PreIncDecExpression() {return expr;}
2569 expr = PrimaryExpression()
2570 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2571 OperatorIds.PLUS_PLUS,
2572 operator.sourceEnd);}
2573 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2574 OperatorIds.MINUS_MINUS,
2575 operator.sourceEnd);}
2580 SwitchStatement SwitchStatement() :
2582 Expression variable;
2583 final AbstractCase[] cases;
2584 final Token switchToken,lparenToken,rparenToken;
2588 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2590 lparenToken = <LPAREN>
2591 {pos = lparenToken.sourceEnd+1;}
2592 } catch (ParseException e) {
2593 errorMessage = "'(' expected after 'switch'";
2597 processParseExceptionDebug(e);
2600 variable = Expression() {pos = variable.sourceEnd+1;}
2601 } catch (ParseException e) {
2602 if (errorMessage != null) {
2605 errorMessage = "expression expected";
2609 processParseExceptionDebug(e);
2610 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2613 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2614 } catch (ParseException e) {
2615 errorMessage = "')' expected";
2619 processParseExceptionDebug(e);
2621 ( cases = switchStatementBrace()
2622 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2623 {return new SwitchStatement(variable,
2625 switchToken.sourceStart,
2626 PHPParser.token.sourceEnd);}
2629 AbstractCase[] switchStatementBrace() :
2632 final ArrayList cases = new ArrayList();
2637 token = <LBRACE> {pos = token.sourceEnd;}
2638 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2641 {pos = token.sourceEnd;}
2642 } catch (ParseException e) {
2643 errorMessage = "'}' expected";
2647 processParseExceptionDebug(e);
2650 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2651 cases.toArray(abcase);
2656 * A Switch statement with : ... endswitch;
2657 * @param start the begin offset of the switch
2658 * @param end the end offset of the switch
2660 AbstractCase[] switchStatementColon(final int start, final int end) :
2663 final ArrayList cases = new ArrayList();
2668 token = <COLON> {pos = token.sourceEnd;}
2670 setMarker(fileToParse,
2671 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2675 "Line " + token.beginLine);
2676 } catch (CoreException e) {
2677 PHPeclipsePlugin.log(e);
2679 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2681 token = <ENDSWITCH> {pos = token.sourceEnd;}
2682 } catch (ParseException e) {
2683 errorMessage = "'endswitch' expected";
2687 processParseExceptionDebug(e);
2690 token = <SEMICOLON> {pos = token.sourceEnd;}
2691 } catch (ParseException e) {
2692 errorMessage = "';' expected after 'endswitch' keyword";
2696 processParseExceptionDebug(e);
2699 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2700 cases.toArray(abcase);
2705 AbstractCase switchLabel0() :
2707 final Expression expr;
2708 Statement statement;
2709 final ArrayList stmts = new ArrayList();
2710 final Token token = PHPParser.token;
2713 expr = SwitchLabel()
2714 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2715 | statement = htmlBlock() {stmts.add(statement);})*
2716 [ statement = BreakStatement() {stmts.add(statement);}]
2718 final int listSize = stmts.size();
2719 final Statement[] stmtsArray = new Statement[listSize];
2720 stmts.toArray(stmtsArray);
2721 if (expr == null) {//it's a default
2722 return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd);
2724 if (listSize != 0) {
2725 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2727 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2734 * case Expression() :
2736 * @return the if it was a case and null if not
2738 Expression SwitchLabel() :
2740 final Expression expr;
2746 } catch (ParseException e) {
2747 if (errorMessage != null) throw e;
2748 errorMessage = "expression expected after 'case' keyword";
2750 errorStart = token.sourceEnd +1;
2751 errorEnd = token.sourceEnd +1;
2757 } catch (ParseException e) {
2758 errorMessage = "':' expected after case expression";
2760 errorStart = expr.sourceEnd+1;
2761 errorEnd = expr.sourceEnd+1;
2762 processParseExceptionDebug(e);
2769 } catch (ParseException e) {
2770 errorMessage = "':' expected after 'default' keyword";
2772 errorStart = token.sourceEnd+1;
2773 errorEnd = token.sourceEnd+1;
2774 processParseExceptionDebug(e);
2778 Break BreakStatement() :
2780 Expression expression = null;
2781 final Token token, token2;
2785 token = <BREAK> {pos = token.sourceEnd+1;}
2786 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2788 token2 = <SEMICOLON>
2789 {pos = token2.sourceEnd;}
2790 } catch (ParseException e) {
2791 errorMessage = "';' expected after 'break' keyword";
2795 processParseExceptionDebug(e);
2797 {return new Break(expression, token.sourceStart, pos);}
2800 IfStatement IfStatement() :
2802 final Expression condition;
2803 final IfStatement ifStatement;
2807 token = <IF> condition = Condition("if")
2808 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2809 {return ifStatement;}
2813 Expression Condition(final String keyword) :
2815 final Expression condition;
2820 } catch (ParseException e) {
2821 errorMessage = "'(' expected after " + keyword + " keyword";
2823 errorStart = PHPParser.token.sourceEnd + 1;
2824 errorEnd = PHPParser.token.sourceEnd + 1;
2825 processParseExceptionDebug(e);
2827 condition = Expression()
2830 } catch (ParseException e) {
2831 errorMessage = "')' expected after " + keyword + " keyword";
2833 errorStart = condition.sourceEnd+1;
2834 errorEnd = condition.sourceEnd+1;
2835 processParseExceptionDebug(e);
2840 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2842 Statement statement;
2843 final Statement stmt;
2844 final Statement[] statementsArray;
2845 ElseIf elseifStatement;
2846 Else elseStatement = null;
2847 final ArrayList stmts;
2848 final ArrayList elseIfList = new ArrayList();
2849 final ElseIf[] elseIfs;
2850 int pos = SimpleCharStream.getPosition();
2851 final int endStatements;
2855 {stmts = new ArrayList();}
2856 ( statement = Statement() {stmts.add(statement);}
2857 | statement = htmlBlock() {stmts.add(statement);})*
2858 {endStatements = SimpleCharStream.getPosition();}
2859 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2860 [elseStatement = ElseStatementColon()]
2863 setMarker(fileToParse,
2864 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2868 "Line " + token.beginLine);
2869 } catch (CoreException e) {
2870 PHPeclipsePlugin.log(e);
2874 } catch (ParseException e) {
2875 errorMessage = "'endif' expected";
2877 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2878 errorEnd = SimpleCharStream.getPosition() + 1;
2883 } catch (ParseException e) {
2884 errorMessage = "';' expected after 'endif' keyword";
2886 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2887 errorEnd = SimpleCharStream.getPosition() + 1;
2891 elseIfs = new ElseIf[elseIfList.size()];
2892 elseIfList.toArray(elseIfs);
2893 if (stmts.size() == 1) {
2894 return new IfStatement(condition,
2895 (Statement) stmts.get(0),
2899 SimpleCharStream.getPosition());
2901 statementsArray = new Statement[stmts.size()];
2902 stmts.toArray(statementsArray);
2903 return new IfStatement(condition,
2904 new Block(statementsArray,pos,endStatements),
2908 SimpleCharStream.getPosition());
2913 (stmt = Statement() | stmt = htmlBlock())
2914 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
2918 {pos = SimpleCharStream.getPosition();}
2919 statement = Statement()
2920 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
2921 } catch (ParseException e) {
2922 if (errorMessage != null) {
2925 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
2927 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2928 errorEnd = SimpleCharStream.getPosition() + 1;
2933 elseIfs = new ElseIf[elseIfList.size()];
2934 elseIfList.toArray(elseIfs);
2935 return new IfStatement(condition,
2940 SimpleCharStream.getPosition());}
2943 ElseIf ElseIfStatementColon() :
2945 final Expression condition;
2946 Statement statement;
2947 final ArrayList list = new ArrayList();
2948 final Token elseifToken;
2951 elseifToken = <ELSEIF> condition = Condition("elseif")
2952 <COLON> ( statement = Statement() {list.add(statement);}
2953 | statement = htmlBlock() {list.add(statement);})*
2955 final int sizeList = list.size();
2956 final Statement[] stmtsArray = new Statement[sizeList];
2957 list.toArray(stmtsArray);
2958 return new ElseIf(condition,stmtsArray ,
2959 elseifToken.sourceStart,
2960 stmtsArray[sizeList-1].sourceEnd);}
2963 Else ElseStatementColon() :
2965 Statement statement;
2966 final ArrayList list = new ArrayList();
2967 final Token elseToken;
2970 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
2971 | statement = htmlBlock() {list.add(statement);})*
2973 final int sizeList = list.size();
2974 final Statement[] stmtsArray = new Statement[sizeList];
2975 list.toArray(stmtsArray);
2976 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
2979 ElseIf ElseIfStatement() :
2981 final Expression condition;
2982 //final Statement statement;
2983 final Token elseifToken;
2984 final Statement[] statement = new Statement[1];
2987 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
2989 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
2992 WhileStatement WhileStatement() :
2994 final Expression condition;
2995 final Statement action;
2996 final Token whileToken;
2999 whileToken = <WHILE>
3000 condition = Condition("while")
3001 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3002 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3005 Statement WhileStatement0(final int start, final int end) :
3007 Statement statement;
3008 final ArrayList stmts = new ArrayList();
3009 final int pos = SimpleCharStream.getPosition();
3012 <COLON> (statement = Statement() {stmts.add(statement);})*
3014 setMarker(fileToParse,
3015 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3019 "Line " + token.beginLine);
3020 } catch (CoreException e) {
3021 PHPeclipsePlugin.log(e);
3025 } catch (ParseException e) {
3026 errorMessage = "'endwhile' expected";
3028 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3029 errorEnd = SimpleCharStream.getPosition() + 1;
3035 final Statement[] stmtsArray = new Statement[stmts.size()];
3036 stmts.toArray(stmtsArray);
3037 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3038 } catch (ParseException e) {
3039 errorMessage = "';' expected after 'endwhile' keyword";
3041 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3042 errorEnd = SimpleCharStream.getPosition() + 1;
3046 statement = Statement()
3050 DoStatement DoStatement() :
3052 final Statement action;
3053 final Expression condition;
3055 Token token2 = null;
3058 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3060 token2 = <SEMICOLON>
3061 } catch (ParseException e) {
3062 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3064 errorStart = condition.sourceEnd+1;
3065 errorEnd = condition.sourceEnd+1;
3066 processParseExceptionDebug(e);
3069 if (token2 == null) {
3070 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3072 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3076 ForeachStatement ForeachStatement() :
3078 Statement statement = null;
3079 Expression expression = null;
3080 ArrayVariableDeclaration variable = null;
3082 Token lparenToken = null;
3083 Token asToken = null;
3084 Token rparenToken = null;
3088 foreachToken = <FOREACH>
3090 lparenToken = <LPAREN>
3091 {pos = lparenToken.sourceEnd+1;}
3092 } catch (ParseException e) {
3093 errorMessage = "'(' expected after 'foreach' keyword";
3095 errorStart = foreachToken.sourceEnd+1;
3096 errorEnd = foreachToken.sourceEnd+1;
3097 processParseExceptionDebug(e);
3098 {pos = foreachToken.sourceEnd+1;}
3101 expression = Expression()
3102 {pos = expression.sourceEnd+1;}
3103 } catch (ParseException e) {
3104 errorMessage = "variable expected";
3108 processParseExceptionDebug(e);
3112 {pos = asToken.sourceEnd+1;}
3113 } catch (ParseException e) {
3114 errorMessage = "'as' expected";
3118 processParseExceptionDebug(e);
3121 variable = ArrayVariable()
3122 {pos = variable.sourceEnd+1;}
3123 } catch (ParseException e) {
3124 if (errorMessage != null) throw e;
3125 errorMessage = "variable expected";
3129 processParseExceptionDebug(e);
3132 rparenToken = <RPAREN>
3133 {pos = rparenToken.sourceEnd+1;}
3134 } catch (ParseException e) {
3135 errorMessage = "')' expected after 'foreach' keyword";
3139 processParseExceptionDebug(e);
3142 statement = Statement()
3143 {pos = rparenToken.sourceEnd+1;}
3144 } catch (ParseException e) {
3145 if (errorMessage != null) throw e;
3146 errorMessage = "statement expected";
3150 processParseExceptionDebug(e);
3152 {return new ForeachStatement(expression,
3155 foreachToken.sourceStart,
3156 statement.sourceEnd);}
3161 * a for declaration.
3162 * @return a node representing the for statement
3164 ForStatement ForStatement() :
3166 final Token token,tokenEndFor,token2,tokenColon;
3168 Expression[] initializations = null;
3169 Expression condition = null;
3170 Expression[] increments = null;
3172 final ArrayList list = new ArrayList();
3178 } catch (ParseException e) {
3179 errorMessage = "'(' expected after 'for' keyword";
3181 errorStart = token.sourceEnd;
3182 errorEnd = token.sourceEnd +1;
3183 processParseExceptionDebug(e);
3185 [ initializations = ForInit() ] <SEMICOLON>
3186 [ condition = Expression() ] <SEMICOLON>
3187 [ increments = StatementExpressionList() ] <RPAREN>
3189 action = Statement()
3190 {return new ForStatement(initializations,
3197 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3198 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3201 setMarker(fileToParse,
3202 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3206 "Line " + token.beginLine);
3207 } catch (CoreException e) {
3208 PHPeclipsePlugin.log(e);
3212 tokenEndFor = <ENDFOR>
3213 {pos = tokenEndFor.sourceEnd+1;}
3214 } catch (ParseException e) {
3215 errorMessage = "'endfor' expected";
3219 processParseExceptionDebug(e);
3222 token2 = <SEMICOLON>
3223 {pos = token2.sourceEnd+1;}
3224 } catch (ParseException e) {
3225 errorMessage = "';' expected after 'endfor' keyword";
3229 processParseExceptionDebug(e);
3232 final Statement[] stmtsArray = new Statement[list.size()];
3233 list.toArray(stmtsArray);
3234 return new ForStatement(initializations,
3237 new Block(stmtsArray,
3238 stmtsArray[0].sourceStart,
3239 stmtsArray[stmtsArray.length-1].sourceEnd),
3245 Expression[] ForInit() :
3247 final Expression[] exprs;
3250 LOOKAHEAD(LocalVariableDeclaration())
3251 exprs = LocalVariableDeclaration()
3254 exprs = StatementExpressionList()
3258 Expression[] StatementExpressionList() :
3260 final ArrayList list = new ArrayList();
3261 final Expression expr;
3264 expr = Expression() {list.add(expr);}
3265 (<COMMA> Expression() {list.add(expr);})*
3267 final Expression[] exprsArray = new Expression[list.size()];
3268 list.toArray(exprsArray);
3273 Continue ContinueStatement() :
3275 Expression expr = null;
3277 Token token2 = null;
3280 token = <CONTINUE> [ expr = Expression() ]
3282 token2 = <SEMICOLON>
3283 } catch (ParseException e) {
3284 errorMessage = "';' expected after 'continue' statement";
3287 errorStart = token.sourceEnd+1;
3288 errorEnd = token.sourceEnd+1;
3290 errorStart = expr.sourceEnd+1;
3291 errorEnd = expr.sourceEnd+1;
3293 processParseExceptionDebug(e);
3296 if (token2 == null) {
3298 return new Continue(expr,token.sourceStart,token.sourceEnd);
3300 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3302 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3306 ReturnStatement ReturnStatement() :
3308 Expression expr = null;
3310 Token token2 = null;
3313 token = <RETURN> [ expr = Expression() ]
3315 token2 = <SEMICOLON>
3316 } catch (ParseException e) {
3317 errorMessage = "';' expected after 'return' statement";
3320 errorStart = token.sourceEnd+1;
3321 errorEnd = token.sourceEnd+1;
3323 errorStart = expr.sourceEnd+1;
3324 errorEnd = expr.sourceEnd+1;
3326 processParseExceptionDebug(e);
3329 if (token2 == null) {
3331 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3333 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3335 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);