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 < 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_2> | <STRING_3>)>
593 //| <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
594 | <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
595 | <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
598 <IN_STRING,DOLLAR_IN_STRING> SKIP :
600 <ESCAPED : ("\\" ~[])> : IN_STRING
605 <DOUBLEQUOTE : "\""> : IN_STRING
611 <DOLLARS : "$"> : DOLLAR_IN_STRING
614 <IN_STRING,DOLLAR_IN_STRING> TOKEN :
616 <DOUBLEQUOTE2 : "\""> : PHPPARSING
619 <DOLLAR_IN_STRING> TOKEN :
621 <LBRACE1 : "{"> : DOLLAR_IN_STRING_EXPR
624 <IN_STRING> SPECIAL_TOKEN :
629 <SKIPSTRING> SPECIAL_TOKEN :
639 <DOLLAR_IN_STRING_EXPR> TOKEN :
641 <RBRACE1 : "}"> : DOLLAR_IN_STRING
644 <DOLLAR_IN_STRING_EXPR> TOKEN :
654 <DOLLAR_IN_STRING_EXPR,IN_STRING> SKIP :
661 <PHPPARSING,IN_VARIABLE> TOKEN : {<DOLLAR : "$"> : IN_VARIABLE}
664 <PHPPARSING, IN_VARIABLE, DOLLAR_IN_STRING> TOKEN :
666 <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
669 ["a"-"z"] | ["A"-"Z"]
677 "_" | ["\u007f"-"\u00ff"]
681 <DOLLAR_IN_STRING> SPECIAL_TOKEN :
687 <PHPPARSING,IN_VARIABLE> TOKEN :
689 <LPAREN : "("> : PHPPARSING
690 | <RPAREN : ")"> : PHPPARSING
691 | <LBRACE : "{"> : PHPPARSING
692 | <RBRACE : "}"> : PHPPARSING
693 | <LBRACKET : "["> : PHPPARSING
694 | <RBRACKET : "]"> : PHPPARSING
695 | <SEMICOLON : ";"> : PHPPARSING
696 | <COMMA : ","> : PHPPARSING
697 | <DOT : "."> : PHPPARSING
702 <PHPPARSING,IN_VARIABLE> TOKEN :
704 <GT : ">"> : PHPPARSING
705 | <LT : "<"> : PHPPARSING
706 | <EQUAL_EQUAL : "=="> : PHPPARSING
707 | <LE : "<="> : PHPPARSING
708 | <GE : ">="> : PHPPARSING
709 | <NOT_EQUAL : "!="> : PHPPARSING
710 | <DIF : "<>"> : PHPPARSING
711 | <BANGDOUBLEEQUAL : "!=="> : PHPPARSING
712 | <TRIPLEEQUAL : "==="> : PHPPARSING
716 <PHPPARSING,IN_VARIABLE> TOKEN :
718 <ASSIGN : "="> : PHPPARSING
719 | <PLUSASSIGN : "+="> : PHPPARSING
720 | <MINUSASSIGN : "-="> : PHPPARSING
721 | <STARASSIGN : "*="> : PHPPARSING
722 | <SLASHASSIGN : "/="> : PHPPARSING
723 | <ANDASSIGN : "&="> : PHPPARSING
724 | <ORASSIGN : "|="> : PHPPARSING
725 | <XORASSIGN : "^="> : PHPPARSING
726 | <DOTASSIGN : ".="> : PHPPARSING
727 | <REMASSIGN : "%="> : PHPPARSING
728 | <TILDEEQUAL : "~="> : PHPPARSING
729 | <LSHIFTASSIGN : "<<="> : PHPPARSING
730 | <RSIGNEDSHIFTASSIGN : ">>="> : PHPPARSING
745 {PHPParser.createNewHTMLCode();}
746 } catch (TokenMgrError e) {
747 PHPeclipsePlugin.log(e);
748 errorStart = SimpleCharStream.beginOffset;
749 errorEnd = SimpleCharStream.endOffset;
750 errorMessage = e.getMessage();
752 throw generateParseException();
757 * A php block is a <?= expression [;]?>
758 * or <?php somephpcode ?>
759 * or <? somephpcode ?>
763 final PHPEchoBlock phpEchoBlock;
764 final Token token,phpEnd;
767 phpEchoBlock = phpEchoBlock()
768 {pushOnAstNodes(phpEchoBlock);}
771 | token = <PHPSTARTSHORT>
773 setMarker(fileToParse,
774 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
778 "Line " + token.beginLine);
779 } catch (CoreException e) {
780 PHPeclipsePlugin.log(e);
783 {PHPParser.createNewHTMLCode();}
787 {htmlStart = phpEnd.sourceEnd;}
788 } catch (ParseException e) {
789 errorMessage = "'?>' expected";
791 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
792 errorEnd = SimpleCharStream.getPosition() + 1;
793 processParseExceptionDebug(e);
797 PHPEchoBlock phpEchoBlock() :
799 final Expression expr;
800 final PHPEchoBlock echoBlock;
801 final Token token, token2;
804 token = <PHPECHOSTART> {PHPParser.createNewHTMLCode();}
805 expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
807 htmlStart = token2.sourceEnd;
809 echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
810 pushOnAstNodes(echoBlock);
820 ClassDeclaration ClassDeclaration() :
822 final ClassDeclaration classDeclaration;
823 Token className = null;
824 final Token superclassName, token, extendsToken;
825 String classNameImage = SYNTAX_ERROR_CHAR;
826 String superclassNameImage = null;
832 className = <IDENTIFIER>
833 {classNameImage = className.image;}
834 } catch (ParseException e) {
835 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
837 errorStart = token.sourceEnd+1;
838 errorEnd = token.sourceEnd+1;
839 processParseExceptionDebug(e);
842 extendsToken = <EXTENDS>
844 superclassName = <IDENTIFIER>
845 {superclassNameImage = superclassName.image;}
846 } catch (ParseException e) {
847 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
849 errorStart = extendsToken.sourceEnd+1;
850 errorEnd = extendsToken.sourceEnd+1;
851 processParseExceptionDebug(e);
852 superclassNameImage = SYNTAX_ERROR_CHAR;
857 if (className == null) {
858 start = token.sourceStart;
859 end = token.sourceEnd;
861 start = className.sourceStart;
862 end = className.sourceEnd;
864 if (superclassNameImage == null) {
866 classDeclaration = new ClassDeclaration(currentSegment,
871 classDeclaration = new ClassDeclaration(currentSegment,
877 currentSegment.add(classDeclaration);
878 currentSegment = classDeclaration;
880 classEnd = ClassBody(classDeclaration)
881 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
882 classDeclaration.sourceEnd = classEnd;
883 pushOnAstNodes(classDeclaration);
884 return classDeclaration;}
887 int ClassBody(final ClassDeclaration classDeclaration) :
894 } catch (ParseException e) {
895 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
897 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
898 errorEnd = SimpleCharStream.getPosition() + 1;
899 processParseExceptionDebug(e);
901 ( ClassBodyDeclaration(classDeclaration) )*
904 {return token.sourceEnd;}
905 } catch (ParseException e) {
906 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
908 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
909 errorEnd = SimpleCharStream.getPosition() + 1;
910 processParseExceptionDebug(e);
911 return PHPParser.token.sourceEnd;
916 * A class can contain only methods and fields.
918 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
920 final MethodDeclaration method;
921 final FieldDeclaration field;
924 method = MethodDeclaration() {method.analyzeCode();
925 classDeclaration.addMethod(method);}
926 | field = FieldDeclaration() {classDeclaration.addField(field);}
930 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
931 * it is only used by ClassBodyDeclaration()
933 FieldDeclaration FieldDeclaration() :
935 VariableDeclaration variableDeclaration;
936 final VariableDeclaration[] list;
937 final ArrayList arrayList = new ArrayList();
943 token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
945 arrayList.add(variableDeclaration);
946 outlineInfo.addVariable(variableDeclaration.name());
947 pos = variableDeclaration.sourceEnd;
950 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
952 arrayList.add(variableDeclaration);
953 outlineInfo.addVariable(variableDeclaration.name());
954 pos = variableDeclaration.sourceEnd;
959 } catch (ParseException e) {
960 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
964 processParseExceptionDebug(e);
967 {list = new VariableDeclaration[arrayList.size()];
968 arrayList.toArray(list);
970 if (token2 == null) {
971 end = list[list.length-1].sourceEnd;
973 end = token2.sourceEnd;
975 return new FieldDeclaration(list,
982 * a strict variable declarator : there cannot be a suffix here.
983 * It will be used by fields and formal parameters
985 VariableDeclaration VariableDeclaratorNoSuffix() :
987 final Token token, lbrace,rbrace;
988 Expression expr, initializer = null;
996 {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
998 lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
999 {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
1002 assignToken = <ASSIGN>
1004 initializer = VariableInitializer()
1005 } catch (ParseException e) {
1006 errorMessage = "Literal expression expected in variable initializer";
1008 errorStart = assignToken.sourceEnd +1;
1009 errorEnd = assignToken.sourceEnd +1;
1010 processParseExceptionDebug(e);
1014 if (initializer == null) {
1015 return new VariableDeclaration(currentSegment,
1017 variable.sourceStart,
1018 variable.sourceEnd);
1020 return new VariableDeclaration(currentSegment,
1023 VariableDeclaration.EQUAL,
1024 variable.sourceStart);
1029 * this will be used by static statement
1031 VariableDeclaration VariableDeclarator() :
1033 final AbstractVariable variable;
1034 Expression initializer = null;
1038 variable = VariableDeclaratorId()
1042 initializer = VariableInitializer()
1043 } catch (ParseException e) {
1044 errorMessage = "Literal expression expected in variable initializer";
1046 errorStart = token.sourceEnd+1;
1047 errorEnd = token.sourceEnd+1;
1048 processParseExceptionDebug(e);
1052 if (initializer == null) {
1053 return new VariableDeclaration(currentSegment,
1055 variable.sourceStart,
1056 variable.sourceEnd);
1058 return new VariableDeclaration(currentSegment,
1061 VariableDeclaration.EQUAL,
1062 variable.sourceStart);
1068 * @return the variable name (with suffix)
1070 AbstractVariable VariableDeclaratorId() :
1073 AbstractVariable expression = null;
1080 expression = VariableSuffix(var)
1083 if (expression == null) {
1088 } catch (ParseException e) {
1089 errorMessage = "'$' expected for variable identifier";
1091 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1092 errorEnd = SimpleCharStream.getPosition() + 1;
1097 Variable Variable() :
1099 Variable variable = null;
1103 token = <DOLLAR> variable = Var()
1111 Variable variable = null;
1112 final Token token,token2;
1113 ConstantIdentifier constant;
1114 Expression expression;
1117 token = <DOLLAR> variable = Var()
1118 {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1120 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1122 return new Variable(expression,
1127 token = <IDENTIFIER>
1128 {return new Variable(token.image,token.sourceStart,token.sourceEnd);}
1131 Expression VariableInitializer() :
1133 final Expression expr;
1134 final Token token, token2;
1140 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1141 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1143 token2.sourceStart);}
1145 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1146 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1148 token2.sourceStart);}
1150 expr = ArrayDeclarator()
1153 token = <IDENTIFIER>
1154 {return new ConstantIdentifier(token);}
1157 ArrayVariableDeclaration ArrayVariable() :
1159 final Expression expr,expr2;
1164 <ARRAYASSIGN> expr2 = Expression()
1165 {return new ArrayVariableDeclaration(expr,expr2);}
1167 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1170 ArrayVariableDeclaration[] ArrayInitializer() :
1172 ArrayVariableDeclaration expr;
1173 final ArrayList list = new ArrayList();
1178 expr = ArrayVariable()
1180 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1185 <COMMA> {list.add(null);}
1189 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1195 * A Method Declaration.
1196 * <b>function</b> MetodDeclarator() Block()
1198 MethodDeclaration MethodDeclaration() :
1200 final MethodDeclaration functionDeclaration;
1202 final OutlineableWithChildren seg = currentSegment;
1208 functionDeclaration = MethodDeclarator(token.sourceStart)
1209 {outlineInfo.addVariable(functionDeclaration.name);}
1210 } catch (ParseException e) {
1211 if (errorMessage != null) throw e;
1212 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1214 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1215 errorEnd = SimpleCharStream.getPosition() + 1;
1218 {currentSegment = functionDeclaration;}
1220 {functionDeclaration.statements = block.statements;
1221 currentSegment = seg;
1222 return functionDeclaration;}
1226 * A MethodDeclarator.
1227 * [&] IDENTIFIER(parameters ...).
1228 * @return a function description for the outline
1230 MethodDeclaration MethodDeclarator(final int start) :
1232 Token identifier = null;
1233 Token reference = null;
1234 final Hashtable formalParameters = new Hashtable();
1235 String identifierChar = SYNTAX_ERROR_CHAR;
1239 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1241 identifier = <IDENTIFIER>
1243 identifierChar = identifier.image;
1244 end = identifier.sourceEnd;
1246 } catch (ParseException e) {
1247 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1249 errorStart = e.currentToken.sourceEnd;
1250 errorEnd = e.currentToken.next.sourceStart;
1251 processParseExceptionDebug(e);
1253 end = FormalParameters(formalParameters)
1255 int nameStart, nameEnd;
1256 if (identifier == null) {
1257 if (reference == null) {
1258 nameStart = start + 9;
1259 nameEnd = start + 10;
1261 nameStart = reference.sourceEnd + 1;
1262 nameEnd = reference.sourceEnd + 2;
1265 nameStart = identifier.sourceStart;
1266 nameEnd = identifier.sourceEnd;
1268 return new MethodDeclaration(currentSegment,
1280 * FormalParameters follows method identifier.
1281 * (FormalParameter())
1283 int FormalParameters(final Hashtable parameters) :
1285 VariableDeclaration var;
1287 Token tok = PHPParser.token;
1288 int end = tok.sourceEnd;
1293 {end = tok.sourceEnd;}
1294 } catch (ParseException e) {
1295 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1297 errorStart = e.currentToken.next.sourceStart;
1298 errorEnd = e.currentToken.next.sourceEnd;
1299 processParseExceptionDebug(e);
1302 var = FormalParameter()
1303 {parameters.put(var.name(),var);end = var.sourceEnd;}
1305 <COMMA> var = FormalParameter()
1306 {parameters.put(var.name(),var);end = var.sourceEnd;}
1311 {end = token.sourceEnd;}
1312 } catch (ParseException e) {
1313 errorMessage = "')' expected";
1315 errorStart = e.currentToken.next.sourceStart;
1316 errorEnd = e.currentToken.next.sourceEnd;
1317 processParseExceptionDebug(e);
1323 * A formal parameter.
1324 * $varname[=value] (,$varname[=value])
1326 VariableDeclaration FormalParameter() :
1328 final VariableDeclaration variableDeclaration;
1332 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1334 if (token != null) {
1335 variableDeclaration.setReference(true);
1337 return variableDeclaration;}
1340 ConstantIdentifier Type() :
1341 {final Token token;}
1343 token = <STRING> {return new ConstantIdentifier(token);}
1344 | token = <BOOL> {return new ConstantIdentifier(token);}
1345 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1346 | token = <REAL> {return new ConstantIdentifier(token);}
1347 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1348 | token = <FLOAT> {return new ConstantIdentifier(token);}
1349 | token = <INT> {return new ConstantIdentifier(token);}
1350 | token = <INTEGER> {return new ConstantIdentifier(token);}
1351 | token = <OBJECT> {return new ConstantIdentifier(token);}
1354 Expression Expression() :
1356 final Expression expr;
1357 Expression initializer = null;
1358 int assignOperator = -1;
1362 expr = ConditionalExpression()
1364 assignOperator = AssignmentOperator()
1366 initializer = Expression()
1367 } catch (ParseException e) {
1368 if (errorMessage != null) {
1371 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1373 errorEnd = SimpleCharStream.getPosition();
1378 if (assignOperator != -1) {// todo : change this, very very bad :(
1379 if (expr instanceof AbstractVariable) {
1380 return new VariableDeclaration(currentSegment,
1381 (AbstractVariable) expr,
1384 initializer.sourceEnd);
1386 String varName = expr.toStringExpression().substring(1);
1387 return new VariableDeclaration(currentSegment,
1388 new Variable(varName,
1392 initializer.sourceEnd);
1396 | expr = ExpressionWBang() {return expr;}
1399 Expression ExpressionWBang() :
1401 final Expression expr;
1405 token = <BANG> expr = ExpressionWBang()
1406 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1407 | expr = ExpressionNoBang() {return expr;}
1410 Expression ExpressionNoBang() :
1415 expr = ListExpression() {return expr;}
1417 expr = PrintExpression() {return expr;}
1421 * Any assignement operator.
1422 * @return the assignement operator id
1424 int AssignmentOperator() :
1427 <ASSIGN> {return VariableDeclaration.EQUAL;}
1428 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1429 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1430 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1431 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1432 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1433 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1434 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1435 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1436 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1437 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1438 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1439 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1442 Expression ConditionalExpression() :
1444 final Expression expr;
1445 Expression expr2 = null;
1446 Expression expr3 = null;
1449 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1451 if (expr3 == null) {
1454 return new ConditionalExpression(expr,expr2,expr3);
1458 Expression ConditionalOrExpression() :
1460 Expression expr,expr2;
1464 expr = ConditionalAndExpression()
1467 <OR_OR> {operator = OperatorIds.OR_OR;}
1468 | <_ORL> {operator = OperatorIds.ORL;}
1470 expr2 = ConditionalAndExpression()
1472 expr = new BinaryExpression(expr,expr2,operator);
1478 Expression ConditionalAndExpression() :
1480 Expression expr,expr2;
1484 expr = ConcatExpression()
1486 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1487 | <_ANDL> {operator = OperatorIds.ANDL;})
1488 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1493 Expression ConcatExpression() :
1495 Expression expr,expr2;
1498 expr = InclusiveOrExpression()
1500 <DOT> expr2 = InclusiveOrExpression()
1501 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1506 Expression InclusiveOrExpression() :
1508 Expression expr,expr2;
1511 expr = ExclusiveOrExpression()
1512 (<BIT_OR> expr2 = ExclusiveOrExpression()
1513 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1518 Expression ExclusiveOrExpression() :
1520 Expression expr,expr2;
1523 expr = AndExpression()
1525 <XOR> expr2 = AndExpression()
1526 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1531 Expression AndExpression() :
1533 Expression expr,expr2;
1536 expr = EqualityExpression()
1539 <BIT_AND> expr2 = EqualityExpression()
1540 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1545 Expression EqualityExpression() :
1547 Expression expr,expr2;
1552 expr = RelationalExpression()
1554 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1555 | token = <DIF> {operator = OperatorIds.DIF;}
1556 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1557 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1558 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1561 expr2 = RelationalExpression()
1562 } catch (ParseException e) {
1563 if (errorMessage != null) {
1566 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1568 errorStart = token.sourceEnd +1;
1569 errorEnd = token.sourceEnd +1;
1570 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1571 processParseExceptionDebug(e);
1574 expr = new BinaryExpression(expr,expr2,operator);
1580 Expression RelationalExpression() :
1582 Expression expr,expr2;
1586 expr = ShiftExpression()
1588 ( <LT> {operator = OperatorIds.LESS;}
1589 | <GT> {operator = OperatorIds.GREATER;}
1590 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1591 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1592 expr2 = ShiftExpression()
1593 {expr = new BinaryExpression(expr,expr2,operator);}
1598 Expression ShiftExpression() :
1600 Expression expr,expr2;
1604 expr = AdditiveExpression()
1606 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1607 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1608 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1609 expr2 = AdditiveExpression()
1610 {expr = new BinaryExpression(expr,expr2,operator);}
1615 Expression AdditiveExpression() :
1617 Expression expr,expr2;
1621 expr = MultiplicativeExpression()
1624 ( <PLUS> {operator = OperatorIds.PLUS;}
1625 | <MINUS> {operator = OperatorIds.MINUS;}
1627 expr2 = MultiplicativeExpression()
1628 {expr = new BinaryExpression(expr,expr2,operator);}
1633 Expression MultiplicativeExpression() :
1635 Expression expr,expr2;
1640 expr = UnaryExpression()
1641 } catch (ParseException e) {
1642 if (errorMessage != null) throw e;
1643 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1645 errorStart = PHPParser.token.sourceStart;
1646 errorEnd = PHPParser.token.sourceEnd;
1650 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1651 | <SLASH> {operator = OperatorIds.DIVIDE;}
1652 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1653 expr2 = UnaryExpression()
1654 {expr = new BinaryExpression(expr,expr2,operator);}
1660 * An unary expression starting with @, & or nothing
1662 Expression UnaryExpression() :
1664 final Expression expr;
1667 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1668 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1670 expr = AtNotTildeUnaryExpression() {return expr;}
1673 Expression AtNotTildeUnaryExpression() :
1675 final Expression expr;
1680 expr = AtNotTildeUnaryExpression()
1681 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1684 expr = AtNotTildeUnaryExpression()
1685 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1688 expr = AtNotUnaryExpression()
1689 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1691 expr = UnaryExpressionNoPrefix()
1696 * An expression prefixed (or not) by one or more @ and !.
1697 * @return the expression
1699 Expression AtNotUnaryExpression() :
1701 final Expression expr;
1706 expr = AtNotUnaryExpression()
1707 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1710 expr = AtNotUnaryExpression()
1711 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1713 expr = UnaryExpressionNoPrefix()
1717 Expression UnaryExpressionNoPrefix() :
1719 final Expression expr;
1723 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1725 token.sourceStart);}
1727 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1729 token.sourceStart);}
1731 expr = PreIncDecExpression()
1734 expr = UnaryExpressionNotPlusMinus()
1739 Expression PreIncDecExpression() :
1741 final Expression expr;
1747 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1749 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1751 expr = PrimaryExpression()
1752 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1755 Expression UnaryExpressionNotPlusMinus() :
1757 final Expression expr;
1760 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1761 expr = CastExpression() {return expr;}
1762 | expr = PostfixExpression() {return expr;}
1763 | expr = Literal() {return expr;}
1764 | <LPAREN> expr = Expression()
1767 } catch (ParseException e) {
1768 errorMessage = "')' expected";
1770 errorStart = expr.sourceEnd +1;
1771 errorEnd = expr.sourceEnd +1;
1772 processParseExceptionDebug(e);
1777 CastExpression CastExpression() :
1779 final ConstantIdentifier type;
1780 final Expression expr;
1781 final Token token,token1;
1788 token = <ARRAY> {type = new ConstantIdentifier(token);}
1790 <RPAREN> expr = UnaryExpression()
1791 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1794 Expression PostfixExpression() :
1796 final Expression expr;
1801 expr = PrimaryExpression()
1803 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1805 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1808 if (operator == -1) {
1811 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1815 Expression PrimaryExpression() :
1821 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1824 expr = ArrayDeclarator()
1828 Expression refPrimaryExpression(final Token reference) :
1831 Expression expr2 = null;
1832 final Token identifier;
1835 identifier = <IDENTIFIER>
1837 expr = new ConstantIdentifier(identifier);
1840 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1841 {expr = new ClassAccess(expr,
1843 ClassAccess.STATIC);}
1845 [ expr2 = Arguments(expr) ]
1847 if (expr2 == null) {
1848 if (reference != null) {
1849 ParseException e = generateParseException();
1850 errorMessage = "you cannot use a constant by reference";
1852 errorStart = reference.sourceStart;
1853 errorEnd = reference.sourceEnd;
1854 processParseExceptionDebug(e);
1861 expr = VariableDeclaratorId() //todo use the reference parameter ...
1862 [ expr = Arguments(expr) ]
1866 expr = ClassIdentifier()
1869 if (reference == null) {
1870 start = token.sourceStart;
1872 start = reference.sourceStart;
1874 expr = new ClassInstantiation(expr,
1878 [ expr = Arguments(expr) ]
1883 * An array declarator.
1887 ArrayInitializer ArrayDeclarator() :
1889 final ArrayVariableDeclaration[] vars;
1893 token = <ARRAY> vars = ArrayInitializer()
1894 {return new ArrayInitializer(vars,
1896 PHPParser.token.sourceEnd);}
1899 Expression ClassIdentifier():
1901 final Expression expr;
1905 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1906 | expr = Type() {return expr;}
1907 | expr = VariableDeclaratorId() {return expr;}
1911 * Used by Variabledeclaratorid and primarysuffix
1913 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1915 Expression expression = null;
1916 final Token classAccessToken,lbrace,rbrace;
1921 classAccessToken = <CLASSACCESS>
1924 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1926 expression = new Variable(expression,
1931 token = <IDENTIFIER>
1932 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1934 expression = Variable()
1936 } catch (ParseException e) {
1937 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1939 errorStart = classAccessToken.sourceEnd +1;
1940 errorEnd = classAccessToken.sourceEnd +1;
1941 processParseExceptionDebug(e);
1943 {return new ClassAccess(prefix,
1945 ClassAccess.NORMAL);}
1947 token = <LBRACKET> {pos = token.sourceEnd+1;}
1948 [ expression = Expression() {pos = expression.sourceEnd+1;}
1949 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1952 {pos = token.sourceEnd;}
1953 } catch (ParseException e) {
1954 errorMessage = "']' expected";
1958 processParseExceptionDebug(e);
1960 {return new ArrayDeclarator(prefix,expression,pos);}
1962 token = <LBRACE> {pos = token.sourceEnd+1;}
1963 [ expression = Expression() {pos = expression.sourceEnd+1;}
1964 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1967 {pos = token.sourceEnd;}
1968 } catch (ParseException e) {
1969 errorMessage = "']' expected";
1973 processParseExceptionDebug(e);
1975 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1981 StringLiteral literal;
1984 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1985 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1986 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1987 | token = <TRUE> {return new TrueLiteral(token);}
1988 | token = <FALSE> {return new FalseLiteral(token);}
1989 | token = <NULL> {return new NullLiteral(token);}
1990 | literal = evaluableString() {return literal;}
1993 StringLiteral evaluableString() :
1995 ArrayList list = new ArrayList();
1997 Token token,lbrace,rbrace;
1998 AbstractVariable var;
2002 start = <DOUBLEQUOTE>
2006 token = <IDENTIFIER> {list.add(new Variable(token.image,
2012 {list.add(new Variable(token.image,
2018 end = <DOUBLEQUOTE2>
2020 AbstractVariable[] vars = new AbstractVariable[list.size()];
2022 return new StringLiteral(SimpleCharStream.currentBuffer.substring(start.sourceEnd,end.sourceStart),
2029 FunctionCall Arguments(final Expression func) :
2031 Expression[] args = null;
2032 final Token token,lparen;
2035 lparen = <LPAREN> [ args = ArgumentList() ]
2038 {return new FunctionCall(func,args,token.sourceEnd);}
2039 } catch (ParseException e) {
2040 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2043 errorStart = lparen.sourceEnd+1;
2044 errorEnd = lparen.sourceEnd+2;
2046 errorStart = args[args.length-1].sourceEnd+1;
2047 errorEnd = args[args.length-1].sourceEnd+2;
2049 processParseExceptionDebug(e);
2052 int sourceEnd = (args == null && args.length != 0) ? lparen.sourceEnd+1 : args[args.length-1].sourceEnd;
2053 return new FunctionCall(func,args,sourceEnd);}
2057 * An argument list is a list of arguments separated by comma :
2058 * argumentDeclaration() (, argumentDeclaration)*
2059 * @return an array of arguments
2061 Expression[] ArgumentList() :
2064 final ArrayList list = new ArrayList();
2070 {list.add(arg);pos = arg.sourceEnd;}
2071 ( token = <COMMA> {pos = token.sourceEnd;}
2075 pos = arg.sourceEnd;}
2076 } catch (ParseException e) {
2077 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2081 processParseException(e);
2085 final Expression[] arguments = new Expression[list.size()];
2086 list.toArray(arguments);
2091 * A Statement without break.
2092 * @return a statement
2094 Statement StatementNoBreak() :
2096 final Statement statement;
2101 statement = expressionStatement() {return statement;}
2103 statement = LabeledStatement() {return statement;}
2104 | statement = Block() {return statement;}
2105 | statement = EmptyStatement() {return statement;}
2106 | statement = SwitchStatement() {return statement;}
2107 | statement = IfStatement() {return statement;}
2108 | statement = WhileStatement() {return statement;}
2109 | statement = DoStatement() {return statement;}
2110 | statement = ForStatement() {return statement;}
2111 | statement = ForeachStatement() {return statement;}
2112 | statement = ContinueStatement() {return statement;}
2113 | statement = ReturnStatement() {return statement;}
2114 | statement = EchoStatement() {return statement;}
2115 | [token=<AT>] statement = IncludeStatement()
2116 {if (token != null) {
2117 ((InclusionStatement)statement).silent = true;
2118 statement.sourceStart = token.sourceStart;
2121 | statement = StaticStatement() {return statement;}
2122 | statement = GlobalStatement() {return statement;}
2123 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2127 * A statement expression.
2129 * @return an expression
2131 Statement expressionStatement() :
2133 final Statement statement;
2137 statement = Expression()
2140 {statement.sourceEnd = token.sourceEnd;}
2141 } catch (ParseException e) {
2142 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2143 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2145 errorStart = statement.sourceEnd+1;
2146 errorEnd = statement.sourceEnd+1;
2147 processParseExceptionDebug(e);
2153 Define defineStatement() :
2155 Expression defineName,defineValue;
2156 final Token defineToken;
2161 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2164 {pos = token.sourceEnd+1;}
2165 } catch (ParseException e) {
2166 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2170 processParseExceptionDebug(e);
2173 defineName = Expression()
2174 {pos = defineName.sourceEnd+1;}
2175 } catch (ParseException e) {
2176 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2180 processParseExceptionDebug(e);
2181 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2185 {pos = defineName.sourceEnd+1;}
2186 } catch (ParseException e) {
2187 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2191 processParseExceptionDebug(e);
2194 defineValue = Expression()
2195 {pos = defineValue.sourceEnd+1;}
2196 } catch (ParseException e) {
2197 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2201 processParseExceptionDebug(e);
2202 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2206 {pos = token.sourceEnd+1;}
2207 } catch (ParseException e) {
2208 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2212 processParseExceptionDebug(e);
2214 {return new Define(currentSegment,
2217 defineToken.sourceStart,
2222 * A Normal statement.
2224 Statement Statement() :
2226 final Statement statement;
2229 statement = StatementNoBreak() {return statement;}
2230 | statement = BreakStatement() {return statement;}
2234 * An html block inside a php syntax.
2236 HTMLBlock htmlBlock() :
2238 final int startIndex = nodePtr;
2239 final AstNode[] blockNodes;
2245 {htmlStart = phpEnd.sourceEnd;}
2248 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2249 {PHPParser.createNewHTMLCode();}
2250 } catch (ParseException e) {
2251 errorMessage = "unexpected end of file , '<?php' expected";
2253 errorStart = SimpleCharStream.getPosition();
2254 errorEnd = SimpleCharStream.getPosition();
2258 nbNodes = nodePtr - startIndex;
2262 blockNodes = new AstNode[nbNodes];
2263 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2264 nodePtr = startIndex;
2265 return new HTMLBlock(blockNodes);}
2269 * An include statement. It's "include" an expression;
2271 InclusionStatement IncludeStatement() :
2275 final InclusionStatement inclusionStatement;
2276 final Token token, token2;
2280 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2281 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2282 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2283 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2286 {pos = expr.sourceEnd;}
2287 } catch (ParseException e) {
2288 if (errorMessage != null) {
2291 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2293 errorStart = e.currentToken.next.sourceStart;
2294 errorEnd = e.currentToken.next.sourceEnd;
2295 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2296 processParseExceptionDebug(e);
2299 token2 = <SEMICOLON>
2300 {pos=token2.sourceEnd;}
2301 } catch (ParseException e) {
2302 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2304 errorStart = e.currentToken.next.sourceStart;
2305 errorEnd = e.currentToken.next.sourceEnd;
2306 processParseExceptionDebug(e);
2309 inclusionStatement = new InclusionStatement(currentSegment,
2314 currentSegment.add(inclusionStatement);
2315 return inclusionStatement;
2319 PrintExpression PrintExpression() :
2321 final Expression expr;
2322 final Token printToken;
2325 token = <PRINT> expr = Expression()
2326 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2329 ListExpression ListExpression() :
2331 Expression expr = null;
2332 final Expression expression;
2333 final ArrayList list = new ArrayList();
2335 final Token listToken, rParen;
2339 listToken = <LIST> {pos = listToken.sourceEnd;}
2341 token = <LPAREN> {pos = token.sourceEnd;}
2342 } catch (ParseException e) {
2343 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2345 errorStart = listToken.sourceEnd+1;
2346 errorEnd = listToken.sourceEnd+1;
2347 processParseExceptionDebug(e);
2350 expr = VariableDeclaratorId()
2351 {list.add(expr);pos = expr.sourceEnd;}
2353 {if (expr == null) list.add(null);}
2357 {pos = token.sourceEnd;}
2358 } catch (ParseException e) {
2359 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2363 processParseExceptionDebug(e);
2365 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2369 {pos = rParen.sourceEnd;}
2370 } catch (ParseException e) {
2371 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2375 processParseExceptionDebug(e);
2377 [ <ASSIGN> expression = Expression()
2379 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2381 return new ListExpression(vars,
2383 listToken.sourceStart,
2384 expression.sourceEnd);}
2387 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2389 return new ListExpression(vars,listToken.sourceStart,pos);}
2393 * An echo statement.
2394 * echo anyexpression (, otherexpression)*
2396 EchoStatement EchoStatement() :
2398 final ArrayList expressions = new ArrayList();
2401 Token token2 = null;
2404 token = <ECHO> expr = Expression()
2405 {expressions.add(expr);}
2407 <COMMA> expr = Expression()
2408 {expressions.add(expr);}
2411 token2 = <SEMICOLON>
2412 } catch (ParseException e) {
2413 if (e.currentToken.next.kind != 4) {
2414 errorMessage = "';' expected after 'echo' statement";
2416 errorStart = e.currentToken.sourceEnd;
2417 errorEnd = e.currentToken.sourceEnd;
2418 processParseExceptionDebug(e);
2422 final Expression[] exprs = new Expression[expressions.size()];
2423 expressions.toArray(exprs);
2424 if (token2 == null) {
2425 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2427 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2431 GlobalStatement GlobalStatement() :
2434 final ArrayList vars = new ArrayList();
2435 final GlobalStatement global;
2436 final Token token, token2;
2442 {vars.add(expr);pos = expr.sourceEnd+1;}
2445 {vars.add(expr);pos = expr.sourceEnd+1;}
2448 token2 = <SEMICOLON>
2449 {pos = token2.sourceEnd+1;}
2450 } catch (ParseException e) {
2451 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2455 processParseExceptionDebug(e);
2458 final Variable[] variables = new Variable[vars.size()];
2459 vars.toArray(variables);
2460 global = new GlobalStatement(currentSegment,
2464 currentSegment.add(global);
2468 StaticStatement StaticStatement() :
2470 final ArrayList vars = new ArrayList();
2471 VariableDeclaration expr;
2472 final Token token, token2;
2476 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2478 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2481 token2 = <SEMICOLON>
2482 {pos = token2.sourceEnd+1;}
2483 } catch (ParseException e) {
2484 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2488 processParseException(e);
2491 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2492 vars.toArray(variables);
2493 return new StaticStatement(variables,
2498 LabeledStatement LabeledStatement() :
2501 final Statement statement;
2504 label = <IDENTIFIER> <COLON> statement = Statement()
2505 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2517 final ArrayList list = new ArrayList();
2518 Statement statement;
2519 final Token token, token2;
2525 {pos = token.sourceEnd+1;start=token.sourceStart;}
2526 } catch (ParseException e) {
2527 errorMessage = "'{' expected";
2529 pos = PHPParser.token.sourceEnd+1;
2533 processParseExceptionDebug(e);
2535 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2536 | statement = htmlBlock() {if (statement != null) {
2537 list.add(statement);
2538 pos = statement.sourceEnd+1;
2540 pos = PHPParser.token.sourceEnd+1;
2545 {pos = token2.sourceEnd+1;}
2546 } catch (ParseException e) {
2547 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2551 processParseExceptionDebug(e);
2554 final Statement[] statements = new Statement[list.size()];
2555 list.toArray(statements);
2556 return new Block(statements,start,pos);}
2559 Statement BlockStatement() :
2561 final Statement statement;
2565 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2567 } catch (ParseException e) {
2568 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2570 errorStart = e.currentToken.sourceStart;
2571 errorEnd = e.currentToken.sourceEnd;
2574 | statement = ClassDeclaration() {return statement;}
2575 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2576 currentSegment.add((MethodDeclaration) statement);
2577 ((MethodDeclaration) statement).analyzeCode();
2582 * A Block statement that will not contain any 'break'
2584 Statement BlockStatementNoBreak() :
2586 final Statement statement;
2589 statement = StatementNoBreak() {return statement;}
2590 | statement = ClassDeclaration() {return statement;}
2591 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2592 ((MethodDeclaration) statement).analyzeCode();
2597 * used only by ForInit()
2599 Expression[] LocalVariableDeclaration() :
2601 final ArrayList list = new ArrayList();
2607 ( <COMMA> var = Expression() {list.add(var);})*
2609 final Expression[] vars = new Expression[list.size()];
2616 * used only by LocalVariableDeclaration().
2618 VariableDeclaration LocalVariableDeclarator() :
2620 final Variable varName;
2621 Expression initializer = null;
2624 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2626 if (initializer == null) {
2627 return new VariableDeclaration(currentSegment,
2629 varName.sourceStart,
2632 return new VariableDeclaration(currentSegment,
2635 VariableDeclaration.EQUAL,
2636 varName.sourceStart);
2640 EmptyStatement EmptyStatement() :
2646 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2650 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2652 Expression StatementExpression() :
2654 final Expression expr;
2655 final Token operator;
2658 expr = PreIncDecExpression() {return expr;}
2660 expr = PrimaryExpression()
2661 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2662 OperatorIds.PLUS_PLUS,
2663 operator.sourceEnd);}
2664 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2665 OperatorIds.MINUS_MINUS,
2666 operator.sourceEnd);}
2671 SwitchStatement SwitchStatement() :
2673 Expression variable;
2674 final AbstractCase[] cases;
2675 final Token switchToken,lparenToken,rparenToken;
2679 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2681 lparenToken = <LPAREN>
2682 {pos = lparenToken.sourceEnd+1;}
2683 } catch (ParseException e) {
2684 errorMessage = "'(' expected after 'switch'";
2688 processParseExceptionDebug(e);
2691 variable = Expression() {pos = variable.sourceEnd+1;}
2692 } catch (ParseException e) {
2693 if (errorMessage != null) {
2696 errorMessage = "expression expected";
2700 processParseExceptionDebug(e);
2701 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2704 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2705 } catch (ParseException e) {
2706 errorMessage = "')' expected";
2710 processParseExceptionDebug(e);
2712 ( cases = switchStatementBrace()
2713 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2714 {return new SwitchStatement(variable,
2716 switchToken.sourceStart,
2717 PHPParser.token.sourceEnd);}
2720 AbstractCase[] switchStatementBrace() :
2723 final ArrayList cases = new ArrayList();
2728 token = <LBRACE> {pos = token.sourceEnd;}
2729 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2732 {pos = token.sourceEnd;}
2733 } catch (ParseException e) {
2734 errorMessage = "'}' expected";
2738 processParseExceptionDebug(e);
2741 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2742 cases.toArray(abcase);
2748 * A Switch statement with : ... endswitch;
2749 * @param start the begin offset of the switch
2750 * @param end the end offset of the switch
2752 AbstractCase[] switchStatementColon(final int start, final int end) :
2755 final ArrayList cases = new ArrayList();
2760 token = <COLON> {pos = token.sourceEnd;}
2762 setMarker(fileToParse,
2763 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2767 "Line " + token.beginLine);
2768 } catch (CoreException e) {
2769 PHPeclipsePlugin.log(e);
2771 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2773 token = <ENDSWITCH> {pos = token.sourceEnd;}
2774 } catch (ParseException e) {
2775 errorMessage = "'endswitch' expected";
2779 processParseExceptionDebug(e);
2782 token = <SEMICOLON> {pos = token.sourceEnd;}
2783 } catch (ParseException e) {
2784 errorMessage = "';' expected after 'endswitch' keyword";
2788 processParseExceptionDebug(e);
2791 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2792 cases.toArray(abcase);
2797 AbstractCase switchLabel0() :
2799 final Expression expr;
2800 Statement statement;
2801 final ArrayList stmts = new ArrayList();
2802 final Token token = PHPParser.token;
2805 expr = SwitchLabel()
2806 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2807 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2808 | statement = BreakStatement() {stmts.add(statement);})*
2809 //[ statement = BreakStatement() {stmts.add(statement);}]
2811 final int listSize = stmts.size();
2812 final Statement[] stmtsArray = new Statement[listSize];
2813 stmts.toArray(stmtsArray);
2814 if (expr == null) {//it's a default
2815 return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd);
2817 if (listSize != 0) {
2818 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2820 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2827 * case Expression() :
2829 * @return the if it was a case and null if not
2831 Expression SwitchLabel() :
2833 final Expression expr;
2839 } catch (ParseException e) {
2840 if (errorMessage != null) throw e;
2841 errorMessage = "expression expected after 'case' keyword";
2843 errorStart = token.sourceEnd +1;
2844 errorEnd = token.sourceEnd +1;
2850 } catch (ParseException e) {
2851 errorMessage = "':' expected after case expression";
2853 errorStart = expr.sourceEnd+1;
2854 errorEnd = expr.sourceEnd+1;
2855 processParseExceptionDebug(e);
2861 } catch (ParseException e) {
2862 errorMessage = "':' expected after 'default' keyword";
2864 errorStart = token.sourceEnd+1;
2865 errorEnd = token.sourceEnd+1;
2866 processParseExceptionDebug(e);
2871 Break BreakStatement() :
2873 Expression expression = null;
2874 final Token token, token2;
2878 token = <BREAK> {pos = token.sourceEnd+1;}
2879 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2881 token2 = <SEMICOLON>
2882 {pos = token2.sourceEnd;}
2883 } catch (ParseException e) {
2884 errorMessage = "';' expected after 'break' keyword";
2888 processParseExceptionDebug(e);
2890 {return new Break(expression, token.sourceStart, pos);}
2893 IfStatement IfStatement() :
2895 final Expression condition;
2896 final IfStatement ifStatement;
2900 token = <IF> condition = Condition("if")
2901 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2902 {return ifStatement;}
2906 Expression Condition(final String keyword) :
2908 final Expression condition;
2913 } catch (ParseException e) {
2914 errorMessage = "'(' expected after " + keyword + " keyword";
2916 errorStart = PHPParser.token.sourceEnd + 1;
2917 errorEnd = PHPParser.token.sourceEnd + 1;
2918 processParseExceptionDebug(e);
2920 condition = Expression()
2923 } catch (ParseException e) {
2924 errorMessage = "')' expected after " + keyword + " keyword";
2926 errorStart = condition.sourceEnd+1;
2927 errorEnd = condition.sourceEnd+1;
2928 processParseExceptionDebug(e);
2933 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2935 Statement statement;
2936 final Statement stmt;
2937 final Statement[] statementsArray;
2938 ElseIf elseifStatement;
2939 Else elseStatement = null;
2940 final ArrayList stmts;
2941 final ArrayList elseIfList = new ArrayList();
2942 final ElseIf[] elseIfs;
2943 int pos = SimpleCharStream.getPosition();
2944 final int endStatements;
2948 {stmts = new ArrayList();}
2949 ( statement = Statement() {stmts.add(statement);}
2950 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2951 {endStatements = SimpleCharStream.getPosition();}
2952 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2953 [elseStatement = ElseStatementColon()]
2956 setMarker(fileToParse,
2957 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2961 "Line " + token.beginLine);
2962 } catch (CoreException e) {
2963 PHPeclipsePlugin.log(e);
2967 } catch (ParseException e) {
2968 errorMessage = "'endif' expected";
2970 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2971 errorEnd = SimpleCharStream.getPosition() + 1;
2976 } catch (ParseException e) {
2977 errorMessage = "';' expected after 'endif' keyword";
2979 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2980 errorEnd = SimpleCharStream.getPosition() + 1;
2984 elseIfs = new ElseIf[elseIfList.size()];
2985 elseIfList.toArray(elseIfs);
2986 if (stmts.size() == 1) {
2987 return new IfStatement(condition,
2988 (Statement) stmts.get(0),
2992 SimpleCharStream.getPosition());
2994 statementsArray = new Statement[stmts.size()];
2995 stmts.toArray(statementsArray);
2996 return new IfStatement(condition,
2997 new Block(statementsArray,pos,endStatements),
3001 SimpleCharStream.getPosition());
3006 (stmt = Statement() | stmt = htmlBlock())
3007 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3011 {pos = SimpleCharStream.getPosition();}
3012 statement = Statement()
3013 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
3014 } catch (ParseException e) {
3015 if (errorMessage != null) {
3018 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3020 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3021 errorEnd = SimpleCharStream.getPosition() + 1;
3026 elseIfs = new ElseIf[elseIfList.size()];
3027 elseIfList.toArray(elseIfs);
3028 return new IfStatement(condition,
3033 SimpleCharStream.getPosition());}
3036 ElseIf ElseIfStatementColon() :
3038 final Expression condition;
3039 Statement statement;
3040 final ArrayList list = new ArrayList();
3041 final Token elseifToken;
3044 elseifToken = <ELSEIF> condition = Condition("elseif")
3045 <COLON> ( statement = Statement() {list.add(statement);}
3046 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3048 final int sizeList = list.size();
3049 final Statement[] stmtsArray = new Statement[sizeList];
3050 list.toArray(stmtsArray);
3051 return new ElseIf(condition,stmtsArray ,
3052 elseifToken.sourceStart,
3053 stmtsArray[sizeList-1].sourceEnd);}
3056 Else ElseStatementColon() :
3058 Statement statement;
3059 final ArrayList list = new ArrayList();
3060 final Token elseToken;
3063 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3064 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3066 final int sizeList = list.size();
3067 final Statement[] stmtsArray = new Statement[sizeList];
3068 list.toArray(stmtsArray);
3069 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3072 ElseIf ElseIfStatement() :
3074 final Expression condition;
3075 //final Statement statement;
3076 final Token elseifToken;
3077 final Statement[] statement = new Statement[1];
3080 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3082 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3085 WhileStatement WhileStatement() :
3087 final Expression condition;
3088 final Statement action;
3089 final Token whileToken;
3092 whileToken = <WHILE>
3093 condition = Condition("while")
3094 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3095 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3098 Statement WhileStatement0(final int start, final int end) :
3100 Statement statement;
3101 final ArrayList stmts = new ArrayList();
3102 final int pos = SimpleCharStream.getPosition();
3105 <COLON> (statement = Statement() {stmts.add(statement);})*
3107 setMarker(fileToParse,
3108 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3112 "Line " + token.beginLine);
3113 } catch (CoreException e) {
3114 PHPeclipsePlugin.log(e);
3118 } catch (ParseException e) {
3119 errorMessage = "'endwhile' expected";
3121 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3122 errorEnd = SimpleCharStream.getPosition() + 1;
3128 final Statement[] stmtsArray = new Statement[stmts.size()];
3129 stmts.toArray(stmtsArray);
3130 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3131 } catch (ParseException e) {
3132 errorMessage = "';' expected after 'endwhile' keyword";
3134 errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3135 errorEnd = SimpleCharStream.getPosition() + 1;
3139 statement = Statement()
3143 DoStatement DoStatement() :
3145 final Statement action;
3146 final Expression condition;
3148 Token token2 = null;
3151 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3153 token2 = <SEMICOLON>
3154 } catch (ParseException e) {
3155 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3157 errorStart = condition.sourceEnd+1;
3158 errorEnd = condition.sourceEnd+1;
3159 processParseExceptionDebug(e);
3162 if (token2 == null) {
3163 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3165 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3169 ForeachStatement ForeachStatement() :
3171 Statement statement = null;
3172 Expression expression = null;
3173 ArrayVariableDeclaration variable = null;
3175 Token lparenToken = null;
3176 Token asToken = null;
3177 Token rparenToken = null;
3181 foreachToken = <FOREACH>
3183 lparenToken = <LPAREN>
3184 {pos = lparenToken.sourceEnd+1;}
3185 } catch (ParseException e) {
3186 errorMessage = "'(' expected after 'foreach' keyword";
3188 errorStart = foreachToken.sourceEnd+1;
3189 errorEnd = foreachToken.sourceEnd+1;
3190 processParseExceptionDebug(e);
3191 {pos = foreachToken.sourceEnd+1;}
3194 expression = Expression()
3195 {pos = expression.sourceEnd+1;}
3196 } catch (ParseException e) {
3197 errorMessage = "variable expected";
3201 processParseExceptionDebug(e);
3205 {pos = asToken.sourceEnd+1;}
3206 } catch (ParseException e) {
3207 errorMessage = "'as' expected";
3211 processParseExceptionDebug(e);
3214 variable = ArrayVariable()
3215 {pos = variable.sourceEnd+1;}
3216 } catch (ParseException e) {
3217 if (errorMessage != null) throw e;
3218 errorMessage = "variable expected";
3222 processParseExceptionDebug(e);
3225 rparenToken = <RPAREN>
3226 {pos = rparenToken.sourceEnd+1;}
3227 } catch (ParseException e) {
3228 errorMessage = "')' expected after 'foreach' keyword";
3232 processParseExceptionDebug(e);
3235 statement = Statement()
3236 {pos = rparenToken.sourceEnd+1;}
3237 } catch (ParseException e) {
3238 if (errorMessage != null) throw e;
3239 errorMessage = "statement expected";
3243 processParseExceptionDebug(e);
3245 {return new ForeachStatement(expression,
3248 foreachToken.sourceStart,
3249 statement.sourceEnd);}
3254 * a for declaration.
3255 * @return a node representing the for statement
3257 ForStatement ForStatement() :
3259 final Token token,tokenEndFor,token2,tokenColon;
3261 Expression[] initializations = null;
3262 Expression condition = null;
3263 Expression[] increments = null;
3265 final ArrayList list = new ArrayList();
3271 } catch (ParseException e) {
3272 errorMessage = "'(' expected after 'for' keyword";
3274 errorStart = token.sourceEnd;
3275 errorEnd = token.sourceEnd +1;
3276 processParseExceptionDebug(e);
3278 [ initializations = ForInit() ] <SEMICOLON>
3279 [ condition = Expression() ] <SEMICOLON>
3280 [ increments = StatementExpressionList() ] <RPAREN>
3282 action = Statement()
3283 {return new ForStatement(initializations,
3290 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3291 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3294 setMarker(fileToParse,
3295 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3299 "Line " + token.beginLine);
3300 } catch (CoreException e) {
3301 PHPeclipsePlugin.log(e);
3305 tokenEndFor = <ENDFOR>
3306 {pos = tokenEndFor.sourceEnd+1;}
3307 } catch (ParseException e) {
3308 errorMessage = "'endfor' expected";
3312 processParseExceptionDebug(e);
3315 token2 = <SEMICOLON>
3316 {pos = token2.sourceEnd+1;}
3317 } catch (ParseException e) {
3318 errorMessage = "';' expected after 'endfor' keyword";
3322 processParseExceptionDebug(e);
3325 final Statement[] stmtsArray = new Statement[list.size()];
3326 list.toArray(stmtsArray);
3327 return new ForStatement(initializations,
3330 new Block(stmtsArray,
3331 stmtsArray[0].sourceStart,
3332 stmtsArray[stmtsArray.length-1].sourceEnd),
3338 Expression[] ForInit() :
3340 final Expression[] exprs;
3343 LOOKAHEAD(LocalVariableDeclaration())
3344 exprs = LocalVariableDeclaration()
3347 exprs = StatementExpressionList()
3351 Expression[] StatementExpressionList() :
3353 final ArrayList list = new ArrayList();
3354 final Expression expr;
3357 expr = Expression() {list.add(expr);}
3358 (<COMMA> Expression() {list.add(expr);})*
3360 final Expression[] exprsArray = new Expression[list.size()];
3361 list.toArray(exprsArray);
3366 Continue ContinueStatement() :
3368 Expression expr = null;
3370 Token token2 = null;
3373 token = <CONTINUE> [ expr = Expression() ]
3375 token2 = <SEMICOLON>
3376 } catch (ParseException e) {
3377 errorMessage = "';' expected after 'continue' statement";
3380 errorStart = token.sourceEnd+1;
3381 errorEnd = token.sourceEnd+1;
3383 errorStart = expr.sourceEnd+1;
3384 errorEnd = expr.sourceEnd+1;
3386 processParseExceptionDebug(e);
3389 if (token2 == null) {
3391 return new Continue(expr,token.sourceStart,token.sourceEnd);
3393 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3395 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3399 ReturnStatement ReturnStatement() :
3401 Expression expr = null;
3403 Token token2 = null;
3406 token = <RETURN> [ expr = Expression() ]
3408 token2 = <SEMICOLON>
3409 } catch (ParseException e) {
3410 errorMessage = "';' expected after 'return' statement";
3413 errorStart = token.sourceEnd+1;
3414 errorEnd = token.sourceEnd+1;
3416 errorStart = expr.sourceEnd+1;
3417 errorEnd = expr.sourceEnd+1;
3419 processParseExceptionDebug(e);
3422 if (token2 == null) {
3424 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3426 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3428 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);