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 = e.currentToken.sourceStart;
792 errorEnd = e.currentToken.sourceEnd;
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 = e.currentToken.sourceStart;
898 errorEnd = e.currentToken.sourceEnd;
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 = e.currentToken.sourceStart;
909 errorEnd = e.currentToken.sourceEnd;
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 pos = variableDeclaration.sourceEnd;
949 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
951 arrayList.add(variableDeclaration);
952 outlineInfo.addVariable(variableDeclaration.name());
953 pos = variableDeclaration.sourceEnd;
958 } catch (ParseException e) {
959 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
963 processParseExceptionDebug(e);
966 {list = new VariableDeclaration[arrayList.size()];
967 arrayList.toArray(list);
969 if (token2 == null) {
970 end = list[list.length-1].sourceEnd;
972 end = token2.sourceEnd;
974 return new FieldDeclaration(list,
981 * a strict variable declarator : there cannot be a suffix here.
982 * It will be used by fields and formal parameters
984 VariableDeclaration VariableDeclaratorNoSuffix() :
986 final Token token, lbrace,rbrace;
987 Expression expr, initializer = null;
995 {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
997 lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
998 {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
1001 assignToken = <ASSIGN>
1003 initializer = VariableInitializer()
1004 } catch (ParseException e) {
1005 errorMessage = "Literal expression expected in variable initializer";
1007 errorStart = assignToken.sourceEnd +1;
1008 errorEnd = assignToken.sourceEnd +1;
1009 processParseExceptionDebug(e);
1013 if (initializer == null) {
1014 return new VariableDeclaration(currentSegment,
1016 variable.sourceStart,
1017 variable.sourceEnd);
1019 return new VariableDeclaration(currentSegment,
1022 VariableDeclaration.EQUAL,
1023 variable.sourceStart);
1028 * this will be used by static statement
1030 VariableDeclaration VariableDeclarator() :
1032 final AbstractVariable variable;
1033 Expression initializer = null;
1037 variable = VariableDeclaratorId()
1041 initializer = VariableInitializer()
1042 } catch (ParseException e) {
1043 errorMessage = "Literal expression expected in variable initializer";
1045 errorStart = token.sourceEnd+1;
1046 errorEnd = token.sourceEnd+1;
1047 processParseExceptionDebug(e);
1051 if (initializer == null) {
1052 return new VariableDeclaration(currentSegment,
1054 variable.sourceStart,
1055 variable.sourceEnd);
1057 return new VariableDeclaration(currentSegment,
1060 VariableDeclaration.EQUAL,
1061 variable.sourceStart);
1067 * @return the variable name (with suffix)
1069 AbstractVariable VariableDeclaratorId() :
1072 AbstractVariable expression = null;
1079 expression = VariableSuffix(var)
1082 if (expression == null) {
1087 } catch (ParseException e) {
1088 errorMessage = "'$' expected for variable identifier";
1090 errorStart = e.currentToken.sourceStart;
1091 errorEnd = e.currentToken.sourceEnd;
1096 Variable Variable() :
1098 Variable variable = null;
1102 token = <DOLLAR> variable = Var()
1110 Variable variable = null;
1111 final Token token,token2;
1112 ConstantIdentifier constant;
1113 Expression expression;
1116 token = <DOLLAR> variable = Var()
1117 {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1119 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1121 return new Variable(expression,
1126 token = <IDENTIFIER>
1128 outlineInfo.addVariable("$" + token.image);
1129 return new Variable(token.image,token.sourceStart,token.sourceEnd);
1133 Expression VariableInitializer() :
1135 final Expression expr;
1136 final Token token, token2;
1142 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1143 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1145 token2.sourceStart);}
1147 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1148 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1150 token2.sourceStart);}
1152 expr = ArrayDeclarator()
1155 token = <IDENTIFIER>
1156 {return new ConstantIdentifier(token);}
1159 ArrayVariableDeclaration ArrayVariable() :
1161 final Expression expr,expr2;
1166 <ARRAYASSIGN> expr2 = Expression()
1167 {return new ArrayVariableDeclaration(expr,expr2);}
1169 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1172 ArrayVariableDeclaration[] ArrayInitializer() :
1174 ArrayVariableDeclaration expr;
1175 final ArrayList list = new ArrayList();
1180 expr = ArrayVariable()
1182 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1187 <COMMA> {list.add(null);}
1191 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1197 * A Method Declaration.
1198 * <b>function</b> MetodDeclarator() Block()
1200 MethodDeclaration MethodDeclaration() :
1202 final MethodDeclaration functionDeclaration;
1204 final OutlineableWithChildren seg = currentSegment;
1210 functionDeclaration = MethodDeclarator(token.sourceStart)
1211 {outlineInfo.addVariable(functionDeclaration.name);}
1212 } catch (ParseException e) {
1213 if (errorMessage != null) throw e;
1214 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1216 errorStart = e.currentToken.sourceStart;
1217 errorEnd = e.currentToken.sourceEnd;
1220 {currentSegment = functionDeclaration;}
1222 {functionDeclaration.statements = block.statements;
1223 currentSegment = seg;
1224 return functionDeclaration;}
1228 * A MethodDeclarator.
1229 * [&] IDENTIFIER(parameters ...).
1230 * @return a function description for the outline
1232 MethodDeclaration MethodDeclarator(final int start) :
1234 Token identifier = null;
1235 Token reference = null;
1236 final ArrayList formalParameters = new ArrayList();
1237 String identifierChar = SYNTAX_ERROR_CHAR;
1241 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1243 identifier = <IDENTIFIER>
1245 identifierChar = identifier.image;
1246 end = identifier.sourceEnd;
1248 } catch (ParseException e) {
1249 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1251 errorStart = e.currentToken.sourceEnd;
1252 errorEnd = e.currentToken.next.sourceStart;
1253 processParseExceptionDebug(e);
1255 end = FormalParameters(formalParameters)
1257 int nameStart, nameEnd;
1258 if (identifier == null) {
1259 if (reference == null) {
1260 nameStart = start + 9;
1261 nameEnd = start + 10;
1263 nameStart = reference.sourceEnd + 1;
1264 nameEnd = reference.sourceEnd + 2;
1267 nameStart = identifier.sourceStart;
1268 nameEnd = identifier.sourceEnd;
1270 return new MethodDeclaration(currentSegment,
1282 * FormalParameters follows method identifier.
1283 * (FormalParameter())
1285 int FormalParameters(final ArrayList parameters) :
1287 VariableDeclaration var;
1289 Token tok = PHPParser.token;
1290 int end = tok.sourceEnd;
1295 {end = tok.sourceEnd;}
1296 } catch (ParseException e) {
1297 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1299 errorStart = e.currentToken.next.sourceStart;
1300 errorEnd = e.currentToken.next.sourceEnd;
1301 processParseExceptionDebug(e);
1304 var = FormalParameter()
1305 {parameters.add(var);end = var.sourceEnd;}
1307 <COMMA> var = FormalParameter()
1308 {parameters.add(var);end = var.sourceEnd;}
1313 {end = token.sourceEnd;}
1314 } catch (ParseException e) {
1315 errorMessage = "')' expected";
1317 errorStart = e.currentToken.next.sourceStart;
1318 errorEnd = e.currentToken.next.sourceEnd;
1319 processParseExceptionDebug(e);
1325 * A formal parameter.
1326 * $varname[=value] (,$varname[=value])
1328 VariableDeclaration FormalParameter() :
1330 final VariableDeclaration variableDeclaration;
1334 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1336 outlineInfo.addVariable("$"+variableDeclaration.name());
1337 if (token != null) {
1338 variableDeclaration.setReference(true);
1340 return variableDeclaration;}
1343 ConstantIdentifier Type() :
1344 {final Token token;}
1346 token = <STRING> {return new ConstantIdentifier(token);}
1347 | token = <BOOL> {return new ConstantIdentifier(token);}
1348 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1349 | token = <REAL> {return new ConstantIdentifier(token);}
1350 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1351 | token = <FLOAT> {return new ConstantIdentifier(token);}
1352 | token = <INT> {return new ConstantIdentifier(token);}
1353 | token = <INTEGER> {return new ConstantIdentifier(token);}
1354 | token = <OBJECT> {return new ConstantIdentifier(token);}
1357 Expression Expression() :
1359 final Expression expr;
1360 Expression initializer = null;
1361 int assignOperator = -1;
1365 expr = ConditionalExpression()
1367 assignOperator = AssignmentOperator()
1369 initializer = Expression()
1370 } catch (ParseException e) {
1371 if (errorMessage != null) {
1374 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1376 errorEnd = SimpleCharStream.getPosition();
1381 if (assignOperator != -1) {// todo : change this, very very bad :(
1382 if (expr instanceof AbstractVariable) {
1383 return new VariableDeclaration(currentSegment,
1384 (AbstractVariable) expr,
1387 initializer.sourceEnd);
1389 String varName = expr.toStringExpression().substring(1);
1390 return new VariableDeclaration(currentSegment,
1391 new Variable(varName,
1395 initializer.sourceEnd);
1399 | expr = ExpressionWBang() {return expr;}
1402 Expression ExpressionWBang() :
1404 final Expression expr;
1408 token = <BANG> expr = ExpressionWBang()
1409 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1410 | expr = ExpressionNoBang() {return expr;}
1413 Expression ExpressionNoBang() :
1418 expr = ListExpression() {return expr;}
1420 expr = PrintExpression() {return expr;}
1424 * Any assignement operator.
1425 * @return the assignement operator id
1427 int AssignmentOperator() :
1430 <ASSIGN> {return VariableDeclaration.EQUAL;}
1431 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1432 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1433 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1434 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1435 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1436 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1437 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1438 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1439 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1440 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1441 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1442 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1445 Expression ConditionalExpression() :
1447 final Expression expr;
1448 Expression expr2 = null;
1449 Expression expr3 = null;
1452 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1454 if (expr3 == null) {
1457 return new ConditionalExpression(expr,expr2,expr3);
1461 Expression ConditionalOrExpression() :
1463 Expression expr,expr2;
1467 expr = ConditionalAndExpression()
1470 <OR_OR> {operator = OperatorIds.OR_OR;}
1471 | <_ORL> {operator = OperatorIds.ORL;}
1473 expr2 = ConditionalAndExpression()
1475 expr = new BinaryExpression(expr,expr2,operator);
1481 Expression ConditionalAndExpression() :
1483 Expression expr,expr2;
1487 expr = ConcatExpression()
1489 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1490 | <_ANDL> {operator = OperatorIds.ANDL;})
1491 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1496 Expression ConcatExpression() :
1498 Expression expr,expr2;
1501 expr = InclusiveOrExpression()
1503 <DOT> expr2 = InclusiveOrExpression()
1504 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1509 Expression InclusiveOrExpression() :
1511 Expression expr,expr2;
1514 expr = ExclusiveOrExpression()
1515 (<BIT_OR> expr2 = ExclusiveOrExpression()
1516 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1521 Expression ExclusiveOrExpression() :
1523 Expression expr,expr2;
1526 expr = AndExpression()
1528 <XOR> expr2 = AndExpression()
1529 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1534 Expression AndExpression() :
1536 Expression expr,expr2;
1539 expr = EqualityExpression()
1542 <BIT_AND> expr2 = EqualityExpression()
1543 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1548 Expression EqualityExpression() :
1550 Expression expr,expr2;
1555 expr = RelationalExpression()
1557 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1558 | token = <DIF> {operator = OperatorIds.DIF;}
1559 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1560 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1561 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1564 expr2 = RelationalExpression()
1565 } catch (ParseException e) {
1566 if (errorMessage != null) {
1569 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1571 errorStart = token.sourceEnd +1;
1572 errorEnd = token.sourceEnd +1;
1573 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1574 processParseExceptionDebug(e);
1577 expr = new BinaryExpression(expr,expr2,operator);
1583 Expression RelationalExpression() :
1585 Expression expr,expr2;
1589 expr = ShiftExpression()
1591 ( <LT> {operator = OperatorIds.LESS;}
1592 | <GT> {operator = OperatorIds.GREATER;}
1593 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1594 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1595 expr2 = ShiftExpression()
1596 {expr = new BinaryExpression(expr,expr2,operator);}
1601 Expression ShiftExpression() :
1603 Expression expr,expr2;
1607 expr = AdditiveExpression()
1609 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1610 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1611 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1612 expr2 = AdditiveExpression()
1613 {expr = new BinaryExpression(expr,expr2,operator);}
1618 Expression AdditiveExpression() :
1620 Expression expr,expr2;
1624 expr = MultiplicativeExpression()
1627 ( <PLUS> {operator = OperatorIds.PLUS;}
1628 | <MINUS> {operator = OperatorIds.MINUS;}
1630 expr2 = MultiplicativeExpression()
1631 {expr = new BinaryExpression(expr,expr2,operator);}
1636 Expression MultiplicativeExpression() :
1638 Expression expr,expr2;
1643 expr = UnaryExpression()
1644 } catch (ParseException e) {
1645 if (errorMessage != null) throw e;
1646 errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1648 errorStart = PHPParser.token.sourceStart;
1649 errorEnd = PHPParser.token.sourceEnd;
1653 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1654 | <SLASH> {operator = OperatorIds.DIVIDE;}
1655 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1656 expr2 = UnaryExpression()
1657 {expr = new BinaryExpression(expr,expr2,operator);}
1663 * An unary expression starting with @, & or nothing
1665 Expression UnaryExpression() :
1667 final Expression expr;
1670 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1671 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1673 expr = AtNotTildeUnaryExpression() {return expr;}
1676 Expression AtNotTildeUnaryExpression() :
1678 final Expression expr;
1683 expr = AtNotTildeUnaryExpression()
1684 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1687 expr = AtNotTildeUnaryExpression()
1688 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1691 expr = AtNotUnaryExpression()
1692 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1694 expr = UnaryExpressionNoPrefix()
1699 * An expression prefixed (or not) by one or more @ and !.
1700 * @return the expression
1702 Expression AtNotUnaryExpression() :
1704 final Expression expr;
1709 expr = AtNotUnaryExpression()
1710 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1713 expr = AtNotUnaryExpression()
1714 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1716 expr = UnaryExpressionNoPrefix()
1720 Expression UnaryExpressionNoPrefix() :
1722 final Expression expr;
1726 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1728 token.sourceStart);}
1730 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1732 token.sourceStart);}
1734 expr = PreIncDecExpression()
1737 expr = UnaryExpressionNotPlusMinus()
1742 Expression PreIncDecExpression() :
1744 final Expression expr;
1750 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1752 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1754 expr = PrimaryExpression()
1755 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1758 Expression UnaryExpressionNotPlusMinus() :
1760 final Expression expr;
1763 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1764 expr = CastExpression() {return expr;}
1765 | expr = PostfixExpression() {return expr;}
1766 | expr = Literal() {return expr;}
1767 | <LPAREN> expr = Expression()
1770 } catch (ParseException e) {
1771 errorMessage = "')' expected";
1773 errorStart = expr.sourceEnd +1;
1774 errorEnd = expr.sourceEnd +1;
1775 processParseExceptionDebug(e);
1780 CastExpression CastExpression() :
1782 final ConstantIdentifier type;
1783 final Expression expr;
1784 final Token token,token1;
1791 token = <ARRAY> {type = new ConstantIdentifier(token);}
1793 <RPAREN> expr = UnaryExpression()
1794 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1797 Expression PostfixExpression() :
1799 final Expression expr;
1804 expr = PrimaryExpression()
1806 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1808 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1811 if (operator == -1) {
1814 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1818 Expression PrimaryExpression() :
1824 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1827 expr = ArrayDeclarator()
1831 Expression refPrimaryExpression(final Token reference) :
1834 Expression expr2 = null;
1835 final Token identifier;
1838 identifier = <IDENTIFIER>
1840 expr = new ConstantIdentifier(identifier);
1843 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1844 {expr = new ClassAccess(expr,
1846 ClassAccess.STATIC);}
1848 [ expr2 = Arguments(expr) ]
1850 if (expr2 == null) {
1851 if (reference != null) {
1852 ParseException e = generateParseException();
1853 errorMessage = "you cannot use a constant by reference";
1855 errorStart = reference.sourceStart;
1856 errorEnd = reference.sourceEnd;
1857 processParseExceptionDebug(e);
1864 expr = VariableDeclaratorId() //todo use the reference parameter ...
1865 [ expr = Arguments(expr) ]
1869 expr = ClassIdentifier()
1872 if (reference == null) {
1873 start = token.sourceStart;
1875 start = reference.sourceStart;
1877 expr = new ClassInstantiation(expr,
1881 [ expr = Arguments(expr) ]
1886 * An array declarator.
1890 ArrayInitializer ArrayDeclarator() :
1892 final ArrayVariableDeclaration[] vars;
1896 token = <ARRAY> vars = ArrayInitializer()
1897 {return new ArrayInitializer(vars,
1899 PHPParser.token.sourceEnd);}
1902 Expression ClassIdentifier():
1904 final Expression expr;
1908 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1909 | expr = Type() {return expr;}
1910 | expr = VariableDeclaratorId() {return expr;}
1914 * Used by Variabledeclaratorid and primarysuffix
1916 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1918 Expression expression = null;
1919 final Token classAccessToken,lbrace,rbrace;
1924 classAccessToken = <CLASSACCESS>
1927 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1929 expression = new Variable(expression,
1934 token = <IDENTIFIER>
1935 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1937 expression = Variable()
1939 } catch (ParseException e) {
1940 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1942 errorStart = classAccessToken.sourceEnd +1;
1943 errorEnd = classAccessToken.sourceEnd +1;
1944 processParseExceptionDebug(e);
1946 {return new ClassAccess(prefix,
1948 ClassAccess.NORMAL);}
1950 token = <LBRACKET> {pos = token.sourceEnd+1;}
1951 [ expression = Expression() {pos = expression.sourceEnd+1;}
1952 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1955 {pos = token.sourceEnd;}
1956 } catch (ParseException e) {
1957 errorMessage = "']' expected";
1961 processParseExceptionDebug(e);
1963 {return new ArrayDeclarator(prefix,expression,pos);}
1965 token = <LBRACE> {pos = token.sourceEnd+1;}
1966 [ expression = Expression() {pos = expression.sourceEnd+1;}
1967 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1970 {pos = token.sourceEnd;}
1971 } catch (ParseException e) {
1972 errorMessage = "']' expected";
1976 processParseExceptionDebug(e);
1978 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1984 StringLiteral literal;
1987 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1988 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1989 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1990 | token = <TRUE> {return new TrueLiteral(token);}
1991 | token = <FALSE> {return new FalseLiteral(token);}
1992 | token = <NULL> {return new NullLiteral(token);}
1993 | literal = evaluableString() {return literal;}
1996 StringLiteral evaluableString() :
1998 ArrayList list = new ArrayList();
2000 Token token,lbrace,rbrace;
2001 AbstractVariable var;
2005 start = <DOUBLEQUOTE>
2009 token = <IDENTIFIER> {list.add(new Variable(token.image,
2015 {list.add(new Variable(token.image,
2021 end = <DOUBLEQUOTE2>
2023 AbstractVariable[] vars = new AbstractVariable[list.size()];
2025 return new StringLiteral(SimpleCharStream.currentBuffer.substring(start.sourceEnd,end.sourceStart),
2032 FunctionCall Arguments(final Expression func) :
2034 Expression[] args = null;
2035 final Token token,lparen;
2038 lparen = <LPAREN> [ args = ArgumentList() ]
2041 {return new FunctionCall(func,args,token.sourceEnd);}
2042 } catch (ParseException e) {
2043 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2046 errorStart = lparen.sourceEnd+1;
2047 errorEnd = lparen.sourceEnd+2;
2049 errorStart = args[args.length-1].sourceEnd+1;
2050 errorEnd = args[args.length-1].sourceEnd+2;
2052 processParseExceptionDebug(e);
2055 int sourceEnd = (args == null && args.length != 0) ? lparen.sourceEnd+1 : args[args.length-1].sourceEnd;
2056 return new FunctionCall(func,args,sourceEnd);}
2060 * An argument list is a list of arguments separated by comma :
2061 * argumentDeclaration() (, argumentDeclaration)*
2062 * @return an array of arguments
2064 Expression[] ArgumentList() :
2067 final ArrayList list = new ArrayList();
2073 {list.add(arg);pos = arg.sourceEnd;}
2074 ( token = <COMMA> {pos = token.sourceEnd;}
2078 pos = arg.sourceEnd;}
2079 } catch (ParseException e) {
2080 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2084 processParseException(e);
2088 final Expression[] arguments = new Expression[list.size()];
2089 list.toArray(arguments);
2094 * A Statement without break.
2095 * @return a statement
2097 Statement StatementNoBreak() :
2099 final Statement statement;
2104 statement = expressionStatement() {return statement;}
2106 statement = LabeledStatement() {return statement;}
2107 | statement = Block() {return statement;}
2108 | statement = EmptyStatement() {return statement;}
2109 | statement = SwitchStatement() {return statement;}
2110 | statement = IfStatement() {return statement;}
2111 | statement = WhileStatement() {return statement;}
2112 | statement = DoStatement() {return statement;}
2113 | statement = ForStatement() {return statement;}
2114 | statement = ForeachStatement() {return statement;}
2115 | statement = ContinueStatement() {return statement;}
2116 | statement = ReturnStatement() {return statement;}
2117 | statement = EchoStatement() {return statement;}
2118 | [token=<AT>] statement = IncludeStatement()
2119 {if (token != null) {
2120 ((InclusionStatement)statement).silent = true;
2121 statement.sourceStart = token.sourceStart;
2124 | statement = StaticStatement() {return statement;}
2125 | statement = GlobalStatement() {return statement;}
2126 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2130 * A statement expression.
2132 * @return an expression
2134 Statement expressionStatement() :
2136 final Statement statement;
2140 statement = Expression()
2143 {statement.sourceEnd = token.sourceEnd;}
2144 } catch (ParseException e) {
2145 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2146 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2148 errorStart = statement.sourceEnd+1;
2149 errorEnd = statement.sourceEnd+1;
2150 processParseExceptionDebug(e);
2156 Define defineStatement() :
2158 Expression defineName,defineValue;
2159 final Token defineToken;
2164 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2167 {pos = token.sourceEnd+1;}
2168 } catch (ParseException e) {
2169 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2173 processParseExceptionDebug(e);
2176 defineName = Expression()
2177 {pos = defineName.sourceEnd+1;}
2178 } catch (ParseException e) {
2179 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2183 processParseExceptionDebug(e);
2184 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2188 {pos = defineName.sourceEnd+1;}
2189 } catch (ParseException e) {
2190 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2194 processParseExceptionDebug(e);
2197 defineValue = Expression()
2198 {pos = defineValue.sourceEnd+1;}
2199 } catch (ParseException e) {
2200 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2204 processParseExceptionDebug(e);
2205 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2209 {pos = token.sourceEnd+1;}
2210 } catch (ParseException e) {
2211 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2215 processParseExceptionDebug(e);
2217 {return new Define(currentSegment,
2220 defineToken.sourceStart,
2225 * A Normal statement.
2227 Statement Statement() :
2229 final Statement statement;
2232 statement = StatementNoBreak() {return statement;}
2233 | statement = BreakStatement() {return statement;}
2237 * An html block inside a php syntax.
2239 HTMLBlock htmlBlock() :
2241 final int startIndex = nodePtr;
2242 final AstNode[] blockNodes;
2248 {htmlStart = phpEnd.sourceEnd;}
2251 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2252 {PHPParser.createNewHTMLCode();}
2253 } catch (ParseException e) {
2254 errorMessage = "unexpected end of file , '<?php' expected";
2256 errorStart = e.currentToken.sourceStart;
2257 errorEnd = e.currentToken.sourceEnd;
2261 nbNodes = nodePtr - startIndex;
2265 blockNodes = new AstNode[nbNodes];
2266 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2267 nodePtr = startIndex;
2268 return new HTMLBlock(blockNodes);}
2272 * An include statement. It's "include" an expression;
2274 InclusionStatement IncludeStatement() :
2278 final InclusionStatement inclusionStatement;
2279 final Token token, token2;
2283 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2284 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2285 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2286 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2289 {pos = expr.sourceEnd;}
2290 } catch (ParseException e) {
2291 if (errorMessage != null) {
2294 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2296 errorStart = e.currentToken.next.sourceStart;
2297 errorEnd = e.currentToken.next.sourceEnd;
2298 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2299 processParseExceptionDebug(e);
2302 token2 = <SEMICOLON>
2303 {pos=token2.sourceEnd;}
2304 } catch (ParseException e) {
2305 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2307 errorStart = e.currentToken.next.sourceStart;
2308 errorEnd = e.currentToken.next.sourceEnd;
2309 processParseExceptionDebug(e);
2312 inclusionStatement = new InclusionStatement(currentSegment,
2317 currentSegment.add(inclusionStatement);
2318 return inclusionStatement;
2322 PrintExpression PrintExpression() :
2324 final Expression expr;
2325 final Token printToken;
2328 token = <PRINT> expr = Expression()
2329 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2332 ListExpression ListExpression() :
2334 Expression expr = null;
2335 final Expression expression;
2336 final ArrayList list = new ArrayList();
2338 final Token listToken, rParen;
2342 listToken = <LIST> {pos = listToken.sourceEnd;}
2344 token = <LPAREN> {pos = token.sourceEnd;}
2345 } catch (ParseException e) {
2346 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2348 errorStart = listToken.sourceEnd+1;
2349 errorEnd = listToken.sourceEnd+1;
2350 processParseExceptionDebug(e);
2353 expr = VariableDeclaratorId()
2354 {list.add(expr);pos = expr.sourceEnd;}
2356 {if (expr == null) list.add(null);}
2360 {pos = token.sourceEnd;}
2361 } catch (ParseException e) {
2362 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2366 processParseExceptionDebug(e);
2368 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2372 {pos = rParen.sourceEnd;}
2373 } catch (ParseException e) {
2374 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2378 processParseExceptionDebug(e);
2380 [ <ASSIGN> expression = Expression()
2382 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2384 return new ListExpression(vars,
2386 listToken.sourceStart,
2387 expression.sourceEnd);}
2390 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2392 return new ListExpression(vars,listToken.sourceStart,pos);}
2396 * An echo statement.
2397 * echo anyexpression (, otherexpression)*
2399 EchoStatement EchoStatement() :
2401 final ArrayList expressions = new ArrayList();
2404 Token token2 = null;
2407 token = <ECHO> expr = Expression()
2408 {expressions.add(expr);}
2410 <COMMA> expr = Expression()
2411 {expressions.add(expr);}
2414 token2 = <SEMICOLON>
2415 } catch (ParseException e) {
2416 if (e.currentToken.next.kind != 4) {
2417 errorMessage = "';' expected after 'echo' statement";
2419 errorStart = e.currentToken.sourceEnd;
2420 errorEnd = e.currentToken.sourceEnd;
2421 processParseExceptionDebug(e);
2425 final Expression[] exprs = new Expression[expressions.size()];
2426 expressions.toArray(exprs);
2427 if (token2 == null) {
2428 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2430 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2434 GlobalStatement GlobalStatement() :
2437 final ArrayList vars = new ArrayList();
2438 final GlobalStatement global;
2439 final Token token, token2;
2445 {vars.add(expr);pos = expr.sourceEnd+1;}
2448 {vars.add(expr);pos = expr.sourceEnd+1;}
2451 token2 = <SEMICOLON>
2452 {pos = token2.sourceEnd+1;}
2453 } catch (ParseException e) {
2454 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2458 processParseExceptionDebug(e);
2461 final Variable[] variables = new Variable[vars.size()];
2462 vars.toArray(variables);
2463 global = new GlobalStatement(currentSegment,
2467 currentSegment.add(global);
2471 StaticStatement StaticStatement() :
2473 final ArrayList vars = new ArrayList();
2474 VariableDeclaration expr;
2475 final Token token, token2;
2479 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2481 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2484 token2 = <SEMICOLON>
2485 {pos = token2.sourceEnd+1;}
2486 } catch (ParseException e) {
2487 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2491 processParseException(e);
2494 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2495 vars.toArray(variables);
2496 return new StaticStatement(variables,
2501 LabeledStatement LabeledStatement() :
2504 final Statement statement;
2507 label = <IDENTIFIER> <COLON> statement = Statement()
2508 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2520 final ArrayList list = new ArrayList();
2521 Statement statement;
2522 final Token token, token2;
2528 {pos = token.sourceEnd+1;start=token.sourceStart;}
2529 } catch (ParseException e) {
2530 errorMessage = "'{' expected";
2532 pos = PHPParser.token.sourceEnd+1;
2536 processParseExceptionDebug(e);
2538 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2539 | statement = htmlBlock() {if (statement != null) {
2540 list.add(statement);
2541 pos = statement.sourceEnd+1;
2543 pos = PHPParser.token.sourceEnd+1;
2548 {pos = token2.sourceEnd+1;}
2549 } catch (ParseException e) {
2550 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2554 processParseExceptionDebug(e);
2557 final Statement[] statements = new Statement[list.size()];
2558 list.toArray(statements);
2559 return new Block(statements,start,pos);}
2562 Statement BlockStatement() :
2564 final Statement statement;
2568 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2570 } catch (ParseException e) {
2571 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2573 errorStart = e.currentToken.sourceStart;
2574 errorEnd = e.currentToken.sourceEnd;
2577 | statement = ClassDeclaration() {return statement;}
2578 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2579 currentSegment.add((MethodDeclaration) statement);
2580 ((MethodDeclaration) statement).analyzeCode();
2585 * A Block statement that will not contain any 'break'
2587 Statement BlockStatementNoBreak() :
2589 final Statement statement;
2592 statement = StatementNoBreak() {return statement;}
2593 | statement = ClassDeclaration() {return statement;}
2594 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2595 ((MethodDeclaration) statement).analyzeCode();
2600 * used only by ForInit()
2602 Expression[] LocalVariableDeclaration() :
2604 final ArrayList list = new ArrayList();
2610 ( <COMMA> var = Expression() {list.add(var);})*
2612 final Expression[] vars = new Expression[list.size()];
2619 * used only by LocalVariableDeclaration().
2621 VariableDeclaration LocalVariableDeclarator() :
2623 final Variable varName;
2624 Expression initializer = null;
2627 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2629 if (initializer == null) {
2630 return new VariableDeclaration(currentSegment,
2632 varName.sourceStart,
2635 return new VariableDeclaration(currentSegment,
2638 VariableDeclaration.EQUAL,
2639 varName.sourceStart);
2643 EmptyStatement EmptyStatement() :
2649 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2653 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2655 Expression StatementExpression() :
2657 final Expression expr;
2658 final Token operator;
2661 expr = PreIncDecExpression() {return expr;}
2663 expr = PrimaryExpression()
2664 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2665 OperatorIds.PLUS_PLUS,
2666 operator.sourceEnd);}
2667 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2668 OperatorIds.MINUS_MINUS,
2669 operator.sourceEnd);}
2674 SwitchStatement SwitchStatement() :
2676 Expression variable;
2677 final AbstractCase[] cases;
2678 final Token switchToken,lparenToken,rparenToken;
2682 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2684 lparenToken = <LPAREN>
2685 {pos = lparenToken.sourceEnd+1;}
2686 } catch (ParseException e) {
2687 errorMessage = "'(' expected after 'switch'";
2691 processParseExceptionDebug(e);
2694 variable = Expression() {pos = variable.sourceEnd+1;}
2695 } catch (ParseException e) {
2696 if (errorMessage != null) {
2699 errorMessage = "expression expected";
2703 processParseExceptionDebug(e);
2704 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2707 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2708 } catch (ParseException e) {
2709 errorMessage = "')' expected";
2713 processParseExceptionDebug(e);
2715 ( cases = switchStatementBrace()
2716 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2717 {return new SwitchStatement(variable,
2719 switchToken.sourceStart,
2720 PHPParser.token.sourceEnd);}
2723 AbstractCase[] switchStatementBrace() :
2726 final ArrayList cases = new ArrayList();
2731 token = <LBRACE> {pos = token.sourceEnd;}
2732 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2735 {pos = token.sourceEnd;}
2736 } catch (ParseException e) {
2737 errorMessage = "'}' expected";
2741 processParseExceptionDebug(e);
2744 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2745 cases.toArray(abcase);
2751 * A Switch statement with : ... endswitch;
2752 * @param start the begin offset of the switch
2753 * @param end the end offset of the switch
2755 AbstractCase[] switchStatementColon(final int start, final int end) :
2758 final ArrayList cases = new ArrayList();
2763 token = <COLON> {pos = token.sourceEnd;}
2765 setMarker(fileToParse,
2766 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2770 "Line " + token.beginLine);
2771 } catch (CoreException e) {
2772 PHPeclipsePlugin.log(e);
2774 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2776 token = <ENDSWITCH> {pos = token.sourceEnd;}
2777 } catch (ParseException e) {
2778 errorMessage = "'endswitch' expected";
2782 processParseExceptionDebug(e);
2785 token = <SEMICOLON> {pos = token.sourceEnd;}
2786 } catch (ParseException e) {
2787 errorMessage = "';' expected after 'endswitch' keyword";
2791 processParseExceptionDebug(e);
2794 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2795 cases.toArray(abcase);
2800 AbstractCase switchLabel0() :
2802 final Expression expr;
2803 Statement statement;
2804 final ArrayList stmts = new ArrayList();
2805 final Token token = PHPParser.token;
2808 expr = SwitchLabel()
2809 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2810 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2811 | statement = BreakStatement() {stmts.add(statement);})*
2812 //[ statement = BreakStatement() {stmts.add(statement);}]
2814 final int listSize = stmts.size();
2815 final Statement[] stmtsArray = new Statement[listSize];
2816 stmts.toArray(stmtsArray);
2817 if (expr == null) {//it's a default
2818 return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd);
2820 if (listSize != 0) {
2821 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2823 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2830 * case Expression() :
2832 * @return the if it was a case and null if not
2834 Expression SwitchLabel() :
2836 final Expression expr;
2842 } catch (ParseException e) {
2843 if (errorMessage != null) throw e;
2844 errorMessage = "expression expected after 'case' keyword";
2846 errorStart = token.sourceEnd +1;
2847 errorEnd = token.sourceEnd +1;
2852 } catch (ParseException e) {
2853 errorMessage = "':' expected after case expression";
2855 errorStart = expr.sourceEnd+1;
2856 errorEnd = expr.sourceEnd+1;
2857 processParseExceptionDebug(e);
2864 } catch (ParseException e) {
2865 errorMessage = "':' expected after 'default' keyword";
2867 errorStart = token.sourceEnd+1;
2868 errorEnd = token.sourceEnd+1;
2869 processParseExceptionDebug(e);
2874 Break BreakStatement() :
2876 Expression expression = null;
2877 final Token token, token2;
2881 token = <BREAK> {pos = token.sourceEnd+1;}
2882 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2884 token2 = <SEMICOLON>
2885 {pos = token2.sourceEnd;}
2886 } catch (ParseException e) {
2887 errorMessage = "';' expected after 'break' keyword";
2891 processParseExceptionDebug(e);
2893 {return new Break(expression, token.sourceStart, pos);}
2896 IfStatement IfStatement() :
2898 final Expression condition;
2899 final IfStatement ifStatement;
2903 token = <IF> condition = Condition("if")
2904 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2905 {return ifStatement;}
2909 Expression Condition(final String keyword) :
2911 final Expression condition;
2916 } catch (ParseException e) {
2917 errorMessage = "'(' expected after " + keyword + " keyword";
2919 errorStart = PHPParser.token.sourceEnd + 1;
2920 errorEnd = PHPParser.token.sourceEnd + 1;
2921 processParseExceptionDebug(e);
2923 condition = Expression()
2926 } catch (ParseException e) {
2927 errorMessage = "')' expected after " + keyword + " keyword";
2929 errorStart = condition.sourceEnd+1;
2930 errorEnd = condition.sourceEnd+1;
2931 processParseExceptionDebug(e);
2936 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2938 Statement statement;
2939 final Statement stmt;
2940 final Statement[] statementsArray;
2941 ElseIf elseifStatement;
2942 Else elseStatement = null;
2943 final ArrayList stmts;
2944 final ArrayList elseIfList = new ArrayList();
2945 final ElseIf[] elseIfs;
2946 int pos = SimpleCharStream.getPosition();
2947 final int endStatements;
2951 {stmts = new ArrayList();}
2952 ( statement = Statement() {stmts.add(statement);}
2953 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2954 {endStatements = SimpleCharStream.getPosition();}
2955 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2956 [elseStatement = ElseStatementColon()]
2959 setMarker(fileToParse,
2960 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2964 "Line " + token.beginLine);
2965 } catch (CoreException e) {
2966 PHPeclipsePlugin.log(e);
2970 } catch (ParseException e) {
2971 errorMessage = "'endif' expected";
2973 errorStart = e.currentToken.sourceStart;
2974 errorEnd = e.currentToken.sourceEnd;
2979 } catch (ParseException e) {
2980 errorMessage = "';' expected after 'endif' keyword";
2982 errorStart = e.currentToken.sourceStart;
2983 errorEnd = e.currentToken.sourceEnd;
2987 elseIfs = new ElseIf[elseIfList.size()];
2988 elseIfList.toArray(elseIfs);
2989 if (stmts.size() == 1) {
2990 return new IfStatement(condition,
2991 (Statement) stmts.get(0),
2995 SimpleCharStream.getPosition());
2997 statementsArray = new Statement[stmts.size()];
2998 stmts.toArray(statementsArray);
2999 return new IfStatement(condition,
3000 new Block(statementsArray,pos,endStatements),
3004 SimpleCharStream.getPosition());
3009 (stmt = Statement() | stmt = htmlBlock())
3010 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3014 {pos = SimpleCharStream.getPosition();}
3015 statement = Statement()
3016 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
3017 } catch (ParseException e) {
3018 if (errorMessage != null) {
3021 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3023 errorStart = e.currentToken.sourceStart;
3024 errorEnd = e.currentToken.sourceEnd;
3029 elseIfs = new ElseIf[elseIfList.size()];
3030 elseIfList.toArray(elseIfs);
3031 return new IfStatement(condition,
3036 SimpleCharStream.getPosition());}
3039 ElseIf ElseIfStatementColon() :
3041 final Expression condition;
3042 Statement statement;
3043 final ArrayList list = new ArrayList();
3044 final Token elseifToken;
3047 elseifToken = <ELSEIF> condition = Condition("elseif")
3048 <COLON> ( statement = Statement() {list.add(statement);}
3049 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3051 final int sizeList = list.size();
3052 final Statement[] stmtsArray = new Statement[sizeList];
3053 list.toArray(stmtsArray);
3054 return new ElseIf(condition,stmtsArray ,
3055 elseifToken.sourceStart,
3056 stmtsArray[sizeList-1].sourceEnd);}
3059 Else ElseStatementColon() :
3061 Statement statement;
3062 final ArrayList list = new ArrayList();
3063 final Token elseToken;
3066 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3067 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3069 final int sizeList = list.size();
3070 final Statement[] stmtsArray = new Statement[sizeList];
3071 list.toArray(stmtsArray);
3072 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3075 ElseIf ElseIfStatement() :
3077 final Expression condition;
3078 //final Statement statement;
3079 final Token elseifToken;
3080 final Statement[] statement = new Statement[1];
3083 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3085 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3088 WhileStatement WhileStatement() :
3090 final Expression condition;
3091 final Statement action;
3092 final Token whileToken;
3095 whileToken = <WHILE>
3096 condition = Condition("while")
3097 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3098 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3101 Statement WhileStatement0(final int start, final int end) :
3103 Statement statement;
3104 final ArrayList stmts = new ArrayList();
3105 final int pos = SimpleCharStream.getPosition();
3108 <COLON> (statement = Statement() {stmts.add(statement);})*
3110 setMarker(fileToParse,
3111 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3115 "Line " + token.beginLine);
3116 } catch (CoreException e) {
3117 PHPeclipsePlugin.log(e);
3121 } catch (ParseException e) {
3122 errorMessage = "'endwhile' expected";
3124 errorStart = e.currentToken.sourceStart;
3125 errorEnd = e.currentToken.sourceEnd;
3131 final Statement[] stmtsArray = new Statement[stmts.size()];
3132 stmts.toArray(stmtsArray);
3133 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3134 } catch (ParseException e) {
3135 errorMessage = "';' expected after 'endwhile' keyword";
3137 errorStart = e.currentToken.sourceStart;
3138 errorEnd = e.currentToken.sourceEnd;
3142 statement = Statement()
3146 DoStatement DoStatement() :
3148 final Statement action;
3149 final Expression condition;
3151 Token token2 = null;
3154 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3156 token2 = <SEMICOLON>
3157 } catch (ParseException e) {
3158 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3160 errorStart = condition.sourceEnd+1;
3161 errorEnd = condition.sourceEnd+1;
3162 processParseExceptionDebug(e);
3165 if (token2 == null) {
3166 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3168 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3172 ForeachStatement ForeachStatement() :
3174 Statement statement = null;
3175 Expression expression = null;
3176 ArrayVariableDeclaration variable = null;
3178 Token lparenToken = null;
3179 Token asToken = null;
3180 Token rparenToken = null;
3184 foreachToken = <FOREACH>
3186 lparenToken = <LPAREN>
3187 {pos = lparenToken.sourceEnd+1;}
3188 } catch (ParseException e) {
3189 errorMessage = "'(' expected after 'foreach' keyword";
3191 errorStart = e.currentToken.sourceStart;
3192 errorEnd = e.currentToken.sourceEnd;
3193 processParseExceptionDebug(e);
3194 {pos = foreachToken.sourceEnd+1;}
3197 expression = Expression()
3198 {pos = expression.sourceEnd+1;}
3199 } catch (ParseException e) {
3200 errorMessage = "variable expected";
3202 errorStart = e.currentToken.sourceStart;
3203 errorEnd = e.currentToken.sourceEnd;
3204 processParseExceptionDebug(e);
3208 {pos = asToken.sourceEnd+1;}
3209 } catch (ParseException e) {
3210 errorMessage = "'as' expected";
3212 errorStart = e.currentToken.sourceStart;
3213 errorEnd = e.currentToken.sourceEnd;
3214 processParseExceptionDebug(e);
3217 variable = ArrayVariable()
3218 {pos = variable.sourceEnd+1;}
3219 } catch (ParseException e) {
3220 if (errorMessage != null) throw e;
3221 errorMessage = "variable expected";
3223 errorStart = e.currentToken.sourceStart;
3224 errorEnd = e.currentToken.sourceEnd;
3225 processParseExceptionDebug(e);
3228 rparenToken = <RPAREN>
3229 {pos = rparenToken.sourceEnd+1;}
3230 } catch (ParseException e) {
3231 errorMessage = "')' expected after 'foreach' keyword";
3233 errorStart = e.currentToken.sourceStart;
3234 errorEnd = e.currentToken.sourceEnd;
3235 processParseExceptionDebug(e);
3238 statement = Statement()
3239 {pos = rparenToken.sourceEnd+1;}
3240 } catch (ParseException e) {
3241 if (errorMessage != null) throw e;
3242 errorMessage = "statement expected";
3244 errorStart = e.currentToken.sourceStart;
3245 errorEnd = e.currentToken.sourceEnd;
3246 processParseExceptionDebug(e);
3248 {return new ForeachStatement(expression,
3251 foreachToken.sourceStart,
3252 statement.sourceEnd);}
3257 * a for declaration.
3258 * @return a node representing the for statement
3260 ForStatement ForStatement() :
3262 final Token token,tokenEndFor,token2,tokenColon;
3264 Expression[] initializations = null;
3265 Expression condition = null;
3266 Expression[] increments = null;
3268 final ArrayList list = new ArrayList();
3274 } catch (ParseException e) {
3275 errorMessage = "'(' expected after 'for' keyword";
3277 errorStart = token.sourceEnd;
3278 errorEnd = token.sourceEnd +1;
3279 processParseExceptionDebug(e);
3281 [ initializations = ForInit() ] <SEMICOLON>
3282 [ condition = Expression() ] <SEMICOLON>
3283 [ increments = StatementExpressionList() ] <RPAREN>
3285 action = Statement()
3286 {return new ForStatement(initializations,
3293 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3294 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3297 setMarker(fileToParse,
3298 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3302 "Line " + token.beginLine);
3303 } catch (CoreException e) {
3304 PHPeclipsePlugin.log(e);
3308 tokenEndFor = <ENDFOR>
3309 {pos = tokenEndFor.sourceEnd+1;}
3310 } catch (ParseException e) {
3311 errorMessage = "'endfor' expected";
3315 processParseExceptionDebug(e);
3318 token2 = <SEMICOLON>
3319 {pos = token2.sourceEnd+1;}
3320 } catch (ParseException e) {
3321 errorMessage = "';' expected after 'endfor' keyword";
3325 processParseExceptionDebug(e);
3328 final Statement[] stmtsArray = new Statement[list.size()];
3329 list.toArray(stmtsArray);
3330 return new ForStatement(initializations,
3333 new Block(stmtsArray,
3334 stmtsArray[0].sourceStart,
3335 stmtsArray[stmtsArray.length-1].sourceEnd),
3341 Expression[] ForInit() :
3343 final Expression[] exprs;
3346 LOOKAHEAD(LocalVariableDeclaration())
3347 exprs = LocalVariableDeclaration()
3350 exprs = StatementExpressionList()
3354 Expression[] StatementExpressionList() :
3356 final ArrayList list = new ArrayList();
3357 final Expression expr;
3360 expr = Expression() {list.add(expr);}
3361 (<COMMA> Expression() {list.add(expr);})*
3363 final Expression[] exprsArray = new Expression[list.size()];
3364 list.toArray(exprsArray);
3369 Continue ContinueStatement() :
3371 Expression expr = null;
3373 Token token2 = null;
3376 token = <CONTINUE> [ expr = Expression() ]
3378 token2 = <SEMICOLON>
3379 } catch (ParseException e) {
3380 errorMessage = "';' expected after 'continue' statement";
3383 errorStart = token.sourceEnd+1;
3384 errorEnd = token.sourceEnd+1;
3386 errorStart = expr.sourceEnd+1;
3387 errorEnd = expr.sourceEnd+1;
3389 processParseExceptionDebug(e);
3392 if (token2 == null) {
3394 return new Continue(expr,token.sourceStart,token.sourceEnd);
3396 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3398 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3402 ReturnStatement ReturnStatement() :
3404 Expression expr = null;
3406 Token token2 = null;
3409 token = <RETURN> [ expr = Expression() ]
3411 token2 = <SEMICOLON>
3412 } catch (ParseException e) {
3413 errorMessage = "';' expected after 'return' statement";
3416 errorStart = token.sourceEnd+1;
3417 errorEnd = token.sourceEnd+1;
3419 errorStart = expr.sourceEnd+1;
3420 errorEnd = expr.sourceEnd+1;
3422 processParseExceptionDebug(e);
3425 if (token2 == null) {
3427 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3429 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3431 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);