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() :
1075 AbstractVariable var;
1082 var = VariableSuffix(var)
1087 } catch (ParseException e) {
1088 errorMessage = "'$' expected for variable identifier";
1090 errorStart = e.currentToken.sourceStart;
1091 errorEnd = e.currentToken.sourceEnd;
1096 Variable Variable() :
1098 Variable variable = null;
1102 token = <DOLLAR> variable = Var()
1110 Variable variable = null;
1111 final Token token,token2;
1112 ConstantIdentifier constant;
1113 Expression expression;
1116 token = <DOLLAR> variable = Var()
1117 {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1119 token = <LBRACE> expression = Expression() token2 = <RBRACE>
1121 return new Variable(expression,
1126 token = <IDENTIFIER>
1128 outlineInfo.addVariable('$' + token.image);
1129 return new Variable(token.image,token.sourceStart,token.sourceEnd);
1133 Expression VariableInitializer() :
1135 final Expression expr;
1136 final Token token, token2;
1142 token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1143 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1145 token2.sourceStart);}
1147 token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1148 {return new PrefixedUnaryExpression(new NumberLiteral(token),
1150 token2.sourceStart);}
1152 expr = ArrayDeclarator()
1155 token = <IDENTIFIER>
1156 {return new ConstantIdentifier(token);}
1159 ArrayVariableDeclaration ArrayVariable() :
1161 final Expression expr,expr2;
1166 <ARRAYASSIGN> expr2 = Expression()
1167 {return new ArrayVariableDeclaration(expr,expr2);}
1169 {return new ArrayVariableDeclaration(expr,jj_input_stream.getPosition());}
1172 ArrayVariableDeclaration[] ArrayInitializer() :
1174 ArrayVariableDeclaration expr;
1175 final ArrayList list = new ArrayList();
1180 expr = ArrayVariable()
1182 ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1187 <COMMA> {list.add(null);}
1191 final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1197 * A Method Declaration.
1198 * <b>function</b> MetodDeclarator() Block()
1200 MethodDeclaration MethodDeclaration() :
1202 final MethodDeclaration functionDeclaration;
1204 final OutlineableWithChildren seg = currentSegment;
1210 functionDeclaration = MethodDeclarator(token.sourceStart)
1211 {outlineInfo.addVariable(functionDeclaration.name);}
1212 } catch (ParseException e) {
1213 if (errorMessage != null) throw e;
1214 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1216 errorStart = e.currentToken.sourceStart;
1217 errorEnd = e.currentToken.sourceEnd;
1220 {currentSegment = functionDeclaration;}
1222 {functionDeclaration.statements = block.statements;
1223 currentSegment = seg;
1224 return functionDeclaration;}
1228 * A MethodDeclarator.
1229 * [&] IDENTIFIER(parameters ...).
1230 * @return a function description for the outline
1232 MethodDeclaration MethodDeclarator(final int start) :
1234 Token identifier = null;
1235 Token reference = null;
1236 final ArrayList formalParameters = new ArrayList();
1237 String identifierChar = SYNTAX_ERROR_CHAR;
1241 [reference = <BIT_AND> {end = reference.sourceEnd;}]
1243 identifier = <IDENTIFIER>
1245 identifierChar = identifier.image;
1246 end = identifier.sourceEnd;
1248 } catch (ParseException e) {
1249 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1251 errorStart = e.currentToken.sourceEnd;
1252 errorEnd = e.currentToken.next.sourceStart;
1253 processParseExceptionDebug(e);
1255 end = FormalParameters(formalParameters)
1257 int nameStart, nameEnd;
1258 if (identifier == null) {
1259 if (reference == null) {
1260 nameStart = start + 9;
1261 nameEnd = start + 10;
1263 nameStart = reference.sourceEnd + 1;
1264 nameEnd = reference.sourceEnd + 2;
1267 nameStart = identifier.sourceStart;
1268 nameEnd = identifier.sourceEnd;
1270 return new MethodDeclaration(currentSegment,
1282 * FormalParameters follows method identifier.
1283 * (FormalParameter())
1285 int FormalParameters(final ArrayList parameters) :
1287 VariableDeclaration var;
1289 Token tok = this.token;
1290 int end = tok.sourceEnd;
1295 {end = tok.sourceEnd;}
1296 } catch (ParseException e) {
1297 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1299 errorStart = e.currentToken.next.sourceStart;
1300 errorEnd = e.currentToken.next.sourceEnd;
1301 processParseExceptionDebug(e);
1304 var = FormalParameter()
1305 {parameters.add(var);end = var.sourceEnd;}
1307 <COMMA> var = FormalParameter()
1308 {parameters.add(var);end = var.sourceEnd;}
1313 {end = token.sourceEnd;}
1314 } catch (ParseException e) {
1315 errorMessage = "')' expected";
1317 errorStart = e.currentToken.next.sourceStart;
1318 errorEnd = e.currentToken.next.sourceEnd;
1319 processParseExceptionDebug(e);
1325 * A formal parameter.
1326 * $varname[=value] (,$varname[=value])
1328 VariableDeclaration FormalParameter() :
1330 final VariableDeclaration variableDeclaration;
1334 [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1336 outlineInfo.addVariable('$'+variableDeclaration.name());
1337 if (token != null) {
1338 variableDeclaration.setReference(true);
1340 return variableDeclaration;}
1343 ConstantIdentifier Type() :
1344 {final Token token;}
1346 token = <STRING> {return new ConstantIdentifier(token);}
1347 | token = <BOOL> {return new ConstantIdentifier(token);}
1348 | token = <BOOLEAN> {return new ConstantIdentifier(token);}
1349 | token = <REAL> {return new ConstantIdentifier(token);}
1350 | token = <DOUBLE> {return new ConstantIdentifier(token);}
1351 | token = <FLOAT> {return new ConstantIdentifier(token);}
1352 | token = <INT> {return new ConstantIdentifier(token);}
1353 | token = <INTEGER> {return new ConstantIdentifier(token);}
1354 | token = <OBJECT> {return new ConstantIdentifier(token);}
1357 Expression Expression() :
1359 final Expression expr;
1360 Expression initializer = null;
1361 int assignOperator = -1;
1365 expr = ConditionalExpression()
1367 assignOperator = AssignmentOperator()
1369 initializer = Expression()
1370 } catch (ParseException e) {
1371 if (errorMessage != null) {
1374 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1376 errorEnd = jj_input_stream.getPosition();
1381 if (assignOperator != -1) {// todo : change this, very very bad :(
1382 if (expr instanceof AbstractVariable) {
1383 return new VariableDeclaration(currentSegment,
1384 (AbstractVariable) expr,
1387 initializer.sourceEnd);
1389 String varName = expr.toStringExpression().substring(1);
1390 return new VariableDeclaration(currentSegment,
1391 new Variable(varName,
1395 initializer.sourceEnd);
1399 | expr = ExpressionWBang() {return expr;}
1402 Expression ExpressionWBang() :
1404 final Expression expr;
1408 token = <BANG> expr = ExpressionWBang()
1409 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1410 | expr = ExpressionNoBang() {return expr;}
1413 Expression ExpressionNoBang() :
1418 expr = ListExpression() {return expr;}
1420 expr = PrintExpression() {return expr;}
1424 * Any assignement operator.
1425 * @return the assignement operator id
1427 int AssignmentOperator() :
1430 <ASSIGN> {return VariableDeclaration.EQUAL;}
1431 | <STARASSIGN> {return VariableDeclaration.STAR_EQUAL;}
1432 | <SLASHASSIGN> {return VariableDeclaration.SLASH_EQUAL;}
1433 | <REMASSIGN> {return VariableDeclaration.REM_EQUAL;}
1434 | <PLUSASSIGN> {return VariableDeclaration.PLUS_EQUAL;}
1435 | <MINUSASSIGN> {return VariableDeclaration.MINUS_EQUAL;}
1436 | <LSHIFTASSIGN> {return VariableDeclaration.LSHIFT_EQUAL;}
1437 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1438 | <ANDASSIGN> {return VariableDeclaration.AND_EQUAL;}
1439 | <XORASSIGN> {return VariableDeclaration.XOR_EQUAL;}
1440 | <ORASSIGN> {return VariableDeclaration.OR_EQUAL;}
1441 | <DOTASSIGN> {return VariableDeclaration.DOT_EQUAL;}
1442 | <TILDEEQUAL> {return VariableDeclaration.TILDE_EQUAL;}
1445 Expression ConditionalExpression() :
1447 final Expression expr;
1448 Expression expr2 = null;
1449 Expression expr3 = null;
1452 expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1454 if (expr3 == null) {
1457 return new ConditionalExpression(expr,expr2,expr3);
1461 Expression ConditionalOrExpression() :
1463 Expression expr,expr2;
1467 expr = ConditionalAndExpression()
1470 <OR_OR> {operator = OperatorIds.OR_OR;}
1471 | <_ORL> {operator = OperatorIds.ORL;}
1473 expr2 = ConditionalAndExpression()
1475 expr = new BinaryExpression(expr,expr2,operator);
1481 Expression ConditionalAndExpression() :
1483 Expression expr,expr2;
1487 expr = ConcatExpression()
1489 ( <AND_AND> {operator = OperatorIds.AND_AND;}
1490 | <_ANDL> {operator = OperatorIds.ANDL;})
1491 expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1496 Expression ConcatExpression() :
1498 Expression expr,expr2;
1501 expr = InclusiveOrExpression()
1503 <DOT> expr2 = InclusiveOrExpression()
1504 {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1509 Expression InclusiveOrExpression() :
1511 Expression expr,expr2;
1514 expr = ExclusiveOrExpression()
1515 (<BIT_OR> expr2 = ExclusiveOrExpression()
1516 {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1521 Expression ExclusiveOrExpression() :
1523 Expression expr,expr2;
1526 expr = AndExpression()
1528 <XOR> expr2 = AndExpression()
1529 {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1534 Expression AndExpression() :
1536 Expression expr,expr2;
1539 expr = EqualityExpression()
1542 <BIT_AND> expr2 = EqualityExpression()
1543 {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1548 Expression EqualityExpression() :
1550 Expression expr,expr2;
1555 expr = RelationalExpression()
1557 ( token = <EQUAL_EQUAL> {operator = OperatorIds.EQUAL_EQUAL;}
1558 | token = <DIF> {operator = OperatorIds.DIF;}
1559 | token = <NOT_EQUAL> {operator = OperatorIds.DIF;}
1560 | token = <BANGDOUBLEEQUAL> {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1561 | token = <TRIPLEEQUAL> {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1564 expr2 = RelationalExpression()
1565 } catch (ParseException e) {
1566 if (errorMessage != null) {
1569 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1571 errorStart = token.sourceEnd +1;
1572 errorEnd = token.sourceEnd +1;
1573 expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1574 processParseExceptionDebug(e);
1577 expr = new BinaryExpression(expr,expr2,operator);
1583 Expression RelationalExpression() :
1585 Expression expr,expr2;
1589 expr = ShiftExpression()
1591 ( <LT> {operator = OperatorIds.LESS;}
1592 | <GT> {operator = OperatorIds.GREATER;}
1593 | <LE> {operator = OperatorIds.LESS_EQUAL;}
1594 | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1595 expr2 = ShiftExpression()
1596 {expr = new BinaryExpression(expr,expr2,operator);}
1601 Expression ShiftExpression() :
1603 Expression expr,expr2;
1607 expr = AdditiveExpression()
1609 ( <LSHIFT> {operator = OperatorIds.LEFT_SHIFT;}
1610 | <RSIGNEDSHIFT> {operator = OperatorIds.RIGHT_SHIFT;}
1611 | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1612 expr2 = AdditiveExpression()
1613 {expr = new BinaryExpression(expr,expr2,operator);}
1618 Expression AdditiveExpression() :
1620 Expression expr,expr2;
1624 expr = MultiplicativeExpression()
1627 ( <PLUS> {operator = OperatorIds.PLUS;}
1628 | <MINUS> {operator = OperatorIds.MINUS;}
1630 expr2 = MultiplicativeExpression()
1631 {expr = new BinaryExpression(expr,expr2,operator);}
1636 Expression MultiplicativeExpression() :
1638 Expression expr,expr2;
1643 expr = UnaryExpression()
1644 } catch (ParseException e) {
1645 if (errorMessage != null) throw e;
1646 errorMessage = "unexpected token '"+e.currentToken.next.image+'\'';
1648 errorStart = this.token.sourceStart;
1649 errorEnd = this.token.sourceEnd;
1653 ( <STAR> {operator = OperatorIds.MULTIPLY;}
1654 | <SLASH> {operator = OperatorIds.DIVIDE;}
1655 | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1656 expr2 = UnaryExpression()
1657 {expr = new BinaryExpression(expr,expr2,operator);}
1663 * An unary expression starting with @, & or nothing
1665 Expression UnaryExpression() :
1667 final Expression expr;
1670 /* <BIT_AND> expr = UnaryExpressionNoPrefix() //why did I had that ?
1671 {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1673 expr = AtNotTildeUnaryExpression() {return expr;}
1676 Expression AtNotTildeUnaryExpression() :
1678 final Expression expr;
1683 expr = AtNotTildeUnaryExpression()
1684 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1687 expr = AtNotTildeUnaryExpression()
1688 {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1691 expr = AtNotUnaryExpression()
1692 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1694 expr = UnaryExpressionNoPrefix()
1699 * An expression prefixed (or not) by one or more @ and !.
1700 * @return the expression
1702 Expression AtNotUnaryExpression() :
1704 final Expression expr;
1709 expr = AtNotUnaryExpression()
1710 {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1713 expr = AtNotUnaryExpression()
1714 {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1716 expr = UnaryExpressionNoPrefix()
1720 Expression UnaryExpressionNoPrefix() :
1722 final Expression expr;
1726 token = <PLUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1728 token.sourceStart);}
1730 token = <MINUS> expr = AtNotTildeUnaryExpression() {return new PrefixedUnaryExpression(expr,
1732 token.sourceStart);}
1734 expr = PreIncDecExpression()
1737 expr = UnaryExpressionNotPlusMinus()
1742 Expression PreIncDecExpression() :
1744 final Expression expr;
1750 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1752 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1754 expr = PrimaryExpression()
1755 {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1758 Expression UnaryExpressionNotPlusMinus() :
1760 final Expression expr;
1763 LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1764 expr = CastExpression() {return expr;}
1765 | expr = PostfixExpression() {return expr;}
1766 | expr = Literal() {return expr;}
1767 | <LPAREN> expr = Expression()
1770 } catch (ParseException e) {
1771 errorMessage = "')' expected";
1773 errorStart = expr.sourceEnd +1;
1774 errorEnd = expr.sourceEnd +1;
1775 processParseExceptionDebug(e);
1780 CastExpression CastExpression() :
1782 final ConstantIdentifier type;
1783 final Expression expr;
1784 final Token token,token1;
1791 token = <ARRAY> {type = new ConstantIdentifier(token);}
1793 <RPAREN> expr = UnaryExpression()
1794 {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1797 Expression PostfixExpression() :
1799 final Expression expr;
1804 expr = PrimaryExpression()
1806 token = <PLUS_PLUS> {operator = OperatorIds.PLUS_PLUS;}
1808 token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1811 if (operator == -1) {
1814 return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1818 Expression PrimaryExpression() :
1824 [token = <BIT_AND>] expr = refPrimaryExpression(token)
1827 expr = ArrayDeclarator()
1831 Expression refPrimaryExpression(final Token reference) :
1834 Expression expr2 = null;
1835 final Token identifier;
1838 identifier = <IDENTIFIER>
1840 expr = new ConstantIdentifier(identifier);
1843 <STATICCLASSACCESS> expr2 = ClassIdentifier()
1844 {expr = new ClassAccess(expr,
1846 ClassAccess.STATIC);}
1848 [ expr2 = Arguments(expr) ]
1850 if (expr2 == null) {
1851 if (reference != null) {
1852 ParseException e = generateParseException();
1853 errorMessage = "you cannot use a constant by reference";
1855 errorStart = reference.sourceStart;
1856 errorEnd = reference.sourceEnd;
1857 processParseExceptionDebug(e);
1864 expr = VariableDeclaratorId() //todo use the reference parameter ...
1865 [ expr = Arguments(expr) ]
1869 expr = ClassIdentifier()
1872 if (reference == null) {
1873 start = token.sourceStart;
1875 start = reference.sourceStart;
1877 expr = new ClassInstantiation(expr,
1881 [ expr = Arguments(expr) ]
1886 * An array declarator.
1890 ArrayInitializer ArrayDeclarator() :
1892 final ArrayVariableDeclaration[] vars;
1896 token = <ARRAY> vars = ArrayInitializer()
1897 {return new ArrayInitializer(vars,
1899 this.token.sourceEnd);}
1902 Expression ClassIdentifier():
1904 final Expression expr;
1908 token = <IDENTIFIER> {return new ConstantIdentifier(token);}
1909 | expr = Type() {return expr;}
1910 | expr = VariableDeclaratorId() {return expr;}
1914 * Used by Variabledeclaratorid and primarysuffix
1916 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1918 Expression expression = null;
1919 final Token classAccessToken,lbrace,rbrace;
1924 classAccessToken = <CLASSACCESS>
1927 lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1929 expression = new Variable(expression,
1934 token = <IDENTIFIER>
1935 {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1937 expression = Variable()
1939 } catch (ParseException e) {
1940 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1942 errorStart = classAccessToken.sourceEnd +1;
1943 errorEnd = classAccessToken.sourceEnd +1;
1944 processParseExceptionDebug(e);
1946 {return new ClassAccess(prefix,
1948 ClassAccess.NORMAL);}
1950 token = <LBRACKET> {pos = token.sourceEnd+1;}
1951 [ expression = Expression() {pos = expression.sourceEnd+1;}
1952 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1955 {pos = token.sourceEnd;}
1956 } catch (ParseException e) {
1957 errorMessage = "']' expected";
1961 processParseExceptionDebug(e);
1963 {return new ArrayDeclarator(prefix,expression,pos);}
1965 token = <LBRACE> {pos = token.sourceEnd+1;}
1966 [ expression = Expression() {pos = expression.sourceEnd+1;}
1967 | expression = Type() {pos = expression.sourceEnd+1;}] //Not good
1970 {pos = token.sourceEnd;}
1971 } catch (ParseException e) {
1972 errorMessage = "']' expected";
1976 processParseExceptionDebug(e);
1978 {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1984 StringLiteral literal;
1987 token = <INTEGER_LITERAL> {return new NumberLiteral(token);}
1988 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1989 | token = <STRING_LITERAL> {return new StringLiteral(token);}
1990 | token = <TRUE> {return new TrueLiteral(token);}
1991 | token = <FALSE> {return new FalseLiteral(token);}
1992 | token = <NULL> {return new NullLiteral(token);}
1993 | literal = evaluableString() {return literal;}
1996 StringLiteral evaluableString() :
1998 ArrayList list = new ArrayList();
2000 Token token,lbrace,rbrace;
2001 AbstractVariable var;
2005 start = <DOUBLEQUOTE>
2009 token = <IDENTIFIER> {list.add(new Variable(token.image,
2015 {list.add(new Variable(token.image,
2021 end = <DOUBLEQUOTE2>
2023 AbstractVariable[] vars = new AbstractVariable[list.size()];
2025 return new StringLiteral(jj_input_stream.getCurrentBuffer().substring(start.sourceEnd,end.sourceStart),
2032 FunctionCall Arguments(final Expression func) :
2034 Expression[] args = null;
2035 final Token token,lparen;
2038 lparen = <LPAREN> [ args = ArgumentList() ]
2041 {return new FunctionCall(func,args,token.sourceEnd);}
2042 } catch (ParseException e) {
2043 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2046 errorStart = lparen.sourceEnd+1;
2047 errorEnd = lparen.sourceEnd+2;
2049 errorStart = args[args.length-1].sourceEnd+1;
2050 errorEnd = args[args.length-1].sourceEnd+2;
2052 processParseExceptionDebug(e);
2055 int sourceEnd = (args == null && args.length != 0) ? lparen.sourceEnd+1 : args[args.length-1].sourceEnd;
2056 return new FunctionCall(func,args,sourceEnd);}
2060 * An argument list is a list of arguments separated by comma :
2061 * argumentDeclaration() (, argumentDeclaration)*
2062 * @return an array of arguments
2064 Expression[] ArgumentList() :
2067 final ArrayList list = new ArrayList();
2073 {list.add(arg);pos = arg.sourceEnd;}
2074 ( token = <COMMA> {pos = token.sourceEnd;}
2078 pos = arg.sourceEnd;}
2079 } catch (ParseException e) {
2080 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2084 processParseException(e);
2088 final Expression[] arguments = new Expression[list.size()];
2089 list.toArray(arguments);
2094 * A Statement without break.
2095 * @return a statement
2097 Statement StatementNoBreak() :
2099 final Statement statement;
2104 statement = expressionStatement() {return statement;}
2106 statement = LabeledStatement() {return statement;}
2107 | statement = Block() {return statement;}
2108 | statement = EmptyStatement() {return statement;}
2109 | statement = SwitchStatement() {return statement;}
2110 | statement = IfStatement() {return statement;}
2111 | statement = WhileStatement() {return statement;}
2112 | statement = DoStatement() {return statement;}
2113 | statement = ForStatement() {return statement;}
2114 | statement = ForeachStatement() {return statement;}
2115 | statement = ContinueStatement() {return statement;}
2116 | statement = ReturnStatement() {return statement;}
2117 | statement = EchoStatement() {return statement;}
2118 | [token=<AT>] statement = IncludeStatement()
2119 {if (token != null) {
2120 ((InclusionStatement)statement).silent = true;
2121 statement.sourceStart = token.sourceStart;
2124 | statement = StaticStatement() {return statement;}
2125 | statement = GlobalStatement() {return statement;}
2126 | statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;}
2130 * A statement expression.
2132 * @return an expression
2134 Statement expressionStatement() :
2136 final Statement statement;
2140 statement = Expression()
2143 {statement.sourceEnd = token.sourceEnd;}
2144 } catch (ParseException e) {
2145 if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2146 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2148 errorStart = statement.sourceEnd+1;
2149 errorEnd = statement.sourceEnd+1;
2150 processParseExceptionDebug(e);
2156 Define defineStatement() :
2158 Expression defineName,defineValue;
2159 final Token defineToken;
2164 defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2167 {pos = token.sourceEnd+1;}
2168 } catch (ParseException e) {
2169 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2173 processParseExceptionDebug(e);
2176 defineName = Expression()
2177 {pos = defineName.sourceEnd+1;}
2178 } catch (ParseException e) {
2179 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2183 processParseExceptionDebug(e);
2184 defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2188 {pos = defineName.sourceEnd+1;}
2189 } catch (ParseException e) {
2190 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2194 processParseExceptionDebug(e);
2197 defineValue = Expression()
2198 {pos = defineValue.sourceEnd+1;}
2199 } catch (ParseException e) {
2200 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2204 processParseExceptionDebug(e);
2205 defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2209 {pos = token.sourceEnd+1;}
2210 } catch (ParseException e) {
2211 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2215 processParseExceptionDebug(e);
2217 {return new Define(currentSegment,
2220 defineToken.sourceStart,
2225 * A Normal statement.
2227 Statement Statement() :
2229 final Statement statement;
2232 statement = StatementNoBreak() {return statement;}
2233 | statement = BreakStatement() {return statement;}
2237 * An html block inside a php syntax.
2239 HTMLBlock htmlBlock() :
2241 final int startIndex = nodePtr;
2242 final AstNode[] blockNodes;
2248 {htmlStart = phpEnd.sourceEnd;}
2251 (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2252 {createNewHTMLCode();}
2253 } catch (ParseException e) {
2254 errorMessage = "unexpected end of file , '<?php' expected";
2256 errorStart = e.currentToken.sourceStart;
2257 errorEnd = e.currentToken.sourceEnd;
2261 nbNodes = nodePtr - startIndex;
2265 blockNodes = new AstNode[nbNodes];
2266 System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2267 nodePtr = startIndex;
2268 return new HTMLBlock(blockNodes);}
2272 * An include statement. It's "include" an expression;
2274 InclusionStatement IncludeStatement() :
2278 final InclusionStatement inclusionStatement;
2279 final Token token, token2;
2283 ( token = <REQUIRE> {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2284 | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2285 | token = <INCLUDE> {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2286 | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2289 {pos = expr.sourceEnd;}
2290 } catch (ParseException e) {
2291 if (errorMessage != null) {
2294 errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2296 errorStart = e.currentToken.next.sourceStart;
2297 errorEnd = e.currentToken.next.sourceEnd;
2298 expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2299 processParseExceptionDebug(e);
2302 token2 = <SEMICOLON>
2303 {pos=token2.sourceEnd;}
2304 } catch (ParseException e) {
2305 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2307 errorStart = e.currentToken.next.sourceStart;
2308 errorEnd = e.currentToken.next.sourceEnd;
2309 processParseExceptionDebug(e);
2312 inclusionStatement = new InclusionStatement(currentSegment,
2317 currentSegment.add(inclusionStatement);
2318 return inclusionStatement;
2322 PrintExpression PrintExpression() :
2324 final Expression expr;
2325 final Token printToken;
2328 token = <PRINT> expr = Expression()
2329 {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2332 ListExpression ListExpression() :
2334 Expression expr = null;
2335 final Expression expression;
2336 final ArrayList list = new ArrayList();
2338 final Token listToken, rParen;
2342 listToken = <LIST> {pos = listToken.sourceEnd;}
2344 token = <LPAREN> {pos = token.sourceEnd;}
2345 } catch (ParseException e) {
2346 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2348 errorStart = listToken.sourceEnd+1;
2349 errorEnd = listToken.sourceEnd+1;
2350 processParseExceptionDebug(e);
2353 expr = VariableDeclaratorId()
2354 {list.add(expr);pos = expr.sourceEnd;}
2356 {if (expr == null) list.add(null);}
2360 {pos = token.sourceEnd;}
2361 } catch (ParseException e) {
2362 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2366 processParseExceptionDebug(e);
2368 [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2372 {pos = rParen.sourceEnd;}
2373 } catch (ParseException e) {
2374 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2378 processParseExceptionDebug(e);
2380 [ <ASSIGN> expression = Expression()
2382 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2384 return new ListExpression(vars,
2386 listToken.sourceStart,
2387 expression.sourceEnd);}
2390 final AbstractVariable[] vars = new AbstractVariable[list.size()];
2392 return new ListExpression(vars,listToken.sourceStart,pos);}
2396 * An echo statement.
2397 * echo anyexpression (, otherexpression)*
2399 EchoStatement EchoStatement() :
2401 final ArrayList expressions = new ArrayList();
2404 Token token2 = null;
2407 token = <ECHO> expr = Expression()
2408 {expressions.add(expr);}
2410 <COMMA> expr = Expression()
2411 {expressions.add(expr);}
2414 token2 = <SEMICOLON>
2415 } catch (ParseException e) {
2416 if (e.currentToken.next.kind != 4) {
2417 errorMessage = "';' expected after 'echo' statement";
2419 errorStart = e.currentToken.sourceEnd;
2420 errorEnd = e.currentToken.sourceEnd;
2421 processParseExceptionDebug(e);
2425 final Expression[] exprs = new Expression[expressions.size()];
2426 expressions.toArray(exprs);
2427 if (token2 == null) {
2428 return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2430 return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2434 GlobalStatement GlobalStatement() :
2437 final ArrayList vars = new ArrayList();
2438 final GlobalStatement global;
2439 final Token token, token2;
2445 {vars.add(expr);pos = expr.sourceEnd+1;}
2448 {vars.add(expr);pos = expr.sourceEnd+1;}
2451 token2 = <SEMICOLON>
2452 {pos = token2.sourceEnd+1;}
2453 } catch (ParseException e) {
2454 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2458 processParseExceptionDebug(e);
2461 final Variable[] variables = new Variable[vars.size()];
2462 vars.toArray(variables);
2463 global = new GlobalStatement(currentSegment,
2467 currentSegment.add(global);
2471 StaticStatement StaticStatement() :
2473 final ArrayList vars = new ArrayList();
2474 VariableDeclaration expr;
2475 final Token token, token2;
2479 token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2481 <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2484 token2 = <SEMICOLON>
2485 {pos = token2.sourceEnd+1;}
2486 } catch (ParseException e) {
2487 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2491 processParseException(e);
2494 final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2495 vars.toArray(variables);
2496 return new StaticStatement(variables,
2501 LabeledStatement LabeledStatement() :
2504 final Statement statement;
2507 label = <IDENTIFIER> <COLON> statement = Statement()
2508 {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2520 final ArrayList list = new ArrayList();
2521 Statement statement;
2522 final Token token, token2;
2528 {pos = token.sourceEnd+1;start=token.sourceStart;}
2529 } catch (ParseException e) {
2530 errorMessage = "'{' expected";
2532 pos = this.token.sourceEnd+1;
2536 processParseExceptionDebug(e);
2538 ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2539 | statement = htmlBlock() {if (statement != null) {
2540 list.add(statement);
2541 pos = statement.sourceEnd+1;
2543 pos = this.token.sourceEnd+1;
2548 {pos = token2.sourceEnd+1;}
2549 } catch (ParseException e) {
2550 errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2554 processParseExceptionDebug(e);
2557 final Statement[] statements = new Statement[list.size()];
2558 list.toArray(statements);
2559 return new Block(statements,start,pos);}
2562 Statement BlockStatement() :
2564 final Statement statement;
2568 statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2570 } catch (ParseException e) {
2571 errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2573 errorStart = e.currentToken.sourceStart;
2574 errorEnd = e.currentToken.sourceEnd;
2577 | statement = ClassDeclaration() {return statement;}
2578 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2579 currentSegment.add((MethodDeclaration) statement);
2580 ((MethodDeclaration) statement).analyzeCode();
2585 * A Block statement that will not contain any 'break'
2587 Statement BlockStatementNoBreak() :
2589 final Statement statement;
2592 statement = StatementNoBreak() {return statement;}
2593 | statement = ClassDeclaration() {return statement;}
2594 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2595 ((MethodDeclaration) statement).analyzeCode();
2600 * used only by ForInit()
2602 Expression[] LocalVariableDeclaration() :
2604 final ArrayList list = new ArrayList();
2610 ( <COMMA> var = Expression() {list.add(var);})*
2612 final Expression[] vars = new Expression[list.size()];
2619 * used only by LocalVariableDeclaration().
2621 VariableDeclaration LocalVariableDeclarator() :
2623 final Variable varName;
2624 Expression initializer = null;
2627 varName = Variable() [ <ASSIGN> initializer = Expression() ]
2629 if (initializer == null) {
2630 return new VariableDeclaration(currentSegment,
2632 varName.sourceStart,
2635 return new VariableDeclaration(currentSegment,
2638 VariableDeclaration.EQUAL,
2639 varName.sourceStart);
2643 EmptyStatement EmptyStatement() :
2649 {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2653 * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2655 Expression StatementExpression() :
2657 final Expression expr;
2658 final Token operator;
2661 expr = PreIncDecExpression() {return expr;}
2663 expr = PrimaryExpression()
2664 [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2665 OperatorIds.PLUS_PLUS,
2666 operator.sourceEnd);}
2667 | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2668 OperatorIds.MINUS_MINUS,
2669 operator.sourceEnd);}
2674 SwitchStatement SwitchStatement() :
2676 Expression variable;
2677 final AbstractCase[] cases;
2678 final Token switchToken,lparenToken,rparenToken;
2682 switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2684 lparenToken = <LPAREN>
2685 {pos = lparenToken.sourceEnd+1;}
2686 } catch (ParseException e) {
2687 errorMessage = "'(' expected after 'switch'";
2691 processParseExceptionDebug(e);
2694 variable = Expression() {pos = variable.sourceEnd+1;}
2695 } catch (ParseException e) {
2696 if (errorMessage != null) {
2699 errorMessage = "expression expected";
2703 processParseExceptionDebug(e);
2704 variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2707 rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2708 } catch (ParseException e) {
2709 errorMessage = "')' expected";
2713 processParseExceptionDebug(e);
2715 ( cases = switchStatementBrace()
2716 | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2717 {return new SwitchStatement(variable,
2719 switchToken.sourceStart,
2720 this.token.sourceEnd);}
2723 AbstractCase[] switchStatementBrace() :
2726 final ArrayList cases = new ArrayList();
2731 token = <LBRACE> {pos = token.sourceEnd;}
2732 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2735 {pos = token.sourceEnd;}
2736 } catch (ParseException e) {
2737 errorMessage = "'}' expected";
2741 processParseExceptionDebug(e);
2744 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2745 cases.toArray(abcase);
2751 * A Switch statement with : ... endswitch;
2752 * @param start the begin offset of the switch
2753 * @param end the end offset of the switch
2755 AbstractCase[] switchStatementColon(final int start, final int end) :
2758 final ArrayList cases = new ArrayList();
2763 token = <COLON> {pos = token.sourceEnd;}
2765 setMarker(fileToParse,
2766 "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2770 "Line " + token.beginLine);
2771 } catch (CoreException e) {
2772 PHPeclipsePlugin.log(e);
2774 ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2776 token = <ENDSWITCH> {pos = token.sourceEnd;}
2777 } catch (ParseException e) {
2778 errorMessage = "'endswitch' expected";
2782 processParseExceptionDebug(e);
2785 token = <SEMICOLON> {pos = token.sourceEnd;}
2786 } catch (ParseException e) {
2787 errorMessage = "';' expected after 'endswitch' keyword";
2791 processParseExceptionDebug(e);
2794 final AbstractCase[] abcase = new AbstractCase[cases.size()];
2795 cases.toArray(abcase);
2800 AbstractCase switchLabel0() :
2802 final Expression expr;
2803 Statement statement;
2804 final ArrayList stmts = new ArrayList();
2805 final Token token = this.token;
2806 final int start = this.token.next.sourceStart;
2809 expr = SwitchLabel()
2810 ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2811 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}}
2812 | statement = BreakStatement() {stmts.add(statement);})*
2813 //[ statement = BreakStatement() {stmts.add(statement);}]
2815 final int listSize = stmts.size();
2816 final Statement[] stmtsArray = new Statement[listSize];
2817 stmts.toArray(stmtsArray);
2818 if (expr == null) {//it's a default
2819 final int end = this.token.next.sourceStart;
2820 return new DefaultCase(stmtsArray,start,end);
2822 if (listSize != 0) {
2823 return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2825 return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2832 * case Expression() :
2834 * @return the if it was a case and null if not
2836 Expression SwitchLabel() :
2838 final Expression expr;
2844 } catch (ParseException e) {
2845 if (errorMessage != null) throw e;
2846 errorMessage = "expression expected after 'case' keyword";
2848 errorStart = token.sourceEnd +1;
2849 errorEnd = token.sourceEnd +1;
2854 } catch (ParseException e) {
2855 errorMessage = "':' expected after case expression";
2857 errorStart = expr.sourceEnd+1;
2858 errorEnd = expr.sourceEnd+1;
2859 processParseExceptionDebug(e);
2866 } catch (ParseException e) {
2867 errorMessage = "':' expected after 'default' keyword";
2869 errorStart = token.sourceEnd+1;
2870 errorEnd = token.sourceEnd+1;
2871 processParseExceptionDebug(e);
2876 Break BreakStatement() :
2878 Expression expression = null;
2879 final Token token, token2;
2883 token = <BREAK> {pos = token.sourceEnd+1;}
2884 [ expression = Expression() {pos = expression.sourceEnd+1;}]
2886 token2 = <SEMICOLON>
2887 {pos = token2.sourceEnd;}
2888 } catch (ParseException e) {
2889 errorMessage = "';' expected after 'break' keyword";
2893 processParseExceptionDebug(e);
2895 {return new Break(expression, token.sourceStart, pos);}
2898 IfStatement IfStatement() :
2900 final Expression condition;
2901 final IfStatement ifStatement;
2905 token = <IF> condition = Condition("if")
2906 ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2907 {return ifStatement;}
2911 Expression Condition(final String keyword) :
2913 final Expression condition;
2918 } catch (ParseException e) {
2919 errorMessage = "'(' expected after " + keyword + " keyword";
2921 errorStart = this.token.sourceEnd + 1;
2922 errorEnd = this.token.sourceEnd + 1;
2923 processParseExceptionDebug(e);
2925 condition = Expression()
2928 } catch (ParseException e) {
2929 errorMessage = "')' expected after " + keyword + " keyword";
2931 errorStart = condition.sourceEnd+1;
2932 errorEnd = condition.sourceEnd+1;
2933 processParseExceptionDebug(e);
2938 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2940 Statement statement;
2941 final Statement stmt;
2942 final Statement[] statementsArray;
2943 ElseIf elseifStatement;
2944 Else elseStatement = null;
2945 final ArrayList stmts;
2946 final ArrayList elseIfList = new ArrayList();
2947 final ElseIf[] elseIfs;
2948 int pos = jj_input_stream.getPosition();
2949 final int endStatements;
2953 {stmts = new ArrayList();}
2954 ( statement = Statement() {stmts.add(statement);}
2955 | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2956 {endStatements = jj_input_stream.getPosition();}
2957 (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2958 [elseStatement = ElseStatementColon()]
2961 setMarker(fileToParse,
2962 "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2966 "Line " + token.beginLine);
2967 } catch (CoreException e) {
2968 PHPeclipsePlugin.log(e);
2972 } catch (ParseException e) {
2973 errorMessage = "'endif' expected";
2975 errorStart = e.currentToken.sourceStart;
2976 errorEnd = e.currentToken.sourceEnd;
2981 } catch (ParseException e) {
2982 errorMessage = "';' expected after 'endif' keyword";
2984 errorStart = e.currentToken.sourceStart;
2985 errorEnd = e.currentToken.sourceEnd;
2989 elseIfs = new ElseIf[elseIfList.size()];
2990 elseIfList.toArray(elseIfs);
2991 if (stmts.size() == 1) {
2992 return new IfStatement(condition,
2993 (Statement) stmts.get(0),
2997 jj_input_stream.getPosition());
2999 statementsArray = new Statement[stmts.size()];
3000 stmts.toArray(statementsArray);
3001 return new IfStatement(condition,
3002 new Block(statementsArray,pos,endStatements),
3006 jj_input_stream.getPosition());
3011 (stmt = Statement() | stmt = htmlBlock())
3012 ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3016 {pos = jj_input_stream.getPosition();}
3017 statement = Statement()
3018 {elseStatement = new Else(statement,pos,jj_input_stream.getPosition());}
3019 } catch (ParseException e) {
3020 if (errorMessage != null) {
3023 errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3025 errorStart = e.currentToken.sourceStart;
3026 errorEnd = e.currentToken.sourceEnd;
3031 elseIfs = new ElseIf[elseIfList.size()];
3032 elseIfList.toArray(elseIfs);
3033 return new IfStatement(condition,
3038 jj_input_stream.getPosition());}
3041 ElseIf ElseIfStatementColon() :
3043 final Expression condition;
3044 Statement statement;
3045 final ArrayList list = new ArrayList();
3046 final Token elseifToken;
3049 elseifToken = <ELSEIF> condition = Condition("elseif")
3050 <COLON> ( statement = Statement() {list.add(statement);}
3051 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3053 final int sizeList = list.size();
3054 final Statement[] stmtsArray = new Statement[sizeList];
3055 list.toArray(stmtsArray);
3056 return new ElseIf(condition,stmtsArray ,
3057 elseifToken.sourceStart,
3058 stmtsArray[sizeList-1].sourceEnd);}
3061 Else ElseStatementColon() :
3063 Statement statement;
3064 final ArrayList list = new ArrayList();
3065 final Token elseToken;
3068 elseToken = <ELSE> <COLON> ( statement = Statement() {list.add(statement);}
3069 | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3071 final int sizeList = list.size();
3072 final Statement[] stmtsArray = new Statement[sizeList];
3073 list.toArray(stmtsArray);
3074 return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3077 ElseIf ElseIfStatement() :
3079 final Expression condition;
3080 //final Statement statement;
3081 final Token elseifToken;
3082 final Statement[] statement = new Statement[1];
3085 elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3087 return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3090 WhileStatement WhileStatement() :
3092 final Expression condition;
3093 final Statement action;
3094 final Token whileToken;
3097 whileToken = <WHILE>
3098 condition = Condition("while")
3099 action = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3100 {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3103 Statement WhileStatement0(final int start, final int end) :
3105 Statement statement;
3106 final ArrayList stmts = new ArrayList();
3107 final int pos = jj_input_stream.getPosition();
3110 <COLON> (statement = Statement() {stmts.add(statement);})*
3112 setMarker(fileToParse,
3113 "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3117 "Line " + token.beginLine);
3118 } catch (CoreException e) {
3119 PHPeclipsePlugin.log(e);
3123 } catch (ParseException e) {
3124 errorMessage = "'endwhile' expected";
3126 errorStart = e.currentToken.sourceStart;
3127 errorEnd = e.currentToken.sourceEnd;
3133 final Statement[] stmtsArray = new Statement[stmts.size()];
3134 stmts.toArray(stmtsArray);
3135 return new Block(stmtsArray,pos,jj_input_stream.getPosition());}
3136 } catch (ParseException e) {
3137 errorMessage = "';' expected after 'endwhile' keyword";
3139 errorStart = e.currentToken.sourceStart;
3140 errorEnd = e.currentToken.sourceEnd;
3144 statement = Statement()
3148 DoStatement DoStatement() :
3150 final Statement action;
3151 final Expression condition;
3153 Token token2 = null;
3156 token = <DO> action = Statement() <WHILE> condition = Condition("while")
3158 token2 = <SEMICOLON>
3159 } catch (ParseException e) {
3160 errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3162 errorStart = condition.sourceEnd+1;
3163 errorEnd = condition.sourceEnd+1;
3164 processParseExceptionDebug(e);
3167 if (token2 == null) {
3168 return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3170 return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3174 ForeachStatement ForeachStatement() :
3176 Statement statement = null;
3177 Expression expression = null;
3178 ArrayVariableDeclaration variable = null;
3180 Token lparenToken = null;
3181 Token asToken = null;
3182 Token rparenToken = null;
3186 foreachToken = <FOREACH>
3188 lparenToken = <LPAREN>
3189 {pos = lparenToken.sourceEnd+1;}
3190 } catch (ParseException e) {
3191 errorMessage = "'(' expected after 'foreach' keyword";
3193 errorStart = e.currentToken.sourceStart;
3194 errorEnd = e.currentToken.sourceEnd;
3195 processParseExceptionDebug(e);
3196 {pos = foreachToken.sourceEnd+1;}
3199 expression = Expression()
3200 {pos = expression.sourceEnd+1;}
3201 } catch (ParseException e) {
3202 errorMessage = "variable expected";
3204 errorStart = e.currentToken.sourceStart;
3205 errorEnd = e.currentToken.sourceEnd;
3206 processParseExceptionDebug(e);
3210 {pos = asToken.sourceEnd+1;}
3211 } catch (ParseException e) {
3212 errorMessage = "'as' expected";
3214 errorStart = e.currentToken.sourceStart;
3215 errorEnd = e.currentToken.sourceEnd;
3216 processParseExceptionDebug(e);
3219 variable = ArrayVariable()
3220 {pos = variable.sourceEnd+1;}
3221 } catch (ParseException e) {
3222 if (errorMessage != null) throw e;
3223 errorMessage = "variable expected";
3225 errorStart = e.currentToken.sourceStart;
3226 errorEnd = e.currentToken.sourceEnd;
3227 processParseExceptionDebug(e);
3230 rparenToken = <RPAREN>
3231 {pos = rparenToken.sourceEnd+1;}
3232 } catch (ParseException e) {
3233 errorMessage = "')' expected after 'foreach' keyword";
3235 errorStart = e.currentToken.sourceStart;
3236 errorEnd = e.currentToken.sourceEnd;
3237 processParseExceptionDebug(e);
3240 statement = Statement()
3241 {pos = statement.sourceEnd+1;}
3242 } catch (ParseException e) {
3243 if (errorMessage != null) throw e;
3244 errorMessage = "statement expected";
3246 errorStart = e.currentToken.sourceStart;
3247 errorEnd = e.currentToken.sourceEnd;
3248 processParseExceptionDebug(e);
3251 return new ForeachStatement(expression,
3254 foreachToken.sourceStart,
3260 * a for declaration.
3261 * @return a node representing the for statement
3263 ForStatement ForStatement() :
3265 final Token token,tokenEndFor,token2,tokenColon;
3267 Expression[] initializations = null;
3268 Expression condition = null;
3269 Expression[] increments = null;
3271 final ArrayList list = new ArrayList();
3277 } catch (ParseException e) {
3278 errorMessage = "'(' expected after 'for' keyword";
3280 errorStart = token.sourceEnd;
3281 errorEnd = token.sourceEnd +1;
3282 processParseExceptionDebug(e);
3284 [ initializations = ForInit() ] <SEMICOLON>
3285 [ condition = Expression() ] <SEMICOLON>
3286 [ increments = StatementExpressionList() ] <RPAREN>
3288 action = Statement()
3289 {return new ForStatement(initializations,
3296 tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3297 (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3300 setMarker(fileToParse,
3301 "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3305 "Line " + token.beginLine);
3306 } catch (CoreException e) {
3307 PHPeclipsePlugin.log(e);
3311 tokenEndFor = <ENDFOR>
3312 {pos = tokenEndFor.sourceEnd+1;}
3313 } catch (ParseException e) {
3314 errorMessage = "'endfor' expected";
3318 processParseExceptionDebug(e);
3321 token2 = <SEMICOLON>
3322 {pos = token2.sourceEnd+1;}
3323 } catch (ParseException e) {
3324 errorMessage = "';' expected after 'endfor' keyword";
3328 processParseExceptionDebug(e);
3331 final Statement[] stmtsArray = new Statement[list.size()];
3332 list.toArray(stmtsArray);
3333 return new ForStatement(initializations,
3336 new Block(stmtsArray,
3337 stmtsArray[0].sourceStart,
3338 stmtsArray[stmtsArray.length-1].sourceEnd),
3344 Expression[] ForInit() :
3346 final Expression[] exprs;
3349 LOOKAHEAD(LocalVariableDeclaration())
3350 exprs = LocalVariableDeclaration()
3353 exprs = StatementExpressionList()
3357 Expression[] StatementExpressionList() :
3359 final ArrayList list = new ArrayList();
3360 final Expression expr;
3363 expr = Expression() {list.add(expr);}
3364 (<COMMA> Expression() {list.add(expr);})*
3366 final Expression[] exprsArray = new Expression[list.size()];
3367 list.toArray(exprsArray);
3372 Continue ContinueStatement() :
3374 Expression expr = null;
3376 Token token2 = null;
3379 token = <CONTINUE> [ expr = Expression() ]
3381 token2 = <SEMICOLON>
3382 } catch (ParseException e) {
3383 errorMessage = "';' expected after 'continue' statement";
3386 errorStart = token.sourceEnd+1;
3387 errorEnd = token.sourceEnd+1;
3389 errorStart = expr.sourceEnd+1;
3390 errorEnd = expr.sourceEnd+1;
3392 processParseExceptionDebug(e);
3395 if (token2 == null) {
3397 return new Continue(expr,token.sourceStart,token.sourceEnd);
3399 return new Continue(expr,token.sourceStart,expr.sourceEnd);
3401 return new Continue(expr,token.sourceStart,token2.sourceEnd);
3405 ReturnStatement ReturnStatement() :
3407 Expression expr = null;
3409 Token token2 = null;
3412 token = <RETURN> [ expr = Expression() ]
3414 token2 = <SEMICOLON>
3415 } catch (ParseException e) {
3416 errorMessage = "';' expected after 'return' statement";
3419 errorStart = token.sourceEnd+1;
3420 errorEnd = token.sourceEnd+1;
3422 errorStart = expr.sourceEnd+1;
3423 errorEnd = expr.sourceEnd+1;
3425 processParseExceptionDebug(e);
3428 if (token2 == null) {
3430 return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3432 return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3434 return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);