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;
12 import java.util.HashSet;
14 import net.sourceforge.phpdt.core.compiler.CharOperation;
15 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
16 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
17 import net.sourceforge.phpdt.internal.compiler.ast.AND_AND_Expression;
18 import net.sourceforge.phpdt.internal.compiler.ast.ASTNode;
19 import net.sourceforge.phpdt.internal.compiler.ast.AbstractMethodDeclaration;
20 import net.sourceforge.phpdt.internal.compiler.ast.BinaryExpression;
21 import net.sourceforge.phpdt.internal.compiler.ast.Block;
22 import net.sourceforge.phpdt.internal.compiler.ast.BreakStatement;
23 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
24 import net.sourceforge.phpdt.internal.compiler.ast.ConditionalExpression;
25 import net.sourceforge.phpdt.internal.compiler.ast.ContinueStatement;
26 import net.sourceforge.phpdt.internal.compiler.ast.EqualExpression;
27 import net.sourceforge.phpdt.internal.compiler.ast.Expression;
28 import net.sourceforge.phpdt.internal.compiler.ast.FieldDeclaration;
29 import net.sourceforge.phpdt.internal.compiler.ast.FieldReference;
30 import net.sourceforge.phpdt.internal.compiler.ast.IfStatement;
31 import net.sourceforge.phpdt.internal.compiler.ast.ImportReference;
32 import net.sourceforge.phpdt.internal.compiler.ast.InstanceOfExpression;
33 import net.sourceforge.phpdt.internal.compiler.ast.MethodDeclaration;
34 import net.sourceforge.phpdt.internal.compiler.ast.OR_OR_Expression;
35 import net.sourceforge.phpdt.internal.compiler.ast.OperatorIds;
36 import net.sourceforge.phpdt.internal.compiler.ast.ReturnStatement;
37 import net.sourceforge.phpdt.internal.compiler.ast.SingleTypeReference;
38 import net.sourceforge.phpdt.internal.compiler.ast.Statement;
39 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteral;
40 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteralDQ;
41 import net.sourceforge.phpdt.internal.compiler.ast.StringLiteralSQ;
42 import net.sourceforge.phpdt.internal.compiler.ast.TypeDeclaration;
43 import net.sourceforge.phpdt.internal.compiler.ast.TypeReference;
44 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
45 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
46 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
47 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
48 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
49 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
50 import net.sourceforge.phpdt.internal.compiler.util.Util;
51 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
52 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
53 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
55 import org.eclipse.core.resources.IFile;
56 import org.eclipse.core.resources.IProject;
57 import org.eclipse.core.resources.IResource;
58 import org.eclipse.core.runtime.IPath;
60 public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation {
61 protected final static int StackIncrement = 255;
63 protected int stateStackTop;
65 // protected int[] stack = new int[StackIncrement];
67 public int firstToken; // handle for multiple parsing goals
69 public int lastAct; // handle for multiple parsing goals
71 // protected RecoveredElement currentElement;
73 public static boolean VERBOSE_RECOVERY = false;
75 protected boolean diet = false; // tells the scanner to jump over some
78 * the PHP token scanner
80 public Scanner scanner;
84 protected int modifiers;
86 protected int modifiersSourceStart;
88 protected Parser(ProblemReporter problemReporter) {
89 this.problemReporter = problemReporter;
90 this.options = problemReporter.options;
91 this.token = TokenNameEOF;
92 this.initializeScanner();
95 public void setFileToParse(IFile fileToParse) {
96 this.token = TokenNameEOF;
97 this.initializeScanner();
101 * ClassDeclaration Constructor.
105 * Description of Parameter
108 public Parser(IFile fileToParse) {
109 // if (keywordMap == null) {
110 // keywordMap = new HashMap();
111 // for (int i = 0; i < PHP_KEYWORS.length; i++) {
112 // keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
115 // this.currentPHPString = 0;
116 // PHPParserSuperclass.fileToParse = fileToParse;
117 // this.phpList = null;
118 this.includesList = null;
120 this.token = TokenNameEOF;
122 // this.rowCount = 1;
123 // this.columnCount = 0;
124 // this.phpEnd = false;
126 this.initializeScanner();
129 public void initializeScanner() {
130 this.scanner = new Scanner(false /* comment */, false /* whitespace */, this.options
131 .getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore /* nls */, false, false,
132 this.options.taskTags/* taskTags */, this.options.taskPriorites/* taskPriorities */, true/* isTaskCaseSensitive */);
136 * Create marker for the parse error
138 // private void setMarker(String message, int charStart, int charEnd, int
140 // setMarker(fileToParse, message, charStart, charEnd, errorLevel);
143 * This method will throw the SyntaxError. It will add the good lines and
144 * columns to the Error
148 * @throws SyntaxError
151 private void throwSyntaxError(String error) {
152 int problemStartPosition = scanner.getCurrentTokenStartPosition();
153 int problemEndPosition = scanner.getCurrentTokenEndPosition() + 1;
154 if (scanner.source.length <= problemEndPosition && problemEndPosition > 0) {
155 problemEndPosition = scanner.source.length - 1;
156 if (problemStartPosition > 0 && problemStartPosition >= problemEndPosition && problemEndPosition > 0) {
157 problemStartPosition = problemEndPosition - 1;
160 throwSyntaxError(error, problemStartPosition, problemEndPosition);
164 * This method will throw the SyntaxError. It will add the good lines and
165 * columns to the Error
169 * @throws SyntaxError
172 // private void throwSyntaxError(String error, int startRow) {
173 // throw new SyntaxError(startRow, 0, " ", error);
175 private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
176 if (referenceContext != null) {
177 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
178 compilationUnit.compilationResult);
180 throw new SyntaxError(1, 0, " ", error);
183 private void reportSyntaxError(String error) {
184 int problemStartPosition = scanner.getCurrentTokenStartPosition();
185 int problemEndPosition = scanner.getCurrentTokenEndPosition();
186 reportSyntaxError(error, problemStartPosition, problemEndPosition + 1);
189 private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
190 if (referenceContext != null) {
191 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
192 compilationUnit.compilationResult);
196 // private void reportSyntaxWarning(String error, int problemStartPosition,
197 // int problemEndPosition) {
198 // if (referenceContext != null) {
199 // problemReporter.phpParsingWarning(new String[] { error },
200 // problemStartPosition, problemEndPosition, referenceContext,
201 // compilationUnit.compilationResult);
206 * gets the next token from input
208 private void getNextToken() {
210 token = scanner.getNextToken();
212 int currentEndPosition = scanner.getCurrentTokenEndPosition();
213 int currentStartPosition = scanner.getCurrentTokenStartPosition();
214 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
215 System.out.println(scanner.toStringAction(token));
217 } catch (InvalidInputException e) {
218 token = TokenNameERROR;
219 String detailedMessage = e.getMessage();
221 if (detailedMessage == Scanner.UNTERMINATED_STRING) {
222 throwSyntaxError("Unterminated string.");
223 } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
224 throwSyntaxError("Unterminated commment.");
230 public void init(String s) {
232 this.token = TokenNameEOF;
233 this.includesList = new ArrayList();
235 // this.rowCount = 1;
236 // this.columnCount = 0;
237 // this.phpEnd = false;
238 // this.phpMode = false;
239 /* scanner initialization */
240 scanner.setSource(s.toCharArray());
241 scanner.setPHPMode(false);
245 protected void initialize(boolean phpMode) {
246 initialize(phpMode, null);
249 protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
250 compilationUnit = null;
251 referenceContext = null;
252 this.includesList = new ArrayList();
253 // this.indexManager = indexManager;
255 this.token = TokenNameEOF;
257 // this.rowCount = 1;
258 // this.columnCount = 0;
259 // this.phpEnd = false;
260 // this.phpMode = phpMode;
261 scanner.setPHPMode(phpMode);
266 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?>
269 public void parse(String s) {
274 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?>
277 public void parse(String s, HashMap variables) {
278 fMethodVariables = variables;
279 fStackUnassigned = new ArrayList();
285 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?>
288 protected void parse() {
289 if (scanner.compilationUnit != null) {
290 IResource resource = scanner.compilationUnit.getResource();
291 if (resource != null && resource instanceof IFile) {
292 // set the package name
293 consumePackageDeclarationName((IFile) resource);
299 if (token != TokenNameEOF && token != TokenNameERROR) {
302 if (token != TokenNameEOF) {
303 if (token == TokenNameERROR) {
304 throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
306 if (token == TokenNameRPAREN) {
307 throwSyntaxError("Too many closing ')'; end-of-file not reached.");
309 if (token == TokenNameRBRACE) {
310 throwSyntaxError("Too many closing '}'; end-of-file not reached.");
312 if (token == TokenNameRBRACKET) {
313 throwSyntaxError("Too many closing ']'; end-of-file not reached.");
315 if (token == TokenNameLPAREN) {
316 throwSyntaxError("Read character '('; end-of-file not reached.");
318 if (token == TokenNameLBRACE) {
319 throwSyntaxError("Read character '{'; end-of-file not reached.");
321 if (token == TokenNameLBRACKET) {
322 throwSyntaxError("Read character '['; end-of-file not reached.");
324 throwSyntaxError("End-of-file not reached.");
327 } catch (SyntaxError syntaxError) {
328 // syntaxError.printStackTrace();
337 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?>
340 public void parseFunction(String s, HashMap variables) {
342 scanner.phpMode = true;
343 parseFunction(variables);
347 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?>
350 protected void parseFunction(HashMap variables) {
352 boolean hasModifiers = member_modifiers();
353 if (token == TokenNamefunction) {
355 checkAndSetModifiers(AccPublic);
357 this.fMethodVariables = variables;
359 MethodDeclaration methodDecl = new MethodDeclaration(null);
360 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
361 methodDecl.modifiers = this.modifiers;
362 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
365 functionDefinition(methodDecl);
366 } catch (SyntaxError sytaxErr1) {
369 int sourceEnd = methodDecl.sourceEnd;
370 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
371 sourceEnd = methodDecl.declarationSourceStart + 1;
373 methodDecl.sourceEnd = sourceEnd;
374 methodDecl.declarationSourceEnd = sourceEnd;
379 protected CompilationUnitDeclaration endParse(int act) {
383 // if (currentElement != null) {
384 // currentElement.topElement().updateParseTree();
385 // if (VERBOSE_RECOVERY) {
386 // System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
387 // System.out.println("--------------------------"); //$NON-NLS-1$
388 // System.out.println(compilationUnit);
389 // System.out.println("----------------------------------"); //$NON-NLS-1$
392 if (diet & VERBOSE_RECOVERY) {
393 System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
394 System.out.println("--------------------------"); //$NON-NLS-1$
395 System.out.println(compilationUnit);
396 System.out.println("----------------------------------"); //$NON-NLS-1$
399 if (scanner.recordLineSeparator) {
400 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
402 if (scanner.taskTags != null) {
403 for (int i = 0; i < scanner.foundTaskCount; i++) {
404 problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
405 scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
406 scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
409 compilationUnit.imports = new ImportReference[includesList.size()];
410 for (int i = 0; i < includesList.size(); i++) {
411 compilationUnit.imports[i] = (ImportReference) includesList.get(i);
413 return compilationUnit;
416 private Block statementList() {
417 boolean branchStatement = false;
419 int blockStart = scanner.getCurrentTokenStartPosition();
420 ArrayList blockStatements = new ArrayList();
423 statement = statement();
424 blockStatements.add(statement);
425 if (token == TokenNameEOF) {
428 if (branchStatement && statement != null) {
429 // reportSyntaxError("Unreachable code", statement.sourceStart,
430 // statement.sourceEnd);
431 problemReporter.unreachableCode(new String(scanner.getCurrentIdentifierSource()), statement.sourceStart,
432 statement.sourceEnd, referenceContext, compilationUnit.compilationResult);
434 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
435 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
436 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
437 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
438 return createBlock(blockStart, blockStatements);
440 branchStatement = checkUnreachableStatements(statement);
441 } catch (SyntaxError sytaxErr1) {
442 // if an error occured,
443 // try to find keywords
444 // to parse the rest of the string
445 boolean tokenize = scanner.tokenizeStrings;
447 scanner.tokenizeStrings = true;
450 while (token != TokenNameEOF) {
451 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
452 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
453 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
454 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
455 return createBlock(blockStart, blockStatements);
457 if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
458 || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
459 || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameECHO_INVISIBLE
460 || token == TokenNameglobal || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction
461 || token == TokenNamedeclare || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow
462 || token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
465 // System.out.println(scanner.toStringAction(token));
467 // System.out.println(scanner.toStringAction(token));
469 if (token == TokenNameEOF) {
473 scanner.tokenizeStrings = tokenize;
483 private boolean checkUnreachableStatements(Statement statement) {
484 if (statement instanceof ReturnStatement || statement instanceof ContinueStatement || statement instanceof BreakStatement) {
486 } else if (statement instanceof IfStatement && ((IfStatement) statement).checkUnreachable) {
494 * @param blockStatements
497 private Block createBlock(int blockStart, ArrayList blockStatements) {
498 int blockEnd = scanner.getCurrentTokenEndPosition();
499 Block b = Block.EmptyWith(blockStart, blockEnd);
500 b.statements = new Statement[blockStatements.size()];
501 blockStatements.toArray(b.statements);
505 private void functionBody(MethodDeclaration methodDecl) {
506 // '{' [statement-list] '}'
507 if (token == TokenNameLBRACE) {
510 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
511 throwSyntaxError("'{' expected in compound-statement.");
513 if (token != TokenNameRBRACE) {
516 if (token == TokenNameRBRACE) {
517 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
520 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
521 throwSyntaxError("'}' expected in compound-statement.");
525 private Statement statement() {
526 Statement statement = null;
527 Expression expression;
528 int sourceStart = scanner.getCurrentTokenStartPosition();
530 if (token == TokenNameif) {
531 // T_IF '(' expr ')' statement elseif_list else_single
532 // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
533 // new_else_single T_ENDIF ';'
535 if (token == TokenNameLPAREN) {
538 throwSyntaxError("'(' expected after 'if' keyword.");
541 if (token == TokenNameRPAREN) {
544 throwSyntaxError("')' expected after 'if' condition.");
546 // create basic IfStatement
547 IfStatement ifStatement = new IfStatement(expression, null, null, sourceStart, -1);
548 if (token == TokenNameCOLON) {
550 ifStatementColon(ifStatement);
552 ifStatement(ifStatement);
555 } else if (token == TokenNameswitch) {
557 if (token == TokenNameLPAREN) {
560 throwSyntaxError("'(' expected after 'switch' keyword.");
563 if (token == TokenNameRPAREN) {
566 throwSyntaxError("')' expected after 'switch' condition.");
570 } else if (token == TokenNamefor) {
572 if (token == TokenNameLPAREN) {
575 throwSyntaxError("'(' expected after 'for' keyword.");
577 if (token == TokenNameSEMICOLON) {
581 if (token == TokenNameSEMICOLON) {
584 throwSyntaxError("';' expected after 'for'.");
587 if (token == TokenNameSEMICOLON) {
591 if (token == TokenNameSEMICOLON) {
594 throwSyntaxError("';' expected after 'for'.");
597 if (token == TokenNameRPAREN) {
601 if (token == TokenNameRPAREN) {
604 throwSyntaxError("')' expected after 'for'.");
609 } else if (token == TokenNamewhile) {
611 if (token == TokenNameLPAREN) {
614 throwSyntaxError("'(' expected after 'while' keyword.");
617 if (token == TokenNameRPAREN) {
620 throwSyntaxError("')' expected after 'while' condition.");
624 } else if (token == TokenNamedo) {
626 if (token == TokenNameLBRACE) {
628 if (token != TokenNameRBRACE) {
631 if (token == TokenNameRBRACE) {
634 throwSyntaxError("'}' expected after 'do' keyword.");
639 if (token == TokenNamewhile) {
641 if (token == TokenNameLPAREN) {
644 throwSyntaxError("'(' expected after 'while' keyword.");
647 if (token == TokenNameRPAREN) {
650 throwSyntaxError("')' expected after 'while' condition.");
653 throwSyntaxError("'while' expected after 'do' keyword.");
655 if (token == TokenNameSEMICOLON) {
658 if (token != TokenNameINLINE_HTML) {
659 throwSyntaxError("';' expected after do-while statement.");
664 } else if (token == TokenNameforeach) {
666 if (token == TokenNameLPAREN) {
669 throwSyntaxError("'(' expected after 'foreach' keyword.");
672 if (token == TokenNameas) {
675 throwSyntaxError("'as' expected after 'foreach' exxpression.");
679 foreach_optional_arg();
680 if (token == TokenNameEQUAL_GREATER) {
682 variable(false, false);
684 if (token == TokenNameRPAREN) {
687 throwSyntaxError("')' expected after 'foreach' expression.");
691 } else if (token == TokenNamebreak) {
694 if (token != TokenNameSEMICOLON) {
697 if (token == TokenNameSEMICOLON) {
698 sourceEnd = scanner.getCurrentTokenEndPosition();
701 if (token != TokenNameINLINE_HTML) {
702 throwSyntaxError("';' expected after 'break'.");
704 sourceEnd = scanner.getCurrentTokenEndPosition();
707 return new BreakStatement(null, sourceStart, sourceEnd);
708 } else if (token == TokenNamecontinue) {
711 if (token != TokenNameSEMICOLON) {
714 if (token == TokenNameSEMICOLON) {
715 sourceEnd = scanner.getCurrentTokenEndPosition();
718 if (token != TokenNameINLINE_HTML) {
719 throwSyntaxError("';' expected after 'continue'.");
721 sourceEnd = scanner.getCurrentTokenEndPosition();
724 return new ContinueStatement(null, sourceStart, sourceEnd);
725 } else if (token == TokenNamereturn) {
728 if (token != TokenNameSEMICOLON) {
731 if (token == TokenNameSEMICOLON) {
732 sourceEnd = scanner.getCurrentTokenEndPosition();
735 if (token != TokenNameINLINE_HTML) {
736 throwSyntaxError("';' expected after 'return'.");
738 sourceEnd = scanner.getCurrentTokenEndPosition();
741 return new ReturnStatement(expression, sourceStart, sourceEnd);
742 } else if (token == TokenNameecho) {
745 if (token == TokenNameSEMICOLON) {
748 if (token != TokenNameINLINE_HTML) {
749 throwSyntaxError("';' expected after 'echo' statement.");
754 } else if (token == TokenNameECHO_INVISIBLE) {
755 // 0-length token directly after PHP short tag <?=
758 if (token == TokenNameSEMICOLON) {
760 if (token != TokenNameINLINE_HTML) {
761 // TODO should this become a configurable warning?
762 reportSyntaxError("Probably '?>' expected after PHP short tag expression (only the first expression will be echoed).");
765 if (token != TokenNameINLINE_HTML) {
766 throwSyntaxError("';' expected after PHP short tag '<?=' expression.");
771 } else if (token == TokenNameINLINE_HTML) {
774 } else if (token == TokenNameglobal) {
777 if (token == TokenNameSEMICOLON) {
780 if (token != TokenNameINLINE_HTML) {
781 throwSyntaxError("';' expected after 'global' statement.");
786 } else if (token == TokenNamestatic) {
789 if (token == TokenNameSEMICOLON) {
792 if (token != TokenNameINLINE_HTML) {
793 throwSyntaxError("';' expected after 'static' statement.");
798 } else if (token == TokenNameunset) {
800 if (token == TokenNameLPAREN) {
803 throwSyntaxError("'(' expected after 'unset' statement.");
806 if (token == TokenNameRPAREN) {
809 throwSyntaxError("')' expected after 'unset' statement.");
811 if (token == TokenNameSEMICOLON) {
814 if (token != TokenNameINLINE_HTML) {
815 throwSyntaxError("';' expected after 'unset' statement.");
820 } else if (token == TokenNamefunction) {
821 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
822 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
823 methodDecl.modifiers = AccDefault;
824 methodDecl.type = MethodDeclaration.FUNCTION_DEFINITION;
827 functionDefinition(methodDecl);
829 sourceEnd = methodDecl.sourceEnd;
830 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
831 sourceEnd = methodDecl.declarationSourceStart + 1;
833 methodDecl.declarationSourceEnd = sourceEnd;
834 methodDecl.sourceEnd = sourceEnd;
837 } else if (token == TokenNamedeclare) {
838 // T_DECLARE '(' declare_list ')' declare_statement
840 if (token != TokenNameLPAREN) {
841 throwSyntaxError("'(' expected in 'declare' statement.");
845 if (token != TokenNameRPAREN) {
846 throwSyntaxError("')' expected in 'declare' statement.");
851 } else if (token == TokenNametry) {
853 if (token != TokenNameLBRACE) {
854 throwSyntaxError("'{' expected in 'try' statement.");
858 if (token != TokenNameRBRACE) {
859 throwSyntaxError("'}' expected in 'try' statement.");
863 } else if (token == TokenNamecatch) {
865 if (token != TokenNameLPAREN) {
866 throwSyntaxError("'(' expected in 'catch' statement.");
869 fully_qualified_class_name();
870 if (token != TokenNameVariable) {
871 throwSyntaxError("Variable expected in 'catch' statement.");
875 if (token != TokenNameRPAREN) {
876 throwSyntaxError("')' expected in 'catch' statement.");
879 if (token != TokenNameLBRACE) {
880 throwSyntaxError("'{' expected in 'catch' statement.");
883 if (token != TokenNameRBRACE) {
885 if (token != TokenNameRBRACE) {
886 throwSyntaxError("'}' expected in 'catch' statement.");
890 additional_catches();
892 } else if (token == TokenNamethrow) {
895 if (token == TokenNameSEMICOLON) {
898 throwSyntaxError("';' expected after 'throw' exxpression.");
901 } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
903 TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
904 typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
905 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
906 typeDecl.name = new char[] { ' ' };
907 // default super class
908 typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
909 compilationUnit.types.add(typeDecl);
910 pushOnAstStack(typeDecl);
911 unticked_class_declaration_statement(typeDecl);
919 // throwSyntaxError("Unexpected keyword '" + keyword + "'");
920 } else if (token == TokenNameLBRACE) {
922 if (token != TokenNameRBRACE) {
923 statement = statementList();
925 if (token == TokenNameRBRACE) {
929 throwSyntaxError("'}' expected.");
932 if (token != TokenNameSEMICOLON) {
935 if (token == TokenNameSEMICOLON) {
939 if (token == TokenNameRBRACE) {
940 reportSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
942 if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
943 throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
953 private void declare_statement() {
955 // | ':' inner_statement_list T_ENDDECLARE ';'
957 if (token == TokenNameCOLON) {
959 // TODO: implement inner_statement_list();
961 if (token != TokenNameenddeclare) {
962 throwSyntaxError("'enddeclare' expected in 'declare' statement.");
965 if (token != TokenNameSEMICOLON) {
966 throwSyntaxError("';' expected after 'enddeclare' keyword.");
974 private void declare_list() {
975 // T_STRING '=' static_scalar
976 // | declare_list ',' T_STRING '=' static_scalar
978 if (token != TokenNameIdentifier) {
979 throwSyntaxError("Identifier expected in 'declare' list.");
982 if (token != TokenNameEQUAL) {
983 throwSyntaxError("'=' expected in 'declare' list.");
987 if (token != TokenNameCOMMA) {
994 private void additional_catches() {
995 while (token == TokenNamecatch) {
997 if (token != TokenNameLPAREN) {
998 throwSyntaxError("'(' expected in 'catch' statement.");
1001 fully_qualified_class_name();
1002 if (token != TokenNameVariable) {
1003 throwSyntaxError("Variable expected in 'catch' statement.");
1007 if (token != TokenNameRPAREN) {
1008 throwSyntaxError("')' expected in 'catch' statement.");
1011 if (token != TokenNameLBRACE) {
1012 throwSyntaxError("'{' expected in 'catch' statement.");
1015 if (token != TokenNameRBRACE) {
1018 if (token != TokenNameRBRACE) {
1019 throwSyntaxError("'}' expected in 'catch' statement.");
1025 private void foreach_variable() {
1028 if (token == TokenNameAND) {
1034 private void foreach_optional_arg() {
1036 // | T_DOUBLE_ARROW foreach_variable
1037 if (token == TokenNameEQUAL_GREATER) {
1043 private void global_var_list() {
1045 // global_var_list ',' global_var
1047 HashSet set = peekVariableSet();
1050 if (token != TokenNameCOMMA) {
1057 private void global_var(HashSet set) {
1061 // | '$' '{' expr '}'
1062 if (token == TokenNameVariable) {
1063 if (fMethodVariables != null) {
1064 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
1065 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1067 addVariableSet(set);
1069 } else if (token == TokenNameDOLLAR) {
1071 if (token == TokenNameLBRACE) {
1074 if (token != TokenNameRBRACE) {
1075 throwSyntaxError("'}' expected in global variable.");
1084 private void static_var_list() {
1086 // static_var_list ',' T_VARIABLE
1087 // | static_var_list ',' T_VARIABLE '=' static_scalar
1089 // | T_VARIABLE '=' static_scalar,
1090 HashSet set = peekVariableSet();
1092 if (token == TokenNameVariable) {
1093 if (fMethodVariables != null) {
1094 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
1095 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1097 addVariableSet(set);
1099 if (token == TokenNameEQUAL) {
1103 if (token != TokenNameCOMMA) {
1113 private void unset_variables() {
1116 // | unset_variables ',' unset_variable
1120 variable(false, false);
1121 if (token != TokenNameCOMMA) {
1128 private final void initializeModifiers() {
1130 this.modifiersSourceStart = -1;
1133 private final void checkAndSetModifiers(int flag) {
1134 this.modifiers |= flag;
1135 if (this.modifiersSourceStart < 0)
1136 this.modifiersSourceStart = this.scanner.startPosition;
1139 private void unticked_class_declaration_statement(TypeDeclaration typeDecl) {
1140 initializeModifiers();
1141 if (token == TokenNameinterface) {
1142 // interface_entry T_STRING
1143 // interface_extends_list
1144 // '{' class_statement_list '}'
1145 checkAndSetModifiers(AccInterface);
1147 typeDecl.modifiers = this.modifiers;
1148 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1149 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1150 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1151 typeDecl.name = scanner.getCurrentIdentifierSource();
1152 if (token > TokenNameKEYWORD) {
1153 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1154 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1155 // throwSyntaxError("Don't use a keyword for interface declaration ["
1156 // + scanner.toStringAction(token) + "].",
1157 // typeDecl.sourceStart, typeDecl.sourceEnd);
1160 interface_extends_list(typeDecl);
1162 typeDecl.name = new char[] { ' ' };
1163 throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1167 // class_entry_type T_STRING extends_from
1169 // '{' class_statement_list'}'
1171 typeDecl.modifiers = this.modifiers;
1172 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1173 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1175 // identifier 'extends' identifier
1176 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1177 typeDecl.name = scanner.getCurrentIdentifierSource();
1178 if (token > TokenNameKEYWORD) {
1179 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1180 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1181 // throwSyntaxError("Don't use a keyword for class declaration [" +
1182 // scanner.toStringAction(token) + "].",
1183 // typeDecl.sourceStart, typeDecl.sourceEnd);
1188 // | T_EXTENDS fully_qualified_class_name
1189 if (token == TokenNameextends) {
1190 interface_extends_list(typeDecl);
1192 // if (token != TokenNameIdentifier) {
1193 // throwSyntaxError("Class name expected after keyword
1195 // scanner.getCurrentTokenStartPosition(), scanner
1196 // .getCurrentTokenEndPosition());
1199 implements_list(typeDecl);
1201 typeDecl.name = new char[] { ' ' };
1202 throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1206 // '{' class_statement_list '}'
1207 if (token == TokenNameLBRACE) {
1209 if (token != TokenNameRBRACE) {
1210 ArrayList list = new ArrayList();
1211 class_statement_list(list);
1212 typeDecl.fields = new FieldDeclaration[list.size()];
1213 for (int i = 0; i < list.size(); i++) {
1214 typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1217 if (token == TokenNameRBRACE) {
1218 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1221 throwSyntaxError("'}' expected at end of class body.");
1224 throwSyntaxError("'{' expected at start of class body.");
1228 private void class_entry_type() {
1230 // | T_ABSTRACT T_CLASS
1231 // | T_FINAL T_CLASS
1232 if (token == TokenNameclass) {
1234 } else if (token == TokenNameabstract) {
1235 checkAndSetModifiers(AccAbstract);
1237 if (token != TokenNameclass) {
1238 throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1241 } else if (token == TokenNamefinal) {
1242 checkAndSetModifiers(AccFinal);
1244 if (token != TokenNameclass) {
1245 throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1249 throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1253 // private void class_extends(TypeDeclaration typeDecl) {
1255 // // | T_EXTENDS interface_list
1256 // if (token == TokenNameextends) {
1259 // if (token == TokenNameIdentifier) {
1262 // throwSyntaxError("Class name expected after keyword 'extends'.");
1267 private void interface_extends_list(TypeDeclaration typeDecl) {
1269 // | T_EXTENDS interface_list
1270 if (token == TokenNameextends) {
1276 private void implements_list(TypeDeclaration typeDecl) {
1278 // | T_IMPLEMENTS interface_list
1279 if (token == TokenNameimplements) {
1285 private void interface_list() {
1287 // fully_qualified_class_name
1288 // | interface_list ',' fully_qualified_class_name
1290 if (token == TokenNameIdentifier) {
1293 throwSyntaxError("Interface name expected after keyword 'implements'.");
1295 if (token != TokenNameCOMMA) {
1302 // private void classBody(TypeDeclaration typeDecl) {
1303 // //'{' [class-element-list] '}'
1304 // if (token == TokenNameLBRACE) {
1306 // if (token != TokenNameRBRACE) {
1307 // class_statement_list();
1309 // if (token == TokenNameRBRACE) {
1310 // typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1313 // throwSyntaxError("'}' expected at end of class body.");
1316 // throwSyntaxError("'{' expected at start of class body.");
1319 private void class_statement_list(ArrayList list) {
1322 class_statement(list);
1323 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1324 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1325 || token == TokenNameconst) {
1328 if (token == TokenNameRBRACE) {
1331 throwSyntaxError("'}' at end of class statement.");
1332 } catch (SyntaxError sytaxErr1) {
1333 boolean tokenize = scanner.tokenizeStrings;
1335 scanner.tokenizeStrings = true;
1338 // if an error occured,
1339 // try to find keywords
1340 // to parse the rest of the string
1341 while (token != TokenNameEOF) {
1342 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1343 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1344 || token == TokenNameconst) {
1347 // System.out.println(scanner.toStringAction(token));
1350 if (token == TokenNameEOF) {
1354 scanner.tokenizeStrings = tokenize;
1360 private void class_statement(ArrayList list) {
1362 // variable_modifiers class_variable_declaration ';'
1363 // | class_constant_declaration ';'
1364 // | method_modifiers T_FUNCTION is_reference T_STRING
1365 // '(' parameter_list ')' method_body
1366 initializeModifiers();
1367 int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1369 if (token == TokenNamevar) {
1370 checkAndSetModifiers(AccPublic);
1371 problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1372 referenceContext, compilationUnit.compilationResult);
1374 class_variable_declaration(declarationSourceStart, list);
1375 } else if (token == TokenNameconst) {
1376 checkAndSetModifiers(AccFinal | AccPublic);
1377 class_constant_declaration(declarationSourceStart, list);
1378 if (token != TokenNameSEMICOLON) {
1379 throwSyntaxError("';' expected after class const declaration.");
1383 boolean hasModifiers = member_modifiers();
1384 if (token == TokenNamefunction) {
1385 if (!hasModifiers) {
1386 checkAndSetModifiers(AccPublic);
1388 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1389 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1390 methodDecl.modifiers = this.modifiers;
1391 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
1394 functionDefinition(methodDecl);
1396 int sourceEnd = methodDecl.sourceEnd;
1397 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
1398 sourceEnd = methodDecl.declarationSourceStart + 1;
1400 methodDecl.declarationSourceEnd = sourceEnd;
1401 methodDecl.sourceEnd = sourceEnd;
1404 if (!hasModifiers) {
1405 throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1407 class_variable_declaration(declarationSourceStart, list);
1412 private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1413 // class_constant_declaration ',' T_STRING '=' static_scalar
1414 // | T_CONST T_STRING '=' static_scalar
1415 if (token != TokenNameconst) {
1416 throwSyntaxError("'const' keyword expected in class declaration.");
1421 if (token != TokenNameIdentifier) {
1422 throwSyntaxError("Identifier expected in class const declaration.");
1424 FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1425 .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1426 fieldDeclaration.modifiers = this.modifiers;
1427 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1428 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1429 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1430 // fieldDeclaration.type
1431 list.add(fieldDeclaration);
1433 if (token != TokenNameEQUAL) {
1434 throwSyntaxError("'=' expected in class const declaration.");
1438 if (token != TokenNameCOMMA) {
1439 break; // while(true)-loop
1445 // private void variable_modifiers() {
1446 // // variable_modifiers:
1447 // // non_empty_member_modifiers
1449 // initializeModifiers();
1450 // if (token == TokenNamevar) {
1451 // checkAndSetModifiers(AccPublic);
1452 // reportSyntaxError(
1453 // "Keyword 'var' is deprecated. Please use 'public' 'private' or
1455 // modifier for field declarations.",
1456 // scanner.getCurrentTokenStartPosition(), scanner
1457 // .getCurrentTokenEndPosition());
1460 // if (!member_modifiers()) {
1461 // throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1462 // field declarations.");
1466 // private void method_modifiers() {
1467 // //method_modifiers:
1469 // //| non_empty_member_modifiers
1470 // initializeModifiers();
1471 // if (!member_modifiers()) {
1472 // checkAndSetModifiers(AccPublic);
1475 private boolean member_modifiers() {
1482 boolean foundToken = false;
1484 if (token == TokenNamepublic) {
1485 checkAndSetModifiers(AccPublic);
1488 } else if (token == TokenNameprotected) {
1489 checkAndSetModifiers(AccProtected);
1492 } else if (token == TokenNameprivate) {
1493 checkAndSetModifiers(AccPrivate);
1496 } else if (token == TokenNamestatic) {
1497 checkAndSetModifiers(AccStatic);
1500 } else if (token == TokenNameabstract) {
1501 checkAndSetModifiers(AccAbstract);
1504 } else if (token == TokenNamefinal) {
1505 checkAndSetModifiers(AccFinal);
1515 private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1516 // class_variable_declaration:
1517 // class_variable_declaration ',' T_VARIABLE
1518 // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1520 // | T_VARIABLE '=' static_scalar
1521 char[] classVariable;
1523 if (token == TokenNameVariable) {
1524 classVariable = scanner.getCurrentIdentifierSource();
1525 // indexManager.addIdentifierInformation('v', classVariable, buf, -1,
1527 FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1528 .getCurrentTokenEndPosition());
1529 fieldDeclaration.modifiers = this.modifiers;
1530 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1531 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1532 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1533 list.add(fieldDeclaration);
1534 if (fTypeVariables != null) {
1535 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_CLASS_UNIT);
1536 fTypeVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1539 if (token == TokenNameEQUAL) {
1544 // if (token == TokenNamethis) {
1545 // throwSyntaxError("'$this' not allowed after keyword 'public'
1546 // 'protected' 'private' 'var'.");
1548 throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1550 if (token != TokenNameCOMMA) {
1555 if (token != TokenNameSEMICOLON) {
1556 throwSyntaxError("';' expected after field declaration.");
1561 private void functionDefinition(MethodDeclaration methodDecl) {
1562 boolean isAbstract = false;
1564 if (compilationUnit != null) {
1565 compilationUnit.types.add(methodDecl);
1568 ASTNode node = astStack[astPtr];
1569 if (node instanceof TypeDeclaration) {
1570 TypeDeclaration typeDecl = ((TypeDeclaration) node);
1571 if (typeDecl.methods == null) {
1572 typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1574 AbstractMethodDeclaration[] newMethods;
1575 System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 0,
1576 typeDecl.methods.length);
1577 newMethods[typeDecl.methods.length] = methodDecl;
1578 typeDecl.methods = newMethods;
1580 if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1582 } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1588 pushFunctionVariableSet();
1589 functionDeclarator(methodDecl);
1590 if (token == TokenNameSEMICOLON) {
1592 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1593 throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1598 functionBody(methodDecl);
1600 if (!fStackUnassigned.isEmpty()) {
1601 fStackUnassigned.remove(fStackUnassigned.size() - 1);
1606 private void functionDeclarator(MethodDeclaration methodDecl) {
1607 // identifier '(' [parameter-list] ')'
1608 if (token == TokenNameAND) {
1611 methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1612 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1613 if (Scanner.isIdentifierOrKeyword(token)) {
1614 methodDecl.selector = scanner.getCurrentIdentifierSource();
1615 if (token > TokenNameKEYWORD) {
1616 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1617 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1620 if (token == TokenNameLPAREN) {
1623 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1624 throwSyntaxError("'(' expected in function declaration.");
1626 if (token != TokenNameRPAREN) {
1627 parameter_list(methodDecl);
1629 if (token != TokenNameRPAREN) {
1630 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1631 throwSyntaxError("')' expected in function declaration.");
1633 methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1637 methodDecl.selector = "<undefined>".toCharArray();
1638 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1639 throwSyntaxError("Function name expected after keyword 'function'.");
1644 private void parameter_list(MethodDeclaration methodDecl) {
1645 // non_empty_parameter_list
1647 non_empty_parameter_list(methodDecl, true);
1650 private void non_empty_parameter_list(MethodDeclaration methodDecl, boolean empty_allowed) {
1651 // optional_class_type T_VARIABLE
1652 // | optional_class_type '&' T_VARIABLE
1653 // | optional_class_type '&' T_VARIABLE '=' static_scalar
1654 // | optional_class_type T_VARIABLE '=' static_scalar
1655 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1656 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1657 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1659 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1661 char[] typeIdentifier = null;
1662 if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1663 HashSet set = peekVariableSet();
1665 if (token == TokenNameIdentifier) {
1666 typeIdentifier = scanner.getCurrentIdentifierSource();
1669 if (token == TokenNameAND) {
1672 if (token == TokenNameVariable) {
1673 if (fMethodVariables != null) {
1675 if (methodDecl.type == MethodDeclaration.FUNCTION_DEFINITION) {
1676 info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_FUNCTION_DEFINITION);
1678 info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_METHOD_DEFINITION);
1680 info.typeIdentifier = typeIdentifier;
1681 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1683 addVariableSet(set);
1685 if (token == TokenNameEQUAL) {
1690 throwSyntaxError("Variable expected in parameter list.");
1692 if (token != TokenNameCOMMA) {
1699 if (!empty_allowed) {
1700 throwSyntaxError("Identifier expected in parameter list.");
1704 private void optional_class_type() {
1709 // private void parameterDeclaration() {
1711 // //variable-reference
1712 // if (token == TokenNameAND) {
1714 // if (isVariable()) {
1717 // throwSyntaxError("Variable expected after reference operator '&'.");
1720 // //variable '=' constant
1721 // if (token == TokenNameVariable) {
1723 // if (token == TokenNameEQUAL) {
1729 // // if (token == TokenNamethis) {
1730 // // throwSyntaxError("Reserved word '$this' not allowed in parameter
1731 // // declaration.");
1735 private void labeledStatementList() {
1736 if (token != TokenNamecase && token != TokenNamedefault) {
1737 throwSyntaxError("'case' or 'default' expected.");
1740 if (token == TokenNamecase) {
1742 expr(); // constant();
1743 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1745 if (token == TokenNameRBRACE) {
1746 // empty case; assumes that the '}' token belongs to the wrapping
1747 // switch statement - #1371992
1750 if (token == TokenNamecase || token == TokenNamedefault) {
1751 // empty case statement ?
1756 // else if (token == TokenNameSEMICOLON) {
1758 // "':' expected after 'case' keyword (Found token: " +
1759 // scanner.toStringAction(token) + ")",
1760 // scanner.getCurrentTokenStartPosition(),
1761 // scanner.getCurrentTokenEndPosition(),
1764 // if (token == TokenNamecase) { // empty case statement ?
1770 throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1772 } else { // TokenNamedefault
1774 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1776 if (token == TokenNameRBRACE) {
1777 // empty default case; ; assumes that the '}' token belongs to the
1778 // wrapping switch statement - #1371992
1781 if (token != TokenNamecase) {
1785 throwSyntaxError("':' character expected after 'default'.");
1788 } while (token == TokenNamecase || token == TokenNamedefault);
1791 private void ifStatementColon(IfStatement iState) {
1792 // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
1793 // new_else_single T_ENDIF ';'
1794 HashSet assignedVariableSet = null;
1796 Block b = inner_statement_list();
1797 iState.thenStatement = b;
1798 checkUnreachable(iState, b);
1800 assignedVariableSet = removeIfVariableSet();
1802 if (token == TokenNameelseif) {
1804 pushIfVariableSet();
1805 new_elseif_list(iState);
1807 HashSet set = removeIfVariableSet();
1808 if (assignedVariableSet != null && set != null) {
1809 assignedVariableSet.addAll(set);
1814 pushIfVariableSet();
1815 new_else_single(iState);
1817 HashSet set = removeIfVariableSet();
1818 if (assignedVariableSet != null) {
1819 HashSet topSet = peekVariableSet();
1820 if (topSet != null) {
1824 topSet.addAll(assignedVariableSet);
1828 if (token != TokenNameendif) {
1829 throwSyntaxError("'endif' expected.");
1832 if (token != TokenNameSEMICOLON) {
1833 reportSyntaxError("';' expected after if-statement.");
1834 iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1836 iState.sourceEnd = scanner.getCurrentTokenEndPosition();
1841 private void ifStatement(IfStatement iState) {
1842 // T_IF '(' expr ')' statement elseif_list else_single
1843 HashSet assignedVariableSet = null;
1845 pushIfVariableSet();
1846 Statement s = statement();
1847 iState.thenStatement = s;
1848 checkUnreachable(iState, s);
1850 assignedVariableSet = removeIfVariableSet();
1853 if (token == TokenNameelseif) {
1855 pushIfVariableSet();
1856 elseif_list(iState);
1858 HashSet set = removeIfVariableSet();
1859 if (assignedVariableSet != null && set != null) {
1860 assignedVariableSet.addAll(set);
1865 pushIfVariableSet();
1866 else_single(iState);
1868 HashSet set = removeIfVariableSet();
1869 if (assignedVariableSet != null) {
1870 HashSet topSet = peekVariableSet();
1871 if (topSet != null) {
1875 topSet.addAll(assignedVariableSet);
1881 private void elseif_list(IfStatement iState) {
1883 // | elseif_list T_ELSEIF '(' expr ')' statement
1884 ArrayList conditionList = new ArrayList();
1885 ArrayList statementList = new ArrayList();
1888 while (token == TokenNameelseif) {
1890 if (token == TokenNameLPAREN) {
1893 throwSyntaxError("'(' expected after 'elseif' keyword.");
1896 conditionList.add(e);
1897 if (token == TokenNameRPAREN) {
1900 throwSyntaxError("')' expected after 'elseif' condition.");
1903 statementList.add(s);
1904 checkUnreachable(iState, s);
1906 iState.elseifConditions = new Expression[conditionList.size()];
1907 iState.elseifStatements = new Statement[statementList.size()];
1908 conditionList.toArray(iState.elseifConditions);
1909 statementList.toArray(iState.elseifStatements);
1912 private void new_elseif_list(IfStatement iState) {
1914 // | new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
1915 ArrayList conditionList = new ArrayList();
1916 ArrayList statementList = new ArrayList();
1919 while (token == TokenNameelseif) {
1921 if (token == TokenNameLPAREN) {
1924 throwSyntaxError("'(' expected after 'elseif' keyword.");
1927 conditionList.add(e);
1928 if (token == TokenNameRPAREN) {
1931 throwSyntaxError("')' expected after 'elseif' condition.");
1933 if (token == TokenNameCOLON) {
1936 throwSyntaxError("':' expected after 'elseif' keyword.");
1938 b = inner_statement_list();
1939 statementList.add(b);
1940 checkUnreachable(iState, b);
1942 iState.elseifConditions = new Expression[conditionList.size()];
1943 iState.elseifStatements = new Statement[statementList.size()];
1944 conditionList.toArray(iState.elseifConditions);
1945 statementList.toArray(iState.elseifStatements);
1948 private void else_single(IfStatement iState) {
1951 if (token == TokenNameelse) {
1953 Statement s = statement();
1954 iState.elseStatement = s;
1955 checkUnreachable(iState, s);
1957 iState.checkUnreachable = false;
1959 iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1962 private void new_else_single(IfStatement iState) {
1964 // | T_ELSE ':' inner_statement_list
1965 if (token == TokenNameelse) {
1967 if (token == TokenNameCOLON) {
1970 throwSyntaxError("':' expected after 'else' keyword.");
1972 Block b = inner_statement_list();
1973 iState.elseStatement = b;
1974 checkUnreachable(iState, b);
1976 iState.checkUnreachable = false;
1980 private Block inner_statement_list() {
1981 // inner_statement_list inner_statement
1983 return statementList();
1990 private void checkUnreachable(IfStatement iState, Statement s) {
1991 if (s instanceof Block) {
1992 Block b = (Block) s;
1993 if (b.statements == null || b.statements.length == 0) {
1994 iState.checkUnreachable = false;
1996 int off = b.statements.length - 1;
1997 if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
1998 && !(b.statements[off] instanceof BreakStatement)) {
1999 if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
2000 iState.checkUnreachable = false;
2005 if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
2006 if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
2007 iState.checkUnreachable = false;
2013 // private void elseifStatementList() {
2015 // elseifStatement();
2017 // case TokenNameelse:
2019 // if (token == TokenNameCOLON) {
2021 // if (token != TokenNameendif) {
2026 // if (token == TokenNameif) { //'else if'
2029 // throwSyntaxError("':' expected after 'else'.");
2033 // case TokenNameelseif:
2042 // private void elseifStatement() {
2043 // if (token == TokenNameLPAREN) {
2046 // if (token != TokenNameRPAREN) {
2047 // throwSyntaxError("')' expected in else-if-statement.");
2050 // if (token != TokenNameCOLON) {
2051 // throwSyntaxError("':' expected in else-if-statement.");
2054 // if (token != TokenNameendif) {
2060 private void switchStatement() {
2061 if (token == TokenNameCOLON) {
2062 // ':' [labeled-statement-list] 'endswitch' ';'
2064 labeledStatementList();
2065 if (token != TokenNameendswitch) {
2066 throwSyntaxError("'endswitch' expected.");
2069 if (token != TokenNameSEMICOLON) {
2070 throwSyntaxError("';' expected after switch-statement.");
2074 // '{' [labeled-statement-list] '}'
2075 if (token != TokenNameLBRACE) {
2076 throwSyntaxError("'{' expected in switch statement.");
2079 if (token != TokenNameRBRACE) {
2080 labeledStatementList();
2082 if (token != TokenNameRBRACE) {
2083 throwSyntaxError("'}' expected in switch statement.");
2089 private void forStatement() {
2090 if (token == TokenNameCOLON) {
2093 if (token != TokenNameendfor) {
2094 throwSyntaxError("'endfor' expected.");
2097 if (token != TokenNameSEMICOLON) {
2098 throwSyntaxError("';' expected after for-statement.");
2106 private void whileStatement() {
2107 // ':' statement-list 'endwhile' ';'
2108 if (token == TokenNameCOLON) {
2111 if (token != TokenNameendwhile) {
2112 throwSyntaxError("'endwhile' expected.");
2115 if (token != TokenNameSEMICOLON) {
2116 throwSyntaxError("';' expected after while-statement.");
2124 private void foreachStatement() {
2125 if (token == TokenNameCOLON) {
2128 if (token != TokenNameendforeach) {
2129 throwSyntaxError("'endforeach' expected.");
2132 if (token != TokenNameSEMICOLON) {
2133 throwSyntaxError("';' expected after foreach-statement.");
2141 // private void exitStatus() {
2142 // if (token == TokenNameLPAREN) {
2145 // throwSyntaxError("'(' expected in 'exit-status'.");
2147 // if (token != TokenNameRPAREN) {
2150 // if (token == TokenNameRPAREN) {
2153 // throwSyntaxError("')' expected after 'exit-status'.");
2156 private void expressionList() {
2159 if (token == TokenNameCOMMA) {
2167 private Expression expr() {
2169 // | expr_without_variable
2170 // if (token!=TokenNameEOF) {
2171 if (Scanner.TRACE) {
2172 System.out.println("TRACE: expr()");
2174 return expr_without_variable(true);
2178 private Expression expr_without_variable(boolean only_variable) {
2179 int exprSourceStart = scanner.getCurrentTokenStartPosition();
2180 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2181 Expression expression = new Expression();
2182 expression.sourceStart = exprSourceStart;
2183 // default, may be overwritten
2184 expression.sourceEnd = exprSourceEnd;
2186 // internal_functions_in_yacc
2195 // | T_INC rw_variable
2196 // | T_DEC rw_variable
2197 // | T_INT_CAST expr
2198 // | T_DOUBLE_CAST expr
2199 // | T_STRING_CAST expr
2200 // | T_ARRAY_CAST expr
2201 // | T_OBJECT_CAST expr
2202 // | T_BOOL_CAST expr
2203 // | T_UNSET_CAST expr
2204 // | T_EXIT exit_expr
2206 // | T_ARRAY '(' array_pair_list ')'
2207 // | '`' encaps_list '`'
2208 // | T_LIST '(' assignment_list ')' '=' expr
2209 // | T_NEW class_name_reference ctor_arguments
2210 // | variable '=' expr
2211 // | variable '=' '&' variable
2212 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2213 // | variable T_PLUS_EQUAL expr
2214 // | variable T_MINUS_EQUAL expr
2215 // | variable T_MUL_EQUAL expr
2216 // | variable T_DIV_EQUAL expr
2217 // | variable T_CONCAT_EQUAL expr
2218 // | variable T_MOD_EQUAL expr
2219 // | variable T_AND_EQUAL expr
2220 // | variable T_OR_EQUAL expr
2221 // | variable T_XOR_EQUAL expr
2222 // | variable T_SL_EQUAL expr
2223 // | variable T_SR_EQUAL expr
2224 // | rw_variable T_INC
2225 // | rw_variable T_DEC
2226 // | expr T_BOOLEAN_OR expr
2227 // | expr T_BOOLEAN_AND expr
2228 // | expr T_LOGICAL_OR expr
2229 // | expr T_LOGICAL_AND expr
2230 // | expr T_LOGICAL_XOR expr
2242 // | expr T_IS_IDENTICAL expr
2243 // | expr T_IS_NOT_IDENTICAL expr
2244 // | expr T_IS_EQUAL expr
2245 // | expr T_IS_NOT_EQUAL expr
2247 // | expr T_IS_SMALLER_OR_EQUAL expr
2249 // | expr T_IS_GREATER_OR_EQUAL expr
2250 // | expr T_INSTANCEOF class_name_reference
2251 // | expr '?' expr ':' expr
2252 if (Scanner.TRACE) {
2253 System.out.println("TRACE: expr_without_variable() PART 1");
2256 case TokenNameisset:
2257 // T_ISSET '(' isset_variables ')'
2259 if (token != TokenNameLPAREN) {
2260 throwSyntaxError("'(' expected after keyword 'isset'");
2264 if (token != TokenNameRPAREN) {
2265 throwSyntaxError("')' expected after keyword 'isset'");
2269 case TokenNameempty:
2271 if (token != TokenNameLPAREN) {
2272 throwSyntaxError("'(' expected after keyword 'empty'");
2275 variable(true, false);
2276 if (token != TokenNameRPAREN) {
2277 throwSyntaxError("')' expected after keyword 'empty'");
2282 case TokenNameinclude:
2283 case TokenNameinclude_once:
2284 case TokenNamerequire:
2285 case TokenNamerequire_once:
2286 internal_functions_in_yacc();
2289 case TokenNameLPAREN:
2292 if (token == TokenNameRPAREN) {
2295 throwSyntaxError("')' expected in expression.");
2305 // | T_INT_CAST expr
2306 // | T_DOUBLE_CAST expr
2307 // | T_STRING_CAST expr
2308 // | T_ARRAY_CAST expr
2309 // | T_OBJECT_CAST expr
2310 // | T_BOOL_CAST expr
2311 // | T_UNSET_CAST expr
2312 case TokenNameclone:
2313 case TokenNameprint:
2316 case TokenNameMINUS:
2318 case TokenNameTWIDDLE:
2319 case TokenNameintCAST:
2320 case TokenNamedoubleCAST:
2321 case TokenNamestringCAST:
2322 case TokenNamearrayCAST:
2323 case TokenNameobjectCAST:
2324 case TokenNameboolCAST:
2325 case TokenNameunsetCAST:
2335 // | T_STRING_VARNAME
2337 // | T_START_HEREDOC encaps_list T_END_HEREDOC
2338 // | '`' encaps_list '`'
2340 // | '`' encaps_list '`'
2341 // case TokenNameEncapsedString0:
2342 // scanner.encapsedStringStack.push(new Character('`'));
2345 // if (token == TokenNameEncapsedString0) {
2348 // if (token != TokenNameEncapsedString0) {
2349 // throwSyntaxError("\'`\' expected at end of string" + "(Found token: " +
2350 // scanner.toStringAction(token) + " )");
2354 // scanner.encapsedStringStack.pop();
2358 // // | '\'' encaps_list '\''
2359 // case TokenNameEncapsedString1:
2360 // scanner.encapsedStringStack.push(new Character('\''));
2363 // exprSourceStart = scanner.getCurrentTokenStartPosition();
2364 // if (token == TokenNameEncapsedString1) {
2366 // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2367 // exprSourceStart, scanner
2368 // .getCurrentTokenEndPosition());
2371 // if (token != TokenNameEncapsedString1) {
2372 // throwSyntaxError("\'\'\' expected at end of string" + "(Found token: "
2373 // + scanner.toStringAction(token) + " )");
2376 // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2377 // exprSourceStart, scanner
2378 // .getCurrentTokenEndPosition());
2382 // scanner.encapsedStringStack.pop();
2386 // //| '"' encaps_list '"'
2387 // case TokenNameEncapsedString2:
2388 // scanner.encapsedStringStack.push(new Character('"'));
2391 // exprSourceStart = scanner.getCurrentTokenStartPosition();
2392 // if (token == TokenNameEncapsedString2) {
2394 // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2395 // exprSourceStart, scanner
2396 // .getCurrentTokenEndPosition());
2399 // if (token != TokenNameEncapsedString2) {
2400 // throwSyntaxError("'\"' expected at end of string" + "(Found token: " +
2401 // scanner.toStringAction(token) + " )");
2404 // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2405 // exprSourceStart, scanner
2406 // .getCurrentTokenEndPosition());
2410 // scanner.encapsedStringStack.pop();
2414 case TokenNameStringDoubleQuote:
2415 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2416 .getCurrentTokenEndPosition());
2419 case TokenNameStringSingleQuote:
2420 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2421 .getCurrentTokenEndPosition());
2424 case TokenNameIntegerLiteral:
2425 case TokenNameDoubleLiteral:
2426 case TokenNameStringInterpolated:
2429 case TokenNameCLASS_C:
2430 case TokenNameMETHOD_C:
2431 case TokenNameFUNC_C:
2434 case TokenNameHEREDOC:
2437 case TokenNamearray:
2438 // T_ARRAY '(' array_pair_list ')'
2440 if (token == TokenNameLPAREN) {
2442 if (token == TokenNameRPAREN) {
2447 if (token != TokenNameRPAREN) {
2448 throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2452 throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2456 // | T_LIST '(' assignment_list ')' '=' expr
2458 if (token == TokenNameLPAREN) {
2461 if (token != TokenNameRPAREN) {
2462 throwSyntaxError("')' expected after 'list' keyword.");
2465 if (token != TokenNameEQUAL) {
2466 throwSyntaxError("'=' expected after 'list' keyword.");
2471 throwSyntaxError("'(' expected after 'list' keyword.");
2475 // | T_NEW class_name_reference ctor_arguments
2477 Expression typeRef = class_name_reference();
2479 if (typeRef != null) {
2480 expression = typeRef;
2483 // | T_INC rw_variable
2484 // | T_DEC rw_variable
2485 case TokenNamePLUS_PLUS:
2486 case TokenNameMINUS_MINUS:
2490 // | variable '=' expr
2491 // | variable '=' '&' variable
2492 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2493 // | variable T_PLUS_EQUAL expr
2494 // | variable T_MINUS_EQUAL expr
2495 // | variable T_MUL_EQUAL expr
2496 // | variable T_DIV_EQUAL expr
2497 // | variable T_CONCAT_EQUAL expr
2498 // | variable T_MOD_EQUAL expr
2499 // | variable T_AND_EQUAL expr
2500 // | variable T_OR_EQUAL expr
2501 // | variable T_XOR_EQUAL expr
2502 // | variable T_SL_EQUAL expr
2503 // | variable T_SR_EQUAL expr
2504 // | rw_variable T_INC
2505 // | rw_variable T_DEC
2506 case TokenNameIdentifier:
2507 case TokenNameVariable:
2508 case TokenNameDOLLAR:
2509 Expression lhs = null;
2510 boolean rememberedVar = false;
2511 if (token == TokenNameIdentifier) {
2512 lhs = identifier(true, true);
2517 lhs = variable(true, true);
2521 if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
2522 && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
2523 && token != TokenNameDOT_EQUAL && token != TokenNameREMAINDER_EQUAL && token != TokenNameAND_EQUAL
2524 && token != TokenNameOR_EQUAL && token != TokenNameXOR_EQUAL && token != TokenNameRIGHT_SHIFT_EQUAL
2525 && token != TokenNameLEFT_SHIFT_EQUAL) {
2526 FieldReference ref = (FieldReference) lhs;
2527 if (!containsVariableSet(ref.token)) {
2528 problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(),
2529 referenceContext, compilationUnit.compilationResult);
2530 addVariableSet(ref.token);
2535 case TokenNameEQUAL:
2536 if (lhs != null && lhs instanceof FieldReference) {
2537 addVariableSet(((FieldReference) lhs).token);
2540 if (token == TokenNameAND) {
2542 if (token == TokenNamenew) {
2543 // | variable '=' '&' T_NEW class_name_reference
2546 SingleTypeReference classRef = class_name_reference();
2548 if (classRef != null) {
2549 if (lhs != null && lhs instanceof FieldReference) {
2551 // $var = & new Object();
2552 if (fMethodVariables != null) {
2553 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2554 lhsInfo.reference = classRef;
2555 lhsInfo.typeIdentifier = classRef.token;
2556 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2557 rememberedVar = true;
2562 Expression rhs = variable(false, false);
2563 if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2566 if (fMethodVariables != null) {
2567 VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2568 if (rhsInfo != null && rhsInfo.reference != null) {
2569 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2570 lhsInfo.reference = rhsInfo.reference;
2571 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2572 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2573 rememberedVar = true;
2579 Expression rhs = expr();
2580 if (lhs != null && lhs instanceof FieldReference) {
2581 if (rhs != null && rhs instanceof FieldReference) {
2584 if (fMethodVariables != null) {
2585 VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2586 if (rhsInfo != null && rhsInfo.reference != null) {
2587 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2588 lhsInfo.reference = rhsInfo.reference;
2589 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2590 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2591 rememberedVar = true;
2594 } else if (rhs != null && rhs instanceof SingleTypeReference) {
2596 // $var = new Object();
2597 if (fMethodVariables != null) {
2598 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2599 lhsInfo.reference = (SingleTypeReference) rhs;
2600 lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2601 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2602 rememberedVar = true;
2607 if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2608 if (fMethodVariables != null) {
2609 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2610 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2614 case TokenNamePLUS_EQUAL:
2615 case TokenNameMINUS_EQUAL:
2616 case TokenNameMULTIPLY_EQUAL:
2617 case TokenNameDIVIDE_EQUAL:
2618 case TokenNameDOT_EQUAL:
2619 case TokenNameREMAINDER_EQUAL:
2620 case TokenNameAND_EQUAL:
2621 case TokenNameOR_EQUAL:
2622 case TokenNameXOR_EQUAL:
2623 case TokenNameRIGHT_SHIFT_EQUAL:
2624 case TokenNameLEFT_SHIFT_EQUAL:
2625 if (lhs != null && lhs instanceof FieldReference) {
2626 addVariableSet(((FieldReference) lhs).token);
2631 case TokenNamePLUS_PLUS:
2632 case TokenNameMINUS_MINUS:
2636 if (!only_variable) {
2637 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2645 if (token != TokenNameINLINE_HTML) {
2646 if (token > TokenNameKEYWORD) {
2650 // System.out.println(scanner.getCurrentTokenStartPosition());
2651 // System.out.println(scanner.getCurrentTokenEndPosition());
2653 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2658 if (Scanner.TRACE) {
2659 System.out.println("TRACE: expr_without_variable() PART 2");
2661 // | expr T_BOOLEAN_OR expr
2662 // | expr T_BOOLEAN_AND expr
2663 // | expr T_LOGICAL_OR expr
2664 // | expr T_LOGICAL_AND expr
2665 // | expr T_LOGICAL_XOR expr
2677 // | expr T_IS_IDENTICAL expr
2678 // | expr T_IS_NOT_IDENTICAL expr
2679 // | expr T_IS_EQUAL expr
2680 // | expr T_IS_NOT_EQUAL expr
2682 // | expr T_IS_SMALLER_OR_EQUAL expr
2684 // | expr T_IS_GREATER_OR_EQUAL expr
2687 case TokenNameOR_OR:
2689 expression = new OR_OR_Expression(expression, expr(), token);
2691 case TokenNameAND_AND:
2693 expression = new AND_AND_Expression(expression, expr(), token);
2695 case TokenNameEQUAL_EQUAL:
2697 expression = new EqualExpression(expression, expr(), token);
2707 case TokenNameMINUS:
2708 case TokenNameMULTIPLY:
2709 case TokenNameDIVIDE:
2710 case TokenNameREMAINDER:
2711 case TokenNameLEFT_SHIFT:
2712 case TokenNameRIGHT_SHIFT:
2713 case TokenNameEQUAL_EQUAL_EQUAL:
2714 case TokenNameNOT_EQUAL_EQUAL:
2715 case TokenNameNOT_EQUAL:
2717 case TokenNameLESS_EQUAL:
2718 case TokenNameGREATER:
2719 case TokenNameGREATER_EQUAL:
2721 expression = new BinaryExpression(expression, expr(), token);
2723 // | expr T_INSTANCEOF class_name_reference
2724 // | expr '?' expr ':' expr
2725 case TokenNameinstanceof:
2727 TypeReference classRef = class_name_reference();
2728 if (classRef != null) {
2729 expression = new InstanceOfExpression(expression, classRef, OperatorIds.INSTANCEOF);
2730 expression.sourceStart = exprSourceStart;
2731 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2734 case TokenNameQUESTION:
2736 Expression valueIfTrue = expr();
2737 if (token != TokenNameCOLON) {
2738 throwSyntaxError("':' expected in conditional expression.");
2741 Expression valueIfFalse = expr();
2743 expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2749 } catch (SyntaxError e) {
2750 // try to find next token after expression with errors:
2751 if (token == TokenNameSEMICOLON) {
2755 if (token == TokenNameRBRACE || token == TokenNameRPAREN || token == TokenNameRBRACKET) {
2763 private SingleTypeReference class_name_reference() {
2764 // class_name_reference:
2766 // | dynamic_class_name_reference
2767 SingleTypeReference ref = null;
2768 if (Scanner.TRACE) {
2769 System.out.println("TRACE: class_name_reference()");
2771 if (token == TokenNameIdentifier) {
2772 ref = new SingleTypeReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2776 dynamic_class_name_reference();
2781 private void dynamic_class_name_reference() {
2782 // dynamic_class_name_reference:
2783 // base_variable T_OBJECT_OPERATOR object_property
2784 // dynamic_class_name_variable_properties
2786 if (Scanner.TRACE) {
2787 System.out.println("TRACE: dynamic_class_name_reference()");
2789 base_variable(true);
2790 if (token == TokenNameMINUS_GREATER) {
2793 dynamic_class_name_variable_properties();
2797 private void dynamic_class_name_variable_properties() {
2798 // dynamic_class_name_variable_properties:
2799 // dynamic_class_name_variable_properties
2800 // dynamic_class_name_variable_property
2802 if (Scanner.TRACE) {
2803 System.out.println("TRACE: dynamic_class_name_variable_properties()");
2805 while (token == TokenNameMINUS_GREATER) {
2806 dynamic_class_name_variable_property();
2810 private void dynamic_class_name_variable_property() {
2811 // dynamic_class_name_variable_property:
2812 // T_OBJECT_OPERATOR object_property
2813 if (Scanner.TRACE) {
2814 System.out.println("TRACE: dynamic_class_name_variable_property()");
2816 if (token == TokenNameMINUS_GREATER) {
2822 private void ctor_arguments() {
2825 // | '(' function_call_parameter_list ')'
2826 if (token == TokenNameLPAREN) {
2828 if (token == TokenNameRPAREN) {
2832 non_empty_function_call_parameter_list();
2833 if (token != TokenNameRPAREN) {
2834 throwSyntaxError("')' expected in ctor_arguments.");
2840 private void assignment_list() {
2842 // assignment_list ',' assignment_list_element
2843 // | assignment_list_element
2845 assignment_list_element();
2846 if (token != TokenNameCOMMA) {
2853 private void assignment_list_element() {
2854 // assignment_list_element:
2856 // | T_LIST '(' assignment_list ')'
2858 if (token == TokenNameVariable) {
2859 variable(true, false);
2860 } else if (token == TokenNameDOLLAR) {
2861 variable(false, false);
2863 if (token == TokenNamelist) {
2865 if (token == TokenNameLPAREN) {
2868 if (token != TokenNameRPAREN) {
2869 throwSyntaxError("')' expected after 'list' keyword.");
2873 throwSyntaxError("'(' expected after 'list' keyword.");
2879 private void array_pair_list() {
2882 // | non_empty_array_pair_list possible_comma
2883 non_empty_array_pair_list();
2884 if (token == TokenNameCOMMA) {
2889 private void non_empty_array_pair_list() {
2890 // non_empty_array_pair_list:
2891 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2892 // | non_empty_array_pair_list ',' expr
2893 // | expr T_DOUBLE_ARROW expr
2895 // | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2896 // | non_empty_array_pair_list ',' '&' w_variable
2897 // | expr T_DOUBLE_ARROW '&' w_variable
2900 if (token == TokenNameAND) {
2902 variable(true, false);
2905 if (token == TokenNameAND) {
2907 variable(true, false);
2908 } else if (token == TokenNameEQUAL_GREATER) {
2910 if (token == TokenNameAND) {
2912 variable(true, false);
2918 if (token != TokenNameCOMMA) {
2922 if (token == TokenNameRPAREN) {
2928 // private void variableList() {
2931 // if (token == TokenNameCOMMA) {
2938 private Expression variable_without_objects(boolean lefthandside, boolean ignoreVar) {
2939 // variable_without_objects:
2940 // reference_variable
2941 // | simple_indirect_reference reference_variable
2942 if (Scanner.TRACE) {
2943 System.out.println("TRACE: variable_without_objects()");
2945 while (token == TokenNameDOLLAR) {
2948 return reference_variable(lefthandside, ignoreVar);
2951 private Expression function_call(boolean lefthandside, boolean ignoreVar) {
2953 // T_STRING '(' function_call_parameter_list ')'
2954 // | class_constant '(' function_call_parameter_list ')'
2955 // | static_member '(' function_call_parameter_list ')'
2956 // | variable_without_objects '(' function_call_parameter_list ')'
2957 char[] defineName = null;
2958 char[] ident = null;
2961 Expression ref = null;
2962 if (Scanner.TRACE) {
2963 System.out.println("TRACE: function_call()");
2965 if (token == TokenNameIdentifier) {
2966 ident = scanner.getCurrentIdentifierSource();
2968 startPos = scanner.getCurrentTokenStartPosition();
2969 endPos = scanner.getCurrentTokenEndPosition();
2972 case TokenNamePAAMAYIM_NEKUDOTAYIM:
2976 if (token == TokenNameIdentifier) {
2981 variable_without_objects(true, false);
2986 ref = variable_without_objects(lefthandside, ignoreVar);
2988 if (token != TokenNameLPAREN) {
2989 if (defineName != null) {
2990 // does this identifier contain only uppercase characters?
2991 if (defineName.length == 3) {
2992 if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2995 } else if (defineName.length == 4) {
2996 if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2998 } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
3001 } else if (defineName.length == 5) {
3002 if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
3006 if (defineName != null) {
3007 for (int i = 0; i < defineName.length; i++) {
3008 if (Character.isLowerCase(defineName[i])) {
3009 problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
3017 if (token == TokenNameRPAREN) {
3021 non_empty_function_call_parameter_list();
3022 if (token != TokenNameRPAREN) {
3023 String functionName;
3024 if (ident == null) {
3025 functionName = new String(" ");
3027 functionName = new String(ident);
3029 throwSyntaxError("')' expected in function call (" + functionName + ").");
3036 // private void function_call_parameter_list() {
3037 // function_call_parameter_list:
3038 // non_empty_function_call_parameter_list { $$ = $1; }
3041 private void non_empty_function_call_parameter_list() {
3042 // non_empty_function_call_parameter_list:
3043 // expr_without_variable
3046 // | non_empty_function_call_parameter_list ',' expr_without_variable
3047 // | non_empty_function_call_parameter_list ',' variable
3048 // | non_empty_function_call_parameter_list ',' '&' w_variable
3049 if (Scanner.TRACE) {
3050 System.out.println("TRACE: non_empty_function_call_parameter_list()");
3053 if (token == TokenNameAND) {
3057 // if (token == TokenNameIdentifier || token ==
3058 // TokenNameVariable
3059 // || token == TokenNameDOLLAR) {
3062 expr_without_variable(true);
3065 if (token != TokenNameCOMMA) {
3072 private void fully_qualified_class_name() {
3073 if (token == TokenNameIdentifier) {
3076 throwSyntaxError("Class name expected.");
3080 private void static_member() {
3082 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
3083 // variable_without_objects
3084 if (Scanner.TRACE) {
3085 System.out.println("TRACE: static_member()");
3087 fully_qualified_class_name();
3088 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
3089 throwSyntaxError("'::' expected after class name (static_member).");
3092 variable_without_objects(false, false);
3095 private Expression base_variable_with_function_calls(boolean lefthandside, boolean ignoreVar) {
3096 // base_variable_with_function_calls:
3099 if (Scanner.TRACE) {
3100 System.out.println("TRACE: base_variable_with_function_calls()");
3102 return function_call(lefthandside, ignoreVar);
3105 private Expression base_variable(boolean lefthandside) {
3107 // reference_variable
3108 // | simple_indirect_reference reference_variable
3110 Expression ref = null;
3111 if (Scanner.TRACE) {
3112 System.out.println("TRACE: base_variable()");
3114 if (token == TokenNameIdentifier) {
3117 while (token == TokenNameDOLLAR) {
3120 reference_variable(lefthandside, false);
3125 // private void simple_indirect_reference() {
3126 // // simple_indirect_reference:
3128 // //| simple_indirect_reference '$'
3130 private Expression reference_variable(boolean lefthandside, boolean ignoreVar) {
3131 // reference_variable:
3132 // reference_variable '[' dim_offset ']'
3133 // | reference_variable '{' expr '}'
3134 // | compound_variable
3135 Expression ref = null;
3136 if (Scanner.TRACE) {
3137 System.out.println("TRACE: reference_variable()");
3139 ref = compound_variable(lefthandside, ignoreVar);
3141 if (token == TokenNameLBRACE) {
3145 if (token != TokenNameRBRACE) {
3146 throwSyntaxError("'}' expected in reference variable.");
3149 } else if (token == TokenNameLBRACKET) {
3150 // To remove "ref = null;" here, is probably better than the patch
3151 // commented in #1368081 - axelcl
3153 if (token != TokenNameRBRACKET) {
3156 if (token != TokenNameRBRACKET) {
3157 throwSyntaxError("']' expected in reference variable.");
3168 private Expression compound_variable(boolean lefthandside, boolean ignoreVar) {
3169 // compound_variable:
3171 // | '$' '{' expr '}'
3172 if (Scanner.TRACE) {
3173 System.out.println("TRACE: compound_variable()");
3175 if (token == TokenNameVariable) {
3176 if (!lefthandside) {
3177 if (!containsVariableSet()) {
3178 // reportSyntaxError("The local variable " + new
3179 // String(scanner.getCurrentIdentifierSource())
3180 // + " may not have been initialized");
3181 problemReporter.uninitializedLocalVariable(new String(scanner.getCurrentIdentifierSource()), scanner
3182 .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), referenceContext,
3183 compilationUnit.compilationResult);
3190 FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
3194 // because of simple_indirect_reference
3195 while (token == TokenNameDOLLAR) {
3198 if (token != TokenNameLBRACE) {
3199 reportSyntaxError("'{' expected after compound variable token '$'.");
3204 if (token != TokenNameRBRACE) {
3205 throwSyntaxError("'}' expected after compound variable token '$'.");
3210 } // private void dim_offset() { // // dim_offset: // // /* empty */
3215 private void object_property() {
3218 // | variable_without_objects
3219 if (Scanner.TRACE) {
3220 System.out.println("TRACE: object_property()");
3222 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
3223 variable_without_objects(false, false);
3229 private void object_dim_list() {
3231 // object_dim_list '[' dim_offset ']'
3232 // | object_dim_list '{' expr '}'
3234 if (Scanner.TRACE) {
3235 System.out.println("TRACE: object_dim_list()");
3239 if (token == TokenNameLBRACE) {
3242 if (token != TokenNameRBRACE) {
3243 throwSyntaxError("'}' expected in object_dim_list.");
3246 } else if (token == TokenNameLBRACKET) {
3248 if (token == TokenNameRBRACKET) {
3253 if (token != TokenNameRBRACKET) {
3254 throwSyntaxError("']' expected in object_dim_list.");
3263 private void variable_name() {
3267 if (Scanner.TRACE) {
3268 System.out.println("TRACE: variable_name()");
3270 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3271 if (token > TokenNameKEYWORD) {
3272 // TODO show a warning "Keyword used as variable" ?
3276 if (token != TokenNameLBRACE) {
3277 throwSyntaxError("'{' expected in variable name.");
3281 if (token != TokenNameRBRACE) {
3282 throwSyntaxError("'}' expected in variable name.");
3288 private void r_variable() {
3289 variable(false, false);
3292 private void w_variable(boolean lefthandside) {
3293 variable(lefthandside, false);
3296 private void rw_variable() {
3297 variable(false, false);
3300 private Expression variable(boolean lefthandside, boolean ignoreVar) {
3302 // base_variable_with_function_calls T_OBJECT_OPERATOR
3303 // object_property method_or_not variable_properties
3304 // | base_variable_with_function_calls
3305 Expression ref = base_variable_with_function_calls(lefthandside, ignoreVar);
3306 if (token == TokenNameMINUS_GREATER) {
3311 variable_properties();
3316 private void variable_properties() {
3317 // variable_properties:
3318 // variable_properties variable_property
3320 while (token == TokenNameMINUS_GREATER) {
3321 variable_property();
3325 private void variable_property() {
3326 // variable_property:
3327 // T_OBJECT_OPERATOR object_property method_or_not
3328 if (Scanner.TRACE) {
3329 System.out.println("TRACE: variable_property()");
3331 if (token == TokenNameMINUS_GREATER) {
3336 throwSyntaxError("'->' expected in variable_property.");
3340 private Expression identifier(boolean lefthandside, boolean ignoreVar) {
3342 // base_variable_with_function_calls T_OBJECT_OPERATOR
3343 // object_property method_or_not variable_properties
3344 // | base_variable_with_function_calls
3346 // Expression ref = function_call(lefthandside, ignoreVar);
3349 // T_STRING '(' function_call_parameter_list ')'
3350 // | class_constant '(' function_call_parameter_list ')'
3351 // | static_member '(' function_call_parameter_list ')'
3352 // | variable_without_objects '(' function_call_parameter_list ')'
3353 char[] defineName = null;
3354 char[] ident = null;
3357 Expression ref = null;
3358 if (Scanner.TRACE) {
3359 System.out.println("TRACE: function_call()");
3361 if (token == TokenNameIdentifier) {
3362 ident = scanner.getCurrentIdentifierSource();
3364 startPos = scanner.getCurrentTokenStartPosition();
3365 endPos = scanner.getCurrentTokenEndPosition();
3368 if (token == TokenNameEQUAL || token == TokenNamePLUS_EQUAL || token == TokenNameMINUS_EQUAL
3369 || token == TokenNameMULTIPLY_EQUAL || token == TokenNameDIVIDE_EQUAL || token == TokenNameDOT_EQUAL
3370 || token == TokenNameREMAINDER_EQUAL || token == TokenNameAND_EQUAL || token == TokenNameOR_EQUAL
3371 || token == TokenNameXOR_EQUAL || token == TokenNameRIGHT_SHIFT_EQUAL || token == TokenNameLEFT_SHIFT_EQUAL) {
3372 String error = "Assignment operator '" + scanner.toStringAction(token) + "' not allowed after identifier '"
3373 + new String(ident) + "' (use 'define(...)' to define constants).";
3374 reportSyntaxError(error);
3378 case TokenNamePAAMAYIM_NEKUDOTAYIM:
3382 if (token == TokenNameIdentifier) {
3387 variable_without_objects(true, false);
3392 ref = variable_without_objects(lefthandside, ignoreVar);
3394 if (token != TokenNameLPAREN) {
3395 if (defineName != null) {
3396 // does this identifier contain only uppercase characters?
3397 if (defineName.length == 3) {
3398 if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
3401 } else if (defineName.length == 4) {
3402 if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
3404 } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
3407 } else if (defineName.length == 5) {
3408 if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
3412 if (defineName != null) {
3413 for (int i = 0; i < defineName.length; i++) {
3414 if (Character.isLowerCase(defineName[i])) {
3415 problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
3421 // TODO is this ok ?
3423 // throwSyntaxError("'(' expected in function call.");
3427 if (token == TokenNameRPAREN) {
3431 non_empty_function_call_parameter_list();
3432 if (token != TokenNameRPAREN) {
3433 String functionName;
3434 if (ident == null) {
3435 functionName = new String(" ");
3437 functionName = new String(ident);
3439 throwSyntaxError("')' expected in function call (" + functionName + ").");
3444 if (token == TokenNameMINUS_GREATER) {
3449 variable_properties();
3454 private void method_or_not() {
3456 // '(' function_call_parameter_list ')'
3458 if (Scanner.TRACE) {
3459 System.out.println("TRACE: method_or_not()");
3461 if (token == TokenNameLPAREN) {
3463 if (token == TokenNameRPAREN) {
3467 non_empty_function_call_parameter_list();
3468 if (token != TokenNameRPAREN) {
3469 throwSyntaxError("')' expected in method_or_not.");
3475 private void exit_expr() {
3479 if (token != TokenNameLPAREN) {
3483 if (token == TokenNameRPAREN) {
3488 if (token != TokenNameRPAREN) {
3489 throwSyntaxError("')' expected after keyword 'exit'");
3494 // private void encaps_list() {
3495 // // encaps_list encaps_var
3496 // // | encaps_list T_STRING
3497 // // | encaps_list T_NUM_STRING
3498 // // | encaps_list T_ENCAPSED_AND_WHITESPACE
3499 // // | encaps_list T_CHARACTER
3500 // // | encaps_list T_BAD_CHARACTER
3501 // // | encaps_list '['
3502 // // | encaps_list ']'
3503 // // | encaps_list '{'
3504 // // | encaps_list '}'
3505 // // | encaps_list T_OBJECT_OPERATOR
3509 // case TokenNameSTRING:
3512 // case TokenNameLBRACE:
3513 // // scanner.encapsedStringStack.pop();
3516 // case TokenNameRBRACE:
3517 // // scanner.encapsedStringStack.pop();
3520 // case TokenNameLBRACKET:
3521 // // scanner.encapsedStringStack.pop();
3524 // case TokenNameRBRACKET:
3525 // // scanner.encapsedStringStack.pop();
3528 // case TokenNameMINUS_GREATER:
3529 // // scanner.encapsedStringStack.pop();
3532 // case TokenNameVariable:
3533 // case TokenNameDOLLAR_LBRACE:
3534 // case TokenNameLBRACE_DOLLAR:
3538 // char encapsedChar = ((Character)
3539 // scanner.encapsedStringStack.peek()).charValue();
3540 // if (encapsedChar == '$') {
3541 // scanner.encapsedStringStack.pop();
3542 // encapsedChar = ((Character)
3543 // scanner.encapsedStringStack.peek()).charValue();
3544 // switch (encapsedChar) {
3546 // if (token == TokenNameEncapsedString0) {
3549 // token = TokenNameSTRING;
3552 // if (token == TokenNameEncapsedString1) {
3555 // token = TokenNameSTRING;
3558 // if (token == TokenNameEncapsedString2) {
3561 // token = TokenNameSTRING;
3570 // private void encaps_var() {
3572 // // | T_VARIABLE '[' encaps_var_offset ']'
3573 // // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3574 // // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3575 // // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3576 // // | T_CURLY_OPEN variable '}'
3578 // case TokenNameVariable:
3580 // if (token == TokenNameLBRACKET) {
3582 // expr(); //encaps_var_offset();
3583 // if (token != TokenNameRBRACKET) {
3584 // throwSyntaxError("']' expected after variable.");
3586 // // scanner.encapsedStringStack.pop();
3589 // } else if (token == TokenNameMINUS_GREATER) {
3591 // if (token != TokenNameIdentifier) {
3592 // throwSyntaxError("Identifier expected after '->'.");
3594 // // scanner.encapsedStringStack.pop();
3598 // // // scanner.encapsedStringStack.pop();
3599 // // int tempToken = TokenNameSTRING;
3600 // // if (!scanner.encapsedStringStack.isEmpty()
3601 // // && (token == TokenNameEncapsedString0
3602 // // || token == TokenNameEncapsedString1
3603 // // || token == TokenNameEncapsedString2 || token ==
3604 // // TokenNameERROR)) {
3605 // // char encapsedChar = ((Character)
3606 // // scanner.encapsedStringStack.peek())
3608 // // switch (token) {
3609 // // case TokenNameEncapsedString0 :
3610 // // if (encapsedChar == '`') {
3611 // // tempToken = TokenNameEncapsedString0;
3614 // // case TokenNameEncapsedString1 :
3615 // // if (encapsedChar == '\'') {
3616 // // tempToken = TokenNameEncapsedString1;
3619 // // case TokenNameEncapsedString2 :
3620 // // if (encapsedChar == '"') {
3621 // // tempToken = TokenNameEncapsedString2;
3624 // // case TokenNameERROR :
3625 // // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3626 // // scanner.currentPosition--;
3627 // // getNextToken();
3632 // // token = tempToken;
3635 // case TokenNameDOLLAR_LBRACE:
3637 // if (token == TokenNameDOLLAR_LBRACE) {
3639 // } else if (token == TokenNameIdentifier) {
3641 // if (token == TokenNameLBRACKET) {
3643 // // if (token == TokenNameRBRACKET) {
3644 // // getNextToken();
3647 // if (token != TokenNameRBRACKET) {
3648 // throwSyntaxError("']' expected after '${'.");
3656 // if (token != TokenNameRBRACE) {
3657 // throwSyntaxError("'}' expected.");
3661 // case TokenNameLBRACE_DOLLAR:
3663 // if (token == TokenNameLBRACE_DOLLAR) {
3665 // } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3667 // if (token == TokenNameLBRACKET) {
3669 // // if (token == TokenNameRBRACKET) {
3670 // // getNextToken();
3673 // if (token != TokenNameRBRACKET) {
3674 // throwSyntaxError("']' expected.");
3678 // } else if (token == TokenNameMINUS_GREATER) {
3680 // if (token != TokenNameIdentifier && token != TokenNameVariable) {
3681 // throwSyntaxError("String or Variable token expected.");
3684 // if (token == TokenNameLBRACKET) {
3686 // // if (token == TokenNameRBRACKET) {
3687 // // getNextToken();
3690 // if (token != TokenNameRBRACKET) {
3691 // throwSyntaxError("']' expected after '${'.");
3697 // // if (token != TokenNameRBRACE) {
3698 // // throwSyntaxError("'}' expected after '{$'.");
3700 // // // scanner.encapsedStringStack.pop();
3701 // // getNextToken();
3704 // if (token != TokenNameRBRACE) {
3705 // throwSyntaxError("'}' expected.");
3707 // // scanner.encapsedStringStack.pop();
3714 // private void encaps_var_offset() {
3716 // // | T_NUM_STRING
3719 // case TokenNameSTRING:
3722 // case TokenNameIntegerLiteral:
3725 // case TokenNameVariable:
3728 // case TokenNameIdentifier:
3732 // throwSyntaxError("Variable or String token expected.");
3737 private void internal_functions_in_yacc() {
3740 // case TokenNameisset:
3741 // // T_ISSET '(' isset_variables ')'
3743 // if (token != TokenNameLPAREN) {
3744 // throwSyntaxError("'(' expected after keyword 'isset'");
3747 // isset_variables();
3748 // if (token != TokenNameRPAREN) {
3749 // throwSyntaxError("')' expected after keyword 'isset'");
3753 // case TokenNameempty:
3754 // // T_EMPTY '(' variable ')'
3756 // if (token != TokenNameLPAREN) {
3757 // throwSyntaxError("'(' expected after keyword 'empty'");
3761 // if (token != TokenNameRPAREN) {
3762 // throwSyntaxError("')' expected after keyword 'empty'");
3766 case TokenNameinclude:
3768 checkFileName(token);
3770 case TokenNameinclude_once:
3771 // T_INCLUDE_ONCE expr
3772 checkFileName(token);
3775 // T_EVAL '(' expr ')'
3777 if (token != TokenNameLPAREN) {
3778 throwSyntaxError("'(' expected after keyword 'eval'");
3782 if (token != TokenNameRPAREN) {
3783 throwSyntaxError("')' expected after keyword 'eval'");
3787 case TokenNamerequire:
3789 checkFileName(token);
3791 case TokenNamerequire_once:
3792 // T_REQUIRE_ONCE expr
3793 checkFileName(token);
3799 * Parse and check the include file name
3801 * @param includeToken
3803 private void checkFileName(int includeToken) {
3804 // <include-token> expr
3805 int start = scanner.getCurrentTokenStartPosition();
3806 boolean hasLPAREN = false;
3808 if (token == TokenNameLPAREN) {
3812 Expression expression = expr();
3814 if (token == TokenNameRPAREN) {
3817 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3820 char[] currTokenSource = scanner.getCurrentTokenSource(start);
3822 if (scanner.compilationUnit != null) {
3823 IResource resource = scanner.compilationUnit.getResource();
3824 if (resource != null && resource instanceof IFile) {
3825 file = (IFile) resource;
3829 tokens = new char[1][];
3830 tokens[0] = currTokenSource;
3832 ImportReference impt = new ImportReference(tokens, currTokenSource, start, scanner.getCurrentTokenEndPosition(), false);
3833 impt.declarationSourceEnd = impt.sourceEnd;
3834 impt.declarationEnd = impt.declarationSourceEnd;
3835 // endPosition is just before the ;
3836 impt.declarationSourceStart = start;
3837 includesList.add(impt);
3839 if (expression instanceof StringLiteral) {
3840 StringLiteral literal = (StringLiteral) expression;
3841 char[] includeName = literal.source();
3842 if (includeName.length == 0) {
3843 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3844 literal.sourceStart + 1);
3846 String includeNameString = new String(includeName);
3847 if (literal instanceof StringLiteralDQ) {
3848 if (includeNameString.indexOf('$') >= 0) {
3849 // assuming that the filename contains a variable => no filename check
3853 if (includeNameString.startsWith("http://")) {
3854 // assuming external include location
3858 // check the filename:
3859 // System.out.println(new String(compilationUnit.getFileName())+" - "+
3860 // expression.toStringExpression());
3861 IProject project = file.getProject();
3862 if (project != null) {
3863 IPath path = PHPFileUtil.determineFilePath(includeNameString, file, project);
3866 // SyntaxError: "File: << >> doesn't exist in project."
3867 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3868 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3869 compilationUnit.compilationResult);
3872 String filePath = path.toString();
3873 String ext = file.getRawLocation().getFileExtension();
3874 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
3876 impt.tokens = CharOperation.splitOn('/', filePath.toCharArray(), 0, filePath.length() - fileExtensionLength);
3877 impt.setFile(PHPFileUtil.createFile(path, project));
3878 } catch (Exception e) {
3879 // the file is outside of the workspace
3887 private void isset_variables() {
3889 // | isset_variables ','
3890 if (token == TokenNameRPAREN) {
3891 throwSyntaxError("Variable expected after keyword 'isset'");
3894 variable(true, false);
3895 if (token == TokenNameCOMMA) {
3903 private boolean common_scalar() {
3907 // | T_CONSTANT_ENCAPSED_STRING
3914 case TokenNameIntegerLiteral:
3917 case TokenNameDoubleLiteral:
3920 case TokenNameStringDoubleQuote:
3923 case TokenNameStringSingleQuote:
3926 case TokenNameStringInterpolated:
3935 case TokenNameCLASS_C:
3938 case TokenNameMETHOD_C:
3941 case TokenNameFUNC_C:
3948 private void scalar() {
3951 // | T_STRING_VARNAME
3954 // | '"' encaps_list '"'
3955 // | '\'' encaps_list '\''
3956 // | T_START_HEREDOC encaps_list T_END_HEREDOC
3957 throwSyntaxError("Not yet implemented (scalar).");
3960 private void static_scalar() {
3961 // static_scalar: /* compile-time evaluated scalars */
3964 // | '+' static_scalar
3965 // | '-' static_scalar
3966 // | T_ARRAY '(' static_array_pair_list ')'
3967 // | static_class_constant
3968 if (common_scalar()) {
3972 case TokenNameIdentifier:
3974 // static_class_constant:
3975 // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3976 if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3978 if (token == TokenNameIdentifier) {
3981 throwSyntaxError("Identifier expected after '::' operator.");
3985 case TokenNameEncapsedString0:
3987 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3988 while (scanner.currentCharacter != '`') {
3989 if (scanner.currentCharacter == '\\') {
3990 scanner.currentPosition++;
3992 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3995 } catch (IndexOutOfBoundsException e) {
3996 throwSyntaxError("'`' expected at end of static string.");
3999 // case TokenNameEncapsedString1:
4001 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4002 // while (scanner.currentCharacter != '\'') {
4003 // if (scanner.currentCharacter == '\\') {
4004 // scanner.currentPosition++;
4006 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4009 // } catch (IndexOutOfBoundsException e) {
4010 // throwSyntaxError("'\'' expected at end of static string.");
4013 // case TokenNameEncapsedString2:
4015 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4016 // while (scanner.currentCharacter != '"') {
4017 // if (scanner.currentCharacter == '\\') {
4018 // scanner.currentPosition++;
4020 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
4023 // } catch (IndexOutOfBoundsException e) {
4024 // throwSyntaxError("'\"' expected at end of static string.");
4027 case TokenNameStringSingleQuote:
4030 case TokenNameStringDoubleQuote:
4037 case TokenNameMINUS:
4041 case TokenNamearray:
4043 if (token != TokenNameLPAREN) {
4044 throwSyntaxError("'(' expected after keyword 'array'");
4047 if (token == TokenNameRPAREN) {
4051 non_empty_static_array_pair_list();
4052 if (token != TokenNameRPAREN) {
4053 throwSyntaxError("')' or ',' expected after keyword 'array'");
4057 // case TokenNamenull :
4060 // case TokenNamefalse :
4063 // case TokenNametrue :
4067 throwSyntaxError("Static scalar/constant expected.");
4071 private void non_empty_static_array_pair_list() {
4072 // non_empty_static_array_pair_list:
4073 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
4075 // | non_empty_static_array_pair_list ',' static_scalar
4076 // | static_scalar T_DOUBLE_ARROW static_scalar
4080 if (token == TokenNameEQUAL_GREATER) {
4084 if (token != TokenNameCOMMA) {
4088 if (token == TokenNameRPAREN) {
4094 // public void reportSyntaxError() { //int act, int currentKind, int
4095 // // stateStackTop) {
4096 // /* remember current scanner position */
4097 // int startPos = scanner.startPosition;
4098 // int currentPos = scanner.currentPosition;
4100 // this.checkAndReportBracketAnomalies(problemReporter());
4101 // /* reset scanner where it was */
4102 // scanner.startPosition = startPos;
4103 // scanner.currentPosition = currentPos;
4106 public static final int RoundBracket = 0;
4108 public static final int SquareBracket = 1;
4110 public static final int CurlyBracket = 2;
4112 public static final int BracketKinds = 3;
4114 protected int[] nestedMethod; // the ptr is nestedType
4116 protected int nestedType, dimensions;
4118 // variable set stack
4119 final static int VariableStackIncrement = 10;
4121 HashMap fTypeVariables = null;
4123 HashMap fMethodVariables = null;
4125 ArrayList fStackUnassigned = new ArrayList();
4128 final static int AstStackIncrement = 100;
4130 protected int astPtr;
4132 protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
4134 protected int astLengthPtr;
4136 protected int[] astLengthStack;
4138 ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
4140 public CompilationUnitDeclaration compilationUnit; /*
4141 * the result from parse()
4144 protected ReferenceContext referenceContext;
4146 protected ProblemReporter problemReporter;
4148 protected CompilerOptions options;
4150 private ArrayList includesList;
4152 // protected CompilationResult compilationResult;
4154 * Returns this parser's problem reporter initialized with its reference
4155 * context. Also it is assumed that a problem is going to be reported, so
4156 * initializes the compilation result's line positions.
4158 public ProblemReporter problemReporter() {
4159 if (scanner.recordLineSeparator) {
4160 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4162 problemReporter.referenceContext = referenceContext;
4163 return problemReporter;
4167 * Reconsider the entire source looking for inconsistencies in {} () []
4169 // public boolean checkAndReportBracketAnomalies(ProblemReporter
4170 // problemReporter) {
4171 // scanner.wasAcr = false;
4172 // boolean anomaliesDetected = false;
4174 // char[] source = scanner.source;
4175 // int[] leftCount = { 0, 0, 0 };
4176 // int[] rightCount = { 0, 0, 0 };
4177 // int[] depths = { 0, 0, 0 };
4178 // int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10]
4180 // int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
4181 // int[][] rightPositions = new int[][] { new int[10], new int[10], new
4183 // int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10]
4185 // scanner.currentPosition = scanner.initialPosition; //starting
4187 // // (first-zero-based
4189 // while (scanner.currentPosition < scanner.eofPosition) { //loop for
4194 // // ---------Consume white space and handles
4195 // // startPosition---------
4196 // boolean isWhiteSpace;
4198 // scanner.startPosition = scanner.currentPosition;
4199 // // if (((scanner.currentCharacter =
4200 // // source[scanner.currentPosition++]) == '\\') &&
4201 // // (source[scanner.currentPosition] == 'u')) {
4202 // // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
4204 // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4205 // (scanner.currentCharacter == '\n'))) {
4206 // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4207 // // only record line positions we have not
4209 // scanner.pushLineSeparator();
4212 // isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
4214 // } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
4215 // // -------consume token until } is found---------
4216 // switch (scanner.currentCharacter) {
4218 // int index = leftCount[CurlyBracket]++;
4219 // if (index == leftPositions[CurlyBracket].length) {
4220 // System.arraycopy(leftPositions[CurlyBracket], 0,
4221 // (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
4222 // System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] =
4223 // new int[index * 2]), 0, index);
4225 // leftPositions[CurlyBracket][index] = scanner.startPosition;
4226 // leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
4230 // int index = rightCount[CurlyBracket]++;
4231 // if (index == rightPositions[CurlyBracket].length) {
4232 // System.arraycopy(rightPositions[CurlyBracket], 0,
4233 // (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
4234 // System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] =
4235 // new int[index * 2]), 0, index);
4237 // rightPositions[CurlyBracket][index] = scanner.startPosition;
4238 // rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4242 // int index = leftCount[RoundBracket]++;
4243 // if (index == leftPositions[RoundBracket].length) {
4244 // System.arraycopy(leftPositions[RoundBracket], 0,
4245 // (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4246 // System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] =
4247 // new int[index * 2]), 0, index);
4249 // leftPositions[RoundBracket][index] = scanner.startPosition;
4250 // leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4254 // int index = rightCount[RoundBracket]++;
4255 // if (index == rightPositions[RoundBracket].length) {
4256 // System.arraycopy(rightPositions[RoundBracket], 0,
4257 // (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4258 // System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] =
4259 // new int[index * 2]), 0, index);
4261 // rightPositions[RoundBracket][index] = scanner.startPosition;
4262 // rightDepths[RoundBracket][index] = --depths[RoundBracket];
4266 // int index = leftCount[SquareBracket]++;
4267 // if (index == leftPositions[SquareBracket].length) {
4268 // System.arraycopy(leftPositions[SquareBracket], 0,
4269 // (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4270 // System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] =
4271 // new int[index * 2]), 0, index);
4273 // leftPositions[SquareBracket][index] = scanner.startPosition;
4274 // leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4278 // int index = rightCount[SquareBracket]++;
4279 // if (index == rightPositions[SquareBracket].length) {
4280 // System.arraycopy(rightPositions[SquareBracket], 0,
4281 // (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4282 // System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket]
4283 // = new int[index * 2]), 0, index);
4285 // rightPositions[SquareBracket][index] = scanner.startPosition;
4286 // rightDepths[SquareBracket][index] = --depths[SquareBracket];
4290 // if (scanner.getNextChar('\\')) {
4291 // scanner.scanEscapeCharacter();
4292 // } else { // consume next character
4293 // scanner.unicodeAsBackSlash = false;
4294 // // if (((scanner.currentCharacter =
4295 // // source[scanner.currentPosition++]) ==
4297 // // (source[scanner.currentPosition] ==
4299 // // scanner.getNextUnicodeChar();
4301 // if (scanner.withoutUnicodePtr != 0) {
4302 // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4303 // scanner.currentCharacter;
4307 // scanner.getNextChar('\'');
4311 // // consume next character
4312 // scanner.unicodeAsBackSlash = false;
4313 // // if (((scanner.currentCharacter =
4314 // // source[scanner.currentPosition++]) == '\\') &&
4315 // // (source[scanner.currentPosition] == 'u')) {
4316 // // scanner.getNextUnicodeChar();
4318 // if (scanner.withoutUnicodePtr != 0) {
4319 // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4320 // scanner.currentCharacter;
4323 // while (scanner.currentCharacter != '"') {
4324 // if (scanner.currentCharacter == '\r') {
4325 // if (source[scanner.currentPosition] == '\n')
4326 // scanner.currentPosition++;
4327 // break; // the string cannot go further that
4330 // if (scanner.currentCharacter == '\n') {
4331 // break; // the string cannot go further that
4334 // if (scanner.currentCharacter == '\\') {
4335 // scanner.scanEscapeCharacter();
4337 // // consume next character
4338 // scanner.unicodeAsBackSlash = false;
4339 // // if (((scanner.currentCharacter =
4340 // // source[scanner.currentPosition++]) == '\\')
4341 // // && (source[scanner.currentPosition] == 'u'))
4343 // // scanner.getNextUnicodeChar();
4345 // if (scanner.withoutUnicodePtr != 0) {
4346 // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4347 // scanner.currentCharacter;
4354 // if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4356 // //get the next char
4357 // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4359 // && (source[scanner.currentPosition] == 'u')) {
4360 // //-------------unicode traitement
4362 // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4363 // scanner.currentPosition++;
4364 // while (source[scanner.currentPosition] == 'u') {
4365 // scanner.currentPosition++;
4367 // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4369 // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4371 // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4373 // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4374 // || c4 < 0) { //error
4378 // scanner.currentCharacter = 'A';
4379 // } //something different from \n and \r
4381 // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4384 // while (scanner.currentCharacter != '\r' && scanner.currentCharacter !=
4386 // //get the next char
4387 // scanner.startPosition = scanner.currentPosition;
4388 // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4390 // && (source[scanner.currentPosition] == 'u')) {
4391 // //-------------unicode traitement
4393 // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4394 // scanner.currentPosition++;
4395 // while (source[scanner.currentPosition] == 'u') {
4396 // scanner.currentPosition++;
4398 // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4400 // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4402 // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4404 // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4405 // || c4 < 0) { //error
4409 // scanner.currentCharacter = 'A';
4410 // } //something different from \n
4413 // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4417 // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4418 // (scanner.currentCharacter == '\n'))) {
4419 // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4420 // // only record line positions we
4421 // // have not recorded yet
4422 // scanner.pushLineSeparator();
4423 // if (this.scanner.taskTags != null) {
4424 // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4426 // .getCurrentTokenEndPosition());
4432 // if (test > 0) { //traditional and annotation
4434 // boolean star = false;
4435 // // consume next character
4436 // scanner.unicodeAsBackSlash = false;
4437 // // if (((scanner.currentCharacter =
4438 // // source[scanner.currentPosition++]) ==
4440 // // (source[scanner.currentPosition] ==
4442 // // scanner.getNextUnicodeChar();
4444 // if (scanner.withoutUnicodePtr != 0) {
4445 // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4446 // scanner.currentCharacter;
4449 // if (scanner.currentCharacter == '*') {
4452 // //get the next char
4453 // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4455 // && (source[scanner.currentPosition] == 'u')) {
4456 // //-------------unicode traitement
4458 // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4459 // scanner.currentPosition++;
4460 // while (source[scanner.currentPosition] == 'u') {
4461 // scanner.currentPosition++;
4463 // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4465 // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4467 // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4469 // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4470 // || c4 < 0) { //error
4474 // scanner.currentCharacter = 'A';
4475 // } //something different from * and /
4477 // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4480 // //loop until end of comment */
4481 // while ((scanner.currentCharacter != '/') || (!star)) {
4482 // star = scanner.currentCharacter == '*';
4484 // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4486 // && (source[scanner.currentPosition] == 'u')) {
4487 // //-------------unicode traitement
4489 // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4490 // scanner.currentPosition++;
4491 // while (source[scanner.currentPosition] == 'u') {
4492 // scanner.currentPosition++;
4494 // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4496 // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4498 // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4500 // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4501 // || c4 < 0) { //error
4505 // scanner.currentCharacter = 'A';
4506 // } //something different from * and
4509 // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4513 // if (this.scanner.taskTags != null) {
4514 // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4515 // this.scanner.getCurrentTokenEndPosition());
4522 // if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4523 // scanner.scanIdentifierOrKeyword(false);
4526 // if (Character.isDigit(scanner.currentCharacter)) {
4527 // scanner.scanNumber(false);
4531 // //-----------------end switch while
4532 // // try--------------------
4533 // } catch (IndexOutOfBoundsException e) {
4534 // break; // read until EOF
4535 // } catch (InvalidInputException e) {
4536 // return false; // no clue
4539 // if (scanner.recordLineSeparator) {
4540 // compilationUnit.compilationResult.lineSeparatorPositions =
4541 // scanner.getLineEnds();
4543 // // check placement anomalies against other kinds of brackets
4544 // for (int kind = 0; kind < BracketKinds; kind++) {
4545 // for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4546 // int start = leftPositions[kind][leftIndex]; // deepest
4548 // // find matching closing bracket
4549 // int depth = leftDepths[kind][leftIndex];
4551 // for (int i = 0; i < rightCount[kind]; i++) {
4552 // int pos = rightPositions[kind][i];
4553 // // want matching bracket further in source with same
4555 // if ((pos > start) && (depth == rightDepths[kind][i])) {
4560 // if (end < 0) { // did not find a good closing match
4561 // problemReporter.unmatchedBracket(start, referenceContext,
4562 // compilationUnit.compilationResult);
4565 // // check if even number of opening/closing other brackets
4566 // // in between this pair of brackets
4568 // for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds);
4570 // for (int i = 0; i < leftCount[otherKind]; i++) {
4571 // int pos = leftPositions[otherKind][i];
4572 // if ((pos > start) && (pos < end))
4575 // for (int i = 0; i < rightCount[otherKind]; i++) {
4576 // int pos = rightPositions[otherKind][i];
4577 // if ((pos > start) && (pos < end))
4580 // if (balance != 0) {
4581 // problemReporter.unmatchedBracket(start, referenceContext,
4582 // compilationUnit.compilationResult); //bracket
4588 // // too many opening brackets ?
4589 // for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4590 // anomaliesDetected = true;
4591 // problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i -
4592 // 1], referenceContext,
4593 // compilationUnit.compilationResult);
4595 // // too many closing brackets ?
4596 // for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4597 // anomaliesDetected = true;
4598 // problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext,
4599 // compilationUnit.compilationResult);
4601 // if (anomaliesDetected)
4604 // return anomaliesDetected;
4605 // } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4606 // return anomaliesDetected;
4607 // } catch (NullPointerException e) { // jdk1.2.2 jit bug
4608 // return anomaliesDetected;
4611 protected void pushOnAstLengthStack(int pos) {
4613 astLengthStack[++astLengthPtr] = pos;
4614 } catch (IndexOutOfBoundsException e) {
4615 int oldStackLength = astLengthStack.length;
4616 int[] oldPos = astLengthStack;
4617 astLengthStack = new int[oldStackLength + StackIncrement];
4618 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4619 astLengthStack[astLengthPtr] = pos;
4623 protected void pushOnAstStack(ASTNode node) {
4625 * add a new obj on top of the ast stack
4628 astStack[++astPtr] = node;
4629 } catch (IndexOutOfBoundsException e) {
4630 int oldStackLength = astStack.length;
4631 ASTNode[] oldStack = astStack;
4632 astStack = new ASTNode[oldStackLength + AstStackIncrement];
4633 System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4634 astPtr = oldStackLength;
4635 astStack[astPtr] = node;
4638 astLengthStack[++astLengthPtr] = 1;
4639 } catch (IndexOutOfBoundsException e) {
4640 int oldStackLength = astLengthStack.length;
4641 int[] oldPos = astLengthStack;
4642 astLengthStack = new int[oldStackLength + AstStackIncrement];
4643 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4644 astLengthStack[astLengthPtr] = 1;
4648 protected void resetModifiers() {
4649 this.modifiers = AccDefault;
4650 this.modifiersSourceStart = -1; // <-- see comment into
4651 // modifiersFlag(int)
4652 this.scanner.commentPtr = -1;
4655 protected void consumePackageDeclarationName(IFile file) {
4656 // create a package name similar to java package names
4657 String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4658 String filePath = file.getRawLocation().toString();
4659 String ext = file.getRawLocation().getFileExtension();
4660 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4661 ImportReference impt;
4663 if (filePath.startsWith(projectPath)) {
4664 tokens = CharOperation
4665 .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4667 String name = file.getName();
4668 tokens = new char[1][];
4669 tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4672 this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4674 impt.declarationSourceStart = 0;
4675 impt.declarationSourceEnd = 0;
4676 impt.declarationEnd = 0;
4677 // endPosition is just before the ;
4681 public final static String[] GLOBALS = { "$this", "$_COOKIE", "$_ENV", "$_FILES", "$_GET", "$GLOBALS", "$_POST", "$_REQUEST",
4682 "$_SESSION", "$_SERVER" };
4687 private void pushFunctionVariableSet() {
4688 HashSet set = new HashSet();
4689 if (fStackUnassigned.isEmpty()) {
4690 for (int i = 0; i < GLOBALS.length; i++) {
4691 set.add(GLOBALS[i]);
4694 fStackUnassigned.add(set);
4697 private void pushIfVariableSet() {
4698 if (!fStackUnassigned.isEmpty()) {
4699 HashSet set = new HashSet();
4700 fStackUnassigned.add(set);
4704 private HashSet removeIfVariableSet() {
4705 if (!fStackUnassigned.isEmpty()) {
4706 return (HashSet) fStackUnassigned.remove(fStackUnassigned.size() - 1);
4712 * Returns the <i>set of assigned variables </i> returns null if no Set is
4713 * defined at the current scanner position
4715 private HashSet peekVariableSet() {
4716 if (!fStackUnassigned.isEmpty()) {
4717 return (HashSet) fStackUnassigned.get(fStackUnassigned.size() - 1);
4723 * add the current identifier source to the <i>set of assigned variables </i>
4727 private void addVariableSet(HashSet set) {
4729 set.add(new String(scanner.getCurrentTokenSource()));
4734 * add the current identifier source to the <i>set of assigned variables </i>
4737 private void addVariableSet() {
4738 HashSet set = peekVariableSet();
4740 set.add(new String(scanner.getCurrentTokenSource()));
4745 * add the current identifier source to the <i>set of assigned variables </i>
4748 private void addVariableSet(char[] token) {
4749 HashSet set = peekVariableSet();
4751 set.add(new String(token));
4756 * check if the current identifier source is in the <i>set of assigned
4757 * variables </i> Returns true, if no set is defined for the current scanner
4761 private boolean containsVariableSet() {
4762 return containsVariableSet(scanner.getCurrentTokenSource());
4765 private boolean containsVariableSet(char[] token) {
4767 if (!fStackUnassigned.isEmpty()) {
4769 String str = new String(token);
4770 for (int i = 0; i < fStackUnassigned.size(); i++) {
4771 set = (HashSet) fStackUnassigned.get(i);
4772 if (set.contains(str)) {