Bug #1248155: avoid NPE in parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / Parser.java
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
5  * 
6  * Contributors: www.phpeclipse.de
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpdt.internal.compiler.parser;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13
14 import net.sourceforge.phpdt.core.compiler.CharOperation;
15 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
16 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
17 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
18 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
19 import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
21 import net.sourceforge.phpdt.internal.compiler.problem.ProblemReporter;
22 import net.sourceforge.phpdt.internal.compiler.problem.ProblemSeverities;
23 import net.sourceforge.phpdt.internal.compiler.util.Util;
24 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
25 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
26 import net.sourceforge.phpeclipse.internal.compiler.ast.AND_AND_Expression;
27 import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
28 import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
29 import net.sourceforge.phpeclipse.internal.compiler.ast.BinaryExpression;
30 import net.sourceforge.phpeclipse.internal.compiler.ast.Block;
31 import net.sourceforge.phpeclipse.internal.compiler.ast.BreakStatement;
32 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
33 import net.sourceforge.phpeclipse.internal.compiler.ast.ConditionalExpression;
34 import net.sourceforge.phpeclipse.internal.compiler.ast.ContinueStatement;
35 import net.sourceforge.phpeclipse.internal.compiler.ast.EqualExpression;
36 import net.sourceforge.phpeclipse.internal.compiler.ast.Expression;
37 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
38 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldReference;
39 import net.sourceforge.phpeclipse.internal.compiler.ast.IfStatement;
40 import net.sourceforge.phpeclipse.internal.compiler.ast.ImportReference;
41 import net.sourceforge.phpeclipse.internal.compiler.ast.InstanceOfExpression;
42 import net.sourceforge.phpeclipse.internal.compiler.ast.MethodDeclaration;
43 import net.sourceforge.phpeclipse.internal.compiler.ast.OR_OR_Expression;
44 import net.sourceforge.phpeclipse.internal.compiler.ast.OperatorIds;
45 import net.sourceforge.phpeclipse.internal.compiler.ast.ReturnStatement;
46 import net.sourceforge.phpeclipse.internal.compiler.ast.SingleTypeReference;
47 import net.sourceforge.phpeclipse.internal.compiler.ast.Statement;
48 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteral;
49 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralDQ;
50 import net.sourceforge.phpeclipse.internal.compiler.ast.StringLiteralSQ;
51 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
52 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeReference;
53 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
54
55 import org.eclipse.core.resources.IFile;
56 import org.eclipse.core.resources.IProject;
57 import org.eclipse.core.resources.IResource;
58 import org.eclipse.core.runtime.IPath;
59
60 public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicInformation {
61         protected final static int StackIncrement = 255;
62
63         protected int stateStackTop;
64
65         // protected int[] stack = new int[StackIncrement];
66
67         public int firstToken; // handle for multiple parsing goals
68
69         public int lastAct; // handle for multiple parsing goals
70
71         // protected RecoveredElement currentElement;
72
73         public static boolean VERBOSE_RECOVERY = false;
74
75         protected boolean diet = false; // tells the scanner to jump over some
76
77         /**
78          * the PHP token scanner
79          */
80         public Scanner scanner;
81
82         int token;
83
84         protected int modifiers;
85
86         protected int modifiersSourceStart;
87
88         protected Parser(ProblemReporter problemReporter) {
89                 this.problemReporter = problemReporter;
90                 this.options = problemReporter.options;
91                 this.token = TokenNameEOF;
92                 this.initializeScanner();
93         }
94
95         public void setFileToParse(IFile fileToParse) {
96                 this.token = TokenNameEOF;
97                 this.initializeScanner();
98         }
99
100         /**
101          * ClassDeclaration Constructor.
102          * 
103          * @param s
104          * @param sess
105          *          Description of Parameter
106          * @see
107          */
108         public Parser(IFile fileToParse) {
109                 // if (keywordMap == null) {
110                 // keywordMap = new HashMap();
111                 // for (int i = 0; i < PHP_KEYWORS.length; i++) {
112                 // keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
113                 // }
114                 // }
115                 // this.currentPHPString = 0;
116                 // PHPParserSuperclass.fileToParse = fileToParse;
117                 // this.phpList = null;
118                 this.includesList = null;
119                 // this.str = "";
120                 this.token = TokenNameEOF;
121                 // this.chIndx = 0;
122                 // this.rowCount = 1;
123                 // this.columnCount = 0;
124                 // this.phpEnd = false;
125                 // getNextToken();
126                 this.initializeScanner();
127         }
128
129         public void initializeScanner() {
130                 this.scanner = new Scanner(false /* comment */, false /* whitespace */, this.options
131                                 .getSeverity(CompilerOptions.NonExternalizedString) != ProblemSeverities.Ignore /* nls */, false, false,
132                                 this.options.taskTags/* taskTags */, this.options.taskPriorites/* taskPriorities */, true/* isTaskCaseSensitive */);
133         }
134
135         /**
136          * Create marker for the parse error
137          */
138         // private void setMarker(String message, int charStart, int charEnd, int
139         // errorLevel) {
140         // setMarker(fileToParse, message, charStart, charEnd, errorLevel);
141         // }
142         /**
143          * This method will throw the SyntaxError. It will add the good lines and
144          * columns to the Error
145          * 
146          * @param error
147          *          the error message
148          * @throws SyntaxError
149          *           the error raised
150          */
151         private void throwSyntaxError(String error) {
152                 int problemStartPosition = scanner.getCurrentTokenStartPosition();
153                 int problemEndPosition = scanner.getCurrentTokenEndPosition() + 1;
154                 if (scanner.source.length <= problemEndPosition && problemEndPosition > 0) {
155                         problemEndPosition = scanner.source.length - 1;
156                         if (problemStartPosition > 0 && problemStartPosition >= problemEndPosition && problemEndPosition > 0) {
157                                 problemStartPosition = problemEndPosition - 1;
158                         }
159                 }
160                 throwSyntaxError(error, problemStartPosition, problemEndPosition);
161         }
162
163         /**
164          * This method will throw the SyntaxError. It will add the good lines and
165          * columns to the Error
166          * 
167          * @param error
168          *          the error message
169          * @throws SyntaxError
170          *           the error raised
171          */
172         // private void throwSyntaxError(String error, int startRow) {
173         // throw new SyntaxError(startRow, 0, " ", error);
174         // }
175         private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
176                 if (referenceContext != null) {
177                         problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
178                                         compilationUnit.compilationResult);
179                 }
180                 throw new SyntaxError(1, 0, " ", error);
181         }
182
183         private void reportSyntaxError(String error) {
184                 int problemStartPosition = scanner.getCurrentTokenStartPosition();
185                 int problemEndPosition = scanner.getCurrentTokenEndPosition();
186                 reportSyntaxError(error, problemStartPosition, problemEndPosition + 1);
187         }
188
189         private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
190                 if (referenceContext != null) {
191                         problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
192                                         compilationUnit.compilationResult);
193                 }
194         }
195
196         // private void reportSyntaxWarning(String error, int problemStartPosition,
197         // int problemEndPosition) {
198         // if (referenceContext != null) {
199         // problemReporter.phpParsingWarning(new String[] { error },
200         // problemStartPosition, problemEndPosition, referenceContext,
201         // compilationUnit.compilationResult);
202         // }
203         // }
204
205         /**
206          * gets the next token from input
207          */
208         private void getNextToken() {
209                 try {
210                         token = scanner.getNextToken();
211                         if (Scanner.DEBUG) {
212                                 int currentEndPosition = scanner.getCurrentTokenEndPosition();
213                                 int currentStartPosition = scanner.getCurrentTokenStartPosition();
214                                 System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
215                                 System.out.println(scanner.toStringAction(token));
216                         }
217                 } catch (InvalidInputException e) {
218                         token = TokenNameERROR;
219                         String detailedMessage = e.getMessage();
220
221                         if (detailedMessage == Scanner.UNTERMINATED_STRING) {
222                                 throwSyntaxError("Unterminated string.");
223                         } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
224                                 throwSyntaxError("Unterminated commment.");
225                         }
226                 }
227                 return;
228         }
229
230         public void init(String s) {
231                 // this.str = s;
232                 this.token = TokenNameEOF;
233                 this.includesList = new ArrayList();
234                 // this.chIndx = 0;
235                 // this.rowCount = 1;
236                 // this.columnCount = 0;
237                 // this.phpEnd = false;
238                 // this.phpMode = false;
239                 /* scanner initialization */
240                 scanner.setSource(s.toCharArray());
241                 scanner.setPHPMode(false);
242                 astPtr = 0;
243         }
244
245         protected void initialize(boolean phpMode) {
246                 initialize(phpMode, null);
247         }
248
249         protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
250                 compilationUnit = null;
251                 referenceContext = null;
252                 this.includesList = new ArrayList();
253                 // this.indexManager = indexManager;
254                 // this.str = "";
255                 this.token = TokenNameEOF;
256                 // this.chIndx = 0;
257                 // this.rowCount = 1;
258                 // this.columnCount = 0;
259                 // this.phpEnd = false;
260                 // this.phpMode = phpMode;
261                 scanner.setPHPMode(phpMode);
262                 astPtr = 0;
263         }
264
265         /**
266          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
267          * &lt;/body&gt;'
268          */
269         public void parse(String s) {
270                 parse(s, null);
271         }
272
273         /**
274          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
275          * &lt;/body&gt;'
276          */
277         public void parse(String s, HashMap variables) {
278                 fMethodVariables = variables;
279                 fStackUnassigned = new ArrayList();
280                 init(s);
281                 parse();
282         }
283
284         /**
285          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
286          * &lt;/body&gt;'
287          */
288         protected void parse() {
289                 if (scanner.compilationUnit != null) {
290                         IResource resource = scanner.compilationUnit.getResource();
291                         if (resource != null && resource instanceof IFile) {
292                                 // set the package name
293                                 consumePackageDeclarationName((IFile) resource);
294                         }
295                 }
296                 getNextToken();
297                 do {
298                         try {
299                                 if (token != TokenNameEOF && token != TokenNameERROR) {
300                                         statementList();
301                                 }
302                                 if (token != TokenNameEOF) {
303                                         if (token == TokenNameERROR) {
304                                                 throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
305                                         }
306                                         if (token == TokenNameRPAREN) {
307                                                 throwSyntaxError("Too many closing ')'; end-of-file not reached.");
308                                         }
309                                         if (token == TokenNameRBRACE) {
310                                                 throwSyntaxError("Too many closing '}'; end-of-file not reached.");
311                                         }
312                                         if (token == TokenNameRBRACKET) {
313                                                 throwSyntaxError("Too many closing ']'; end-of-file not reached.");
314                                         }
315                                         if (token == TokenNameLPAREN) {
316                                                 throwSyntaxError("Read character '('; end-of-file not reached.");
317                                         }
318                                         if (token == TokenNameLBRACE) {
319                                                 throwSyntaxError("Read character '{';  end-of-file not reached.");
320                                         }
321                                         if (token == TokenNameLBRACKET) {
322                                                 throwSyntaxError("Read character '[';  end-of-file not reached.");
323                                         }
324                                         throwSyntaxError("End-of-file not reached.");
325                                 }
326                                 break;
327                         } catch (SyntaxError syntaxError) {
328                                 // syntaxError.printStackTrace();
329                                 break;
330                         }
331                 } while (true);
332
333                 endParse(0);
334         }
335
336         /**
337          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
338          * &lt;/body&gt;'
339          */
340         public void parseFunction(String s, HashMap variables) {
341                 init(s);
342                 scanner.phpMode = true;
343                 parseFunction(variables);
344         }
345
346         /**
347          * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt;
348          * &lt;/body&gt;'
349          */
350         protected void parseFunction(HashMap variables) {
351                 getNextToken();
352                 boolean hasModifiers = member_modifiers();
353                 if (token == TokenNamefunction) {
354                         if (!hasModifiers) {
355                                 checkAndSetModifiers(AccPublic);
356                         }
357                         this.fMethodVariables = variables;
358
359                         MethodDeclaration methodDecl = new MethodDeclaration(null);
360                         methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
361                         methodDecl.modifiers = this.modifiers;
362                         methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
363                         try {
364                                 getNextToken();
365                                 functionDefinition(methodDecl);
366                         } catch (SyntaxError sytaxErr1) {
367                                 return;
368                         } finally {
369                                 int sourceEnd = methodDecl.sourceEnd;
370                                 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
371                                         sourceEnd = methodDecl.declarationSourceStart + 1;
372                                 }
373                                 methodDecl.sourceEnd = sourceEnd;
374                                 methodDecl.declarationSourceEnd = sourceEnd;
375                         }
376                 }
377         }
378
379         protected CompilationUnitDeclaration endParse(int act) {
380
381                 this.lastAct = act;
382
383                 // if (currentElement != null) {
384                 // currentElement.topElement().updateParseTree();
385                 // if (VERBOSE_RECOVERY) {
386                 // System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
387                 // System.out.println("--------------------------"); //$NON-NLS-1$
388                 // System.out.println(compilationUnit);
389                 // System.out.println("----------------------------------"); //$NON-NLS-1$
390                 // }
391                 // } else {
392                 if (diet & VERBOSE_RECOVERY) {
393                         System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
394                         System.out.println("--------------------------"); //$NON-NLS-1$
395                         System.out.println(compilationUnit);
396                         System.out.println("----------------------------------"); //$NON-NLS-1$
397                 }
398                 // }
399                 if (scanner.recordLineSeparator) {
400                         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
401                 }
402                 if (scanner.taskTags != null) {
403                         for (int i = 0; i < scanner.foundTaskCount; i++) {
404                                 problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
405                                                 scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
406                                                 scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
407                         }
408                 }
409                 compilationUnit.imports = new ImportReference[includesList.size()];
410                 for (int i = 0; i < includesList.size(); i++) {
411                         compilationUnit.imports[i] = (ImportReference) includesList.get(i);
412                 }
413                 return compilationUnit;
414         }
415
416         private Block statementList() {
417                 boolean branchStatement = false;
418                 Statement statement;
419                 int blockStart = scanner.getCurrentTokenStartPosition();
420                 ArrayList blockStatements = new ArrayList();
421                 do {
422                         try {
423                                 statement = statement();
424                                 blockStatements.add(statement);
425                                 if (branchStatement && statement != null) {
426                                         // reportSyntaxError("Unreachable code", statement.sourceStart,
427                                         // statement.sourceEnd);
428                                         problemReporter.unreachableCode(new String(scanner.getCurrentIdentifierSource()), statement.sourceStart,
429                                                         statement.sourceEnd, referenceContext, compilationUnit.compilationResult);
430                                 }
431                                 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
432                                                 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
433                                                 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
434                                                 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
435                                         return createBlock(blockStart, blockStatements);
436                                 }
437                                 branchStatement = checkUnreachableStatements(statement);
438                         } catch (SyntaxError sytaxErr1) {
439                                 // if an error occured,
440                                 // try to find keywords
441                                 // to parse the rest of the string
442                                 boolean tokenize = scanner.tokenizeStrings;
443                                 if (!tokenize) {
444                                         scanner.tokenizeStrings = true;
445                                 }
446                                 try {
447                                         while (token != TokenNameEOF) {
448                                                 if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
449                                                                 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
450                                                                 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
451                                                                 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
452                                                         return createBlock(blockStart, blockStatements);
453                                                 }
454                                                 if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
455                                                                 || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
456                                                                 || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameglobal
457                                                                 || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction || token == TokenNamedeclare
458                                                                 || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow || token == TokenNamefinal
459                                                                 || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
460                                                         break;
461                                                 }
462                                                 // System.out.println(scanner.toStringAction(token));
463                                                 getNextToken();
464                                                 // System.out.println(scanner.toStringAction(token));
465                                         }
466                                         if (token == TokenNameEOF) {
467                                                 throw sytaxErr1;
468                                         }
469                                 } finally {
470                                         scanner.tokenizeStrings = tokenize;
471                                 }
472                         }
473                 } while (true);
474         }
475
476         /**
477          * @param statement
478          * @return
479          */
480         private boolean checkUnreachableStatements(Statement statement) {
481                 if (statement instanceof ReturnStatement || statement instanceof ContinueStatement || statement instanceof BreakStatement) {
482                         return true;
483                 } else if (statement instanceof IfStatement && ((IfStatement) statement).checkUnreachable) {
484                         return true;
485                 }
486                 return false;
487         }
488
489         /**
490          * @param blockStart
491          * @param blockStatements
492          * @return
493          */
494         private Block createBlock(int blockStart, ArrayList blockStatements) {
495                 int blockEnd = scanner.getCurrentTokenEndPosition();
496                 Block b = Block.EmptyWith(blockStart, blockEnd);
497                 b.statements = new Statement[blockStatements.size()];
498                 blockStatements.toArray(b.statements);
499                 return b;
500         }
501
502         private void functionBody(MethodDeclaration methodDecl) {
503                 // '{' [statement-list] '}'
504                 if (token == TokenNameLBRACE) {
505                         getNextToken();
506                 } else {
507                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
508                         throwSyntaxError("'{' expected in compound-statement.");
509                 }
510                 if (token != TokenNameRBRACE) {
511                         statementList();
512                 }
513                 if (token == TokenNameRBRACE) {
514                         methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
515                         getNextToken();
516                 } else {
517                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
518                         throwSyntaxError("'}' expected in compound-statement.");
519                 }
520         }
521
522         private Statement statement() {
523                 Statement statement = null;
524                 Expression expression;
525                 int sourceStart = scanner.getCurrentTokenStartPosition();
526                 int sourceEnd;
527                 if (token == TokenNameif) {
528                         // T_IF '(' expr ')' statement elseif_list else_single
529                         // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
530                         // new_else_single T_ENDIF ';'
531                         getNextToken();
532                         if (token == TokenNameLPAREN) {
533                                 getNextToken();
534                         } else {
535                                 throwSyntaxError("'(' expected after 'if' keyword.");
536                         }
537                         expression = expr();
538                         if (token == TokenNameRPAREN) {
539                                 getNextToken();
540                         } else {
541                                 throwSyntaxError("')' expected after 'if' condition.");
542                         }
543                         // create basic IfStatement
544                         IfStatement ifStatement = new IfStatement(expression, null, null, sourceStart, -1);
545                         if (token == TokenNameCOLON) {
546                                 getNextToken();
547                                 ifStatementColon(ifStatement);
548                         } else {
549                                 ifStatement(ifStatement);
550                         }
551                         return ifStatement;
552                 } else if (token == TokenNameswitch) {
553                         getNextToken();
554                         if (token == TokenNameLPAREN) {
555                                 getNextToken();
556                         } else {
557                                 throwSyntaxError("'(' expected after 'switch' keyword.");
558                         }
559                         expr();
560                         if (token == TokenNameRPAREN) {
561                                 getNextToken();
562                         } else {
563                                 throwSyntaxError("')' expected after 'switch' condition.");
564                         }
565                         switchStatement();
566                         return statement;
567                 } else if (token == TokenNamefor) {
568                         getNextToken();
569                         if (token == TokenNameLPAREN) {
570                                 getNextToken();
571                         } else {
572                                 throwSyntaxError("'(' expected after 'for' keyword.");
573                         }
574                         if (token == TokenNameSEMICOLON) {
575                                 getNextToken();
576                         } else {
577                                 expressionList();
578                                 if (token == TokenNameSEMICOLON) {
579                                         getNextToken();
580                                 } else {
581                                         throwSyntaxError("';' expected after 'for'.");
582                                 }
583                         }
584                         if (token == TokenNameSEMICOLON) {
585                                 getNextToken();
586                         } else {
587                                 expressionList();
588                                 if (token == TokenNameSEMICOLON) {
589                                         getNextToken();
590                                 } else {
591                                         throwSyntaxError("';' expected after 'for'.");
592                                 }
593                         }
594                         if (token == TokenNameRPAREN) {
595                                 getNextToken();
596                         } else {
597                                 expressionList();
598                                 if (token == TokenNameRPAREN) {
599                                         getNextToken();
600                                 } else {
601                                         throwSyntaxError("')' expected after 'for'.");
602                                 }
603                         }
604                         forStatement();
605                         return statement;
606                 } else if (token == TokenNamewhile) {
607                         getNextToken();
608                         if (token == TokenNameLPAREN) {
609                                 getNextToken();
610                         } else {
611                                 throwSyntaxError("'(' expected after 'while' keyword.");
612                         }
613                         expr();
614                         if (token == TokenNameRPAREN) {
615                                 getNextToken();
616                         } else {
617                                 throwSyntaxError("')' expected after 'while' condition.");
618                         }
619                         whileStatement();
620                         return statement;
621                 } else if (token == TokenNamedo) {
622                         getNextToken();
623                         if (token == TokenNameLBRACE) {
624                                 getNextToken();
625                                 if (token != TokenNameRBRACE) {
626                                         statementList();
627                                 }
628                                 if (token == TokenNameRBRACE) {
629                                         getNextToken();
630                                 } else {
631                                         throwSyntaxError("'}' expected after 'do' keyword.");
632                                 }
633                         } else {
634                                 statement();
635                         }
636                         if (token == TokenNamewhile) {
637                                 getNextToken();
638                                 if (token == TokenNameLPAREN) {
639                                         getNextToken();
640                                 } else {
641                                         throwSyntaxError("'(' expected after 'while' keyword.");
642                                 }
643                                 expr();
644                                 if (token == TokenNameRPAREN) {
645                                         getNextToken();
646                                 } else {
647                                         throwSyntaxError("')' expected after 'while' condition.");
648                                 }
649                         } else {
650                                 throwSyntaxError("'while' expected after 'do' keyword.");
651                         }
652                         if (token == TokenNameSEMICOLON) {
653                                 getNextToken();
654                         } else {
655                                 if (token != TokenNameINLINE_HTML) {
656                                         throwSyntaxError("';' expected after do-while statement.");
657                                 }
658                                 getNextToken();
659                         }
660                         return statement;
661                 } else if (token == TokenNameforeach) {
662                         getNextToken();
663                         if (token == TokenNameLPAREN) {
664                                 getNextToken();
665                         } else {
666                                 throwSyntaxError("'(' expected after 'foreach' keyword.");
667                         }
668                         expr();
669                         if (token == TokenNameas) {
670                                 getNextToken();
671                         } else {
672                                 throwSyntaxError("'as' expected after 'foreach' exxpression.");
673                         }
674                         // variable();
675                         foreach_variable();
676                         foreach_optional_arg();
677                         if (token == TokenNameEQUAL_GREATER) {
678                                 getNextToken();
679                                 variable(false, false);
680                         }
681                         if (token == TokenNameRPAREN) {
682                                 getNextToken();
683                         } else {
684                                 throwSyntaxError("')' expected after 'foreach' expression.");
685                         }
686                         foreachStatement();
687                         return statement;
688                 } else if (token == TokenNamebreak) {
689                         expression = null;
690                         getNextToken();
691                         if (token != TokenNameSEMICOLON) {
692                                 expression = expr();
693                         }
694                         if (token == TokenNameSEMICOLON) {
695                                 sourceEnd = scanner.getCurrentTokenEndPosition();
696                                 getNextToken();
697                         } else {
698                                 if (token != TokenNameINLINE_HTML) {
699                                         throwSyntaxError("';' expected after 'break'.");
700                                 }
701                                 sourceEnd = scanner.getCurrentTokenEndPosition();
702                                 getNextToken();
703                         }
704                         return new BreakStatement(null, sourceStart, sourceEnd);
705                 } else if (token == TokenNamecontinue) {
706                         expression = null;
707                         getNextToken();
708                         if (token != TokenNameSEMICOLON) {
709                                 expression = expr();
710                         }
711                         if (token == TokenNameSEMICOLON) {
712                                 sourceEnd = scanner.getCurrentTokenEndPosition();
713                                 getNextToken();
714                         } else {
715                                 if (token != TokenNameINLINE_HTML) {
716                                         throwSyntaxError("';' expected after 'continue'.");
717                                 }
718                                 sourceEnd = scanner.getCurrentTokenEndPosition();
719                                 getNextToken();
720                         }
721                         return new ContinueStatement(null, sourceStart, sourceEnd);
722                 } else if (token == TokenNamereturn) {
723                         expression = null;
724                         getNextToken();
725                         if (token != TokenNameSEMICOLON) {
726                                 expression = expr();
727                         }
728                         if (token == TokenNameSEMICOLON) {
729                                 sourceEnd = scanner.getCurrentTokenEndPosition();
730                                 getNextToken();
731                         } else {
732                                 if (token != TokenNameINLINE_HTML) {
733                                         throwSyntaxError("';' expected after 'return'.");
734                                 }
735                                 sourceEnd = scanner.getCurrentTokenEndPosition();
736                                 getNextToken();
737                         }
738                         return new ReturnStatement(expression, sourceStart, sourceEnd);
739                 } else if (token == TokenNameecho) {
740                         getNextToken();
741                         expressionList();
742                         if (token == TokenNameSEMICOLON) {
743                                 getNextToken();
744                         } else {
745                                 if (token != TokenNameINLINE_HTML) {
746                                         throwSyntaxError("';' expected after 'echo' statement.");
747                                 }
748                                 getNextToken();
749                         }
750                         return statement;
751                 } else if (token == TokenNameINLINE_HTML) {
752                         if (scanner.phpExpressionTag) {
753                                 // start of <?= ... ?> block
754                                 getNextToken();
755                                 expr();
756                                 if (token == TokenNameSEMICOLON) {
757                                         getNextToken();
758                                 }
759                                 if (token != TokenNameINLINE_HTML) {
760                                         throwSyntaxError("Missing '?>' for open PHP expression block ('<?=').");
761                                 }
762                         } else {
763                                 getNextToken();
764                         }
765                         return statement;
766                         // } else if (token == TokenNameprint) {
767                         // getNextToken();
768                         // expression();
769                         // if (token == TokenNameSEMICOLON) {
770                         // getNextToken();
771                         // } else {
772                         // if (token != TokenNameStopPHP) {
773                         // throwSyntaxError("';' expected after 'print' statement.");
774                         // }
775                         // getNextToken();
776                         // }
777                         // return;
778                 } else if (token == TokenNameglobal) {
779                         getNextToken();
780                         global_var_list();
781                         if (token == TokenNameSEMICOLON) {
782                                 getNextToken();
783                         } else {
784                                 if (token != TokenNameINLINE_HTML) {
785                                         throwSyntaxError("';' expected after 'global' statement.");
786                                 }
787                                 getNextToken();
788                         }
789                         return statement;
790                 } else if (token == TokenNamestatic) {
791                         getNextToken();
792                         static_var_list();
793                         if (token == TokenNameSEMICOLON) {
794                                 getNextToken();
795                         } else {
796                                 if (token != TokenNameINLINE_HTML) {
797                                         throwSyntaxError("';' expected after 'static' statement.");
798                                 }
799                                 getNextToken();
800                         }
801                         return statement;
802                 } else if (token == TokenNameunset) {
803                         getNextToken();
804                         if (token == TokenNameLPAREN) {
805                                 getNextToken();
806                         } else {
807                                 throwSyntaxError("'(' expected after 'unset' statement.");
808                         }
809                         unset_variables();
810                         if (token == TokenNameRPAREN) {
811                                 getNextToken();
812                         } else {
813                                 throwSyntaxError("')' expected after 'unset' statement.");
814                         }
815                         if (token == TokenNameSEMICOLON) {
816                                 getNextToken();
817                         } else {
818                                 if (token != TokenNameINLINE_HTML) {
819                                         throwSyntaxError("';' expected after 'unset' statement.");
820                                 }
821                                 getNextToken();
822                         }
823                         return statement;
824                 } else if (token == TokenNamefunction) {
825                         MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
826                         methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
827                         methodDecl.modifiers = AccDefault;
828                         methodDecl.type = MethodDeclaration.FUNCTION_DEFINITION;
829                         try {
830                                 getNextToken();
831                                 functionDefinition(methodDecl);
832                         } finally {
833                                 sourceEnd = methodDecl.sourceEnd;
834                                 if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
835                                         sourceEnd = methodDecl.declarationSourceStart + 1;
836                                 }
837                                 methodDecl.declarationSourceEnd = sourceEnd;
838                                 methodDecl.sourceEnd = sourceEnd;
839                         }
840                         return statement;
841                 } else if (token == TokenNamedeclare) {
842                         // T_DECLARE '(' declare_list ')' declare_statement
843                         getNextToken();
844                         if (token != TokenNameLPAREN) {
845                                 throwSyntaxError("'(' expected in 'declare' statement.");
846                         }
847                         getNextToken();
848                         declare_list();
849                         if (token != TokenNameRPAREN) {
850                                 throwSyntaxError("')' expected in 'declare' statement.");
851                         }
852                         getNextToken();
853                         declare_statement();
854                         return statement;
855                 } else if (token == TokenNametry) {
856                         getNextToken();
857                         if (token != TokenNameLBRACE) {
858                                 throwSyntaxError("'{' expected in 'try' statement.");
859                         }
860                         getNextToken();
861                         statementList();
862                         if (token != TokenNameRBRACE) {
863                                 throwSyntaxError("'}' expected in 'try' statement.");
864                         }
865                         getNextToken();
866                         return statement;
867                 } else if (token == TokenNamecatch) {
868                         getNextToken();
869                         if (token != TokenNameLPAREN) {
870                                 throwSyntaxError("'(' expected in 'catch' statement.");
871                         }
872                         getNextToken();
873                         fully_qualified_class_name();
874                         if (token != TokenNameVariable) {
875                                 throwSyntaxError("Variable expected in 'catch' statement.");
876                         }
877                         addVariableSet();
878                         getNextToken();
879                         if (token != TokenNameRPAREN) {
880                                 throwSyntaxError("')' expected in 'catch' statement.");
881                         }
882                         getNextToken();
883                         if (token != TokenNameLBRACE) {
884                                 throwSyntaxError("'{' expected in 'catch' statement.");
885                         }
886                         getNextToken();
887                         if (token != TokenNameRBRACE) {
888                                 statementList();
889                                 if (token != TokenNameRBRACE) {
890                                         throwSyntaxError("'}' expected in 'catch' statement.");
891                                 }
892                         }
893                         getNextToken();
894                         additional_catches();
895                         return statement;
896                 } else if (token == TokenNamethrow) {
897                         getNextToken();
898                         expr();
899                         if (token == TokenNameSEMICOLON) {
900                                 getNextToken();
901                         } else {
902                                 throwSyntaxError("';' expected after 'throw' exxpression.");
903                         }
904                         return statement;
905                 } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
906                         try {
907                                 TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
908                                 typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
909                                 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
910                                 typeDecl.name = new char[] { ' ' };
911                                 // default super class
912                                 typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
913                                 compilationUnit.types.add(typeDecl);
914                                 pushOnAstStack(typeDecl);
915                                 unticked_class_declaration_statement(typeDecl);
916                         } finally {
917                                 // reduce stack:
918                                 astPtr--;
919                                 astLengthPtr--;
920                         }
921                         return statement;
922                         // } else {
923                         // throwSyntaxError("Unexpected keyword '" + keyword + "'");
924                 } else if (token == TokenNameLBRACE) {
925                         getNextToken();
926                         if (token != TokenNameRBRACE) {
927                                 statement = statementList();
928                         }
929                         if (token == TokenNameRBRACE) {
930                                 getNextToken();
931                                 return statement;
932                         } else {
933                                 throwSyntaxError("'}' expected.");
934                         }
935                 } else {
936                         if (token != TokenNameSEMICOLON) {
937                                 expr();
938                         }
939                         if (token == TokenNameSEMICOLON) {
940                                 getNextToken();
941                                 return statement;
942                         } else {
943                                 if (token == TokenNameRBRACE) {
944                                         reportSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
945                                 } else {
946                                         if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
947                                                 throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
948                                         }
949                                         getNextToken();
950                                 }
951                         }
952                 }
953                 // may be null
954                 return statement;
955         }
956
957         private void declare_statement() {
958                 // statement
959                 // | ':' inner_statement_list T_ENDDECLARE ';'
960                 // ;
961                 if (token == TokenNameCOLON) {
962                         getNextToken();
963                         // TODO: implement inner_statement_list();
964                         statementList();
965                         if (token != TokenNameenddeclare) {
966                                 throwSyntaxError("'enddeclare' expected in 'declare' statement.");
967                         }
968                         getNextToken();
969                         if (token != TokenNameSEMICOLON) {
970                                 throwSyntaxError("';' expected after 'enddeclare' keyword.");
971                         }
972                         getNextToken();
973                 } else {
974                         statement();
975                 }
976         }
977
978         private void declare_list() {
979                 // T_STRING '=' static_scalar
980                 // | declare_list ',' T_STRING '=' static_scalar
981                 while (true) {
982                         if (token != TokenNameIdentifier) {
983                                 throwSyntaxError("Identifier expected in 'declare' list.");
984                         }
985                         getNextToken();
986                         if (token != TokenNameEQUAL) {
987                                 throwSyntaxError("'=' expected in 'declare' list.");
988                         }
989                         getNextToken();
990                         static_scalar();
991                         if (token != TokenNameCOMMA) {
992                                 break;
993                         }
994                         getNextToken();
995                 }
996         }
997
998         private void additional_catches() {
999                 while (token == TokenNamecatch) {
1000                         getNextToken();
1001                         if (token != TokenNameLPAREN) {
1002                                 throwSyntaxError("'(' expected in 'catch' statement.");
1003                         }
1004                         getNextToken();
1005                         fully_qualified_class_name();
1006                         if (token != TokenNameVariable) {
1007                                 throwSyntaxError("Variable expected in 'catch' statement.");
1008                         }
1009                         addVariableSet();
1010                         getNextToken();
1011                         if (token != TokenNameRPAREN) {
1012                                 throwSyntaxError("')' expected in 'catch' statement.");
1013                         }
1014                         getNextToken();
1015                         if (token != TokenNameLBRACE) {
1016                                 throwSyntaxError("'{' expected in 'catch' statement.");
1017                         }
1018                         getNextToken();
1019                         if (token != TokenNameRBRACE) {
1020                                 statementList();
1021                         }
1022                         if (token != TokenNameRBRACE) {
1023                                 throwSyntaxError("'}' expected in 'catch' statement.");
1024                         }
1025                         getNextToken();
1026                 }
1027         }
1028
1029         private void foreach_variable() {
1030                 // w_variable
1031                 // | '&' w_variable
1032                 if (token == TokenNameAND) {
1033                         getNextToken();
1034                 }
1035                 w_variable(true);
1036         }
1037
1038         private void foreach_optional_arg() {
1039                 // /* empty */
1040                 // | T_DOUBLE_ARROW foreach_variable
1041                 if (token == TokenNameEQUAL_GREATER) {
1042                         getNextToken();
1043                         foreach_variable();
1044                 }
1045         }
1046
1047         private void global_var_list() {
1048                 // global_var_list:
1049                 // global_var_list ',' global_var
1050                 // | global_var
1051                 HashSet set = peekVariableSet();
1052                 while (true) {
1053                         global_var(set);
1054                         if (token != TokenNameCOMMA) {
1055                                 break;
1056                         }
1057                         getNextToken();
1058                 }
1059         }
1060
1061         private void global_var(HashSet set) {
1062                 // global_var:
1063                 // T_VARIABLE
1064                 // | '$' r_variable
1065                 // | '$' '{' expr '}'
1066                 if (token == TokenNameVariable) {
1067                         if (fMethodVariables != null) {
1068                                 VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
1069                                 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1070                         }
1071                         addVariableSet(set);
1072                         getNextToken();
1073                 } else if (token == TokenNameDOLLAR) {
1074                         getNextToken();
1075                         if (token == TokenNameLBRACE) {
1076                                 getNextToken();
1077                                 expr();
1078                                 if (token != TokenNameRBRACE) {
1079                                         throwSyntaxError("'}' expected in global variable.");
1080                                 }
1081                                 getNextToken();
1082                         } else {
1083                                 r_variable();
1084                         }
1085                 }
1086         }
1087
1088         private void static_var_list() {
1089                 // static_var_list:
1090                 // static_var_list ',' T_VARIABLE
1091                 // | static_var_list ',' T_VARIABLE '=' static_scalar
1092                 // | T_VARIABLE
1093                 // | T_VARIABLE '=' static_scalar,
1094                 HashSet set = peekVariableSet();
1095                 while (true) {
1096                         if (token == TokenNameVariable) {
1097                                 if (fMethodVariables != null) {
1098                                         VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
1099                                         fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1100                                 }
1101                                 addVariableSet(set);
1102                                 getNextToken();
1103                                 if (token == TokenNameEQUAL) {
1104                                         getNextToken();
1105                                         static_scalar();
1106                                 }
1107                                 if (token != TokenNameCOMMA) {
1108                                         break;
1109                                 }
1110                                 getNextToken();
1111                         } else {
1112                                 break;
1113                         }
1114                 }
1115         }
1116
1117         private void unset_variables() {
1118                 // unset_variables:
1119                 // unset_variable
1120                 // | unset_variables ',' unset_variable
1121                 // unset_variable:
1122                 // variable
1123                 while (true) {
1124                         variable(false, false);
1125                         if (token != TokenNameCOMMA) {
1126                                 break;
1127                         }
1128                         getNextToken();
1129                 }
1130         }
1131
1132         private final void initializeModifiers() {
1133                 this.modifiers = 0;
1134                 this.modifiersSourceStart = -1;
1135         }
1136
1137         private final void checkAndSetModifiers(int flag) {
1138                 this.modifiers |= flag;
1139                 if (this.modifiersSourceStart < 0)
1140                         this.modifiersSourceStart = this.scanner.startPosition;
1141         }
1142
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);
1150                         getNextToken();
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 ["
1160                                         // + scanner.toStringAction(token) + "].",
1161                                         // typeDecl.sourceStart, typeDecl.sourceEnd);
1162                                 }
1163                                 getNextToken();
1164                                 interface_extends_list(typeDecl);
1165                         } else {
1166                                 typeDecl.name = new char[] { ' ' };
1167                                 throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1168                                 return;
1169                         }
1170                 } else {
1171                         // class_entry_type T_STRING extends_from
1172                         // implements_list
1173                         // '{' class_statement_list'}'
1174                         class_entry_type();
1175                         typeDecl.modifiers = this.modifiers;
1176                         typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1177                         typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1178                         // identifier
1179                         // identifier 'extends' identifier
1180                         if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1181                                 typeDecl.name = scanner.getCurrentIdentifierSource();
1182                                 if (token > TokenNameKEYWORD) {
1183                                         problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1184                                                         scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1185                                         // throwSyntaxError("Don't use a keyword for class declaration [" +
1186                                         // scanner.toStringAction(token) + "].",
1187                                         // typeDecl.sourceStart, typeDecl.sourceEnd);
1188                                 }
1189                                 getNextToken();
1190                                 // extends_from:
1191                                 // /* empty */
1192                                 // | T_EXTENDS fully_qualified_class_name
1193                                 if (token == TokenNameextends) {
1194                                         interface_extends_list(typeDecl);
1195                                         // getNextToken();
1196                                         // if (token != TokenNameIdentifier) {
1197                                         // throwSyntaxError("Class name expected after keyword
1198                                         // 'extends'.",
1199                                         // scanner.getCurrentTokenStartPosition(), scanner
1200                                         // .getCurrentTokenEndPosition());
1201                                         // }
1202                                 }
1203                                 implements_list(typeDecl);
1204                         } else {
1205                                 typeDecl.name = new char[] { ' ' };
1206                                 throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1207                                 return;
1208                         }
1209                 }
1210                 // '{' class_statement_list '}'
1211                 if (token == TokenNameLBRACE) {
1212                         getNextToken();
1213                         if (token != TokenNameRBRACE) {
1214                                 ArrayList list = new ArrayList();
1215                                 class_statement_list(list);
1216                                 typeDecl.fields = new FieldDeclaration[list.size()];
1217                                 for (int i = 0; i < list.size(); i++) {
1218                                         typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1219                                 }
1220                         }
1221                         if (token == TokenNameRBRACE) {
1222                                 typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1223                                 getNextToken();
1224                         } else {
1225                                 throwSyntaxError("'}' expected at end of class body.");
1226                         }
1227                 } else {
1228                         throwSyntaxError("'{' expected at start of class body.");
1229                 }
1230         }
1231
1232         private void class_entry_type() {
1233                 // T_CLASS
1234                 // | T_ABSTRACT T_CLASS
1235                 // | T_FINAL T_CLASS
1236                 if (token == TokenNameclass) {
1237                         getNextToken();
1238                 } else if (token == TokenNameabstract) {
1239                         checkAndSetModifiers(AccAbstract);
1240                         getNextToken();
1241                         if (token != TokenNameclass) {
1242                                 throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1243                         }
1244                         getNextToken();
1245                 } else if (token == TokenNamefinal) {
1246                         checkAndSetModifiers(AccFinal);
1247                         getNextToken();
1248                         if (token != TokenNameclass) {
1249                                 throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1250                         }
1251                         getNextToken();
1252                 } else {
1253                         throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1254                 }
1255         }
1256
1257         // private void class_extends(TypeDeclaration typeDecl) {
1258         // // /* empty */
1259         // // | T_EXTENDS interface_list
1260         // if (token == TokenNameextends) {
1261         // getNextToken();
1262         //
1263         // if (token == TokenNameIdentifier) {
1264         // getNextToken();
1265         // } else {
1266         // throwSyntaxError("Class name expected after keyword 'extends'.");
1267         // }
1268         // }
1269         // }
1270
1271         private void interface_extends_list(TypeDeclaration typeDecl) {
1272                 // /* empty */
1273                 // | T_EXTENDS interface_list
1274                 if (token == TokenNameextends) {
1275                         getNextToken();
1276                         interface_list();
1277                 }
1278         }
1279
1280         private void implements_list(TypeDeclaration typeDecl) {
1281                 // /* empty */
1282                 // | T_IMPLEMENTS interface_list
1283                 if (token == TokenNameimplements) {
1284                         getNextToken();
1285                         interface_list();
1286                 }
1287         }
1288
1289         private void interface_list() {
1290                 // interface_list:
1291                 // fully_qualified_class_name
1292                 // | interface_list ',' fully_qualified_class_name
1293                 do {
1294                         if (token == TokenNameIdentifier) {
1295                                 getNextToken();
1296                         } else {
1297                                 throwSyntaxError("Interface name expected after keyword 'implements'.");
1298                         }
1299                         if (token != TokenNameCOMMA) {
1300                                 return;
1301                         }
1302                         getNextToken();
1303                 } while (true);
1304         }
1305
1306         // private void classBody(TypeDeclaration typeDecl) {
1307         // //'{' [class-element-list] '}'
1308         // if (token == TokenNameLBRACE) {
1309         // getNextToken();
1310         // if (token != TokenNameRBRACE) {
1311         // class_statement_list();
1312         // }
1313         // if (token == TokenNameRBRACE) {
1314         // typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1315         // getNextToken();
1316         // } else {
1317         // throwSyntaxError("'}' expected at end of class body.");
1318         // }
1319         // } else {
1320         // throwSyntaxError("'{' expected at start of class body.");
1321         // }
1322         // }
1323         private void class_statement_list(ArrayList list) {
1324                 do {
1325                         try {
1326                                 class_statement(list);
1327                                 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1328                                                 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1329                                                 || token == TokenNameconst) {
1330                                         continue;
1331                                 }
1332                                 if (token == TokenNameRBRACE) {
1333                                         break;
1334                                 }
1335                                 throwSyntaxError("'}' at end of class statement.");
1336                         } catch (SyntaxError sytaxErr1) {
1337                                 boolean tokenize = scanner.tokenizeStrings;
1338                                 if (!tokenize) {
1339                                         scanner.tokenizeStrings = true;
1340                                 }
1341                                 try {
1342                                         // if an error occured,
1343                                         // try to find keywords
1344                                         // to parse the rest of the string
1345                                         while (token != TokenNameEOF) {
1346                                                 if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1347                                                                 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1348                                                                 || token == TokenNameconst) {
1349                                                         break;
1350                                                 }
1351                                                 // System.out.println(scanner.toStringAction(token));
1352                                                 getNextToken();
1353                                         }
1354                                         if (token == TokenNameEOF) {
1355                                                 throw sytaxErr1;
1356                                         }
1357                                 } finally {
1358                                         scanner.tokenizeStrings = tokenize;
1359                                 }
1360                         }
1361                 } while (true);
1362         }
1363
1364         private void class_statement(ArrayList list) {
1365                 // class_statement:
1366                 // variable_modifiers class_variable_declaration ';'
1367                 // | class_constant_declaration ';'
1368                 // | method_modifiers T_FUNCTION is_reference T_STRING
1369                 // '(' parameter_list ')' method_body
1370                 initializeModifiers();
1371                 int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1372
1373                 if (token == TokenNamevar) {
1374                         checkAndSetModifiers(AccPublic);
1375                         problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1376                                         referenceContext, compilationUnit.compilationResult);
1377                         getNextToken();
1378                         class_variable_declaration(declarationSourceStart, list);
1379                 } else if (token == TokenNameconst) {
1380                         checkAndSetModifiers(AccFinal | AccPublic);
1381                         class_constant_declaration(declarationSourceStart, list);
1382                         if (token != TokenNameSEMICOLON) {
1383                                 throwSyntaxError("';' expected after class const declaration.");
1384                         }
1385                         getNextToken();
1386                 } else {
1387                         boolean hasModifiers = member_modifiers();
1388                         if (token == TokenNamefunction) {
1389                                 if (!hasModifiers) {
1390                                         checkAndSetModifiers(AccPublic);
1391                                 }
1392                                 MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1393                                 methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1394                                 methodDecl.modifiers = this.modifiers;
1395                                 methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
1396                                 try {
1397                                         getNextToken();
1398                                         functionDefinition(methodDecl);
1399                                 } finally {
1400                                         int sourceEnd = methodDecl.sourceEnd;
1401                                         if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
1402                                                 sourceEnd = methodDecl.declarationSourceStart + 1;
1403                                         }
1404                                         methodDecl.declarationSourceEnd = sourceEnd;
1405                                         methodDecl.sourceEnd = sourceEnd;
1406                                 }
1407                         } else {
1408                                 if (!hasModifiers) {
1409                                         throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1410                                 }
1411                                 class_variable_declaration(declarationSourceStart, list);
1412                         }
1413                 }
1414         }
1415
1416         private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1417                 // class_constant_declaration ',' T_STRING '=' static_scalar
1418                 // | T_CONST T_STRING '=' static_scalar
1419                 if (token != TokenNameconst) {
1420                         throwSyntaxError("'const' keyword expected in class declaration.");
1421                 } else {
1422                         getNextToken();
1423                 }
1424                 while (true) {
1425                         if (token != TokenNameIdentifier) {
1426                                 throwSyntaxError("Identifier expected in class const declaration.");
1427                         }
1428                         FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1429                                         .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1430                         fieldDeclaration.modifiers = this.modifiers;
1431                         fieldDeclaration.declarationSourceStart = declarationSourceStart;
1432                         fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1433                         fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1434                         // fieldDeclaration.type
1435                         list.add(fieldDeclaration);
1436                         getNextToken();
1437                         if (token != TokenNameEQUAL) {
1438                                 throwSyntaxError("'=' expected in class const declaration.");
1439                         }
1440                         getNextToken();
1441                         static_scalar();
1442                         if (token != TokenNameCOMMA) {
1443                                 break; // while(true)-loop
1444                         }
1445                         getNextToken();
1446                 }
1447         }
1448
1449         // private void variable_modifiers() {
1450         // // variable_modifiers:
1451         // // non_empty_member_modifiers
1452         // //| T_VAR
1453         // initializeModifiers();
1454         // if (token == TokenNamevar) {
1455         // checkAndSetModifiers(AccPublic);
1456         // reportSyntaxError(
1457         // "Keyword 'var' is deprecated. Please use 'public' 'private' or
1458         // 'protected'
1459         // modifier for field declarations.",
1460         // scanner.getCurrentTokenStartPosition(), scanner
1461         // .getCurrentTokenEndPosition());
1462         // getNextToken();
1463         // } else {
1464         // if (!member_modifiers()) {
1465         // throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1466         // field declarations.");
1467         // }
1468         // }
1469         // }
1470         // private void method_modifiers() {
1471         // //method_modifiers:
1472         // // /* empty */
1473         // //| non_empty_member_modifiers
1474         // initializeModifiers();
1475         // if (!member_modifiers()) {
1476         // checkAndSetModifiers(AccPublic);
1477         // }
1478         // }
1479         private boolean member_modifiers() {
1480                 // T_PUBLIC
1481                 // | T_PROTECTED
1482                 // | T_PRIVATE
1483                 // | T_STATIC
1484                 // | T_ABSTRACT
1485                 // | T_FINAL
1486                 boolean foundToken = false;
1487                 while (true) {
1488                         if (token == TokenNamepublic) {
1489                                 checkAndSetModifiers(AccPublic);
1490                                 getNextToken();
1491                                 foundToken = true;
1492                         } else if (token == TokenNameprotected) {
1493                                 checkAndSetModifiers(AccProtected);
1494                                 getNextToken();
1495                                 foundToken = true;
1496                         } else if (token == TokenNameprivate) {
1497                                 checkAndSetModifiers(AccPrivate);
1498                                 getNextToken();
1499                                 foundToken = true;
1500                         } else if (token == TokenNamestatic) {
1501                                 checkAndSetModifiers(AccStatic);
1502                                 getNextToken();
1503                                 foundToken = true;
1504                         } else if (token == TokenNameabstract) {
1505                                 checkAndSetModifiers(AccAbstract);
1506                                 getNextToken();
1507                                 foundToken = true;
1508                         } else if (token == TokenNamefinal) {
1509                                 checkAndSetModifiers(AccFinal);
1510                                 getNextToken();
1511                                 foundToken = true;
1512                         } else {
1513                                 break;
1514                         }
1515                 }
1516                 return foundToken;
1517         }
1518
1519         private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1520                 // class_variable_declaration:
1521                 // class_variable_declaration ',' T_VARIABLE
1522                 // | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1523                 // | T_VARIABLE
1524                 // | T_VARIABLE '=' static_scalar
1525                 char[] classVariable;
1526                 do {
1527                         if (token == TokenNameVariable) {
1528                                 classVariable = scanner.getCurrentIdentifierSource();
1529                                 // indexManager.addIdentifierInformation('v', classVariable, buf, -1,
1530                                 // -1);
1531                                 FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1532                                                 .getCurrentTokenEndPosition());
1533                                 fieldDeclaration.modifiers = this.modifiers;
1534                                 fieldDeclaration.declarationSourceStart = declarationSourceStart;
1535                                 fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1536                                 fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1537                                 list.add(fieldDeclaration);
1538                                 if (fTypeVariables != null) {
1539                                         VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_CLASS_UNIT);
1540                                         fTypeVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1541                                 }
1542                                 getNextToken();
1543                                 if (token == TokenNameEQUAL) {
1544                                         getNextToken();
1545                                         static_scalar();
1546                                 }
1547                         } else {
1548                                 // if (token == TokenNamethis) {
1549                                 // throwSyntaxError("'$this' not allowed after keyword 'public'
1550                                 // 'protected' 'private' 'var'.");
1551                                 // }
1552                                 throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1553                         }
1554                         if (token != TokenNameCOMMA) {
1555                                 break;
1556                         }
1557                         getNextToken();
1558                 } while (true);
1559                 if (token != TokenNameSEMICOLON) {
1560                         throwSyntaxError("';' expected after field declaration.");
1561                 }
1562                 getNextToken();
1563         }
1564
1565         private void functionDefinition(MethodDeclaration methodDecl) {
1566                 boolean isAbstract = false;
1567                 if (astPtr == 0) {
1568                         if (compilationUnit != null) {
1569                                 compilationUnit.types.add(methodDecl);
1570                         }
1571                 } else {
1572                         ASTNode node = astStack[astPtr];
1573                         if (node instanceof TypeDeclaration) {
1574                                 TypeDeclaration typeDecl = ((TypeDeclaration) node);
1575                                 if (typeDecl.methods == null) {
1576                                         typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1577                                 } else {
1578                                         AbstractMethodDeclaration[] newMethods;
1579                                         System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 0,
1580                                                         typeDecl.methods.length);
1581                                         newMethods[typeDecl.methods.length] = methodDecl;
1582                                         typeDecl.methods = newMethods;
1583                                 }
1584                                 if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1585                                         isAbstract = true;
1586                                 } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1587                                         isAbstract = true;
1588                                 }
1589                         }
1590                 }
1591                 try {
1592                         pushFunctionVariableSet();
1593                         functionDeclarator(methodDecl);
1594                         if (token == TokenNameSEMICOLON) {
1595                                 if (!isAbstract) {
1596                                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1597                                         throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1598                                 }
1599                                 getNextToken();
1600                                 return;
1601                         }
1602                         functionBody(methodDecl);
1603                 } finally {
1604                         if (!fStackUnassigned.isEmpty()) {
1605                                 fStackUnassigned.remove(fStackUnassigned.size() - 1);
1606                         }
1607                 }
1608         }
1609
1610         private void functionDeclarator(MethodDeclaration methodDecl) {
1611                 // identifier '(' [parameter-list] ')'
1612                 if (token == TokenNameAND) {
1613                         getNextToken();
1614                 }
1615                 methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1616                 methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1617                 if (Scanner.isIdentifierOrKeyword(token)) {
1618                         methodDecl.selector = scanner.getCurrentIdentifierSource();
1619                         if (token > TokenNameKEYWORD) {
1620                                 problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1621                                                 scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1622                         }
1623                         getNextToken();
1624                         if (token == TokenNameLPAREN) {
1625                                 getNextToken();
1626                         } else {
1627                                 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1628                                 throwSyntaxError("'(' expected in function declaration.");
1629                         }
1630                         if (token != TokenNameRPAREN) {
1631                                 parameter_list(methodDecl);
1632                         }
1633                         if (token != TokenNameRPAREN) {
1634                                 methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1635                                 throwSyntaxError("')' expected in function declaration.");
1636                         } else {
1637                                 methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1638                                 getNextToken();
1639                         }
1640                 } else {
1641                         methodDecl.selector = "<undefined>".toCharArray();
1642                         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1643                         throwSyntaxError("Function name expected after keyword 'function'.");
1644                 }
1645         }
1646
1647         //
1648         private void parameter_list(MethodDeclaration methodDecl) {
1649                 // non_empty_parameter_list
1650                 // | /* empty */
1651                 non_empty_parameter_list(methodDecl, true);
1652         }
1653
1654         private void non_empty_parameter_list(MethodDeclaration methodDecl, boolean empty_allowed) {
1655                 // optional_class_type T_VARIABLE
1656                 // | optional_class_type '&' T_VARIABLE
1657                 // | optional_class_type '&' T_VARIABLE '=' static_scalar
1658                 // | optional_class_type T_VARIABLE '=' static_scalar
1659                 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1660                 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1661                 // | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1662                 // static_scalar
1663                 // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1664                 // static_scalar
1665                 char[] typeIdentifier = null;
1666                 if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1667                         HashSet set = peekVariableSet();
1668                         while (true) {
1669                                 if (token == TokenNameIdentifier) {
1670                                         typeIdentifier = scanner.getCurrentIdentifierSource();
1671                                         getNextToken();
1672                                 }
1673                                 if (token == TokenNameAND) {
1674                                         getNextToken();
1675                                 }
1676                                 if (token == TokenNameVariable) {
1677                                         if (fMethodVariables != null) {
1678                                                 VariableInfo info;
1679                                                 if (methodDecl.type == MethodDeclaration.FUNCTION_DEFINITION) {
1680                                                         info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_FUNCTION_DEFINITION);
1681                                                 } else {
1682                                                         info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_METHOD_DEFINITION);
1683                                                 }
1684                                                 info.typeIdentifier = typeIdentifier;
1685                                                 fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1686                                         }
1687                                         addVariableSet(set);
1688                                         getNextToken();
1689                                         if (token == TokenNameEQUAL) {
1690                                                 getNextToken();
1691                                                 static_scalar();
1692                                         }
1693                                 } else {
1694                                         throwSyntaxError("Variable expected in parameter list.");
1695                                 }
1696                                 if (token != TokenNameCOMMA) {
1697                                         break;
1698                                 }
1699                                 getNextToken();
1700                         }
1701                         return;
1702                 }
1703                 if (!empty_allowed) {
1704                         throwSyntaxError("Identifier expected in parameter list.");
1705                 }
1706         }
1707
1708         private void optional_class_type() {
1709                 // /* empty */
1710                 // | T_STRING
1711         }
1712
1713         // private void parameterDeclaration() {
1714         // //variable
1715         // //variable-reference
1716         // if (token == TokenNameAND) {
1717         // getNextToken();
1718         // if (isVariable()) {
1719         // getNextToken();
1720         // } else {
1721         // throwSyntaxError("Variable expected after reference operator '&'.");
1722         // }
1723         // }
1724         // //variable '=' constant
1725         // if (token == TokenNameVariable) {
1726         // getNextToken();
1727         // if (token == TokenNameEQUAL) {
1728         // getNextToken();
1729         // static_scalar();
1730         // }
1731         // return;
1732         // }
1733         // // if (token == TokenNamethis) {
1734         // // throwSyntaxError("Reserved word '$this' not allowed in parameter
1735         // // declaration.");
1736         // // }
1737         // }
1738
1739         private void labeledStatementList() {
1740                 if (token != TokenNamecase && token != TokenNamedefault) {
1741                         throwSyntaxError("'case' or 'default' expected.");
1742                 }
1743                 do {
1744                         if (token == TokenNamecase) {
1745                                 getNextToken();
1746                                 expr(); // constant();
1747                                 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1748                                         getNextToken();
1749                                         if (token == TokenNamecase || token == TokenNamedefault) {
1750                                                 // empty case statement ?
1751                                                 continue;
1752                                         }
1753                                         statementList();
1754                                 }
1755                                 // else if (token == TokenNameSEMICOLON) {
1756                                 // setMarker(
1757                                 // "':' expected after 'case' keyword (Found token: " +
1758                                 // scanner.toStringAction(token) + ")",
1759                                 // scanner.getCurrentTokenStartPosition(),
1760                                 // scanner.getCurrentTokenEndPosition(),
1761                                 // INFO);
1762                                 // getNextToken();
1763                                 // if (token == TokenNamecase) { // empty case statement ?
1764                                 // continue;
1765                                 // }
1766                                 // statementList();
1767                                 // }
1768                                 else {
1769                                         throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1770                                 }
1771                         } else { // TokenNamedefault
1772                                 getNextToken();
1773                                 if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1774                                         getNextToken();
1775                                         if (token == TokenNameRBRACE) {
1776                                                 // empty default case
1777                                                 break;
1778                                         }
1779                                         if (token != TokenNamecase) {
1780                                                 statementList();
1781                                         }
1782                                 } else {
1783                                         throwSyntaxError("':' character expected after 'default'.");
1784                                 }
1785                         }
1786                 } while (token == TokenNamecase || token == TokenNamedefault);
1787         }
1788
1789         private void ifStatementColon(IfStatement iState) {
1790                 // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list
1791                 // new_else_single T_ENDIF ';'
1792                 HashSet assignedVariableSet = null;
1793                 try {
1794                         Block b = inner_statement_list();
1795                         iState.thenStatement = b;
1796                         checkUnreachable(iState, b);
1797                 } finally {
1798                         assignedVariableSet = removeIfVariableSet();
1799                 }
1800                 if (token == TokenNameelseif) {
1801                         try {
1802                                 pushIfVariableSet();
1803                                 new_elseif_list(iState);
1804                         } finally {
1805                                 HashSet set = removeIfVariableSet();
1806                                 if (assignedVariableSet != null && set != null) {
1807                                         assignedVariableSet.addAll(set);
1808                                 }
1809                         }
1810                 }
1811                 try {
1812                         pushIfVariableSet();
1813                         new_else_single(iState);
1814                 } finally {
1815                         HashSet set = removeIfVariableSet();
1816                         if (assignedVariableSet != null) {
1817                                 HashSet topSet = peekVariableSet();
1818                                 if (topSet != null) {
1819                                         if (set != null) {
1820                                                 topSet.addAll(set);
1821                                         }
1822                                         topSet.addAll(assignedVariableSet);
1823                                 }
1824                         }
1825                 }
1826                 if (token != TokenNameendif) {
1827                         throwSyntaxError("'endif' expected.");
1828                 }
1829                 getNextToken();
1830                 if (token != TokenNameSEMICOLON) {
1831                         reportSyntaxError("';' expected after if-statement.");
1832                         iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1833                 } else {
1834                         iState.sourceEnd = scanner.getCurrentTokenEndPosition();
1835                         getNextToken();
1836                 }
1837         }
1838
1839         private void ifStatement(IfStatement iState) {
1840                 // T_IF '(' expr ')' statement elseif_list else_single
1841                 HashSet assignedVariableSet = null;
1842                 try {
1843                         pushIfVariableSet();
1844                         Statement s = statement();
1845                         iState.thenStatement = s;
1846                         checkUnreachable(iState, s);
1847                 } finally {
1848                         assignedVariableSet = removeIfVariableSet();
1849                 }
1850
1851                 if (token == TokenNameelseif) {
1852                         try {
1853                                 pushIfVariableSet();
1854                                 elseif_list(iState);
1855                         } finally {
1856                                 HashSet set = removeIfVariableSet();
1857                                 if (assignedVariableSet != null && set != null) {
1858                                         assignedVariableSet.addAll(set);
1859                                 }
1860                         }
1861                 }
1862                 try {
1863                         pushIfVariableSet();
1864                         else_single(iState);
1865                 } finally {
1866                         HashSet set = removeIfVariableSet();
1867                         if (assignedVariableSet != null) {
1868                                 HashSet topSet = peekVariableSet();
1869                                 if (topSet != null) {
1870                                         if (set != null) {
1871                                                 topSet.addAll(set);
1872                                         }
1873                                         topSet.addAll(assignedVariableSet);
1874                                 }
1875                         }
1876                 }
1877         }
1878
1879         private void elseif_list(IfStatement iState) {
1880                 // /* empty */
1881                 // | elseif_list T_ELSEIF '(' expr ')' statement
1882                 ArrayList conditionList = new ArrayList();
1883                 ArrayList statementList = new ArrayList();
1884                 Expression e;
1885                 Statement s;
1886                 while (token == TokenNameelseif) {
1887                         getNextToken();
1888                         if (token == TokenNameLPAREN) {
1889                                 getNextToken();
1890                         } else {
1891                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1892                         }
1893                         e = expr();
1894                         conditionList.add(e);
1895                         if (token == TokenNameRPAREN) {
1896                                 getNextToken();
1897                         } else {
1898                                 throwSyntaxError("')' expected after 'elseif' condition.");
1899                         }
1900                         s = statement();
1901                         statementList.add(s);
1902                         checkUnreachable(iState, s);
1903                 }
1904                 iState.elseifConditions = new Expression[conditionList.size()];
1905                 iState.elseifStatements = new Statement[statementList.size()];
1906                 conditionList.toArray(iState.elseifConditions);
1907                 statementList.toArray(iState.elseifStatements);
1908         }
1909
1910         private void new_elseif_list(IfStatement iState) {
1911                 // /* empty */
1912                 // | new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
1913                 ArrayList conditionList = new ArrayList();
1914                 ArrayList statementList = new ArrayList();
1915                 Expression e;
1916                 Block b;
1917                 while (token == TokenNameelseif) {
1918                         getNextToken();
1919                         if (token == TokenNameLPAREN) {
1920                                 getNextToken();
1921                         } else {
1922                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1923                         }
1924                         e = expr();
1925                         conditionList.add(e);
1926                         if (token == TokenNameRPAREN) {
1927                                 getNextToken();
1928                         } else {
1929                                 throwSyntaxError("')' expected after 'elseif' condition.");
1930                         }
1931                         if (token == TokenNameCOLON) {
1932                                 getNextToken();
1933                         } else {
1934                                 throwSyntaxError("':' expected after 'elseif' keyword.");
1935                         }
1936                         b = inner_statement_list();
1937                         statementList.add(b);
1938                         checkUnreachable(iState, b);
1939                 }
1940                 iState.elseifConditions = new Expression[conditionList.size()];
1941                 iState.elseifStatements = new Statement[statementList.size()];
1942                 conditionList.toArray(iState.elseifConditions);
1943                 statementList.toArray(iState.elseifStatements);
1944         }
1945
1946         private void else_single(IfStatement iState) {
1947                 // /* empty */
1948                 // T_ELSE statement
1949                 if (token == TokenNameelse) {
1950                         getNextToken();
1951                         Statement s = statement();
1952                         iState.elseStatement = s;
1953                         checkUnreachable(iState, s);
1954                 } else {
1955                         iState.checkUnreachable = false;
1956                 }
1957                 iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1958         }
1959
1960         private void new_else_single(IfStatement iState) {
1961                 // /* empty */
1962                 // | T_ELSE ':' inner_statement_list
1963                 if (token == TokenNameelse) {
1964                         getNextToken();
1965                         if (token == TokenNameCOLON) {
1966                                 getNextToken();
1967                         } else {
1968                                 throwSyntaxError("':' expected after 'else' keyword.");
1969                         }
1970                         Block b = inner_statement_list();
1971                         iState.elseStatement = b;
1972                         checkUnreachable(iState, b);
1973                 } else {
1974                         iState.checkUnreachable = false;
1975                 }
1976         }
1977
1978         private Block inner_statement_list() {
1979                 // inner_statement_list inner_statement
1980                 // /* empty */
1981                 return statementList();
1982         }
1983
1984         /**
1985          * @param iState
1986          * @param b
1987          */
1988         private void checkUnreachable(IfStatement iState, Statement s) {
1989                 if (s instanceof Block) {
1990                         Block b = (Block) s;
1991                         if (b.statements == null || b.statements.length == 0) {
1992                                 iState.checkUnreachable = false;
1993                         } else {
1994                                 int off = b.statements.length - 1;
1995                                 if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
1996                                                 && !(b.statements[off] instanceof BreakStatement)) {
1997                                         if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
1998                                                 iState.checkUnreachable = false;
1999                                         }
2000                                 }
2001                         }
2002                 } else {
2003                         if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
2004                                 if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
2005                                         iState.checkUnreachable = false;
2006                                 }
2007                         }
2008                 }
2009         }
2010
2011         // private void elseifStatementList() {
2012         // do {
2013         // elseifStatement();
2014         // switch (token) {
2015         // case TokenNameelse:
2016         // getNextToken();
2017         // if (token == TokenNameCOLON) {
2018         // getNextToken();
2019         // if (token != TokenNameendif) {
2020         // statementList();
2021         // }
2022         // return;
2023         // } else {
2024         // if (token == TokenNameif) { //'else if'
2025         // getNextToken();
2026         // } else {
2027         // throwSyntaxError("':' expected after 'else'.");
2028         // }
2029         // }
2030         // break;
2031         // case TokenNameelseif:
2032         // getNextToken();
2033         // break;
2034         // default:
2035         // return;
2036         // }
2037         // } while (true);
2038         // }
2039
2040         // private void elseifStatement() {
2041         // if (token == TokenNameLPAREN) {
2042         // getNextToken();
2043         // expr();
2044         // if (token != TokenNameRPAREN) {
2045         // throwSyntaxError("')' expected in else-if-statement.");
2046         // }
2047         // getNextToken();
2048         // if (token != TokenNameCOLON) {
2049         // throwSyntaxError("':' expected in else-if-statement.");
2050         // }
2051         // getNextToken();
2052         // if (token != TokenNameendif) {
2053         // statementList();
2054         // }
2055         // }
2056         // }
2057
2058         private void switchStatement() {
2059                 if (token == TokenNameCOLON) {
2060                         // ':' [labeled-statement-list] 'endswitch' ';'
2061                         getNextToken();
2062                         labeledStatementList();
2063                         if (token != TokenNameendswitch) {
2064                                 throwSyntaxError("'endswitch' expected.");
2065                         }
2066                         getNextToken();
2067                         if (token != TokenNameSEMICOLON) {
2068                                 throwSyntaxError("';' expected after switch-statement.");
2069                         }
2070                         getNextToken();
2071                 } else {
2072                         // '{' [labeled-statement-list] '}'
2073                         if (token != TokenNameLBRACE) {
2074                                 throwSyntaxError("'{' expected in switch statement.");
2075                         }
2076                         getNextToken();
2077                         if (token != TokenNameRBRACE) {
2078                                 labeledStatementList();
2079                         }
2080                         if (token != TokenNameRBRACE) {
2081                                 throwSyntaxError("'}' expected in switch statement.");
2082                         }
2083                         getNextToken();
2084                 }
2085         }
2086
2087         private void forStatement() {
2088                 if (token == TokenNameCOLON) {
2089                         getNextToken();
2090                         statementList();
2091                         if (token != TokenNameendfor) {
2092                                 throwSyntaxError("'endfor' expected.");
2093                         }
2094                         getNextToken();
2095                         if (token != TokenNameSEMICOLON) {
2096                                 throwSyntaxError("';' expected after for-statement.");
2097                         }
2098                         getNextToken();
2099                 } else {
2100                         statement();
2101                 }
2102         }
2103
2104         private void whileStatement() {
2105                 // ':' statement-list 'endwhile' ';'
2106                 if (token == TokenNameCOLON) {
2107                         getNextToken();
2108                         statementList();
2109                         if (token != TokenNameendwhile) {
2110                                 throwSyntaxError("'endwhile' expected.");
2111                         }
2112                         getNextToken();
2113                         if (token != TokenNameSEMICOLON) {
2114                                 throwSyntaxError("';' expected after while-statement.");
2115                         }
2116                         getNextToken();
2117                 } else {
2118                         statement();
2119                 }
2120         }
2121
2122         private void foreachStatement() {
2123                 if (token == TokenNameCOLON) {
2124                         getNextToken();
2125                         statementList();
2126                         if (token != TokenNameendforeach) {
2127                                 throwSyntaxError("'endforeach' expected.");
2128                         }
2129                         getNextToken();
2130                         if (token != TokenNameSEMICOLON) {
2131                                 throwSyntaxError("';' expected after foreach-statement.");
2132                         }
2133                         getNextToken();
2134                 } else {
2135                         statement();
2136                 }
2137         }
2138
2139         // private void exitStatus() {
2140         // if (token == TokenNameLPAREN) {
2141         // getNextToken();
2142         // } else {
2143         // throwSyntaxError("'(' expected in 'exit-status'.");
2144         // }
2145         // if (token != TokenNameRPAREN) {
2146         // expression();
2147         // }
2148         // if (token == TokenNameRPAREN) {
2149         // getNextToken();
2150         // } else {
2151         // throwSyntaxError("')' expected after 'exit-status'.");
2152         // }
2153         // }
2154         private void expressionList() {
2155                 do {
2156                         expr();
2157                         if (token == TokenNameCOMMA) {
2158                                 getNextToken();
2159                         } else {
2160                                 break;
2161                         }
2162                 } while (true);
2163         }
2164
2165         private Expression expr() {
2166                 // r_variable
2167                 // | expr_without_variable
2168                 // if (token!=TokenNameEOF) {
2169                 if (Scanner.TRACE) {
2170                         System.out.println("TRACE: expr()");
2171                 }
2172                 return expr_without_variable(true);
2173                 // }
2174         }
2175
2176         private Expression expr_without_variable(boolean only_variable) {
2177                 int exprSourceStart = scanner.getCurrentTokenStartPosition();
2178                 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2179                 Expression expression = new Expression();
2180                 expression.sourceStart = exprSourceStart;
2181                 // default, may be overwritten
2182                 expression.sourceEnd = exprSourceEnd;
2183                 try {
2184                         // internal_functions_in_yacc
2185                         // | T_CLONE expr
2186                         // | T_PRINT expr
2187                         // | '(' expr ')'
2188                         // | '@' expr
2189                         // | '+' expr
2190                         // | '-' expr
2191                         // | '!' expr
2192                         // | '~' expr
2193                         // | T_INC rw_variable
2194                         // | T_DEC rw_variable
2195                         // | T_INT_CAST expr
2196                         // | T_DOUBLE_CAST expr
2197                         // | T_STRING_CAST expr
2198                         // | T_ARRAY_CAST expr
2199                         // | T_OBJECT_CAST expr
2200                         // | T_BOOL_CAST expr
2201                         // | T_UNSET_CAST expr
2202                         // | T_EXIT exit_expr
2203                         // | scalar
2204                         // | T_ARRAY '(' array_pair_list ')'
2205                         // | '`' encaps_list '`'
2206                         // | T_LIST '(' assignment_list ')' '=' expr
2207                         // | T_NEW class_name_reference ctor_arguments
2208                         // | variable '=' expr
2209                         // | variable '=' '&' variable
2210                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2211                         // | variable T_PLUS_EQUAL expr
2212                         // | variable T_MINUS_EQUAL expr
2213                         // | variable T_MUL_EQUAL expr
2214                         // | variable T_DIV_EQUAL expr
2215                         // | variable T_CONCAT_EQUAL expr
2216                         // | variable T_MOD_EQUAL expr
2217                         // | variable T_AND_EQUAL expr
2218                         // | variable T_OR_EQUAL expr
2219                         // | variable T_XOR_EQUAL expr
2220                         // | variable T_SL_EQUAL expr
2221                         // | variable T_SR_EQUAL expr
2222                         // | rw_variable T_INC
2223                         // | rw_variable T_DEC
2224                         // | expr T_BOOLEAN_OR expr
2225                         // | expr T_BOOLEAN_AND expr
2226                         // | expr T_LOGICAL_OR expr
2227                         // | expr T_LOGICAL_AND expr
2228                         // | expr T_LOGICAL_XOR expr
2229                         // | expr '|' expr
2230                         // | expr '&' expr
2231                         // | expr '^' expr
2232                         // | expr '.' expr
2233                         // | expr '+' expr
2234                         // | expr '-' expr
2235                         // | expr '*' expr
2236                         // | expr '/' expr
2237                         // | expr '%' expr
2238                         // | expr T_SL expr
2239                         // | expr T_SR expr
2240                         // | expr T_IS_IDENTICAL expr
2241                         // | expr T_IS_NOT_IDENTICAL expr
2242                         // | expr T_IS_EQUAL expr
2243                         // | expr T_IS_NOT_EQUAL expr
2244                         // | expr '<' expr
2245                         // | expr T_IS_SMALLER_OR_EQUAL expr
2246                         // | expr '>' expr
2247                         // | expr T_IS_GREATER_OR_EQUAL expr
2248                         // | expr T_INSTANCEOF class_name_reference
2249                         // | expr '?' expr ':' expr
2250                         if (Scanner.TRACE) {
2251                                 System.out.println("TRACE: expr_without_variable() PART 1");
2252                         }
2253                         switch (token) {
2254                         case TokenNameisset:
2255                                 // T_ISSET '(' isset_variables ')'
2256                                 getNextToken();
2257                                 if (token != TokenNameLPAREN) {
2258                                         throwSyntaxError("'(' expected after keyword 'isset'");
2259                                 }
2260                                 getNextToken();
2261                                 isset_variables();
2262                                 if (token != TokenNameRPAREN) {
2263                                         throwSyntaxError("')' expected after keyword 'isset'");
2264                                 }
2265                                 getNextToken();
2266                                 break;
2267                         case TokenNameempty:
2268                                 getNextToken();
2269                                 if (token != TokenNameLPAREN) {
2270                                         throwSyntaxError("'(' expected after keyword 'empty'");
2271                                 }
2272                                 getNextToken();
2273                                 variable(true, false);
2274                                 if (token != TokenNameRPAREN) {
2275                                         throwSyntaxError("')' expected after keyword 'empty'");
2276                                 }
2277                                 getNextToken();
2278                                 break;
2279                         case TokenNameeval:
2280                         case TokenNameinclude:
2281                         case TokenNameinclude_once:
2282                         case TokenNamerequire:
2283                         case TokenNamerequire_once:
2284                                 internal_functions_in_yacc();
2285                                 break;
2286                         // | '(' expr ')'
2287                         case TokenNameLPAREN:
2288                                 getNextToken();
2289                                 expr();
2290                                 if (token == TokenNameRPAREN) {
2291                                         getNextToken();
2292                                 } else {
2293                                         throwSyntaxError("')' expected in expression.");
2294                                 }
2295                                 break;
2296                         // | T_CLONE expr
2297                         // | T_PRINT expr
2298                         // | '@' expr
2299                         // | '+' expr
2300                         // | '-' expr
2301                         // | '!' expr
2302                         // | '~' expr
2303                         // | T_INT_CAST expr
2304                         // | T_DOUBLE_CAST expr
2305                         // | T_STRING_CAST expr
2306                         // | T_ARRAY_CAST expr
2307                         // | T_OBJECT_CAST expr
2308                         // | T_BOOL_CAST expr
2309                         // | T_UNSET_CAST expr
2310                         case TokenNameclone:
2311                         case TokenNameprint:
2312                         case TokenNameAT:
2313                         case TokenNamePLUS:
2314                         case TokenNameMINUS:
2315                         case TokenNameNOT:
2316                         case TokenNameTWIDDLE:
2317                         case TokenNameintCAST:
2318                         case TokenNamedoubleCAST:
2319                         case TokenNamestringCAST:
2320                         case TokenNamearrayCAST:
2321                         case TokenNameobjectCAST:
2322                         case TokenNameboolCAST:
2323                         case TokenNameunsetCAST:
2324                                 getNextToken();
2325                                 expr();
2326                                 break;
2327                         case TokenNameexit:
2328                                 getNextToken();
2329                                 exit_expr();
2330                                 break;
2331                         // scalar:
2332                         // T_STRING
2333                         // | T_STRING_VARNAME
2334                         // | class_constant
2335                         // | T_START_HEREDOC encaps_list T_END_HEREDOC
2336                         // | '`' encaps_list '`'
2337                         // | common_scalar
2338                         // | '`' encaps_list '`'
2339                         // case TokenNameEncapsedString0:
2340                         // scanner.encapsedStringStack.push(new Character('`'));
2341                         // getNextToken();
2342                         // try {
2343                         // if (token == TokenNameEncapsedString0) {
2344                         // } else {
2345                         // encaps_list();
2346                         // if (token != TokenNameEncapsedString0) {
2347                         // throwSyntaxError("\'`\' expected at end of string" + "(Found token: " +
2348                         // scanner.toStringAction(token) + " )");
2349                         // }
2350                         // }
2351                         // } finally {
2352                         // scanner.encapsedStringStack.pop();
2353                         // getNextToken();
2354                         // }
2355                         // break;
2356                         // // | '\'' encaps_list '\''
2357                         // case TokenNameEncapsedString1:
2358                         // scanner.encapsedStringStack.push(new Character('\''));
2359                         // getNextToken();
2360                         // try {
2361                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2362                         // if (token == TokenNameEncapsedString1) {
2363                         // expression = new
2364                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2365                         // exprSourceStart, scanner
2366                         // .getCurrentTokenEndPosition());
2367                         // } else {
2368                         // encaps_list();
2369                         // if (token != TokenNameEncapsedString1) {
2370                         // throwSyntaxError("\'\'\' expected at end of string" + "(Found token: "
2371                         // + scanner.toStringAction(token) + " )");
2372                         // } else {
2373                         // expression = new
2374                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2375                         // exprSourceStart, scanner
2376                         // .getCurrentTokenEndPosition());
2377                         // }
2378                         // }
2379                         // } finally {
2380                         // scanner.encapsedStringStack.pop();
2381                         // getNextToken();
2382                         // }
2383                         // break;
2384                         // //| '"' encaps_list '"'
2385                         // case TokenNameEncapsedString2:
2386                         // scanner.encapsedStringStack.push(new Character('"'));
2387                         // getNextToken();
2388                         // try {
2389                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2390                         // if (token == TokenNameEncapsedString2) {
2391                         // expression = new
2392                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2393                         // exprSourceStart, scanner
2394                         // .getCurrentTokenEndPosition());
2395                         // } else {
2396                         // encaps_list();
2397                         // if (token != TokenNameEncapsedString2) {
2398                         // throwSyntaxError("'\"' expected at end of string" + "(Found token: " +
2399                         // scanner.toStringAction(token) + " )");
2400                         // } else {
2401                         // expression = new
2402                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2403                         // exprSourceStart, scanner
2404                         // .getCurrentTokenEndPosition());
2405                         // }
2406                         // }
2407                         // } finally {
2408                         // scanner.encapsedStringStack.pop();
2409                         // getNextToken();
2410                         // }
2411                         // break;
2412                         case TokenNameStringDoubleQuote:
2413                                 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2414                                                 .getCurrentTokenEndPosition());
2415                                 common_scalar();
2416                                 break;
2417                         case TokenNameStringSingleQuote:
2418                                 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2419                                                 .getCurrentTokenEndPosition());
2420                                 common_scalar();
2421                                 break;
2422                         case TokenNameIntegerLiteral:
2423                         case TokenNameDoubleLiteral:
2424                         case TokenNameStringInterpolated:
2425                         case TokenNameFILE:
2426                         case TokenNameLINE:
2427                         case TokenNameCLASS_C:
2428                         case TokenNameMETHOD_C:
2429                         case TokenNameFUNC_C:
2430                                 common_scalar();
2431                                 break;
2432                         case TokenNameHEREDOC:
2433                                 getNextToken();
2434                                 break;
2435                         case TokenNamearray:
2436                                 // T_ARRAY '(' array_pair_list ')'
2437                                 getNextToken();
2438                                 if (token == TokenNameLPAREN) {
2439                                         getNextToken();
2440                                         if (token == TokenNameRPAREN) {
2441                                                 getNextToken();
2442                                                 break;
2443                                         }
2444                                         array_pair_list();
2445                                         if (token != TokenNameRPAREN) {
2446                                                 throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2447                                         }
2448                                         getNextToken();
2449                                 } else {
2450                                         throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2451                                 }
2452                                 break;
2453                         case TokenNamelist:
2454                                 // | T_LIST '(' assignment_list ')' '=' expr
2455                                 getNextToken();
2456                                 if (token == TokenNameLPAREN) {
2457                                         getNextToken();
2458                                         assignment_list();
2459                                         if (token != TokenNameRPAREN) {
2460                                                 throwSyntaxError("')' expected after 'list' keyword.");
2461                                         }
2462                                         getNextToken();
2463                                         if (token != TokenNameEQUAL) {
2464                                                 throwSyntaxError("'=' expected after 'list' keyword.");
2465                                         }
2466                                         getNextToken();
2467                                         expr();
2468                                 } else {
2469                                         throwSyntaxError("'(' expected after 'list' keyword.");
2470                                 }
2471                                 break;
2472                         case TokenNamenew:
2473                                 // | T_NEW class_name_reference ctor_arguments
2474                                 getNextToken();
2475                                 Expression typeRef = class_name_reference();
2476                                 ctor_arguments();
2477                                 if (typeRef != null) {
2478                                         expression = typeRef;
2479                                 }
2480                                 break;
2481                         // | T_INC rw_variable
2482                         // | T_DEC rw_variable
2483                         case TokenNamePLUS_PLUS:
2484                         case TokenNameMINUS_MINUS:
2485                                 getNextToken();
2486                                 rw_variable();
2487                                 break;
2488                         // | variable '=' expr
2489                         // | variable '=' '&' variable
2490                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2491                         // | variable T_PLUS_EQUAL expr
2492                         // | variable T_MINUS_EQUAL expr
2493                         // | variable T_MUL_EQUAL expr
2494                         // | variable T_DIV_EQUAL expr
2495                         // | variable T_CONCAT_EQUAL expr
2496                         // | variable T_MOD_EQUAL expr
2497                         // | variable T_AND_EQUAL expr
2498                         // | variable T_OR_EQUAL expr
2499                         // | variable T_XOR_EQUAL expr
2500                         // | variable T_SL_EQUAL expr
2501                         // | variable T_SR_EQUAL expr
2502                         // | rw_variable T_INC
2503                         // | rw_variable T_DEC
2504                         case TokenNameIdentifier:
2505                         case TokenNameVariable:
2506                         case TokenNameDOLLAR:
2507                                 boolean rememberedVar = false;
2508                                 Expression lhs = variable(true, true);
2509                                 if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
2510                                                 && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
2511                                                 && token != TokenNameDOT_EQUAL && token != TokenNameREMAINDER_EQUAL && token != TokenNameAND_EQUAL
2512                                                 && token != TokenNameOR_EQUAL && token != TokenNameXOR_EQUAL && token != TokenNameRIGHT_SHIFT_EQUAL
2513                                                 && token != TokenNameLEFT_SHIFT_EQUAL) {
2514                                         FieldReference ref = (FieldReference) lhs;
2515                                         if (!containsVariableSet(ref.token)) {
2516                                                 problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(), referenceContext,
2517                                                                 compilationUnit.compilationResult);
2518                                                 addVariableSet(ref.token);
2519                                         }
2520                                 }
2521
2522                                 switch (token) {
2523                                 case TokenNameEQUAL:
2524                                         if (lhs != null && lhs instanceof FieldReference) {
2525                                                 addVariableSet(((FieldReference) lhs).token);
2526                                         }
2527                                         getNextToken();
2528                                         if (token == TokenNameAND) {
2529                                                 getNextToken();
2530                                                 if (token == TokenNamenew) {
2531                                                         // | variable '=' '&' T_NEW class_name_reference
2532                                                         // ctor_arguments
2533                                                         getNextToken();
2534                                                         SingleTypeReference classRef = class_name_reference();
2535                                                         ctor_arguments();
2536                                                         if (classRef != null) {
2537                                                                 if (lhs != null && lhs instanceof FieldReference) {
2538                                                                         // example:
2539                                                                         // $var = & new Object();
2540                                                                         if (fMethodVariables != null) {
2541                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2542                                                                                 lhsInfo.reference = classRef;
2543                                                                                 lhsInfo.typeIdentifier = classRef.token;
2544                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2545                                                                                 rememberedVar = true;
2546                                                                         }
2547                                                                 }
2548                                                         }
2549                                                 } else {
2550                                                         Expression rhs = variable(false, false);
2551                                                         if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2552                                                                 // example:
2553                                                                 // $var = &$ref;
2554                                                                 if (fMethodVariables != null) {
2555                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2556                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2557                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2558                                                                                 lhsInfo.reference = rhsInfo.reference;
2559                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2560                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2561                                                                                 rememberedVar = true;
2562                                                                         }
2563                                                                 }
2564                                                         }
2565                                                 }
2566                                         } else {
2567                                                 Expression rhs = expr();
2568                                                 if (lhs != null && lhs instanceof FieldReference) {
2569                                                         if (rhs != null && rhs instanceof FieldReference) {
2570                                                                 // example:
2571                                                                 // $var = $ref;
2572                                                                 if (fMethodVariables != null) {
2573                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2574                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2575                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2576                                                                                 lhsInfo.reference = rhsInfo.reference;
2577                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2578                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2579                                                                                 rememberedVar = true;
2580                                                                         }
2581                                                                 }
2582                                                         } else if (rhs != null && rhs instanceof SingleTypeReference) {
2583                                                                 // example:
2584                                                                 // $var = new Object();
2585                                                                 if (fMethodVariables != null) {
2586                                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2587                                                                         lhsInfo.reference = (SingleTypeReference) rhs;
2588                                                                         lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2589                                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2590                                                                         rememberedVar = true;
2591                                                                 }
2592                                                         }
2593                                                 }
2594                                         }
2595                                         if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2596                                                 if (fMethodVariables != null) {
2597                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2598                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2599                                                 }
2600                                         }
2601                                         break;
2602                                 case TokenNamePLUS_EQUAL:
2603                                 case TokenNameMINUS_EQUAL:
2604                                 case TokenNameMULTIPLY_EQUAL:
2605                                 case TokenNameDIVIDE_EQUAL:
2606                                 case TokenNameDOT_EQUAL:
2607                                 case TokenNameREMAINDER_EQUAL:
2608                                 case TokenNameAND_EQUAL:
2609                                 case TokenNameOR_EQUAL:
2610                                 case TokenNameXOR_EQUAL:
2611                                 case TokenNameRIGHT_SHIFT_EQUAL:
2612                                 case TokenNameLEFT_SHIFT_EQUAL:
2613                                         if (lhs != null && lhs instanceof FieldReference) {
2614                                                 addVariableSet(((FieldReference) lhs).token);
2615                                         }
2616                                         getNextToken();
2617                                         expr();
2618                                         break;
2619                                 case TokenNamePLUS_PLUS:
2620                                 case TokenNameMINUS_MINUS:
2621                                         getNextToken();
2622                                         break;
2623                                 default:
2624                                         if (!only_variable) {
2625                                                 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2626                                         }
2627                                         if (lhs != null) {
2628                                                 expression = lhs;
2629                                         }
2630                                 }
2631                                 break;
2632                         default:
2633                                 if (token != TokenNameINLINE_HTML) {
2634                                         if (token > TokenNameKEYWORD) {
2635                                                 getNextToken();
2636                                                 break;
2637                                         } else {
2638                                                 // System.out.println(scanner.getCurrentTokenStartPosition());
2639                                                 // System.out.println(scanner.getCurrentTokenEndPosition());
2640
2641                                                 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2642                                         }
2643                                 }
2644                                 return expression;
2645                         }
2646                         if (Scanner.TRACE) {
2647                                 System.out.println("TRACE: expr_without_variable() PART 2");
2648                         }
2649                         // | expr T_BOOLEAN_OR expr
2650                         // | expr T_BOOLEAN_AND expr
2651                         // | expr T_LOGICAL_OR expr
2652                         // | expr T_LOGICAL_AND expr
2653                         // | expr T_LOGICAL_XOR expr
2654                         // | expr '|' expr
2655                         // | expr '&' expr
2656                         // | expr '^' expr
2657                         // | expr '.' expr
2658                         // | expr '+' expr
2659                         // | expr '-' expr
2660                         // | expr '*' expr
2661                         // | expr '/' expr
2662                         // | expr '%' expr
2663                         // | expr T_SL expr
2664                         // | expr T_SR expr
2665                         // | expr T_IS_IDENTICAL expr
2666                         // | expr T_IS_NOT_IDENTICAL expr
2667                         // | expr T_IS_EQUAL expr
2668                         // | expr T_IS_NOT_EQUAL expr
2669                         // | expr '<' expr
2670                         // | expr T_IS_SMALLER_OR_EQUAL expr
2671                         // | expr '>' expr
2672                         // | expr T_IS_GREATER_OR_EQUAL expr
2673                         while (true) {
2674                                 switch (token) {
2675                                 case TokenNameOR_OR:
2676                                         getNextToken();
2677                                         expression = new OR_OR_Expression(expression, expr(), token);
2678                                         break;
2679                                 case TokenNameAND_AND:
2680                                         getNextToken();
2681                                         expression = new AND_AND_Expression(expression, expr(), token);
2682                                         break;
2683                                 case TokenNameEQUAL_EQUAL:
2684                                         getNextToken();
2685                                         expression = new EqualExpression(expression, expr(), token);
2686                                         break;
2687                                 case TokenNameand:
2688                                 case TokenNameor:
2689                                 case TokenNamexor:
2690                                 case TokenNameAND:
2691                                 case TokenNameOR:
2692                                 case TokenNameXOR:
2693                                 case TokenNameDOT:
2694                                 case TokenNamePLUS:
2695                                 case TokenNameMINUS:
2696                                 case TokenNameMULTIPLY:
2697                                 case TokenNameDIVIDE:
2698                                 case TokenNameREMAINDER:
2699                                 case TokenNameLEFT_SHIFT:
2700                                 case TokenNameRIGHT_SHIFT:
2701                                 case TokenNameEQUAL_EQUAL_EQUAL:
2702                                 case TokenNameNOT_EQUAL_EQUAL:
2703                                 case TokenNameNOT_EQUAL:
2704                                 case TokenNameLESS:
2705                                 case TokenNameLESS_EQUAL:
2706                                 case TokenNameGREATER:
2707                                 case TokenNameGREATER_EQUAL:
2708                                         getNextToken();
2709                                         expression = new BinaryExpression(expression, expr(), token);
2710                                         break;
2711                                 // | expr T_INSTANCEOF class_name_reference
2712                                 // | expr '?' expr ':' expr
2713                                 case TokenNameinstanceof:
2714                                         getNextToken();
2715                                         TypeReference classRef = class_name_reference();
2716                                         if (classRef != null) {
2717                                                 expression = new InstanceOfExpression(expression, classRef, OperatorIds.INSTANCEOF);
2718                                                 expression.sourceStart = exprSourceStart;
2719                                                 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2720                                         }
2721                                         break;
2722                                 case TokenNameQUESTION:
2723                                         getNextToken();
2724                                         Expression valueIfTrue = expr();
2725                                         if (token != TokenNameCOLON) {
2726                                                 throwSyntaxError("':' expected in conditional expression.");
2727                                         }
2728                                         getNextToken();
2729                                         Expression valueIfFalse = expr();
2730
2731                                         expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2732                                         break;
2733                                 default:
2734                                         return expression;
2735                                 }
2736                         }
2737                 } catch (SyntaxError e) {
2738                         // try to find next token after expression with errors:
2739                         if (token == TokenNameSEMICOLON) {
2740                                 getNextToken();
2741                                 return expression;
2742                         }
2743                         if (token == TokenNameRBRACE || token == TokenNameRPAREN || token == TokenNameRBRACKET) {
2744                                 getNextToken();
2745                                 return expression;
2746                         }
2747                         throw e;
2748                 }
2749         }
2750
2751         private SingleTypeReference class_name_reference() {
2752                 // class_name_reference:
2753                 // T_STRING
2754                 // | dynamic_class_name_reference
2755                 SingleTypeReference ref = null;
2756                 if (Scanner.TRACE) {
2757                         System.out.println("TRACE: class_name_reference()");
2758                 }
2759                 if (token == TokenNameIdentifier) {
2760                         ref = new SingleTypeReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2761                         getNextToken();
2762                 } else {
2763                         ref = null;
2764                         dynamic_class_name_reference();
2765                 }
2766                 return ref;
2767         }
2768
2769         private void dynamic_class_name_reference() {
2770                 // dynamic_class_name_reference:
2771                 // base_variable T_OBJECT_OPERATOR object_property
2772                 // dynamic_class_name_variable_properties
2773                 // | base_variable
2774                 if (Scanner.TRACE) {
2775                         System.out.println("TRACE: dynamic_class_name_reference()");
2776                 }
2777                 base_variable();
2778                 if (token == TokenNameMINUS_GREATER) {
2779                         getNextToken();
2780                         object_property();
2781                         dynamic_class_name_variable_properties();
2782                 }
2783         }
2784
2785         private void dynamic_class_name_variable_properties() {
2786                 // dynamic_class_name_variable_properties:
2787                 // dynamic_class_name_variable_properties
2788                 // dynamic_class_name_variable_property
2789                 // | /* empty */
2790                 if (Scanner.TRACE) {
2791                         System.out.println("TRACE: dynamic_class_name_variable_properties()");
2792                 }
2793                 while (token == TokenNameMINUS_GREATER) {
2794                         dynamic_class_name_variable_property();
2795                 }
2796         }
2797
2798         private void dynamic_class_name_variable_property() {
2799                 // dynamic_class_name_variable_property:
2800                 // T_OBJECT_OPERATOR object_property
2801                 if (Scanner.TRACE) {
2802                         System.out.println("TRACE: dynamic_class_name_variable_property()");
2803                 }
2804                 if (token == TokenNameMINUS_GREATER) {
2805                         getNextToken();
2806                         object_property();
2807                 }
2808         }
2809
2810         private void ctor_arguments() {
2811                 // ctor_arguments:
2812                 // /* empty */
2813                 // | '(' function_call_parameter_list ')'
2814                 if (token == TokenNameLPAREN) {
2815                         getNextToken();
2816                         if (token == TokenNameRPAREN) {
2817                                 getNextToken();
2818                                 return;
2819                         }
2820                         non_empty_function_call_parameter_list();
2821                         if (token != TokenNameRPAREN) {
2822                                 throwSyntaxError("')' expected in ctor_arguments.");
2823                         }
2824                         getNextToken();
2825                 }
2826         }
2827
2828         private void assignment_list() {
2829                 // assignment_list:
2830                 // assignment_list ',' assignment_list_element
2831                 // | assignment_list_element
2832                 while (true) {
2833                         assignment_list_element();
2834                         if (token != TokenNameCOMMA) {
2835                                 break;
2836                         }
2837                         getNextToken();
2838                 }
2839         }
2840
2841         private void assignment_list_element() {
2842                 // assignment_list_element:
2843                 // variable
2844                 // | T_LIST '(' assignment_list ')'
2845                 // | /* empty */
2846                 if (token == TokenNameVariable) {
2847                         variable(true, false);
2848                 } else if (token == TokenNameDOLLAR) {
2849                         variable(false, false);
2850                 } else {
2851                         if (token == TokenNamelist) {
2852                                 getNextToken();
2853                                 if (token == TokenNameLPAREN) {
2854                                         getNextToken();
2855                                         assignment_list();
2856                                         if (token != TokenNameRPAREN) {
2857                                                 throwSyntaxError("')' expected after 'list' keyword.");
2858                                         }
2859                                         getNextToken();
2860                                 } else {
2861                                         throwSyntaxError("'(' expected after 'list' keyword.");
2862                                 }
2863                         }
2864                 }
2865         }
2866
2867         private void array_pair_list() {
2868                 // array_pair_list:
2869                 // /* empty */
2870                 // | non_empty_array_pair_list possible_comma
2871                 non_empty_array_pair_list();
2872                 if (token == TokenNameCOMMA) {
2873                         getNextToken();
2874                 }
2875         }
2876
2877         private void non_empty_array_pair_list() {
2878                 // non_empty_array_pair_list:
2879                 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2880                 // | non_empty_array_pair_list ',' expr
2881                 // | expr T_DOUBLE_ARROW expr
2882                 // | expr
2883                 // | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2884                 // | non_empty_array_pair_list ',' '&' w_variable
2885                 // | expr T_DOUBLE_ARROW '&' w_variable
2886                 // | '&' w_variable
2887                 while (true) {
2888                         if (token == TokenNameAND) {
2889                                 getNextToken();
2890                                 variable(true, false);
2891                         } else {
2892                                 expr();
2893                                 if (token == TokenNameAND) {
2894                                         getNextToken();
2895                                         variable(true, false);
2896                                 } else if (token == TokenNameEQUAL_GREATER) {
2897                                         getNextToken();
2898                                         if (token == TokenNameAND) {
2899                                                 getNextToken();
2900                                                 variable(true, false);
2901                                         } else {
2902                                                 expr();
2903                                         }
2904                                 }
2905                         }
2906                         if (token != TokenNameCOMMA) {
2907                                 return;
2908                         }
2909                         getNextToken();
2910                         if (token == TokenNameRPAREN) {
2911                                 return;
2912                         }
2913                 }
2914         }
2915
2916         // private void variableList() {
2917         // do {
2918         // variable();
2919         // if (token == TokenNameCOMMA) {
2920         // getNextToken();
2921         // } else {
2922         // break;
2923         // }
2924         // } while (true);
2925         // }
2926         private Expression variable_without_objects(boolean lefthandside, boolean ignoreVar) {
2927                 // variable_without_objects:
2928                 // reference_variable
2929                 // | simple_indirect_reference reference_variable
2930                 if (Scanner.TRACE) {
2931                         System.out.println("TRACE: variable_without_objects()");
2932                 }
2933                 while (token == TokenNameDOLLAR) {
2934                         getNextToken();
2935                 }
2936                 return reference_variable(lefthandside, ignoreVar);
2937         }
2938
2939         private Expression function_call(boolean lefthandside, boolean ignoreVar) {
2940                 // function_call:
2941                 // T_STRING '(' function_call_parameter_list ')'
2942                 // | class_constant '(' function_call_parameter_list ')'
2943                 // | static_member '(' function_call_parameter_list ')'
2944                 // | variable_without_objects '(' function_call_parameter_list ')'
2945                 char[] defineName = null;
2946                 char[] ident = null;
2947                 int startPos = 0;
2948                 int endPos = 0;
2949                 Expression ref = null;
2950                 if (Scanner.TRACE) {
2951                         System.out.println("TRACE: function_call()");
2952                 }
2953                 if (token == TokenNameIdentifier) {
2954                         ident = scanner.getCurrentIdentifierSource();
2955                         defineName = ident;
2956                         startPos = scanner.getCurrentTokenStartPosition();
2957                         endPos = scanner.getCurrentTokenEndPosition();
2958                         getNextToken();
2959                         switch (token) {
2960                         case TokenNamePAAMAYIM_NEKUDOTAYIM:
2961                                 // static member:
2962                                 defineName = null;
2963                                 getNextToken();
2964                                 if (token == TokenNameIdentifier) {
2965                                         // class _constant
2966                                         getNextToken();
2967                                 } else {
2968                                         // static member:
2969                                         variable_without_objects(true, false);
2970                                 }
2971                                 break;
2972                         }
2973                 } else {
2974                         ref = variable_without_objects(lefthandside, ignoreVar);
2975                 }
2976                 if (token != TokenNameLPAREN) {
2977                         if (defineName != null) {
2978                                 // does this identifier contain only uppercase characters?
2979                                 if (defineName.length == 3) {
2980                                         if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2981                                                 defineName = null;
2982                                         }
2983                                 } else if (defineName.length == 4) {
2984                                         if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2985                                                 defineName = null;
2986                                         } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
2987                                                 defineName = null;
2988                                         }
2989                                 } else if (defineName.length == 5) {
2990                                         if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
2991                                                 defineName = null;
2992                                         }
2993                                 }
2994                                 if (defineName != null) {
2995                                         for (int i = 0; i < defineName.length; i++) {
2996                                                 if (Character.isLowerCase(defineName[i])) {
2997                                                         problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
2998                                                         break;
2999                                                 }
3000                                         }
3001                                 }
3002                         }
3003                         // TODO is this ok ?
3004                         return ref;
3005                         // throwSyntaxError("'(' expected in function call.");
3006                 }
3007                 getNextToken();
3008                 if (token == TokenNameRPAREN) {
3009                         getNextToken();
3010                         return ref;
3011                 }
3012                 non_empty_function_call_parameter_list();
3013                 if (token != TokenNameRPAREN) {
3014                         String functionName;
3015                         if (ident == null) {
3016                                 functionName = new String(" ");
3017                         } else {
3018                                 functionName = new String(ident);
3019                         }
3020                         throwSyntaxError("')' expected in function call (" + functionName + ").");
3021                 }
3022                 getNextToken();
3023                 return ref;
3024         }
3025
3026         // private void function_call_parameter_list() {
3027         // function_call_parameter_list:
3028         // non_empty_function_call_parameter_list { $$ = $1; }
3029         // | /* empty */
3030         // }
3031         private void non_empty_function_call_parameter_list() {
3032                 // non_empty_function_call_parameter_list:
3033                 // expr_without_variable
3034                 // | variable
3035                 // | '&' w_variable
3036                 // | non_empty_function_call_parameter_list ',' expr_without_variable
3037                 // | non_empty_function_call_parameter_list ',' variable
3038                 // | non_empty_function_call_parameter_list ',' '&' w_variable
3039                 if (Scanner.TRACE) {
3040                         System.out.println("TRACE: non_empty_function_call_parameter_list()");
3041                 }
3042                 while (true) {
3043                         if (token == TokenNameAND) {
3044                                 getNextToken();
3045                                 w_variable(true);
3046                         } else {
3047                                 // if (token == TokenNameIdentifier || token ==
3048                                 // TokenNameVariable
3049                                 // || token == TokenNameDOLLAR) {
3050                                 // variable();
3051                                 // } else {
3052                                 expr_without_variable(true);
3053                                 // }
3054                         }
3055                         if (token != TokenNameCOMMA) {
3056                                 break;
3057                         }
3058                         getNextToken();
3059                 }
3060         }
3061
3062         private void fully_qualified_class_name() {
3063                 if (token == TokenNameIdentifier) {
3064                         getNextToken();
3065                 } else {
3066                         throwSyntaxError("Class name expected.");
3067                 }
3068         }
3069
3070         private void static_member() {
3071                 // static_member:
3072                 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
3073                 // variable_without_objects
3074                 if (Scanner.TRACE) {
3075                         System.out.println("TRACE: static_member()");
3076                 }
3077                 fully_qualified_class_name();
3078                 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
3079                         throwSyntaxError("'::' expected after class name (static_member).");
3080                 }
3081                 getNextToken();
3082                 variable_without_objects(false, false);
3083         }
3084
3085         private Expression base_variable_with_function_calls(boolean lefthandside, boolean ignoreVar) {
3086                 // base_variable_with_function_calls:
3087                 // base_variable
3088                 // | function_call
3089                 if (Scanner.TRACE) {
3090                         System.out.println("TRACE: base_variable_with_function_calls()");
3091                 }
3092                 return function_call(lefthandside, ignoreVar);
3093         }
3094
3095         private Expression base_variable() {
3096                 // base_variable:
3097                 // reference_variable
3098                 // | simple_indirect_reference reference_variable
3099                 // | static_member
3100                 Expression ref = null;
3101                 if (Scanner.TRACE) {
3102                         System.out.println("TRACE: base_variable()");
3103                 }
3104                 if (token == TokenNameIdentifier) {
3105                         static_member();
3106                 } else {
3107                         while (token == TokenNameDOLLAR) {
3108                                 getNextToken();
3109                         }
3110                         reference_variable(false, false);
3111                 }
3112                 return ref;
3113         }
3114
3115         // private void simple_indirect_reference() {
3116         // // simple_indirect_reference:
3117         // // '$'
3118         // //| simple_indirect_reference '$'
3119         // }
3120         private Expression reference_variable(boolean lefthandside, boolean ignoreVar) {
3121                 // reference_variable:
3122                 // reference_variable '[' dim_offset ']'
3123                 // | reference_variable '{' expr '}'
3124                 // | compound_variable
3125                 Expression ref = null;
3126                 if (Scanner.TRACE) {
3127                         System.out.println("TRACE: reference_variable()");
3128                 }
3129                 ref = compound_variable(lefthandside, ignoreVar);
3130                 while (true) {
3131                         if (token == TokenNameLBRACE) {
3132                                 ref = null;
3133                                 getNextToken();
3134                                 expr();
3135                                 if (token != TokenNameRBRACE) {
3136                                         throwSyntaxError("'}' expected in reference variable.");
3137                                 }
3138                                 getNextToken();
3139                         } else if (token == TokenNameLBRACKET) {
3140                                 if (ref != null && ref instanceof FieldReference) {
3141                                         FieldReference fref = (FieldReference) ref;
3142                                         addVariableSet(fref.token);
3143                                 }
3144                                 ref = null;
3145                                 getNextToken();
3146                                 if (token != TokenNameRBRACKET) {
3147                                         expr();
3148                                         // dim_offset();
3149                                         if (token != TokenNameRBRACKET) {
3150                                                 throwSyntaxError("']' expected in reference variable.");
3151                                         }
3152                                 }
3153                                 getNextToken();
3154                         } else {
3155                                 break;
3156                         }
3157                 }
3158                 return ref;
3159         }
3160
3161         private Expression compound_variable(boolean lefthandside, boolean ignoreVar) {
3162                 // compound_variable:
3163                 // T_VARIABLE
3164                 // | '$' '{' expr '}'
3165                 if (Scanner.TRACE) {
3166                         System.out.println("TRACE: compound_variable()");
3167                 }
3168                 if (token == TokenNameVariable) {
3169                         if (!lefthandside) {
3170                                 if (!containsVariableSet()) {
3171                                         // reportSyntaxError("The local variable " + new
3172                                         // String(scanner.getCurrentIdentifierSource())
3173                                         // + " may not have been initialized");
3174                                         problemReporter.uninitializedLocalVariable(new String(scanner.getCurrentIdentifierSource()), scanner
3175                                                         .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), referenceContext,
3176                                                         compilationUnit.compilationResult);
3177                                 }
3178                         } else {
3179                                 if (!ignoreVar) {
3180                                         addVariableSet();
3181                                 }
3182                         }
3183                         FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
3184                         getNextToken();
3185                         return ref;
3186                 } else {
3187                         // because of simple_indirect_reference
3188                         while (token == TokenNameDOLLAR) {
3189                                 getNextToken();
3190                         }
3191                         if (token != TokenNameLBRACE) {
3192                                 reportSyntaxError("'{' expected after compound variable token '$'.");
3193                                 return null;
3194                         }
3195                         getNextToken();
3196                         expr();
3197                         if (token != TokenNameRBRACE) {
3198                                 throwSyntaxError("'}' expected after compound variable token '$'.");
3199                         }
3200                         getNextToken();
3201                 }
3202                 return null;
3203         } // private void dim_offset() { // // dim_offset: // // /* empty */
3204
3205         // // | expr
3206         // expr();
3207         // }
3208         private void object_property() {
3209                 // object_property:
3210                 // object_dim_list
3211                 // | variable_without_objects
3212                 if (Scanner.TRACE) {
3213                         System.out.println("TRACE: object_property()");
3214                 }
3215                 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
3216                         variable_without_objects(false, false);
3217                 } else {
3218                         object_dim_list();
3219                 }
3220         }
3221
3222         private void object_dim_list() {
3223                 // object_dim_list:
3224                 // object_dim_list '[' dim_offset ']'
3225                 // | object_dim_list '{' expr '}'
3226                 // | variable_name
3227                 if (Scanner.TRACE) {
3228                         System.out.println("TRACE: object_dim_list()");
3229                 }
3230                 variable_name();
3231                 while (true) {
3232                         if (token == TokenNameLBRACE) {
3233                                 getNextToken();
3234                                 expr();
3235                                 if (token != TokenNameRBRACE) {
3236                                         throwSyntaxError("'}' expected in object_dim_list.");
3237                                 }
3238                                 getNextToken();
3239                         } else if (token == TokenNameLBRACKET) {
3240                                 getNextToken();
3241                                 if (token == TokenNameRBRACKET) {
3242                                         getNextToken();
3243                                         continue;
3244                                 }
3245                                 expr();
3246                                 if (token != TokenNameRBRACKET) {
3247                                         throwSyntaxError("']' expected in object_dim_list.");
3248                                 }
3249                                 getNextToken();
3250                         } else {
3251                                 break;
3252                         }
3253                 }
3254         }
3255
3256         private void variable_name() {
3257                 // variable_name:
3258                 // T_STRING
3259                 // | '{' expr '}'
3260                 if (Scanner.TRACE) {
3261                         System.out.println("TRACE: variable_name()");
3262                 }
3263                 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3264                         if (token > TokenNameKEYWORD) {
3265                                 // TODO show a warning "Keyword used as variable" ?
3266                         }
3267                         getNextToken();
3268                 } else {
3269                         if (token != TokenNameLBRACE) {
3270                                 throwSyntaxError("'{' expected in variable name.");
3271                         }
3272                         getNextToken();
3273                         expr();
3274                         if (token != TokenNameRBRACE) {
3275                                 throwSyntaxError("'}' expected in variable name.");
3276                         }
3277                         getNextToken();
3278                 }
3279         }
3280
3281         private void r_variable() {
3282                 variable(false, false);
3283         }
3284
3285         private void w_variable(boolean lefthandside) {
3286                 variable(lefthandside, false);
3287         }
3288
3289         private void rw_variable() {
3290                 variable(false, false);
3291         }
3292
3293         private Expression variable(boolean lefthandside, boolean ignoreVar) {
3294                 // variable:
3295                 // base_variable_with_function_calls T_OBJECT_OPERATOR
3296                 // object_property method_or_not variable_properties
3297                 // | base_variable_with_function_calls
3298                 Expression ref = base_variable_with_function_calls(lefthandside, ignoreVar);
3299                 if (token == TokenNameMINUS_GREATER) {
3300                         ref = null;
3301                         getNextToken();
3302                         object_property();
3303                         method_or_not();
3304                         variable_properties();
3305                 }
3306                 return ref;
3307         }
3308
3309         private void variable_properties() {
3310                 // variable_properties:
3311                 // variable_properties variable_property
3312                 // | /* empty */
3313                 while (token == TokenNameMINUS_GREATER) {
3314                         variable_property();
3315                 }
3316         }
3317
3318         private void variable_property() {
3319                 // variable_property:
3320                 // T_OBJECT_OPERATOR object_property method_or_not
3321                 if (Scanner.TRACE) {
3322                         System.out.println("TRACE: variable_property()");
3323                 }
3324                 if (token == TokenNameMINUS_GREATER) {
3325                         getNextToken();
3326                         object_property();
3327                         method_or_not();
3328                 } else {
3329                         throwSyntaxError("'->' expected in variable_property.");
3330                 }
3331         }
3332
3333         private void method_or_not() {
3334                 // method_or_not:
3335                 // '(' function_call_parameter_list ')'
3336                 // | /* empty */
3337                 if (Scanner.TRACE) {
3338                         System.out.println("TRACE: method_or_not()");
3339                 }
3340                 if (token == TokenNameLPAREN) {
3341                         getNextToken();
3342                         if (token == TokenNameRPAREN) {
3343                                 getNextToken();
3344                                 return;
3345                         }
3346                         non_empty_function_call_parameter_list();
3347                         if (token != TokenNameRPAREN) {
3348                                 throwSyntaxError("')' expected in method_or_not.");
3349                         }
3350                         getNextToken();
3351                 }
3352         }
3353
3354         private void exit_expr() {
3355                 // /* empty */
3356                 // | '(' ')'
3357                 // | '(' expr ')'
3358                 if (token != TokenNameLPAREN) {
3359                         return;
3360                 }
3361                 getNextToken();
3362                 if (token == TokenNameRPAREN) {
3363                         getNextToken();
3364                         return;
3365                 }
3366                 expr();
3367                 if (token != TokenNameRPAREN) {
3368                         throwSyntaxError("')' expected after keyword 'exit'");
3369                 }
3370                 getNextToken();
3371         }
3372
3373         // private void encaps_list() {
3374         // // encaps_list encaps_var
3375         // // | encaps_list T_STRING
3376         // // | encaps_list T_NUM_STRING
3377         // // | encaps_list T_ENCAPSED_AND_WHITESPACE
3378         // // | encaps_list T_CHARACTER
3379         // // | encaps_list T_BAD_CHARACTER
3380         // // | encaps_list '['
3381         // // | encaps_list ']'
3382         // // | encaps_list '{'
3383         // // | encaps_list '}'
3384         // // | encaps_list T_OBJECT_OPERATOR
3385         // // | /* empty */
3386         // while (true) {
3387         // switch (token) {
3388         // case TokenNameSTRING:
3389         // getNextToken();
3390         // break;
3391         // case TokenNameLBRACE:
3392         // // scanner.encapsedStringStack.pop();
3393         // getNextToken();
3394         // break;
3395         // case TokenNameRBRACE:
3396         // // scanner.encapsedStringStack.pop();
3397         // getNextToken();
3398         // break;
3399         // case TokenNameLBRACKET:
3400         // // scanner.encapsedStringStack.pop();
3401         // getNextToken();
3402         // break;
3403         // case TokenNameRBRACKET:
3404         // // scanner.encapsedStringStack.pop();
3405         // getNextToken();
3406         // break;
3407         // case TokenNameMINUS_GREATER:
3408         // // scanner.encapsedStringStack.pop();
3409         // getNextToken();
3410         // break;
3411         // case TokenNameVariable:
3412         // case TokenNameDOLLAR_LBRACE:
3413         // case TokenNameLBRACE_DOLLAR:
3414         // encaps_var();
3415         // break;
3416         // default:
3417         // char encapsedChar = ((Character)
3418         // scanner.encapsedStringStack.peek()).charValue();
3419         // if (encapsedChar == '$') {
3420         // scanner.encapsedStringStack.pop();
3421         // encapsedChar = ((Character)
3422         // scanner.encapsedStringStack.peek()).charValue();
3423         // switch (encapsedChar) {
3424         // case '`':
3425         // if (token == TokenNameEncapsedString0) {
3426         // return;
3427         // }
3428         // token = TokenNameSTRING;
3429         // continue;
3430         // case '\'':
3431         // if (token == TokenNameEncapsedString1) {
3432         // return;
3433         // }
3434         // token = TokenNameSTRING;
3435         // continue;
3436         // case '"':
3437         // if (token == TokenNameEncapsedString2) {
3438         // return;
3439         // }
3440         // token = TokenNameSTRING;
3441         // continue;
3442         // }
3443         // }
3444         // return;
3445         // }
3446         // }
3447         // }
3448
3449         // private void encaps_var() {
3450         // // T_VARIABLE
3451         // // | T_VARIABLE '[' encaps_var_offset ']'
3452         // // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3453         // // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3454         // // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3455         // // | T_CURLY_OPEN variable '}'
3456         // switch (token) {
3457         // case TokenNameVariable:
3458         // getNextToken();
3459         // if (token == TokenNameLBRACKET) {
3460         // getNextToken();
3461         // expr(); //encaps_var_offset();
3462         // if (token != TokenNameRBRACKET) {
3463         // throwSyntaxError("']' expected after variable.");
3464         // }
3465         // // scanner.encapsedStringStack.pop();
3466         // getNextToken();
3467         // // }
3468         // } else if (token == TokenNameMINUS_GREATER) {
3469         // getNextToken();
3470         // if (token != TokenNameIdentifier) {
3471         // throwSyntaxError("Identifier expected after '->'.");
3472         // }
3473         // // scanner.encapsedStringStack.pop();
3474         // getNextToken();
3475         // }
3476         // // else {
3477         // // // scanner.encapsedStringStack.pop();
3478         // // int tempToken = TokenNameSTRING;
3479         // // if (!scanner.encapsedStringStack.isEmpty()
3480         // // && (token == TokenNameEncapsedString0
3481         // // || token == TokenNameEncapsedString1
3482         // // || token == TokenNameEncapsedString2 || token ==
3483         // // TokenNameERROR)) {
3484         // // char encapsedChar = ((Character)
3485         // // scanner.encapsedStringStack.peek())
3486         // // .charValue();
3487         // // switch (token) {
3488         // // case TokenNameEncapsedString0 :
3489         // // if (encapsedChar == '`') {
3490         // // tempToken = TokenNameEncapsedString0;
3491         // // }
3492         // // break;
3493         // // case TokenNameEncapsedString1 :
3494         // // if (encapsedChar == '\'') {
3495         // // tempToken = TokenNameEncapsedString1;
3496         // // }
3497         // // break;
3498         // // case TokenNameEncapsedString2 :
3499         // // if (encapsedChar == '"') {
3500         // // tempToken = TokenNameEncapsedString2;
3501         // // }
3502         // // break;
3503         // // case TokenNameERROR :
3504         // // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3505         // // scanner.currentPosition--;
3506         // // getNextToken();
3507         // // }
3508         // // break;
3509         // // }
3510         // // }
3511         // // token = tempToken;
3512         // // }
3513         // break;
3514         // case TokenNameDOLLAR_LBRACE:
3515         // getNextToken();
3516         // if (token == TokenNameDOLLAR_LBRACE) {
3517         // encaps_var();
3518         // } else if (token == TokenNameIdentifier) {
3519         // getNextToken();
3520         // if (token == TokenNameLBRACKET) {
3521         // getNextToken();
3522         // // if (token == TokenNameRBRACKET) {
3523         // // getNextToken();
3524         // // } else {
3525         // expr();
3526         // if (token != TokenNameRBRACKET) {
3527         // throwSyntaxError("']' expected after '${'.");
3528         // }
3529         // getNextToken();
3530         // // }
3531         // }
3532         // } else {
3533         // expr();
3534         // }
3535         // if (token != TokenNameRBRACE) {
3536         // throwSyntaxError("'}' expected.");
3537         // }
3538         // getNextToken();
3539         // break;
3540         // case TokenNameLBRACE_DOLLAR:
3541         // getNextToken();
3542         // if (token == TokenNameLBRACE_DOLLAR) {
3543         // encaps_var();
3544         // } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3545         // getNextToken();
3546         // if (token == TokenNameLBRACKET) {
3547         // getNextToken();
3548         // // if (token == TokenNameRBRACKET) {
3549         // // getNextToken();
3550         // // } else {
3551         // expr();
3552         // if (token != TokenNameRBRACKET) {
3553         // throwSyntaxError("']' expected.");
3554         // }
3555         // getNextToken();
3556         // // }
3557         // } else if (token == TokenNameMINUS_GREATER) {
3558         // getNextToken();
3559         // if (token != TokenNameIdentifier && token != TokenNameVariable) {
3560         // throwSyntaxError("String or Variable token expected.");
3561         // }
3562         // getNextToken();
3563         // if (token == TokenNameLBRACKET) {
3564         // getNextToken();
3565         // // if (token == TokenNameRBRACKET) {
3566         // // getNextToken();
3567         // // } else {
3568         // expr();
3569         // if (token != TokenNameRBRACKET) {
3570         // throwSyntaxError("']' expected after '${'.");
3571         // }
3572         // getNextToken();
3573         // // }
3574         // }
3575         // }
3576         // // if (token != TokenNameRBRACE) {
3577         // // throwSyntaxError("'}' expected after '{$'.");
3578         // // }
3579         // // // scanner.encapsedStringStack.pop();
3580         // // getNextToken();
3581         // } else {
3582         // expr();
3583         // if (token != TokenNameRBRACE) {
3584         // throwSyntaxError("'}' expected.");
3585         // }
3586         // // scanner.encapsedStringStack.pop();
3587         // getNextToken();
3588         // }
3589         // break;
3590         // }
3591         // }
3592
3593         // private void encaps_var_offset() {
3594         // // T_STRING
3595         // // | T_NUM_STRING
3596         // // | T_VARIABLE
3597         // switch (token) {
3598         // case TokenNameSTRING:
3599         // getNextToken();
3600         // break;
3601         // case TokenNameIntegerLiteral:
3602         // getNextToken();
3603         // break;
3604         // case TokenNameVariable:
3605         // getNextToken();
3606         // break;
3607         // case TokenNameIdentifier:
3608         // getNextToken();
3609         // break;
3610         // default:
3611         // throwSyntaxError("Variable or String token expected.");
3612         // break;
3613         // }
3614         // }
3615
3616         private void internal_functions_in_yacc() {
3617                 // int start = 0;
3618                 switch (token) {
3619                 // case TokenNameisset:
3620                 // // T_ISSET '(' isset_variables ')'
3621                 // getNextToken();
3622                 // if (token != TokenNameLPAREN) {
3623                 // throwSyntaxError("'(' expected after keyword 'isset'");
3624                 // }
3625                 // getNextToken();
3626                 // isset_variables();
3627                 // if (token != TokenNameRPAREN) {
3628                 // throwSyntaxError("')' expected after keyword 'isset'");
3629                 // }
3630                 // getNextToken();
3631                 // break;
3632                 // case TokenNameempty:
3633                 // // T_EMPTY '(' variable ')'
3634                 // getNextToken();
3635                 // if (token != TokenNameLPAREN) {
3636                 // throwSyntaxError("'(' expected after keyword 'empty'");
3637                 // }
3638                 // getNextToken();
3639                 // variable(false);
3640                 // if (token != TokenNameRPAREN) {
3641                 // throwSyntaxError("')' expected after keyword 'empty'");
3642                 // }
3643                 // getNextToken();
3644                 // break;
3645                 case TokenNameinclude:
3646                         // T_INCLUDE expr
3647                         checkFileName(token);
3648                         break;
3649                 case TokenNameinclude_once:
3650                         // T_INCLUDE_ONCE expr
3651                         checkFileName(token);
3652                         break;
3653                 case TokenNameeval:
3654                         // T_EVAL '(' expr ')'
3655                         getNextToken();
3656                         if (token != TokenNameLPAREN) {
3657                                 throwSyntaxError("'(' expected after keyword 'eval'");
3658                         }
3659                         getNextToken();
3660                         expr();
3661                         if (token != TokenNameRPAREN) {
3662                                 throwSyntaxError("')' expected after keyword 'eval'");
3663                         }
3664                         getNextToken();
3665                         break;
3666                 case TokenNamerequire:
3667                         // T_REQUIRE expr
3668                         checkFileName(token);
3669                         break;
3670                 case TokenNamerequire_once:
3671                         // T_REQUIRE_ONCE expr
3672                         checkFileName(token);
3673                         break;
3674                 }
3675         }
3676
3677         /**
3678          * Parse and check the include file name
3679          * 
3680          * @param includeToken
3681          */
3682         private void checkFileName(int includeToken) {
3683                 // <include-token> expr
3684                 int start = scanner.getCurrentTokenStartPosition();
3685                 boolean hasLPAREN = false;
3686                 getNextToken();
3687                 if (token == TokenNameLPAREN) {
3688                         hasLPAREN = true;
3689                         getNextToken();
3690                 }
3691                 Expression expression = expr();
3692                 if (hasLPAREN) {
3693                         if (token == TokenNameRPAREN) {
3694                                 getNextToken();
3695                         } else {
3696                                 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3697                         }
3698                 }
3699                 char[] currTokenSource = scanner.getCurrentTokenSource(start);
3700                 IFile file = null;
3701                 if (scanner.compilationUnit != null) {
3702                         IResource resource = scanner.compilationUnit.getResource();
3703                         if (resource != null && resource instanceof IFile) {
3704                                 file = (IFile) resource;
3705                         }
3706                 }
3707                 char[][] tokens;
3708                 tokens = new char[1][];
3709                 tokens[0] = currTokenSource;
3710
3711                 ImportReference impt = new ImportReference(tokens, currTokenSource, start, scanner.getCurrentTokenEndPosition(), false);
3712                 impt.declarationSourceEnd = impt.sourceEnd;
3713                 impt.declarationEnd = impt.declarationSourceEnd;
3714                 // endPosition is just before the ;
3715                 impt.declarationSourceStart = start;
3716                 includesList.add(impt);
3717
3718                 if (expression instanceof StringLiteral) {
3719                         StringLiteral literal = (StringLiteral) expression;
3720                         char[] includeName = literal.source();
3721                         if (includeName.length == 0) {
3722                                 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3723                                                 literal.sourceStart + 1);
3724                         }
3725                         String includeNameString = new String(includeName);
3726                         if (literal instanceof StringLiteralDQ) {
3727                                 if (includeNameString.indexOf('$') >= 0) {
3728                                         // assuming that the filename contains a variable => no filename check
3729                                         return;
3730                                 }
3731                         }
3732                         if (includeNameString.startsWith("http://")) {
3733                                 // assuming external include location
3734                                 return;
3735                         }
3736                         if (file != null) {
3737                                 // check the filename:
3738                                 // System.out.println(new String(compilationUnit.getFileName())+" - "+
3739                                 // expression.toStringExpression());
3740                                 IProject project = file.getProject();
3741                                 if (project != null) {
3742                                         IPath path = PHPFileUtil.determineFilePath(includeNameString, file, project);
3743
3744                                         if (path == null) {
3745                                                 // SyntaxError: "File: << >> doesn't exist in project."
3746                                                 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3747                                                 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3748                                                                 compilationUnit.compilationResult);
3749                                         } else {
3750                                                 try {
3751                                                         String filePath = path.toString();
3752                                                         String ext = file.getRawLocation().getFileExtension();
3753                                                         int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
3754
3755                                                         impt.tokens = CharOperation.splitOn('/', filePath.toCharArray(), 0, filePath.length() - fileExtensionLength);
3756                                                         impt.setFile(PHPFileUtil.createFile(path, project));
3757                                                 } catch (Exception e) {
3758                                                         // the file is outside of the workspace
3759                                                 }
3760                                         }
3761                                 }
3762                         }
3763                 }
3764         }
3765
3766         private void isset_variables() {
3767                 // variable
3768                 // | isset_variables ','
3769                 if (token == TokenNameRPAREN) {
3770                         throwSyntaxError("Variable expected after keyword 'isset'");
3771                 }
3772                 while (true) {
3773                         variable(true, false);
3774                         if (token == TokenNameCOMMA) {
3775                                 getNextToken();
3776                         } else {
3777                                 break;
3778                         }
3779                 }
3780         }
3781
3782         private boolean common_scalar() {
3783                 // common_scalar:
3784                 // T_LNUMBER
3785                 // | T_DNUMBER
3786                 // | T_CONSTANT_ENCAPSED_STRING
3787                 // | T_LINE
3788                 // | T_FILE
3789                 // | T_CLASS_C
3790                 // | T_METHOD_C
3791                 // | T_FUNC_C
3792                 switch (token) {
3793                 case TokenNameIntegerLiteral:
3794                         getNextToken();
3795                         return true;
3796                 case TokenNameDoubleLiteral:
3797                         getNextToken();
3798                         return true;
3799                 case TokenNameStringDoubleQuote:
3800                         getNextToken();
3801                         return true;
3802                 case TokenNameStringSingleQuote:
3803                         getNextToken();
3804                         return true;
3805                 case TokenNameStringInterpolated:
3806                         getNextToken();
3807                         return true;
3808                 case TokenNameFILE:
3809                         getNextToken();
3810                         return true;
3811                 case TokenNameLINE:
3812                         getNextToken();
3813                         return true;
3814                 case TokenNameCLASS_C:
3815                         getNextToken();
3816                         return true;
3817                 case TokenNameMETHOD_C:
3818                         getNextToken();
3819                         return true;
3820                 case TokenNameFUNC_C:
3821                         getNextToken();
3822                         return true;
3823                 }
3824                 return false;
3825         }
3826
3827         private void scalar() {
3828                 // scalar:
3829                 // T_STRING
3830                 // | T_STRING_VARNAME
3831                 // | class_constant
3832                 // | common_scalar
3833                 // | '"' encaps_list '"'
3834                 // | '\'' encaps_list '\''
3835                 // | T_START_HEREDOC encaps_list T_END_HEREDOC
3836                 throwSyntaxError("Not yet implemented (scalar).");
3837         }
3838
3839         private void static_scalar() {
3840                 // static_scalar: /* compile-time evaluated scalars */
3841                 // common_scalar
3842                 // | T_STRING
3843                 // | '+' static_scalar
3844                 // | '-' static_scalar
3845                 // | T_ARRAY '(' static_array_pair_list ')'
3846                 // | static_class_constant
3847                 if (common_scalar()) {
3848                         return;
3849                 }
3850                 switch (token) {
3851                 case TokenNameIdentifier:
3852                         getNextToken();
3853                         // static_class_constant:
3854                         // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3855                         if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3856                                 getNextToken();
3857                                 if (token == TokenNameIdentifier) {
3858                                         getNextToken();
3859                                 } else {
3860                                         throwSyntaxError("Identifier expected after '::' operator.");
3861                                 }
3862                         }
3863                         break;
3864                 case TokenNameEncapsedString0:
3865                         try {
3866                                 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3867                                 while (scanner.currentCharacter != '`') {
3868                                         if (scanner.currentCharacter == '\\') {
3869                                                 scanner.currentPosition++;
3870                                         }
3871                                         scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3872                                 }
3873                                 getNextToken();
3874                         } catch (IndexOutOfBoundsException e) {
3875                                 throwSyntaxError("'`' expected at end of static string.");
3876                         }
3877                         break;
3878                 // case TokenNameEncapsedString1:
3879                 // try {
3880                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3881                 // while (scanner.currentCharacter != '\'') {
3882                 // if (scanner.currentCharacter == '\\') {
3883                 // scanner.currentPosition++;
3884                 // }
3885                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3886                 // }
3887                 // getNextToken();
3888                 // } catch (IndexOutOfBoundsException e) {
3889                 // throwSyntaxError("'\'' expected at end of static string.");
3890                 // }
3891                 // break;
3892                 // case TokenNameEncapsedString2:
3893                 // try {
3894                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3895                 // while (scanner.currentCharacter != '"') {
3896                 // if (scanner.currentCharacter == '\\') {
3897                 // scanner.currentPosition++;
3898                 // }
3899                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3900                 // }
3901                 // getNextToken();
3902                 // } catch (IndexOutOfBoundsException e) {
3903                 // throwSyntaxError("'\"' expected at end of static string.");
3904                 // }
3905                 // break;
3906                 case TokenNameStringSingleQuote:
3907                         getNextToken();
3908                         break;
3909                 case TokenNameStringDoubleQuote:
3910                         getNextToken();
3911                         break;
3912                 case TokenNamePLUS:
3913                         getNextToken();
3914                         static_scalar();
3915                         break;
3916                 case TokenNameMINUS:
3917                         getNextToken();
3918                         static_scalar();
3919                         break;
3920                 case TokenNamearray:
3921                         getNextToken();
3922                         if (token != TokenNameLPAREN) {
3923                                 throwSyntaxError("'(' expected after keyword 'array'");
3924                         }
3925                         getNextToken();
3926                         if (token == TokenNameRPAREN) {
3927                                 getNextToken();
3928                                 break;
3929                         }
3930                         non_empty_static_array_pair_list();
3931                         if (token != TokenNameRPAREN) {
3932                                 throwSyntaxError("')' or ',' expected after keyword 'array'");
3933                         }
3934                         getNextToken();
3935                         break;
3936                 // case TokenNamenull :
3937                 // getNextToken();
3938                 // break;
3939                 // case TokenNamefalse :
3940                 // getNextToken();
3941                 // break;
3942                 // case TokenNametrue :
3943                 // getNextToken();
3944                 // break;
3945                 default:
3946                         throwSyntaxError("Static scalar/constant expected.");
3947                 }
3948         }
3949
3950         private void non_empty_static_array_pair_list() {
3951                 // non_empty_static_array_pair_list:
3952                 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3953                 // static_scalar
3954                 // | non_empty_static_array_pair_list ',' static_scalar
3955                 // | static_scalar T_DOUBLE_ARROW static_scalar
3956                 // | static_scalar
3957                 while (true) {
3958                         static_scalar();
3959                         if (token == TokenNameEQUAL_GREATER) {
3960                                 getNextToken();
3961                                 static_scalar();
3962                         }
3963                         if (token != TokenNameCOMMA) {
3964                                 break;
3965                         }
3966                         getNextToken();
3967                         if (token == TokenNameRPAREN) {
3968                                 break;
3969                         }
3970                 }
3971         }
3972
3973         // public void reportSyntaxError() { //int act, int currentKind, int
3974         // // stateStackTop) {
3975         // /* remember current scanner position */
3976         // int startPos = scanner.startPosition;
3977         // int currentPos = scanner.currentPosition;
3978         //    
3979         // this.checkAndReportBracketAnomalies(problemReporter());
3980         // /* reset scanner where it was */
3981         // scanner.startPosition = startPos;
3982         // scanner.currentPosition = currentPos;
3983         // }
3984
3985         public static final int RoundBracket = 0;
3986
3987         public static final int SquareBracket = 1;
3988
3989         public static final int CurlyBracket = 2;
3990
3991         public static final int BracketKinds = 3;
3992
3993         protected int[] nestedMethod; // the ptr is nestedType
3994
3995         protected int nestedType, dimensions;
3996
3997         // variable set stack
3998         final static int VariableStackIncrement = 10;
3999
4000         HashMap fTypeVariables = null;
4001
4002         HashMap fMethodVariables = null;
4003
4004         ArrayList fStackUnassigned = new ArrayList();
4005
4006         // ast stack
4007         final static int AstStackIncrement = 100;
4008
4009         protected int astPtr;
4010
4011         protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
4012
4013         protected int astLengthPtr;
4014
4015         protected int[] astLengthStack;
4016
4017         ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
4018
4019         public CompilationUnitDeclaration compilationUnit; /*
4020                                                                                                                                                                                                                          * the result from parse()
4021                                                                                                                                                                                                                          */
4022
4023         protected ReferenceContext referenceContext;
4024
4025         protected ProblemReporter problemReporter;
4026
4027         protected CompilerOptions options;
4028
4029         private ArrayList includesList;
4030
4031         // protected CompilationResult compilationResult;
4032         /**
4033          * Returns this parser's problem reporter initialized with its reference
4034          * context. Also it is assumed that a problem is going to be reported, so
4035          * initializes the compilation result's line positions.
4036          */
4037         public ProblemReporter problemReporter() {
4038                 if (scanner.recordLineSeparator) {
4039                         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4040                 }
4041                 problemReporter.referenceContext = referenceContext;
4042                 return problemReporter;
4043         }
4044
4045         /*
4046          * Reconsider the entire source looking for inconsistencies in {} () []
4047          */
4048         // public boolean checkAndReportBracketAnomalies(ProblemReporter
4049         // problemReporter) {
4050         // scanner.wasAcr = false;
4051         // boolean anomaliesDetected = false;
4052         // try {
4053         // char[] source = scanner.source;
4054         // int[] leftCount = { 0, 0, 0 };
4055         // int[] rightCount = { 0, 0, 0 };
4056         // int[] depths = { 0, 0, 0 };
4057         // int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10]
4058         // };
4059         // int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
4060         // int[][] rightPositions = new int[][] { new int[10], new int[10], new
4061         // int[10] };
4062         // int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10]
4063         // };
4064         // scanner.currentPosition = scanner.initialPosition; //starting
4065         // // point
4066         // // (first-zero-based
4067         // // char)
4068         // while (scanner.currentPosition < scanner.eofPosition) { //loop for
4069         // // jumping
4070         // // over
4071         // // comments
4072         // try {
4073         // // ---------Consume white space and handles
4074         // // startPosition---------
4075         // boolean isWhiteSpace;
4076         // do {
4077         // scanner.startPosition = scanner.currentPosition;
4078         // // if (((scanner.currentCharacter =
4079         // // source[scanner.currentPosition++]) == '\\') &&
4080         // // (source[scanner.currentPosition] == 'u')) {
4081         // // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
4082         // // } else {
4083         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4084         // (scanner.currentCharacter == '\n'))) {
4085         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4086         // // only record line positions we have not
4087         // // recorded yet
4088         // scanner.pushLineSeparator();
4089         // }
4090         // }
4091         // isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
4092         // // }
4093         // } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
4094         // // -------consume token until } is found---------
4095         // switch (scanner.currentCharacter) {
4096         // case '{': {
4097         // int index = leftCount[CurlyBracket]++;
4098         // if (index == leftPositions[CurlyBracket].length) {
4099         // System.arraycopy(leftPositions[CurlyBracket], 0,
4100         // (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
4101         // System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] =
4102         // new int[index * 2]), 0, index);
4103         // }
4104         // leftPositions[CurlyBracket][index] = scanner.startPosition;
4105         // leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
4106         // }
4107         // break;
4108         // case '}': {
4109         // int index = rightCount[CurlyBracket]++;
4110         // if (index == rightPositions[CurlyBracket].length) {
4111         // System.arraycopy(rightPositions[CurlyBracket], 0,
4112         // (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
4113         // System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] =
4114         // new int[index * 2]), 0, index);
4115         // }
4116         // rightPositions[CurlyBracket][index] = scanner.startPosition;
4117         // rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4118         // }
4119         // break;
4120         // case '(': {
4121         // int index = leftCount[RoundBracket]++;
4122         // if (index == leftPositions[RoundBracket].length) {
4123         // System.arraycopy(leftPositions[RoundBracket], 0,
4124         // (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4125         // System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] =
4126         // new int[index * 2]), 0, index);
4127         // }
4128         // leftPositions[RoundBracket][index] = scanner.startPosition;
4129         // leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4130         // }
4131         // break;
4132         // case ')': {
4133         // int index = rightCount[RoundBracket]++;
4134         // if (index == rightPositions[RoundBracket].length) {
4135         // System.arraycopy(rightPositions[RoundBracket], 0,
4136         // (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4137         // System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] =
4138         // new int[index * 2]), 0, index);
4139         // }
4140         // rightPositions[RoundBracket][index] = scanner.startPosition;
4141         // rightDepths[RoundBracket][index] = --depths[RoundBracket];
4142         // }
4143         // break;
4144         // case '[': {
4145         // int index = leftCount[SquareBracket]++;
4146         // if (index == leftPositions[SquareBracket].length) {
4147         // System.arraycopy(leftPositions[SquareBracket], 0,
4148         // (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4149         // System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] =
4150         // new int[index * 2]), 0, index);
4151         // }
4152         // leftPositions[SquareBracket][index] = scanner.startPosition;
4153         // leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4154         // }
4155         // break;
4156         // case ']': {
4157         // int index = rightCount[SquareBracket]++;
4158         // if (index == rightPositions[SquareBracket].length) {
4159         // System.arraycopy(rightPositions[SquareBracket], 0,
4160         // (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4161         // System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket]
4162         // = new int[index * 2]), 0, index);
4163         // }
4164         // rightPositions[SquareBracket][index] = scanner.startPosition;
4165         // rightDepths[SquareBracket][index] = --depths[SquareBracket];
4166         // }
4167         // break;
4168         // case '\'': {
4169         // if (scanner.getNextChar('\\')) {
4170         // scanner.scanEscapeCharacter();
4171         // } else { // consume next character
4172         // scanner.unicodeAsBackSlash = false;
4173         // // if (((scanner.currentCharacter =
4174         // // source[scanner.currentPosition++]) ==
4175         // // '\\') &&
4176         // // (source[scanner.currentPosition] ==
4177         // // 'u')) {
4178         // // scanner.getNextUnicodeChar();
4179         // // } else {
4180         // if (scanner.withoutUnicodePtr != 0) {
4181         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4182         // scanner.currentCharacter;
4183         // }
4184         // // }
4185         // }
4186         // scanner.getNextChar('\'');
4187         // break;
4188         // }
4189         // case '"':
4190         // // consume next character
4191         // scanner.unicodeAsBackSlash = false;
4192         // // if (((scanner.currentCharacter =
4193         // // source[scanner.currentPosition++]) == '\\') &&
4194         // // (source[scanner.currentPosition] == 'u')) {
4195         // // scanner.getNextUnicodeChar();
4196         // // } else {
4197         // if (scanner.withoutUnicodePtr != 0) {
4198         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4199         // scanner.currentCharacter;
4200         // }
4201         // // }
4202         // while (scanner.currentCharacter != '"') {
4203         // if (scanner.currentCharacter == '\r') {
4204         // if (source[scanner.currentPosition] == '\n')
4205         // scanner.currentPosition++;
4206         // break; // the string cannot go further that
4207         // // the line
4208         // }
4209         // if (scanner.currentCharacter == '\n') {
4210         // break; // the string cannot go further that
4211         // // the line
4212         // }
4213         // if (scanner.currentCharacter == '\\') {
4214         // scanner.scanEscapeCharacter();
4215         // }
4216         // // consume next character
4217         // scanner.unicodeAsBackSlash = false;
4218         // // if (((scanner.currentCharacter =
4219         // // source[scanner.currentPosition++]) == '\\')
4220         // // && (source[scanner.currentPosition] == 'u'))
4221         // // {
4222         // // scanner.getNextUnicodeChar();
4223         // // } else {
4224         // if (scanner.withoutUnicodePtr != 0) {
4225         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4226         // scanner.currentCharacter;
4227         // }
4228         // // }
4229         // }
4230         // break;
4231         // case '/': {
4232         // int test;
4233         // if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4234         // // comment
4235         // //get the next char
4236         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4237         // '\\')
4238         // && (source[scanner.currentPosition] == 'u')) {
4239         // //-------------unicode traitement
4240         // // ------------
4241         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4242         // scanner.currentPosition++;
4243         // while (source[scanner.currentPosition] == 'u') {
4244         // scanner.currentPosition++;
4245         // }
4246         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4247         // 15 || c1 < 0
4248         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4249         // || c2 < 0
4250         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4251         // || c3 < 0
4252         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4253         // || c4 < 0) { //error
4254         // // don't
4255         // // care of the
4256         // // value
4257         // scanner.currentCharacter = 'A';
4258         // } //something different from \n and \r
4259         // else {
4260         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4261         // }
4262         // }
4263         // while (scanner.currentCharacter != '\r' && scanner.currentCharacter !=
4264         // '\n') {
4265         // //get the next char
4266         // scanner.startPosition = scanner.currentPosition;
4267         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4268         // '\\')
4269         // && (source[scanner.currentPosition] == 'u')) {
4270         // //-------------unicode traitement
4271         // // ------------
4272         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4273         // scanner.currentPosition++;
4274         // while (source[scanner.currentPosition] == 'u') {
4275         // scanner.currentPosition++;
4276         // }
4277         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4278         // 15 || c1 < 0
4279         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4280         // || c2 < 0
4281         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4282         // || c3 < 0
4283         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4284         // || c4 < 0) { //error
4285         // // don't
4286         // // care of the
4287         // // value
4288         // scanner.currentCharacter = 'A';
4289         // } //something different from \n
4290         // // and \r
4291         // else {
4292         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4293         // }
4294         // }
4295         // }
4296         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4297         // (scanner.currentCharacter == '\n'))) {
4298         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4299         // // only record line positions we
4300         // // have not recorded yet
4301         // scanner.pushLineSeparator();
4302         // if (this.scanner.taskTags != null) {
4303         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4304         // this.scanner
4305         // .getCurrentTokenEndPosition());
4306         // }
4307         // }
4308         // }
4309         // break;
4310         // }
4311         // if (test > 0) { //traditional and annotation
4312         // // comment
4313         // boolean star = false;
4314         // // consume next character
4315         // scanner.unicodeAsBackSlash = false;
4316         // // if (((scanner.currentCharacter =
4317         // // source[scanner.currentPosition++]) ==
4318         // // '\\') &&
4319         // // (source[scanner.currentPosition] ==
4320         // // 'u')) {
4321         // // scanner.getNextUnicodeChar();
4322         // // } else {
4323         // if (scanner.withoutUnicodePtr != 0) {
4324         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4325         // scanner.currentCharacter;
4326         // }
4327         // // }
4328         // if (scanner.currentCharacter == '*') {
4329         // star = true;
4330         // }
4331         // //get the next char
4332         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4333         // '\\')
4334         // && (source[scanner.currentPosition] == 'u')) {
4335         // //-------------unicode traitement
4336         // // ------------
4337         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4338         // scanner.currentPosition++;
4339         // while (source[scanner.currentPosition] == 'u') {
4340         // scanner.currentPosition++;
4341         // }
4342         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4343         // 15 || c1 < 0
4344         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4345         // || c2 < 0
4346         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4347         // || c3 < 0
4348         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4349         // || c4 < 0) { //error
4350         // // don't
4351         // // care of the
4352         // // value
4353         // scanner.currentCharacter = 'A';
4354         // } //something different from * and /
4355         // else {
4356         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4357         // }
4358         // }
4359         // //loop until end of comment */
4360         // while ((scanner.currentCharacter != '/') || (!star)) {
4361         // star = scanner.currentCharacter == '*';
4362         // //get next char
4363         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4364         // '\\')
4365         // && (source[scanner.currentPosition] == 'u')) {
4366         // //-------------unicode traitement
4367         // // ------------
4368         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4369         // scanner.currentPosition++;
4370         // while (source[scanner.currentPosition] == 'u') {
4371         // scanner.currentPosition++;
4372         // }
4373         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4374         // 15 || c1 < 0
4375         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4376         // || c2 < 0
4377         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4378         // || c3 < 0
4379         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4380         // || c4 < 0) { //error
4381         // // don't
4382         // // care of the
4383         // // value
4384         // scanner.currentCharacter = 'A';
4385         // } //something different from * and
4386         // // /
4387         // else {
4388         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4389         // }
4390         // }
4391         // }
4392         // if (this.scanner.taskTags != null) {
4393         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4394         // this.scanner.getCurrentTokenEndPosition());
4395         // }
4396         // break;
4397         // }
4398         // break;
4399         // }
4400         // default:
4401         // if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4402         // scanner.scanIdentifierOrKeyword(false);
4403         // break;
4404         // }
4405         // if (Character.isDigit(scanner.currentCharacter)) {
4406         // scanner.scanNumber(false);
4407         // break;
4408         // }
4409         // }
4410         // //-----------------end switch while
4411         // // try--------------------
4412         // } catch (IndexOutOfBoundsException e) {
4413         // break; // read until EOF
4414         // } catch (InvalidInputException e) {
4415         // return false; // no clue
4416         // }
4417         // }
4418         // if (scanner.recordLineSeparator) {
4419         // compilationUnit.compilationResult.lineSeparatorPositions =
4420         // scanner.getLineEnds();
4421         // }
4422         // // check placement anomalies against other kinds of brackets
4423         // for (int kind = 0; kind < BracketKinds; kind++) {
4424         // for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4425         // int start = leftPositions[kind][leftIndex]; // deepest
4426         // // first
4427         // // find matching closing bracket
4428         // int depth = leftDepths[kind][leftIndex];
4429         // int end = -1;
4430         // for (int i = 0; i < rightCount[kind]; i++) {
4431         // int pos = rightPositions[kind][i];
4432         // // want matching bracket further in source with same
4433         // // depth
4434         // if ((pos > start) && (depth == rightDepths[kind][i])) {
4435         // end = pos;
4436         // break;
4437         // }
4438         // }
4439         // if (end < 0) { // did not find a good closing match
4440         // problemReporter.unmatchedBracket(start, referenceContext,
4441         // compilationUnit.compilationResult);
4442         // return true;
4443         // }
4444         // // check if even number of opening/closing other brackets
4445         // // in between this pair of brackets
4446         // int balance = 0;
4447         // for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds);
4448         // otherKind++) {
4449         // for (int i = 0; i < leftCount[otherKind]; i++) {
4450         // int pos = leftPositions[otherKind][i];
4451         // if ((pos > start) && (pos < end))
4452         // balance++;
4453         // }
4454         // for (int i = 0; i < rightCount[otherKind]; i++) {
4455         // int pos = rightPositions[otherKind][i];
4456         // if ((pos > start) && (pos < end))
4457         // balance--;
4458         // }
4459         // if (balance != 0) {
4460         // problemReporter.unmatchedBracket(start, referenceContext,
4461         // compilationUnit.compilationResult); //bracket
4462         // // anomaly
4463         // return true;
4464         // }
4465         // }
4466         // }
4467         // // too many opening brackets ?
4468         // for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4469         // anomaliesDetected = true;
4470         // problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i -
4471         // 1], referenceContext,
4472         // compilationUnit.compilationResult);
4473         // }
4474         // // too many closing brackets ?
4475         // for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4476         // anomaliesDetected = true;
4477         // problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext,
4478         // compilationUnit.compilationResult);
4479         // }
4480         // if (anomaliesDetected)
4481         // return true;
4482         // }
4483         // return anomaliesDetected;
4484         // } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4485         // return anomaliesDetected;
4486         // } catch (NullPointerException e) { // jdk1.2.2 jit bug
4487         // return anomaliesDetected;
4488         // }
4489         // }
4490         protected void pushOnAstLengthStack(int pos) {
4491                 try {
4492                         astLengthStack[++astLengthPtr] = pos;
4493                 } catch (IndexOutOfBoundsException e) {
4494                         int oldStackLength = astLengthStack.length;
4495                         int[] oldPos = astLengthStack;
4496                         astLengthStack = new int[oldStackLength + StackIncrement];
4497                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4498                         astLengthStack[astLengthPtr] = pos;
4499                 }
4500         }
4501
4502         protected void pushOnAstStack(ASTNode node) {
4503                 /*
4504                  * add a new obj on top of the ast stack
4505                  */
4506                 try {
4507                         astStack[++astPtr] = node;
4508                 } catch (IndexOutOfBoundsException e) {
4509                         int oldStackLength = astStack.length;
4510                         ASTNode[] oldStack = astStack;
4511                         astStack = new ASTNode[oldStackLength + AstStackIncrement];
4512                         System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4513                         astPtr = oldStackLength;
4514                         astStack[astPtr] = node;
4515                 }
4516                 try {
4517                         astLengthStack[++astLengthPtr] = 1;
4518                 } catch (IndexOutOfBoundsException e) {
4519                         int oldStackLength = astLengthStack.length;
4520                         int[] oldPos = astLengthStack;
4521                         astLengthStack = new int[oldStackLength + AstStackIncrement];
4522                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4523                         astLengthStack[astLengthPtr] = 1;
4524                 }
4525         }
4526
4527         protected void resetModifiers() {
4528                 this.modifiers = AccDefault;
4529                 this.modifiersSourceStart = -1; // <-- see comment into
4530                 // modifiersFlag(int)
4531                 this.scanner.commentPtr = -1;
4532         }
4533
4534         protected void consumePackageDeclarationName(IFile file) {
4535                 // create a package name similar to java package names
4536                 String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4537                 String filePath = file.getRawLocation().toString();
4538                 String ext = file.getRawLocation().getFileExtension();
4539                 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4540                 ImportReference impt;
4541                 char[][] tokens;
4542                 if (filePath.startsWith(projectPath)) {
4543                         tokens = CharOperation
4544                                         .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4545                 } else {
4546                         String name = file.getName();
4547                         tokens = new char[1][];
4548                         tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4549                 }
4550
4551                 this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4552
4553                 impt.declarationSourceStart = 0;
4554                 impt.declarationSourceEnd = 0;
4555                 impt.declarationEnd = 0;
4556                 // endPosition is just before the ;
4557
4558         }
4559
4560         public final static String[] GLOBALS = { "$this", "$_COOKIE", "$_ENV", "$_FILES", "$_GET", "$GLOBALS", "$_POST", "$_REQUEST",
4561                         "$_SESSION", "$_SERVER" };
4562
4563         /**
4564          * 
4565          */
4566         private void pushFunctionVariableSet() {
4567                 HashSet set = new HashSet();
4568                 if (fStackUnassigned.isEmpty()) {
4569                         for (int i = 0; i < GLOBALS.length; i++) {
4570                                 set.add(GLOBALS[i]);
4571                         }
4572                 }
4573                 fStackUnassigned.add(set);
4574         }
4575
4576         private void pushIfVariableSet() {
4577                 if (!fStackUnassigned.isEmpty()) {
4578                         HashSet set = new HashSet();
4579                         fStackUnassigned.add(set);
4580                 }
4581         }
4582
4583         private HashSet removeIfVariableSet() {
4584                 if (!fStackUnassigned.isEmpty()) {
4585                         return (HashSet) fStackUnassigned.remove(fStackUnassigned.size() - 1);
4586                 }
4587                 return null;
4588         }
4589
4590         /**
4591          * Returns the <i>set of assigned variables </i> returns null if no Set is
4592          * defined at the current scanner position
4593          */
4594         private HashSet peekVariableSet() {
4595                 if (!fStackUnassigned.isEmpty()) {
4596                         return (HashSet) fStackUnassigned.get(fStackUnassigned.size() - 1);
4597                 }
4598                 return null;
4599         }
4600
4601         /**
4602          * add the current identifier source to the <i>set of assigned variables </i>
4603          * 
4604          * @param set
4605          */
4606         private void addVariableSet(HashSet set) {
4607                 if (set != null) {
4608                         set.add(new String(scanner.getCurrentTokenSource()));
4609                 }
4610         }
4611
4612         /**
4613          * add the current identifier source to the <i>set of assigned variables </i>
4614          * 
4615          */
4616         private void addVariableSet() {
4617                 HashSet set = peekVariableSet();
4618                 if (set != null) {
4619                         set.add(new String(scanner.getCurrentTokenSource()));
4620                 }
4621         }
4622
4623         /**
4624          * add the current identifier source to the <i>set of assigned variables </i>
4625          * 
4626          */
4627         private void addVariableSet(char[] token) {
4628                 HashSet set = peekVariableSet();
4629                 if (set != null) {
4630                         set.add(new String(token));
4631                 }
4632         }
4633
4634         /**
4635          * check if the current identifier source is in the <i>set of assigned
4636          * variables </i> Returns true, if no set is defined for the current scanner
4637          * position
4638          * 
4639          */
4640         private boolean containsVariableSet() {
4641                 return containsVariableSet(scanner.getCurrentTokenSource());
4642         }
4643
4644         private boolean containsVariableSet(char[] token) {
4645
4646                 if (!fStackUnassigned.isEmpty()) {
4647                         HashSet set;
4648                         String str = new String(token);
4649                         for (int i = 0; i < fStackUnassigned.size(); i++) {
4650                                 set = (HashSet) fStackUnassigned.get(i);
4651                                 if (set.contains(str)) {
4652                                         return true;
4653                                 }
4654                         }
4655                         return false;
4656                 }
4657                 return true;
4658         }
4659 }