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 = true;
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 < htmlStart ||
335 currentPosition > SimpleCharStream.currentBuffer.length()) {
338 final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,
339 currentPosition).toCharArray();
340 pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
343 /** Create a new task. */
344 public static final void createNewTask(final int todoStart) {
345 final String todo = SimpleCharStream.currentBuffer.substring(todoStart,
346 SimpleCharStream.currentBuffer.indexOf("\n",
350 setMarker(fileToParse,
352 SimpleCharStream.getBeginLine(),
354 "Line "+SimpleCharStream.getBeginLine());
355 } catch (CoreException e) {
356 PHPeclipsePlugin.log(e);
361 private static final void parse() throws ParseException {
366 PARSER_END(PHPParser)
370 // CommonTokenAction: use the begins/ends fields added to the Jack
371 // CharStream class to set corresponding fields in each Token (which was
372 // also extended with new fields). By default Jack doesn't supply absolute
373 // offsets, just line/column offsets
374 static void CommonTokenAction(Token t) {
375 t.sourceStart = input_stream.beginOffset;
376 t.sourceEnd = input_stream.endOffset;
377 } // CommonTokenAction
382 <PHPSTARTSHORT : "<?"> : PHPPARSING
383 | <PHPSTARTLONG : "<?php"> : PHPPARSING
384 | <PHPECHOSTART : "<?="> : PHPPARSING
387 <PHPPARSING, IN_SINGLE_LINE_COMMENT,IN_VARIABLE> TOKEN :
389 <PHPEND :"?>"> : DEFAULT
392 /* Skip any character if we are not in php mode */
409 <IN_VARIABLE> SPECIAL_TOKEN :
418 <PHPPARSING> SPECIAL_TOKEN :
420 "//" : IN_SINGLE_LINE_COMMENT
421 | "#" : IN_SINGLE_LINE_COMMENT
422 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
423 | "/*" : IN_MULTI_LINE_COMMENT
426 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
428 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
432 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
440 todoToken = "TODO" {createNewTask(todoToken.sourceStart);}
442 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
447 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
452 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
462 | <FUNCTION : "function">
465 | <ELSEIF : "elseif">
472 /* LANGUAGE CONSTRUCT */
477 | <INCLUDE : "include">
478 | <REQUIRE : "require">
479 | <INCLUDE_ONCE : "include_once">
480 | <REQUIRE_ONCE : "require_once">
481 | <GLOBAL : "global">
482 | <DEFINE : "define">
483 | <STATIC : "static">
486 <PHPPARSING,IN_VARIABLE> TOKEN :
488 <CLASSACCESS : "->"> : PHPPARSING
489 | <STATICCLASSACCESS : "::"> : PHPPARSING
490 | <ARRAYASSIGN : "=>"> : PHPPARSING
493 /* RESERVED WORDS AND LITERALS */
499 | <CONTINUE : "continue">
500 | <_DEFAULT : "default">
502 | <EXTENDS : "extends">
507 | <RETURN : "return">
509 | <SWITCH : "switch">
514 | <ENDWHILE : "endwhile">
515 | <ENDSWITCH: "endswitch">
517 | <ENDFOR : "endfor">
518 | <FOREACH : "foreach">
526 | <OBJECT : "object">
528 | <BOOLEAN : "boolean">
530 | <DOUBLE : "double">
533 | <INTEGER : "integer">
537 <PHPPARSING,IN_VARIABLE> TOKEN :
539 <AT : "@"> : PHPPARSING
540 | <BANG : "!"> : PHPPARSING
541 | <TILDE : "~"> : PHPPARSING
542 | <HOOK : "?"> : PHPPARSING
543 | <COLON : ":"> : PHPPARSING
547 <PHPPARSING,IN_VARIABLE> TOKEN :
549 <OR_OR : "||"> : PHPPARSING
550 | <AND_AND : "&&"> : PHPPARSING
551 | <PLUS_PLUS : "++"> : PHPPARSING
552 | <MINUS_MINUS : "--"> : PHPPARSING
553 | <PLUS : "+"> : PHPPARSING
554 | <MINUS : "-"> : PHPPARSING
555 | <STAR : "*"> : PHPPARSING
556 | <SLASH : "/"> : PHPPARSING
557 | <BIT_AND : "&"> : PHPPARSING
558 | <BIT_OR : "|"> : PHPPARSING
559 | <XOR : "^"> : PHPPARSING
560 | <REMAINDER : "%"> : PHPPARSING
561 | <LSHIFT : "<<"> : PHPPARSING
562 | <RSIGNEDSHIFT : ">>"> : PHPPARSING
563 | <RUNSIGNEDSHIFT : ">>>"> : PHPPARSING
564 | <_ORL : "OR"> : PHPPARSING
565 | <_ANDL : "AND"> : PHPPARSING
572 <DECIMAL_LITERAL> (["l","L"])?
573 | <HEX_LITERAL> (["l","L"])?
574 | <OCTAL_LITERAL> (["l","L"])?
577 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
579 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
581 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
583 <FLOATING_POINT_LITERAL:
584 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
585 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
586 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
587 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
590 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
592 <STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
593 | <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
594 | <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
595 | <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
601 <PHPPARSING,IN_VARIABLE> TOKEN : {<DOLLAR : "$"> : IN_VARIABLE}
604 <PHPPARSING, IN_VARIABLE> TOKEN :
606 <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
609 ["a"-"z"] | ["A"-"Z"]
617 "_" | ["\u007f"-"\u00ff"]
623 <PHPPARSING,IN_VARIABLE> TOKEN :
625 <LPAREN : "("> : PHPPARSING
626 | <RPAREN : ")"> : PHPPARSING
627 | <LBRACE : "{"> : PHPPARSING
628 | <RBRACE : "}"> : PHPPARSING
629 | <LBRACKET : "["> : PHPPARSING
630 | <RBRACKET : "]"> : PHPPARSING
631 | <SEMICOLON : ";"> : PHPPARSING
632 | <COMMA : ","> : PHPPARSING
633 | <DOT : "."> : PHPPARSING
638 <PHPPARSING,IN_VARIABLE> TOKEN :
640 <GT : ">"> : PHPPARSING
641 | <LT : "<"> : PHPPARSING
642 | <EQUAL_EQUAL : "=="> : PHPPARSING
643 | <LE : "<="> : PHPPARSING
644 | <GE : ">="> : PHPPARSING
645 | <NOT_EQUAL : "!="> : PHPPARSING
646 | <DIF : "<>"> : PHPPARSING
647 | <BANGDOUBLEEQUAL : "!=="> : PHPPARSING
648 | <TRIPLEEQUAL : "==="> : PHPPARSING
652 <PHPPARSING,IN_VARIABLE> TOKEN :
654 <ASSIGN : "="> : PHPPARSING
655 | <PLUSASSIGN : "+="> : PHPPARSING
656 | <MINUSASSIGN : "-="> : PHPPARSING
657 | <STARASSIGN : "*="> : PHPPARSING
658 | <SLASHASSIGN : "/="> : PHPPARSING
659 | <ANDASSIGN : "&="> : PHPPARSING
660 | <ORASSIGN : "|="> : PHPPARSING
661 | <XORASSIGN : "^="> : PHPPARSING
662 | <DOTASSIGN : ".="> : PHPPARSING
663 | <REMASSIGN : "%="> : PHPPARSING
664 | <TILDEEQUAL : "~="> : PHPPARSING
665 | <LSHIFTASSIGN : "<<="> : PHPPARSING
666 | <RSIGNEDSHIFTASSIGN : ">>="> : PHPPARSING
681 {PHPParser.createNewHTMLCode();}
682 } catch (TokenMgrError e) {
683 PHPeclipsePlugin.log(e);
684 errorStart = SimpleCharStream.beginOffset;
685 errorEnd = SimpleCharStream.endOffset;
686 errorMessage = e.getMessage();
688 throw generateParseException();
693 * A php block is a <?= expression [;]?>
694 * or <?php somephpcode ?>
695 * or <? somephpcode ?>
699 final PHPEchoBlock phpEchoBlock;
700 final Token token,phpEnd;
703 phpEchoBlock = phpEchoBlock()
704 {pushOnAstNodes(phpEchoBlock);}
707 | token = <PHPSTARTSHORT>
709 setMarker(fileToParse,
710 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
714 "Line " + token.beginLine);
715 } catch (CoreException e) {
716 PHPeclipsePlugin.log(e);
719 {PHPParser.createNewHTMLCode();}
723 {htmlStart = phpEnd.sourceEnd;}
724 } catch (ParseException e) {
725 errorMessage = "'?>' expected";
727 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
728 errorEnd = SimpleCharStream.getPosition() + 1;
729 processParseExceptionDebug(e);
733 PHPEchoBlock phpEchoBlock() :
735 final Expression expr;
736 final PHPEchoBlock echoBlock;
737 final Token token, token2;
740 token = <PHPECHOSTART> {PHPParser.createNewHTMLCode();}
741 expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
743 htmlStart = token2.sourceEnd;
745 echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
746 pushOnAstNodes(echoBlock);
756 ClassDeclaration ClassDeclaration() :
758 final ClassDeclaration classDeclaration;
759 Token className = null;
760 final Token superclassName, token, extendsToken;
761 String classNameImage = SYNTAX_ERROR_CHAR;
762 String superclassNameImage = null;
768 className = <IDENTIFIER>
769 {classNameImage = className.image;}
770 } catch (ParseException e) {
771 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
773 errorStart = token.sourceEnd+1;
774 errorEnd = token.sourceEnd+1;
775 processParseExceptionDebug(e);
778 extendsToken = <EXTENDS>
780 superclassName = <IDENTIFIER>
781 {superclassNameImage = superclassName.image;}
782 } catch (ParseException e) {
783 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
785 errorStart = extendsToken.sourceEnd+1;
786 errorEnd = extendsToken.sourceEnd+1;
787 processParseExceptionDebug(e);
788 superclassNameImage = SYNTAX_ERROR_CHAR;
793 if (className == null) {
794 start = token.sourceStart;
795 end = token.sourceEnd;
797 start = className.sourceStart;
798 end = className.sourceEnd;
800 if (superclassNameImage == null) {
802 classDeclaration = new ClassDeclaration(currentSegment,
807 classDeclaration = new ClassDeclaration(currentSegment,
813 currentSegment.add(classDeclaration);
814 currentSegment = classDeclaration;
816 classEnd = ClassBody(classDeclaration)
817 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
818 classDeclaration.sourceEnd = classEnd;
819 pushOnAstNodes(classDeclaration);
820 return classDeclaration;}
823 int ClassBody(final ClassDeclaration classDeclaration) :
830 } catch (ParseException e) {
831 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
833 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
834 errorEnd = SimpleCharStream.getPosition() + 1;
835 processParseExceptionDebug(e);
837 ( ClassBodyDeclaration(classDeclaration) )*
840 {return token.sourceEnd;}
841 } catch (ParseException e) {
842 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
844 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
845 errorEnd = SimpleCharStream.getPosition() + 1;
846 processParseExceptionDebug(e);
847 return PHPParser.token.sourceEnd;
852 * A class can contain only methods and fields.
854 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
856 final MethodDeclaration method;
857 final FieldDeclaration field;
860 method = MethodDeclaration() {method.analyzeCode();
861 classDeclaration.addMethod(method);}
862 | field = FieldDeclaration() {classDeclaration.addField(field);}
866 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
867 * it is only used by ClassBodyDeclaration()
869 FieldDeclaration FieldDeclaration() :
871 VariableDeclaration variableDeclaration;
872 final VariableDeclaration[] list;
873 final ArrayList arrayList = new ArrayList();
879 token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
881 arrayList.add(variableDeclaration);
882 outlineInfo.addVariable(variableDeclaration.name());
883 pos = variableDeclaration.sourceEnd;
886 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
888 arrayList.add(variableDeclaration);
889 outlineInfo.addVariable(variableDeclaration.name());
890 pos = variableDeclaration.sourceEnd;
895 } catch (ParseException e) {
896 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
900 processParseExceptionDebug(e);
903 {list = new VariableDeclaration[arrayList.size()];
904 arrayList.toArray(list);
906 if (token2 == null) {
907 end = list[list.length-1].sourceEnd;
909 end = token2.sourceEnd;
911 return new FieldDeclaration(list,
918 * a strict variable declarator : there cannot be a suffix here.
919 * It will be used by fields and formal parameters
921 VariableDeclaration VariableDeclaratorNoSuffix() :
923 final Token token, lbrace,rbrace;
924 Expression expr, initializer = null;
932 {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
934 lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
935 {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
938 assignToken = <ASSIGN>
940 initializer = VariableInitializer()
941 } catch (ParseException e) {
942 errorMessage = "Literal expression expected in variable initializer";
944 errorStart = assignToken.sourceEnd +1;
945 errorEnd = assignToken.sourceEnd +1;
946 processParseExceptionDebug(e);
950 if (initializer == null) {
951 return new VariableDeclaration(currentSegment,
953 variable.sourceStart,
956 return new VariableDeclaration(currentSegment,
959 VariableDeclaration.EQUAL,
960 variable.sourceStart);
965 * this will be used by static statement
967 VariableDeclaration VariableDeclarator() :
969 final AbstractVariable variable;
970 Expression initializer = null;
974 variable = VariableDeclaratorId()
978 initializer = VariableInitializer()
979 } catch (ParseException e) {
980 errorMessage = "Literal expression expected in variable initializer";
982 errorStart = token.sourceEnd+1;
983 errorEnd = token.sourceEnd+1;
984 processParseExceptionDebug(e);
988 if (initializer == null) {
989 return new VariableDeclaration(currentSegment,
991 variable.sourceStart,
994 return new VariableDeclaration(currentSegment,
997 VariableDeclaration.EQUAL,
998 variable.sourceStart);
1004 * @return the variable name (with suffix)
1006 AbstractVariable VariableDeclaratorId() :
1009 AbstractVariable expression = null;
1016 expression = VariableSuffix(var)
1019 if (expression == null) {
1024 } catch (ParseException e) {
1025 errorMessage = "'$' expected for variable identifier";
1027 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1028 errorEnd = SimpleCharStream.getPosition() + 1;
1033 Variable Variable() :
1035 Variable variable = null;
1039 token = <DOLLAR> variable = Var(token)
1041 return new Variable(variable,token.sourceEnd,variable.sourceEnd);
1045 Variable Var(final Token dollar) :
1047 Variable variable = null;
1048 final Token token,token2;
1049 ConstantIdentifier constant;
1050 Expression expression;
1053 token = <DOLLAR> variable = Var(token)
1054 {return new Variable(variable,dollar.sourceEnd,variable.sourceEnd);}
1056 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1058 return new Variable(expression,
1063 token = <IDENTIFIER>
1064 {return new Variable(token.image,dollar.sourceStart,token.sourceEnd);}
1067 Expression VariableInitializer() :
1069 final Expression expr;
1070 final Token token, token2;
1076 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1077 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1079 token2.sourceStart);}
1081 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1082 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1084 token2.sourceStart);}
1086 expr = ArrayDeclarator()
1089 token = <IDENTIFIER>
1090 {return new ConstantIdentifier(token);}
1093 ArrayVariableDeclaration ArrayVariable() :
1095 final Expression expr,expr2;
1100 <ARRAYASSIGN> expr2 = Expression()
1101 {return new ArrayVariableDeclaration(expr,expr2);}
1103 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1106 ArrayVariableDeclaration[] ArrayInitializer() :
1108 ArrayVariableDeclaration expr;
1109 final ArrayList list = new ArrayList();
1114 expr = ArrayVariable()
1116 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1121 <COMMA> {list.add(null);}
1125 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1131 * A Method Declaration.
1132 * <b>function</b> MetodDeclarator() Block()
1134 MethodDeclaration MethodDeclaration() :
1136 final MethodDeclaration functionDeclaration;
1138 final OutlineableWithChildren seg = currentSegment;
1144 functionDeclaration = MethodDeclarator(token.sourceStart)
1145 {outlineInfo.addVariable(functionDeclaration.name);}
1146 } catch (ParseException e) {
1147 if (errorMessage != null) throw e;
1148 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1150 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1151 errorEnd = SimpleCharStream.getPosition() + 1;
1154 {currentSegment = functionDeclaration;}
1156 {functionDeclaration.statements = block.statements;
1157 currentSegment = seg;
1158 return functionDeclaration;}
1162 * A MethodDeclarator.
1163 * [&] IDENTIFIER(parameters ...).
1164 * @return a function description for the outline
1166 MethodDeclaration MethodDeclarator(final int start) :
1168 Token identifier = null;
1169 Token reference = null;
1170 final Hashtable formalParameters = new Hashtable();
1171 String identifierChar = SYNTAX_ERROR_CHAR;
1175 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1177 identifier = <IDENTIFIER>
1179 identifierChar = identifier.image;
1180 end = identifier.sourceEnd;
1182 } catch (ParseException e) {
1183 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1185 errorStart = e.currentToken.sourceEnd;
1186 errorEnd = e.currentToken.next.sourceStart;
1187 processParseExceptionDebug(e);
1189 end = FormalParameters(formalParameters)
1191 int nameStart, nameEnd;
1192 if (identifier == null) {
1193 if (reference == null) {
1194 nameStart = start + 9;
1195 nameEnd = start + 10;
1197 nameStart = reference.sourceEnd + 1;
1198 nameEnd = reference.sourceEnd + 2;
1201 nameStart = identifier.sourceStart;
1202 nameEnd = identifier.sourceEnd;
1204 return new MethodDeclaration(currentSegment,
1216 * FormalParameters follows method identifier.
1217 * (FormalParameter())
1219 int FormalParameters(final Hashtable parameters) :
1221 VariableDeclaration var;
1223 Token tok = PHPParser.token;
1224 int end = tok.sourceEnd;
1229 {end = tok.sourceEnd;}
1230 } catch (ParseException e) {
1231 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1233 errorStart = e.currentToken.next.sourceStart;
1234 errorEnd = e.currentToken.next.sourceEnd;
1235 processParseExceptionDebug(e);
1238 var = FormalParameter()
1239 {parameters.put(var.name(),var);end = var.sourceEnd;}
1241 <COMMA> var = FormalParameter()
1242 {parameters.put(var.name(),var);end = var.sourceEnd;}
1247 {end = token.sourceEnd;}
1248 } catch (ParseException e) {
1249 errorMessage = "')' expected";
1251 errorStart = e.currentToken.next.sourceStart;
1252 errorEnd = e.currentToken.next.sourceEnd;
1253 processParseExceptionDebug(e);
1259 * A formal parameter.
1260 * $varname[=value] (,$varname[=value])
1262 VariableDeclaration FormalParameter() :
1264 final VariableDeclaration variableDeclaration;
1268 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1270 if (token != null) {
1271 variableDeclaration.setReference(true);
1273 return variableDeclaration;}
1276 ConstantIdentifier Type() :
1277 {final Token token;}
1279 token = <STRING> {return new ConstantIdentifier(token);}
1280 | token = <BOOL> {return new ConstantIdentifier(token);}
1281 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1282 | token = <REAL> {return new ConstantIdentifier(token);}
1283 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1284 | token = <FLOAT> {return new ConstantIdentifier(token);}
1285 | token = <INT> {return new ConstantIdentifier(token);}
1286 | token = <INTEGER> {return new ConstantIdentifier(token);}
1287 | token = <OBJECT> {return new ConstantIdentifier(token);}
1290 Expression Expression() :
1292 final Expression expr;
1293 Expression initializer = null;
1294 int assignOperator = -1;
1298 expr = ConditionalExpression()
1300 assignOperator = AssignmentOperator()
1302 initializer = Expression()
1303 } catch (ParseException e) {
1304 if (errorMessage != null) {
1307 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1309 errorEnd = SimpleCharStream.getPosition();
1314 if (assignOperator != -1) {// todo : change this, very very bad :(
1315 if (expr instanceof AbstractVariable) {
1316 return new VariableDeclaration(currentSegment,
1317 (AbstractVariable) expr,
1320 initializer.sourceEnd);
1322 String varName = expr.toStringExpression().substring(1);
1323 return new VariableDeclaration(currentSegment,
1324 new Variable(varName,
1328 initializer.sourceEnd);
1332 | expr = ExpressionWBang() {return expr;}
1335 Expression ExpressionWBang() :
1337 final Expression expr;
1341 token = <BANG> expr = ExpressionWBang()
1342 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1343 | expr = ExpressionNoBang() {return expr;}
1346 Expression ExpressionNoBang() :
1351 expr = ListExpression() {return expr;}
1353 expr = PrintExpression() {return expr;}
1357 * Any assignement operator.
1358 * @return the assignement operator id
1360 int AssignmentOperator() :
1363 <ASSIGN> {return VariableDeclaration.EQUAL;}
1364 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1365 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1366 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1367 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1368 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1369 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1370 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1371 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1372 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1373 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1374 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1375 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1378 Expression ConditionalExpression() :
1380 final Expression expr;
1381 Expression expr2 = null;
1382 Expression expr3 = null;
1385 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1387 if (expr3 == null) {
1390 return new ConditionalExpression(expr,expr2,expr3);
1394 Expression ConditionalOrExpression() :
1396 Expression expr,expr2;
1400 expr = ConditionalAndExpression()
1403 <OR_OR> {operator = OperatorIds.OR_OR;}
1404 | <_ORL> {operator = OperatorIds.ORL;}
1406 expr2 = ConditionalAndExpression()
1408 expr = new BinaryExpression(expr,expr2,operator);
1414 Expression ConditionalAndExpression() :
1416 Expression expr,expr2;
1420 expr = ConcatExpression()
1422 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1423 | <_ANDL> {operator = OperatorIds.ANDL;})
1424 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1429 Expression ConcatExpression() :
1431 Expression expr,expr2;
1434 expr = InclusiveOrExpression()
1436 <DOT> expr2 = InclusiveOrExpression()
1437 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1442 Expression InclusiveOrExpression() :
1444 Expression expr,expr2;
1447 expr = ExclusiveOrExpression()
1448 (<BIT_OR> expr2 = ExclusiveOrExpression()
1449 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1454 Expression ExclusiveOrExpression() :
1456 Expression expr,expr2;
1459 expr = AndExpression()
1461 <XOR> expr2 = AndExpression()
1462 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1467 Expression AndExpression() :
1469 Expression expr,expr2;
1472 expr = EqualityExpression()
1475 <BIT_AND> expr2 = EqualityExpression()
1476 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1481 Expression EqualityExpression() :
1483 Expression expr,expr2;
1488 expr = RelationalExpression()
1490 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1491 | token = <DIF> {operator = OperatorIds.DIF;}
1492 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1493 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1494 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1497 expr2 = RelationalExpression()
1498 } catch (ParseException e) {
1499 if (errorMessage != null) {
1502 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1504 errorStart = token.sourceEnd +1;
1505 errorEnd = token.sourceEnd +1;
1506 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1507 processParseExceptionDebug(e);
1510 expr = new BinaryExpression(expr,expr2,operator);
1516 Expression RelationalExpression() :
1518 Expression expr,expr2;
1522 expr = ShiftExpression()
1524 ( <LT> {operator = OperatorIds.LESS;}
1525 | <GT> {operator = OperatorIds.GREATER;}
1526 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1527 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1528 expr2 = ShiftExpression()
1529 {expr = new BinaryExpression(expr,expr2,operator);}
1534 Expression ShiftExpression() :
1536 Expression expr,expr2;
1540 expr = AdditiveExpression()
1542 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1543 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1544 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1545 expr2 = AdditiveExpression()
1546 {expr = new BinaryExpression(expr,expr2,operator);}
1551 Expression AdditiveExpression() :
1553 Expression expr,expr2;
1557 expr = MultiplicativeExpression()
1560 ( <PLUS> {operator = OperatorIds.PLUS;}
1561 | <MINUS> {operator = OperatorIds.MINUS;}
1563 expr2 = MultiplicativeExpression()
1564 {expr = new BinaryExpression(expr,expr2,operator);}
1569 Expression MultiplicativeExpression() :
1571 Expression expr,expr2;
1576 expr = UnaryExpression()
1577 } catch (ParseException e) {
1578 if (errorMessage != null) throw e;
1579 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1581 errorStart = PHPParser.token.sourceStart;
1582 errorEnd = PHPParser.token.sourceEnd;
1586 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1587 | <SLASH> {operator = OperatorIds.DIVIDE;}
1588 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1589 expr2 = UnaryExpression()
1590 {expr = new BinaryExpression(expr,expr2,operator);}
1596 * An unary expression starting with @, & or nothing
1598 Expression UnaryExpression() :
1600 final Expression expr;
1603 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1604 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1606 expr = AtNotTildeUnaryExpression() {return expr;}
1609 Expression AtNotTildeUnaryExpression() :
1611 final Expression expr;
1616 expr = AtNotTildeUnaryExpression()
1617 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1620 expr = AtNotTildeUnaryExpression()
1621 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1624 expr = AtNotUnaryExpression()
1625 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1627 expr = UnaryExpressionNoPrefix()
1632 * An expression prefixed (or not) by one or more @ and !.
1633 * @return the expression
1635 Expression AtNotUnaryExpression() :
1637 final Expression expr;
1642 expr = AtNotUnaryExpression()
1643 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1646 expr = AtNotUnaryExpression()
1647 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1649 expr = UnaryExpressionNoPrefix()
1653 Expression UnaryExpressionNoPrefix() :
1655 final Expression expr;
1659 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1661 token.sourceStart);}
1663 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1665 token.sourceStart);}
1667 expr = PreIncDecExpression()
1670 expr = UnaryExpressionNotPlusMinus()
1675 Expression PreIncDecExpression() :
1677 final Expression expr;
1683 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1685 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1687 expr = PrimaryExpression()
1688 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1691 Expression UnaryExpressionNotPlusMinus() :
1693 final Expression expr;
1696 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1697 expr = CastExpression() {return expr;}
1698 | expr = PostfixExpression() {return expr;}
1699 | expr = Literal() {return expr;}
1700 | <LPAREN> expr = Expression()
1703 } catch (ParseException e) {
1704 errorMessage = "')' expected";
1706 errorStart = expr.sourceEnd +1;
1707 errorEnd = expr.sourceEnd +1;
1708 processParseExceptionDebug(e);
1713 CastExpression CastExpression() :
1715 final ConstantIdentifier type;
1716 final Expression expr;
1717 final Token token,token1;
1724 token = <ARRAY> {type = new ConstantIdentifier(token);}
1726 <RPAREN> expr = UnaryExpression()
1727 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1730 Expression PostfixExpression() :
1732 final Expression expr;
1737 expr = PrimaryExpression()
1739 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1741 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1744 if (operator == -1) {
1747 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1751 Expression PrimaryExpression() :
1757 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1760 expr = ArrayDeclarator()
1764 Expression refPrimaryExpression(final Token reference) :
1767 Expression expr2 = null;
1768 final Token identifier;
1771 identifier = <IDENTIFIER>
1773 expr = new ConstantIdentifier(identifier);
1776 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1777 {expr = new ClassAccess(expr,
1779 ClassAccess.STATIC);}
1781 [ expr2 = Arguments(expr) ]
1783 if (expr2 == null) {
1784 if (reference != null) {
1785 ParseException e = generateParseException();
1786 errorMessage = "you cannot use a constant by reference";
1788 errorStart = reference.sourceStart;
1789 errorEnd = reference.sourceEnd;
1790 processParseExceptionDebug(e);
1797 expr = VariableDeclaratorId() //todo use the reference parameter ...
1798 [ expr = Arguments(expr) ]
1802 expr = ClassIdentifier()
1805 if (reference == null) {
1806 start = token.sourceStart;
1808 start = reference.sourceStart;
1810 expr = new ClassInstantiation(expr,
1814 [ expr = Arguments(expr) ]
1819 * An array declarator.
1823 ArrayInitializer ArrayDeclarator() :
1825 final ArrayVariableDeclaration[] vars;
1829 token = <ARRAY> vars = ArrayInitializer()
1830 {return new ArrayInitializer(vars,
1832 PHPParser.token.sourceEnd);}
1835 Expression ClassIdentifier():
1837 final Expression expr;
1841 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1842 | expr = Type() {return expr;}
1843 | expr = VariableDeclaratorId() {return expr;}
1847 * Used by Variabledeclaratorid and primarysuffix
1849 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1851 Expression expression = null;
1852 final Token classAccessToken,lbrace,rbrace;
1857 classAccessToken = <CLASSACCESS>
1860 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1862 expression = new Variable(expression,
1867 token = <IDENTIFIER>
1868 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1870 expression = Variable()
1872 } catch (ParseException e) {
1873 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1875 errorStart = classAccessToken.sourceEnd +1;
1876 errorEnd = classAccessToken.sourceEnd +1;
1877 processParseExceptionDebug(e);
1879 {return new ClassAccess(prefix,
1881 ClassAccess.NORMAL);}
1883 token = <LBRACKET> {pos = token.sourceEnd+1;}
1884 [ expression = Expression() {pos = expression.sourceEnd+1;}
1885 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1888 {pos = token.sourceEnd;}
1889 } catch (ParseException e) {
1890 errorMessage = "']' expected";
1894 processParseExceptionDebug(e);
1896 {return new ArrayDeclarator(prefix,expression,pos);}
1898 token = <LBRACE> {pos = token.sourceEnd+1;}
1899 [ expression = Expression() {pos = expression.sourceEnd+1;}
1900 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1903 {pos = token.sourceEnd;}
1904 } catch (ParseException e) {
1905 errorMessage = "']' expected";
1909 processParseExceptionDebug(e);
1911 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1919 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1920 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1921 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1922 | token = <TRUE> {return new TrueLiteral(token);}
1923 | token = <FALSE> {return new FalseLiteral(token);}
1924 | token = <NULL> {return new NullLiteral(token);}
1927 FunctionCall Arguments(final Expression func) :
1929 Expression[] args = null;
1933 <LPAREN> [ args = ArgumentList() ]
1936 {return new FunctionCall(func,args,token.sourceEnd);}
1937 } catch (ParseException e) {
1938 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
1940 errorStart = args[args.length-1].sourceEnd+1;
1941 errorEnd = args[args.length-1].sourceEnd+1;
1942 processParseExceptionDebug(e);
1944 {return new FunctionCall(func,args,args[args.length-1].sourceEnd);}
1948 * An argument list is a list of arguments separated by comma :
1949 * argumentDeclaration() (, argumentDeclaration)*
1950 * @return an array of arguments
1952 Expression[] ArgumentList() :
1955 final ArrayList list = new ArrayList();
1961 {list.add(arg);pos = arg.sourceEnd;}
1962 ( token = <COMMA> {pos = token.sourceEnd;}
1966 pos = arg.sourceEnd;}
1967 } catch (ParseException e) {
1968 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
1972 processParseException(e);
1976 final Expression[] arguments = new Expression[list.size()];
1977 list.toArray(arguments);
1982 * A Statement without break.
1983 * @return a statement
1985 Statement StatementNoBreak() :
1987 final Statement statement;
1992 statement = expressionStatement() {return statement;}
1994 statement = LabeledStatement() {return statement;}
1995 | statement = Block() {return statement;}
1996 | statement = EmptyStatement() {return statement;}
1997 | statement = SwitchStatement() {return statement;}
1998 | statement = IfStatement() {return statement;}
1999 | statement = WhileStatement() {return statement;}
2000 | statement = DoStatement() {return statement;}
2001 | statement = ForStatement() {return statement;}
2002 | statement = ForeachStatement() {return statement;}
2003 | statement = ContinueStatement() {return statement;}
2004 | statement = ReturnStatement() {return statement;}
2005 | statement = EchoStatement() {return statement;}
2006 | [token=<AT>] statement = IncludeStatement()
2007 {if (token != null) {
2008 ((InclusionStatement)statement).silent = true;
2009 statement.sourceStart = token.sourceStart;
2012 | statement = StaticStatement() {return statement;}
2013 | statement = GlobalStatement() {return statement;}
2014 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2018 * A statement expression.
2020 * @return an expression
2022 Statement expressionStatement() :
2024 final Statement statement;
2028 statement = Expression()
2031 {statement.sourceEnd = token.sourceEnd;}
2032 } catch (ParseException e) {
2033 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2034 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2036 errorStart = statement.sourceEnd+1;
2037 errorEnd = statement.sourceEnd+1;
2038 processParseExceptionDebug(e);
2044 Define defineStatement() :
2046 Expression defineName,defineValue;
2047 final Token defineToken;
2052 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2055 {pos = token.sourceEnd+1;}
2056 } catch (ParseException e) {
2057 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2061 processParseExceptionDebug(e);
2064 defineName = Expression()
2065 {pos = defineName.sourceEnd+1;}
2066 } catch (ParseException e) {
2067 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2071 processParseExceptionDebug(e);
2072 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2076 {pos = defineName.sourceEnd+1;}
2077 } catch (ParseException e) {
2078 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2082 processParseExceptionDebug(e);
2085 defineValue = Expression()
2086 {pos = defineValue.sourceEnd+1;}
2087 } catch (ParseException e) {
2088 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2092 processParseExceptionDebug(e);
2093 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2097 {pos = token.sourceEnd+1;}
2098 } catch (ParseException e) {
2099 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2103 processParseExceptionDebug(e);
2105 {return new Define(currentSegment,
2108 defineToken.sourceStart,
2113 * A Normal statement.
2115 Statement Statement() :
2117 final Statement statement;
2120 statement = StatementNoBreak() {return statement;}
2121 | statement = BreakStatement() {return statement;}
2125 * An html block inside a php syntax.
2127 HTMLBlock htmlBlock() :
2129 final int startIndex = nodePtr;
2130 final AstNode[] blockNodes;
2136 {htmlStart = phpEnd.sourceEnd;}
2139 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2140 {PHPParser.createNewHTMLCode();}
2141 } catch (ParseException e) {
2142 errorMessage = "unexpected end of file , '<?php' expected";
2144 errorStart = SimpleCharStream.getPosition();
2145 errorEnd = SimpleCharStream.getPosition();
2149 nbNodes = nodePtr - startIndex;
2153 blockNodes = new AstNode[nbNodes];
2154 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2155 nodePtr = startIndex;
2156 return new HTMLBlock(blockNodes);}
2160 * An include statement. It's "include" an expression;
2162 InclusionStatement IncludeStatement() :
2166 final InclusionStatement inclusionStatement;
2167 final Token token, token2;
2171 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2172 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2173 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2174 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2177 {pos = expr.sourceEnd;}
2178 } catch (ParseException e) {
2179 if (errorMessage != null) {
2182 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2184 errorStart = e.currentToken.next.sourceStart;
2185 errorEnd = e.currentToken.next.sourceEnd;
2186 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2187 processParseExceptionDebug(e);
2190 token2 = <SEMICOLON>
2191 {pos=token2.sourceEnd;}
2192 } catch (ParseException e) {
2193 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2195 errorStart = e.currentToken.next.sourceStart;
2196 errorEnd = e.currentToken.next.sourceEnd;
2197 processParseExceptionDebug(e);
2200 inclusionStatement = new InclusionStatement(currentSegment,
2205 currentSegment.add(inclusionStatement);
2206 return inclusionStatement;
2210 PrintExpression PrintExpression() :
2212 final Expression expr;
2213 final Token printToken;
2216 token = <PRINT> expr = Expression()
2217 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2220 ListExpression ListExpression() :
2222 Expression expr = null;
2223 final Expression expression;
2224 final ArrayList list = new ArrayList();
2226 final Token listToken, rParen;
2230 listToken = <LIST> {pos = listToken.sourceEnd;}
2232 token = <LPAREN> {pos = token.sourceEnd;}
2233 } catch (ParseException e) {
2234 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2236 errorStart = listToken.sourceEnd+1;
2237 errorEnd = listToken.sourceEnd+1;
2238 processParseExceptionDebug(e);
2241 expr = VariableDeclaratorId()
2242 {list.add(expr);pos = expr.sourceEnd;}
2244 {if (expr == null) list.add(null);}
2248 {pos = token.sourceEnd;}
2249 } catch (ParseException e) {
2250 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2254 processParseExceptionDebug(e);
2256 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2260 {pos = rParen.sourceEnd;}
2261 } catch (ParseException e) {
2262 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2266 processParseExceptionDebug(e);
2268 [ <ASSIGN> expression = Expression()
2270 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2272 return new ListExpression(vars,
2274 listToken.sourceStart,
2275 expression.sourceEnd);}
2278 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2280 return new ListExpression(vars,listToken.sourceStart,pos);}
2284 * An echo statement.
2285 * echo anyexpression (, otherexpression)*
2287 EchoStatement EchoStatement() :
2289 final ArrayList expressions = new ArrayList();
2292 Token token2 = null;
2295 token = <ECHO> expr = Expression()
2296 {expressions.add(expr);}
2298 <COMMA> expr = Expression()
2299 {expressions.add(expr);}
2302 token2 = <SEMICOLON>
2303 } catch (ParseException e) {
2304 if (e.currentToken.next.kind != 4) {
2305 errorMessage = "';' expected after 'echo' statement";
2307 errorStart = e.currentToken.sourceEnd;
2308 errorEnd = e.currentToken.sourceEnd;
2309 processParseExceptionDebug(e);
2313 final Expression[] exprs = new Expression[expressions.size()];
2314 expressions.toArray(exprs);
2315 if (token2 == null) {
2316 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2318 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2322 GlobalStatement GlobalStatement() :
2325 final ArrayList vars = new ArrayList();
2326 final GlobalStatement global;
2327 final Token token, token2;
2333 {vars.add(expr);pos = expr.sourceEnd+1;}
2336 {vars.add(expr);pos = expr.sourceEnd+1;}
2339 token2 = <SEMICOLON>
2340 {pos = token2.sourceEnd+1;}
2341 } catch (ParseException e) {
2342 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2346 processParseExceptionDebug(e);
2349 final Variable[] variables = new Variable[vars.size()];
2350 vars.toArray(variables);
2351 global = new GlobalStatement(currentSegment,
2355 currentSegment.add(global);
2359 StaticStatement StaticStatement() :
2361 final ArrayList vars = new ArrayList();
2362 VariableDeclaration expr;
2363 final Token token, token2;
2367 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2369 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2372 token2 = <SEMICOLON>
2373 {pos = token2.sourceEnd+1;}
2374 } catch (ParseException e) {
2375 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2379 processParseException(e);
2382 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2383 vars.toArray(variables);
2384 return new StaticStatement(variables,
2389 LabeledStatement LabeledStatement() :
2392 final Statement statement;
2395 label = <IDENTIFIER> <COLON> statement = Statement()
2396 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2408 final ArrayList list = new ArrayList();
2409 Statement statement;
2410 final Token token, token2;
2416 {pos = token.sourceEnd+1;start=token.sourceStart;}
2417 } catch (ParseException e) {
2418 errorMessage = "'{' expected";
2420 pos = PHPParser.token.sourceEnd+1;
2424 processParseExceptionDebug(e);
2426 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2427 | statement = htmlBlock() {if (statement != null) {
2428 list.add(statement);
2429 pos = statement.sourceEnd+1;
2431 pos = PHPParser.token.sourceEnd+1;
2436 {pos = token2.sourceEnd+1;}
2437 } catch (ParseException e) {
2438 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2442 processParseExceptionDebug(e);
2445 final Statement[] statements = new Statement[list.size()];
2446 list.toArray(statements);
2447 return new Block(statements,start,pos);}
2450 Statement BlockStatement() :
2452 final Statement statement;
2456 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2458 } catch (ParseException e) {
2459 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2461 errorStart = e.currentToken.sourceStart;
2462 errorEnd = e.currentToken.sourceEnd;
2465 | statement = ClassDeclaration() {return statement;}
2466 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2467 currentSegment.add((MethodDeclaration) statement);
2468 ((MethodDeclaration) statement).analyzeCode();
2473 * A Block statement that will not contain any 'break'
2475 Statement BlockStatementNoBreak() :
2477 final Statement statement;
2480 statement = StatementNoBreak() {return statement;}
2481 | statement = ClassDeclaration() {return statement;}
2482 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2483 ((MethodDeclaration) statement).analyzeCode();
2488 * used only by ForInit()
2490 Expression[] LocalVariableDeclaration() :
2492 final ArrayList list = new ArrayList();
2498 ( <COMMA> var = Expression() {list.add(var);})*
2500 final Expression[] vars = new Expression[list.size()];
2507 * used only by LocalVariableDeclaration().
2509 VariableDeclaration LocalVariableDeclarator() :
2511 final Variable varName;
2512 Expression initializer = null;
2515 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2517 if (initializer == null) {
2518 return new VariableDeclaration(currentSegment,
2520 varName.sourceStart,
2523 return new VariableDeclaration(currentSegment,
2526 VariableDeclaration.EQUAL,
2527 varName.sourceStart);
2531 EmptyStatement EmptyStatement() :
2537 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2541 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2543 Expression StatementExpression() :
2545 final Expression expr;
2546 final Token operator;
2549 expr = PreIncDecExpression() {return expr;}
2551 expr = PrimaryExpression()
2552 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2553 OperatorIds.PLUS_PLUS,
2554 operator.sourceEnd);}
2555 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2556 OperatorIds.MINUS_MINUS,
2557 operator.sourceEnd);}
2562 SwitchStatement SwitchStatement() :
2564 Expression variable;
2565 final AbstractCase[] cases;
2566 final Token switchToken,lparenToken,rparenToken;
2570 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2572 lparenToken = <LPAREN>
2573 {pos = lparenToken.sourceEnd+1;}
2574 } catch (ParseException e) {
2575 errorMessage = "'(' expected after 'switch'";
2579 processParseExceptionDebug(e);
2582 variable = Expression() {pos = variable.sourceEnd+1;}
2583 } catch (ParseException e) {
2584 if (errorMessage != null) {
2587 errorMessage = "expression expected";
2591 processParseExceptionDebug(e);
2592 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2595 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2596 } catch (ParseException e) {
2597 errorMessage = "')' expected";
2601 processParseExceptionDebug(e);
2603 ( cases = switchStatementBrace()
2604 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2605 {return new SwitchStatement(variable,
2607 switchToken.sourceStart,
2608 PHPParser.token.sourceEnd);}
2611 AbstractCase[] switchStatementBrace() :
2614 final ArrayList cases = new ArrayList();
2619 token = <LBRACE> {pos = token.sourceEnd;}
2620 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2623 {pos = token.sourceEnd;}
2624 } catch (ParseException e) {
2625 errorMessage = "'}' expected";
2629 processParseExceptionDebug(e);
2632 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2633 cases.toArray(abcase);
2639 * A Switch statement with : ... endswitch;
2640 * @param start the begin offset of the switch
2641 * @param end the end offset of the switch
2643 AbstractCase[] switchStatementColon(final int start, final int end) :
2646 final ArrayList cases = new ArrayList();
2651 token = <COLON> {pos = token.sourceEnd;}
2653 setMarker(fileToParse,
2654 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2658 "Line " + token.beginLine);
2659 } catch (CoreException e) {
2660 PHPeclipsePlugin.log(e);
2662 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2664 token = <ENDSWITCH> {pos = token.sourceEnd;}
2665 } catch (ParseException e) {
2666 errorMessage = "'endswitch' expected";
2670 processParseExceptionDebug(e);
2673 token = <SEMICOLON> {pos = token.sourceEnd;}
2674 } catch (ParseException e) {
2675 errorMessage = "';' expected after 'endswitch' keyword";
2679 processParseExceptionDebug(e);
2682 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2683 cases.toArray(abcase);
2688 AbstractCase switchLabel0() :
2690 final Expression expr;
2691 Statement statement;
2692 final ArrayList stmts = new ArrayList();
2693 final Token token = PHPParser.token;
2696 expr = SwitchLabel()
2697 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2698 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2699 | statement = BreakStatement() {stmts.add(statement);})*
2700 //[ statement = BreakStatement() {stmts.add(statement);}]
2702 final int listSize = stmts.size();
2703 final Statement[] stmtsArray = new Statement[listSize];
2704 stmts.toArray(stmtsArray);
2705 if (expr == null) {//it's a default
2706 return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd);
2708 if (listSize != 0) {
2709 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2711 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2718 * case Expression() :
2720 * @return the if it was a case and null if not
2722 Expression SwitchLabel() :
2724 final Expression expr;
2730 } catch (ParseException e) {
2731 if (errorMessage != null) throw e;
2732 errorMessage = "expression expected after 'case' keyword";
2734 errorStart = token.sourceEnd +1;
2735 errorEnd = token.sourceEnd +1;
2741 } catch (ParseException e) {
2742 errorMessage = "':' expected after case expression";
2744 errorStart = expr.sourceEnd+1;
2745 errorEnd = expr.sourceEnd+1;
2746 processParseExceptionDebug(e);
2753 } catch (ParseException e) {
2754 errorMessage = "':' expected after 'default' keyword";
2756 errorStart = token.sourceEnd+1;
2757 errorEnd = token.sourceEnd+1;
2758 processParseExceptionDebug(e);
2762 Break BreakStatement() :
2764 Expression expression = null;
2765 final Token token, token2;
2769 token = <BREAK> {pos = token.sourceEnd+1;}
2770 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2772 token2 = <SEMICOLON>
2773 {pos = token2.sourceEnd;}
2774 } catch (ParseException e) {
2775 errorMessage = "';' expected after 'break' keyword";
2779 processParseExceptionDebug(e);
2781 {return new Break(expression, token.sourceStart, pos);}
2784 IfStatement IfStatement() :
2786 final Expression condition;
2787 final IfStatement ifStatement;
2791 token = <IF> condition = Condition("if")
2792 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2793 {return ifStatement;}
2797 Expression Condition(final String keyword) :
2799 final Expression condition;
2804 } catch (ParseException e) {
2805 errorMessage = "'(' expected after " + keyword + " keyword";
2807 errorStart = PHPParser.token.sourceEnd + 1;
2808 errorEnd = PHPParser.token.sourceEnd + 1;
2809 processParseExceptionDebug(e);
2811 condition = Expression()
2814 } catch (ParseException e) {
2815 errorMessage = "')' expected after " + keyword + " keyword";
2817 errorStart = condition.sourceEnd+1;
2818 errorEnd = condition.sourceEnd+1;
2819 processParseExceptionDebug(e);
2824 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2826 Statement statement;
2827 final Statement stmt;
2828 final Statement[] statementsArray;
2829 ElseIf elseifStatement;
2830 Else elseStatement = null;
2831 final ArrayList stmts;
2832 final ArrayList elseIfList = new ArrayList();
2833 final ElseIf[] elseIfs;
2834 int pos = SimpleCharStream.getPosition();
2835 final int endStatements;
2839 {stmts = new ArrayList();}
2840 ( statement = Statement() {stmts.add(statement);}
2841 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2842 {endStatements = SimpleCharStream.getPosition();}
2843 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2844 [elseStatement = ElseStatementColon()]
2847 setMarker(fileToParse,
2848 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2852 "Line " + token.beginLine);
2853 } catch (CoreException e) {
2854 PHPeclipsePlugin.log(e);
2858 } catch (ParseException e) {
2859 errorMessage = "'endif' expected";
2861 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2862 errorEnd = SimpleCharStream.getPosition() + 1;
2867 } catch (ParseException e) {
2868 errorMessage = "';' expected after 'endif' keyword";
2870 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2871 errorEnd = SimpleCharStream.getPosition() + 1;
2875 elseIfs = new ElseIf[elseIfList.size()];
2876 elseIfList.toArray(elseIfs);
2877 if (stmts.size() == 1) {
2878 return new IfStatement(condition,
2879 (Statement) stmts.get(0),
2883 SimpleCharStream.getPosition());
2885 statementsArray = new Statement[stmts.size()];
2886 stmts.toArray(statementsArray);
2887 return new IfStatement(condition,
2888 new Block(statementsArray,pos,endStatements),
2892 SimpleCharStream.getPosition());
2897 (stmt = Statement() | stmt = htmlBlock())
2898 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
2902 {pos = SimpleCharStream.getPosition();}
2903 statement = Statement()
2904 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
2905 } catch (ParseException e) {
2906 if (errorMessage != null) {
2909 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
2911 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2912 errorEnd = SimpleCharStream.getPosition() + 1;
2917 elseIfs = new ElseIf[elseIfList.size()];
2918 elseIfList.toArray(elseIfs);
2919 return new IfStatement(condition,
2924 SimpleCharStream.getPosition());}
2927 ElseIf ElseIfStatementColon() :
2929 final Expression condition;
2930 Statement statement;
2931 final ArrayList list = new ArrayList();
2932 final Token elseifToken;
2935 elseifToken = <ELSEIF> condition = Condition("elseif")
2936 <COLON> ( statement = Statement() {list.add(statement);}
2937 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
2939 final int sizeList = list.size();
2940 final Statement[] stmtsArray = new Statement[sizeList];
2941 list.toArray(stmtsArray);
2942 return new ElseIf(condition,stmtsArray ,
2943 elseifToken.sourceStart,
2944 stmtsArray[sizeList-1].sourceEnd);}
2947 Else ElseStatementColon() :
2949 Statement statement;
2950 final ArrayList list = new ArrayList();
2951 final Token elseToken;
2954 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
2955 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
2957 final int sizeList = list.size();
2958 final Statement[] stmtsArray = new Statement[sizeList];
2959 list.toArray(stmtsArray);
2960 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
2963 ElseIf ElseIfStatement() :
2965 final Expression condition;
2966 //final Statement statement;
2967 final Token elseifToken;
2968 final Statement[] statement = new Statement[1];
2971 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
2973 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
2976 WhileStatement WhileStatement() :
2978 final Expression condition;
2979 final Statement action;
2980 final Token whileToken;
2983 whileToken = <WHILE>
2984 condition = Condition("while")
2985 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
2986 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
2989 Statement WhileStatement0(final int start, final int end) :
2991 Statement statement;
2992 final ArrayList stmts = new ArrayList();
2993 final int pos = SimpleCharStream.getPosition();
2996 <COLON> (statement = Statement() {stmts.add(statement);})*
2998 setMarker(fileToParse,
2999 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3003 "Line " + token.beginLine);
3004 } catch (CoreException e) {
3005 PHPeclipsePlugin.log(e);
3009 } catch (ParseException e) {
3010 errorMessage = "'endwhile' expected";
3012 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3013 errorEnd = SimpleCharStream.getPosition() + 1;
3019 final Statement[] stmtsArray = new Statement[stmts.size()];
3020 stmts.toArray(stmtsArray);
3021 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3022 } catch (ParseException e) {
3023 errorMessage = "';' expected after 'endwhile' keyword";
3025 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3026 errorEnd = SimpleCharStream.getPosition() + 1;
3030 statement = Statement()
3034 DoStatement DoStatement() :
3036 final Statement action;
3037 final Expression condition;
3039 Token token2 = null;
3042 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3044 token2 = <SEMICOLON>
3045 } catch (ParseException e) {
3046 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3048 errorStart = condition.sourceEnd+1;
3049 errorEnd = condition.sourceEnd+1;
3050 processParseExceptionDebug(e);
3053 if (token2 == null) {
3054 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3056 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3060 ForeachStatement ForeachStatement() :
3062 Statement statement = null;
3063 Expression expression = null;
3064 ArrayVariableDeclaration variable = null;
3066 Token lparenToken = null;
3067 Token asToken = null;
3068 Token rparenToken = null;
3072 foreachToken = <FOREACH>
3074 lparenToken = <LPAREN>
3075 {pos = lparenToken.sourceEnd+1;}
3076 } catch (ParseException e) {
3077 errorMessage = "'(' expected after 'foreach' keyword";
3079 errorStart = foreachToken.sourceEnd+1;
3080 errorEnd = foreachToken.sourceEnd+1;
3081 processParseExceptionDebug(e);
3082 {pos = foreachToken.sourceEnd+1;}
3085 expression = Expression()
3086 {pos = expression.sourceEnd+1;}
3087 } catch (ParseException e) {
3088 errorMessage = "variable expected";
3092 processParseExceptionDebug(e);
3096 {pos = asToken.sourceEnd+1;}
3097 } catch (ParseException e) {
3098 errorMessage = "'as' expected";
3102 processParseExceptionDebug(e);
3105 variable = ArrayVariable()
3106 {pos = variable.sourceEnd+1;}
3107 } catch (ParseException e) {
3108 if (errorMessage != null) throw e;
3109 errorMessage = "variable expected";
3113 processParseExceptionDebug(e);
3116 rparenToken = <RPAREN>
3117 {pos = rparenToken.sourceEnd+1;}
3118 } catch (ParseException e) {
3119 errorMessage = "')' expected after 'foreach' keyword";
3123 processParseExceptionDebug(e);
3126 statement = Statement()
3127 {pos = rparenToken.sourceEnd+1;}
3128 } catch (ParseException e) {
3129 if (errorMessage != null) throw e;
3130 errorMessage = "statement expected";
3134 processParseExceptionDebug(e);
3136 {return new ForeachStatement(expression,
3139 foreachToken.sourceStart,
3140 statement.sourceEnd);}
3145 * a for declaration.
3146 * @return a node representing the for statement
3148 ForStatement ForStatement() :
3150 final Token token,tokenEndFor,token2,tokenColon;
3152 Expression[] initializations = null;
3153 Expression condition = null;
3154 Expression[] increments = null;
3156 final ArrayList list = new ArrayList();
3162 } catch (ParseException e) {
3163 errorMessage = "'(' expected after 'for' keyword";
3165 errorStart = token.sourceEnd;
3166 errorEnd = token.sourceEnd +1;
3167 processParseExceptionDebug(e);
3169 [ initializations = ForInit() ] <SEMICOLON>
3170 [ condition = Expression() ] <SEMICOLON>
3171 [ increments = StatementExpressionList() ] <RPAREN>
3173 action = Statement()
3174 {return new ForStatement(initializations,
3181 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3182 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3185 setMarker(fileToParse,
3186 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3190 "Line " + token.beginLine);
3191 } catch (CoreException e) {
3192 PHPeclipsePlugin.log(e);
3196 tokenEndFor = <ENDFOR>
3197 {pos = tokenEndFor.sourceEnd+1;}
3198 } catch (ParseException e) {
3199 errorMessage = "'endfor' expected";
3203 processParseExceptionDebug(e);
3206 token2 = <SEMICOLON>
3207 {pos = token2.sourceEnd+1;}
3208 } catch (ParseException e) {
3209 errorMessage = "';' expected after 'endfor' keyword";
3213 processParseExceptionDebug(e);
3216 final Statement[] stmtsArray = new Statement[list.size()];
3217 list.toArray(stmtsArray);
3218 return new ForStatement(initializations,
3221 new Block(stmtsArray,
3222 stmtsArray[0].sourceStart,
3223 stmtsArray[stmtsArray.length-1].sourceEnd),
3229 Expression[] ForInit() :
3231 final Expression[] exprs;
3234 LOOKAHEAD(LocalVariableDeclaration())
3235 exprs = LocalVariableDeclaration()
3238 exprs = StatementExpressionList()
3242 Expression[] StatementExpressionList() :
3244 final ArrayList list = new ArrayList();
3245 final Expression expr;
3248 expr = Expression() {list.add(expr);}
3249 (<COMMA> Expression() {list.add(expr);})*
3251 final Expression[] exprsArray = new Expression[list.size()];
3252 list.toArray(exprsArray);
3257 Continue ContinueStatement() :
3259 Expression expr = null;
3261 Token token2 = null;
3264 token = <CONTINUE> [ expr = Expression() ]
3266 token2 = <SEMICOLON>
3267 } catch (ParseException e) {
3268 errorMessage = "';' expected after 'continue' statement";
3271 errorStart = token.sourceEnd+1;
3272 errorEnd = token.sourceEnd+1;
3274 errorStart = expr.sourceEnd+1;
3275 errorEnd = expr.sourceEnd+1;
3277 processParseExceptionDebug(e);
3280 if (token2 == null) {
3282 return new Continue(expr,token.sourceStart,token.sourceEnd);
3284 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3286 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3290 ReturnStatement ReturnStatement() :
3292 Expression expr = null;
3294 Token token2 = null;
3297 token = <RETURN> [ expr = Expression() ]
3299 token2 = <SEMICOLON>
3300 } catch (ParseException e) {
3301 errorMessage = "';' expected after 'return' statement";
3304 errorStart = token.sourceEnd+1;
3305 errorEnd = token.sourceEnd+1;
3307 errorStart = expr.sourceEnd+1;
3308 errorEnd = expr.sourceEnd+1;
3310 processParseExceptionDebug(e);
3313 if (token2 == null) {
3315 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3317 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3319 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);