1 /***********************************************************************************************************************************
2 * Copyright (c) 2002 www.phpeclipse.de All rights reserved. This program and the accompanying material are made available under the
3 * terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4 * http://www.eclipse.org/legal/cpl-v10.html
6 * Contributors: www.phpeclipse.de
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.parser;
10 import java.util.ArrayList;
11 import java.util.HashMap;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
15 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
16 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
17 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
18 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
19 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
20 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
21 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
22 import net.sourceforge.phpdt.internal.compiler.util.Util;
23 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
24 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
25 import net.sourceforge.phpeclipse.internal.compiler.ast.AND_AND_Expression;
26 import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
27 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
28 import net.sourceforge.phpeclipse.internal.compiler.ast.BinaryExpression;
29 import net.sourceforge.phpeclipse.internal.compiler.ast.BreakStatement;
30 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
31 import net.sourceforge.phpeclipse.internal.compiler.ast.ConditionalExpression;
32 import net.sourceforge.phpeclipse.internal.compiler.ast.ContinueStatement;
33 import net.sourceforge.phpeclipse.internal.compiler.ast.EqualExpression;
34 import net.sourceforge.phpeclipse.internal.compiler.ast.Expression;
35 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
36 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldReference;
37 import net.sourceforge.phpeclipse.internal.compiler.ast.IfStatement;
38 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
39 import net.sourceforge.phpeclipse.internal.compiler.ast.InstanceOfExpression;
40 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
41 import net.sourceforge.phpeclipse.internal.compiler.ast.OR_OR_Expression;
42 import net.sourceforge.phpeclipse.internal.compiler.ast.OperatorIds;
43 import net.sourceforge.phpeclipse.internal.compiler.ast.ReturnStatement;
44 import net.sourceforge.phpeclipse.internal.compiler.ast.SingleTypeReference;
45 import net.sourceforge.phpeclipse.internal.compiler.ast.Statement;
46 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteral;
47 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralDQ;
48 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralSQ;
49 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
50 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeReference;
51 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
53 import org.eclipse.core.resources.IFile;
54 import org.eclipse.core.resources.IProject;
55 import org.eclipse.core.resources.IResource;
56 import org.eclipse.core.runtime.IPath;
58 public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation {
59 protected final static int StackIncrement = 255;
61 protected int stateStackTop;
63 // protected int[] stack = new int[StackIncrement];
65 public int firstToken; // handle for multiple parsing goals
67 public int lastAct; //handle for multiple parsing goals
69 // protected RecoveredElement currentElement;
71 public static boolean VERBOSE_RECOVERY = false;
73 protected boolean diet = false; //tells the scanner to jump over some
75 // parts of the code/expressions like
78 public Scanner scanner;
80 // private ArrayList phpList;
82 // private int currentPHPString;
84 // private boolean phpEnd;
86 // private static HashMap keywordMap = null;
94 // row counter for syntax errors:
96 // column counter for syntax errors:
100 // // current identifier
101 // String identifier;
104 // Double doubleNumber;
106 // private String stringValue;
108 /** Contains the current expression. */
109 // private StringBuffer expression;
110 //private boolean phpMode;
111 protected int modifiers;
113 protected int modifiersSourceStart;
115 protected Parser(ProblemReporter problemReporter) {
116 this.problemReporter = problemReporter;
117 this.options = problemReporter.options;
118 // this.currentPHPString = 0;
119 // PHPParserSuperclass.fileToParse = fileToParse;
120 // this.phpList = null;
121 // this.indexManager = null;
123 this.token = TokenNameEOF;
125 // this.rowCount = 1;
126 // this.columnCount = 0;
127 // this.phpEnd = false;
129 this.initializeScanner();
132 public void setFileToParse(IFile fileToParse) {
133 // this.currentPHPString = 0;
134 // PHPParserSuperclass.fileToParse = fileToParse;
135 // this.phpList = null;
136 // this.indexManager = null;
138 this.token = TokenNameEOF;
139 // this.phpEnd = false;
140 this.initializeScanner();
144 * ClassDeclaration Constructor.
148 * Description of Parameter
151 public Parser(IFile fileToParse) {
152 // if (keywordMap == null) {
153 // keywordMap = new HashMap();
154 // for (int i = 0; i < PHP_KEYWORS.length; i++) {
155 // keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
158 // this.currentPHPString = 0;
159 // PHPParserSuperclass.fileToParse = fileToParse;
160 // this.phpList = null;
161 this.includesList = null;
163 this.token = TokenNameEOF;
165 // this.rowCount = 1;
166 // this.columnCount = 0;
167 // this.phpEnd = false;
169 this.initializeScanner();
172 public void initializeScanner() {
173 this.scanner = new Scanner(false /* comment */, false /* whitespace */, this.options
174 .getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore /* nls */, false, false,
175 this.options.taskTags/* taskTags */, this.options.taskPriorites/* taskPriorities */, true/* isTaskCaseSensitive */);
179 * Create marker for the parse error
181 // private void setMarker(String message, int charStart, int charEnd, int
183 // setMarker(fileToParse, message, charStart, charEnd, errorLevel);
186 * This method will throw the SyntaxError. It will add the good lines and columns to the Error
190 * @throws SyntaxError
193 private void throwSyntaxError(String error) {
194 int problemStartPosition = scanner.getCurrentTokenStartPosition();
195 int problemEndPosition = scanner.getCurrentTokenEndPosition();
196 throwSyntaxError(error, problemStartPosition, problemEndPosition + 1);
200 * This method will throw the SyntaxError. It will add the good lines and columns to the Error
204 * @throws SyntaxError
207 // private void throwSyntaxError(String error, int startRow) {
208 // throw new SyntaxError(startRow, 0, " ", error);
210 private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
211 if (referenceContext != null) {
212 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
213 compilationUnit.compilationResult);
215 throw new SyntaxError(1, 0, " ", error);
218 private void reportSyntaxError(String error) {
219 int problemStartPosition = scanner.getCurrentTokenStartPosition();
220 int problemEndPosition = scanner.getCurrentTokenEndPosition();
221 reportSyntaxError(error, problemStartPosition, problemEndPosition + 1);
224 private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
225 if (referenceContext != null) {
226 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
227 compilationUnit.compilationResult);
231 private void reportSyntaxWarning(String error, int problemStartPosition, int problemEndPosition) {
232 if (referenceContext != null) {
233 problemReporter.phpParsingWarning(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
234 compilationUnit.compilationResult);
239 * gets the next token from input
241 private void getNextToken() {
243 token = scanner.getNextToken();
245 int currentEndPosition = scanner.getCurrentTokenEndPosition();
246 int currentStartPosition = scanner.getCurrentTokenStartPosition();
247 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
248 System.out.println(scanner.toStringAction(token));
250 } catch (InvalidInputException e) {
251 token = TokenNameERROR;
252 String detailedMessage = e.getMessage();
254 if (detailedMessage == Scanner.UNTERMINATED_STRING) {
255 throwSyntaxError("Unterminated string.");
256 } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
257 throwSyntaxError("Unterminated commment.");
263 public void init(String s) {
265 this.token = TokenNameEOF;
266 this.includesList = new ArrayList();
268 // this.rowCount = 1;
269 // this.columnCount = 0;
270 // this.phpEnd = false;
271 // this.phpMode = false;
272 /* scanner initialization */
273 scanner.setSource(s.toCharArray());
274 scanner.setPHPMode(false);
278 protected void initialize(boolean phpMode) {
279 initialize(phpMode, null);
282 protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
283 compilationUnit = null;
284 referenceContext = null;
285 this.includesList = new ArrayList();
286 // this.indexManager = indexManager;
288 this.token = TokenNameEOF;
290 // this.rowCount = 1;
291 // this.columnCount = 0;
292 // this.phpEnd = false;
293 // this.phpMode = phpMode;
294 scanner.setPHPMode(phpMode);
299 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
301 public void parse(String s) {
306 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
308 public void parse(String s, HashMap variables) {
309 fMethodVariables = variables;
315 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
317 protected void parse() {
318 if (scanner.compilationUnit != null) {
319 IResource resource = scanner.compilationUnit.getResource();
320 if (resource != null && resource instanceof IFile) {
321 // set the package name
322 consumePackageDeclarationName((IFile) resource);
328 if (token != TokenNameEOF && token != TokenNameERROR) {
331 if (token != TokenNameEOF) {
332 if (token == TokenNameERROR) {
333 throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
335 if (token == TokenNameRPAREN) {
336 throwSyntaxError("Too many closing ')'; end-of-file not reached.");
338 if (token == TokenNameRBRACE) {
339 throwSyntaxError("Too many closing '}'; end-of-file not reached.");
341 if (token == TokenNameRBRACKET) {
342 throwSyntaxError("Too many closing ']'; end-of-file not reached.");
344 if (token == TokenNameLPAREN) {
345 throwSyntaxError("Read character '('; end-of-file not reached.");
347 if (token == TokenNameLBRACE) {
348 throwSyntaxError("Read character '{'; end-of-file not reached.");
350 if (token == TokenNameLBRACKET) {
351 throwSyntaxError("Read character '['; end-of-file not reached.");
353 throwSyntaxError("End-of-file not reached.");
356 } catch (SyntaxError sytaxErr1) {
358 // // if an error occured,
359 // // try to find keywords 'abstract' 'final' 'class' or 'function'
360 // // to parse the rest of the string
361 // boolean tokenize = scanner.tokenizeStrings;
363 // scanner.tokenizeStrings = true;
366 // while (token != TokenNameEOF) {
367 // if (token == TokenNameabstract || token == TokenNamefinal || token == TokenNameclass || token == TokenNamefunction) {
372 // if (token == TokenNameEOF) {
375 // } catch (SyntaxError sytaxErr2) {
378 // scanner.tokenizeStrings = tokenize;
387 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
389 public void parseFunction(String s, HashMap variables) {
391 scanner.phpMode = true;
392 parseFunction(variables);
396 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
398 protected void parseFunction(HashMap variables) {
400 boolean hasModifiers = member_modifiers();
401 if (token == TokenNamefunction) {
403 checkAndSetModifiers(AccPublic);
405 this.fMethodVariables = variables;
407 MethodDeclaration methodDecl = new MethodDeclaration(null);
408 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
409 methodDecl.modifiers = this.modifiers;
410 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
413 functionDefinition(methodDecl);
414 } catch (SyntaxError sytaxErr1) {
417 int sourceEnd = scanner.getCurrentTokenStartPosition();
418 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
419 sourceEnd = methodDecl.declarationSourceStart + 1;
421 methodDecl.declarationSourceEnd = sourceEnd;
426 protected CompilationUnitDeclaration endParse(int act) {
430 // if (currentElement != null) {
431 // currentElement.topElement().updateParseTree();
432 // if (VERBOSE_RECOVERY) {
433 // System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
434 // System.out.println("--------------------------"); //$NON-NLS-1$
435 // System.out.println(compilationUnit);
436 // System.out.println("----------------------------------"); //$NON-NLS-1$
439 if (diet & VERBOSE_RECOVERY) {
440 System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
441 System.out.println("--------------------------"); //$NON-NLS-1$
442 System.out.println(compilationUnit);
443 System.out.println("----------------------------------"); //$NON-NLS-1$
446 if (scanner.recordLineSeparator) {
447 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
449 if (scanner.taskTags != null) {
450 for (int i = 0; i < scanner.foundTaskCount; i++) {
451 problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
452 scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
453 scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
456 compilationUnit.imports = new ImportReference[includesList.size()];
457 for (int i = 0; i < includesList.size(); i++) {
458 compilationUnit.imports[i] = (ImportReference) includesList.get(i);
460 return compilationUnit;
463 // private boolean isVariable() {
464 // return token == TokenNameVariable; // || token == TokenNamethis;
467 private void statementList() {
468 boolean branchStatement = false;
474 sourceStart = scanner.getCurrentTokenStartPosition();
475 statement = statement(TokenNameEOF);
476 if (branchStatement) {
477 sourceEnd = scanner.getCurrentTokenEndPosition();
478 reportSyntaxError("Unreachable code", sourceStart, sourceEnd);
480 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
481 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
482 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
483 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
486 branchStatement = false;
487 if (statement instanceof ReturnStatement ||statement instanceof ContinueStatement || statement instanceof BreakStatement) {
488 branchStatement = true;
490 } catch (SyntaxError sytaxErr1) {
491 // if an error occured,
492 // try to find keywords
493 // to parse the rest of the string
494 boolean tokenize = scanner.tokenizeStrings;
496 scanner.tokenizeStrings = true;
499 while (token != TokenNameEOF) {
500 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
501 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
502 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
503 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
506 if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
507 || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
508 || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameglobal
509 || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction || token == TokenNamedeclare
510 || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow || token == TokenNamefinal
511 || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
514 // System.out.println(scanner.toStringAction(token));
516 // System.out.println(scanner.toStringAction(token));
518 if (token == TokenNameEOF) {
522 scanner.tokenizeStrings = tokenize;
528 private void functionBody(MethodDeclaration methodDecl) {
529 // '{' [statement-list] '}'
530 if (token == TokenNameLBRACE) {
533 throwSyntaxError("'{' expected in compound-statement.");
535 if (token != TokenNameRBRACE) {
538 if (token == TokenNameRBRACE) {
539 // methodDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
542 throwSyntaxError("'}' expected in compound-statement.");
546 private Statement statement(int previousToken) {
547 Statement statement = null;
548 Expression expression;
549 int sourceStart = scanner.getCurrentTokenStartPosition();
551 if (token == TokenNameif) {
553 if (token == TokenNameLPAREN) {
556 throwSyntaxError("'(' expected after 'if' keyword.");
559 if (token == TokenNameRPAREN) {
562 throwSyntaxError("')' expected after 'if' condition.");
565 return new IfStatement(expression, statement, sourceStart, scanner.getCurrentTokenEndPosition());
566 } else if (token == TokenNameswitch) {
568 if (token == TokenNameLPAREN) {
571 throwSyntaxError("'(' expected after 'switch' keyword.");
574 if (token == TokenNameRPAREN) {
577 throwSyntaxError("')' expected after 'switch' condition.");
581 } else if (token == TokenNamefor) {
583 if (token == TokenNameLPAREN) {
586 throwSyntaxError("'(' expected after 'for' keyword.");
588 if (token == TokenNameSEMICOLON) {
592 if (token == TokenNameSEMICOLON) {
595 throwSyntaxError("';' expected after 'for'.");
598 if (token == TokenNameSEMICOLON) {
602 if (token == TokenNameSEMICOLON) {
605 throwSyntaxError("';' expected after 'for'.");
608 if (token == TokenNameRPAREN) {
612 if (token == TokenNameRPAREN) {
615 throwSyntaxError("')' expected after 'for'.");
620 } else if (token == TokenNamewhile) {
622 if (token == TokenNameLPAREN) {
625 throwSyntaxError("'(' expected after 'while' keyword.");
628 if (token == TokenNameRPAREN) {
631 throwSyntaxError("')' expected after 'while' condition.");
635 } else if (token == TokenNamedo) {
637 if (token == TokenNameLBRACE) {
639 if (token != TokenNameRBRACE) {
642 if (token == TokenNameRBRACE) {
645 throwSyntaxError("'}' expected after 'do' keyword.");
648 statement(TokenNameEOF);
650 if (token == TokenNamewhile) {
652 if (token == TokenNameLPAREN) {
655 throwSyntaxError("'(' expected after 'while' keyword.");
658 if (token == TokenNameRPAREN) {
661 throwSyntaxError("')' expected after 'while' condition.");
664 throwSyntaxError("'while' expected after 'do' keyword.");
666 if (token == TokenNameSEMICOLON) {
669 if (token != TokenNameINLINE_HTML) {
670 throwSyntaxError("';' expected after do-while statement.");
675 } else if (token == TokenNameforeach) {
677 if (token == TokenNameLPAREN) {
680 throwSyntaxError("'(' expected after 'foreach' keyword.");
683 if (token == TokenNameas) {
686 throwSyntaxError("'as' expected after 'foreach' exxpression.");
690 foreach_optional_arg();
691 if (token == TokenNameEQUAL_GREATER) {
695 if (token == TokenNameRPAREN) {
698 throwSyntaxError("')' expected after 'foreach' expression.");
702 } else if (token == TokenNamebreak) {
705 if (token != TokenNameSEMICOLON) {
708 if (token == TokenNameSEMICOLON) {
709 sourceEnd = scanner.getCurrentTokenEndPosition();
712 if (token != TokenNameINLINE_HTML) {
713 throwSyntaxError("';' expected after 'break'.");
715 sourceEnd = scanner.getCurrentTokenEndPosition();
718 return new BreakStatement(null,sourceStart, sourceEnd);
719 } else if (token == TokenNamecontinue) {
722 if (token != TokenNameSEMICOLON) {
725 if (token == TokenNameSEMICOLON) {
726 sourceEnd = scanner.getCurrentTokenEndPosition();
729 if (token != TokenNameINLINE_HTML) {
730 throwSyntaxError("';' expected after 'continue'.");
732 sourceEnd = scanner.getCurrentTokenEndPosition();
735 return new ContinueStatement(null,sourceStart, sourceEnd);
736 } else if (token == TokenNamereturn) {
739 if (token != TokenNameSEMICOLON) {
742 if (token == TokenNameSEMICOLON) {
743 sourceEnd = scanner.getCurrentTokenEndPosition();
746 if (token != TokenNameINLINE_HTML) {
747 throwSyntaxError("';' expected after 'return'.");
749 sourceEnd = scanner.getCurrentTokenEndPosition();
752 return new ReturnStatement(expression,sourceStart, sourceEnd);
753 } else if (token == TokenNameecho) {
756 if (token == TokenNameSEMICOLON) {
759 if (token != TokenNameINLINE_HTML) {
760 throwSyntaxError("';' expected after 'echo' statement.");
765 } else if (token == TokenNameINLINE_HTML) {
768 // } else if (token == TokenNameprint) {
771 // if (token == TokenNameSEMICOLON) {
774 // if (token != TokenNameStopPHP) {
775 // throwSyntaxError("';' expected after 'print' statement.");
780 } else if (token == TokenNameglobal) {
783 if (token == TokenNameSEMICOLON) {
786 if (token != TokenNameINLINE_HTML) {
787 throwSyntaxError("';' expected after 'global' statement.");
792 } else if (token == TokenNamestatic) {
795 if (token == TokenNameSEMICOLON) {
798 if (token != TokenNameINLINE_HTML) {
799 throwSyntaxError("';' expected after 'static' statement.");
804 } else if (token == TokenNameunset) {
806 if (token == TokenNameLPAREN) {
809 throwSyntaxError("'(' expected after 'unset' statement.");
812 if (token == TokenNameRPAREN) {
815 throwSyntaxError("')' expected after 'unset' statement.");
817 if (token == TokenNameSEMICOLON) {
820 if (token != TokenNameINLINE_HTML) {
821 throwSyntaxError("';' expected after 'unset' statement.");
826 } else if (token == TokenNamefunction) {
827 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
828 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
829 methodDecl.modifiers = AccDefault;
830 methodDecl.type = MethodDeclaration.FUNCTION_DEFINITION;
833 functionDefinition(methodDecl);
835 sourceEnd = scanner.getCurrentTokenStartPosition();
836 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
837 sourceEnd = methodDecl.declarationSourceStart + 1;
839 methodDecl.declarationSourceEnd = sourceEnd;
842 } else if (token == TokenNamedeclare) {
843 //T_DECLARE '(' declare_list ')' declare_statement
845 if (token != TokenNameLPAREN) {
846 throwSyntaxError("'(' expected in 'declare' statement.");
850 if (token != TokenNameRPAREN) {
851 throwSyntaxError("')' expected in 'declare' statement.");
856 } else if (token == TokenNametry) {
858 if (token != TokenNameLBRACE) {
859 throwSyntaxError("'{' expected in 'try' statement.");
863 if (token != TokenNameRBRACE) {
864 throwSyntaxError("'}' expected in 'try' statement.");
868 } else if (token == TokenNamecatch) {
870 if (token != TokenNameLPAREN) {
871 throwSyntaxError("'(' expected in 'catch' statement.");
874 fully_qualified_class_name();
875 if (token != TokenNameVariable) {
876 throwSyntaxError("Variable expected in 'catch' statement.");
879 if (token != TokenNameRPAREN) {
880 throwSyntaxError("')' expected in 'catch' statement.");
883 if (token != TokenNameLBRACE) {
884 throwSyntaxError("'{' expected in 'catch' statement.");
887 if (token != TokenNameRBRACE) {
889 if (token != TokenNameRBRACE) {
890 throwSyntaxError("'}' expected in 'catch' statement.");
894 additional_catches();
896 } else if (token == TokenNamethrow) {
899 if (token == TokenNameSEMICOLON) {
902 throwSyntaxError("';' expected after 'throw' exxpression.");
905 } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
907 TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
908 typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
909 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
910 typeDecl.name = new char[] { ' ' };
911 // default super class
912 typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
913 compilationUnit.types.add(typeDecl);
914 pushOnAstStack(typeDecl);
915 unticked_class_declaration_statement(typeDecl);
923 // throwSyntaxError("Unexpected keyword '" + keyword + "'");
924 } else if (token == TokenNameLBRACE) {
926 if (token != TokenNameRBRACE) {
929 if (token == TokenNameRBRACE) {
933 throwSyntaxError("'}' expected.");
936 if (token != TokenNameSEMICOLON) {
939 if (token == TokenNameSEMICOLON) {
943 if (token == TokenNameRBRACE) {
944 reportSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
946 if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
947 throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
957 private void declare_statement() {
959 //| ':' inner_statement_list T_ENDDECLARE ';'
961 if (token == TokenNameCOLON) {
963 // TODO: implement inner_statement_list();
965 if (token != TokenNameenddeclare) {
966 throwSyntaxError("'enddeclare' expected in 'declare' statement.");
969 if (token != TokenNameSEMICOLON) {
970 throwSyntaxError("';' expected after 'enddeclare' keyword.");
974 statement(TokenNameRPAREN);
978 private void declare_list() {
979 // T_STRING '=' static_scalar
980 //| declare_list ',' T_STRING '=' static_scalar
982 if (token != TokenNameIdentifier) {
983 throwSyntaxError("Identifier expected in 'declare' list.");
986 if (token != TokenNameEQUAL) {
987 throwSyntaxError("'=' expected in 'declare' list.");
991 if (token != TokenNameCOMMA) {
998 private void additional_catches() {
999 while (token == TokenNamecatch) {
1001 if (token != TokenNameLPAREN) {
1002 throwSyntaxError("'(' expected in 'catch' statement.");
1005 fully_qualified_class_name();
1006 if (token != TokenNameVariable) {
1007 throwSyntaxError("Variable expected in 'catch' statement.");
1010 if (token != TokenNameRPAREN) {
1011 throwSyntaxError("')' expected in 'catch' statement.");
1014 if (token != TokenNameLBRACE) {
1015 throwSyntaxError("'{' expected in 'catch' statement.");
1018 if (token != TokenNameRBRACE) {
1021 if (token != TokenNameRBRACE) {
1022 throwSyntaxError("'}' expected in 'catch' statement.");
1028 private void foreach_variable() {
1031 if (token == TokenNameAND) {
1037 private void foreach_optional_arg() {
1039 //| T_DOUBLE_ARROW foreach_variable
1040 if (token == TokenNameEQUAL_GREATER) {
1046 private void global_var_list() {
1048 // global_var_list ',' global_var
1052 if (token != TokenNameCOMMA) {
1059 private void global_var() {
1063 //| '$' '{' expr '}'
1064 if (token == TokenNameVariable) {
1065 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
1066 if (fMethodVariables != null) {
1067 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1070 } else if (token == TokenNameDOLLAR) {
1072 if (token == TokenNameLBRACE) {
1075 if (token != TokenNameRBRACE) {
1076 throwSyntaxError("'}' expected in global variable.");
1085 private void static_var_list() {
1087 // static_var_list ',' T_VARIABLE
1088 //| static_var_list ',' T_VARIABLE '=' static_scalar
1090 //| T_VARIABLE '=' static_scalar
1092 if (token == TokenNameVariable) {
1093 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
1094 if (fMethodVariables != null) {
1095 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1098 if (token == TokenNameEQUAL) {
1102 if (token != TokenNameCOMMA) {
1112 private void unset_variables() {
1115 // | unset_variables ',' unset_variable
1120 if (token != TokenNameCOMMA) {
1127 private final void initializeModifiers() {
1129 this.modifiersSourceStart = -1;
1132 private final void checkAndSetModifiers(int flag) {
1133 this.modifiers |= flag;
1134 if (this.modifiersSourceStart < 0)
1135 this.modifiersSourceStart = this.scanner.startPosition;
1138 private void unticked_class_declaration_statement(TypeDeclaration typeDecl) {
1139 initializeModifiers();
1140 if (token == TokenNameinterface) {
1141 // interface_entry T_STRING
1142 // interface_extends_list
1143 // '{' class_statement_list '}'
1144 checkAndSetModifiers(AccInterface);
1146 typeDecl.modifiers = this.modifiers;
1147 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1148 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1149 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1150 typeDecl.name = scanner.getCurrentIdentifierSource();
1151 if (token > TokenNameKEYWORD) {
1152 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1153 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1154 // throwSyntaxError("Don't use a keyword for interface declaration [" + scanner.toStringAction(token) + "].",
1155 // typeDecl.sourceStart, typeDecl.sourceEnd);
1158 interface_extends_list(typeDecl);
1160 typeDecl.name = new char[] { ' ' };
1161 throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1165 // class_entry_type T_STRING extends_from
1167 // '{' class_statement_list'}'
1169 typeDecl.modifiers = this.modifiers;
1170 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1171 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1173 //identifier 'extends' identifier
1174 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1175 typeDecl.name = scanner.getCurrentIdentifierSource();
1176 if (token > TokenNameKEYWORD) {
1177 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1178 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1179 // throwSyntaxError("Don't use a keyword for class declaration [" + scanner.toStringAction(token) + "].",
1180 // typeDecl.sourceStart, typeDecl.sourceEnd);
1185 // | T_EXTENDS fully_qualified_class_name
1186 if (token == TokenNameextends) {
1187 interface_extends_list(typeDecl);
1189 // if (token != TokenNameIdentifier) {
1190 // throwSyntaxError("Class name expected after keyword
1192 // scanner.getCurrentTokenStartPosition(), scanner
1193 // .getCurrentTokenEndPosition());
1196 implements_list(typeDecl);
1198 typeDecl.name = new char[] { ' ' };
1199 throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1203 // '{' class_statement_list '}'
1204 if (token == TokenNameLBRACE) {
1206 if (token != TokenNameRBRACE) {
1207 ArrayList list = new ArrayList();
1208 class_statement_list(list);
1209 typeDecl.fields = new FieldDeclaration[list.size()];
1210 for (int i = 0; i < list.size(); i++) {
1211 typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1214 if (token == TokenNameRBRACE) {
1215 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1218 throwSyntaxError("'}' expected at end of class body.");
1221 throwSyntaxError("'{' expected at start of class body.");
1225 private void class_entry_type() {
1227 // | T_ABSTRACT T_CLASS
1228 // | T_FINAL T_CLASS
1229 if (token == TokenNameclass) {
1231 } else if (token == TokenNameabstract) {
1232 checkAndSetModifiers(AccAbstract);
1234 if (token != TokenNameclass) {
1235 throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1238 } else if (token == TokenNamefinal) {
1239 checkAndSetModifiers(AccFinal);
1241 if (token != TokenNameclass) {
1242 throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1246 throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1250 private void class_extends(TypeDeclaration typeDecl) {
1252 // | T_EXTENDS interface_list
1253 if (token == TokenNameextends) {
1256 if (token == TokenNameIdentifier) {
1259 throwSyntaxError("Class name expected after keyword 'extends'.");
1264 private void interface_extends_list(TypeDeclaration typeDecl) {
1266 // | T_EXTENDS interface_list
1267 if (token == TokenNameextends) {
1273 private void implements_list(TypeDeclaration typeDecl) {
1275 // | T_IMPLEMENTS interface_list
1276 if (token == TokenNameimplements) {
1282 private void interface_list() {
1284 // fully_qualified_class_name
1285 //| interface_list ',' fully_qualified_class_name
1287 if (token == TokenNameIdentifier) {
1290 throwSyntaxError("Interface name expected after keyword 'implements'.");
1292 if (token != TokenNameCOMMA) {
1299 // private void classBody(TypeDeclaration typeDecl) {
1300 // //'{' [class-element-list] '}'
1301 // if (token == TokenNameLBRACE) {
1303 // if (token != TokenNameRBRACE) {
1304 // class_statement_list();
1306 // if (token == TokenNameRBRACE) {
1307 // typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1310 // throwSyntaxError("'}' expected at end of class body.");
1313 // throwSyntaxError("'{' expected at start of class body.");
1316 private void class_statement_list(ArrayList list) {
1319 class_statement(list);
1320 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1321 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1322 || token == TokenNameconst) {
1325 if (token == TokenNameRBRACE) {
1328 throwSyntaxError("'}' at end of class statement.");
1329 } catch (SyntaxError sytaxErr1) {
1330 boolean tokenize = scanner.tokenizeStrings;
1332 scanner.tokenizeStrings = true;
1335 // if an error occured,
1336 // try to find keywords
1337 // to parse the rest of the string
1338 while (token != TokenNameEOF) {
1339 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1340 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1341 || token == TokenNameconst) {
1344 // System.out.println(scanner.toStringAction(token));
1347 if (token == TokenNameEOF) {
1351 scanner.tokenizeStrings = tokenize;
1357 private void class_statement(ArrayList list) {
1359 // variable_modifiers class_variable_declaration ';'
1360 // | class_constant_declaration ';'
1361 // | method_modifiers T_FUNCTION is_reference T_STRING
1362 // '(' parameter_list ')' method_body
1363 initializeModifiers();
1364 int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1366 if (token == TokenNamevar) {
1367 checkAndSetModifiers(AccPublic);
1368 problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1369 referenceContext, compilationUnit.compilationResult);
1371 class_variable_declaration(declarationSourceStart, list);
1372 } else if (token == TokenNameconst) {
1373 checkAndSetModifiers(AccFinal | AccPublic);
1374 class_constant_declaration(declarationSourceStart, list);
1375 if (token != TokenNameSEMICOLON) {
1376 throwSyntaxError("';' expected after class const declaration.");
1380 boolean hasModifiers = member_modifiers();
1381 if (token == TokenNamefunction) {
1382 if (!hasModifiers) {
1383 checkAndSetModifiers(AccPublic);
1385 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1386 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1387 methodDecl.modifiers = this.modifiers;
1388 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
1391 functionDefinition(methodDecl);
1393 int sourceEnd = scanner.getCurrentTokenStartPosition();
1394 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
1395 sourceEnd = methodDecl.declarationSourceStart + 1;
1397 methodDecl.declarationSourceEnd = sourceEnd;
1400 if (!hasModifiers) {
1401 throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1403 class_variable_declaration(declarationSourceStart, list);
1408 private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1409 // class_constant_declaration ',' T_STRING '=' static_scalar
1410 // | T_CONST T_STRING '=' static_scalar
1411 if (token != TokenNameconst) {
1412 throwSyntaxError("'const' keyword expected in class declaration.");
1417 if (token != TokenNameIdentifier) {
1418 throwSyntaxError("Identifier expected in class const declaration.");
1420 FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1421 .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1422 fieldDeclaration.modifiers = this.modifiers;
1423 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1424 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1425 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1426 // fieldDeclaration.type
1427 list.add(fieldDeclaration);
1429 if (token != TokenNameEQUAL) {
1430 throwSyntaxError("'=' expected in class const declaration.");
1434 if (token != TokenNameCOMMA) {
1435 break; // while(true)-loop
1441 // private void variable_modifiers() {
1442 // // variable_modifiers:
1443 // // non_empty_member_modifiers
1445 // initializeModifiers();
1446 // if (token == TokenNamevar) {
1447 // checkAndSetModifiers(AccPublic);
1448 // reportSyntaxError(
1449 // "Keyword 'var' is deprecated. Please use 'public' 'private' or
1451 // modifier for field declarations.",
1452 // scanner.getCurrentTokenStartPosition(), scanner
1453 // .getCurrentTokenEndPosition());
1456 // if (!member_modifiers()) {
1457 // throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1458 // field declarations.");
1462 // private void method_modifiers() {
1463 // //method_modifiers:
1465 // //| non_empty_member_modifiers
1466 // initializeModifiers();
1467 // if (!member_modifiers()) {
1468 // checkAndSetModifiers(AccPublic);
1471 private boolean member_modifiers() {
1478 boolean foundToken = false;
1480 if (token == TokenNamepublic) {
1481 checkAndSetModifiers(AccPublic);
1484 } else if (token == TokenNameprotected) {
1485 checkAndSetModifiers(AccProtected);
1488 } else if (token == TokenNameprivate) {
1489 checkAndSetModifiers(AccPrivate);
1492 } else if (token == TokenNamestatic) {
1493 checkAndSetModifiers(AccStatic);
1496 } else if (token == TokenNameabstract) {
1497 checkAndSetModifiers(AccAbstract);
1500 } else if (token == TokenNamefinal) {
1501 checkAndSetModifiers(AccFinal);
1511 private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1512 // class_variable_declaration:
1513 // class_variable_declaration ',' T_VARIABLE
1514 // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1516 // | T_VARIABLE '=' static_scalar
1517 char[] classVariable;
1519 if (token == TokenNameVariable) {
1520 classVariable = scanner.getCurrentIdentifierSource();
1521 // indexManager.addIdentifierInformation('v', classVariable, buf, -1, -1);
1522 FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1523 .getCurrentTokenEndPosition());
1524 fieldDeclaration.modifiers = this.modifiers;
1525 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1526 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1527 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1528 list.add(fieldDeclaration);
1529 if (fTypeVariables != null) {
1530 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_CLASS_UNIT);
1531 fTypeVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1534 if (token == TokenNameEQUAL) {
1539 // if (token == TokenNamethis) {
1540 // throwSyntaxError("'$this' not allowed after keyword 'public'
1541 // 'protected' 'private' 'var'.");
1543 throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1545 if (token != TokenNameCOMMA) {
1550 if (token != TokenNameSEMICOLON) {
1551 throwSyntaxError("';' expected after field declaration.");
1556 private void functionDefinition(MethodDeclaration methodDecl) {
1557 boolean isAbstract = false;
1559 if (compilationUnit != null) {
1560 compilationUnit.types.add(methodDecl);
1563 ASTNode node = astStack[astPtr];
1564 if (node instanceof TypeDeclaration) {
1565 TypeDeclaration typeDecl = ((TypeDeclaration) node);
1566 if (typeDecl.methods == null) {
1567 typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1569 AbstractMethodDeclaration[] newMethods;
1570 System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 0,
1571 typeDecl.methods.length);
1572 newMethods[typeDecl.methods.length] = methodDecl;
1573 typeDecl.methods = newMethods;
1575 if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1577 } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1582 functionDeclarator(methodDecl);
1583 if (token == TokenNameSEMICOLON) {
1585 throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1590 functionBody(methodDecl);
1593 private void functionDeclarator(MethodDeclaration methodDecl) {
1594 //identifier '(' [parameter-list] ')'
1595 if (token == TokenNameAND) {
1598 methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1599 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1600 if (Scanner.isIdentifierOrKeyword(token)) {
1601 methodDecl.selector = scanner.getCurrentIdentifierSource();
1602 if (token > TokenNameKEYWORD) {
1603 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1604 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1607 if (token == TokenNameLPAREN) {
1610 throwSyntaxError("'(' expected in function declaration.");
1612 if (token != TokenNameRPAREN) {
1613 parameter_list(methodDecl);
1615 if (token != TokenNameRPAREN) {
1616 throwSyntaxError("')' expected in function declaration.");
1618 methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1622 methodDecl.selector = "<undefined>".toCharArray();
1623 throwSyntaxError("Function name expected after keyword 'function'.");
1628 private void parameter_list(MethodDeclaration methodDecl) {
1629 // non_empty_parameter_list
1631 non_empty_parameter_list(methodDecl, true);
1634 private void non_empty_parameter_list(MethodDeclaration methodDecl, boolean empty_allowed) {
1635 // optional_class_type T_VARIABLE
1636 // | optional_class_type '&' T_VARIABLE
1637 // | optional_class_type '&' T_VARIABLE '=' static_scalar
1638 // | optional_class_type T_VARIABLE '=' static_scalar
1639 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1640 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1641 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1643 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1645 char[] typeIdentifier = null;
1646 if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1648 if (token == TokenNameIdentifier) {
1649 typeIdentifier = scanner.getCurrentIdentifierSource();
1652 if (token == TokenNameAND) {
1655 if (token == TokenNameVariable) {
1656 if (fMethodVariables != null) {
1658 if (methodDecl.type == MethodDeclaration.FUNCTION_DEFINITION) {
1659 info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_FUNCTION_DEFINITION);
1661 info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_METHOD_DEFINITION);
1663 info.typeIdentifier = typeIdentifier;
1664 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1667 if (token == TokenNameEQUAL) {
1672 throwSyntaxError("Variable expected in parameter list.");
1674 if (token != TokenNameCOMMA) {
1681 if (!empty_allowed) {
1682 throwSyntaxError("Identifier expected in parameter list.");
1686 private void optional_class_type() {
1691 // private void parameterDeclaration() {
1693 // //variable-reference
1694 // if (token == TokenNameAND) {
1696 // if (isVariable()) {
1699 // throwSyntaxError("Variable expected after reference operator '&'.");
1702 // //variable '=' constant
1703 // if (token == TokenNameVariable) {
1705 // if (token == TokenNameEQUAL) {
1711 // // if (token == TokenNamethis) {
1712 // // throwSyntaxError("Reserved word '$this' not allowed in parameter
1713 // // declaration.");
1717 private void labeledStatementList() {
1718 if (token != TokenNamecase && token != TokenNamedefault) {
1719 throwSyntaxError("'case' or 'default' expected.");
1722 if (token == TokenNamecase) {
1724 expr(); //constant();
1725 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1727 if (token == TokenNamecase || token == TokenNamedefault) {
1728 // empty case statement ?
1733 // else if (token == TokenNameSEMICOLON) {
1735 // "':' expected after 'case' keyword (Found token: " +
1736 // scanner.toStringAction(token) + ")",
1737 // scanner.getCurrentTokenStartPosition(),
1738 // scanner.getCurrentTokenEndPosition(),
1741 // if (token == TokenNamecase) { // empty case statement ?
1747 throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1749 } else { // TokenNamedefault
1751 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1753 if (token == TokenNameRBRACE) {
1754 // empty default case
1757 if (token != TokenNamecase) {
1761 throwSyntaxError("':' character expected after 'default'.");
1764 } while (token == TokenNamecase || token == TokenNamedefault);
1767 // public void labeledStatement() {
1768 // if (token == TokenNamecase) {
1771 // if (token == TokenNameDDOT) {
1775 // throwSyntaxError("':' character after 'case' constant expected.");
1778 // } else if (token == TokenNamedefault) {
1780 // if (token == TokenNameDDOT) {
1784 // throwSyntaxError("':' character after 'default' expected.");
1789 // public void expressionStatement() {
1791 // private void inclusionStatement() {
1793 // public void compoundStatement() {
1795 // public void selectionStatement() {
1798 // public void iterationStatement() {
1801 // public void jumpStatement() {
1804 // public void outputStatement() {
1807 // public void scopeStatement() {
1810 // public void flowStatement() {
1813 // public void definitionStatement() {
1815 private void ifStatement() {
1816 // ':' statement-list [elseif-list] [else-colon-statement] 'endif' ';'
1817 if (token == TokenNameCOLON) {
1819 if (token != TokenNameendif) {
1824 if (token == TokenNameCOLON) {
1826 if (token != TokenNameendif) {
1830 if (token == TokenNameif) { //'else if'
1832 elseifStatementList();
1834 throwSyntaxError("':' expected after 'else'.");
1838 case TokenNameelseif:
1840 elseifStatementList();
1844 if (token != TokenNameendif) {
1845 throwSyntaxError("'endif' expected.");
1848 if (token != TokenNameSEMICOLON) {
1849 throwSyntaxError("';' expected after if-statement.");
1853 // statement [else-statement]
1854 statement(TokenNameEOF);
1855 if (token == TokenNameelseif) {
1857 if (token == TokenNameLPAREN) {
1860 throwSyntaxError("'(' expected after 'elseif' keyword.");
1863 if (token == TokenNameRPAREN) {
1866 throwSyntaxError("')' expected after 'elseif' condition.");
1869 } else if (token == TokenNameelse) {
1871 statement(TokenNameEOF);
1876 private void elseifStatementList() {
1882 if (token == TokenNameCOLON) {
1884 if (token != TokenNameendif) {
1889 if (token == TokenNameif) { //'else if'
1892 throwSyntaxError("':' expected after 'else'.");
1896 case TokenNameelseif:
1905 private void elseifStatement() {
1906 if (token == TokenNameLPAREN) {
1909 if (token != TokenNameRPAREN) {
1910 throwSyntaxError("')' expected in else-if-statement.");
1913 if (token != TokenNameCOLON) {
1914 throwSyntaxError("':' expected in else-if-statement.");
1917 if (token != TokenNameendif) {
1923 private void switchStatement() {
1924 if (token == TokenNameCOLON) {
1925 // ':' [labeled-statement-list] 'endswitch' ';'
1927 labeledStatementList();
1928 if (token != TokenNameendswitch) {
1929 throwSyntaxError("'endswitch' expected.");
1932 if (token != TokenNameSEMICOLON) {
1933 throwSyntaxError("';' expected after switch-statement.");
1937 // '{' [labeled-statement-list] '}'
1938 if (token != TokenNameLBRACE) {
1939 throwSyntaxError("'{' expected in switch statement.");
1942 if (token != TokenNameRBRACE) {
1943 labeledStatementList();
1945 if (token != TokenNameRBRACE) {
1946 throwSyntaxError("'}' expected in switch statement.");
1952 private void forStatement() {
1953 if (token == TokenNameCOLON) {
1956 if (token != TokenNameendfor) {
1957 throwSyntaxError("'endfor' expected.");
1960 if (token != TokenNameSEMICOLON) {
1961 throwSyntaxError("';' expected after for-statement.");
1965 statement(TokenNameEOF);
1969 private void whileStatement() {
1970 // ':' statement-list 'endwhile' ';'
1971 if (token == TokenNameCOLON) {
1974 if (token != TokenNameendwhile) {
1975 throwSyntaxError("'endwhile' expected.");
1978 if (token != TokenNameSEMICOLON) {
1979 throwSyntaxError("';' expected after while-statement.");
1983 statement(TokenNameEOF);
1987 private void foreachStatement() {
1988 if (token == TokenNameCOLON) {
1991 if (token != TokenNameendforeach) {
1992 throwSyntaxError("'endforeach' expected.");
1995 if (token != TokenNameSEMICOLON) {
1996 throwSyntaxError("';' expected after foreach-statement.");
2000 statement(TokenNameEOF);
2004 // private void exitStatus() {
2005 // if (token == TokenNameLPAREN) {
2008 // throwSyntaxError("'(' expected in 'exit-status'.");
2010 // if (token != TokenNameRPAREN) {
2013 // if (token == TokenNameRPAREN) {
2016 // throwSyntaxError("')' expected after 'exit-status'.");
2019 private void expressionList() {
2022 if (token == TokenNameCOMMA) {
2030 private Expression expr() {
2032 // | expr_without_variable
2033 // if (token!=TokenNameEOF) {
2034 if (Scanner.TRACE) {
2035 System.out.println("TRACE: expr()");
2037 return expr_without_variable(true);
2041 private Expression expr_without_variable(boolean only_variable) {
2042 int exprSourceStart = scanner.getCurrentTokenStartPosition();
2043 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2044 Expression expression = new Expression();
2045 expression.sourceStart = exprSourceStart;
2046 // default, may be overwritten
2047 expression.sourceEnd = exprSourceEnd;
2049 // internal_functions_in_yacc
2058 // | T_INC rw_variable
2059 // | T_DEC rw_variable
2060 // | T_INT_CAST expr
2061 // | T_DOUBLE_CAST expr
2062 // | T_STRING_CAST expr
2063 // | T_ARRAY_CAST expr
2064 // | T_OBJECT_CAST expr
2065 // | T_BOOL_CAST expr
2066 // | T_UNSET_CAST expr
2067 // | T_EXIT exit_expr
2069 // | T_ARRAY '(' array_pair_list ')'
2070 // | '`' encaps_list '`'
2071 // | T_LIST '(' assignment_list ')' '=' expr
2072 // | T_NEW class_name_reference ctor_arguments
2073 // | variable '=' expr
2074 // | variable '=' '&' variable
2075 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2076 // | variable T_PLUS_EQUAL expr
2077 // | variable T_MINUS_EQUAL expr
2078 // | variable T_MUL_EQUAL expr
2079 // | variable T_DIV_EQUAL expr
2080 // | variable T_CONCAT_EQUAL expr
2081 // | variable T_MOD_EQUAL expr
2082 // | variable T_AND_EQUAL expr
2083 // | variable T_OR_EQUAL expr
2084 // | variable T_XOR_EQUAL expr
2085 // | variable T_SL_EQUAL expr
2086 // | variable T_SR_EQUAL expr
2087 // | rw_variable T_INC
2088 // | rw_variable T_DEC
2089 // | expr T_BOOLEAN_OR expr
2090 // | expr T_BOOLEAN_AND expr
2091 // | expr T_LOGICAL_OR expr
2092 // | expr T_LOGICAL_AND expr
2093 // | expr T_LOGICAL_XOR expr
2105 // | expr T_IS_IDENTICAL expr
2106 // | expr T_IS_NOT_IDENTICAL expr
2107 // | expr T_IS_EQUAL expr
2108 // | expr T_IS_NOT_EQUAL expr
2110 // | expr T_IS_SMALLER_OR_EQUAL expr
2112 // | expr T_IS_GREATER_OR_EQUAL expr
2113 // | expr T_INSTANCEOF class_name_reference
2114 // | expr '?' expr ':' expr
2115 if (Scanner.TRACE) {
2116 System.out.println("TRACE: expr_without_variable() PART 1");
2119 case TokenNameisset:
2120 case TokenNameempty:
2122 case TokenNameinclude:
2123 case TokenNameinclude_once:
2124 case TokenNamerequire:
2125 case TokenNamerequire_once:
2126 internal_functions_in_yacc();
2129 case TokenNameLPAREN:
2132 if (token == TokenNameRPAREN) {
2135 throwSyntaxError("')' expected in expression.");
2145 // | T_INT_CAST expr
2146 // | T_DOUBLE_CAST expr
2147 // | T_STRING_CAST expr
2148 // | T_ARRAY_CAST expr
2149 // | T_OBJECT_CAST expr
2150 // | T_BOOL_CAST expr
2151 // | T_UNSET_CAST expr
2152 case TokenNameclone:
2153 case TokenNameprint:
2156 case TokenNameMINUS:
2158 case TokenNameTWIDDLE:
2159 case TokenNameintCAST:
2160 case TokenNamedoubleCAST:
2161 case TokenNamestringCAST:
2162 case TokenNamearrayCAST:
2163 case TokenNameobjectCAST:
2164 case TokenNameboolCAST:
2165 case TokenNameunsetCAST:
2175 //| T_STRING_VARNAME
2177 //| T_START_HEREDOC encaps_list T_END_HEREDOC
2178 // | '`' encaps_list '`'
2180 // | '`' encaps_list '`'
2181 case TokenNameEncapsedString0:
2182 scanner.encapsedStringStack.push(new Character('`'));
2185 if (token == TokenNameEncapsedString0) {
2188 if (token != TokenNameEncapsedString0) {
2189 throwSyntaxError("\'`\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2193 scanner.encapsedStringStack.pop();
2197 // | '\'' encaps_list '\''
2198 case TokenNameEncapsedString1:
2199 scanner.encapsedStringStack.push(new Character('\''));
2202 exprSourceStart = scanner.getCurrentTokenStartPosition();
2203 if (token == TokenNameEncapsedString1) {
2204 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2205 .getCurrentTokenEndPosition());
2208 if (token != TokenNameEncapsedString1) {
2209 throwSyntaxError("\'\'\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2211 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2212 .getCurrentTokenEndPosition());
2216 scanner.encapsedStringStack.pop();
2220 //| '"' encaps_list '"'
2221 case TokenNameEncapsedString2:
2222 scanner.encapsedStringStack.push(new Character('"'));
2225 exprSourceStart = scanner.getCurrentTokenStartPosition();
2226 if (token == TokenNameEncapsedString2) {
2227 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2228 .getCurrentTokenEndPosition());
2231 if (token != TokenNameEncapsedString2) {
2232 throwSyntaxError("'\"' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2234 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2235 .getCurrentTokenEndPosition());
2239 scanner.encapsedStringStack.pop();
2243 case TokenNameStringDoubleQuote:
2244 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2245 .getCurrentTokenEndPosition());
2248 case TokenNameStringSingleQuote:
2249 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2250 .getCurrentTokenEndPosition());
2253 case TokenNameIntegerLiteral:
2254 case TokenNameDoubleLiteral:
2255 case TokenNameStringInterpolated:
2258 case TokenNameCLASS_C:
2259 case TokenNameMETHOD_C:
2260 case TokenNameFUNC_C:
2263 case TokenNameHEREDOC:
2266 case TokenNamearray:
2267 // T_ARRAY '(' array_pair_list ')'
2269 if (token == TokenNameLPAREN) {
2271 if (token == TokenNameRPAREN) {
2276 if (token != TokenNameRPAREN) {
2277 throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2281 throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2285 // | T_LIST '(' assignment_list ')' '=' expr
2287 if (token == TokenNameLPAREN) {
2290 if (token != TokenNameRPAREN) {
2291 throwSyntaxError("')' expected after 'list' keyword.");
2294 if (token != TokenNameEQUAL) {
2295 throwSyntaxError("'=' expected after 'list' keyword.");
2300 throwSyntaxError("'(' expected after 'list' keyword.");
2304 // | T_NEW class_name_reference ctor_arguments
2306 Expression typeRef = class_name_reference();
2308 if (typeRef != null) {
2309 expression = typeRef;
2312 // | T_INC rw_variable
2313 // | T_DEC rw_variable
2314 case TokenNamePLUS_PLUS:
2315 case TokenNameMINUS_MINUS:
2319 // | variable '=' expr
2320 // | variable '=' '&' variable
2321 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2322 // | variable T_PLUS_EQUAL expr
2323 // | variable T_MINUS_EQUAL expr
2324 // | variable T_MUL_EQUAL expr
2325 // | variable T_DIV_EQUAL expr
2326 // | variable T_CONCAT_EQUAL expr
2327 // | variable T_MOD_EQUAL expr
2328 // | variable T_AND_EQUAL expr
2329 // | variable T_OR_EQUAL expr
2330 // | variable T_XOR_EQUAL expr
2331 // | variable T_SL_EQUAL expr
2332 // | variable T_SR_EQUAL expr
2333 // | rw_variable T_INC
2334 // | rw_variable T_DEC
2335 case TokenNameIdentifier:
2336 case TokenNameVariable:
2337 case TokenNameDOLLAR:
2338 boolean rememberedVar = false;
2339 Expression lhs = variable();
2341 case TokenNameEQUAL:
2343 if (token == TokenNameAND) {
2345 if (token == TokenNamenew) {
2346 // | variable '=' '&' T_NEW class_name_reference
2349 SingleTypeReference classRef = class_name_reference();
2351 if (classRef != null) {
2352 if (lhs != null && lhs instanceof FieldReference) {
2354 // $var = & new Object();
2355 if (fMethodVariables != null) {
2356 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2357 lhsInfo.reference = classRef;
2358 lhsInfo.typeIdentifier = classRef.token;
2359 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2360 rememberedVar = true;
2365 Expression rhs = variable();
2366 if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2369 if (fMethodVariables != null) {
2370 VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2371 if (rhsInfo != null && rhsInfo.reference != null) {
2372 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2373 lhsInfo.reference = rhsInfo.reference;
2374 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2375 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2376 rememberedVar = true;
2382 Expression rhs = expr();
2383 if (lhs != null && lhs instanceof FieldReference) {
2384 if (rhs != null && rhs instanceof FieldReference) {
2387 if (fMethodVariables != null) {
2388 VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2389 if (rhsInfo != null && rhsInfo.reference != null) {
2390 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2391 lhsInfo.reference = rhsInfo.reference;
2392 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2393 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2394 rememberedVar = true;
2397 } else if (rhs != null && rhs instanceof SingleTypeReference) {
2399 // $var = new Object();
2400 if (fMethodVariables != null) {
2401 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2402 lhsInfo.reference = (SingleTypeReference) rhs;
2403 lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2404 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2405 rememberedVar = true;
2410 if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2411 if (fMethodVariables != null) {
2412 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2413 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2417 case TokenNamePLUS_EQUAL:
2418 case TokenNameMINUS_EQUAL:
2419 case TokenNameMULTIPLY_EQUAL:
2420 case TokenNameDIVIDE_EQUAL:
2421 case TokenNameDOT_EQUAL:
2422 case TokenNameREMAINDER_EQUAL:
2423 case TokenNameAND_EQUAL:
2424 case TokenNameOR_EQUAL:
2425 case TokenNameXOR_EQUAL:
2426 case TokenNameRIGHT_SHIFT_EQUAL:
2427 case TokenNameLEFT_SHIFT_EQUAL:
2431 case TokenNamePLUS_PLUS:
2432 case TokenNameMINUS_MINUS:
2436 if (!only_variable) {
2437 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2445 if (token != TokenNameINLINE_HTML) {
2446 if (token > TokenNameKEYWORD) {
2450 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2455 if (Scanner.TRACE) {
2456 System.out.println("TRACE: expr_without_variable() PART 2");
2458 // | expr T_BOOLEAN_OR expr
2459 // | expr T_BOOLEAN_AND expr
2460 // | expr T_LOGICAL_OR expr
2461 // | expr T_LOGICAL_AND expr
2462 // | expr T_LOGICAL_XOR expr
2474 // | expr T_IS_IDENTICAL expr
2475 // | expr T_IS_NOT_IDENTICAL expr
2476 // | expr T_IS_EQUAL expr
2477 // | expr T_IS_NOT_EQUAL expr
2479 // | expr T_IS_SMALLER_OR_EQUAL expr
2481 // | expr T_IS_GREATER_OR_EQUAL expr
2484 case TokenNameOR_OR:
2486 expression = new OR_OR_Expression(expression, expr(), token);
2488 case TokenNameAND_AND:
2490 expression = new AND_AND_Expression(expression, expr(), token);
2492 case TokenNameEQUAL_EQUAL:
2494 expression = new EqualExpression(expression, expr(), token);
2504 case TokenNameMINUS:
2505 case TokenNameMULTIPLY:
2506 case TokenNameDIVIDE:
2507 case TokenNameREMAINDER:
2508 case TokenNameLEFT_SHIFT:
2509 case TokenNameRIGHT_SHIFT:
2510 case TokenNameEQUAL_EQUAL_EQUAL:
2511 case TokenNameNOT_EQUAL_EQUAL:
2512 case TokenNameNOT_EQUAL:
2514 case TokenNameLESS_EQUAL:
2515 case TokenNameGREATER:
2516 case TokenNameGREATER_EQUAL:
2518 expression = new BinaryExpression(expression, expr(), token);
2520 // | expr T_INSTANCEOF class_name_reference
2521 // | expr '?' expr ':' expr
2522 case TokenNameinstanceof:
2524 TypeReference classRef = class_name_reference();
2525 expression = new InstanceOfExpression(expression, classRef, OperatorIds.INSTANCEOF);
2526 expression.sourceStart = exprSourceStart;
2527 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2529 case TokenNameQUESTION:
2531 Expression valueIfTrue = expr();
2532 if (token != TokenNameCOLON) {
2533 throwSyntaxError("':' expected in conditional expression.");
2536 Expression valueIfFalse = expr();
2538 expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2544 } catch (SyntaxError e) {
2545 // try to find next token after expression with errors:
2546 if (token == TokenNameSEMICOLON) {
2550 if (token == TokenNameRBRACE || token == TokenNameRPAREN || token == TokenNameRBRACKET) {
2558 private SingleTypeReference class_name_reference() {
2559 // class_name_reference:
2561 //| dynamic_class_name_reference
2562 SingleTypeReference ref = null;
2563 if (Scanner.TRACE) {
2564 System.out.println("TRACE: class_name_reference()");
2566 if (token == TokenNameIdentifier) {
2567 ref = new SingleTypeReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2571 dynamic_class_name_reference();
2576 private void dynamic_class_name_reference() {
2577 //dynamic_class_name_reference:
2578 // base_variable T_OBJECT_OPERATOR object_property
2579 // dynamic_class_name_variable_properties
2581 if (Scanner.TRACE) {
2582 System.out.println("TRACE: dynamic_class_name_reference()");
2585 if (token == TokenNameMINUS_GREATER) {
2588 dynamic_class_name_variable_properties();
2592 private void dynamic_class_name_variable_properties() {
2593 // dynamic_class_name_variable_properties:
2594 // dynamic_class_name_variable_properties
2595 // dynamic_class_name_variable_property
2597 if (Scanner.TRACE) {
2598 System.out.println("TRACE: dynamic_class_name_variable_properties()");
2600 while (token == TokenNameMINUS_GREATER) {
2601 dynamic_class_name_variable_property();
2605 private void dynamic_class_name_variable_property() {
2606 // dynamic_class_name_variable_property:
2607 // T_OBJECT_OPERATOR object_property
2608 if (Scanner.TRACE) {
2609 System.out.println("TRACE: dynamic_class_name_variable_property()");
2611 if (token == TokenNameMINUS_GREATER) {
2617 private void ctor_arguments() {
2620 //| '(' function_call_parameter_list ')'
2621 if (token == TokenNameLPAREN) {
2623 if (token == TokenNameRPAREN) {
2627 non_empty_function_call_parameter_list();
2628 if (token != TokenNameRPAREN) {
2629 throwSyntaxError("')' expected in ctor_arguments.");
2635 private void assignment_list() {
2637 // assignment_list ',' assignment_list_element
2638 //| assignment_list_element
2640 assignment_list_element();
2641 if (token != TokenNameCOMMA) {
2648 private void assignment_list_element() {
2649 //assignment_list_element:
2651 //| T_LIST '(' assignment_list ')'
2653 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
2656 if (token == TokenNamelist) {
2658 if (token == TokenNameLPAREN) {
2661 if (token != TokenNameRPAREN) {
2662 throwSyntaxError("')' expected after 'list' keyword.");
2666 throwSyntaxError("'(' expected after 'list' keyword.");
2672 private void array_pair_list() {
2675 //| non_empty_array_pair_list possible_comma
2676 non_empty_array_pair_list();
2677 if (token == TokenNameCOMMA) {
2682 private void non_empty_array_pair_list() {
2683 //non_empty_array_pair_list:
2684 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2685 //| non_empty_array_pair_list ',' expr
2686 //| expr T_DOUBLE_ARROW expr
2688 //| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2689 //| non_empty_array_pair_list ',' '&' w_variable
2690 //| expr T_DOUBLE_ARROW '&' w_variable
2693 if (token == TokenNameAND) {
2698 if (token == TokenNameAND) {
2701 } else if (token == TokenNameEQUAL_GREATER) {
2703 if (token == TokenNameAND) {
2711 if (token != TokenNameCOMMA) {
2715 if (token == TokenNameRPAREN) {
2721 // private void variableList() {
2724 // if (token == TokenNameCOMMA) {
2731 private Expression variable_without_objects() {
2732 // variable_without_objects:
2733 // reference_variable
2734 // | simple_indirect_reference reference_variable
2735 if (Scanner.TRACE) {
2736 System.out.println("TRACE: variable_without_objects()");
2738 while (token == TokenNameDOLLAR) {
2741 return reference_variable();
2744 private Expression function_call() {
2746 // T_STRING '(' function_call_parameter_list ')'
2747 //| class_constant '(' function_call_parameter_list ')'
2748 //| static_member '(' function_call_parameter_list ')'
2749 //| variable_without_objects '(' function_call_parameter_list ')'
2750 char[] defineName = null;
2751 char[] ident = null;
2754 Expression ref = null;
2755 if (Scanner.TRACE) {
2756 System.out.println("TRACE: function_call()");
2758 if (token == TokenNameIdentifier) {
2759 ident = scanner.getCurrentIdentifierSource();
2761 startPos = scanner.getCurrentTokenStartPosition();
2762 endPos = scanner.getCurrentTokenEndPosition();
2765 case TokenNamePAAMAYIM_NEKUDOTAYIM:
2769 if (token == TokenNameIdentifier) {
2774 variable_without_objects();
2779 ref = variable_without_objects();
2781 if (token != TokenNameLPAREN) {
2782 if (defineName != null) {
2783 // does this identifier contain only uppercase characters?
2784 if (defineName.length == 3) {
2785 if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2788 } else if (defineName.length == 4) {
2789 if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2791 } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
2794 } else if (defineName.length == 5) {
2795 if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
2799 if (defineName != null) {
2800 for (int i = 0; i < defineName.length; i++) {
2801 if (Character.isLowerCase(defineName[i])) {
2802 problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
2808 // TODO is this ok ?
2810 // throwSyntaxError("'(' expected in function call.");
2813 if (token == TokenNameRPAREN) {
2817 non_empty_function_call_parameter_list();
2818 if (token != TokenNameRPAREN) {
2819 String functionName;
2820 if (ident == null) {
2821 functionName = new String(" ");
2823 functionName = new String(ident);
2825 throwSyntaxError("')' expected in function call (" + functionName + ").");
2831 // private void function_call_parameter_list() {
2832 // function_call_parameter_list:
2833 // non_empty_function_call_parameter_list { $$ = $1; }
2836 private void non_empty_function_call_parameter_list() {
2837 //non_empty_function_call_parameter_list:
2838 // expr_without_variable
2841 // | non_empty_function_call_parameter_list ',' expr_without_variable
2842 // | non_empty_function_call_parameter_list ',' variable
2843 // | non_empty_function_call_parameter_list ',' '&' w_variable
2844 if (Scanner.TRACE) {
2845 System.out.println("TRACE: non_empty_function_call_parameter_list()");
2848 if (token == TokenNameAND) {
2852 // if (token == TokenNameIdentifier || token ==
2853 // TokenNameVariable
2854 // || token == TokenNameDOLLAR) {
2857 expr_without_variable(true);
2860 if (token != TokenNameCOMMA) {
2867 private void fully_qualified_class_name() {
2868 if (token == TokenNameIdentifier) {
2871 throwSyntaxError("Class name expected.");
2875 private void static_member() {
2877 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
2878 // variable_without_objects
2879 if (Scanner.TRACE) {
2880 System.out.println("TRACE: static_member()");
2882 fully_qualified_class_name();
2883 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
2884 throwSyntaxError("'::' expected after class name (static_member).");
2887 variable_without_objects();
2890 private Expression base_variable_with_function_calls() {
2891 // base_variable_with_function_calls:
2894 boolean functionCall = false;
2895 if (Scanner.TRACE) {
2896 System.out.println("TRACE: base_variable_with_function_calls()");
2898 // if (token == TokenNameIdentifier) {
2899 // functionCall = true;
2900 // } else if (token == TokenNameVariable) {
2901 // int tempToken = token;
2902 // int tempPosition = scanner.currentPosition;
2904 // if (token == TokenNameLPAREN) {
2905 // functionCall = true;
2907 // token = tempToken;
2908 // scanner.currentPosition = tempPosition;
2909 // scanner.phpMode = true;
2911 // if (functionCall) {
2912 return function_call();
2918 private Expression base_variable() {
2920 // reference_variable
2921 // | simple_indirect_reference reference_variable
2923 Expression ref = null;
2924 if (Scanner.TRACE) {
2925 System.out.println("TRACE: base_variable()");
2927 if (token == TokenNameIdentifier) {
2930 while (token == TokenNameDOLLAR) {
2933 reference_variable();
2938 // private void simple_indirect_reference() {
2939 // // simple_indirect_reference:
2941 // //| simple_indirect_reference '$'
2943 private Expression reference_variable() {
2944 // reference_variable:
2945 // reference_variable '[' dim_offset ']'
2946 // | reference_variable '{' expr '}'
2947 // | compound_variable
2948 Expression ref = null;
2949 if (Scanner.TRACE) {
2950 System.out.println("TRACE: reference_variable()");
2952 ref = compound_variable();
2954 if (token == TokenNameLBRACE) {
2958 if (token != TokenNameRBRACE) {
2959 throwSyntaxError("'}' expected in reference variable.");
2962 } else if (token == TokenNameLBRACKET) {
2965 if (token != TokenNameRBRACKET) {
2968 if (token != TokenNameRBRACKET) {
2969 throwSyntaxError("']' expected in reference variable.");
2980 private Expression compound_variable() {
2981 // compound_variable:
2983 // | '$' '{' expr '}'
2984 if (Scanner.TRACE) {
2985 System.out.println("TRACE: compound_variable()");
2987 if (token == TokenNameVariable) {
2988 FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2992 // because of simple_indirect_reference
2993 while (token == TokenNameDOLLAR) {
2996 if (token != TokenNameLBRACE) {
2997 reportSyntaxError("'{' expected after compound variable token '$'.");
3002 if (token != TokenNameRBRACE) {
3003 throwSyntaxError("'}' expected after compound variable token '$'.");
3010 // private void dim_offset() {
3016 private void object_property() {
3019 //| variable_without_objects
3020 if (Scanner.TRACE) {
3021 System.out.println("TRACE: object_property()");
3023 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
3024 variable_without_objects();
3030 private void object_dim_list() {
3032 // object_dim_list '[' dim_offset ']'
3033 //| object_dim_list '{' expr '}'
3035 if (Scanner.TRACE) {
3036 System.out.println("TRACE: object_dim_list()");
3040 if (token == TokenNameLBRACE) {
3043 if (token != TokenNameRBRACE) {
3044 throwSyntaxError("'}' expected in object_dim_list.");
3047 } else if (token == TokenNameLBRACKET) {
3049 if (token == TokenNameRBRACKET) {
3054 if (token != TokenNameRBRACKET) {
3055 throwSyntaxError("']' expected in object_dim_list.");
3064 private void variable_name() {
3068 if (Scanner.TRACE) {
3069 System.out.println("TRACE: variable_name()");
3071 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3072 if (token > TokenNameKEYWORD) {
3073 // TODO show a warning "Keyword used as variable" ?
3077 if (token != TokenNameLBRACE) {
3078 throwSyntaxError("'{' expected in variable name.");
3082 if (token != TokenNameRBRACE) {
3083 throwSyntaxError("'}' expected in variable name.");
3089 private void r_variable() {
3093 private void w_variable() {
3097 private void rw_variable() {
3101 private Expression variable() {
3103 // base_variable_with_function_calls T_OBJECT_OPERATOR
3104 // object_property method_or_not variable_properties
3105 // | base_variable_with_function_calls
3106 Expression ref = base_variable_with_function_calls();
3107 if (token == TokenNameMINUS_GREATER) {
3112 variable_properties();
3117 private void variable_properties() {
3118 // variable_properties:
3119 // variable_properties variable_property
3121 while (token == TokenNameMINUS_GREATER) {
3122 variable_property();
3126 private void variable_property() {
3127 // variable_property:
3128 // T_OBJECT_OPERATOR object_property method_or_not
3129 if (Scanner.TRACE) {
3130 System.out.println("TRACE: variable_property()");
3132 if (token == TokenNameMINUS_GREATER) {
3137 throwSyntaxError("'->' expected in variable_property.");
3141 private void method_or_not() {
3143 // '(' function_call_parameter_list ')'
3145 if (Scanner.TRACE) {
3146 System.out.println("TRACE: method_or_not()");
3148 if (token == TokenNameLPAREN) {
3150 if (token == TokenNameRPAREN) {
3154 non_empty_function_call_parameter_list();
3155 if (token != TokenNameRPAREN) {
3156 throwSyntaxError("')' expected in method_or_not.");
3162 private void exit_expr() {
3166 if (token != TokenNameLPAREN) {
3170 if (token == TokenNameRPAREN) {
3175 if (token != TokenNameRPAREN) {
3176 throwSyntaxError("')' expected after keyword 'exit'");
3181 private void encaps_list() {
3182 // encaps_list encaps_var
3183 // | encaps_list T_STRING
3184 // | encaps_list T_NUM_STRING
3185 // | encaps_list T_ENCAPSED_AND_WHITESPACE
3186 // | encaps_list T_CHARACTER
3187 // | encaps_list T_BAD_CHARACTER
3188 // | encaps_list '['
3189 // | encaps_list ']'
3190 // | encaps_list '{'
3191 // | encaps_list '}'
3192 // | encaps_list T_OBJECT_OPERATOR
3196 case TokenNameSTRING:
3199 case TokenNameLBRACE:
3200 // scanner.encapsedStringStack.pop();
3203 case TokenNameRBRACE:
3204 // scanner.encapsedStringStack.pop();
3207 case TokenNameLBRACKET:
3208 // scanner.encapsedStringStack.pop();
3211 case TokenNameRBRACKET:
3212 // scanner.encapsedStringStack.pop();
3215 case TokenNameMINUS_GREATER:
3216 // scanner.encapsedStringStack.pop();
3219 case TokenNameVariable:
3220 case TokenNameDOLLAR_LBRACE:
3221 case TokenNameLBRACE_DOLLAR:
3225 char encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
3226 if (encapsedChar == '$') {
3227 scanner.encapsedStringStack.pop();
3228 encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
3229 switch (encapsedChar) {
3231 if (token == TokenNameEncapsedString0) {
3234 token = TokenNameSTRING;
3237 if (token == TokenNameEncapsedString1) {
3240 token = TokenNameSTRING;
3243 if (token == TokenNameEncapsedString2) {
3246 token = TokenNameSTRING;
3255 private void encaps_var() {
3257 // | T_VARIABLE '[' encaps_var_offset ']'
3258 // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3259 // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3260 // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3261 // | T_CURLY_OPEN variable '}'
3263 case TokenNameVariable:
3265 if (token == TokenNameLBRACKET) {
3267 expr(); //encaps_var_offset();
3268 if (token != TokenNameRBRACKET) {
3269 throwSyntaxError("']' expected after variable.");
3271 // scanner.encapsedStringStack.pop();
3274 } else if (token == TokenNameMINUS_GREATER) {
3276 if (token != TokenNameIdentifier) {
3277 throwSyntaxError("Identifier expected after '->'.");
3279 // scanner.encapsedStringStack.pop();
3283 // // scanner.encapsedStringStack.pop();
3284 // int tempToken = TokenNameSTRING;
3285 // if (!scanner.encapsedStringStack.isEmpty()
3286 // && (token == TokenNameEncapsedString0
3287 // || token == TokenNameEncapsedString1
3288 // || token == TokenNameEncapsedString2 || token ==
3289 // TokenNameERROR)) {
3290 // char encapsedChar = ((Character)
3291 // scanner.encapsedStringStack.peek())
3294 // case TokenNameEncapsedString0 :
3295 // if (encapsedChar == '`') {
3296 // tempToken = TokenNameEncapsedString0;
3299 // case TokenNameEncapsedString1 :
3300 // if (encapsedChar == '\'') {
3301 // tempToken = TokenNameEncapsedString1;
3304 // case TokenNameEncapsedString2 :
3305 // if (encapsedChar == '"') {
3306 // tempToken = TokenNameEncapsedString2;
3309 // case TokenNameERROR :
3310 // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3311 // scanner.currentPosition--;
3317 // token = tempToken;
3320 case TokenNameDOLLAR_LBRACE:
3322 if (token == TokenNameDOLLAR_LBRACE) {
3324 } else if (token == TokenNameIdentifier) {
3326 if (token == TokenNameLBRACKET) {
3328 // if (token == TokenNameRBRACKET) {
3332 if (token != TokenNameRBRACKET) {
3333 throwSyntaxError("']' expected after '${'.");
3341 if (token != TokenNameRBRACE) {
3342 throwSyntaxError("'}' expected.");
3346 case TokenNameLBRACE_DOLLAR:
3348 if (token == TokenNameLBRACE_DOLLAR) {
3350 } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3352 if (token == TokenNameLBRACKET) {
3354 // if (token == TokenNameRBRACKET) {
3358 if (token != TokenNameRBRACKET) {
3359 throwSyntaxError("']' expected.");
3363 } else if (token == TokenNameMINUS_GREATER) {
3365 if (token != TokenNameIdentifier && token != TokenNameVariable) {
3366 throwSyntaxError("String or Variable token expected.");
3369 if (token == TokenNameLBRACKET) {
3371 // if (token == TokenNameRBRACKET) {
3375 if (token != TokenNameRBRACKET) {
3376 throwSyntaxError("']' expected after '${'.");
3382 // if (token != TokenNameRBRACE) {
3383 // throwSyntaxError("'}' expected after '{$'.");
3385 // // scanner.encapsedStringStack.pop();
3389 if (token != TokenNameRBRACE) {
3390 throwSyntaxError("'}' expected.");
3392 // scanner.encapsedStringStack.pop();
3399 private void encaps_var_offset() {
3404 case TokenNameSTRING:
3407 case TokenNameIntegerLiteral:
3410 case TokenNameVariable:
3413 case TokenNameIdentifier:
3417 throwSyntaxError("Variable or String token expected.");
3422 private void internal_functions_in_yacc() {
3425 case TokenNameisset:
3426 // T_ISSET '(' isset_variables ')'
3428 if (token != TokenNameLPAREN) {
3429 throwSyntaxError("'(' expected after keyword 'isset'");
3433 if (token != TokenNameRPAREN) {
3434 throwSyntaxError("')' expected after keyword 'isset'");
3438 case TokenNameempty:
3439 // T_EMPTY '(' variable ')'
3441 if (token != TokenNameLPAREN) {
3442 throwSyntaxError("'(' expected after keyword 'empty'");
3446 if (token != TokenNameRPAREN) {
3447 throwSyntaxError("')' expected after keyword 'empty'");
3451 case TokenNameinclude:
3453 checkFileName(token);
3455 case TokenNameinclude_once:
3456 // T_INCLUDE_ONCE expr
3457 checkFileName(token);
3460 // T_EVAL '(' expr ')'
3462 if (token != TokenNameLPAREN) {
3463 throwSyntaxError("'(' expected after keyword 'eval'");
3467 if (token != TokenNameRPAREN) {
3468 throwSyntaxError("')' expected after keyword 'eval'");
3472 case TokenNamerequire:
3474 checkFileName(token);
3476 case TokenNamerequire_once:
3477 // T_REQUIRE_ONCE expr
3478 checkFileName(token);
3483 private void checkFileName(int includeToken) {
3484 //<include-token> expr
3485 int start = scanner.getCurrentTokenStartPosition();
3486 boolean hasLPAREN = false;
3488 if (token == TokenNameLPAREN) {
3492 Expression expression = expr();
3494 if (token == TokenNameRPAREN) {
3497 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3500 char[] currTokenSource = scanner.getCurrentTokenSource(start);
3502 if (scanner.compilationUnit != null) {
3503 IResource resource = scanner.compilationUnit.getResource();
3504 if (resource != null && resource instanceof IFile) {
3505 file = (IFile) resource;
3509 tokens = new char[1][];
3510 tokens[0] = currTokenSource;
3512 ImportReference impt = new ImportReference(tokens, currTokenSource, start, scanner.getCurrentTokenEndPosition(), false);
3513 impt.declarationSourceEnd = impt.sourceEnd;
3514 impt.declarationEnd = impt.declarationSourceEnd;
3515 //endPosition is just before the ;
3516 impt.declarationSourceStart = start;
3517 includesList.add(impt);
3519 if (expression instanceof StringLiteral) {
3520 StringLiteral literal = (StringLiteral) expression;
3521 char[] includeName = literal.source();
3522 if (includeName.length == 0) {
3523 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3524 literal.sourceStart + 1);
3526 String includeNameString = new String(includeName);
3527 if (literal instanceof StringLiteralDQ) {
3528 if (includeNameString.indexOf('$') >= 0) {
3529 // assuming that the filename contains a variable => no filename check
3533 if (includeNameString.startsWith("http://")) {
3534 // assuming external include location
3538 // check the filename:
3539 // System.out.println(new String(compilationUnit.getFileName())+" - "+ expression.toStringExpression());
3540 IProject project = file.getProject();
3541 if (project != null) {
3542 IPath path = PHPFileUtil.determineFilePath(includeNameString, file, project);
3545 // reportSyntaxError("File: " + expression.toStringExpression() + " doesn't exist in project: "
3546 // + project.getLocation().toString(), literal.sourceStart, literal.sourceEnd);
3547 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3548 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3549 compilationUnit.compilationResult);
3552 // String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
3553 // String filePath = file.getRawLocation().toString();
3554 String filePath = path.toString();
3555 String ext = file.getRawLocation().getFileExtension();
3556 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
3559 impt.tokens = CharOperation.splitOn('/', filePath.toCharArray(), 0, filePath.length() - fileExtensionLength);
3560 impt.setFile(PHPFileUtil.createFile(path, project));
3561 } catch (Exception e) {
3562 // the file is outside of the workspace
3570 private void isset_variables() {
3572 // | isset_variables ','
3573 if (token == TokenNameRPAREN) {
3574 throwSyntaxError("Variable expected after keyword 'isset'");
3578 if (token == TokenNameCOMMA) {
3586 private boolean common_scalar() {
3590 // | T_CONSTANT_ENCAPSED_STRING
3597 case TokenNameIntegerLiteral:
3600 case TokenNameDoubleLiteral:
3603 case TokenNameStringDoubleQuote:
3606 case TokenNameStringSingleQuote:
3609 case TokenNameStringInterpolated:
3618 case TokenNameCLASS_C:
3621 case TokenNameMETHOD_C:
3624 case TokenNameFUNC_C:
3631 private void scalar() {
3634 //| T_STRING_VARNAME
3637 //| '"' encaps_list '"'
3638 //| '\'' encaps_list '\''
3639 //| T_START_HEREDOC encaps_list T_END_HEREDOC
3640 throwSyntaxError("Not yet implemented (scalar).");
3643 private void static_scalar() {
3644 // static_scalar: /* compile-time evaluated scalars */
3647 // | '+' static_scalar
3648 // | '-' static_scalar
3649 // | T_ARRAY '(' static_array_pair_list ')'
3650 // | static_class_constant
3651 if (common_scalar()) {
3655 case TokenNameIdentifier:
3657 // static_class_constant:
3658 // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3659 if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3661 if (token == TokenNameIdentifier) {
3664 throwSyntaxError("Identifier expected after '::' operator.");
3668 case TokenNameEncapsedString0:
3670 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3671 while (scanner.currentCharacter != '`') {
3672 if (scanner.currentCharacter == '\\') {
3673 scanner.currentPosition++;
3675 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3678 } catch (IndexOutOfBoundsException e) {
3679 throwSyntaxError("'`' expected at end of static string.");
3682 case TokenNameEncapsedString1:
3684 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3685 while (scanner.currentCharacter != '\'') {
3686 if (scanner.currentCharacter == '\\') {
3687 scanner.currentPosition++;
3689 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3692 } catch (IndexOutOfBoundsException e) {
3693 throwSyntaxError("'\'' expected at end of static string.");
3696 case TokenNameEncapsedString2:
3698 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3699 while (scanner.currentCharacter != '"') {
3700 if (scanner.currentCharacter == '\\') {
3701 scanner.currentPosition++;
3703 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3706 } catch (IndexOutOfBoundsException e) {
3707 throwSyntaxError("'\"' expected at end of static string.");
3714 case TokenNameMINUS:
3718 case TokenNamearray:
3720 if (token != TokenNameLPAREN) {
3721 throwSyntaxError("'(' expected after keyword 'array'");
3724 if (token == TokenNameRPAREN) {
3728 non_empty_static_array_pair_list();
3729 if (token != TokenNameRPAREN) {
3730 throwSyntaxError("')' or ',' expected after keyword 'array'");
3734 // case TokenNamenull :
3737 // case TokenNamefalse :
3740 // case TokenNametrue :
3744 throwSyntaxError("Static scalar/constant expected.");
3748 private void non_empty_static_array_pair_list() {
3749 // non_empty_static_array_pair_list:
3750 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3752 //| non_empty_static_array_pair_list ',' static_scalar
3753 //| static_scalar T_DOUBLE_ARROW static_scalar
3757 if (token == TokenNameEQUAL_GREATER) {
3761 if (token != TokenNameCOMMA) {
3765 if (token == TokenNameRPAREN) {
3771 public void reportSyntaxError() { //int act, int currentKind, int
3773 /* remember current scanner position */
3774 int startPos = scanner.startPosition;
3775 int currentPos = scanner.currentPosition;
3776 // String[] expectings;
3777 // String tokenName = name[symbol_index[currentKind]];
3778 //fetch all "accurate" possible terminals that could recover the error
3779 // int start, end = start = asi(stack[stateStackTop]);
3780 // while (asr[end] != 0)
3782 // int length = end - start;
3783 // expectings = new String[length];
3784 // if (length != 0) {
3785 // char[] indexes = new char[length];
3786 // System.arraycopy(asr, start, indexes, 0, length);
3787 // for (int i = 0; i < length; i++) {
3788 // expectings[i] = name[symbol_index[indexes[i]]];
3791 //if the pb is an EOF, try to tell the user that they are some
3792 // if (tokenName.equals(UNEXPECTED_EOF)) {
3793 // if (!this.checkAndReportBracketAnomalies(problemReporter())) {
3794 // char[] tokenSource;
3796 // tokenSource = this.scanner.getCurrentTokenSource();
3797 // } catch (Exception e) {
3798 // tokenSource = new char[] {};
3800 // problemReporter().parseError(
3801 // this.scanner.startPosition,
3802 // this.scanner.currentPosition - 1,
3807 // } else { //the next test is HEAVILY grammar DEPENDENT.
3808 // if ((length == 14)
3809 // && (expectings[0] == "=") //$NON-NLS-1$
3810 // && (expectings[1] == "*=") //$NON-NLS-1$
3811 // && (expressionPtr > -1)) {
3812 // switch(currentKind) {
3813 // case TokenNameSEMICOLON:
3814 // case TokenNamePLUS:
3815 // case TokenNameMINUS:
3816 // case TokenNameDIVIDE:
3817 // case TokenNameREMAINDER:
3818 // case TokenNameMULTIPLY:
3819 // case TokenNameLEFT_SHIFT:
3820 // case TokenNameRIGHT_SHIFT:
3821 //// case TokenNameUNSIGNED_RIGHT_SHIFT:
3822 // case TokenNameLESS:
3823 // case TokenNameGREATER:
3824 // case TokenNameLESS_EQUAL:
3825 // case TokenNameGREATER_EQUAL:
3826 // case TokenNameEQUAL_EQUAL:
3827 // case TokenNameNOT_EQUAL:
3828 // case TokenNameXOR:
3829 // case TokenNameAND:
3830 // case TokenNameOR:
3831 // case TokenNameOR_OR:
3832 // case TokenNameAND_AND:
3833 // // the ; is not the expected token ==> it ends a statement when an
3834 // expression is not ended
3835 // problemReporter().invalidExpressionAsStatement(expressionStack[expressionPtr]);
3837 // case TokenNameRBRACE :
3838 // problemReporter().missingSemiColon(expressionStack[expressionPtr]);
3841 // char[] tokenSource;
3843 // tokenSource = this.scanner.getCurrentTokenSource();
3844 // } catch (Exception e) {
3845 // tokenSource = new char[] {};
3847 // problemReporter().parseError(
3848 // this.scanner.startPosition,
3849 // this.scanner.currentPosition - 1,
3853 // this.checkAndReportBracketAnomalies(problemReporter());
3858 tokenSource = this.scanner.getCurrentTokenSource();
3859 } catch (Exception e) {
3860 tokenSource = new char[] {};
3862 // problemReporter().parseError(
3863 // this.scanner.startPosition,
3864 // this.scanner.currentPosition - 1,
3868 this.checkAndReportBracketAnomalies(problemReporter());
3871 /* reset scanner where it was */
3872 scanner.startPosition = startPos;
3873 scanner.currentPosition = currentPos;
3876 public static final int RoundBracket = 0;
3878 public static final int SquareBracket = 1;
3880 public static final int CurlyBracket = 2;
3882 public static final int BracketKinds = 3;
3884 protected int[] nestedMethod; //the ptr is nestedType
3886 protected int nestedType, dimensions;
3888 //variable set stack
3889 final static int VariableStackIncrement = 10;
3891 HashMap fTypeVariables = null;
3893 HashMap fMethodVariables = null;
3896 final static int AstStackIncrement = 100;
3898 protected int astPtr;
3900 protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
3902 protected int astLengthPtr;
3904 protected int[] astLengthStack;
3906 ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
3908 public CompilationUnitDeclaration compilationUnit; /*
3909 * the result from parse()
3912 protected ReferenceContext referenceContext;
3914 protected ProblemReporter problemReporter;
3916 protected CompilerOptions options;
3918 private ArrayList includesList;
3920 // protected CompilationResult compilationResult;
3922 * Returns this parser's problem reporter initialized with its reference context. Also it is assumed that a problem is going to be
3923 * reported, so initializes the compilation result's line positions.
3925 public ProblemReporter problemReporter() {
3926 if (scanner.recordLineSeparator) {
3927 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
3929 problemReporter.referenceContext = referenceContext;
3930 return problemReporter;
3934 * Reconsider the entire source looking for inconsistencies in {} () []
3936 public boolean checkAndReportBracketAnomalies(ProblemReporter problemReporter) {
3937 scanner.wasAcr = false;
3938 boolean anomaliesDetected = false;
3940 char[] source = scanner.source;
3941 int[] leftCount = { 0, 0, 0 };
3942 int[] rightCount = { 0, 0, 0 };
3943 int[] depths = { 0, 0, 0 };
3944 int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10] };
3945 int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
3946 int[][] rightPositions = new int[][] { new int[10], new int[10], new int[10] };
3947 int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10] };
3948 scanner.currentPosition = scanner.initialPosition; //starting
3950 // (first-zero-based
3952 while (scanner.currentPosition < scanner.eofPosition) { //loop for
3957 // ---------Consume white space and handles
3958 // startPosition---------
3959 boolean isWhiteSpace;
3961 scanner.startPosition = scanner.currentPosition;
3962 // if (((scanner.currentCharacter =
3963 // source[scanner.currentPosition++]) == '\\') &&
3964 // (source[scanner.currentPosition] == 'u')) {
3965 // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
3967 if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
3968 if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
3969 // only record line positions we have not
3971 scanner.pushLineSeparator();
3974 isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
3976 } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
3977 // -------consume token until } is found---------
3978 switch (scanner.currentCharacter) {
3980 int index = leftCount[CurlyBracket]++;
3981 if (index == leftPositions[CurlyBracket].length) {
3982 System.arraycopy(leftPositions[CurlyBracket], 0, (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
3983 System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] = new int[index * 2]), 0, index);
3985 leftPositions[CurlyBracket][index] = scanner.startPosition;
3986 leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
3990 int index = rightCount[CurlyBracket]++;
3991 if (index == rightPositions[CurlyBracket].length) {
3992 System.arraycopy(rightPositions[CurlyBracket], 0, (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
3993 System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] = new int[index * 2]), 0, index);
3995 rightPositions[CurlyBracket][index] = scanner.startPosition;
3996 rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4000 int index = leftCount[RoundBracket]++;
4001 if (index == leftPositions[RoundBracket].length) {
4002 System.arraycopy(leftPositions[RoundBracket], 0, (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4003 System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] = new int[index * 2]), 0, index);
4005 leftPositions[RoundBracket][index] = scanner.startPosition;
4006 leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4010 int index = rightCount[RoundBracket]++;
4011 if (index == rightPositions[RoundBracket].length) {
4012 System.arraycopy(rightPositions[RoundBracket], 0, (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4013 System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] = new int[index * 2]), 0, index);
4015 rightPositions[RoundBracket][index] = scanner.startPosition;
4016 rightDepths[RoundBracket][index] = --depths[RoundBracket];
4020 int index = leftCount[SquareBracket]++;
4021 if (index == leftPositions[SquareBracket].length) {
4022 System.arraycopy(leftPositions[SquareBracket], 0, (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4023 System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] = new int[index * 2]), 0, index);
4025 leftPositions[SquareBracket][index] = scanner.startPosition;
4026 leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4030 int index = rightCount[SquareBracket]++;
4031 if (index == rightPositions[SquareBracket].length) {
4032 System.arraycopy(rightPositions[SquareBracket], 0, (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4033 System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket] = new int[index * 2]), 0, index);
4035 rightPositions[SquareBracket][index] = scanner.startPosition;
4036 rightDepths[SquareBracket][index] = --depths[SquareBracket];
4040 if (scanner.getNextChar('\\')) {
4041 scanner.scanEscapeCharacter();
4042 } else { // consume next character
4043 scanner.unicodeAsBackSlash = false;
4044 // if (((scanner.currentCharacter =
4045 // source[scanner.currentPosition++]) ==
4047 // (source[scanner.currentPosition] ==
4049 // scanner.getNextUnicodeChar();
4051 if (scanner.withoutUnicodePtr != 0) {
4052 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4056 scanner.getNextChar('\'');
4060 // consume next character
4061 scanner.unicodeAsBackSlash = false;
4062 // if (((scanner.currentCharacter =
4063 // source[scanner.currentPosition++]) == '\\') &&
4064 // (source[scanner.currentPosition] == 'u')) {
4065 // scanner.getNextUnicodeChar();
4067 if (scanner.withoutUnicodePtr != 0) {
4068 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4071 while (scanner.currentCharacter != '"') {
4072 if (scanner.currentCharacter == '\r') {
4073 if (source[scanner.currentPosition] == '\n')
4074 scanner.currentPosition++;
4075 break; // the string cannot go further that
4078 if (scanner.currentCharacter == '\n') {
4079 break; // the string cannot go further that
4082 if (scanner.currentCharacter == '\\') {
4083 scanner.scanEscapeCharacter();
4085 // consume next character
4086 scanner.unicodeAsBackSlash = false;
4087 // if (((scanner.currentCharacter =
4088 // source[scanner.currentPosition++]) == '\\')
4089 // && (source[scanner.currentPosition] == 'u'))
4091 // scanner.getNextUnicodeChar();
4093 if (scanner.withoutUnicodePtr != 0) {
4094 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4101 if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4104 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4105 && (source[scanner.currentPosition] == 'u')) {
4106 //-------------unicode traitement
4108 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4109 scanner.currentPosition++;
4110 while (source[scanner.currentPosition] == 'u') {
4111 scanner.currentPosition++;
4113 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4114 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4115 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4116 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4120 scanner.currentCharacter = 'A';
4121 } //something different from \n and \r
4123 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4126 while (scanner.currentCharacter != '\r' && scanner.currentCharacter != '\n') {
4128 scanner.startPosition = scanner.currentPosition;
4129 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4130 && (source[scanner.currentPosition] == 'u')) {
4131 //-------------unicode traitement
4133 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4134 scanner.currentPosition++;
4135 while (source[scanner.currentPosition] == 'u') {
4136 scanner.currentPosition++;
4138 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4139 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4140 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4141 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4145 scanner.currentCharacter = 'A';
4146 } //something different from \n
4149 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4153 if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
4154 if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4155 // only record line positions we
4156 // have not recorded yet
4157 scanner.pushLineSeparator();
4158 if (this.scanner.taskTags != null) {
4159 this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner
4160 .getCurrentTokenEndPosition());
4166 if (test > 0) { //traditional and annotation
4168 boolean star = false;
4169 // consume next character
4170 scanner.unicodeAsBackSlash = false;
4171 // if (((scanner.currentCharacter =
4172 // source[scanner.currentPosition++]) ==
4174 // (source[scanner.currentPosition] ==
4176 // scanner.getNextUnicodeChar();
4178 if (scanner.withoutUnicodePtr != 0) {
4179 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4182 if (scanner.currentCharacter == '*') {
4186 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4187 && (source[scanner.currentPosition] == 'u')) {
4188 //-------------unicode traitement
4190 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4191 scanner.currentPosition++;
4192 while (source[scanner.currentPosition] == 'u') {
4193 scanner.currentPosition++;
4195 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4196 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4197 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4198 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4202 scanner.currentCharacter = 'A';
4203 } //something different from * and /
4205 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4208 //loop until end of comment */
4209 while ((scanner.currentCharacter != '/') || (!star)) {
4210 star = scanner.currentCharacter == '*';
4212 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4213 && (source[scanner.currentPosition] == 'u')) {
4214 //-------------unicode traitement
4216 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4217 scanner.currentPosition++;
4218 while (source[scanner.currentPosition] == 'u') {
4219 scanner.currentPosition++;
4221 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4222 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4223 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4224 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4228 scanner.currentCharacter = 'A';
4229 } //something different from * and
4232 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4236 if (this.scanner.taskTags != null) {
4237 this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
4244 if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4245 scanner.scanIdentifierOrKeyword(false);
4248 if (Character.isDigit(scanner.currentCharacter)) {
4249 scanner.scanNumber(false);
4253 //-----------------end switch while
4254 // try--------------------
4255 } catch (IndexOutOfBoundsException e) {
4256 break; // read until EOF
4257 } catch (InvalidInputException e) {
4258 return false; // no clue
4261 if (scanner.recordLineSeparator) {
4262 // compilationUnit.compilationResult.lineSeparatorPositions =
4263 // scanner.getLineEnds();
4265 // check placement anomalies against other kinds of brackets
4266 for (int kind = 0; kind < BracketKinds; kind++) {
4267 for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4268 int start = leftPositions[kind][leftIndex]; // deepest
4270 // find matching closing bracket
4271 int depth = leftDepths[kind][leftIndex];
4273 for (int i = 0; i < rightCount[kind]; i++) {
4274 int pos = rightPositions[kind][i];
4275 // want matching bracket further in source with same
4277 if ((pos > start) && (depth == rightDepths[kind][i])) {
4282 if (end < 0) { // did not find a good closing match
4283 problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult);
4286 // check if even number of opening/closing other brackets
4287 // in between this pair of brackets
4289 for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds); otherKind++) {
4290 for (int i = 0; i < leftCount[otherKind]; i++) {
4291 int pos = leftPositions[otherKind][i];
4292 if ((pos > start) && (pos < end))
4295 for (int i = 0; i < rightCount[otherKind]; i++) {
4296 int pos = rightPositions[otherKind][i];
4297 if ((pos > start) && (pos < end))
4301 problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult); //bracket
4307 // too many opening brackets ?
4308 for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4309 anomaliesDetected = true;
4310 problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i - 1], referenceContext,
4311 compilationUnit.compilationResult);
4313 // too many closing brackets ?
4314 for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4315 anomaliesDetected = true;
4316 problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext, compilationUnit.compilationResult);
4318 if (anomaliesDetected)
4321 return anomaliesDetected;
4322 } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4323 return anomaliesDetected;
4324 } catch (NullPointerException e) { // jdk1.2.2 jit bug
4325 return anomaliesDetected;
4329 protected void pushOnAstLengthStack(int pos) {
4331 astLengthStack[++astLengthPtr] = pos;
4332 } catch (IndexOutOfBoundsException e) {
4333 int oldStackLength = astLengthStack.length;
4334 int[] oldPos = astLengthStack;
4335 astLengthStack = new int[oldStackLength + StackIncrement];
4336 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4337 astLengthStack[astLengthPtr] = pos;
4341 protected void pushOnAstStack(ASTNode node) {
4343 * add a new obj on top of the ast stack
4346 astStack[++astPtr] = node;
4347 } catch (IndexOutOfBoundsException e) {
4348 int oldStackLength = astStack.length;
4349 ASTNode[] oldStack = astStack;
4350 astStack = new ASTNode[oldStackLength + AstStackIncrement];
4351 System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4352 astPtr = oldStackLength;
4353 astStack[astPtr] = node;
4356 astLengthStack[++astLengthPtr] = 1;
4357 } catch (IndexOutOfBoundsException e) {
4358 int oldStackLength = astLengthStack.length;
4359 int[] oldPos = astLengthStack;
4360 astLengthStack = new int[oldStackLength + AstStackIncrement];
4361 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4362 astLengthStack[astLengthPtr] = 1;
4366 protected void resetModifiers() {
4367 this.modifiers = AccDefault;
4368 this.modifiersSourceStart = -1; // <-- see comment into
4369 // modifiersFlag(int)
4370 this.scanner.commentPtr = -1;
4373 protected void consumePackageDeclarationName(IFile file) {
4374 // create a package name similar to java package names
4375 String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4376 String filePath = file.getRawLocation().toString();
4377 String ext = file.getRawLocation().getFileExtension();
4378 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4379 ImportReference impt;
4382 if (filePath.startsWith(projectPath)) {
4383 tokens = CharOperation
4384 .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4386 String name = file.getName();
4387 tokens = new char[1][];
4388 tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4391 this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4393 impt.declarationSourceStart = 0;
4394 impt.declarationSourceEnd = 0;
4395 impt.declarationEnd = 0;
4396 //endPosition is just before the ;