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;
12 import net.sourceforge.phpdt.core.compiler.CharOperation;
13 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
14 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
15 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
16 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
17 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
18 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
19 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
20 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
21 import net.sourceforge.phpdt.internal.compiler.util.Util;
22 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
23 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
24 import net.sourceforge.phpeclipse.internal.compiler.ast.AND_AND_Expression;
25 import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
26 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
27 import net.sourceforge.phpeclipse.internal.compiler.ast.BinaryExpression;
28 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
29 import net.sourceforge.phpeclipse.internal.compiler.ast.ConditionalExpression;
30 import net.sourceforge.phpeclipse.internal.compiler.ast.EqualExpression;
31 import net.sourceforge.phpeclipse.internal.compiler.ast.Expression;
32 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
33 import net.sourceforge.phpeclipse.internal.compiler.ast.IfStatement;
34 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
35 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
36 import net.sourceforge.phpeclipse.internal.compiler.ast.OR_OR_Expression;
37 import net.sourceforge.phpeclipse.internal.compiler.ast.SingleTypeReference;
38 import net.sourceforge.phpeclipse.internal.compiler.ast.Statement;
39 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteral;
40 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralDQ;
41 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralSQ;
42 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
44 import org.eclipse.core.resources.IFile;
45 import org.eclipse.core.resources.IProject;
46 import org.eclipse.core.resources.IResource;
47 import org.eclipse.core.runtime.IPath;
49 public class Parser //extends PHPParserSuperclass
50 implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation {
51 //internal data for the automat
52 protected final static int StackIncrement = 255;
54 protected int stateStackTop;
56 protected int[] stack = new int[StackIncrement];
58 public int firstToken; // handle for multiple parsing goals
60 public int lastAct; //handle for multiple parsing goals
62 protected RecoveredElement currentElement;
64 public static boolean VERBOSE_RECOVERY = false;
66 protected boolean diet = false; //tells the scanner to jump over some
68 // parts of the code/expressions like
71 public Scanner scanner;
73 private ArrayList phpList;
75 private int currentPHPString;
77 private boolean phpEnd;
79 // private static HashMap keywordMap = null;
87 // row counter for syntax errors:
89 // column counter for syntax errors:
93 // // current identifier
99 private String stringValue;
101 /** Contains the current expression. */
102 // private StringBuffer expression;
103 //private boolean phpMode;
104 protected int modifiers;
106 protected int modifiersSourceStart;
108 // protected IdentifierIndexManager indexManager;
110 protected Parser(ProblemReporter problemReporter) {
111 this.problemReporter = problemReporter;
112 this.options = problemReporter.options;
113 this.currentPHPString = 0;
114 // PHPParserSuperclass.fileToParse = fileToParse;
116 // this.indexManager = null;
118 this.token = TokenNameEOF;
120 // this.rowCount = 1;
121 // this.columnCount = 0;
124 this.initializeScanner();
127 public void setFileToParse(IFile fileToParse) {
128 this.currentPHPString = 0;
129 // PHPParserSuperclass.fileToParse = fileToParse;
131 // this.indexManager = null;
133 this.token = TokenNameEOF;
135 this.initializeScanner();
139 * ClassDeclaration Constructor.
143 * Description of Parameter
146 public Parser(IFile fileToParse) {
147 // if (keywordMap == null) {
148 // keywordMap = new HashMap();
149 // for (int i = 0; i < PHP_KEYWORS.length; i++) {
150 // keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
153 this.currentPHPString = 0;
154 // PHPParserSuperclass.fileToParse = fileToParse;
156 this.includesList = null;
158 this.token = TokenNameEOF;
160 // this.rowCount = 1;
161 // this.columnCount = 0;
164 this.initializeScanner();
167 public void initializeScanner() {
168 this.scanner = new Scanner(false /* comment */, false /* whitespace */, this.options
169 .getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore /* nls */, false, false,
170 this.options.taskTags/* taskTags */, this.options.taskPriorites/* taskPriorities */);
174 * Create marker for the parse error
176 // private void setMarker(String message, int charStart, int charEnd, int
178 // setMarker(fileToParse, message, charStart, charEnd, errorLevel);
181 * This method will throw the SyntaxError. It will add the good lines and columns to the Error
185 * @throws SyntaxError
188 private void throwSyntaxError(String error) {
189 int problemStartPosition = scanner.getCurrentTokenStartPosition();
190 int problemEndPosition = scanner.getCurrentTokenEndPosition();
191 throwSyntaxError(error, problemStartPosition, problemEndPosition + 1);
195 * This method will throw the SyntaxError. It will add the good lines and columns to the Error
199 * @throws SyntaxError
202 // private void throwSyntaxError(String error, int startRow) {
203 // throw new SyntaxError(startRow, 0, " ", error);
205 private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
206 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
207 compilationUnit.compilationResult);
208 throw new SyntaxError(1, 0, " ", error);
211 private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
212 problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
213 compilationUnit.compilationResult);
216 private void reportSyntaxWarning(String error, int problemStartPosition, int problemEndPosition) {
217 problemReporter.phpParsingWarning(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
218 compilationUnit.compilationResult);
222 * gets the next token from input
224 private void getNextToken() {
226 token = scanner.getNextToken();
228 int currentEndPosition = scanner.getCurrentTokenEndPosition();
229 int currentStartPosition = scanner.getCurrentTokenStartPosition();
230 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
231 System.out.println(scanner.toStringAction(token));
233 } catch (InvalidInputException e) {
234 token = TokenNameERROR;
235 String detailedMessage = e.getMessage();
237 if (detailedMessage == Scanner.UNTERMINATED_STRING) {
238 throwSyntaxError("Unterminated string.");
239 } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
240 throwSyntaxError("Unterminated commment.");
246 public void init(String s) {
248 this.token = TokenNameEOF;
250 // this.rowCount = 1;
251 // this.columnCount = 0;
253 // this.phpMode = false;
254 /* scanner initialization */
255 scanner.setSource(s.toCharArray());
256 scanner.setPHPMode(false);
259 protected void initialize(boolean phpMode) {
260 initialize(phpMode, null);
263 protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
264 compilationUnit = null;
265 referenceContext = null;
266 includesList = new ArrayList();
267 // this.indexManager = indexManager;
269 this.token = TokenNameEOF;
271 // this.rowCount = 1;
272 // this.columnCount = 0;
274 // this.phpMode = phpMode;
275 scanner.setPHPMode(phpMode);
279 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
281 public void parse(String s) {
287 * Parses a string with php tags i.e. '<body> <?php phpinfo() ?> </body>'
289 protected void parse() {
293 if (token != TokenNameEOF && token != TokenNameERROR) {
296 if (token != TokenNameEOF) {
297 if (token == TokenNameERROR) {
298 throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
300 if (token == TokenNameRPAREN) {
301 throwSyntaxError("Too many closing ')'; end-of-file not reached.");
303 if (token == TokenNameRBRACE) {
304 throwSyntaxError("Too many closing '}'; end-of-file not reached.");
306 if (token == TokenNameRBRACKET) {
307 throwSyntaxError("Too many closing ']'; end-of-file not reached.");
309 if (token == TokenNameLPAREN) {
310 throwSyntaxError("Read character '('; end-of-file not reached.");
312 if (token == TokenNameLBRACE) {
313 throwSyntaxError("Read character '{'; end-of-file not reached.");
315 if (token == TokenNameLBRACKET) {
316 throwSyntaxError("Read character '['; end-of-file not reached.");
318 throwSyntaxError("End-of-file not reached.");
321 } catch (SyntaxError sytaxErr1) {
322 // setMarker(sytaxErr1.getMessage(), sytaxErr1.getLine(),
324 // setMarker(sytaxErr1.getMessage(),
325 // scanner.getCurrentTokenStartPosition(),
326 // scanner.getCurrentTokenEndPosition(), ERROR);
328 // if an error occured,
329 // try to find keywords 'class' or 'function'
330 // to parse the rest of the string
331 while (token != TokenNameEOF && token != TokenNameERROR) {
332 if (token == TokenNameabstract || token == TokenNamefinal || token == TokenNameclass || token == TokenNamefunction) {
337 if (token == TokenNameEOF || token == TokenNameERROR) {
340 } catch (SyntaxError sytaxErr2) {
341 // setMarker(sytaxErr2.getMessage(), sytaxErr2.getLine(),
343 // setMarker(sytaxErr2.getMessage(),
344 // scanner.getCurrentTokenStartPosition(),
345 // scanner.getCurrentTokenEndPosition(), ERROR);
354 protected CompilationUnitDeclaration endParse(int act) {
358 if (currentElement != null) {
359 currentElement.topElement().updateParseTree();
360 if (VERBOSE_RECOVERY) {
361 System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
362 System.out.println("--------------------------"); //$NON-NLS-1$
363 System.out.println(compilationUnit);
364 System.out.println("----------------------------------"); //$NON-NLS-1$
367 if (diet & VERBOSE_RECOVERY) {
368 System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
369 System.out.println("--------------------------"); //$NON-NLS-1$
370 System.out.println(compilationUnit);
371 System.out.println("----------------------------------"); //$NON-NLS-1$
374 if (scanner.recordLineSeparator) {
375 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
377 if (scanner.taskTags != null) {
378 for (int i = 0; i < scanner.foundTaskCount; i++) {
379 problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
380 scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
381 scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
384 compilationUnit.imports = new ImportReference[includesList.size()];
385 for (int i = 0; i < includesList.size(); i++) {
386 compilationUnit.imports[i] = (ImportReference) includesList.get(i);
388 return compilationUnit;
391 // public PHPOutlineInfo parseInfo(Object parent, String s) {
392 // PHPOutlineInfo outlineInfo = new PHPOutlineInfo(parent);
393 // // Stack stack = new Stack();
394 // // stack.push(outlineInfo.getDeclarations());
396 // this.token = TokenNameEOF;
397 // // this.chIndx = 0;
398 // // this.rowCount = 1;
399 // // this.columnCount = 0;
400 // this.phpEnd = false;
401 // this.phpMode = false;
402 // scanner.setSource(s.toCharArray());
403 // scanner.setPHPMode(false);
406 // parseDeclarations(outlineInfo, outlineInfo.getDeclarations(), false);
408 // return outlineInfo;
410 private boolean isVariable() {
411 return token == TokenNameVariable; // || token == TokenNamethis;
414 // private void parseDeclarations(PHPOutlineInfo outlineInfo,
415 // OutlineableWithChildren current, boolean goBack) {
417 // // PHPClassDeclaration current = (PHPClassDeclaration) stack.peek();
418 // PHPSegmentWithChildren temp;
420 // IPreferenceStore store =
421 // PHPeclipsePlugin.getDefault().getPreferenceStore();
423 // while (token != TokenNameEOF && token != TokenNameERROR) {
424 // if (token == TokenNameVariable) {
425 // ident = scanner.getCurrentIdentifierSource();
426 // outlineInfo.addVariable(new String(ident));
428 // } else if (token == TokenNamevar) {
430 // if (token == TokenNameVariable
431 // && store.getBoolean(PHPeclipsePlugin.PHP_OUTLINE_VAR)) {
432 // ident = scanner.getCurrentIdentifierSource();
433 // //substring(1) added because PHPVarDeclaration doesn't
434 // // need the $ anymore
435 // String variableName = new String(ident).substring(1);
436 // outlineInfo.addVariable(variableName);
438 // if (token != TokenNameSEMICOLON) {
440 // ident = scanner.getCurrentTokenSource();
441 // if (token > TokenNameKEYWORD) {
442 // current.add(new PHPVarDeclaration(current, variableName,
443 // // chIndx - ident.length,
444 // scanner.getCurrentTokenStartPosition(), new String(ident)));
447 // case TokenNameVariable :
448 // case TokenNamethis :
449 // current.add(new PHPVarDeclaration(current, variableName,
452 // scanner.getCurrentTokenStartPosition(), new String(
455 // case TokenNameIdentifier :
456 // current.add(new PHPVarDeclaration(current, variableName,
459 // scanner.getCurrentTokenStartPosition(), new String(
462 // case TokenNameDoubleLiteral :
463 // current.add(new PHPVarDeclaration(current, variableName
467 // scanner.getCurrentTokenStartPosition(), new String(
470 // case TokenNameIntegerLiteral :
471 // current.add(new PHPVarDeclaration(current, variableName,
474 // scanner.getCurrentTokenStartPosition(), new String(
477 // case TokenNameStringInterpolated :
478 // case TokenNameStringLiteral :
479 // current.add(new PHPVarDeclaration(current, variableName,
482 // scanner.getCurrentTokenStartPosition(), new String(
485 // case TokenNameStringConstant :
486 // current.add(new PHPVarDeclaration(current, variableName,
489 // scanner.getCurrentTokenStartPosition(), new String(
493 // current.add(new PHPVarDeclaration(current, variableName,
496 // scanner.getCurrentTokenStartPosition()));
501 // ident = scanner.getCurrentIdentifierSource();
502 // current.add(new PHPVarDeclaration(current, variableName,
503 // // chIndx - ident.length
504 // scanner.getCurrentTokenStartPosition()));
507 // } else if (token == TokenNamefunction) {
509 // if (token == TokenNameAND) {
512 // if (token == TokenNameIdentifier
513 // && store.getBoolean(PHPeclipsePlugin.PHP_OUTLINE_FUNC)) {
514 // ident = scanner.getCurrentIdentifierSource();
515 // outlineInfo.addVariable(new String(ident));
516 // temp = new PHPFunctionDeclaration(current, new String(ident),
517 // // chIndx - ident.length
518 // scanner.getCurrentTokenStartPosition());
519 // current.add(temp);
521 // parseDeclarations(outlineInfo, temp, true);
523 // } else if (token == TokenNameclass) {
525 // if (token == TokenNameIdentifier
526 // && store.getBoolean(PHPeclipsePlugin.PHP_OUTLINE_CLASS)) {
527 // ident = scanner.getCurrentIdentifierSource();
528 // outlineInfo.addVariable(new String(ident));
529 // temp = new PHPClassDeclaration(current, new String(ident),
530 // // chIndx - ident.len
531 // scanner.getCurrentTokenStartPosition());
532 // current.add(temp);
533 // // stack.push(temp);
535 // //skip tokens for classname, extends and others until
536 // // we have the opening '{'
537 // while (token != TokenNameLBRACE && token != TokenNameEOF
538 // && token != TokenNameERROR) {
541 // parseDeclarations(outlineInfo, temp, true);
544 // } else if ((token == TokenNameLBRACE)
545 // || (token == TokenNameDOLLAR_LBRACE)) {
548 // } else if (token == TokenNameRBRACE) {
551 // if (counter == 0 && goBack) {
554 // } else if (token == TokenNamerequire || token == TokenNamerequire_once
555 // || token == TokenNameinclude || token == TokenNameinclude_once) {
556 // ident = scanner.getCurrentTokenSource();
558 // int startPosition = scanner.getCurrentTokenStartPosition();
560 // char[] expr = scanner.getCurrentTokenSource(startPosition);
561 // outlineInfo.addVariable(new String(ident));
562 // current.add(new PHPReqIncDeclaration(current, new String(ident),
563 // // chIndx - ident.length,
564 // startPosition, new String(expr)));
570 // } catch (SyntaxError sytaxErr) {
572 // // // setMarker(sytaxErr.getMessage(), sytaxErr.getLine(), ERROR);
573 // // setMarker(sytaxErr.getMessage(),
574 // // scanner.getCurrentTokenStartPosition(),
575 // // scanner.getCurrentTokenEndPosition(), ERROR);
576 // // } catch (CoreException e) {
580 private void statementList() {
582 statement(TokenNameEOF);
583 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
584 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
585 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
586 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
592 private void functionBody(MethodDeclaration methodDecl) {
593 // '{' [statement-list] '}'
594 if (token == TokenNameLBRACE) {
597 throwSyntaxError("'{' expected in compound-statement.");
599 if (token != TokenNameRBRACE) {
602 if (token == TokenNameRBRACE) {
603 methodDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
606 throwSyntaxError("'}' expected in compound-statement.");
610 private Statement statement(int previousToken) {
611 Statement statement = null;
612 Expression expression;
613 int sourceStart = scanner.getCurrentTokenStartPosition();
614 if (token == TokenNameif) {
616 if (token == TokenNameLPAREN) {
619 throwSyntaxError("'(' expected after 'if' keyword.");
622 if (token == TokenNameRPAREN) {
625 throwSyntaxError("')' expected after 'if' condition.");
628 return new IfStatement(expression, statement, sourceStart, scanner.getCurrentTokenEndPosition());
629 } else if (token == TokenNameswitch) {
631 if (token == TokenNameLPAREN) {
634 throwSyntaxError("'(' expected after 'switch' keyword.");
637 if (token == TokenNameRPAREN) {
640 throwSyntaxError("')' expected after 'switch' condition.");
644 } else if (token == TokenNamefor) {
646 if (token == TokenNameLPAREN) {
649 throwSyntaxError("'(' expected after 'for' keyword.");
651 if (token == TokenNameSEMICOLON) {
655 if (token == TokenNameSEMICOLON) {
658 throwSyntaxError("';' expected after 'for'.");
661 if (token == TokenNameSEMICOLON) {
665 if (token == TokenNameSEMICOLON) {
668 throwSyntaxError("';' expected after 'for'.");
671 if (token == TokenNameRPAREN) {
675 if (token == TokenNameRPAREN) {
678 throwSyntaxError("')' expected after 'for'.");
683 } else if (token == TokenNamewhile) {
685 if (token == TokenNameLPAREN) {
688 throwSyntaxError("'(' expected after 'while' keyword.");
691 if (token == TokenNameRPAREN) {
694 throwSyntaxError("')' expected after 'while' condition.");
698 } else if (token == TokenNamedo) {
700 if (token == TokenNameLBRACE) {
702 if (token != TokenNameRBRACE) {
705 if (token == TokenNameRBRACE) {
708 throwSyntaxError("'}' expected after 'do' keyword.");
711 statement(TokenNameEOF);
713 if (token == TokenNamewhile) {
715 if (token == TokenNameLPAREN) {
718 throwSyntaxError("'(' expected after 'while' keyword.");
721 if (token == TokenNameRPAREN) {
724 throwSyntaxError("')' expected after 'while' condition.");
727 throwSyntaxError("'while' expected after 'do' keyword.");
729 if (token == TokenNameSEMICOLON) {
732 if (token != TokenNameINLINE_HTML) {
733 throwSyntaxError("';' expected after do-while statement.");
738 } else if (token == TokenNameforeach) {
740 if (token == TokenNameLPAREN) {
743 throwSyntaxError("'(' expected after 'foreach' keyword.");
746 if (token == TokenNameas) {
749 throwSyntaxError("'as' expected after 'foreach' exxpression.");
753 foreach_optional_arg();
754 if (token == TokenNameEQUAL_GREATER) {
758 if (token == TokenNameRPAREN) {
761 throwSyntaxError("')' expected after 'foreach' expression.");
765 } else if (token == TokenNamecontinue || token == TokenNamebreak || token == TokenNamereturn) {
767 if (token != TokenNameSEMICOLON) {
770 if (token == TokenNameSEMICOLON) {
773 if (token != TokenNameINLINE_HTML) {
774 throwSyntaxError("';' expected after 'continue', 'break' or 'return'.");
779 } else if (token == TokenNameecho) {
782 if (token == TokenNameSEMICOLON) {
785 if (token != TokenNameINLINE_HTML) {
786 throwSyntaxError("';' expected after 'echo' statement.");
791 } else if (token == TokenNameINLINE_HTML) {
794 // } else if (token == TokenNameprint) {
797 // if (token == TokenNameSEMICOLON) {
800 // if (token != TokenNameStopPHP) {
801 // throwSyntaxError("';' expected after 'print' statement.");
806 } else if (token == TokenNameglobal) {
809 if (token == TokenNameSEMICOLON) {
812 if (token != TokenNameINLINE_HTML) {
813 throwSyntaxError("';' expected after 'global' statement.");
818 } else if (token == TokenNamestatic) {
821 if (token == TokenNameSEMICOLON) {
824 if (token != TokenNameINLINE_HTML) {
825 throwSyntaxError("';' expected after 'static' statement.");
830 } else if (token == TokenNameunset) {
832 if (token == TokenNameLPAREN) {
835 throwSyntaxError("'(' expected after 'unset' statement.");
838 if (token == TokenNameRPAREN) {
841 throwSyntaxError("')' expected after 'unset' statement.");
843 if (token == TokenNameSEMICOLON) {
846 if (token != TokenNameINLINE_HTML) {
847 throwSyntaxError("';' expected after 'unset' statement.");
852 } else if (token == TokenNamefunction) {
853 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
854 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
855 methodDecl.modifiers = AccDefault;
857 functionDefinition(methodDecl);
859 } else if (token == TokenNamedeclare) {
860 //T_DECLARE '(' declare_list ')' declare_statement
862 if (token != TokenNameLPAREN) {
863 throwSyntaxError("'(' expected in 'declare' statement.");
867 if (token != TokenNameRPAREN) {
868 throwSyntaxError("')' expected in 'declare' statement.");
873 } else if (token == TokenNametry) {
875 if (token != TokenNameLBRACE) {
876 throwSyntaxError("'{' expected in 'try' statement.");
880 if (token != TokenNameRBRACE) {
881 throwSyntaxError("'}' expected in 'try' statement.");
885 } else if (token == TokenNamecatch) {
887 if (token != TokenNameLPAREN) {
888 throwSyntaxError("'(' expected in 'catch' statement.");
891 fully_qualified_class_name();
892 if (token != TokenNameVariable) {
893 throwSyntaxError("Variable expected in 'catch' statement.");
896 if (token != TokenNameRPAREN) {
897 throwSyntaxError("')' expected in 'catch' statement.");
900 if (token != TokenNameLBRACE) {
901 throwSyntaxError("'{' expected in 'catch' statement.");
904 if (token != TokenNameRBRACE) {
906 if (token != TokenNameRBRACE) {
907 throwSyntaxError("'}' expected in 'catch' statement.");
911 additional_catches();
913 } else if (token == TokenNamethrow) {
916 if (token == TokenNameSEMICOLON) {
919 throwSyntaxError("';' expected after 'throw' exxpression.");
922 } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
923 TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
924 typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
925 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
926 typeDecl.name = new char[] { ' ' };
927 // default super class
928 typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
929 compilationUnit.types.add(typeDecl);
931 pushOnAstStack(typeDecl);
932 unticked_class_declaration_statement(typeDecl);
933 // classBody(typeDecl);
940 // throwSyntaxError("Unexpected keyword '" + keyword + "'");
941 } else if (token == TokenNameLBRACE) {
943 if (token != TokenNameRBRACE) {
946 if (token == TokenNameRBRACE) {
950 throwSyntaxError("'}' expected.");
953 if (token != TokenNameSEMICOLON) {
956 if (token == TokenNameSEMICOLON) {
960 if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
961 throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
970 private void declare_statement() {
972 //| ':' inner_statement_list T_ENDDECLARE ';'
974 if (token == TokenNameCOLON) {
976 // TODO: implement inner_statement_list();
978 if (token != TokenNameenddeclare) {
979 throwSyntaxError("'enddeclare' expected in 'declare' statement.");
982 if (token != TokenNameSEMICOLON) {
983 throwSyntaxError("';' expected after 'enddeclare' keyword.");
987 statement(TokenNameRPAREN);
991 private void declare_list() {
992 // T_STRING '=' static_scalar
993 //| declare_list ',' T_STRING '=' static_scalar
995 if (token != TokenNameIdentifier) {
996 throwSyntaxError("Identifier expected in 'declare' list.");
999 if (token != TokenNameEQUAL) {
1000 throwSyntaxError("'=' expected in 'declare' list.");
1004 if (token != TokenNameCOMMA) {
1011 private void additional_catches() {
1012 while (token == TokenNamecatch) {
1014 if (token != TokenNameLPAREN) {
1015 throwSyntaxError("'(' expected in 'catch' statement.");
1018 fully_qualified_class_name();
1019 if (token != TokenNameVariable) {
1020 throwSyntaxError("Variable expected in 'catch' statement.");
1023 if (token != TokenNameRPAREN) {
1024 throwSyntaxError("')' expected in 'catch' statement.");
1027 if (token != TokenNameLBRACE) {
1028 throwSyntaxError("'{' expected in 'catch' statement.");
1031 if (token != TokenNameRBRACE) {
1034 if (token != TokenNameRBRACE) {
1035 throwSyntaxError("'}' expected in 'catch' statement.");
1041 private void foreach_variable() {
1044 if (token == TokenNameAND) {
1050 private void foreach_optional_arg() {
1052 //| T_DOUBLE_ARROW foreach_variable
1053 if (token == TokenNameEQUAL_GREATER) {
1059 private void global_var_list() {
1061 // global_var_list ',' global_var
1065 if (token != TokenNameCOMMA) {
1072 private void global_var() {
1076 //| '$' '{' expr '}'
1077 if (token == TokenNameVariable) {
1079 } else if (token == TokenNameDOLLAR) {
1081 if (token == TokenNameLPAREN) {
1084 if (token != TokenNameLPAREN) {
1085 throwSyntaxError("')' expected in global variable.");
1094 private void static_var_list() {
1096 // static_var_list ',' T_VARIABLE
1097 //| static_var_list ',' T_VARIABLE '=' static_scalar
1099 //| T_VARIABLE '=' static_scalar
1101 if (token == TokenNameVariable) {
1103 if (token == TokenNameEQUAL) {
1107 if (token != TokenNameCOMMA) {
1117 private void unset_variables() {
1120 // | unset_variables ',' unset_variable
1125 if (token != TokenNameCOMMA) {
1132 private final void initializeModifiers() {
1134 this.modifiersSourceStart = -1;
1137 private final void checkAndSetModifiers(int flag) {
1138 this.modifiers |= flag;
1139 if (this.modifiersSourceStart < 0)
1140 this.modifiersSourceStart = this.scanner.startPosition;
1143 private void unticked_class_declaration_statement(TypeDeclaration typeDecl) {
1144 initializeModifiers();
1145 if (token == TokenNameinterface) {
1146 // interface_entry T_STRING
1147 // interface_extends_list
1148 // '{' class_statement_list '}'
1149 checkAndSetModifiers(AccInterface);
1151 typeDecl.modifiers = this.modifiers;
1152 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1153 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1154 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1155 typeDecl.name = scanner.getCurrentIdentifierSource();
1156 if (token > TokenNameKEYWORD) {
1157 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1158 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1159 // throwSyntaxError("Don't use a keyword for interface declaration [" + scanner.toStringAction(token) + "].",
1160 // typeDecl.sourceStart, typeDecl.sourceEnd);
1163 interface_extends_list();
1165 typeDecl.name = new char[] { ' ' };
1166 throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1170 // class_entry_type T_STRING extends_from
1172 // '{' class_statement_list'}'
1174 typeDecl.modifiers = this.modifiers;
1175 typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1176 typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1178 //identifier 'extends' identifier
1179 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1180 typeDecl.name = scanner.getCurrentIdentifierSource();
1181 if (token > TokenNameKEYWORD) {
1182 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1183 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1184 // throwSyntaxError("Don't use a keyword for class declaration [" + scanner.toStringAction(token) + "].",
1185 // typeDecl.sourceStart, typeDecl.sourceEnd);
1190 // | T_EXTENDS fully_qualified_class_name
1191 if (token == TokenNameextends) {
1192 interface_extends_list();
1194 // if (token != TokenNameIdentifier) {
1195 // throwSyntaxError("Class name expected after keyword
1197 // scanner.getCurrentTokenStartPosition(), scanner
1198 // .getCurrentTokenEndPosition());
1203 typeDecl.name = new char[] { ' ' };
1204 throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1208 // '{' class_statement_list '}'
1209 if (token == TokenNameLBRACE) {
1211 if (token != TokenNameRBRACE) {
1212 ArrayList list = new ArrayList();
1213 class_statement_list(list);
1214 typeDecl.fields = new FieldDeclaration[list.size()];
1215 for (int i = 0; i < list.size(); i++) {
1216 typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1219 if (token == TokenNameRBRACE) {
1220 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1223 throwSyntaxError("'}' expected at end of class body.");
1226 throwSyntaxError("'{' expected at start of class body.");
1230 private void class_entry_type() {
1232 // | T_ABSTRACT T_CLASS
1233 // | T_FINAL T_CLASS
1234 if (token == TokenNameclass) {
1236 } else if (token == TokenNameabstract) {
1237 checkAndSetModifiers(AccAbstract);
1239 if (token != TokenNameclass) {
1240 throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1243 } else if (token == TokenNamefinal) {
1244 checkAndSetModifiers(AccFinal);
1246 if (token != TokenNameclass) {
1247 throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1251 throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1255 private void interface_extends_list() {
1257 // | T_EXTENDS interface_list
1258 if (token == TokenNameextends) {
1264 private void implements_list() {
1266 // | T_IMPLEMENTS interface_list
1267 if (token == TokenNameimplements) {
1273 private void interface_list() {
1275 // fully_qualified_class_name
1276 //| interface_list ',' fully_qualified_class_name
1278 if (token == TokenNameIdentifier) {
1281 throwSyntaxError("Interface name expected after keyword 'implements'.");
1283 if (token != TokenNameCOMMA) {
1290 // private void classBody(TypeDeclaration typeDecl) {
1291 // //'{' [class-element-list] '}'
1292 // if (token == TokenNameLBRACE) {
1294 // if (token != TokenNameRBRACE) {
1295 // class_statement_list();
1297 // if (token == TokenNameRBRACE) {
1298 // typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1301 // throwSyntaxError("'}' expected at end of class body.");
1304 // throwSyntaxError("'{' expected at start of class body.");
1307 private void class_statement_list(ArrayList list) {
1309 class_statement(list);
1310 } while (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1311 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1312 || token == TokenNameconst);
1315 private void class_statement(ArrayList list) {
1317 // variable_modifiers class_variable_declaration ';'
1318 // | class_constant_declaration ';'
1319 // | method_modifiers T_FUNCTION is_reference T_STRING
1320 // '(' parameter_list ')' method_body
1321 initializeModifiers();
1322 int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1324 if (token == TokenNamevar) {
1325 checkAndSetModifiers(AccPublic);
1326 problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1327 referenceContext, compilationUnit.compilationResult);
1329 class_variable_declaration(declarationSourceStart, list);
1330 } else if (token == TokenNameconst) {
1331 checkAndSetModifiers(AccFinal | AccPublic);
1332 class_constant_declaration(declarationSourceStart, list);
1333 if (token != TokenNameSEMICOLON) {
1334 throwSyntaxError("';' expected after class const declaration.");
1338 boolean hasModifiers = member_modifiers();
1339 if (token == TokenNamefunction) {
1340 if (!hasModifiers) {
1341 checkAndSetModifiers(AccPublic);
1343 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1344 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1345 methodDecl.modifiers = this.modifiers;
1347 functionDefinition(methodDecl);
1349 if (!hasModifiers) {
1350 throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1352 class_variable_declaration(declarationSourceStart, list);
1357 private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1358 // class_constant_declaration ',' T_STRING '=' static_scalar
1359 // | T_CONST T_STRING '=' static_scalar
1360 if (token != TokenNameconst) {
1361 throwSyntaxError("'const' keyword expected in class declaration.");
1366 if (token != TokenNameIdentifier) {
1367 throwSyntaxError("Identifier expected in class const declaration.");
1369 FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1370 .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1371 fieldDeclaration.modifiers = this.modifiers;
1372 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1373 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1374 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1375 // fieldDeclaration.type
1376 list.add(fieldDeclaration);
1378 if (token != TokenNameEQUAL) {
1379 throwSyntaxError("'=' expected in class const declaration.");
1383 if (token != TokenNameCOMMA) {
1384 break; // while(true)-loop
1390 // private void variable_modifiers() {
1391 // // variable_modifiers:
1392 // // non_empty_member_modifiers
1394 // initializeModifiers();
1395 // if (token == TokenNamevar) {
1396 // checkAndSetModifiers(AccPublic);
1397 // reportSyntaxError(
1398 // "Keyword 'var' is deprecated. Please use 'public' 'private' or
1400 // modifier for field declarations.",
1401 // scanner.getCurrentTokenStartPosition(), scanner
1402 // .getCurrentTokenEndPosition());
1405 // if (!member_modifiers()) {
1406 // throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1407 // field declarations.");
1411 // private void method_modifiers() {
1412 // //method_modifiers:
1414 // //| non_empty_member_modifiers
1415 // initializeModifiers();
1416 // if (!member_modifiers()) {
1417 // checkAndSetModifiers(AccPublic);
1420 private boolean member_modifiers() {
1427 boolean foundToken = false;
1429 if (token == TokenNamepublic) {
1430 checkAndSetModifiers(AccPublic);
1433 } else if (token == TokenNameprotected) {
1434 checkAndSetModifiers(AccProtected);
1437 } else if (token == TokenNameprivate) {
1438 checkAndSetModifiers(AccPrivate);
1441 } else if (token == TokenNamestatic) {
1442 checkAndSetModifiers(AccStatic);
1445 } else if (token == TokenNameabstract) {
1446 checkAndSetModifiers(AccAbstract);
1449 } else if (token == TokenNamefinal) {
1450 checkAndSetModifiers(AccFinal);
1460 private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1461 // class_variable_declaration:
1462 // class_variable_declaration ',' T_VARIABLE
1463 // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1465 // | T_VARIABLE '=' static_scalar
1466 char[] classVariable;
1468 if (token == TokenNameVariable) {
1469 classVariable = scanner.getCurrentIdentifierSource();
1470 // indexManager.addIdentifierInformation('v', classVariable, buf, -1, -1);
1471 FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1472 .getCurrentTokenEndPosition());
1473 fieldDeclaration.modifiers = this.modifiers;
1474 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1475 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1476 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1477 // fieldDeclaration.type
1478 list.add(fieldDeclaration);
1480 if (token == TokenNameEQUAL) {
1485 // if (token == TokenNamethis) {
1486 // throwSyntaxError("'$this' not allowed after keyword 'public'
1487 // 'protected' 'private' 'var'.");
1489 throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1491 if (token != TokenNameCOMMA) {
1496 if (token != TokenNameSEMICOLON) {
1497 throwSyntaxError("';' expected after field declaration.");
1502 private void functionDefinition(MethodDeclaration methodDecl) {
1503 boolean isAbstract = false;
1505 compilationUnit.types.add(methodDecl);
1507 ASTNode node = astStack[astPtr];
1508 if (node instanceof TypeDeclaration) {
1509 TypeDeclaration typeDecl = ((TypeDeclaration) node);
1510 if (typeDecl.methods == null) {
1511 typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1513 AbstractMethodDeclaration[] newMethods;
1514 System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 1,
1515 typeDecl.methods.length);
1516 newMethods[0] = methodDecl;
1517 typeDecl.methods = newMethods;
1519 if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1521 } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1526 functionDeclarator(methodDecl);
1527 if (token == TokenNameSEMICOLON) {
1529 throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1534 functionBody(methodDecl);
1537 private void functionDeclarator(MethodDeclaration methodDecl) {
1538 //identifier '(' [parameter-list] ')'
1539 if (token == TokenNameAND) {
1542 methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1543 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1544 if (Scanner.isIdentifierOrKeyword(token)) {
1545 methodDecl.selector = scanner.getCurrentIdentifierSource();
1546 if (token > TokenNameKEYWORD) {
1547 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1548 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1549 // reportSyntaxWarning("Don't use keyword for function declaration [" + scanner.toStringAction(token) + "].",
1550 // scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1553 if (token == TokenNameLPAREN) {
1556 throwSyntaxError("'(' expected in function declaration.");
1558 if (token != TokenNameRPAREN) {
1561 if (token != TokenNameRPAREN) {
1562 throwSyntaxError("')' expected in function declaration.");
1564 methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1568 methodDecl.selector = "<undefined>".toCharArray();
1569 throwSyntaxError("Function name expected after keyword 'function'.");
1574 private void parameter_list() {
1575 // non_empty_parameter_list
1577 non_empty_parameter_list(true);
1580 private void non_empty_parameter_list(boolean empty_allowed) {
1581 // optional_class_type T_VARIABLE
1582 // | optional_class_type '&' T_VARIABLE
1583 // | optional_class_type '&' T_VARIABLE '=' static_scalar
1584 // | optional_class_type T_VARIABLE '=' static_scalar
1585 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1586 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1587 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1589 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1591 if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1593 if (token == TokenNameIdentifier) {
1596 if (token == TokenNameAND) {
1599 if (token == TokenNameVariable) {
1601 if (token == TokenNameEQUAL) {
1606 throwSyntaxError("Variable expected in parameter list.");
1608 if (token != TokenNameCOMMA) {
1615 if (!empty_allowed) {
1616 throwSyntaxError("Identifier expected in parameter list.");
1620 private void optional_class_type() {
1625 private void parameterDeclaration() {
1627 //variable-reference
1628 if (token == TokenNameAND) {
1633 throwSyntaxError("Variable expected after reference operator '&'.");
1636 //variable '=' constant
1637 if (token == TokenNameVariable) {
1639 if (token == TokenNameEQUAL) {
1645 // if (token == TokenNamethis) {
1646 // throwSyntaxError("Reserved word '$this' not allowed in parameter
1651 private void labeledStatementList() {
1652 if (token != TokenNamecase && token != TokenNamedefault) {
1653 throwSyntaxError("'case' or 'default' expected.");
1656 if (token == TokenNamecase) {
1658 expr(); //constant();
1659 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1661 if (token == TokenNamecase || token == TokenNamedefault) {
1662 // empty case statement ?
1667 // else if (token == TokenNameSEMICOLON) {
1669 // "':' expected after 'case' keyword (Found token: " +
1670 // scanner.toStringAction(token) + ")",
1671 // scanner.getCurrentTokenStartPosition(),
1672 // scanner.getCurrentTokenEndPosition(),
1675 // if (token == TokenNamecase) { // empty case statement ?
1681 throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1683 } else { // TokenNamedefault
1685 if (token == TokenNameCOLON) {
1687 if (token == TokenNameRBRACE) {
1688 // empty default case
1691 if (token != TokenNamecase) {
1695 throwSyntaxError("':' character expected after 'default'.");
1698 } while (token == TokenNamecase || token == TokenNamedefault);
1701 // public void labeledStatement() {
1702 // if (token == TokenNamecase) {
1705 // if (token == TokenNameDDOT) {
1709 // throwSyntaxError("':' character after 'case' constant expected.");
1712 // } else if (token == TokenNamedefault) {
1714 // if (token == TokenNameDDOT) {
1718 // throwSyntaxError("':' character after 'default' expected.");
1723 // public void expressionStatement() {
1725 // private void inclusionStatement() {
1727 // public void compoundStatement() {
1729 // public void selectionStatement() {
1732 // public void iterationStatement() {
1735 // public void jumpStatement() {
1738 // public void outputStatement() {
1741 // public void scopeStatement() {
1744 // public void flowStatement() {
1747 // public void definitionStatement() {
1749 private void ifStatement() {
1750 // ':' statement-list [elseif-list] [else-colon-statement] 'endif' ';'
1751 if (token == TokenNameCOLON) {
1753 if (token != TokenNameendif) {
1758 if (token == TokenNameCOLON) {
1760 if (token != TokenNameendif) {
1764 if (token == TokenNameif) { //'else if'
1766 elseifStatementList();
1768 throwSyntaxError("':' expected after 'else'.");
1772 case TokenNameelseif:
1774 elseifStatementList();
1778 if (token != TokenNameendif) {
1779 throwSyntaxError("'endif' expected.");
1782 if (token != TokenNameSEMICOLON) {
1783 throwSyntaxError("';' expected after if-statement.");
1787 // statement [else-statement]
1788 statement(TokenNameEOF);
1789 if (token == TokenNameelseif) {
1791 if (token == TokenNameLPAREN) {
1794 throwSyntaxError("'(' expected after 'elseif' keyword.");
1797 if (token == TokenNameRPAREN) {
1800 throwSyntaxError("')' expected after 'elseif' condition.");
1803 } else if (token == TokenNameelse) {
1805 statement(TokenNameEOF);
1810 private void elseifStatementList() {
1816 if (token == TokenNameCOLON) {
1818 if (token != TokenNameendif) {
1823 if (token == TokenNameif) { //'else if'
1826 throwSyntaxError("':' expected after 'else'.");
1830 case TokenNameelseif:
1839 private void elseifStatement() {
1840 if (token == TokenNameLPAREN) {
1843 if (token != TokenNameRPAREN) {
1844 throwSyntaxError("')' expected in else-if-statement.");
1847 if (token != TokenNameCOLON) {
1848 throwSyntaxError("':' expected in else-if-statement.");
1851 if (token != TokenNameendif) {
1857 private void switchStatement() {
1858 if (token == TokenNameCOLON) {
1859 // ':' [labeled-statement-list] 'endswitch' ';'
1861 labeledStatementList();
1862 if (token != TokenNameendswitch) {
1863 throwSyntaxError("'endswitch' expected.");
1866 if (token != TokenNameSEMICOLON) {
1867 throwSyntaxError("';' expected after switch-statement.");
1871 // '{' [labeled-statement-list] '}'
1872 if (token != TokenNameLBRACE) {
1873 throwSyntaxError("'{' expected in switch statement.");
1876 if (token != TokenNameRBRACE) {
1877 labeledStatementList();
1879 if (token != TokenNameRBRACE) {
1880 throwSyntaxError("'}' expected in switch statement.");
1886 private void forStatement() {
1887 if (token == TokenNameCOLON) {
1890 if (token != TokenNameendfor) {
1891 throwSyntaxError("'endfor' expected.");
1894 if (token != TokenNameSEMICOLON) {
1895 throwSyntaxError("';' expected after for-statement.");
1899 statement(TokenNameEOF);
1903 private void whileStatement() {
1904 // ':' statement-list 'endwhile' ';'
1905 if (token == TokenNameCOLON) {
1908 if (token != TokenNameendwhile) {
1909 throwSyntaxError("'endwhile' expected.");
1912 if (token != TokenNameSEMICOLON) {
1913 throwSyntaxError("';' expected after while-statement.");
1917 statement(TokenNameEOF);
1921 private void foreachStatement() {
1922 if (token == TokenNameCOLON) {
1925 if (token != TokenNameendforeach) {
1926 throwSyntaxError("'endforeach' expected.");
1929 if (token != TokenNameSEMICOLON) {
1930 throwSyntaxError("';' expected after foreach-statement.");
1934 statement(TokenNameEOF);
1938 // private void exitStatus() {
1939 // if (token == TokenNameLPAREN) {
1942 // throwSyntaxError("'(' expected in 'exit-status'.");
1944 // if (token != TokenNameRPAREN) {
1947 // if (token == TokenNameRPAREN) {
1950 // throwSyntaxError("')' expected after 'exit-status'.");
1953 private void expressionList() {
1956 if (token == TokenNameCOMMA) {
1964 private Expression expr() {
1966 // | expr_without_variable
1967 // if (token!=TokenNameEOF) {
1968 if (Scanner.TRACE) {
1969 System.out.println("TRACE: expr()");
1971 return expr_without_variable(true);
1975 private Expression expr_without_variable(boolean only_variable) {
1976 int exprSourceStart = scanner.getCurrentTokenStartPosition();
1977 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
1978 Expression expression = new Expression();
1979 expression.sourceStart = exprSourceStart;
1980 // default, may be overwritten
1981 expression.sourceEnd = exprSourceEnd;
1982 // internal_functions_in_yacc
1991 // | T_INC rw_variable
1992 // | T_DEC rw_variable
1993 // | T_INT_CAST expr
1994 // | T_DOUBLE_CAST expr
1995 // | T_STRING_CAST expr
1996 // | T_ARRAY_CAST expr
1997 // | T_OBJECT_CAST expr
1998 // | T_BOOL_CAST expr
1999 // | T_UNSET_CAST expr
2000 // | T_EXIT exit_expr
2002 // | T_ARRAY '(' array_pair_list ')'
2003 // | '`' encaps_list '`'
2004 // | T_LIST '(' assignment_list ')' '=' expr
2005 // | T_NEW class_name_reference ctor_arguments
2006 // | variable '=' expr
2007 // | variable '=' '&' variable
2008 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2009 // | variable T_PLUS_EQUAL expr
2010 // | variable T_MINUS_EQUAL expr
2011 // | variable T_MUL_EQUAL expr
2012 // | variable T_DIV_EQUAL expr
2013 // | variable T_CONCAT_EQUAL expr
2014 // | variable T_MOD_EQUAL expr
2015 // | variable T_AND_EQUAL expr
2016 // | variable T_OR_EQUAL expr
2017 // | variable T_XOR_EQUAL expr
2018 // | variable T_SL_EQUAL expr
2019 // | variable T_SR_EQUAL expr
2020 // | rw_variable T_INC
2021 // | rw_variable T_DEC
2022 // | expr T_BOOLEAN_OR expr
2023 // | expr T_BOOLEAN_AND expr
2024 // | expr T_LOGICAL_OR expr
2025 // | expr T_LOGICAL_AND expr
2026 // | expr T_LOGICAL_XOR expr
2038 // | expr T_IS_IDENTICAL expr
2039 // | expr T_IS_NOT_IDENTICAL expr
2040 // | expr T_IS_EQUAL expr
2041 // | expr T_IS_NOT_EQUAL expr
2043 // | expr T_IS_SMALLER_OR_EQUAL expr
2045 // | expr T_IS_GREATER_OR_EQUAL expr
2046 // | expr T_INSTANCEOF class_name_reference
2047 // | expr '?' expr ':' expr
2048 if (Scanner.TRACE) {
2049 System.out.println("TRACE: expr_without_variable() PART 1");
2052 case TokenNameisset:
2053 case TokenNameempty:
2055 case TokenNameinclude:
2056 case TokenNameinclude_once:
2057 case TokenNamerequire:
2058 case TokenNamerequire_once:
2059 internal_functions_in_yacc();
2062 case TokenNameLPAREN:
2065 if (token == TokenNameRPAREN) {
2068 throwSyntaxError("')' expected in expression.");
2078 // | T_INT_CAST expr
2079 // | T_DOUBLE_CAST expr
2080 // | T_STRING_CAST expr
2081 // | T_ARRAY_CAST expr
2082 // | T_OBJECT_CAST expr
2083 // | T_BOOL_CAST expr
2084 // | T_UNSET_CAST expr
2085 case TokenNameclone:
2086 case TokenNameprint:
2089 case TokenNameMINUS:
2091 case TokenNameTWIDDLE:
2092 case TokenNameintCAST:
2093 case TokenNamedoubleCAST:
2094 case TokenNamestringCAST:
2095 case TokenNamearrayCAST:
2096 case TokenNameobjectCAST:
2097 case TokenNameboolCAST:
2098 case TokenNameunsetCAST:
2108 //| T_STRING_VARNAME
2110 //| T_START_HEREDOC encaps_list T_END_HEREDOC
2111 // | '`' encaps_list '`'
2113 // | '`' encaps_list '`'
2114 case TokenNameEncapsedString0:
2115 scanner.encapsedStringStack.push(new Character('`'));
2118 if (token == TokenNameEncapsedString0) {
2121 if (token != TokenNameEncapsedString0) {
2122 throwSyntaxError("\'`\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2126 scanner.encapsedStringStack.pop();
2130 // | '\'' encaps_list '\''
2131 case TokenNameEncapsedString1:
2132 scanner.encapsedStringStack.push(new Character('\''));
2135 exprSourceStart = scanner.getCurrentTokenStartPosition();
2136 if (token == TokenNameEncapsedString1) {
2137 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2138 .getCurrentTokenEndPosition());
2141 if (token != TokenNameEncapsedString1) {
2142 throwSyntaxError("\'\'\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2144 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2145 .getCurrentTokenEndPosition());
2149 scanner.encapsedStringStack.pop();
2153 //| '"' encaps_list '"'
2154 case TokenNameEncapsedString2:
2155 scanner.encapsedStringStack.push(new Character('"'));
2158 exprSourceStart = scanner.getCurrentTokenStartPosition();
2159 if (token == TokenNameEncapsedString2) {
2160 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2161 .getCurrentTokenEndPosition());
2164 if (token != TokenNameEncapsedString2) {
2165 throwSyntaxError("'\"' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2167 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2168 .getCurrentTokenEndPosition());
2172 scanner.encapsedStringStack.pop();
2176 case TokenNameStringDoubleQuote:
2177 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2178 .getCurrentTokenEndPosition());
2181 case TokenNameStringSingleQuote:
2182 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2183 .getCurrentTokenEndPosition());
2186 case TokenNameIntegerLiteral:
2187 case TokenNameDoubleLiteral:
2188 case TokenNameStringInterpolated:
2191 case TokenNameCLASS_C:
2192 case TokenNameMETHOD_C:
2193 case TokenNameFUNC_C:
2196 case TokenNameHEREDOC:
2199 case TokenNamearray:
2200 // T_ARRAY '(' array_pair_list ')'
2202 if (token == TokenNameLPAREN) {
2204 if (token == TokenNameRPAREN) {
2209 if (token != TokenNameRPAREN) {
2210 throwSyntaxError("')' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2214 throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2218 // | T_LIST '(' assignment_list ')' '=' expr
2220 if (token == TokenNameLPAREN) {
2223 if (token != TokenNameRPAREN) {
2224 throwSyntaxError("')' expected after 'list' keyword.");
2227 if (token != TokenNameEQUAL) {
2228 throwSyntaxError("'=' expected after 'list' keyword.");
2233 throwSyntaxError("'(' expected after 'list' keyword.");
2237 // | T_NEW class_name_reference ctor_arguments
2239 class_name_reference();
2242 // | T_INC rw_variable
2243 // | T_DEC rw_variable
2244 case TokenNamePLUS_PLUS:
2245 case TokenNameMINUS_MINUS:
2249 // | variable '=' expr
2250 // | variable '=' '&' variable
2251 // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2252 // | variable T_PLUS_EQUAL expr
2253 // | variable T_MINUS_EQUAL expr
2254 // | variable T_MUL_EQUAL expr
2255 // | variable T_DIV_EQUAL expr
2256 // | variable T_CONCAT_EQUAL expr
2257 // | variable T_MOD_EQUAL expr
2258 // | variable T_AND_EQUAL expr
2259 // | variable T_OR_EQUAL expr
2260 // | variable T_XOR_EQUAL expr
2261 // | variable T_SL_EQUAL expr
2262 // | variable T_SR_EQUAL expr
2263 // | rw_variable T_INC
2264 // | rw_variable T_DEC
2265 case TokenNameIdentifier:
2266 case TokenNameVariable:
2267 case TokenNameDOLLAR:
2270 case TokenNameEQUAL:
2272 if (token == TokenNameAND) {
2274 if (token == TokenNamenew) {
2275 // | variable '=' '&' T_NEW class_name_reference
2278 class_name_reference();
2287 case TokenNamePLUS_EQUAL:
2288 case TokenNameMINUS_EQUAL:
2289 case TokenNameMULTIPLY_EQUAL:
2290 case TokenNameDIVIDE_EQUAL:
2291 case TokenNameDOT_EQUAL:
2292 case TokenNameREMAINDER_EQUAL:
2293 case TokenNameAND_EQUAL:
2294 case TokenNameOR_EQUAL:
2295 case TokenNameXOR_EQUAL:
2296 case TokenNameRIGHT_SHIFT_EQUAL:
2297 case TokenNameLEFT_SHIFT_EQUAL:
2301 case TokenNamePLUS_PLUS:
2302 case TokenNameMINUS_MINUS:
2306 if (!only_variable) {
2307 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2312 if (token != TokenNameINLINE_HTML) {
2313 if (token > TokenNameKEYWORD) {
2317 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2322 if (Scanner.TRACE) {
2323 System.out.println("TRACE: expr_without_variable() PART 2");
2325 // | expr T_BOOLEAN_OR expr
2326 // | expr T_BOOLEAN_AND expr
2327 // | expr T_LOGICAL_OR expr
2328 // | expr T_LOGICAL_AND expr
2329 // | expr T_LOGICAL_XOR expr
2341 // | expr T_IS_IDENTICAL expr
2342 // | expr T_IS_NOT_IDENTICAL expr
2343 // | expr T_IS_EQUAL expr
2344 // | expr T_IS_NOT_EQUAL expr
2346 // | expr T_IS_SMALLER_OR_EQUAL expr
2348 // | expr T_IS_GREATER_OR_EQUAL expr
2351 case TokenNameOR_OR:
2353 expression = new OR_OR_Expression(expression, expr(), token);
2355 case TokenNameAND_AND:
2357 expression = new AND_AND_Expression(expression, expr(), token);
2359 case TokenNameEQUAL_EQUAL:
2361 expression = new EqualExpression(expression, expr(), token);
2371 case TokenNameMINUS:
2372 case TokenNameMULTIPLY:
2373 case TokenNameDIVIDE:
2374 case TokenNameREMAINDER:
2375 case TokenNameLEFT_SHIFT:
2376 case TokenNameRIGHT_SHIFT:
2377 case TokenNameEQUAL_EQUAL_EQUAL:
2378 case TokenNameNOT_EQUAL_EQUAL:
2379 case TokenNameNOT_EQUAL:
2381 case TokenNameLESS_EQUAL:
2382 case TokenNameGREATER:
2383 case TokenNameGREATER_EQUAL:
2385 expression = new BinaryExpression(expression, expr(), token);
2387 // | expr T_INSTANCEOF class_name_reference
2388 // | expr '?' expr ':' expr
2389 case TokenNameinstanceof:
2391 class_name_reference();
2392 // TODO use InstanceofExpression
2393 expression = new Expression();
2394 expression.sourceStart = exprSourceStart;
2395 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2397 case TokenNameQUESTION:
2399 Expression valueIfTrue = expr();
2400 if (token != TokenNameCOLON) {
2401 throwSyntaxError("':' expected in conditional expression.");
2404 Expression valueIfFalse = expr();
2406 expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2414 private void class_name_reference() {
2415 // class_name_reference:
2417 //| dynamic_class_name_reference
2418 if (Scanner.TRACE) {
2419 System.out.println("TRACE: class_name_reference()");
2421 if (token == TokenNameIdentifier) {
2424 dynamic_class_name_reference();
2428 private void dynamic_class_name_reference() {
2429 //dynamic_class_name_reference:
2430 // base_variable T_OBJECT_OPERATOR object_property
2431 // dynamic_class_name_variable_properties
2433 if (Scanner.TRACE) {
2434 System.out.println("TRACE: dynamic_class_name_reference()");
2437 if (token == TokenNameMINUS_GREATER) {
2440 dynamic_class_name_variable_properties();
2444 private void dynamic_class_name_variable_properties() {
2445 // dynamic_class_name_variable_properties:
2446 // dynamic_class_name_variable_properties
2447 // dynamic_class_name_variable_property
2449 if (Scanner.TRACE) {
2450 System.out.println("TRACE: dynamic_class_name_variable_properties()");
2452 while (token == TokenNameMINUS_GREATER) {
2453 dynamic_class_name_variable_property();
2457 private void dynamic_class_name_variable_property() {
2458 // dynamic_class_name_variable_property:
2459 // T_OBJECT_OPERATOR object_property
2460 if (Scanner.TRACE) {
2461 System.out.println("TRACE: dynamic_class_name_variable_property()");
2463 if (token == TokenNameMINUS_GREATER) {
2469 private void ctor_arguments() {
2472 //| '(' function_call_parameter_list ')'
2473 if (token == TokenNameLPAREN) {
2475 if (token == TokenNameRPAREN) {
2479 non_empty_function_call_parameter_list();
2480 if (token != TokenNameRPAREN) {
2481 throwSyntaxError("')' expected in ctor_arguments.");
2487 private void assignment_list() {
2489 // assignment_list ',' assignment_list_element
2490 //| assignment_list_element
2492 assignment_list_element();
2493 if (token != TokenNameCOMMA) {
2500 private void assignment_list_element() {
2501 //assignment_list_element:
2503 //| T_LIST '(' assignment_list ')'
2505 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
2508 if (token == TokenNamelist) {
2510 if (token == TokenNameLPAREN) {
2513 if (token != TokenNameRPAREN) {
2514 throwSyntaxError("')' expected after 'list' keyword.");
2518 throwSyntaxError("'(' expected after 'list' keyword.");
2524 private void array_pair_list() {
2527 //| non_empty_array_pair_list possible_comma
2528 non_empty_array_pair_list();
2529 if (token == TokenNameCOMMA) {
2534 private void non_empty_array_pair_list() {
2535 //non_empty_array_pair_list:
2536 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2537 //| non_empty_array_pair_list ',' expr
2538 //| expr T_DOUBLE_ARROW expr
2540 //| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2541 //| non_empty_array_pair_list ',' '&' w_variable
2542 //| expr T_DOUBLE_ARROW '&' w_variable
2545 if (token == TokenNameAND) {
2550 if (token == TokenNameAND) {
2553 } else if (token == TokenNameEQUAL_GREATER) {
2555 if (token == TokenNameAND) {
2563 if (token != TokenNameCOMMA) {
2567 if (token == TokenNameRPAREN) {
2573 // private void variableList() {
2576 // if (token == TokenNameCOMMA) {
2583 private void variable_without_objects() {
2584 // variable_without_objects:
2585 // reference_variable
2586 // | simple_indirect_reference reference_variable
2587 if (Scanner.TRACE) {
2588 System.out.println("TRACE: variable_without_objects()");
2590 while (token == TokenNameDOLLAR) {
2593 reference_variable();
2596 private void function_call() {
2598 // T_STRING '(' function_call_parameter_list ')'
2599 //| class_constant '(' function_call_parameter_list ')'
2600 //| static_member '(' function_call_parameter_list ')'
2601 //| variable_without_objects '(' function_call_parameter_list ')'
2602 char[] defineName = null;
2603 char[] ident = null;
2606 if (Scanner.TRACE) {
2607 System.out.println("TRACE: function_call()");
2609 if (token == TokenNameIdentifier) {
2610 ident = scanner.getCurrentIdentifierSource();
2612 startPos = scanner.getCurrentTokenStartPosition();
2613 endPos = scanner.getCurrentTokenEndPosition();
2616 case TokenNamePAAMAYIM_NEKUDOTAYIM:
2620 if (token == TokenNameIdentifier) {
2625 variable_without_objects();
2630 variable_without_objects();
2632 if (token != TokenNameLPAREN) {
2633 if (defineName != null) {
2634 // does this identifier contain only uppercase characters?
2635 if (defineName.length == 3) {
2636 if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2639 } else if (defineName.length == 4) {
2640 if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2642 } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
2645 } else if (defineName.length == 5) {
2646 if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
2650 if (defineName != null) {
2651 for (int i = 0; i < defineName.length; i++) {
2652 if (Character.isLowerCase(defineName[i])) {
2653 problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
2659 // TODO is this ok ?
2661 // throwSyntaxError("'(' expected in function call.");
2664 if (token == TokenNameRPAREN) {
2668 non_empty_function_call_parameter_list();
2669 if (token != TokenNameRPAREN) {
2670 String functionName;
2671 if (ident == null) {
2672 functionName = new String(" ");
2674 functionName = new String(ident);
2676 throwSyntaxError("')' expected in function call (" + functionName + ").");
2681 // private void function_call_parameter_list() {
2682 // function_call_parameter_list:
2683 // non_empty_function_call_parameter_list { $$ = $1; }
2686 private void non_empty_function_call_parameter_list() {
2687 //non_empty_function_call_parameter_list:
2688 // expr_without_variable
2691 // | non_empty_function_call_parameter_list ',' expr_without_variable
2692 // | non_empty_function_call_parameter_list ',' variable
2693 // | non_empty_function_call_parameter_list ',' '&' w_variable
2694 if (Scanner.TRACE) {
2695 System.out.println("TRACE: non_empty_function_call_parameter_list()");
2698 if (token == TokenNameAND) {
2702 // if (token == TokenNameIdentifier || token ==
2703 // TokenNameVariable
2704 // || token == TokenNameDOLLAR) {
2707 expr_without_variable(true);
2710 if (token != TokenNameCOMMA) {
2717 private void fully_qualified_class_name() {
2718 if (token == TokenNameIdentifier) {
2721 throwSyntaxError("Class name expected.");
2725 private void static_member() {
2727 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
2728 // variable_without_objects
2729 if (Scanner.TRACE) {
2730 System.out.println("TRACE: static_member()");
2732 fully_qualified_class_name();
2733 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
2734 throwSyntaxError("'::' expected after class name (static_member).");
2737 variable_without_objects();
2740 private void base_variable_with_function_calls() {
2741 // base_variable_with_function_calls:
2744 boolean functionCall = false;
2745 if (Scanner.TRACE) {
2746 System.out.println("TRACE: base_variable_with_function_calls()");
2748 // if (token == TokenNameIdentifier) {
2749 // functionCall = true;
2750 // } else if (token == TokenNameVariable) {
2751 // int tempToken = token;
2752 // int tempPosition = scanner.currentPosition;
2754 // if (token == TokenNameLPAREN) {
2755 // functionCall = true;
2757 // token = tempToken;
2758 // scanner.currentPosition = tempPosition;
2759 // scanner.phpMode = true;
2761 // if (functionCall) {
2768 private void base_variable() {
2770 // reference_variable
2771 // | simple_indirect_reference reference_variable
2773 if (Scanner.TRACE) {
2774 System.out.println("TRACE: base_variable()");
2776 if (token == TokenNameIdentifier) {
2779 while (token == TokenNameDOLLAR) {
2782 reference_variable();
2786 // private void simple_indirect_reference() {
2787 // // simple_indirect_reference:
2789 // //| simple_indirect_reference '$'
2791 private void reference_variable() {
2792 // reference_variable:
2793 // reference_variable '[' dim_offset ']'
2794 // | reference_variable '{' expr '}'
2795 // | compound_variable
2796 if (Scanner.TRACE) {
2797 System.out.println("TRACE: reference_variable()");
2799 compound_variable();
2801 if (token == TokenNameLBRACE) {
2804 if (token != TokenNameRBRACE) {
2805 throwSyntaxError("'}' expected in reference variable.");
2808 } else if (token == TokenNameLBRACKET) {
2810 if (token != TokenNameRBRACKET) {
2813 if (token != TokenNameRBRACKET) {
2814 throwSyntaxError("']' expected in reference variable.");
2824 private void compound_variable() {
2825 // compound_variable:
2827 // | '$' '{' expr '}'
2828 if (Scanner.TRACE) {
2829 System.out.println("TRACE: compound_variable()");
2831 if (token == TokenNameVariable) {
2834 // because of simple_indirect_reference
2835 while (token == TokenNameDOLLAR) {
2838 if (token != TokenNameLBRACE) {
2839 throwSyntaxError("'{' expected after compound variable token '$'.");
2843 if (token != TokenNameRBRACE) {
2844 throwSyntaxError("'}' expected after compound variable token '$'.");
2850 // private void dim_offset() {
2856 private void object_property() {
2859 //| variable_without_objects
2860 if (Scanner.TRACE) {
2861 System.out.println("TRACE: object_property()");
2863 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
2864 variable_without_objects();
2870 private void object_dim_list() {
2872 // object_dim_list '[' dim_offset ']'
2873 //| object_dim_list '{' expr '}'
2875 if (Scanner.TRACE) {
2876 System.out.println("TRACE: object_dim_list()");
2880 if (token == TokenNameLBRACE) {
2883 if (token != TokenNameRBRACE) {
2884 throwSyntaxError("'}' expected in object_dim_list.");
2887 } else if (token == TokenNameLBRACKET) {
2889 if (token == TokenNameRBRACKET) {
2894 if (token != TokenNameRBRACKET) {
2895 throwSyntaxError("']' expected in object_dim_list.");
2904 private void variable_name() {
2908 if (Scanner.TRACE) {
2909 System.out.println("TRACE: variable_name()");
2911 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
2912 if (token > TokenNameKEYWORD) {
2913 // TODO show a warning "Keyword used as variable" ?
2917 if (token != TokenNameLBRACE) {
2918 throwSyntaxError("'{' expected in variable name.");
2922 if (token != TokenNameRBRACE) {
2923 throwSyntaxError("'}' expected in variable name.");
2929 private void r_variable() {
2933 private void w_variable() {
2937 private void rw_variable() {
2941 private void variable() {
2943 // base_variable_with_function_calls T_OBJECT_OPERATOR
2944 // object_property method_or_not variable_properties
2945 // | base_variable_with_function_calls
2946 base_variable_with_function_calls();
2947 if (token == TokenNameMINUS_GREATER) {
2951 variable_properties();
2953 // if (token == TokenNameDOLLAR_LBRACE) {
2957 // if (token != TokenNameRBRACE) {
2958 // throwSyntaxError("'}' expected after indirect variable token '${'.");
2962 // if (token == TokenNameVariable) {
2964 // if (token == TokenNameLBRACKET) {
2967 // if (token != TokenNameRBRACKET) {
2968 // throwSyntaxError("']' expected in variable-list.");
2971 // } else if (token == TokenNameEQUAL) {
2976 // throwSyntaxError("$-variable expected in variable-list.");
2981 private void variable_properties() {
2982 // variable_properties:
2983 // variable_properties variable_property
2985 while (token == TokenNameMINUS_GREATER) {
2986 variable_property();
2990 private void variable_property() {
2991 // variable_property:
2992 // T_OBJECT_OPERATOR object_property method_or_not
2993 if (Scanner.TRACE) {
2994 System.out.println("TRACE: variable_property()");
2996 if (token == TokenNameMINUS_GREATER) {
3001 throwSyntaxError("'->' expected in variable_property.");
3005 private void method_or_not() {
3007 // '(' function_call_parameter_list ')'
3009 if (Scanner.TRACE) {
3010 System.out.println("TRACE: method_or_not()");
3012 if (token == TokenNameLPAREN) {
3014 if (token == TokenNameRPAREN) {
3018 non_empty_function_call_parameter_list();
3019 if (token != TokenNameRPAREN) {
3020 throwSyntaxError("')' expected in method_or_not.");
3026 private void exit_expr() {
3030 if (token != TokenNameLPAREN) {
3034 if (token == TokenNameRPAREN) {
3039 if (token != TokenNameRPAREN) {
3040 throwSyntaxError("')' expected after keyword 'exit'");
3045 private void encaps_list() {
3046 // encaps_list encaps_var
3047 // | encaps_list T_STRING
3048 // | encaps_list T_NUM_STRING
3049 // | encaps_list T_ENCAPSED_AND_WHITESPACE
3050 // | encaps_list T_CHARACTER
3051 // | encaps_list T_BAD_CHARACTER
3052 // | encaps_list '['
3053 // | encaps_list ']'
3054 // | encaps_list '{'
3055 // | encaps_list '}'
3056 // | encaps_list T_OBJECT_OPERATOR
3060 case TokenNameSTRING:
3063 case TokenNameLBRACE:
3064 // scanner.encapsedStringStack.pop();
3067 case TokenNameRBRACE:
3068 // scanner.encapsedStringStack.pop();
3071 case TokenNameLBRACKET:
3072 // scanner.encapsedStringStack.pop();
3075 case TokenNameRBRACKET:
3076 // scanner.encapsedStringStack.pop();
3079 case TokenNameMINUS_GREATER:
3080 // scanner.encapsedStringStack.pop();
3083 case TokenNameVariable:
3084 case TokenNameDOLLAR_LBRACE:
3085 case TokenNameLBRACE_DOLLAR:
3089 char encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
3090 if (encapsedChar == '$') {
3091 scanner.encapsedStringStack.pop();
3092 encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
3093 switch (encapsedChar) {
3095 if (token == TokenNameEncapsedString0) {
3098 token = TokenNameSTRING;
3101 if (token == TokenNameEncapsedString1) {
3104 token = TokenNameSTRING;
3107 if (token == TokenNameEncapsedString2) {
3110 token = TokenNameSTRING;
3119 private void encaps_var() {
3121 // | T_VARIABLE '[' encaps_var_offset ']'
3122 // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3123 // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3124 // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3125 // | T_CURLY_OPEN variable '}'
3127 case TokenNameVariable:
3129 if (token == TokenNameLBRACKET) {
3131 expr(); //encaps_var_offset();
3132 if (token != TokenNameRBRACKET) {
3133 throwSyntaxError("']' expected after variable.");
3135 // scanner.encapsedStringStack.pop();
3138 } else if (token == TokenNameMINUS_GREATER) {
3140 if (token != TokenNameIdentifier) {
3141 throwSyntaxError("Identifier expected after '->'.");
3143 // scanner.encapsedStringStack.pop();
3147 // // scanner.encapsedStringStack.pop();
3148 // int tempToken = TokenNameSTRING;
3149 // if (!scanner.encapsedStringStack.isEmpty()
3150 // && (token == TokenNameEncapsedString0
3151 // || token == TokenNameEncapsedString1
3152 // || token == TokenNameEncapsedString2 || token ==
3153 // TokenNameERROR)) {
3154 // char encapsedChar = ((Character)
3155 // scanner.encapsedStringStack.peek())
3158 // case TokenNameEncapsedString0 :
3159 // if (encapsedChar == '`') {
3160 // tempToken = TokenNameEncapsedString0;
3163 // case TokenNameEncapsedString1 :
3164 // if (encapsedChar == '\'') {
3165 // tempToken = TokenNameEncapsedString1;
3168 // case TokenNameEncapsedString2 :
3169 // if (encapsedChar == '"') {
3170 // tempToken = TokenNameEncapsedString2;
3173 // case TokenNameERROR :
3174 // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3175 // scanner.currentPosition--;
3181 // token = tempToken;
3184 case TokenNameDOLLAR_LBRACE:
3186 if (token == TokenNameDOLLAR_LBRACE) {
3188 } else if (token == TokenNameIdentifier) {
3190 if (token == TokenNameLBRACKET) {
3192 // if (token == TokenNameRBRACKET) {
3196 if (token != TokenNameRBRACKET) {
3197 throwSyntaxError("']' expected after '${'.");
3205 if (token != TokenNameRBRACE) {
3206 throwSyntaxError("'}' expected.");
3210 case TokenNameLBRACE_DOLLAR:
3212 if (token == TokenNameLBRACE_DOLLAR) {
3214 } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3216 if (token == TokenNameLBRACKET) {
3218 // if (token == TokenNameRBRACKET) {
3222 if (token != TokenNameRBRACKET) {
3223 throwSyntaxError("']' expected.");
3227 } else if (token == TokenNameMINUS_GREATER) {
3229 if (token != TokenNameIdentifier && token != TokenNameVariable) {
3230 throwSyntaxError("String or Variable token expected.");
3233 if (token == TokenNameLBRACKET) {
3235 // if (token == TokenNameRBRACKET) {
3239 if (token != TokenNameRBRACKET) {
3240 throwSyntaxError("']' expected after '${'.");
3246 // if (token != TokenNameRBRACE) {
3247 // throwSyntaxError("'}' expected after '{$'.");
3249 // // scanner.encapsedStringStack.pop();
3253 if (token != TokenNameRBRACE) {
3254 throwSyntaxError("'}' expected.");
3256 // scanner.encapsedStringStack.pop();
3263 private void encaps_var_offset() {
3268 case TokenNameSTRING:
3271 case TokenNameIntegerLiteral:
3274 case TokenNameVariable:
3277 case TokenNameIdentifier:
3281 throwSyntaxError("Variable or String token expected.");
3286 private void internal_functions_in_yacc() {
3288 ImportReference impt = null;
3290 case TokenNameisset:
3291 // T_ISSET '(' isset_variables ')'
3293 if (token != TokenNameLPAREN) {
3294 throwSyntaxError("'(' expected after keyword 'isset'");
3298 if (token != TokenNameRPAREN) {
3299 throwSyntaxError("')' expected after keyword 'isset'");
3303 case TokenNameempty:
3304 // T_EMPTY '(' variable ')'
3306 if (token != TokenNameLPAREN) {
3307 throwSyntaxError("'(' expected after keyword 'empty'");
3311 if (token != TokenNameRPAREN) {
3312 throwSyntaxError("')' expected after keyword 'empty'");
3316 case TokenNameinclude:
3318 checkFileName(token, impt);
3320 case TokenNameinclude_once:
3321 // T_INCLUDE_ONCE expr
3322 checkFileName(token, impt);
3325 // T_EVAL '(' expr ')'
3327 if (token != TokenNameLPAREN) {
3328 throwSyntaxError("'(' expected after keyword 'eval'");
3332 if (token != TokenNameRPAREN) {
3333 throwSyntaxError("')' expected after keyword 'eval'");
3337 case TokenNamerequire:
3339 checkFileName(token, impt);
3341 case TokenNamerequire_once:
3342 // T_REQUIRE_ONCE expr
3343 checkFileName(token, impt);
3348 private void checkFileName(int includeToken, ImportReference impt) {
3349 //<include-token> expr
3350 int start = scanner.getCurrentTokenStartPosition();
3351 boolean hasLPAREN = false;
3353 if (token == TokenNameLPAREN) {
3357 Expression expression = expr();
3359 if (token == TokenNameRPAREN) {
3362 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3365 impt = new ImportReference(scanner.getCurrentTokenSource(start), start, scanner.getCurrentTokenEndPosition(), false);
3366 impt.declarationSourceEnd = impt.sourceEnd;
3367 impt.declarationEnd = impt.declarationSourceEnd;
3368 //endPosition is just before the ;
3369 impt.declarationSourceStart = start;
3370 includesList.add(impt);
3372 if (expression instanceof StringLiteral) {
3373 StringLiteral literal = (StringLiteral) expression;
3374 char[] includeName = literal.source();
3375 if (includeName.length == 0) {
3376 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3377 literal.sourceStart + 1);
3379 String includeNameString = new String(includeName);
3380 if (literal instanceof StringLiteralDQ) {
3381 if (includeNameString.indexOf('$') >= 0) {
3382 // assuming that the filename contains a variable => no filename check
3386 if (includeNameString.startsWith("http://")) {
3387 // assuming external include location
3390 if (scanner.compilationUnit != null) {
3391 IResource resource = scanner.compilationUnit.getResource();
3392 // java.io.File f = new java.io.File(new String(compilationUnit.getFileName()));
3393 // System.out.println(expression.toStringExpression());
3395 if (resource != null && resource instanceof IFile) {
3396 // check the filename:
3397 // System.out.println(new String(compilationUnit.getFileName())+" - "+ expression.toStringExpression());
3398 IProject project = resource.getProject();
3399 if (project != null) {
3400 IPath path = PHPFileUtil.determineFilePath(includeNameString, resource, project);
3403 // reportSyntaxError("File: " + expression.toStringExpression() + " doesn't exist in project: "
3404 // + project.getLocation().toString(), literal.sourceStart, literal.sourceEnd);
3405 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3406 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3407 compilationUnit.compilationResult);
3409 impt.setFile( project.getFile(path) );
3417 private void isset_variables() {
3419 // | isset_variables ','
3420 if (token == TokenNameRPAREN) {
3421 throwSyntaxError("Variable expected after keyword 'isset'");
3425 if (token == TokenNameCOMMA) {
3433 private boolean common_scalar() {
3437 // | T_CONSTANT_ENCAPSED_STRING
3444 case TokenNameIntegerLiteral:
3447 case TokenNameDoubleLiteral:
3450 case TokenNameStringDoubleQuote:
3453 case TokenNameStringSingleQuote:
3456 case TokenNameStringInterpolated:
3465 case TokenNameCLASS_C:
3468 case TokenNameMETHOD_C:
3471 case TokenNameFUNC_C:
3478 private void scalar() {
3481 //| T_STRING_VARNAME
3484 //| '"' encaps_list '"'
3485 //| '\'' encaps_list '\''
3486 //| T_START_HEREDOC encaps_list T_END_HEREDOC
3487 throwSyntaxError("Not yet implemented (scalar).");
3490 private void static_scalar() {
3491 // static_scalar: /* compile-time evaluated scalars */
3494 // | '+' static_scalar
3495 // | '-' static_scalar
3496 // | T_ARRAY '(' static_array_pair_list ')'
3497 // | static_class_constant
3498 if (common_scalar()) {
3502 case TokenNameIdentifier:
3504 // static_class_constant:
3505 // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3506 if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3508 if (token == TokenNameIdentifier) {
3511 throwSyntaxError("Identifier expected after '::' operator.");
3515 case TokenNameEncapsedString0:
3517 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3518 while (scanner.currentCharacter != '`') {
3519 if (scanner.currentCharacter == '\\') {
3520 scanner.currentPosition++;
3522 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3525 } catch (IndexOutOfBoundsException e) {
3526 throwSyntaxError("'`' expected at end of static string.");
3529 case TokenNameEncapsedString1:
3531 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3532 while (scanner.currentCharacter != '\'') {
3533 if (scanner.currentCharacter == '\\') {
3534 scanner.currentPosition++;
3536 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3539 } catch (IndexOutOfBoundsException e) {
3540 throwSyntaxError("'\'' expected at end of static string.");
3543 case TokenNameEncapsedString2:
3545 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3546 while (scanner.currentCharacter != '"') {
3547 if (scanner.currentCharacter == '\\') {
3548 scanner.currentPosition++;
3550 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3553 } catch (IndexOutOfBoundsException e) {
3554 throwSyntaxError("'\"' expected at end of static string.");
3561 case TokenNameMINUS:
3565 case TokenNamearray:
3567 if (token != TokenNameLPAREN) {
3568 throwSyntaxError("'(' expected after keyword 'array'");
3571 if (token == TokenNameRPAREN) {
3575 non_empty_static_array_pair_list();
3576 if (token != TokenNameRPAREN) {
3577 throwSyntaxError("')' expected after keyword 'array'");
3581 // case TokenNamenull :
3584 // case TokenNamefalse :
3587 // case TokenNametrue :
3591 throwSyntaxError("Static scalar/constant expected.");
3595 private void non_empty_static_array_pair_list() {
3596 // non_empty_static_array_pair_list:
3597 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3599 //| non_empty_static_array_pair_list ',' static_scalar
3600 //| static_scalar T_DOUBLE_ARROW static_scalar
3604 if (token == TokenNameEQUAL_GREATER) {
3608 if (token != TokenNameCOMMA) {
3612 if (token == TokenNameRPAREN) {
3618 public void reportSyntaxError() { //int act, int currentKind, int
3620 /* remember current scanner position */
3621 int startPos = scanner.startPosition;
3622 int currentPos = scanner.currentPosition;
3623 // String[] expectings;
3624 // String tokenName = name[symbol_index[currentKind]];
3625 //fetch all "accurate" possible terminals that could recover the error
3626 // int start, end = start = asi(stack[stateStackTop]);
3627 // while (asr[end] != 0)
3629 // int length = end - start;
3630 // expectings = new String[length];
3631 // if (length != 0) {
3632 // char[] indexes = new char[length];
3633 // System.arraycopy(asr, start, indexes, 0, length);
3634 // for (int i = 0; i < length; i++) {
3635 // expectings[i] = name[symbol_index[indexes[i]]];
3638 //if the pb is an EOF, try to tell the user that they are some
3639 // if (tokenName.equals(UNEXPECTED_EOF)) {
3640 // if (!this.checkAndReportBracketAnomalies(problemReporter())) {
3641 // char[] tokenSource;
3643 // tokenSource = this.scanner.getCurrentTokenSource();
3644 // } catch (Exception e) {
3645 // tokenSource = new char[] {};
3647 // problemReporter().parseError(
3648 // this.scanner.startPosition,
3649 // this.scanner.currentPosition - 1,
3654 // } else { //the next test is HEAVILY grammar DEPENDENT.
3655 // if ((length == 14)
3656 // && (expectings[0] == "=") //$NON-NLS-1$
3657 // && (expectings[1] == "*=") //$NON-NLS-1$
3658 // && (expressionPtr > -1)) {
3659 // switch(currentKind) {
3660 // case TokenNameSEMICOLON:
3661 // case TokenNamePLUS:
3662 // case TokenNameMINUS:
3663 // case TokenNameDIVIDE:
3664 // case TokenNameREMAINDER:
3665 // case TokenNameMULTIPLY:
3666 // case TokenNameLEFT_SHIFT:
3667 // case TokenNameRIGHT_SHIFT:
3668 //// case TokenNameUNSIGNED_RIGHT_SHIFT:
3669 // case TokenNameLESS:
3670 // case TokenNameGREATER:
3671 // case TokenNameLESS_EQUAL:
3672 // case TokenNameGREATER_EQUAL:
3673 // case TokenNameEQUAL_EQUAL:
3674 // case TokenNameNOT_EQUAL:
3675 // case TokenNameXOR:
3676 // case TokenNameAND:
3677 // case TokenNameOR:
3678 // case TokenNameOR_OR:
3679 // case TokenNameAND_AND:
3680 // // the ; is not the expected token ==> it ends a statement when an
3681 // expression is not ended
3682 // problemReporter().invalidExpressionAsStatement(expressionStack[expressionPtr]);
3684 // case TokenNameRBRACE :
3685 // problemReporter().missingSemiColon(expressionStack[expressionPtr]);
3688 // char[] tokenSource;
3690 // tokenSource = this.scanner.getCurrentTokenSource();
3691 // } catch (Exception e) {
3692 // tokenSource = new char[] {};
3694 // problemReporter().parseError(
3695 // this.scanner.startPosition,
3696 // this.scanner.currentPosition - 1,
3700 // this.checkAndReportBracketAnomalies(problemReporter());
3705 tokenSource = this.scanner.getCurrentTokenSource();
3706 } catch (Exception e) {
3707 tokenSource = new char[] {};
3709 // problemReporter().parseError(
3710 // this.scanner.startPosition,
3711 // this.scanner.currentPosition - 1,
3715 this.checkAndReportBracketAnomalies(problemReporter());
3718 /* reset scanner where it was */
3719 scanner.startPosition = startPos;
3720 scanner.currentPosition = currentPos;
3723 public static final int RoundBracket = 0;
3725 public static final int SquareBracket = 1;
3727 public static final int CurlyBracket = 2;
3729 public static final int BracketKinds = 3;
3731 protected int[] nestedMethod; //the ptr is nestedType
3733 protected int nestedType, dimensions;
3736 final static int AstStackIncrement = 100;
3738 protected int astPtr;
3740 protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
3742 protected int astLengthPtr;
3744 protected int[] astLengthStack;
3746 ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
3748 public CompilationUnitDeclaration compilationUnit; /*
3749 * the result from parse()
3752 protected ReferenceContext referenceContext;
3754 protected ProblemReporter problemReporter;
3756 protected CompilerOptions options;
3758 private ArrayList includesList;
3760 // protected CompilationResult compilationResult;
3762 * Returns this parser's problem reporter initialized with its reference context. Also it is assumed that a problem is going to be
3763 * reported, so initializes the compilation result's line positions.
3765 public ProblemReporter problemReporter() {
3766 if (scanner.recordLineSeparator) {
3767 compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
3769 problemReporter.referenceContext = referenceContext;
3770 return problemReporter;
3774 * Reconsider the entire source looking for inconsistencies in {} () []
3776 public boolean checkAndReportBracketAnomalies(ProblemReporter problemReporter) {
3777 scanner.wasAcr = false;
3778 boolean anomaliesDetected = false;
3780 char[] source = scanner.source;
3781 int[] leftCount = { 0, 0, 0 };
3782 int[] rightCount = { 0, 0, 0 };
3783 int[] depths = { 0, 0, 0 };
3784 int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10] };
3785 int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
3786 int[][] rightPositions = new int[][] { new int[10], new int[10], new int[10] };
3787 int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10] };
3788 scanner.currentPosition = scanner.initialPosition; //starting
3790 // (first-zero-based
3792 while (scanner.currentPosition < scanner.eofPosition) { //loop for
3797 // ---------Consume white space and handles
3798 // startPosition---------
3799 boolean isWhiteSpace;
3801 scanner.startPosition = scanner.currentPosition;
3802 // if (((scanner.currentCharacter =
3803 // source[scanner.currentPosition++]) == '\\') &&
3804 // (source[scanner.currentPosition] == 'u')) {
3805 // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
3807 if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
3808 if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
3809 // only record line positions we have not
3811 scanner.pushLineSeparator();
3814 isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
3816 } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
3817 // -------consume token until } is found---------
3818 switch (scanner.currentCharacter) {
3820 int index = leftCount[CurlyBracket]++;
3821 if (index == leftPositions[CurlyBracket].length) {
3822 System.arraycopy(leftPositions[CurlyBracket], 0, (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
3823 System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] = new int[index * 2]), 0, index);
3825 leftPositions[CurlyBracket][index] = scanner.startPosition;
3826 leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
3830 int index = rightCount[CurlyBracket]++;
3831 if (index == rightPositions[CurlyBracket].length) {
3832 System.arraycopy(rightPositions[CurlyBracket], 0, (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
3833 System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] = new int[index * 2]), 0, index);
3835 rightPositions[CurlyBracket][index] = scanner.startPosition;
3836 rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
3840 int index = leftCount[RoundBracket]++;
3841 if (index == leftPositions[RoundBracket].length) {
3842 System.arraycopy(leftPositions[RoundBracket], 0, (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
3843 System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] = new int[index * 2]), 0, index);
3845 leftPositions[RoundBracket][index] = scanner.startPosition;
3846 leftDepths[RoundBracket][index] = depths[RoundBracket]++;
3850 int index = rightCount[RoundBracket]++;
3851 if (index == rightPositions[RoundBracket].length) {
3852 System.arraycopy(rightPositions[RoundBracket], 0, (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
3853 System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] = new int[index * 2]), 0, index);
3855 rightPositions[RoundBracket][index] = scanner.startPosition;
3856 rightDepths[RoundBracket][index] = --depths[RoundBracket];
3860 int index = leftCount[SquareBracket]++;
3861 if (index == leftPositions[SquareBracket].length) {
3862 System.arraycopy(leftPositions[SquareBracket], 0, (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
3863 System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] = new int[index * 2]), 0, index);
3865 leftPositions[SquareBracket][index] = scanner.startPosition;
3866 leftDepths[SquareBracket][index] = depths[SquareBracket]++;
3870 int index = rightCount[SquareBracket]++;
3871 if (index == rightPositions[SquareBracket].length) {
3872 System.arraycopy(rightPositions[SquareBracket], 0, (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
3873 System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket] = new int[index * 2]), 0, index);
3875 rightPositions[SquareBracket][index] = scanner.startPosition;
3876 rightDepths[SquareBracket][index] = --depths[SquareBracket];
3880 if (scanner.getNextChar('\\')) {
3881 scanner.scanEscapeCharacter();
3882 } else { // consume next character
3883 scanner.unicodeAsBackSlash = false;
3884 // if (((scanner.currentCharacter =
3885 // source[scanner.currentPosition++]) ==
3887 // (source[scanner.currentPosition] ==
3889 // scanner.getNextUnicodeChar();
3891 if (scanner.withoutUnicodePtr != 0) {
3892 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3896 scanner.getNextChar('\'');
3900 // consume next character
3901 scanner.unicodeAsBackSlash = false;
3902 // if (((scanner.currentCharacter =
3903 // source[scanner.currentPosition++]) == '\\') &&
3904 // (source[scanner.currentPosition] == 'u')) {
3905 // scanner.getNextUnicodeChar();
3907 if (scanner.withoutUnicodePtr != 0) {
3908 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3911 while (scanner.currentCharacter != '"') {
3912 if (scanner.currentCharacter == '\r') {
3913 if (source[scanner.currentPosition] == '\n')
3914 scanner.currentPosition++;
3915 break; // the string cannot go further that
3918 if (scanner.currentCharacter == '\n') {
3919 break; // the string cannot go further that
3922 if (scanner.currentCharacter == '\\') {
3923 scanner.scanEscapeCharacter();
3925 // consume next character
3926 scanner.unicodeAsBackSlash = false;
3927 // if (((scanner.currentCharacter =
3928 // source[scanner.currentPosition++]) == '\\')
3929 // && (source[scanner.currentPosition] == 'u'))
3931 // scanner.getNextUnicodeChar();
3933 if (scanner.withoutUnicodePtr != 0) {
3934 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3941 if ((test = scanner.getNextChar('/', '*')) == 0) { //line
3944 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3945 && (source[scanner.currentPosition] == 'u')) {
3946 //-------------unicode traitement
3948 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3949 scanner.currentPosition++;
3950 while (source[scanner.currentPosition] == 'u') {
3951 scanner.currentPosition++;
3953 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3954 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3955 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3956 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3960 scanner.currentCharacter = 'A';
3961 } //something different from \n and \r
3963 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3966 while (scanner.currentCharacter != '\r' && scanner.currentCharacter != '\n') {
3968 scanner.startPosition = scanner.currentPosition;
3969 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3970 && (source[scanner.currentPosition] == 'u')) {
3971 //-------------unicode traitement
3973 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3974 scanner.currentPosition++;
3975 while (source[scanner.currentPosition] == 'u') {
3976 scanner.currentPosition++;
3978 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3979 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3980 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3981 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3985 scanner.currentCharacter = 'A';
3986 } //something different from \n
3989 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3993 if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
3994 if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
3995 // only record line positions we
3996 // have not recorded yet
3997 scanner.pushLineSeparator();
3998 if (this.scanner.taskTags != null) {
3999 this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner
4000 .getCurrentTokenEndPosition());
4006 if (test > 0) { //traditional and annotation
4008 boolean star = false;
4009 // consume next character
4010 scanner.unicodeAsBackSlash = false;
4011 // if (((scanner.currentCharacter =
4012 // source[scanner.currentPosition++]) ==
4014 // (source[scanner.currentPosition] ==
4016 // scanner.getNextUnicodeChar();
4018 if (scanner.withoutUnicodePtr != 0) {
4019 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4022 if (scanner.currentCharacter == '*') {
4026 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4027 && (source[scanner.currentPosition] == 'u')) {
4028 //-------------unicode traitement
4030 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4031 scanner.currentPosition++;
4032 while (source[scanner.currentPosition] == 'u') {
4033 scanner.currentPosition++;
4035 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4036 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4037 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4038 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4042 scanner.currentCharacter = 'A';
4043 } //something different from * and /
4045 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4048 //loop until end of comment */
4049 while ((scanner.currentCharacter != '/') || (!star)) {
4050 star = scanner.currentCharacter == '*';
4052 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4053 && (source[scanner.currentPosition] == 'u')) {
4054 //-------------unicode traitement
4056 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4057 scanner.currentPosition++;
4058 while (source[scanner.currentPosition] == 'u') {
4059 scanner.currentPosition++;
4061 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4062 || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4063 || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4064 || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4068 scanner.currentCharacter = 'A';
4069 } //something different from * and
4072 scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4076 if (this.scanner.taskTags != null) {
4077 this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
4084 if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4085 scanner.scanIdentifierOrKeyword(false);
4088 if (Character.isDigit(scanner.currentCharacter)) {
4089 scanner.scanNumber(false);
4093 //-----------------end switch while
4094 // try--------------------
4095 } catch (IndexOutOfBoundsException e) {
4096 break; // read until EOF
4097 } catch (InvalidInputException e) {
4098 return false; // no clue
4101 if (scanner.recordLineSeparator) {
4102 // compilationUnit.compilationResult.lineSeparatorPositions =
4103 // scanner.getLineEnds();
4105 // check placement anomalies against other kinds of brackets
4106 for (int kind = 0; kind < BracketKinds; kind++) {
4107 for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4108 int start = leftPositions[kind][leftIndex]; // deepest
4110 // find matching closing bracket
4111 int depth = leftDepths[kind][leftIndex];
4113 for (int i = 0; i < rightCount[kind]; i++) {
4114 int pos = rightPositions[kind][i];
4115 // want matching bracket further in source with same
4117 if ((pos > start) && (depth == rightDepths[kind][i])) {
4122 if (end < 0) { // did not find a good closing match
4123 problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult);
4126 // check if even number of opening/closing other brackets
4127 // in between this pair of brackets
4129 for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds); otherKind++) {
4130 for (int i = 0; i < leftCount[otherKind]; i++) {
4131 int pos = leftPositions[otherKind][i];
4132 if ((pos > start) && (pos < end))
4135 for (int i = 0; i < rightCount[otherKind]; i++) {
4136 int pos = rightPositions[otherKind][i];
4137 if ((pos > start) && (pos < end))
4141 problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult); //bracket
4147 // too many opening brackets ?
4148 for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4149 anomaliesDetected = true;
4150 problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i - 1], referenceContext,
4151 compilationUnit.compilationResult);
4153 // too many closing brackets ?
4154 for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4155 anomaliesDetected = true;
4156 problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext, compilationUnit.compilationResult);
4158 if (anomaliesDetected)
4161 return anomaliesDetected;
4162 } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4163 return anomaliesDetected;
4164 } catch (NullPointerException e) { // jdk1.2.2 jit bug
4165 return anomaliesDetected;
4169 protected void pushOnAstLengthStack(int pos) {
4171 astLengthStack[++astLengthPtr] = pos;
4172 } catch (IndexOutOfBoundsException e) {
4173 int oldStackLength = astLengthStack.length;
4174 int[] oldPos = astLengthStack;
4175 astLengthStack = new int[oldStackLength + StackIncrement];
4176 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4177 astLengthStack[astLengthPtr] = pos;
4181 protected void pushOnAstStack(ASTNode node) {
4183 * add a new obj on top of the ast stack
4186 astStack[++astPtr] = node;
4187 } catch (IndexOutOfBoundsException e) {
4188 int oldStackLength = astStack.length;
4189 ASTNode[] oldStack = astStack;
4190 astStack = new ASTNode[oldStackLength + AstStackIncrement];
4191 System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4192 astPtr = oldStackLength;
4193 astStack[astPtr] = node;
4196 astLengthStack[++astLengthPtr] = 1;
4197 } catch (IndexOutOfBoundsException e) {
4198 int oldStackLength = astLengthStack.length;
4199 int[] oldPos = astLengthStack;
4200 astLengthStack = new int[oldStackLength + AstStackIncrement];
4201 System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4202 astLengthStack[astLengthPtr] = 1;
4206 protected void resetModifiers() {
4207 this.modifiers = AccDefault;
4208 this.modifiersSourceStart = -1; // <-- see comment into
4209 // modifiersFlag(int)
4210 this.scanner.commentPtr = -1;