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 String html = SimpleCharStream.currentBuffer.substring(htmlStart, currentPosition);
339 pushOnAstNodes(new HTMLCode(html, htmlStart,currentPosition));
342 /** Create a new task. */
343 public static final void createNewTask(final int todoStart) {
344 final String todo = SimpleCharStream.currentBuffer.substring(todoStart,
345 SimpleCharStream.currentBuffer.indexOf("\n",
349 setMarker(fileToParse,
351 SimpleCharStream.getBeginLine(),
353 "Line "+SimpleCharStream.getBeginLine());
354 } catch (CoreException e) {
355 PHPeclipsePlugin.log(e);
360 private static final void parse() throws ParseException {
365 PARSER_END(PHPParser)
369 // CommonTokenAction: use the begins/ends fields added to the Jack
370 // CharStream class to set corresponding fields in each Token (which was
371 // also extended with new fields). By default Jack doesn't supply absolute
372 // offsets, just line/column offsets
373 static void CommonTokenAction(Token t) {
374 t.sourceStart = input_stream.beginOffset;
375 t.sourceEnd = input_stream.endOffset;
376 } // CommonTokenAction
381 <PHPSTARTSHORT : "<?"> : PHPPARSING
382 | <PHPSTARTLONG : "<?php"> : PHPPARSING
383 | <PHPECHOSTART : "<?="> : PHPPARSING
386 <PHPPARSING, IN_SINGLE_LINE_COMMENT,IN_VARIABLE> TOKEN :
388 <PHPEND :"?>"> : DEFAULT
391 /* Skip any character if we are not in php mode */
408 <IN_VARIABLE> SPECIAL_TOKEN :
417 <PHPPARSING> SPECIAL_TOKEN :
419 "//" : IN_SINGLE_LINE_COMMENT
420 | "#" : IN_SINGLE_LINE_COMMENT
421 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
422 | "/*" : IN_MULTI_LINE_COMMENT
425 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
427 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
431 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
439 todoToken = "TODO" {createNewTask(todoToken.sourceStart);}
441 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
446 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
451 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
461 | <FUNCTION : "function">
464 | <ELSEIF : "elseif">
471 /* LANGUAGE CONSTRUCT */
476 | <INCLUDE : "include">
477 | <REQUIRE : "require">
478 | <INCLUDE_ONCE : "include_once">
479 | <REQUIRE_ONCE : "require_once">
480 | <GLOBAL : "global">
481 | <DEFINE : "define">
482 | <STATIC : "static">
485 <PHPPARSING,IN_VARIABLE> TOKEN :
487 <CLASSACCESS : "->"> : PHPPARSING
488 | <STATICCLASSACCESS : "::"> : PHPPARSING
489 | <ARRAYASSIGN : "=>"> : PHPPARSING
492 /* RESERVED WORDS AND LITERALS */
498 | <CONTINUE : "continue">
499 | <_DEFAULT : "default">
501 | <EXTENDS : "extends">
506 | <RETURN : "return">
508 | <SWITCH : "switch">
513 | <ENDWHILE : "endwhile">
514 | <ENDSWITCH: "endswitch">
516 | <ENDFOR : "endfor">
517 | <FOREACH : "foreach">
525 | <OBJECT : "object">
527 | <BOOLEAN : "boolean">
529 | <DOUBLE : "double">
532 | <INTEGER : "integer">
536 <PHPPARSING,IN_VARIABLE> TOKEN :
538 <AT : "@"> : PHPPARSING
539 | <BANG : "!"> : PHPPARSING
540 | <TILDE : "~"> : PHPPARSING
541 | <HOOK : "?"> : PHPPARSING
542 | <COLON : ":"> : PHPPARSING
546 <PHPPARSING,IN_VARIABLE> TOKEN :
548 <OR_OR : "||"> : PHPPARSING
549 | <AND_AND : "&&"> : PHPPARSING
550 | <PLUS_PLUS : "++"> : PHPPARSING
551 | <MINUS_MINUS : "--"> : PHPPARSING
552 | <PLUS : "+"> : PHPPARSING
553 | <MINUS : "-"> : PHPPARSING
554 | <STAR : "*"> : PHPPARSING
555 | <SLASH : "/"> : PHPPARSING
556 | <BIT_AND : "&"> : PHPPARSING
557 | <BIT_OR : "|"> : PHPPARSING
558 | <XOR : "^"> : PHPPARSING
559 | <REMAINDER : "%"> : PHPPARSING
560 | <LSHIFT : "<<"> : PHPPARSING
561 | <RSIGNEDSHIFT : ">>"> : PHPPARSING
562 | <RUNSIGNEDSHIFT : ">>>"> : PHPPARSING
563 | <_ORL : "OR"> : PHPPARSING
564 | <_ANDL : "AND"> : PHPPARSING
571 <DECIMAL_LITERAL> (["l","L"])?
572 | <HEX_LITERAL> (["l","L"])?
573 | <OCTAL_LITERAL> (["l","L"])?
576 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
578 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
580 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
582 <FLOATING_POINT_LITERAL:
583 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
584 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
585 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
586 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
589 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
591 <STRING_LITERAL: (<STRING_2> | <STRING_3>)>
592 //| <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
593 | <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
594 | <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
597 <IN_STRING,DOLLAR_IN_STRING> SKIP :
599 <ESCAPED : ("\\" ~[])> : IN_STRING
604 <DOUBLEQUOTE : "\""> : IN_STRING
610 <DOLLARS : "$"> : DOLLAR_IN_STRING
613 <IN_STRING,DOLLAR_IN_STRING> TOKEN :
615 <DOUBLEQUOTE2 : "\""> : PHPPARSING
618 <DOLLAR_IN_STRING> TOKEN :
620 <LBRACE1 : "{"> : DOLLAR_IN_STRING_EXPR
623 <IN_STRING> SPECIAL_TOKEN :
628 <SKIPSTRING> SPECIAL_TOKEN :
638 <DOLLAR_IN_STRING_EXPR> TOKEN :
640 <RBRACE1 : "}"> : DOLLAR_IN_STRING
643 <DOLLAR_IN_STRING_EXPR> TOKEN :
653 <DOLLAR_IN_STRING_EXPR,IN_STRING> SKIP :
660 <PHPPARSING,IN_VARIABLE> TOKEN : {<DOLLAR : "$"> : IN_VARIABLE}
663 <PHPPARSING, IN_VARIABLE, DOLLAR_IN_STRING> TOKEN :
665 <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
668 ["a"-"z"] | ["A"-"Z"]
676 "_" | ["\u007f"-"\u00ff"]
680 <DOLLAR_IN_STRING> SPECIAL_TOKEN :
686 <PHPPARSING,IN_VARIABLE> TOKEN :
688 <LPAREN : "("> : PHPPARSING
689 | <RPAREN : ")"> : PHPPARSING
690 | <LBRACE : "{"> : PHPPARSING
691 | <RBRACE : "}"> : PHPPARSING
692 | <LBRACKET : "["> : PHPPARSING
693 | <RBRACKET : "]"> : PHPPARSING
694 | <SEMICOLON : ";"> : PHPPARSING
695 | <COMMA : ","> : PHPPARSING
696 | <DOT : "."> : PHPPARSING
701 <PHPPARSING,IN_VARIABLE> TOKEN :
703 <GT : ">"> : PHPPARSING
704 | <LT : "<"> : PHPPARSING
705 | <EQUAL_EQUAL : "=="> : PHPPARSING
706 | <LE : "<="> : PHPPARSING
707 | <GE : ">="> : PHPPARSING
708 | <NOT_EQUAL : "!="> : PHPPARSING
709 | <DIF : "<>"> : PHPPARSING
710 | <BANGDOUBLEEQUAL : "!=="> : PHPPARSING
711 | <TRIPLEEQUAL : "==="> : PHPPARSING
715 <PHPPARSING,IN_VARIABLE> TOKEN :
717 <ASSIGN : "="> : PHPPARSING
718 | <PLUSASSIGN : "+="> : PHPPARSING
719 | <MINUSASSIGN : "-="> : PHPPARSING
720 | <STARASSIGN : "*="> : PHPPARSING
721 | <SLASHASSIGN : "/="> : PHPPARSING
722 | <ANDASSIGN : "&="> : PHPPARSING
723 | <ORASSIGN : "|="> : PHPPARSING
724 | <XORASSIGN : "^="> : PHPPARSING
725 | <DOTASSIGN : ".="> : PHPPARSING
726 | <REMASSIGN : "%="> : PHPPARSING
727 | <TILDEEQUAL : "~="> : PHPPARSING
728 | <LSHIFTASSIGN : "<<="> : PHPPARSING
729 | <RSIGNEDSHIFTASSIGN : ">>="> : PHPPARSING
744 {PHPParser.createNewHTMLCode();}
745 } catch (TokenMgrError e) {
746 PHPeclipsePlugin.log(e);
747 errorStart = SimpleCharStream.beginOffset;
748 errorEnd = SimpleCharStream.endOffset;
749 errorMessage = e.getMessage();
751 throw generateParseException();
756 * A php block is a <?= expression [;]?>
757 * or <?php somephpcode ?>
758 * or <? somephpcode ?>
762 final PHPEchoBlock phpEchoBlock;
763 final Token token,phpEnd;
766 phpEchoBlock = phpEchoBlock()
767 {pushOnAstNodes(phpEchoBlock);}
770 | token = <PHPSTARTSHORT>
772 setMarker(fileToParse,
773 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
777 "Line " + token.beginLine);
778 } catch (CoreException e) {
779 PHPeclipsePlugin.log(e);
782 {PHPParser.createNewHTMLCode();}
786 {htmlStart = phpEnd.sourceEnd;}
787 } catch (ParseException e) {
788 errorMessage = "'?>' expected";
790 errorStart = e.currentToken.sourceStart;
791 errorEnd = e.currentToken.sourceEnd;
792 processParseExceptionDebug(e);
796 PHPEchoBlock phpEchoBlock() :
798 final Expression expr;
799 final PHPEchoBlock echoBlock;
800 final Token token, token2;
803 token = <PHPECHOSTART> {PHPParser.createNewHTMLCode();}
804 expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
806 htmlStart = token2.sourceEnd;
808 echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
809 pushOnAstNodes(echoBlock);
819 ClassDeclaration ClassDeclaration() :
821 final ClassDeclaration classDeclaration;
822 Token className = null;
823 final Token superclassName, token, extendsToken;
824 String classNameImage = SYNTAX_ERROR_CHAR;
825 String superclassNameImage = null;
831 className = <IDENTIFIER>
832 {classNameImage = className.image;}
833 } catch (ParseException e) {
834 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
836 errorStart = token.sourceEnd+1;
837 errorEnd = token.sourceEnd+1;
838 processParseExceptionDebug(e);
841 extendsToken = <EXTENDS>
843 superclassName = <IDENTIFIER>
844 {superclassNameImage = superclassName.image;}
845 } catch (ParseException e) {
846 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
848 errorStart = extendsToken.sourceEnd+1;
849 errorEnd = extendsToken.sourceEnd+1;
850 processParseExceptionDebug(e);
851 superclassNameImage = SYNTAX_ERROR_CHAR;
856 if (className == null) {
857 start = token.sourceStart;
858 end = token.sourceEnd;
860 start = className.sourceStart;
861 end = className.sourceEnd;
863 if (superclassNameImage == null) {
865 classDeclaration = new ClassDeclaration(currentSegment,
870 classDeclaration = new ClassDeclaration(currentSegment,
876 currentSegment.add(classDeclaration);
877 currentSegment = classDeclaration;
879 classEnd = ClassBody(classDeclaration)
880 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
881 classDeclaration.sourceEnd = classEnd;
882 pushOnAstNodes(classDeclaration);
883 return classDeclaration;}
886 int ClassBody(final ClassDeclaration classDeclaration) :
893 } catch (ParseException e) {
894 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
896 errorStart = e.currentToken.sourceStart;
897 errorEnd = e.currentToken.sourceEnd;
898 processParseExceptionDebug(e);
900 ( ClassBodyDeclaration(classDeclaration) )*
903 {return token.sourceEnd;}
904 } catch (ParseException e) {
905 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
907 errorStart = e.currentToken.sourceStart;
908 errorEnd = e.currentToken.sourceEnd;
909 processParseExceptionDebug(e);
910 return PHPParser.token.sourceEnd;
915 * A class can contain only methods and fields.
917 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
919 final MethodDeclaration method;
920 final FieldDeclaration field;
923 method = MethodDeclaration() {method.analyzeCode();
924 classDeclaration.addMethod(method);}
925 | field = FieldDeclaration() {classDeclaration.addField(field);}
929 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
930 * it is only used by ClassBodyDeclaration()
932 FieldDeclaration FieldDeclaration() :
934 VariableDeclaration variableDeclaration;
935 final VariableDeclaration[] list;
936 final ArrayList arrayList = new ArrayList();
942 token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
944 arrayList.add(variableDeclaration);
945 pos = variableDeclaration.sourceEnd;
948 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
950 arrayList.add(variableDeclaration);
951 outlineInfo.addVariable(variableDeclaration.name());
952 pos = variableDeclaration.sourceEnd;
957 } catch (ParseException e) {
958 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
962 processParseExceptionDebug(e);
965 {list = new VariableDeclaration[arrayList.size()];
966 arrayList.toArray(list);
968 if (token2 == null) {
969 end = list[list.length-1].sourceEnd;
971 end = token2.sourceEnd;
973 return new FieldDeclaration(list,
980 * a strict variable declarator : there cannot be a suffix here.
981 * It will be used by fields and formal parameters
983 VariableDeclaration VariableDeclaratorNoSuffix() :
985 final Token token, lbrace,rbrace;
986 Expression expr, initializer = null;
994 {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
996 lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
997 {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
1000 assignToken = <ASSIGN>
1002 initializer = VariableInitializer()
1003 } catch (ParseException e) {
1004 errorMessage = "Literal expression expected in variable initializer";
1006 errorStart = assignToken.sourceEnd +1;
1007 errorEnd = assignToken.sourceEnd +1;
1008 processParseExceptionDebug(e);
1012 if (initializer == null) {
1013 return new VariableDeclaration(currentSegment,
1015 variable.sourceStart,
1016 variable.sourceEnd);
1018 return new VariableDeclaration(currentSegment,
1021 VariableDeclaration.EQUAL,
1022 variable.sourceStart);
1027 * this will be used by static statement
1029 VariableDeclaration VariableDeclarator() :
1031 final AbstractVariable variable;
1032 Expression initializer = null;
1036 variable = VariableDeclaratorId()
1040 initializer = VariableInitializer()
1041 } catch (ParseException e) {
1042 errorMessage = "Literal expression expected in variable initializer";
1044 errorStart = token.sourceEnd+1;
1045 errorEnd = token.sourceEnd+1;
1046 processParseExceptionDebug(e);
1050 if (initializer == null) {
1051 return new VariableDeclaration(currentSegment,
1053 variable.sourceStart,
1054 variable.sourceEnd);
1056 return new VariableDeclaration(currentSegment,
1059 VariableDeclaration.EQUAL,
1060 variable.sourceStart);
1066 * @return the variable name (with suffix)
1068 AbstractVariable VariableDeclaratorId() :
1071 AbstractVariable expression = null;
1078 expression = VariableSuffix(var)
1081 if (expression == null) {
1086 } catch (ParseException e) {
1087 errorMessage = "'$' expected for variable identifier";
1089 errorStart = e.currentToken.sourceStart;
1090 errorEnd = e.currentToken.sourceEnd;
1095 Variable Variable() :
1097 Variable variable = null;
1101 token = <DOLLAR> variable = Var()
1109 Variable variable = null;
1110 final Token token,token2;
1111 ConstantIdentifier constant;
1112 Expression expression;
1115 token = <DOLLAR> variable = Var()
1116 {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1118 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1120 return new Variable(expression,
1125 token = <IDENTIFIER>
1127 outlineInfo.addVariable('$' + token.image);
1128 return new Variable(token.image,token.sourceStart,token.sourceEnd);
1132 Expression VariableInitializer() :
1134 final Expression expr;
1135 final Token token, token2;
1141 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1142 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1144 token2.sourceStart);}
1146 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1147 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1149 token2.sourceStart);}
1151 expr = ArrayDeclarator()
1154 token = <IDENTIFIER>
1155 {return new ConstantIdentifier(token);}
1158 ArrayVariableDeclaration ArrayVariable() :
1160 final Expression expr,expr2;
1165 <ARRAYASSIGN> expr2 = Expression()
1166 {return new ArrayVariableDeclaration(expr,expr2);}
1168 {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1171 ArrayVariableDeclaration[] ArrayInitializer() :
1173 ArrayVariableDeclaration expr;
1174 final ArrayList list = new ArrayList();
1179 expr = ArrayVariable()
1181 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1186 <COMMA> {list.add(null);}
1190 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1196 * A Method Declaration.
1197 * <b>function</b> MetodDeclarator() Block()
1199 MethodDeclaration MethodDeclaration() :
1201 final MethodDeclaration functionDeclaration;
1203 final OutlineableWithChildren seg = currentSegment;
1209 functionDeclaration = MethodDeclarator(token.sourceStart)
1210 {outlineInfo.addVariable(functionDeclaration.name);}
1211 } catch (ParseException e) {
1212 if (errorMessage != null) throw e;
1213 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1215 errorStart = e.currentToken.sourceStart;
1216 errorEnd = e.currentToken.sourceEnd;
1219 {currentSegment = functionDeclaration;}
1221 {functionDeclaration.statements = block.statements;
1222 currentSegment = seg;
1223 return functionDeclaration;}
1227 * A MethodDeclarator.
1228 * [&] IDENTIFIER(parameters ...).
1229 * @return a function description for the outline
1231 MethodDeclaration MethodDeclarator(final int start) :
1233 Token identifier = null;
1234 Token reference = null;
1235 final ArrayList formalParameters = new ArrayList();
1236 String identifierChar = SYNTAX_ERROR_CHAR;
1240 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1242 identifier = <IDENTIFIER>
1244 identifierChar = identifier.image;
1245 end = identifier.sourceEnd;
1247 } catch (ParseException e) {
1248 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1250 errorStart = e.currentToken.sourceEnd;
1251 errorEnd = e.currentToken.next.sourceStart;
1252 processParseExceptionDebug(e);
1254 end = FormalParameters(formalParameters)
1256 int nameStart, nameEnd;
1257 if (identifier == null) {
1258 if (reference == null) {
1259 nameStart = start + 9;
1260 nameEnd = start + 10;
1262 nameStart = reference.sourceEnd + 1;
1263 nameEnd = reference.sourceEnd + 2;
1266 nameStart = identifier.sourceStart;
1267 nameEnd = identifier.sourceEnd;
1269 return new MethodDeclaration(currentSegment,
1281 * FormalParameters follows method identifier.
1282 * (FormalParameter())
1284 int FormalParameters(final ArrayList parameters) :
1286 VariableDeclaration var;
1288 Token tok = PHPParser.token;
1289 int end = tok.sourceEnd;
1294 {end = tok.sourceEnd;}
1295 } catch (ParseException e) {
1296 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1298 errorStart = e.currentToken.next.sourceStart;
1299 errorEnd = e.currentToken.next.sourceEnd;
1300 processParseExceptionDebug(e);
1303 var = FormalParameter()
1304 {parameters.add(var);end = var.sourceEnd;}
1306 <COMMA> var = FormalParameter()
1307 {parameters.add(var);end = var.sourceEnd;}
1312 {end = token.sourceEnd;}
1313 } catch (ParseException e) {
1314 errorMessage = "')' expected";
1316 errorStart = e.currentToken.next.sourceStart;
1317 errorEnd = e.currentToken.next.sourceEnd;
1318 processParseExceptionDebug(e);
1324 * A formal parameter.
1325 * $varname[=value] (,$varname[=value])
1327 VariableDeclaration FormalParameter() :
1329 final VariableDeclaration variableDeclaration;
1333 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1335 outlineInfo.addVariable('$'+variableDeclaration.name());
1336 if (token != null) {
1337 variableDeclaration.setReference(true);
1339 return variableDeclaration;}
1342 ConstantIdentifier Type() :
1343 {final Token token;}
1345 token = <STRING> {return new ConstantIdentifier(token);}
1346 | token = <BOOL> {return new ConstantIdentifier(token);}
1347 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1348 | token = <REAL> {return new ConstantIdentifier(token);}
1349 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1350 | token = <FLOAT> {return new ConstantIdentifier(token);}
1351 | token = <INT> {return new ConstantIdentifier(token);}
1352 | token = <INTEGER> {return new ConstantIdentifier(token);}
1353 | token = <OBJECT> {return new ConstantIdentifier(token);}
1356 Expression Expression() :
1358 final Expression expr;
1359 Expression initializer = null;
1360 int assignOperator = -1;
1364 expr = ConditionalExpression()
1366 assignOperator = AssignmentOperator()
1368 initializer = Expression()
1369 } catch (ParseException e) {
1370 if (errorMessage != null) {
1373 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1375 errorEnd = SimpleCharStream.getPosition();
1380 if (assignOperator != -1) {// todo : change this, very very bad :(
1381 if (expr instanceof AbstractVariable) {
1382 return new VariableDeclaration(currentSegment,
1383 (AbstractVariable) expr,
1386 initializer.sourceEnd);
1388 String varName = expr.toStringExpression().substring(1);
1389 return new VariableDeclaration(currentSegment,
1390 new Variable(varName,
1394 initializer.sourceEnd);
1398 | expr = ExpressionWBang() {return expr;}
1401 Expression ExpressionWBang() :
1403 final Expression expr;
1407 token = <BANG> expr = ExpressionWBang()
1408 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1409 | expr = ExpressionNoBang() {return expr;}
1412 Expression ExpressionNoBang() :
1417 expr = ListExpression() {return expr;}
1419 expr = PrintExpression() {return expr;}
1423 * Any assignement operator.
1424 * @return the assignement operator id
1426 int AssignmentOperator() :
1429 <ASSIGN> {return VariableDeclaration.EQUAL;}
1430 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1431 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1432 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1433 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1434 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1435 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1436 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1437 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1438 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1439 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1440 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1441 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1444 Expression ConditionalExpression() :
1446 final Expression expr;
1447 Expression expr2 = null;
1448 Expression expr3 = null;
1451 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1453 if (expr3 == null) {
1456 return new ConditionalExpression(expr,expr2,expr3);
1460 Expression ConditionalOrExpression() :
1462 Expression expr,expr2;
1466 expr = ConditionalAndExpression()
1469 <OR_OR> {operator = OperatorIds.OR_OR;}
1470 | <_ORL> {operator = OperatorIds.ORL;}
1472 expr2 = ConditionalAndExpression()
1474 expr = new BinaryExpression(expr,expr2,operator);
1480 Expression ConditionalAndExpression() :
1482 Expression expr,expr2;
1486 expr = ConcatExpression()
1488 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1489 | <_ANDL> {operator = OperatorIds.ANDL;})
1490 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1495 Expression ConcatExpression() :
1497 Expression expr,expr2;
1500 expr = InclusiveOrExpression()
1502 <DOT> expr2 = InclusiveOrExpression()
1503 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1508 Expression InclusiveOrExpression() :
1510 Expression expr,expr2;
1513 expr = ExclusiveOrExpression()
1514 (<BIT_OR> expr2 = ExclusiveOrExpression()
1515 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1520 Expression ExclusiveOrExpression() :
1522 Expression expr,expr2;
1525 expr = AndExpression()
1527 <XOR> expr2 = AndExpression()
1528 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1533 Expression AndExpression() :
1535 Expression expr,expr2;
1538 expr = EqualityExpression()
1541 <BIT_AND> expr2 = EqualityExpression()
1542 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1547 Expression EqualityExpression() :
1549 Expression expr,expr2;
1554 expr = RelationalExpression()
1556 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1557 | token = <DIF> {operator = OperatorIds.DIF;}
1558 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1559 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1560 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1563 expr2 = RelationalExpression()
1564 } catch (ParseException e) {
1565 if (errorMessage != null) {
1568 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1570 errorStart = token.sourceEnd +1;
1571 errorEnd = token.sourceEnd +1;
1572 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1573 processParseExceptionDebug(e);
1576 expr = new BinaryExpression(expr,expr2,operator);
1582 Expression RelationalExpression() :
1584 Expression expr,expr2;
1588 expr = ShiftExpression()
1590 ( <LT> {operator = OperatorIds.LESS;}
1591 | <GT> {operator = OperatorIds.GREATER;}
1592 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1593 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1594 expr2 = ShiftExpression()
1595 {expr = new BinaryExpression(expr,expr2,operator);}
1600 Expression ShiftExpression() :
1602 Expression expr,expr2;
1606 expr = AdditiveExpression()
1608 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1609 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1610 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1611 expr2 = AdditiveExpression()
1612 {expr = new BinaryExpression(expr,expr2,operator);}
1617 Expression AdditiveExpression() :
1619 Expression expr,expr2;
1623 expr = MultiplicativeExpression()
1626 ( <PLUS> {operator = OperatorIds.PLUS;}
1627 | <MINUS> {operator = OperatorIds.MINUS;}
1629 expr2 = MultiplicativeExpression()
1630 {expr = new BinaryExpression(expr,expr2,operator);}
1635 Expression MultiplicativeExpression() :
1637 Expression expr,expr2;
1642 expr = UnaryExpression()
1643 } catch (ParseException e) {
1644 if (errorMessage != null) throw e;
1645 errorMessage = "unexpected token '"+e.currentToken.next.image+'\'';
1647 errorStart = PHPParser.token.sourceStart;
1648 errorEnd = PHPParser.token.sourceEnd;
1652 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1653 | <SLASH> {operator = OperatorIds.DIVIDE;}
1654 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1655 expr2 = UnaryExpression()
1656 {expr = new BinaryExpression(expr,expr2,operator);}
1662 * An unary expression starting with @, & or nothing
1664 Expression UnaryExpression() :
1666 final Expression expr;
1669 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1670 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1672 expr = AtNotTildeUnaryExpression() {return expr;}
1675 Expression AtNotTildeUnaryExpression() :
1677 final Expression expr;
1682 expr = AtNotTildeUnaryExpression()
1683 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1686 expr = AtNotTildeUnaryExpression()
1687 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1690 expr = AtNotUnaryExpression()
1691 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1693 expr = UnaryExpressionNoPrefix()
1698 * An expression prefixed (or not) by one or more @ and !.
1699 * @return the expression
1701 Expression AtNotUnaryExpression() :
1703 final Expression expr;
1708 expr = AtNotUnaryExpression()
1709 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1712 expr = AtNotUnaryExpression()
1713 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1715 expr = UnaryExpressionNoPrefix()
1719 Expression UnaryExpressionNoPrefix() :
1721 final Expression expr;
1725 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1727 token.sourceStart);}
1729 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1731 token.sourceStart);}
1733 expr = PreIncDecExpression()
1736 expr = UnaryExpressionNotPlusMinus()
1741 Expression PreIncDecExpression() :
1743 final Expression expr;
1749 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1751 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1753 expr = PrimaryExpression()
1754 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1757 Expression UnaryExpressionNotPlusMinus() :
1759 final Expression expr;
1762 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1763 expr = CastExpression() {return expr;}
1764 | expr = PostfixExpression() {return expr;}
1765 | expr = Literal() {return expr;}
1766 | <LPAREN> expr = Expression()
1769 } catch (ParseException e) {
1770 errorMessage = "')' expected";
1772 errorStart = expr.sourceEnd +1;
1773 errorEnd = expr.sourceEnd +1;
1774 processParseExceptionDebug(e);
1779 CastExpression CastExpression() :
1781 final ConstantIdentifier type;
1782 final Expression expr;
1783 final Token token,token1;
1790 token = <ARRAY> {type = new ConstantIdentifier(token);}
1792 <RPAREN> expr = UnaryExpression()
1793 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1796 Expression PostfixExpression() :
1798 final Expression expr;
1803 expr = PrimaryExpression()
1805 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1807 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1810 if (operator == -1) {
1813 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1817 Expression PrimaryExpression() :
1823 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1826 expr = ArrayDeclarator()
1830 Expression refPrimaryExpression(final Token reference) :
1833 Expression expr2 = null;
1834 final Token identifier;
1837 identifier = <IDENTIFIER>
1839 expr = new ConstantIdentifier(identifier);
1842 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1843 {expr = new ClassAccess(expr,
1845 ClassAccess.STATIC);}
1847 [ expr2 = Arguments(expr) ]
1849 if (expr2 == null) {
1850 if (reference != null) {
1851 ParseException e = generateParseException();
1852 errorMessage = "you cannot use a constant by reference";
1854 errorStart = reference.sourceStart;
1855 errorEnd = reference.sourceEnd;
1856 processParseExceptionDebug(e);
1863 expr = VariableDeclaratorId() //todo use the reference parameter ...
1864 [ expr = Arguments(expr) ]
1868 expr = ClassIdentifier()
1871 if (reference == null) {
1872 start = token.sourceStart;
1874 start = reference.sourceStart;
1876 expr = new ClassInstantiation(expr,
1880 [ expr = Arguments(expr) ]
1885 * An array declarator.
1889 ArrayInitializer ArrayDeclarator() :
1891 final ArrayVariableDeclaration[] vars;
1895 token = <ARRAY> vars = ArrayInitializer()
1896 {return new ArrayInitializer(vars,
1898 PHPParser.token.sourceEnd);}
1901 Expression ClassIdentifier():
1903 final Expression expr;
1907 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1908 | expr = Type() {return expr;}
1909 | expr = VariableDeclaratorId() {return expr;}
1913 * Used by Variabledeclaratorid and primarysuffix
1915 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1917 Expression expression = null;
1918 final Token classAccessToken,lbrace,rbrace;
1923 classAccessToken = <CLASSACCESS>
1926 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1928 expression = new Variable(expression,
1933 token = <IDENTIFIER>
1934 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1936 expression = Variable()
1938 } catch (ParseException e) {
1939 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1941 errorStart = classAccessToken.sourceEnd +1;
1942 errorEnd = classAccessToken.sourceEnd +1;
1943 processParseExceptionDebug(e);
1945 {return new ClassAccess(prefix,
1947 ClassAccess.NORMAL);}
1949 token = <LBRACKET> {pos = token.sourceEnd+1;}
1950 [ expression = Expression() {pos = expression.sourceEnd+1;}
1951 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1954 {pos = token.sourceEnd;}
1955 } catch (ParseException e) {
1956 errorMessage = "']' expected";
1960 processParseExceptionDebug(e);
1962 {return new ArrayDeclarator(prefix,expression,pos);}
1964 token = <LBRACE> {pos = token.sourceEnd+1;}
1965 [ expression = Expression() {pos = expression.sourceEnd+1;}
1966 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1969 {pos = token.sourceEnd;}
1970 } catch (ParseException e) {
1971 errorMessage = "']' expected";
1975 processParseExceptionDebug(e);
1977 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1983 StringLiteral literal;
1986 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1987 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1988 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1989 | token = <TRUE> {return new TrueLiteral(token);}
1990 | token = <FALSE> {return new FalseLiteral(token);}
1991 | token = <NULL> {return new NullLiteral(token);}
1992 | literal = evaluableString() {return literal;}
1995 StringLiteral evaluableString() :
1997 ArrayList list = new ArrayList();
1999 Token token,lbrace,rbrace;
2000 AbstractVariable var;
2004 start = <DOUBLEQUOTE>
2008 token = <IDENTIFIER> {list.add(new Variable(token.image,
2014 {list.add(new Variable(token.image,
2020 end = <DOUBLEQUOTE2>
2022 AbstractVariable[] vars = new AbstractVariable[list.size()];
2024 return new StringLiteral(SimpleCharStream.currentBuffer.substring(start.sourceEnd,end.sourceStart),
2031 FunctionCall Arguments(final Expression func) :
2033 Expression[] args = null;
2034 final Token token,lparen;
2037 lparen = <LPAREN> [ args = ArgumentList() ]
2040 {return new FunctionCall(func,args,token.sourceEnd);}
2041 } catch (ParseException e) {
2042 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2045 errorStart = lparen.sourceEnd+1;
2046 errorEnd = lparen.sourceEnd+2;
2048 errorStart = args[args.length-1].sourceEnd+1;
2049 errorEnd = args[args.length-1].sourceEnd+2;
2051 processParseExceptionDebug(e);
2054 int sourceEnd = (args == null && args.length != 0) ? lparen.sourceEnd+1 : args[args.length-1].sourceEnd;
2055 return new FunctionCall(func,args,sourceEnd);}
2059 * An argument list is a list of arguments separated by comma :
2060 * argumentDeclaration() (, argumentDeclaration)*
2061 * @return an array of arguments
2063 Expression[] ArgumentList() :
2066 final ArrayList list = new ArrayList();
2072 {list.add(arg);pos = arg.sourceEnd;}
2073 ( token = <COMMA> {pos = token.sourceEnd;}
2077 pos = arg.sourceEnd;}
2078 } catch (ParseException e) {
2079 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2083 processParseException(e);
2087 final Expression[] arguments = new Expression[list.size()];
2088 list.toArray(arguments);
2093 * A Statement without break.
2094 * @return a statement
2096 Statement StatementNoBreak() :
2098 final Statement statement;
2103 statement = expressionStatement() {return statement;}
2105 statement = LabeledStatement() {return statement;}
2106 | statement = Block() {return statement;}
2107 | statement = EmptyStatement() {return statement;}
2108 | statement = SwitchStatement() {return statement;}
2109 | statement = IfStatement() {return statement;}
2110 | statement = WhileStatement() {return statement;}
2111 | statement = DoStatement() {return statement;}
2112 | statement = ForStatement() {return statement;}
2113 | statement = ForeachStatement() {return statement;}
2114 | statement = ContinueStatement() {return statement;}
2115 | statement = ReturnStatement() {return statement;}
2116 | statement = EchoStatement() {return statement;}
2117 | [token=<AT>] statement = IncludeStatement()
2118 {if (token != null) {
2119 ((InclusionStatement)statement).silent = true;
2120 statement.sourceStart = token.sourceStart;
2123 | statement = StaticStatement() {return statement;}
2124 | statement = GlobalStatement() {return statement;}
2125 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2129 * A statement expression.
2131 * @return an expression
2133 Statement expressionStatement() :
2135 final Statement statement;
2139 statement = Expression()
2142 {statement.sourceEnd = token.sourceEnd;}
2143 } catch (ParseException e) {
2144 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2145 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2147 errorStart = statement.sourceEnd+1;
2148 errorEnd = statement.sourceEnd+1;
2149 processParseExceptionDebug(e);
2155 Define defineStatement() :
2157 Expression defineName,defineValue;
2158 final Token defineToken;
2163 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2166 {pos = token.sourceEnd+1;}
2167 } catch (ParseException e) {
2168 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2172 processParseExceptionDebug(e);
2175 defineName = Expression()
2176 {pos = defineName.sourceEnd+1;}
2177 } catch (ParseException e) {
2178 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2182 processParseExceptionDebug(e);
2183 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2187 {pos = defineName.sourceEnd+1;}
2188 } catch (ParseException e) {
2189 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2193 processParseExceptionDebug(e);
2196 defineValue = Expression()
2197 {pos = defineValue.sourceEnd+1;}
2198 } catch (ParseException e) {
2199 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2203 processParseExceptionDebug(e);
2204 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2208 {pos = token.sourceEnd+1;}
2209 } catch (ParseException e) {
2210 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2214 processParseExceptionDebug(e);
2216 {return new Define(currentSegment,
2219 defineToken.sourceStart,
2224 * A Normal statement.
2226 Statement Statement() :
2228 final Statement statement;
2231 statement = StatementNoBreak() {return statement;}
2232 | statement = BreakStatement() {return statement;}
2236 * An html block inside a php syntax.
2238 HTMLBlock htmlBlock() :
2240 final int startIndex = nodePtr;
2241 final AstNode[] blockNodes;
2247 {htmlStart = phpEnd.sourceEnd;}
2250 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2251 {PHPParser.createNewHTMLCode();}
2252 } catch (ParseException e) {
2253 errorMessage = "unexpected end of file , '<?php' expected";
2255 errorStart = e.currentToken.sourceStart;
2256 errorEnd = e.currentToken.sourceEnd;
2260 nbNodes = nodePtr - startIndex;
2264 blockNodes = new AstNode[nbNodes];
2265 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2266 nodePtr = startIndex;
2267 return new HTMLBlock(blockNodes);}
2271 * An include statement. It's "include" an expression;
2273 InclusionStatement IncludeStatement() :
2277 final InclusionStatement inclusionStatement;
2278 final Token token, token2;
2282 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2283 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2284 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2285 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2288 {pos = expr.sourceEnd;}
2289 } catch (ParseException e) {
2290 if (errorMessage != null) {
2293 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2295 errorStart = e.currentToken.next.sourceStart;
2296 errorEnd = e.currentToken.next.sourceEnd;
2297 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2298 processParseExceptionDebug(e);
2301 token2 = <SEMICOLON>
2302 {pos=token2.sourceEnd;}
2303 } catch (ParseException e) {
2304 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2306 errorStart = e.currentToken.next.sourceStart;
2307 errorEnd = e.currentToken.next.sourceEnd;
2308 processParseExceptionDebug(e);
2311 inclusionStatement = new InclusionStatement(currentSegment,
2316 currentSegment.add(inclusionStatement);
2317 return inclusionStatement;
2321 PrintExpression PrintExpression() :
2323 final Expression expr;
2324 final Token printToken;
2327 token = <PRINT> expr = Expression()
2328 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2331 ListExpression ListExpression() :
2333 Expression expr = null;
2334 final Expression expression;
2335 final ArrayList list = new ArrayList();
2337 final Token listToken, rParen;
2341 listToken = <LIST> {pos = listToken.sourceEnd;}
2343 token = <LPAREN> {pos = token.sourceEnd;}
2344 } catch (ParseException e) {
2345 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2347 errorStart = listToken.sourceEnd+1;
2348 errorEnd = listToken.sourceEnd+1;
2349 processParseExceptionDebug(e);
2352 expr = VariableDeclaratorId()
2353 {list.add(expr);pos = expr.sourceEnd;}
2355 {if (expr == null) list.add(null);}
2359 {pos = token.sourceEnd;}
2360 } catch (ParseException e) {
2361 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2365 processParseExceptionDebug(e);
2367 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2371 {pos = rParen.sourceEnd;}
2372 } catch (ParseException e) {
2373 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2377 processParseExceptionDebug(e);
2379 [ <ASSIGN> expression = Expression()
2381 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2383 return new ListExpression(vars,
2385 listToken.sourceStart,
2386 expression.sourceEnd);}
2389 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2391 return new ListExpression(vars,listToken.sourceStart,pos);}
2395 * An echo statement.
2396 * echo anyexpression (, otherexpression)*
2398 EchoStatement EchoStatement() :
2400 final ArrayList expressions = new ArrayList();
2403 Token token2 = null;
2406 token = <ECHO> expr = Expression()
2407 {expressions.add(expr);}
2409 <COMMA> expr = Expression()
2410 {expressions.add(expr);}
2413 token2 = <SEMICOLON>
2414 } catch (ParseException e) {
2415 if (e.currentToken.next.kind != 4) {
2416 errorMessage = "';' expected after 'echo' statement";
2418 errorStart = e.currentToken.sourceEnd;
2419 errorEnd = e.currentToken.sourceEnd;
2420 processParseExceptionDebug(e);
2424 final Expression[] exprs = new Expression[expressions.size()];
2425 expressions.toArray(exprs);
2426 if (token2 == null) {
2427 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2429 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2433 GlobalStatement GlobalStatement() :
2436 final ArrayList vars = new ArrayList();
2437 final GlobalStatement global;
2438 final Token token, token2;
2444 {vars.add(expr);pos = expr.sourceEnd+1;}
2447 {vars.add(expr);pos = expr.sourceEnd+1;}
2450 token2 = <SEMICOLON>
2451 {pos = token2.sourceEnd+1;}
2452 } catch (ParseException e) {
2453 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2457 processParseExceptionDebug(e);
2460 final Variable[] variables = new Variable[vars.size()];
2461 vars.toArray(variables);
2462 global = new GlobalStatement(currentSegment,
2466 currentSegment.add(global);
2470 StaticStatement StaticStatement() :
2472 final ArrayList vars = new ArrayList();
2473 VariableDeclaration expr;
2474 final Token token, token2;
2478 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2480 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2483 token2 = <SEMICOLON>
2484 {pos = token2.sourceEnd+1;}
2485 } catch (ParseException e) {
2486 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2490 processParseException(e);
2493 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2494 vars.toArray(variables);
2495 return new StaticStatement(variables,
2500 LabeledStatement LabeledStatement() :
2503 final Statement statement;
2506 label = <IDENTIFIER> <COLON> statement = Statement()
2507 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2519 final ArrayList list = new ArrayList();
2520 Statement statement;
2521 final Token token, token2;
2527 {pos = token.sourceEnd+1;start=token.sourceStart;}
2528 } catch (ParseException e) {
2529 errorMessage = "'{' expected";
2531 pos = PHPParser.token.sourceEnd+1;
2535 processParseExceptionDebug(e);
2537 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2538 | statement = htmlBlock() {if (statement != null) {
2539 list.add(statement);
2540 pos = statement.sourceEnd+1;
2542 pos = PHPParser.token.sourceEnd+1;
2547 {pos = token2.sourceEnd+1;}
2548 } catch (ParseException e) {
2549 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2553 processParseExceptionDebug(e);
2556 final Statement[] statements = new Statement[list.size()];
2557 list.toArray(statements);
2558 return new Block(statements,start,pos);}
2561 Statement BlockStatement() :
2563 final Statement statement;
2567 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2569 } catch (ParseException e) {
2570 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2572 errorStart = e.currentToken.sourceStart;
2573 errorEnd = e.currentToken.sourceEnd;
2576 | statement = ClassDeclaration() {return statement;}
2577 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2578 currentSegment.add((MethodDeclaration) statement);
2579 ((MethodDeclaration) statement).analyzeCode();
2584 * A Block statement that will not contain any 'break'
2586 Statement BlockStatementNoBreak() :
2588 final Statement statement;
2591 statement = StatementNoBreak() {return statement;}
2592 | statement = ClassDeclaration() {return statement;}
2593 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2594 ((MethodDeclaration) statement).analyzeCode();
2599 * used only by ForInit()
2601 Expression[] LocalVariableDeclaration() :
2603 final ArrayList list = new ArrayList();
2609 ( <COMMA> var = Expression() {list.add(var);})*
2611 final Expression[] vars = new Expression[list.size()];
2618 * used only by LocalVariableDeclaration().
2620 VariableDeclaration LocalVariableDeclarator() :
2622 final Variable varName;
2623 Expression initializer = null;
2626 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2628 if (initializer == null) {
2629 return new VariableDeclaration(currentSegment,
2631 varName.sourceStart,
2634 return new VariableDeclaration(currentSegment,
2637 VariableDeclaration.EQUAL,
2638 varName.sourceStart);
2642 EmptyStatement EmptyStatement() :
2648 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2652 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2654 Expression StatementExpression() :
2656 final Expression expr;
2657 final Token operator;
2660 expr = PreIncDecExpression() {return expr;}
2662 expr = PrimaryExpression()
2663 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2664 OperatorIds.PLUS_PLUS,
2665 operator.sourceEnd);}
2666 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2667 OperatorIds.MINUS_MINUS,
2668 operator.sourceEnd);}
2673 SwitchStatement SwitchStatement() :
2675 Expression variable;
2676 final AbstractCase[] cases;
2677 final Token switchToken,lparenToken,rparenToken;
2681 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2683 lparenToken = <LPAREN>
2684 {pos = lparenToken.sourceEnd+1;}
2685 } catch (ParseException e) {
2686 errorMessage = "'(' expected after 'switch'";
2690 processParseExceptionDebug(e);
2693 variable = Expression() {pos = variable.sourceEnd+1;}
2694 } catch (ParseException e) {
2695 if (errorMessage != null) {
2698 errorMessage = "expression expected";
2702 processParseExceptionDebug(e);
2703 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2706 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2707 } catch (ParseException e) {
2708 errorMessage = "')' expected";
2712 processParseExceptionDebug(e);
2714 ( cases = switchStatementBrace()
2715 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2716 {return new SwitchStatement(variable,
2718 switchToken.sourceStart,
2719 PHPParser.token.sourceEnd);}
2722 AbstractCase[] switchStatementBrace() :
2725 final ArrayList cases = new ArrayList();
2730 token = <LBRACE> {pos = token.sourceEnd;}
2731 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2734 {pos = token.sourceEnd;}
2735 } catch (ParseException e) {
2736 errorMessage = "'}' expected";
2740 processParseExceptionDebug(e);
2743 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2744 cases.toArray(abcase);
2750 * A Switch statement with : ... endswitch;
2751 * @param start the begin offset of the switch
2752 * @param end the end offset of the switch
2754 AbstractCase[] switchStatementColon(final int start, final int end) :
2757 final ArrayList cases = new ArrayList();
2762 token = <COLON> {pos = token.sourceEnd;}
2764 setMarker(fileToParse,
2765 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2769 "Line " + token.beginLine);
2770 } catch (CoreException e) {
2771 PHPeclipsePlugin.log(e);
2773 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2775 token = <ENDSWITCH> {pos = token.sourceEnd;}
2776 } catch (ParseException e) {
2777 errorMessage = "'endswitch' expected";
2781 processParseExceptionDebug(e);
2784 token = <SEMICOLON> {pos = token.sourceEnd;}
2785 } catch (ParseException e) {
2786 errorMessage = "';' expected after 'endswitch' keyword";
2790 processParseExceptionDebug(e);
2793 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2794 cases.toArray(abcase);
2799 AbstractCase switchLabel0() :
2801 final Expression expr;
2802 Statement statement;
2803 final ArrayList stmts = new ArrayList();
2804 final Token token = PHPParser.token;
2805 final int start = PHPParser.token.next.sourceStart;
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 final int end = PHPParser.token.next.sourceStart;
2819 return new DefaultCase(stmtsArray,start,end);
2821 if (listSize != 0) {
2822 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2824 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2831 * case Expression() :
2833 * @return the if it was a case and null if not
2835 Expression SwitchLabel() :
2837 final Expression expr;
2843 } catch (ParseException e) {
2844 if (errorMessage != null) throw e;
2845 errorMessage = "expression expected after 'case' keyword";
2847 errorStart = token.sourceEnd +1;
2848 errorEnd = token.sourceEnd +1;
2853 } catch (ParseException e) {
2854 errorMessage = "':' expected after case expression";
2856 errorStart = expr.sourceEnd+1;
2857 errorEnd = expr.sourceEnd+1;
2858 processParseExceptionDebug(e);
2865 } catch (ParseException e) {
2866 errorMessage = "':' expected after 'default' keyword";
2868 errorStart = token.sourceEnd+1;
2869 errorEnd = token.sourceEnd+1;
2870 processParseExceptionDebug(e);
2875 Break BreakStatement() :
2877 Expression expression = null;
2878 final Token token, token2;
2882 token = <BREAK> {pos = token.sourceEnd+1;}
2883 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2885 token2 = <SEMICOLON>
2886 {pos = token2.sourceEnd;}
2887 } catch (ParseException e) {
2888 errorMessage = "';' expected after 'break' keyword";
2892 processParseExceptionDebug(e);
2894 {return new Break(expression, token.sourceStart, pos);}
2897 IfStatement IfStatement() :
2899 final Expression condition;
2900 final IfStatement ifStatement;
2904 token = <IF> condition = Condition("if")
2905 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2906 {return ifStatement;}
2910 Expression Condition(final String keyword) :
2912 final Expression condition;
2917 } catch (ParseException e) {
2918 errorMessage = "'(' expected after " + keyword + " keyword";
2920 errorStart = PHPParser.token.sourceEnd + 1;
2921 errorEnd = PHPParser.token.sourceEnd + 1;
2922 processParseExceptionDebug(e);
2924 condition = Expression()
2927 } catch (ParseException e) {
2928 errorMessage = "')' expected after " + keyword + " keyword";
2930 errorStart = condition.sourceEnd+1;
2931 errorEnd = condition.sourceEnd+1;
2932 processParseExceptionDebug(e);
2937 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2939 Statement statement;
2940 final Statement stmt;
2941 final Statement[] statementsArray;
2942 ElseIf elseifStatement;
2943 Else elseStatement = null;
2944 final ArrayList stmts;
2945 final ArrayList elseIfList = new ArrayList();
2946 final ElseIf[] elseIfs;
2947 int pos = SimpleCharStream.getPosition();
2948 final int endStatements;
2952 {stmts = new ArrayList();}
2953 ( statement = Statement() {stmts.add(statement);}
2954 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2955 {endStatements = SimpleCharStream.getPosition();}
2956 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2957 [elseStatement = ElseStatementColon()]
2960 setMarker(fileToParse,
2961 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2965 "Line " + token.beginLine);
2966 } catch (CoreException e) {
2967 PHPeclipsePlugin.log(e);
2971 } catch (ParseException e) {
2972 errorMessage = "'endif' expected";
2974 errorStart = e.currentToken.sourceStart;
2975 errorEnd = e.currentToken.sourceEnd;
2980 } catch (ParseException e) {
2981 errorMessage = "';' expected after 'endif' keyword";
2983 errorStart = e.currentToken.sourceStart;
2984 errorEnd = e.currentToken.sourceEnd;
2988 elseIfs = new ElseIf[elseIfList.size()];
2989 elseIfList.toArray(elseIfs);
2990 if (stmts.size() == 1) {
2991 return new IfStatement(condition,
2992 (Statement) stmts.get(0),
2996 SimpleCharStream.getPosition());
2998 statementsArray = new Statement[stmts.size()];
2999 stmts.toArray(statementsArray);
3000 return new IfStatement(condition,
3001 new Block(statementsArray,pos,endStatements),
3005 SimpleCharStream.getPosition());
3010 (stmt = Statement() | stmt = htmlBlock())
3011 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3015 {pos = SimpleCharStream.getPosition();}
3016 statement = Statement()
3017 {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
3018 } catch (ParseException e) {
3019 if (errorMessage != null) {
3022 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3024 errorStart = e.currentToken.sourceStart;
3025 errorEnd = e.currentToken.sourceEnd;
3030 elseIfs = new ElseIf[elseIfList.size()];
3031 elseIfList.toArray(elseIfs);
3032 return new IfStatement(condition,
3037 SimpleCharStream.getPosition());}
3040 ElseIf ElseIfStatementColon() :
3042 final Expression condition;
3043 Statement statement;
3044 final ArrayList list = new ArrayList();
3045 final Token elseifToken;
3048 elseifToken = <ELSEIF> condition = Condition("elseif")
3049 <COLON> ( statement = Statement() {list.add(statement);}
3050 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3052 final int sizeList = list.size();
3053 final Statement[] stmtsArray = new Statement[sizeList];
3054 list.toArray(stmtsArray);
3055 return new ElseIf(condition,stmtsArray ,
3056 elseifToken.sourceStart,
3057 stmtsArray[sizeList-1].sourceEnd);}
3060 Else ElseStatementColon() :
3062 Statement statement;
3063 final ArrayList list = new ArrayList();
3064 final Token elseToken;
3067 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3068 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3070 final int sizeList = list.size();
3071 final Statement[] stmtsArray = new Statement[sizeList];
3072 list.toArray(stmtsArray);
3073 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3076 ElseIf ElseIfStatement() :
3078 final Expression condition;
3079 //final Statement statement;
3080 final Token elseifToken;
3081 final Statement[] statement = new Statement[1];
3084 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3086 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3089 WhileStatement WhileStatement() :
3091 final Expression condition;
3092 final Statement action;
3093 final Token whileToken;
3096 whileToken = <WHILE>
3097 condition = Condition("while")
3098 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3099 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3102 Statement WhileStatement0(final int start, final int end) :
3104 Statement statement;
3105 final ArrayList stmts = new ArrayList();
3106 final int pos = SimpleCharStream.getPosition();
3109 <COLON> (statement = Statement() {stmts.add(statement);})*
3111 setMarker(fileToParse,
3112 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3116 "Line " + token.beginLine);
3117 } catch (CoreException e) {
3118 PHPeclipsePlugin.log(e);
3122 } catch (ParseException e) {
3123 errorMessage = "'endwhile' expected";
3125 errorStart = e.currentToken.sourceStart;
3126 errorEnd = e.currentToken.sourceEnd;
3132 final Statement[] stmtsArray = new Statement[stmts.size()];
3133 stmts.toArray(stmtsArray);
3134 return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3135 } catch (ParseException e) {
3136 errorMessage = "';' expected after 'endwhile' keyword";
3138 errorStart = e.currentToken.sourceStart;
3139 errorEnd = e.currentToken.sourceEnd;
3143 statement = Statement()
3147 DoStatement DoStatement() :
3149 final Statement action;
3150 final Expression condition;
3152 Token token2 = null;
3155 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3157 token2 = <SEMICOLON>
3158 } catch (ParseException e) {
3159 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3161 errorStart = condition.sourceEnd+1;
3162 errorEnd = condition.sourceEnd+1;
3163 processParseExceptionDebug(e);
3166 if (token2 == null) {
3167 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3169 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3173 ForeachStatement ForeachStatement() :
3175 Statement statement = null;
3176 Expression expression = null;
3177 ArrayVariableDeclaration variable = null;
3179 Token lparenToken = null;
3180 Token asToken = null;
3181 Token rparenToken = null;
3185 foreachToken = <FOREACH>
3187 lparenToken = <LPAREN>
3188 {pos = lparenToken.sourceEnd+1;}
3189 } catch (ParseException e) {
3190 errorMessage = "'(' expected after 'foreach' keyword";
3192 errorStart = e.currentToken.sourceStart;
3193 errorEnd = e.currentToken.sourceEnd;
3194 processParseExceptionDebug(e);
3195 {pos = foreachToken.sourceEnd+1;}
3198 expression = Expression()
3199 {pos = expression.sourceEnd+1;}
3200 } catch (ParseException e) {
3201 errorMessage = "variable expected";
3203 errorStart = e.currentToken.sourceStart;
3204 errorEnd = e.currentToken.sourceEnd;
3205 processParseExceptionDebug(e);
3209 {pos = asToken.sourceEnd+1;}
3210 } catch (ParseException e) {
3211 errorMessage = "'as' expected";
3213 errorStart = e.currentToken.sourceStart;
3214 errorEnd = e.currentToken.sourceEnd;
3215 processParseExceptionDebug(e);
3218 variable = ArrayVariable()
3219 {pos = variable.sourceEnd+1;}
3220 } catch (ParseException e) {
3221 if (errorMessage != null) throw e;
3222 errorMessage = "variable expected";
3224 errorStart = e.currentToken.sourceStart;
3225 errorEnd = e.currentToken.sourceEnd;
3226 processParseExceptionDebug(e);
3229 rparenToken = <RPAREN>
3230 {pos = rparenToken.sourceEnd+1;}
3231 } catch (ParseException e) {
3232 errorMessage = "')' expected after 'foreach' keyword";
3234 errorStart = e.currentToken.sourceStart;
3235 errorEnd = e.currentToken.sourceEnd;
3236 processParseExceptionDebug(e);
3239 statement = Statement()
3240 {pos = statement.sourceEnd+1;}
3241 } catch (ParseException e) {
3242 if (errorMessage != null) throw e;
3243 errorMessage = "statement expected";
3245 errorStart = e.currentToken.sourceStart;
3246 errorEnd = e.currentToken.sourceEnd;
3247 processParseExceptionDebug(e);
3250 return new ForeachStatement(expression,
3253 foreachToken.sourceStart,
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);