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;
2806 final int start = PHPParser.token.next.sourceStart;
2809 expr = SwitchLabel()
2810 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2811 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2812 | statement = BreakStatement() {stmts.add(statement);})*
2813 //[ statement = BreakStatement() {stmts.add(statement);}]
2815 final int listSize = stmts.size();
2816 final Statement[] stmtsArray = new Statement[listSize];
2817 stmts.toArray(stmtsArray);
2818 if (expr == null) {//it's a default
2819 final int end = PHPParser.token.next.sourceStart;
2820 return new DefaultCase(stmtsArray,start,end);
2822 if (listSize != 0) {
2823 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2825 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2832 * case Expression() :
2834 * @return the if it was a case and null if not
2836 Expression SwitchLabel() :
2838 final Expression expr;
2844 } catch (ParseException e) {
2845 if (errorMessage != null) throw e;
2846 errorMessage = "expression expected after 'case' keyword";
2848 errorStart = token.sourceEnd +1;
2849 errorEnd = token.sourceEnd +1;
2854 } catch (ParseException e) {
2855 errorMessage = "':' expected after case expression";
2857 errorStart = expr.sourceEnd+1;
2858 errorEnd = expr.sourceEnd+1;
2859 processParseExceptionDebug(e);
2866 } catch (ParseException e) {
2867 errorMessage = "':' expected after 'default' keyword";
2869 errorStart = token.sourceEnd+1;
2870 errorEnd = token.sourceEnd+1;
2871 processParseExceptionDebug(e);
2876 Break BreakStatement() :
2878 Expression expression = null;
2879 final Token token, token2;
2883 token = <BREAK> {pos = token.sourceEnd+1;}
2884 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2886 token2 = <SEMICOLON>
2887 {pos = token2.sourceEnd;}
2888 } catch (ParseException e) {
2889 errorMessage = "';' expected after 'break' keyword";
2893 processParseExceptionDebug(e);
2895 {return new Break(expression, token.sourceStart, pos);}
2898 IfStatement IfStatement() :
2900 final Expression condition;
2901 final IfStatement ifStatement;
2905 token = <IF> condition = Condition("if")
2906 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2907 {return ifStatement;}
2911 Expression Condition(final String keyword) :
2913 final Expression condition;
2918 } catch (ParseException e) {
2919 errorMessage = "'(' expected after " + keyword + " keyword";
2921 errorStart = PHPParser.token.sourceEnd + 1;
2922 errorEnd = PHPParser.token.sourceEnd + 1;
2923 processParseExceptionDebug(e);
2925 condition = Expression()
2928 } catch (ParseException e) {
2929 errorMessage = "')' expected after " + keyword + " keyword";
2931 errorStart = condition.sourceEnd+1;
2932 errorEnd = condition.sourceEnd+1;
2933 processParseExceptionDebug(e);
2938 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2940 Statement statement;
2941 final Statement stmt;
2942 final Statement[] statementsArray;
2943 ElseIf elseifStatement;
2944 Else elseStatement = null;
2945 final ArrayList stmts;
2946 final ArrayList elseIfList = new ArrayList();
2947 final ElseIf[] elseIfs;
2948 int pos = SimpleCharStream.getPosition();
2949 final int endStatements;
2953 {stmts = new ArrayList();}
2954 ( statement = Statement() {stmts.add(statement);}
2955 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2956 {endStatements = SimpleCharStream.getPosition();}
2957 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2958 [elseStatement = ElseStatementColon()]
2961 setMarker(fileToParse,
2962 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2966 "Line " + token.beginLine);
2967 } catch (CoreException e) {
2968 PHPeclipsePlugin.log(e);
2972 } catch (ParseException e) {
2973 errorMessage = "'endif' expected";
2975 errorStart = e.currentToken.sourceStart;
2976 errorEnd = e.currentToken.sourceEnd;
2981 } catch (ParseException e) {
2982 errorMessage = "';' expected after 'endif' keyword";
2984 errorStart = e.currentToken.sourceStart;
2985 errorEnd = e.currentToken.sourceEnd;
2989 elseIfs = new ElseIf[elseIfList.size()];
2990 elseIfList.toArray(elseIfs);
2991 if (stmts.size() == 1) {
2992 return new IfStatement(condition,
2993 (Statement) stmts.get(0),
2997 SimpleCharStream.getPosition());
2999 statementsArray = new Statement[stmts.size()];
3000 stmts.toArray(statementsArray);
3001 return new IfStatement(condition,
3002 new Block(statementsArray,pos,endStatements),
3006 SimpleCharStream.getPosition());
3011 (stmt = Statement() | stmt = htmlBlock())
3012 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3016 {pos = SimpleCharStream.getPosition();}
3017 statement = Statement()
3018 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
3019 } catch (ParseException e) {
3020 if (errorMessage != null) {
3023 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3025 errorStart = e.currentToken.sourceStart;
3026 errorEnd = e.currentToken.sourceEnd;
3031 elseIfs = new ElseIf[elseIfList.size()];
3032 elseIfList.toArray(elseIfs);
3033 return new IfStatement(condition,
3038 SimpleCharStream.getPosition());}
3041 ElseIf ElseIfStatementColon() :
3043 final Expression condition;
3044 Statement statement;
3045 final ArrayList list = new ArrayList();
3046 final Token elseifToken;
3049 elseifToken = <ELSEIF> condition = Condition("elseif")
3050 <COLON> ( statement = Statement() {list.add(statement);}
3051 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3053 final int sizeList = list.size();
3054 final Statement[] stmtsArray = new Statement[sizeList];
3055 list.toArray(stmtsArray);
3056 return new ElseIf(condition,stmtsArray ,
3057 elseifToken.sourceStart,
3058 stmtsArray[sizeList-1].sourceEnd);}
3061 Else ElseStatementColon() :
3063 Statement statement;
3064 final ArrayList list = new ArrayList();
3065 final Token elseToken;
3068 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3069 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3071 final int sizeList = list.size();
3072 final Statement[] stmtsArray = new Statement[sizeList];
3073 list.toArray(stmtsArray);
3074 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3077 ElseIf ElseIfStatement() :
3079 final Expression condition;
3080 //final Statement statement;
3081 final Token elseifToken;
3082 final Statement[] statement = new Statement[1];
3085 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3087 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3090 WhileStatement WhileStatement() :
3092 final Expression condition;
3093 final Statement action;
3094 final Token whileToken;
3097 whileToken = <WHILE>
3098 condition = Condition("while")
3099 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3100 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3103 Statement WhileStatement0(final int start, final int end) :
3105 Statement statement;
3106 final ArrayList stmts = new ArrayList();
3107 final int pos = SimpleCharStream.getPosition();
3110 <COLON> (statement = Statement() {stmts.add(statement);})*
3112 setMarker(fileToParse,
3113 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3117 "Line " + token.beginLine);
3118 } catch (CoreException e) {
3119 PHPeclipsePlugin.log(e);
3123 } catch (ParseException e) {
3124 errorMessage = "'endwhile' expected";
3126 errorStart = e.currentToken.sourceStart;
3127 errorEnd = e.currentToken.sourceEnd;
3133 final Statement[] stmtsArray = new Statement[stmts.size()];
3134 stmts.toArray(stmtsArray);
3135 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3136 } catch (ParseException e) {
3137 errorMessage = "';' expected after 'endwhile' keyword";
3139 errorStart = e.currentToken.sourceStart;
3140 errorEnd = e.currentToken.sourceEnd;
3144 statement = Statement()
3148 DoStatement DoStatement() :
3150 final Statement action;
3151 final Expression condition;
3153 Token token2 = null;
3156 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3158 token2 = <SEMICOLON>
3159 } catch (ParseException e) {
3160 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3162 errorStart = condition.sourceEnd+1;
3163 errorEnd = condition.sourceEnd+1;
3164 processParseExceptionDebug(e);
3167 if (token2 == null) {
3168 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3170 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3174 ForeachStatement ForeachStatement() :
3176 Statement statement = null;
3177 Expression expression = null;
3178 ArrayVariableDeclaration variable = null;
3180 Token lparenToken = null;
3181 Token asToken = null;
3182 Token rparenToken = null;
3186 foreachToken = <FOREACH>
3188 lparenToken = <LPAREN>
3189 {pos = lparenToken.sourceEnd+1;}
3190 } catch (ParseException e) {
3191 errorMessage = "'(' expected after 'foreach' keyword";
3193 errorStart = e.currentToken.sourceStart;
3194 errorEnd = e.currentToken.sourceEnd;
3195 processParseExceptionDebug(e);
3196 {pos = foreachToken.sourceEnd+1;}
3199 expression = Expression()
3200 {pos = expression.sourceEnd+1;}
3201 } catch (ParseException e) {
3202 errorMessage = "variable expected";
3204 errorStart = e.currentToken.sourceStart;
3205 errorEnd = e.currentToken.sourceEnd;
3206 processParseExceptionDebug(e);
3210 {pos = asToken.sourceEnd+1;}
3211 } catch (ParseException e) {
3212 errorMessage = "'as' expected";
3214 errorStart = e.currentToken.sourceStart;
3215 errorEnd = e.currentToken.sourceEnd;
3216 processParseExceptionDebug(e);
3219 variable = ArrayVariable()
3220 {pos = variable.sourceEnd+1;}
3221 } catch (ParseException e) {
3222 if (errorMessage != null) throw e;
3223 errorMessage = "variable expected";
3225 errorStart = e.currentToken.sourceStart;
3226 errorEnd = e.currentToken.sourceEnd;
3227 processParseExceptionDebug(e);
3230 rparenToken = <RPAREN>
3231 {pos = rparenToken.sourceEnd+1;}
3232 } catch (ParseException e) {
3233 errorMessage = "')' expected after 'foreach' keyword";
3235 errorStart = e.currentToken.sourceStart;
3236 errorEnd = e.currentToken.sourceEnd;
3237 processParseExceptionDebug(e);
3240 statement = Statement()
3241 {pos = rparenToken.sourceEnd+1;}
3242 } catch (ParseException e) {
3243 if (errorMessage != null) throw e;
3244 errorMessage = "statement expected";
3246 errorStart = e.currentToken.sourceStart;
3247 errorEnd = e.currentToken.sourceEnd;
3248 processParseExceptionDebug(e);
3250 {return new ForeachStatement(expression,
3253 foreachToken.sourceStart,
3254 statement.sourceEnd);}
3259 * a for declaration.
3260 * @return a node representing the for statement
3262 ForStatement ForStatement() :
3264 final Token token,tokenEndFor,token2,tokenColon;
3266 Expression[] initializations = null;
3267 Expression condition = null;
3268 Expression[] increments = null;
3270 final ArrayList list = new ArrayList();
3276 } catch (ParseException e) {
3277 errorMessage = "'(' expected after 'for' keyword";
3279 errorStart = token.sourceEnd;
3280 errorEnd = token.sourceEnd +1;
3281 processParseExceptionDebug(e);
3283 [ initializations = ForInit() ] <SEMICOLON>
3284 [ condition = Expression() ] <SEMICOLON>
3285 [ increments = StatementExpressionList() ] <RPAREN>
3287 action = Statement()
3288 {return new ForStatement(initializations,
3295 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3296 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3299 setMarker(fileToParse,
3300 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3304 "Line " + token.beginLine);
3305 } catch (CoreException e) {
3306 PHPeclipsePlugin.log(e);
3310 tokenEndFor = <ENDFOR>
3311 {pos = tokenEndFor.sourceEnd+1;}
3312 } catch (ParseException e) {
3313 errorMessage = "'endfor' expected";
3317 processParseExceptionDebug(e);
3320 token2 = <SEMICOLON>
3321 {pos = token2.sourceEnd+1;}
3322 } catch (ParseException e) {
3323 errorMessage = "';' expected after 'endfor' keyword";
3327 processParseExceptionDebug(e);
3330 final Statement[] stmtsArray = new Statement[list.size()];
3331 list.toArray(stmtsArray);
3332 return new ForStatement(initializations,
3335 new Block(stmtsArray,
3336 stmtsArray[0].sourceStart,
3337 stmtsArray[stmtsArray.length-1].sourceEnd),
3343 Expression[] ForInit() :
3345 final Expression[] exprs;
3348 LOOKAHEAD(LocalVariableDeclaration())
3349 exprs = LocalVariableDeclaration()
3352 exprs = StatementExpressionList()
3356 Expression[] StatementExpressionList() :
3358 final ArrayList list = new ArrayList();
3359 final Expression expr;
3362 expr = Expression() {list.add(expr);}
3363 (<COMMA> Expression() {list.add(expr);})*
3365 final Expression[] exprsArray = new Expression[list.size()];
3366 list.toArray(exprsArray);
3371 Continue ContinueStatement() :
3373 Expression expr = null;
3375 Token token2 = null;
3378 token = <CONTINUE> [ expr = Expression() ]
3380 token2 = <SEMICOLON>
3381 } catch (ParseException e) {
3382 errorMessage = "';' expected after 'continue' statement";
3385 errorStart = token.sourceEnd+1;
3386 errorEnd = token.sourceEnd+1;
3388 errorStart = expr.sourceEnd+1;
3389 errorEnd = expr.sourceEnd+1;
3391 processParseExceptionDebug(e);
3394 if (token2 == null) {
3396 return new Continue(expr,token.sourceStart,token.sourceEnd);
3398 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3400 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3404 ReturnStatement ReturnStatement() :
3406 Expression expr = null;
3408 Token token2 = null;
3411 token = <RETURN> [ expr = Expression() ]
3413 token2 = <SEMICOLON>
3414 } catch (ParseException e) {
3415 errorMessage = "';' expected after 'return' statement";
3418 errorStart = token.sourceEnd+1;
3419 errorEnd = token.sourceEnd+1;
3421 errorStart = expr.sourceEnd+1;
3422 errorEnd = expr.sourceEnd+1;
3424 processParseExceptionDebug(e);
3427 if (token2 == null) {
3429 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3431 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3433 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);