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 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);
108 token_source = new PHPParserTokenManager(jj_input_stream);
110 ReInit(new StringReader(strEval));
112 phpDocument = new PHPDocument(null,"_root".toCharArray());
113 currentSegment = phpDocument;
114 outlineInfo = new PHPOutlineInfo(null, currentSegment);
115 token_source.SwitchTo(PHPParserTokenManager.PHPPARSING);
119 public final void htmlParserTester(final File fileName) throws FileNotFoundException, ParseException {
120 final Reader stream = new FileReader(fileName);
121 if (jj_input_stream == null) {
122 jj_input_stream = new SimpleCharStream(stream, 1, 1);
123 token_source = new PHPParserTokenManager(jj_input_stream);
127 phpDocument = new PHPDocument(null,"_root".toCharArray());
128 currentSegment = phpDocument;
129 outlineInfo = new PHPOutlineInfo(null, currentSegment);
133 public final void htmlParserTester(final String strEval) throws ParseException {
134 final StringReader stream = new StringReader(strEval);
135 if (jj_input_stream == null) {
136 jj_input_stream = new SimpleCharStream(stream, 1, 1);
137 token_source = new PHPParserTokenManager(jj_input_stream);
141 phpDocument = new PHPDocument(null,"_root".toCharArray());
142 currentSegment = phpDocument;
143 outlineInfo = new PHPOutlineInfo(null, currentSegment);
148 * Reinitialize the parser.
150 private static final void init() {
151 nodes = new AstNode[AstStackIncrement];
157 * Add an php node on the stack.
158 * @param node the node that will be added to the stack
160 private static final void pushOnAstNodes(final AstNode node) {
162 nodes[++nodePtr] = node;
163 } catch (IndexOutOfBoundsException e) {
164 final int oldStackLength = nodes.length;
165 final AstNode[] oldStack = nodes;
166 nodes = new AstNode[oldStackLength + AstStackIncrement];
167 System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
168 nodePtr = oldStackLength;
169 nodes[nodePtr] = node;
173 public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
174 phpDocument = new PHPDocument(parent,"_root".toCharArray());
175 currentSegment = phpDocument;
176 outlineInfo = new PHPOutlineInfo(parent, currentSegment);
177 final StringReader stream = new StringReader(s);
178 if (jj_input_stream == null) {
179 jj_input_stream = new SimpleCharStream(stream, 1, 1);
180 token_source = new PHPParserTokenManager(jj_input_stream);
186 phpDocument.nodes = new AstNode[nodes.length];
187 System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
188 if (PHPeclipsePlugin.DEBUG) {
189 PHPeclipsePlugin.log(1,phpDocument.toString());
191 } catch (ParseException e) {
192 processParseException(e);
198 * This function will throw the exception if we are in debug mode
199 * and process it if we are in production mode.
200 * this should be fast since the PARSER_DEBUG is static final so the difference will be at compile time
201 * @param e the exception
202 * @throws ParseException the thrown exception
204 private static void processParseExceptionDebug(final ParseException e) throws ParseException {
208 processParseException(e);
211 * This method will process the parse exception.
212 * If the error message is null, the parse exception wasn't catched and a trace is written in the log
213 * @param e the ParseException
215 private static void processParseException(final ParseException e) {
216 if (errorMessage == null) {
217 PHPeclipsePlugin.log(e);
218 errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
219 errorStart = e.currentToken.sourceStart;
220 errorEnd = e.currentToken.sourceEnd;
224 // if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e);
228 * Create marker for the parse error.
229 * @param e the ParseException
231 private static void setMarker(final ParseException e) {
233 if (errorStart == -1) {
234 setMarker(fileToParse,
236 e.currentToken.sourceStart,
237 e.currentToken.sourceEnd,
239 "Line " + e.currentToken.beginLine+", "+e.currentToken.sourceStart+':'+e.currentToken.sourceEnd);
241 setMarker(fileToParse,
246 "Line " + e.currentToken.beginLine+", "+errorStart+':'+errorEnd);
250 } catch (CoreException e2) {
251 PHPeclipsePlugin.log(e2);
255 private static void scanLine(final String output,
258 final int brIndx) throws CoreException {
260 final StringBuffer lineNumberBuffer = new StringBuffer(10);
262 current = output.substring(indx, brIndx);
264 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
265 final int onLine = current.indexOf("on line <b>");
267 lineNumberBuffer.delete(0, lineNumberBuffer.length());
268 for (int i = onLine; i < current.length(); i++) {
269 ch = current.charAt(i);
270 if ('0' <= ch && '9' >= ch) {
271 lineNumberBuffer.append(ch);
275 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
277 final Hashtable attributes = new Hashtable();
279 current = current.replaceAll("\n", "");
280 current = current.replaceAll("<b>", "");
281 current = current.replaceAll("</b>", "");
282 MarkerUtilities.setMessage(attributes, current);
284 if (current.indexOf(PARSE_ERROR_STRING) != -1)
285 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
286 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
287 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
289 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
290 MarkerUtilities.setLineNumber(attributes, lineNumber);
291 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
296 public final void parse(final String s) {
297 final StringReader stream = new StringReader(s);
298 if (jj_input_stream == null) {
299 jj_input_stream = new SimpleCharStream(stream, 1, 1);
300 token_source = new PHPParserTokenManager(jj_input_stream);
306 } catch (ParseException e) {
307 processParseException(e);
312 * Call the php parse command ( php -l -f <filename> )
313 * and create markers according to the external parser output
315 public static void phpExternalParse(final IFile file) {
316 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
317 final String filename = file.getLocation().toString();
319 final String[] arguments = { filename };
320 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
321 final String command = form.format(arguments);
323 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
326 // parse the buffer to find the errors and warnings
327 createMarkers(parserResult, file);
328 } catch (CoreException e) {
329 PHPeclipsePlugin.log(e);
334 * Put a new html block in the stack.
336 public final void createNewHTMLCode() {
337 final int currentPosition = token.sourceStart;
338 if (currentPosition == htmlStart ||
339 currentPosition < htmlStart ||
340 currentPosition > jj_input_stream.getCurrentBuffer().length()) {
343 final String html = jj_input_stream.getCurrentBuffer().substring(htmlStart, currentPosition);
344 pushOnAstNodes(new HTMLCode(html, htmlStart,currentPosition));
347 /** Create a new task. */
348 public final void createNewTask(final int todoStart) {
349 final String todo = jj_input_stream.getCurrentBuffer().substring(todoStart,
350 jj_input_stream.getCurrentBuffer().indexOf("\n",
354 setMarker(fileToParse,
356 jj_input_stream.getBeginLine(),
358 "Line "+jj_input_stream.getBeginLine());
359 } catch (CoreException e) {
360 PHPeclipsePlugin.log(e);
365 private final void parse() throws ParseException {
370 PARSER_END(PHPParser)
374 // CommonTokenAction: use the begins/ends fields added to the Jack
375 // CharStream class to set corresponding fields in each Token (which was
376 // also extended with new fields). By default Jack doesn't supply absolute
377 // offsets, just line/column offsets
378 void CommonTokenAction(Token t) {
379 t.sourceStart = input_stream.getBeginOffset();
380 t.sourceEnd = input_stream.getBeginOffset();
381 } // CommonTokenAction
386 <PHPSTARTSHORT : "<?"> : PHPPARSING
387 | <PHPSTARTLONG : "<?php"> : PHPPARSING
388 | <PHPECHOSTART : "<?="> : PHPPARSING
391 <PHPPARSING, IN_SINGLE_LINE_COMMENT,IN_VARIABLE> TOKEN :
393 <PHPEND :"?>"> : DEFAULT
396 /* Skip any character if we are not in php mode */
413 <IN_VARIABLE> SPECIAL_TOKEN :
422 <PHPPARSING> SPECIAL_TOKEN :
424 "//" : IN_SINGLE_LINE_COMMENT
425 | "#" : IN_SINGLE_LINE_COMMENT
426 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
427 | "/*" : IN_MULTI_LINE_COMMENT
430 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
432 <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
436 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
444 todoToken = "TODO" {createNewTask(todoToken.sourceStart);}
446 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
451 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
456 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
466 | <FUNCTION : "function">
469 | <ELSEIF : "elseif">
476 /* LANGUAGE CONSTRUCT */
481 | <INCLUDE : "include">
482 | <REQUIRE : "require">
483 | <INCLUDE_ONCE : "include_once">
484 | <REQUIRE_ONCE : "require_once">
485 | <GLOBAL : "global">
486 | <DEFINE : "define">
487 | <STATIC : "static">
490 <PHPPARSING,IN_VARIABLE> TOKEN :
492 <CLASSACCESS : "->"> : PHPPARSING
493 | <STATICCLASSACCESS : "::"> : PHPPARSING
494 | <ARRAYASSIGN : "=>"> : PHPPARSING
497 /* RESERVED WORDS AND LITERALS */
503 | <CONTINUE : "continue">
504 | <_DEFAULT : "default">
506 | <EXTENDS : "extends">
511 | <RETURN : "return">
513 | <SWITCH : "switch">
518 | <ENDWHILE : "endwhile">
519 | <ENDSWITCH: "endswitch">
521 | <ENDFOR : "endfor">
522 | <FOREACH : "foreach">
530 | <OBJECT : "object">
532 | <BOOLEAN : "boolean">
534 | <DOUBLE : "double">
537 | <INTEGER : "integer">
541 <PHPPARSING,IN_VARIABLE> TOKEN :
543 <AT : "@"> : PHPPARSING
544 | <BANG : "!"> : PHPPARSING
545 | <TILDE : "~"> : PHPPARSING
546 | <HOOK : "?"> : PHPPARSING
547 | <COLON : ":"> : PHPPARSING
551 <PHPPARSING,IN_VARIABLE> TOKEN :
553 <OR_OR : "||"> : PHPPARSING
554 | <AND_AND : "&&"> : PHPPARSING
555 | <PLUS_PLUS : "++"> : PHPPARSING
556 | <MINUS_MINUS : "--"> : PHPPARSING
557 | <PLUS : "+"> : PHPPARSING
558 | <MINUS : "-"> : PHPPARSING
559 | <STAR : "*"> : PHPPARSING
560 | <SLASH : "/"> : PHPPARSING
561 | <BIT_AND : "&"> : PHPPARSING
562 | <BIT_OR : "|"> : PHPPARSING
563 | <XOR : "^"> : PHPPARSING
564 | <REMAINDER : "%"> : PHPPARSING
565 | <LSHIFT : "<<"> : PHPPARSING
566 | <RSIGNEDSHIFT : ">>"> : PHPPARSING
567 | <RUNSIGNEDSHIFT : ">>>"> : PHPPARSING
568 | <_ORL : "OR"> : PHPPARSING
569 | <_ANDL : "AND"> : PHPPARSING
576 <DECIMAL_LITERAL> (["l","L"])?
577 | <HEX_LITERAL> (["l","L"])?
578 | <OCTAL_LITERAL> (["l","L"])?
581 <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
583 <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
585 <#OCTAL_LITERAL: "0" (["0"-"7"])* >
587 <FLOATING_POINT_LITERAL:
588 (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
589 | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
590 | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
591 | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
594 <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
596 <STRING_LITERAL: (<STRING_2> | <STRING_3>)>
597 //| <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
598 | <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
599 | <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
602 <IN_STRING,DOLLAR_IN_STRING> SKIP :
604 <ESCAPED : ("\\" ~[])> : IN_STRING
609 <DOUBLEQUOTE : "\""> : IN_STRING
615 <DOLLARS : "$"> : DOLLAR_IN_STRING
618 <IN_STRING,DOLLAR_IN_STRING> TOKEN :
620 <DOUBLEQUOTE2 : "\""> : PHPPARSING
623 <DOLLAR_IN_STRING> TOKEN :
625 <LBRACE1 : "{"> : DOLLAR_IN_STRING_EXPR
628 <IN_STRING> SPECIAL_TOKEN :
633 <SKIPSTRING> SPECIAL_TOKEN :
643 <DOLLAR_IN_STRING_EXPR> TOKEN :
645 <RBRACE1 : "}"> : DOLLAR_IN_STRING
648 <DOLLAR_IN_STRING_EXPR> TOKEN :
658 <DOLLAR_IN_STRING_EXPR,IN_STRING> SKIP :
665 <PHPPARSING,IN_VARIABLE> TOKEN : {<DOLLAR : "$"> : IN_VARIABLE}
668 <PHPPARSING, IN_VARIABLE, DOLLAR_IN_STRING> TOKEN :
670 <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
673 ["a"-"z"] | ["A"-"Z"]
681 "_" | ["\u007f"-"\u00ff"]
685 <DOLLAR_IN_STRING> SPECIAL_TOKEN :
691 <PHPPARSING,IN_VARIABLE> TOKEN :
693 <LPAREN : "("> : PHPPARSING
694 | <RPAREN : ")"> : PHPPARSING
695 | <LBRACE : "{"> : PHPPARSING
696 | <RBRACE : "}"> : PHPPARSING
697 | <LBRACKET : "["> : PHPPARSING
698 | <RBRACKET : "]"> : PHPPARSING
699 | <SEMICOLON : ";"> : PHPPARSING
700 | <COMMA : ","> : PHPPARSING
701 | <DOT : "."> : PHPPARSING
706 <PHPPARSING,IN_VARIABLE> TOKEN :
708 <GT : ">"> : PHPPARSING
709 | <LT : "<"> : PHPPARSING
710 | <EQUAL_EQUAL : "=="> : PHPPARSING
711 | <LE : "<="> : PHPPARSING
712 | <GE : ">="> : PHPPARSING
713 | <NOT_EQUAL : "!="> : PHPPARSING
714 | <DIF : "<>"> : PHPPARSING
715 | <BANGDOUBLEEQUAL : "!=="> : PHPPARSING
716 | <TRIPLEEQUAL : "==="> : PHPPARSING
720 <PHPPARSING,IN_VARIABLE> TOKEN :
722 <ASSIGN : "="> : PHPPARSING
723 | <PLUSASSIGN : "+="> : PHPPARSING
724 | <MINUSASSIGN : "-="> : PHPPARSING
725 | <STARASSIGN : "*="> : PHPPARSING
726 | <SLASHASSIGN : "/="> : PHPPARSING
727 | <ANDASSIGN : "&="> : PHPPARSING
728 | <ORASSIGN : "|="> : PHPPARSING
729 | <XORASSIGN : "^="> : PHPPARSING
730 | <DOTASSIGN : ".="> : PHPPARSING
731 | <REMASSIGN : "%="> : PHPPARSING
732 | <TILDEEQUAL : "~="> : PHPPARSING
733 | <LSHIFTASSIGN : "<<="> : PHPPARSING
734 | <RSIGNEDSHIFTASSIGN : ">>="> : PHPPARSING
749 {createNewHTMLCode();}
750 } catch (TokenMgrError e) {
751 PHPeclipsePlugin.log(e);
752 errorStart = jj_input_stream.getBeginOffset();
753 errorEnd = jj_input_stream.getEndOffset();
754 errorMessage = e.getMessage();
756 throw generateParseException();
761 * A php block is a <?= expression [;]?>
762 * or <?php somephpcode ?>
763 * or <? somephpcode ?>
767 final PHPEchoBlock phpEchoBlock;
768 final Token token,phpEnd;
771 phpEchoBlock = phpEchoBlock()
772 {pushOnAstNodes(phpEchoBlock);}
775 | token = <PHPSTARTSHORT>
777 setMarker(fileToParse,
778 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
782 "Line " + token.beginLine);
783 } catch (CoreException e) {
784 PHPeclipsePlugin.log(e);
787 {createNewHTMLCode();}
791 {htmlStart = phpEnd.sourceEnd;}
792 } catch (ParseException e) {
793 errorMessage = "'?>' expected";
795 errorStart = e.currentToken.sourceStart;
796 errorEnd = e.currentToken.sourceEnd;
797 processParseExceptionDebug(e);
801 PHPEchoBlock phpEchoBlock() :
803 final Expression expr;
804 final PHPEchoBlock echoBlock;
805 final Token token, token2;
808 token = <PHPECHOSTART> {createNewHTMLCode();}
809 expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
811 htmlStart = token2.sourceEnd;
813 echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
814 pushOnAstNodes(echoBlock);
824 ClassDeclaration ClassDeclaration() :
826 final ClassDeclaration classDeclaration;
827 Token className = null;
828 final Token superclassName, token, extendsToken;
829 String classNameImage = SYNTAX_ERROR_CHAR;
830 String superclassNameImage = null;
836 className = <IDENTIFIER>
837 {classNameImage = className.image;}
838 } catch (ParseException e) {
839 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
841 errorStart = token.sourceEnd+1;
842 errorEnd = token.sourceEnd+1;
843 processParseExceptionDebug(e);
846 extendsToken = <EXTENDS>
848 superclassName = <IDENTIFIER>
849 {superclassNameImage = superclassName.image;}
850 } catch (ParseException e) {
851 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
853 errorStart = extendsToken.sourceEnd+1;
854 errorEnd = extendsToken.sourceEnd+1;
855 processParseExceptionDebug(e);
856 superclassNameImage = SYNTAX_ERROR_CHAR;
861 if (className == null) {
862 start = token.sourceStart;
863 end = token.sourceEnd;
865 start = className.sourceStart;
866 end = className.sourceEnd;
868 if (superclassNameImage == null) {
870 classDeclaration = new ClassDeclaration(currentSegment,
875 classDeclaration = new ClassDeclaration(currentSegment,
881 currentSegment.add(classDeclaration);
882 currentSegment = classDeclaration;
884 classEnd = ClassBody(classDeclaration)
885 {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
886 classDeclaration.sourceEnd = classEnd;
887 pushOnAstNodes(classDeclaration);
888 return classDeclaration;}
891 int ClassBody(final ClassDeclaration classDeclaration) :
898 } catch (ParseException e) {
899 errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
901 errorStart = e.currentToken.sourceStart;
902 errorEnd = e.currentToken.sourceEnd;
903 processParseExceptionDebug(e);
905 ( ClassBodyDeclaration(classDeclaration) )*
908 {return token.sourceEnd;}
909 } catch (ParseException e) {
910 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
912 errorStart = e.currentToken.sourceStart;
913 errorEnd = e.currentToken.sourceEnd;
914 processParseExceptionDebug(e);
915 return this.token.sourceEnd;
920 * A class can contain only methods and fields.
922 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
924 final MethodDeclaration method;
925 final FieldDeclaration field;
928 method = MethodDeclaration() {method.analyzeCode();
929 classDeclaration.addMethod(method);}
930 | field = FieldDeclaration() {classDeclaration.addField(field);}
934 * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
935 * it is only used by ClassBodyDeclaration()
937 FieldDeclaration FieldDeclaration() :
939 VariableDeclaration variableDeclaration;
940 final VariableDeclaration[] list;
941 final ArrayList arrayList = new ArrayList();
947 token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
949 arrayList.add(variableDeclaration);
950 pos = variableDeclaration.sourceEnd;
953 <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
955 arrayList.add(variableDeclaration);
956 outlineInfo.addVariable(variableDeclaration.name());
957 pos = variableDeclaration.sourceEnd;
962 } catch (ParseException e) {
963 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
967 processParseExceptionDebug(e);
970 {list = new VariableDeclaration[arrayList.size()];
971 arrayList.toArray(list);
973 if (token2 == null) {
974 end = list[list.length-1].sourceEnd;
976 end = token2.sourceEnd;
978 return new FieldDeclaration(list,
985 * a strict variable declarator : there cannot be a suffix here.
986 * It will be used by fields and formal parameters
988 VariableDeclaration VariableDeclaratorNoSuffix() :
990 final Token token, lbrace,rbrace;
991 Expression expr, initializer = null;
999 {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
1001 lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
1002 {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
1005 assignToken = <ASSIGN>
1007 initializer = VariableInitializer()
1008 } catch (ParseException e) {
1009 errorMessage = "Literal expression expected in variable initializer";
1011 errorStart = assignToken.sourceEnd +1;
1012 errorEnd = assignToken.sourceEnd +1;
1013 processParseExceptionDebug(e);
1017 if (initializer == null) {
1018 return new VariableDeclaration(currentSegment,
1020 variable.sourceStart,
1021 variable.sourceEnd);
1023 return new VariableDeclaration(currentSegment,
1026 VariableDeclaration.EQUAL,
1027 variable.sourceStart);
1032 * this will be used by static statement
1034 VariableDeclaration VariableDeclarator() :
1036 final AbstractVariable variable;
1037 Expression initializer = null;
1041 variable = VariableDeclaratorId()
1045 initializer = VariableInitializer()
1046 } catch (ParseException e) {
1047 errorMessage = "Literal expression expected in variable initializer";
1049 errorStart = token.sourceEnd+1;
1050 errorEnd = token.sourceEnd+1;
1051 processParseExceptionDebug(e);
1055 if (initializer == null) {
1056 return new VariableDeclaration(currentSegment,
1058 variable.sourceStart,
1059 variable.sourceEnd);
1061 return new VariableDeclaration(currentSegment,
1064 VariableDeclaration.EQUAL,
1065 variable.sourceStart);
1071 * @return the variable name (with suffix)
1073 AbstractVariable VariableDeclaratorId() :
1076 AbstractVariable expression = null;
1083 expression = VariableSuffix(var)
1086 if (expression == null) {
1091 } catch (ParseException e) {
1092 errorMessage = "'$' expected for variable identifier";
1094 errorStart = e.currentToken.sourceStart;
1095 errorEnd = e.currentToken.sourceEnd;
1100 Variable Variable() :
1102 Variable variable = null;
1106 token = <DOLLAR> variable = Var()
1114 Variable variable = null;
1115 final Token token,token2;
1116 ConstantIdentifier constant;
1117 Expression expression;
1120 token = <DOLLAR> variable = Var()
1121 {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1123 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1125 return new Variable(expression,
1130 token = <IDENTIFIER>
1132 outlineInfo.addVariable('$' + token.image);
1133 return new Variable(token.image,token.sourceStart,token.sourceEnd);
1137 Expression VariableInitializer() :
1139 final Expression expr;
1140 final Token token, token2;
1146 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1147 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1149 token2.sourceStart);}
1151 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1152 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1154 token2.sourceStart);}
1156 expr = ArrayDeclarator()
1159 token = <IDENTIFIER>
1160 {return new ConstantIdentifier(token);}
1163 ArrayVariableDeclaration ArrayVariable() :
1165 final Expression expr,expr2;
1170 <ARRAYASSIGN> expr2 = Expression()
1171 {return new ArrayVariableDeclaration(expr,expr2);}
1173 {return new ArrayVariableDeclaration(expr,jj_input_stream.getPosition());}
1176 ArrayVariableDeclaration[] ArrayInitializer() :
1178 ArrayVariableDeclaration expr;
1179 final ArrayList list = new ArrayList();
1184 expr = ArrayVariable()
1186 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1191 <COMMA> {list.add(null);}
1195 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1201 * A Method Declaration.
1202 * <b>function</b> MetodDeclarator() Block()
1204 MethodDeclaration MethodDeclaration() :
1206 final MethodDeclaration functionDeclaration;
1208 final OutlineableWithChildren seg = currentSegment;
1214 functionDeclaration = MethodDeclarator(token.sourceStart)
1215 {outlineInfo.addVariable(functionDeclaration.name);}
1216 } catch (ParseException e) {
1217 if (errorMessage != null) throw e;
1218 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1220 errorStart = e.currentToken.sourceStart;
1221 errorEnd = e.currentToken.sourceEnd;
1224 {currentSegment = functionDeclaration;}
1226 {functionDeclaration.statements = block.statements;
1227 currentSegment = seg;
1228 return functionDeclaration;}
1232 * A MethodDeclarator.
1233 * [&] IDENTIFIER(parameters ...).
1234 * @return a function description for the outline
1236 MethodDeclaration MethodDeclarator(final int start) :
1238 Token identifier = null;
1239 Token reference = null;
1240 final ArrayList formalParameters = new ArrayList();
1241 String identifierChar = SYNTAX_ERROR_CHAR;
1245 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1247 identifier = <IDENTIFIER>
1249 identifierChar = identifier.image;
1250 end = identifier.sourceEnd;
1252 } catch (ParseException e) {
1253 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1255 errorStart = e.currentToken.sourceEnd;
1256 errorEnd = e.currentToken.next.sourceStart;
1257 processParseExceptionDebug(e);
1259 end = FormalParameters(formalParameters)
1261 int nameStart, nameEnd;
1262 if (identifier == null) {
1263 if (reference == null) {
1264 nameStart = start + 9;
1265 nameEnd = start + 10;
1267 nameStart = reference.sourceEnd + 1;
1268 nameEnd = reference.sourceEnd + 2;
1271 nameStart = identifier.sourceStart;
1272 nameEnd = identifier.sourceEnd;
1274 return new MethodDeclaration(currentSegment,
1286 * FormalParameters follows method identifier.
1287 * (FormalParameter())
1289 int FormalParameters(final ArrayList parameters) :
1291 VariableDeclaration var;
1293 Token tok = this.token;
1294 int end = tok.sourceEnd;
1299 {end = tok.sourceEnd;}
1300 } catch (ParseException e) {
1301 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1303 errorStart = e.currentToken.next.sourceStart;
1304 errorEnd = e.currentToken.next.sourceEnd;
1305 processParseExceptionDebug(e);
1308 var = FormalParameter()
1309 {parameters.add(var);end = var.sourceEnd;}
1311 <COMMA> var = FormalParameter()
1312 {parameters.add(var);end = var.sourceEnd;}
1317 {end = token.sourceEnd;}
1318 } catch (ParseException e) {
1319 errorMessage = "')' expected";
1321 errorStart = e.currentToken.next.sourceStart;
1322 errorEnd = e.currentToken.next.sourceEnd;
1323 processParseExceptionDebug(e);
1329 * A formal parameter.
1330 * $varname[=value] (,$varname[=value])
1332 VariableDeclaration FormalParameter() :
1334 final VariableDeclaration variableDeclaration;
1338 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1340 outlineInfo.addVariable('$'+variableDeclaration.name());
1341 if (token != null) {
1342 variableDeclaration.setReference(true);
1344 return variableDeclaration;}
1347 ConstantIdentifier Type() :
1348 {final Token token;}
1350 token = <STRING> {return new ConstantIdentifier(token);}
1351 | token = <BOOL> {return new ConstantIdentifier(token);}
1352 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1353 | token = <REAL> {return new ConstantIdentifier(token);}
1354 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1355 | token = <FLOAT> {return new ConstantIdentifier(token);}
1356 | token = <INT> {return new ConstantIdentifier(token);}
1357 | token = <INTEGER> {return new ConstantIdentifier(token);}
1358 | token = <OBJECT> {return new ConstantIdentifier(token);}
1361 Expression Expression() :
1363 final Expression expr;
1364 Expression initializer = null;
1365 int assignOperator = -1;
1369 expr = ConditionalExpression()
1371 assignOperator = AssignmentOperator()
1373 initializer = Expression()
1374 } catch (ParseException e) {
1375 if (errorMessage != null) {
1378 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1380 errorEnd = jj_input_stream.getPosition();
1385 if (assignOperator != -1) {// todo : change this, very very bad :(
1386 if (expr instanceof AbstractVariable) {
1387 return new VariableDeclaration(currentSegment,
1388 (AbstractVariable) expr,
1391 initializer.sourceEnd);
1393 String varName = expr.toStringExpression().substring(1);
1394 return new VariableDeclaration(currentSegment,
1395 new Variable(varName,
1399 initializer.sourceEnd);
1403 | expr = ExpressionWBang() {return expr;}
1406 Expression ExpressionWBang() :
1408 final Expression expr;
1412 token = <BANG> expr = ExpressionWBang()
1413 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1414 | expr = ExpressionNoBang() {return expr;}
1417 Expression ExpressionNoBang() :
1422 expr = ListExpression() {return expr;}
1424 expr = PrintExpression() {return expr;}
1428 * Any assignement operator.
1429 * @return the assignement operator id
1431 int AssignmentOperator() :
1434 <ASSIGN> {return VariableDeclaration.EQUAL;}
1435 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1436 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1437 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1438 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1439 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1440 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1441 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1442 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1443 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1444 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1445 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1446 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1449 Expression ConditionalExpression() :
1451 final Expression expr;
1452 Expression expr2 = null;
1453 Expression expr3 = null;
1456 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1458 if (expr3 == null) {
1461 return new ConditionalExpression(expr,expr2,expr3);
1465 Expression ConditionalOrExpression() :
1467 Expression expr,expr2;
1471 expr = ConditionalAndExpression()
1474 <OR_OR> {operator = OperatorIds.OR_OR;}
1475 | <_ORL> {operator = OperatorIds.ORL;}
1477 expr2 = ConditionalAndExpression()
1479 expr = new BinaryExpression(expr,expr2,operator);
1485 Expression ConditionalAndExpression() :
1487 Expression expr,expr2;
1491 expr = ConcatExpression()
1493 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1494 | <_ANDL> {operator = OperatorIds.ANDL;})
1495 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1500 Expression ConcatExpression() :
1502 Expression expr,expr2;
1505 expr = InclusiveOrExpression()
1507 <DOT> expr2 = InclusiveOrExpression()
1508 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1513 Expression InclusiveOrExpression() :
1515 Expression expr,expr2;
1518 expr = ExclusiveOrExpression()
1519 (<BIT_OR> expr2 = ExclusiveOrExpression()
1520 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1525 Expression ExclusiveOrExpression() :
1527 Expression expr,expr2;
1530 expr = AndExpression()
1532 <XOR> expr2 = AndExpression()
1533 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1538 Expression AndExpression() :
1540 Expression expr,expr2;
1543 expr = EqualityExpression()
1546 <BIT_AND> expr2 = EqualityExpression()
1547 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1552 Expression EqualityExpression() :
1554 Expression expr,expr2;
1559 expr = RelationalExpression()
1561 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1562 | token = <DIF> {operator = OperatorIds.DIF;}
1563 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1564 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1565 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1568 expr2 = RelationalExpression()
1569 } catch (ParseException e) {
1570 if (errorMessage != null) {
1573 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1575 errorStart = token.sourceEnd +1;
1576 errorEnd = token.sourceEnd +1;
1577 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1578 processParseExceptionDebug(e);
1581 expr = new BinaryExpression(expr,expr2,operator);
1587 Expression RelationalExpression() :
1589 Expression expr,expr2;
1593 expr = ShiftExpression()
1595 ( <LT> {operator = OperatorIds.LESS;}
1596 | <GT> {operator = OperatorIds.GREATER;}
1597 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1598 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1599 expr2 = ShiftExpression()
1600 {expr = new BinaryExpression(expr,expr2,operator);}
1605 Expression ShiftExpression() :
1607 Expression expr,expr2;
1611 expr = AdditiveExpression()
1613 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1614 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1615 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1616 expr2 = AdditiveExpression()
1617 {expr = new BinaryExpression(expr,expr2,operator);}
1622 Expression AdditiveExpression() :
1624 Expression expr,expr2;
1628 expr = MultiplicativeExpression()
1631 ( <PLUS> {operator = OperatorIds.PLUS;}
1632 | <MINUS> {operator = OperatorIds.MINUS;}
1634 expr2 = MultiplicativeExpression()
1635 {expr = new BinaryExpression(expr,expr2,operator);}
1640 Expression MultiplicativeExpression() :
1642 Expression expr,expr2;
1647 expr = UnaryExpression()
1648 } catch (ParseException e) {
1649 if (errorMessage != null) throw e;
1650 errorMessage = "unexpected token '"+e.currentToken.next.image+'\'';
1652 errorStart = this.token.sourceStart;
1653 errorEnd = this.token.sourceEnd;
1657 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1658 | <SLASH> {operator = OperatorIds.DIVIDE;}
1659 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1660 expr2 = UnaryExpression()
1661 {expr = new BinaryExpression(expr,expr2,operator);}
1667 * An unary expression starting with @, & or nothing
1669 Expression UnaryExpression() :
1671 final Expression expr;
1674 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1675 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1677 expr = AtNotTildeUnaryExpression() {return expr;}
1680 Expression AtNotTildeUnaryExpression() :
1682 final Expression expr;
1687 expr = AtNotTildeUnaryExpression()
1688 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1691 expr = AtNotTildeUnaryExpression()
1692 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1695 expr = AtNotUnaryExpression()
1696 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1698 expr = UnaryExpressionNoPrefix()
1703 * An expression prefixed (or not) by one or more @ and !.
1704 * @return the expression
1706 Expression AtNotUnaryExpression() :
1708 final Expression expr;
1713 expr = AtNotUnaryExpression()
1714 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1717 expr = AtNotUnaryExpression()
1718 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1720 expr = UnaryExpressionNoPrefix()
1724 Expression UnaryExpressionNoPrefix() :
1726 final Expression expr;
1730 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1732 token.sourceStart);}
1734 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1736 token.sourceStart);}
1738 expr = PreIncDecExpression()
1741 expr = UnaryExpressionNotPlusMinus()
1746 Expression PreIncDecExpression() :
1748 final Expression expr;
1754 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1756 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1758 expr = PrimaryExpression()
1759 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1762 Expression UnaryExpressionNotPlusMinus() :
1764 final Expression expr;
1767 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1768 expr = CastExpression() {return expr;}
1769 | expr = PostfixExpression() {return expr;}
1770 | expr = Literal() {return expr;}
1771 | <LPAREN> expr = Expression()
1774 } catch (ParseException e) {
1775 errorMessage = "')' expected";
1777 errorStart = expr.sourceEnd +1;
1778 errorEnd = expr.sourceEnd +1;
1779 processParseExceptionDebug(e);
1784 CastExpression CastExpression() :
1786 final ConstantIdentifier type;
1787 final Expression expr;
1788 final Token token,token1;
1795 token = <ARRAY> {type = new ConstantIdentifier(token);}
1797 <RPAREN> expr = UnaryExpression()
1798 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1801 Expression PostfixExpression() :
1803 final Expression expr;
1808 expr = PrimaryExpression()
1810 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1812 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1815 if (operator == -1) {
1818 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1822 Expression PrimaryExpression() :
1828 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1831 expr = ArrayDeclarator()
1835 Expression refPrimaryExpression(final Token reference) :
1838 Expression expr2 = null;
1839 final Token identifier;
1842 identifier = <IDENTIFIER>
1844 expr = new ConstantIdentifier(identifier);
1847 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1848 {expr = new ClassAccess(expr,
1850 ClassAccess.STATIC);}
1852 [ expr2 = Arguments(expr) ]
1854 if (expr2 == null) {
1855 if (reference != null) {
1856 ParseException e = generateParseException();
1857 errorMessage = "you cannot use a constant by reference";
1859 errorStart = reference.sourceStart;
1860 errorEnd = reference.sourceEnd;
1861 processParseExceptionDebug(e);
1868 expr = VariableDeclaratorId() //todo use the reference parameter ...
1869 [ expr = Arguments(expr) ]
1873 expr = ClassIdentifier()
1876 if (reference == null) {
1877 start = token.sourceStart;
1879 start = reference.sourceStart;
1881 expr = new ClassInstantiation(expr,
1885 [ expr = Arguments(expr) ]
1890 * An array declarator.
1894 ArrayInitializer ArrayDeclarator() :
1896 final ArrayVariableDeclaration[] vars;
1900 token = <ARRAY> vars = ArrayInitializer()
1901 {return new ArrayInitializer(vars,
1903 this.token.sourceEnd);}
1906 Expression ClassIdentifier():
1908 final Expression expr;
1912 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1913 | expr = Type() {return expr;}
1914 | expr = VariableDeclaratorId() {return expr;}
1918 * Used by Variabledeclaratorid and primarysuffix
1920 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1922 Expression expression = null;
1923 final Token classAccessToken,lbrace,rbrace;
1928 classAccessToken = <CLASSACCESS>
1931 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1933 expression = new Variable(expression,
1938 token = <IDENTIFIER>
1939 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1941 expression = Variable()
1943 } catch (ParseException e) {
1944 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1946 errorStart = classAccessToken.sourceEnd +1;
1947 errorEnd = classAccessToken.sourceEnd +1;
1948 processParseExceptionDebug(e);
1950 {return new ClassAccess(prefix,
1952 ClassAccess.NORMAL);}
1954 token = <LBRACKET> {pos = token.sourceEnd+1;}
1955 [ expression = Expression() {pos = expression.sourceEnd+1;}
1956 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1959 {pos = token.sourceEnd;}
1960 } catch (ParseException e) {
1961 errorMessage = "']' expected";
1965 processParseExceptionDebug(e);
1967 {return new ArrayDeclarator(prefix,expression,pos);}
1969 token = <LBRACE> {pos = token.sourceEnd+1;}
1970 [ expression = Expression() {pos = expression.sourceEnd+1;}
1971 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1974 {pos = token.sourceEnd;}
1975 } catch (ParseException e) {
1976 errorMessage = "']' expected";
1980 processParseExceptionDebug(e);
1982 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1988 StringLiteral literal;
1991 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1992 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1993 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1994 | token = <TRUE> {return new TrueLiteral(token);}
1995 | token = <FALSE> {return new FalseLiteral(token);}
1996 | token = <NULL> {return new NullLiteral(token);}
1997 | literal = evaluableString() {return literal;}
2000 StringLiteral evaluableString() :
2002 ArrayList list = new ArrayList();
2004 Token token,lbrace,rbrace;
2005 AbstractVariable var;
2009 start = <DOUBLEQUOTE>
2013 token = <IDENTIFIER> {list.add(new Variable(token.image,
2019 {list.add(new Variable(token.image,
2025 end = <DOUBLEQUOTE2>
2027 AbstractVariable[] vars = new AbstractVariable[list.size()];
2029 return new StringLiteral(jj_input_stream.getCurrentBuffer().substring(start.sourceEnd,end.sourceStart),
2036 FunctionCall Arguments(final Expression func) :
2038 Expression[] args = null;
2039 final Token token,lparen;
2042 lparen = <LPAREN> [ args = ArgumentList() ]
2045 {return new FunctionCall(func,args,token.sourceEnd);}
2046 } catch (ParseException e) {
2047 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2050 errorStart = lparen.sourceEnd+1;
2051 errorEnd = lparen.sourceEnd+2;
2053 errorStart = args[args.length-1].sourceEnd+1;
2054 errorEnd = args[args.length-1].sourceEnd+2;
2056 processParseExceptionDebug(e);
2059 int sourceEnd = (args == null && args.length != 0) ? lparen.sourceEnd+1 : args[args.length-1].sourceEnd;
2060 return new FunctionCall(func,args,sourceEnd);}
2064 * An argument list is a list of arguments separated by comma :
2065 * argumentDeclaration() (, argumentDeclaration)*
2066 * @return an array of arguments
2068 Expression[] ArgumentList() :
2071 final ArrayList list = new ArrayList();
2077 {list.add(arg);pos = arg.sourceEnd;}
2078 ( token = <COMMA> {pos = token.sourceEnd;}
2082 pos = arg.sourceEnd;}
2083 } catch (ParseException e) {
2084 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2088 processParseException(e);
2092 final Expression[] arguments = new Expression[list.size()];
2093 list.toArray(arguments);
2098 * A Statement without break.
2099 * @return a statement
2101 Statement StatementNoBreak() :
2103 final Statement statement;
2108 statement = expressionStatement() {return statement;}
2110 statement = LabeledStatement() {return statement;}
2111 | statement = Block() {return statement;}
2112 | statement = EmptyStatement() {return statement;}
2113 | statement = SwitchStatement() {return statement;}
2114 | statement = IfStatement() {return statement;}
2115 | statement = WhileStatement() {return statement;}
2116 | statement = DoStatement() {return statement;}
2117 | statement = ForStatement() {return statement;}
2118 | statement = ForeachStatement() {return statement;}
2119 | statement = ContinueStatement() {return statement;}
2120 | statement = ReturnStatement() {return statement;}
2121 | statement = EchoStatement() {return statement;}
2122 | [token=<AT>] statement = IncludeStatement()
2123 {if (token != null) {
2124 ((InclusionStatement)statement).silent = true;
2125 statement.sourceStart = token.sourceStart;
2128 | statement = StaticStatement() {return statement;}
2129 | statement = GlobalStatement() {return statement;}
2130 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2134 * A statement expression.
2136 * @return an expression
2138 Statement expressionStatement() :
2140 final Statement statement;
2144 statement = Expression()
2147 {statement.sourceEnd = token.sourceEnd;}
2148 } catch (ParseException e) {
2149 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2150 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2152 errorStart = statement.sourceEnd+1;
2153 errorEnd = statement.sourceEnd+1;
2154 processParseExceptionDebug(e);
2160 Define defineStatement() :
2162 Expression defineName,defineValue;
2163 final Token defineToken;
2168 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2171 {pos = token.sourceEnd+1;}
2172 } catch (ParseException e) {
2173 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2177 processParseExceptionDebug(e);
2180 defineName = Expression()
2181 {pos = defineName.sourceEnd+1;}
2182 } catch (ParseException e) {
2183 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2187 processParseExceptionDebug(e);
2188 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2192 {pos = defineName.sourceEnd+1;}
2193 } catch (ParseException e) {
2194 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2198 processParseExceptionDebug(e);
2201 defineValue = Expression()
2202 {pos = defineValue.sourceEnd+1;}
2203 } catch (ParseException e) {
2204 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2208 processParseExceptionDebug(e);
2209 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2213 {pos = token.sourceEnd+1;}
2214 } catch (ParseException e) {
2215 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2219 processParseExceptionDebug(e);
2221 {return new Define(currentSegment,
2224 defineToken.sourceStart,
2229 * A Normal statement.
2231 Statement Statement() :
2233 final Statement statement;
2236 statement = StatementNoBreak() {return statement;}
2237 | statement = BreakStatement() {return statement;}
2241 * An html block inside a php syntax.
2243 HTMLBlock htmlBlock() :
2245 final int startIndex = nodePtr;
2246 final AstNode[] blockNodes;
2252 {htmlStart = phpEnd.sourceEnd;}
2255 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2256 {createNewHTMLCode();}
2257 } catch (ParseException e) {
2258 errorMessage = "unexpected end of file , '<?php' expected";
2260 errorStart = e.currentToken.sourceStart;
2261 errorEnd = e.currentToken.sourceEnd;
2265 nbNodes = nodePtr - startIndex;
2269 blockNodes = new AstNode[nbNodes];
2270 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2271 nodePtr = startIndex;
2272 return new HTMLBlock(blockNodes);}
2276 * An include statement. It's "include" an expression;
2278 InclusionStatement IncludeStatement() :
2282 final InclusionStatement inclusionStatement;
2283 final Token token, token2;
2287 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2288 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2289 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2290 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2293 {pos = expr.sourceEnd;}
2294 } catch (ParseException e) {
2295 if (errorMessage != null) {
2298 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2300 errorStart = e.currentToken.next.sourceStart;
2301 errorEnd = e.currentToken.next.sourceEnd;
2302 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2303 processParseExceptionDebug(e);
2306 token2 = <SEMICOLON>
2307 {pos=token2.sourceEnd;}
2308 } catch (ParseException e) {
2309 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2311 errorStart = e.currentToken.next.sourceStart;
2312 errorEnd = e.currentToken.next.sourceEnd;
2313 processParseExceptionDebug(e);
2316 inclusionStatement = new InclusionStatement(currentSegment,
2321 currentSegment.add(inclusionStatement);
2322 return inclusionStatement;
2326 PrintExpression PrintExpression() :
2328 final Expression expr;
2329 final Token printToken;
2332 token = <PRINT> expr = Expression()
2333 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2336 ListExpression ListExpression() :
2338 Expression expr = null;
2339 final Expression expression;
2340 final ArrayList list = new ArrayList();
2342 final Token listToken, rParen;
2346 listToken = <LIST> {pos = listToken.sourceEnd;}
2348 token = <LPAREN> {pos = token.sourceEnd;}
2349 } catch (ParseException e) {
2350 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2352 errorStart = listToken.sourceEnd+1;
2353 errorEnd = listToken.sourceEnd+1;
2354 processParseExceptionDebug(e);
2357 expr = VariableDeclaratorId()
2358 {list.add(expr);pos = expr.sourceEnd;}
2360 {if (expr == null) list.add(null);}
2364 {pos = token.sourceEnd;}
2365 } catch (ParseException e) {
2366 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2370 processParseExceptionDebug(e);
2372 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2376 {pos = rParen.sourceEnd;}
2377 } catch (ParseException e) {
2378 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2382 processParseExceptionDebug(e);
2384 [ <ASSIGN> expression = Expression()
2386 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2388 return new ListExpression(vars,
2390 listToken.sourceStart,
2391 expression.sourceEnd);}
2394 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2396 return new ListExpression(vars,listToken.sourceStart,pos);}
2400 * An echo statement.
2401 * echo anyexpression (, otherexpression)*
2403 EchoStatement EchoStatement() :
2405 final ArrayList expressions = new ArrayList();
2408 Token token2 = null;
2411 token = <ECHO> expr = Expression()
2412 {expressions.add(expr);}
2414 <COMMA> expr = Expression()
2415 {expressions.add(expr);}
2418 token2 = <SEMICOLON>
2419 } catch (ParseException e) {
2420 if (e.currentToken.next.kind != 4) {
2421 errorMessage = "';' expected after 'echo' statement";
2423 errorStart = e.currentToken.sourceEnd;
2424 errorEnd = e.currentToken.sourceEnd;
2425 processParseExceptionDebug(e);
2429 final Expression[] exprs = new Expression[expressions.size()];
2430 expressions.toArray(exprs);
2431 if (token2 == null) {
2432 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2434 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2438 GlobalStatement GlobalStatement() :
2441 final ArrayList vars = new ArrayList();
2442 final GlobalStatement global;
2443 final Token token, token2;
2449 {vars.add(expr);pos = expr.sourceEnd+1;}
2452 {vars.add(expr);pos = expr.sourceEnd+1;}
2455 token2 = <SEMICOLON>
2456 {pos = token2.sourceEnd+1;}
2457 } catch (ParseException e) {
2458 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2462 processParseExceptionDebug(e);
2465 final Variable[] variables = new Variable[vars.size()];
2466 vars.toArray(variables);
2467 global = new GlobalStatement(currentSegment,
2471 currentSegment.add(global);
2475 StaticStatement StaticStatement() :
2477 final ArrayList vars = new ArrayList();
2478 VariableDeclaration expr;
2479 final Token token, token2;
2483 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2485 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2488 token2 = <SEMICOLON>
2489 {pos = token2.sourceEnd+1;}
2490 } catch (ParseException e) {
2491 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2495 processParseException(e);
2498 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2499 vars.toArray(variables);
2500 return new StaticStatement(variables,
2505 LabeledStatement LabeledStatement() :
2508 final Statement statement;
2511 label = <IDENTIFIER> <COLON> statement = Statement()
2512 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2524 final ArrayList list = new ArrayList();
2525 Statement statement;
2526 final Token token, token2;
2532 {pos = token.sourceEnd+1;start=token.sourceStart;}
2533 } catch (ParseException e) {
2534 errorMessage = "'{' expected";
2536 pos = this.token.sourceEnd+1;
2540 processParseExceptionDebug(e);
2542 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2543 | statement = htmlBlock() {if (statement != null) {
2544 list.add(statement);
2545 pos = statement.sourceEnd+1;
2547 pos = this.token.sourceEnd+1;
2552 {pos = token2.sourceEnd+1;}
2553 } catch (ParseException e) {
2554 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2558 processParseExceptionDebug(e);
2561 final Statement[] statements = new Statement[list.size()];
2562 list.toArray(statements);
2563 return new Block(statements,start,pos);}
2566 Statement BlockStatement() :
2568 final Statement statement;
2572 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2574 } catch (ParseException e) {
2575 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2577 errorStart = e.currentToken.sourceStart;
2578 errorEnd = e.currentToken.sourceEnd;
2581 | statement = ClassDeclaration() {return statement;}
2582 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2583 currentSegment.add((MethodDeclaration) statement);
2584 ((MethodDeclaration) statement).analyzeCode();
2589 * A Block statement that will not contain any 'break'
2591 Statement BlockStatementNoBreak() :
2593 final Statement statement;
2596 statement = StatementNoBreak() {return statement;}
2597 | statement = ClassDeclaration() {return statement;}
2598 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2599 ((MethodDeclaration) statement).analyzeCode();
2604 * used only by ForInit()
2606 Expression[] LocalVariableDeclaration() :
2608 final ArrayList list = new ArrayList();
2614 ( <COMMA> var = Expression() {list.add(var);})*
2616 final Expression[] vars = new Expression[list.size()];
2623 * used only by LocalVariableDeclaration().
2625 VariableDeclaration LocalVariableDeclarator() :
2627 final Variable varName;
2628 Expression initializer = null;
2631 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2633 if (initializer == null) {
2634 return new VariableDeclaration(currentSegment,
2636 varName.sourceStart,
2639 return new VariableDeclaration(currentSegment,
2642 VariableDeclaration.EQUAL,
2643 varName.sourceStart);
2647 EmptyStatement EmptyStatement() :
2653 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2657 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2659 Expression StatementExpression() :
2661 final Expression expr;
2662 final Token operator;
2665 expr = PreIncDecExpression() {return expr;}
2667 expr = PrimaryExpression()
2668 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2669 OperatorIds.PLUS_PLUS,
2670 operator.sourceEnd);}
2671 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2672 OperatorIds.MINUS_MINUS,
2673 operator.sourceEnd);}
2678 SwitchStatement SwitchStatement() :
2680 Expression variable;
2681 final AbstractCase[] cases;
2682 final Token switchToken,lparenToken,rparenToken;
2686 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2688 lparenToken = <LPAREN>
2689 {pos = lparenToken.sourceEnd+1;}
2690 } catch (ParseException e) {
2691 errorMessage = "'(' expected after 'switch'";
2695 processParseExceptionDebug(e);
2698 variable = Expression() {pos = variable.sourceEnd+1;}
2699 } catch (ParseException e) {
2700 if (errorMessage != null) {
2703 errorMessage = "expression expected";
2707 processParseExceptionDebug(e);
2708 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2711 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2712 } catch (ParseException e) {
2713 errorMessage = "')' expected";
2717 processParseExceptionDebug(e);
2719 ( cases = switchStatementBrace()
2720 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2721 {return new SwitchStatement(variable,
2723 switchToken.sourceStart,
2724 this.token.sourceEnd);}
2727 AbstractCase[] switchStatementBrace() :
2730 final ArrayList cases = new ArrayList();
2735 token = <LBRACE> {pos = token.sourceEnd;}
2736 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2739 {pos = token.sourceEnd;}
2740 } catch (ParseException e) {
2741 errorMessage = "'}' expected";
2745 processParseExceptionDebug(e);
2748 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2749 cases.toArray(abcase);
2755 * A Switch statement with : ... endswitch;
2756 * @param start the begin offset of the switch
2757 * @param end the end offset of the switch
2759 AbstractCase[] switchStatementColon(final int start, final int end) :
2762 final ArrayList cases = new ArrayList();
2767 token = <COLON> {pos = token.sourceEnd;}
2769 setMarker(fileToParse,
2770 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2774 "Line " + token.beginLine);
2775 } catch (CoreException e) {
2776 PHPeclipsePlugin.log(e);
2778 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2780 token = <ENDSWITCH> {pos = token.sourceEnd;}
2781 } catch (ParseException e) {
2782 errorMessage = "'endswitch' expected";
2786 processParseExceptionDebug(e);
2789 token = <SEMICOLON> {pos = token.sourceEnd;}
2790 } catch (ParseException e) {
2791 errorMessage = "';' expected after 'endswitch' keyword";
2795 processParseExceptionDebug(e);
2798 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2799 cases.toArray(abcase);
2804 AbstractCase switchLabel0() :
2806 final Expression expr;
2807 Statement statement;
2808 final ArrayList stmts = new ArrayList();
2809 final Token token = this.token;
2810 final int start = this.token.next.sourceStart;
2813 expr = SwitchLabel()
2814 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2815 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2816 | statement = BreakStatement() {stmts.add(statement);})*
2817 //[ statement = BreakStatement() {stmts.add(statement);}]
2819 final int listSize = stmts.size();
2820 final Statement[] stmtsArray = new Statement[listSize];
2821 stmts.toArray(stmtsArray);
2822 if (expr == null) {//it's a default
2823 final int end = this.token.next.sourceStart;
2824 return new DefaultCase(stmtsArray,start,end);
2826 if (listSize != 0) {
2827 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2829 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2836 * case Expression() :
2838 * @return the if it was a case and null if not
2840 Expression SwitchLabel() :
2842 final Expression expr;
2848 } catch (ParseException e) {
2849 if (errorMessage != null) throw e;
2850 errorMessage = "expression expected after 'case' keyword";
2852 errorStart = token.sourceEnd +1;
2853 errorEnd = token.sourceEnd +1;
2858 } catch (ParseException e) {
2859 errorMessage = "':' expected after case expression";
2861 errorStart = expr.sourceEnd+1;
2862 errorEnd = expr.sourceEnd+1;
2863 processParseExceptionDebug(e);
2870 } catch (ParseException e) {
2871 errorMessage = "':' expected after 'default' keyword";
2873 errorStart = token.sourceEnd+1;
2874 errorEnd = token.sourceEnd+1;
2875 processParseExceptionDebug(e);
2880 Break BreakStatement() :
2882 Expression expression = null;
2883 final Token token, token2;
2887 token = <BREAK> {pos = token.sourceEnd+1;}
2888 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2890 token2 = <SEMICOLON>
2891 {pos = token2.sourceEnd;}
2892 } catch (ParseException e) {
2893 errorMessage = "';' expected after 'break' keyword";
2897 processParseExceptionDebug(e);
2899 {return new Break(expression, token.sourceStart, pos);}
2902 IfStatement IfStatement() :
2904 final Expression condition;
2905 final IfStatement ifStatement;
2909 token = <IF> condition = Condition("if")
2910 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2911 {return ifStatement;}
2915 Expression Condition(final String keyword) :
2917 final Expression condition;
2922 } catch (ParseException e) {
2923 errorMessage = "'(' expected after " + keyword + " keyword";
2925 errorStart = this.token.sourceEnd + 1;
2926 errorEnd = this.token.sourceEnd + 1;
2927 processParseExceptionDebug(e);
2929 condition = Expression()
2932 } catch (ParseException e) {
2933 errorMessage = "')' expected after " + keyword + " keyword";
2935 errorStart = condition.sourceEnd+1;
2936 errorEnd = condition.sourceEnd+1;
2937 processParseExceptionDebug(e);
2942 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2944 Statement statement;
2945 final Statement stmt;
2946 final Statement[] statementsArray;
2947 ElseIf elseifStatement;
2948 Else elseStatement = null;
2949 final ArrayList stmts;
2950 final ArrayList elseIfList = new ArrayList();
2951 final ElseIf[] elseIfs;
2952 int pos = jj_input_stream.getPosition();
2953 final int endStatements;
2957 {stmts = new ArrayList();}
2958 ( statement = Statement() {stmts.add(statement);}
2959 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2960 {endStatements = jj_input_stream.getPosition();}
2961 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2962 [elseStatement = ElseStatementColon()]
2965 setMarker(fileToParse,
2966 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2970 "Line " + token.beginLine);
2971 } catch (CoreException e) {
2972 PHPeclipsePlugin.log(e);
2976 } catch (ParseException e) {
2977 errorMessage = "'endif' expected";
2979 errorStart = e.currentToken.sourceStart;
2980 errorEnd = e.currentToken.sourceEnd;
2985 } catch (ParseException e) {
2986 errorMessage = "';' expected after 'endif' keyword";
2988 errorStart = e.currentToken.sourceStart;
2989 errorEnd = e.currentToken.sourceEnd;
2993 elseIfs = new ElseIf[elseIfList.size()];
2994 elseIfList.toArray(elseIfs);
2995 if (stmts.size() == 1) {
2996 return new IfStatement(condition,
2997 (Statement) stmts.get(0),
3001 jj_input_stream.getPosition());
3003 statementsArray = new Statement[stmts.size()];
3004 stmts.toArray(statementsArray);
3005 return new IfStatement(condition,
3006 new Block(statementsArray,pos,endStatements),
3010 jj_input_stream.getPosition());
3015 (stmt = Statement() | stmt = htmlBlock())
3016 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3020 {pos = jj_input_stream.getPosition();}
3021 statement = Statement()
3022 {elseStatement = new Else(statement,pos,jj_input_stream.getPosition());}
3023 } catch (ParseException e) {
3024 if (errorMessage != null) {
3027 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3029 errorStart = e.currentToken.sourceStart;
3030 errorEnd = e.currentToken.sourceEnd;
3035 elseIfs = new ElseIf[elseIfList.size()];
3036 elseIfList.toArray(elseIfs);
3037 return new IfStatement(condition,
3042 jj_input_stream.getPosition());}
3045 ElseIf ElseIfStatementColon() :
3047 final Expression condition;
3048 Statement statement;
3049 final ArrayList list = new ArrayList();
3050 final Token elseifToken;
3053 elseifToken = <ELSEIF> condition = Condition("elseif")
3054 <COLON> ( statement = Statement() {list.add(statement);}
3055 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3057 final int sizeList = list.size();
3058 final Statement[] stmtsArray = new Statement[sizeList];
3059 list.toArray(stmtsArray);
3060 return new ElseIf(condition,stmtsArray ,
3061 elseifToken.sourceStart,
3062 stmtsArray[sizeList-1].sourceEnd);}
3065 Else ElseStatementColon() :
3067 Statement statement;
3068 final ArrayList list = new ArrayList();
3069 final Token elseToken;
3072 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3073 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3075 final int sizeList = list.size();
3076 final Statement[] stmtsArray = new Statement[sizeList];
3077 list.toArray(stmtsArray);
3078 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3081 ElseIf ElseIfStatement() :
3083 final Expression condition;
3084 //final Statement statement;
3085 final Token elseifToken;
3086 final Statement[] statement = new Statement[1];
3089 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3091 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3094 WhileStatement WhileStatement() :
3096 final Expression condition;
3097 final Statement action;
3098 final Token whileToken;
3101 whileToken = <WHILE>
3102 condition = Condition("while")
3103 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3104 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3107 Statement WhileStatement0(final int start, final int end) :
3109 Statement statement;
3110 final ArrayList stmts = new ArrayList();
3111 final int pos = jj_input_stream.getPosition();
3114 <COLON> (statement = Statement() {stmts.add(statement);})*
3116 setMarker(fileToParse,
3117 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3121 "Line " + token.beginLine);
3122 } catch (CoreException e) {
3123 PHPeclipsePlugin.log(e);
3127 } catch (ParseException e) {
3128 errorMessage = "'endwhile' expected";
3130 errorStart = e.currentToken.sourceStart;
3131 errorEnd = e.currentToken.sourceEnd;
3137 final Statement[] stmtsArray = new Statement[stmts.size()];
3138 stmts.toArray(stmtsArray);
3139 return new Block(stmtsArray,pos,jj_input_stream.getPosition());}
3140 } catch (ParseException e) {
3141 errorMessage = "';' expected after 'endwhile' keyword";
3143 errorStart = e.currentToken.sourceStart;
3144 errorEnd = e.currentToken.sourceEnd;
3148 statement = Statement()
3152 DoStatement DoStatement() :
3154 final Statement action;
3155 final Expression condition;
3157 Token token2 = null;
3160 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3162 token2 = <SEMICOLON>
3163 } catch (ParseException e) {
3164 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3166 errorStart = condition.sourceEnd+1;
3167 errorEnd = condition.sourceEnd+1;
3168 processParseExceptionDebug(e);
3171 if (token2 == null) {
3172 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3174 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3178 ForeachStatement ForeachStatement() :
3180 Statement statement = null;
3181 Expression expression = null;
3182 ArrayVariableDeclaration variable = null;
3184 Token lparenToken = null;
3185 Token asToken = null;
3186 Token rparenToken = null;
3190 foreachToken = <FOREACH>
3192 lparenToken = <LPAREN>
3193 {pos = lparenToken.sourceEnd+1;}
3194 } catch (ParseException e) {
3195 errorMessage = "'(' expected after 'foreach' keyword";
3197 errorStart = e.currentToken.sourceStart;
3198 errorEnd = e.currentToken.sourceEnd;
3199 processParseExceptionDebug(e);
3200 {pos = foreachToken.sourceEnd+1;}
3203 expression = Expression()
3204 {pos = expression.sourceEnd+1;}
3205 } catch (ParseException e) {
3206 errorMessage = "variable expected";
3208 errorStart = e.currentToken.sourceStart;
3209 errorEnd = e.currentToken.sourceEnd;
3210 processParseExceptionDebug(e);
3214 {pos = asToken.sourceEnd+1;}
3215 } catch (ParseException e) {
3216 errorMessage = "'as' expected";
3218 errorStart = e.currentToken.sourceStart;
3219 errorEnd = e.currentToken.sourceEnd;
3220 processParseExceptionDebug(e);
3223 variable = ArrayVariable()
3224 {pos = variable.sourceEnd+1;}
3225 } catch (ParseException e) {
3226 if (errorMessage != null) throw e;
3227 errorMessage = "variable expected";
3229 errorStart = e.currentToken.sourceStart;
3230 errorEnd = e.currentToken.sourceEnd;
3231 processParseExceptionDebug(e);
3234 rparenToken = <RPAREN>
3235 {pos = rparenToken.sourceEnd+1;}
3236 } catch (ParseException e) {
3237 errorMessage = "')' expected after 'foreach' keyword";
3239 errorStart = e.currentToken.sourceStart;
3240 errorEnd = e.currentToken.sourceEnd;
3241 processParseExceptionDebug(e);
3244 statement = Statement()
3245 {pos = statement.sourceEnd+1;}
3246 } catch (ParseException e) {
3247 if (errorMessage != null) throw e;
3248 errorMessage = "statement expected";
3250 errorStart = e.currentToken.sourceStart;
3251 errorEnd = e.currentToken.sourceEnd;
3252 processParseExceptionDebug(e);
3255 return new ForeachStatement(expression,
3258 foreachToken.sourceStart,
3264 * a for declaration.
3265 * @return a node representing the for statement
3267 ForStatement ForStatement() :
3269 final Token token,tokenEndFor,token2,tokenColon;
3271 Expression[] initializations = null;
3272 Expression condition = null;
3273 Expression[] increments = null;
3275 final ArrayList list = new ArrayList();
3281 } catch (ParseException e) {
3282 errorMessage = "'(' expected after 'for' keyword";
3284 errorStart = token.sourceEnd;
3285 errorEnd = token.sourceEnd +1;
3286 processParseExceptionDebug(e);
3288 [ initializations = ForInit() ] <SEMICOLON>
3289 [ condition = Expression() ] <SEMICOLON>
3290 [ increments = StatementExpressionList() ] <RPAREN>
3292 action = Statement()
3293 {return new ForStatement(initializations,
3300 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3301 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3304 setMarker(fileToParse,
3305 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3309 "Line " + token.beginLine);
3310 } catch (CoreException e) {
3311 PHPeclipsePlugin.log(e);
3315 tokenEndFor = <ENDFOR>
3316 {pos = tokenEndFor.sourceEnd+1;}
3317 } catch (ParseException e) {
3318 errorMessage = "'endfor' expected";
3322 processParseExceptionDebug(e);
3325 token2 = <SEMICOLON>
3326 {pos = token2.sourceEnd+1;}
3327 } catch (ParseException e) {
3328 errorMessage = "';' expected after 'endfor' keyword";
3332 processParseExceptionDebug(e);
3335 final Statement[] stmtsArray = new Statement[list.size()];
3336 list.toArray(stmtsArray);
3337 return new ForStatement(initializations,
3340 new Block(stmtsArray,
3341 stmtsArray[0].sourceStart,
3342 stmtsArray[stmtsArray.length-1].sourceEnd),
3348 Expression[] ForInit() :
3350 final Expression[] exprs;
3353 LOOKAHEAD(LocalVariableDeclaration())
3354 exprs = LocalVariableDeclaration()
3357 exprs = StatementExpressionList()
3361 Expression[] StatementExpressionList() :
3363 final ArrayList list = new ArrayList();
3364 final Expression expr;
3367 expr = Expression() {list.add(expr);}
3368 (<COMMA> Expression() {list.add(expr);})*
3370 final Expression[] exprsArray = new Expression[list.size()];
3371 list.toArray(exprsArray);
3376 Continue ContinueStatement() :
3378 Expression expr = null;
3380 Token token2 = null;
3383 token = <CONTINUE> [ expr = Expression() ]
3385 token2 = <SEMICOLON>
3386 } catch (ParseException e) {
3387 errorMessage = "';' expected after 'continue' statement";
3390 errorStart = token.sourceEnd+1;
3391 errorEnd = token.sourceEnd+1;
3393 errorStart = expr.sourceEnd+1;
3394 errorEnd = expr.sourceEnd+1;
3396 processParseExceptionDebug(e);
3399 if (token2 == null) {
3401 return new Continue(expr,token.sourceStart,token.sourceEnd);
3403 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3405 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3409 ReturnStatement ReturnStatement() :
3411 Expression expr = null;
3413 Token token2 = null;
3416 token = <RETURN> [ expr = Expression() ]
3418 token2 = <SEMICOLON>
3419 } catch (ParseException e) {
3420 errorMessage = "';' expected after 'return' statement";
3423 errorStart = token.sourceEnd+1;
3424 errorEnd = token.sourceEnd+1;
3426 errorStart = expr.sourceEnd+1;
3427 errorEnd = expr.sourceEnd+1;
3429 processParseExceptionDebug(e);
3432 if (token2 == null) {
3434 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3436 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3438 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);