Bugs item #1236682
[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) {
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                                         topSet.addAll(set);
1820                                         topSet.addAll(assignedVariableSet);
1821                                 }
1822                         }
1823                 }
1824                 if (token != TokenNameendif) {
1825                         throwSyntaxError("'endif' expected.");
1826                 }
1827                 getNextToken();
1828                 if (token != TokenNameSEMICOLON) {
1829                         reportSyntaxError("';' expected after if-statement.");
1830                         iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1831                 } else {
1832                         iState.sourceEnd = scanner.getCurrentTokenEndPosition();
1833                         getNextToken();
1834                 }
1835         }
1836
1837         private void ifStatement(IfStatement iState) {
1838                 // T_IF '(' expr ')' statement elseif_list else_single
1839                 HashSet assignedVariableSet = null;
1840                 try {
1841                         pushIfVariableSet();
1842                         Statement s = statement();
1843                         iState.thenStatement = s;
1844                         checkUnreachable(iState, s);
1845                 } finally {
1846                         assignedVariableSet = removeIfVariableSet();
1847                 }
1848
1849                 if (token == TokenNameelseif) {
1850                         try {
1851                                 pushIfVariableSet();
1852                                 elseif_list(iState);
1853                         } finally {
1854                                 HashSet set = removeIfVariableSet();
1855                                 if (assignedVariableSet != null) {
1856                                         assignedVariableSet.addAll(set);
1857                                 }
1858                         }
1859                 }
1860                 try {
1861                         pushIfVariableSet();
1862                         else_single(iState);
1863                 } finally {
1864                         HashSet set = removeIfVariableSet();
1865                         if (assignedVariableSet != null) {
1866                                 HashSet topSet = peekVariableSet();
1867                                 if (topSet != null) {
1868                                         topSet.addAll(set);
1869                                         topSet.addAll(assignedVariableSet);
1870                                 }
1871                         }
1872                 }
1873         }
1874
1875         private void elseif_list(IfStatement iState) {
1876                 // /* empty */
1877                 // | elseif_list T_ELSEIF '(' expr ')' statement
1878                 ArrayList conditionList = new ArrayList();
1879                 ArrayList statementList = new ArrayList();
1880                 Expression e;
1881                 Statement s;
1882                 while (token == TokenNameelseif) {
1883                         getNextToken();
1884                         if (token == TokenNameLPAREN) {
1885                                 getNextToken();
1886                         } else {
1887                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1888                         }
1889                         e = expr();
1890                         conditionList.add(e);
1891                         if (token == TokenNameRPAREN) {
1892                                 getNextToken();
1893                         } else {
1894                                 throwSyntaxError("')' expected after 'elseif' condition.");
1895                         }
1896                         s = statement();
1897                         statementList.add(s);
1898                         checkUnreachable(iState, s);
1899                 }
1900                 iState.elseifConditions = new Expression[conditionList.size()];
1901                 iState.elseifStatements = new Statement[statementList.size()];
1902                 conditionList.toArray(iState.elseifConditions);
1903                 statementList.toArray(iState.elseifStatements);
1904         }
1905
1906         private void new_elseif_list(IfStatement iState) {
1907                 // /* empty */
1908                 // | new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
1909                 ArrayList conditionList = new ArrayList();
1910                 ArrayList statementList = new ArrayList();
1911                 Expression e;
1912                 Block b;
1913                 while (token == TokenNameelseif) {
1914                         getNextToken();
1915                         if (token == TokenNameLPAREN) {
1916                                 getNextToken();
1917                         } else {
1918                                 throwSyntaxError("'(' expected after 'elseif' keyword.");
1919                         }
1920                         e = expr();
1921                         conditionList.add(e);
1922                         if (token == TokenNameRPAREN) {
1923                                 getNextToken();
1924                         } else {
1925                                 throwSyntaxError("')' expected after 'elseif' condition.");
1926                         }
1927                         if (token == TokenNameCOLON) {
1928                                 getNextToken();
1929                         } else {
1930                                 throwSyntaxError("':' expected after 'elseif' keyword.");
1931                         }
1932                         b = inner_statement_list();
1933                         statementList.add(b);
1934                         checkUnreachable(iState, b);
1935                 }
1936                 iState.elseifConditions = new Expression[conditionList.size()];
1937                 iState.elseifStatements = new Statement[statementList.size()];
1938                 conditionList.toArray(iState.elseifConditions);
1939                 statementList.toArray(iState.elseifStatements);
1940         }
1941
1942         private void else_single(IfStatement iState) {
1943                 // /* empty */
1944                 // T_ELSE statement
1945                 if (token == TokenNameelse) {
1946                         getNextToken();
1947                         Statement s = statement();
1948                         iState.elseStatement = s;
1949                         checkUnreachable(iState, s);
1950                 } else {
1951                         iState.checkUnreachable = false;
1952                 }
1953                 iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1954         }
1955
1956         private void new_else_single(IfStatement iState) {
1957                 // /* empty */
1958                 // | T_ELSE ':' inner_statement_list
1959                 if (token == TokenNameelse) {
1960                         getNextToken();
1961                         if (token == TokenNameCOLON) {
1962                                 getNextToken();
1963                         } else {
1964                                 throwSyntaxError("':' expected after 'else' keyword.");
1965                         }
1966                         Block b = inner_statement_list();
1967                         iState.elseStatement = b;
1968                         checkUnreachable(iState, b);
1969                 } else {
1970                         iState.checkUnreachable = false;
1971                 }
1972         }
1973
1974         private Block inner_statement_list() {
1975                 // inner_statement_list inner_statement
1976                 // /* empty */
1977                 return statementList();
1978         }
1979
1980         /**
1981          * @param iState
1982          * @param b
1983          */
1984         private void checkUnreachable(IfStatement iState, Statement s) {
1985                 if (s instanceof Block) {
1986                         Block b = (Block) s;
1987                         if (b.statements == null || b.statements.length == 0) {
1988                                 iState.checkUnreachable = false;
1989                         } else {
1990                                 int off = b.statements.length - 1;
1991                                 if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
1992                                                 && !(b.statements[off] instanceof BreakStatement)) {
1993                                         if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
1994                                                 iState.checkUnreachable = false;
1995                                         }
1996                                 }
1997                         }
1998                 } else {
1999                         if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
2000                                 if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
2001                                         iState.checkUnreachable = false;
2002                                 }
2003                         }
2004                 }
2005         }
2006
2007         // private void elseifStatementList() {
2008         // do {
2009         // elseifStatement();
2010         // switch (token) {
2011         // case TokenNameelse:
2012         // getNextToken();
2013         // if (token == TokenNameCOLON) {
2014         // getNextToken();
2015         // if (token != TokenNameendif) {
2016         // statementList();
2017         // }
2018         // return;
2019         // } else {
2020         // if (token == TokenNameif) { //'else if'
2021         // getNextToken();
2022         // } else {
2023         // throwSyntaxError("':' expected after 'else'.");
2024         // }
2025         // }
2026         // break;
2027         // case TokenNameelseif:
2028         // getNextToken();
2029         // break;
2030         // default:
2031         // return;
2032         // }
2033         // } while (true);
2034         // }
2035
2036         // private void elseifStatement() {
2037         // if (token == TokenNameLPAREN) {
2038         // getNextToken();
2039         // expr();
2040         // if (token != TokenNameRPAREN) {
2041         // throwSyntaxError("')' expected in else-if-statement.");
2042         // }
2043         // getNextToken();
2044         // if (token != TokenNameCOLON) {
2045         // throwSyntaxError("':' expected in else-if-statement.");
2046         // }
2047         // getNextToken();
2048         // if (token != TokenNameendif) {
2049         // statementList();
2050         // }
2051         // }
2052         // }
2053
2054         private void switchStatement() {
2055                 if (token == TokenNameCOLON) {
2056                         // ':' [labeled-statement-list] 'endswitch' ';'
2057                         getNextToken();
2058                         labeledStatementList();
2059                         if (token != TokenNameendswitch) {
2060                                 throwSyntaxError("'endswitch' expected.");
2061                         }
2062                         getNextToken();
2063                         if (token != TokenNameSEMICOLON) {
2064                                 throwSyntaxError("';' expected after switch-statement.");
2065                         }
2066                         getNextToken();
2067                 } else {
2068                         // '{' [labeled-statement-list] '}'
2069                         if (token != TokenNameLBRACE) {
2070                                 throwSyntaxError("'{' expected in switch statement.");
2071                         }
2072                         getNextToken();
2073                         if (token != TokenNameRBRACE) {
2074                                 labeledStatementList();
2075                         }
2076                         if (token != TokenNameRBRACE) {
2077                                 throwSyntaxError("'}' expected in switch statement.");
2078                         }
2079                         getNextToken();
2080                 }
2081         }
2082
2083         private void forStatement() {
2084                 if (token == TokenNameCOLON) {
2085                         getNextToken();
2086                         statementList();
2087                         if (token != TokenNameendfor) {
2088                                 throwSyntaxError("'endfor' expected.");
2089                         }
2090                         getNextToken();
2091                         if (token != TokenNameSEMICOLON) {
2092                                 throwSyntaxError("';' expected after for-statement.");
2093                         }
2094                         getNextToken();
2095                 } else {
2096                         statement();
2097                 }
2098         }
2099
2100         private void whileStatement() {
2101                 // ':' statement-list 'endwhile' ';'
2102                 if (token == TokenNameCOLON) {
2103                         getNextToken();
2104                         statementList();
2105                         if (token != TokenNameendwhile) {
2106                                 throwSyntaxError("'endwhile' expected.");
2107                         }
2108                         getNextToken();
2109                         if (token != TokenNameSEMICOLON) {
2110                                 throwSyntaxError("';' expected after while-statement.");
2111                         }
2112                         getNextToken();
2113                 } else {
2114                         statement();
2115                 }
2116         }
2117
2118         private void foreachStatement() {
2119                 if (token == TokenNameCOLON) {
2120                         getNextToken();
2121                         statementList();
2122                         if (token != TokenNameendforeach) {
2123                                 throwSyntaxError("'endforeach' expected.");
2124                         }
2125                         getNextToken();
2126                         if (token != TokenNameSEMICOLON) {
2127                                 throwSyntaxError("';' expected after foreach-statement.");
2128                         }
2129                         getNextToken();
2130                 } else {
2131                         statement();
2132                 }
2133         }
2134
2135         // private void exitStatus() {
2136         // if (token == TokenNameLPAREN) {
2137         // getNextToken();
2138         // } else {
2139         // throwSyntaxError("'(' expected in 'exit-status'.");
2140         // }
2141         // if (token != TokenNameRPAREN) {
2142         // expression();
2143         // }
2144         // if (token == TokenNameRPAREN) {
2145         // getNextToken();
2146         // } else {
2147         // throwSyntaxError("')' expected after 'exit-status'.");
2148         // }
2149         // }
2150         private void expressionList() {
2151                 do {
2152                         expr();
2153                         if (token == TokenNameCOMMA) {
2154                                 getNextToken();
2155                         } else {
2156                                 break;
2157                         }
2158                 } while (true);
2159         }
2160
2161         private Expression expr() {
2162                 // r_variable
2163                 // | expr_without_variable
2164                 // if (token!=TokenNameEOF) {
2165                 if (Scanner.TRACE) {
2166                         System.out.println("TRACE: expr()");
2167                 }
2168                 return expr_without_variable(true);
2169                 // }
2170         }
2171
2172         private Expression expr_without_variable(boolean only_variable) {
2173                 int exprSourceStart = scanner.getCurrentTokenStartPosition();
2174                 int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2175                 Expression expression = new Expression();
2176                 expression.sourceStart = exprSourceStart;
2177                 // default, may be overwritten
2178                 expression.sourceEnd = exprSourceEnd;
2179                 try {
2180                         // internal_functions_in_yacc
2181                         // | T_CLONE expr
2182                         // | T_PRINT expr
2183                         // | '(' expr ')'
2184                         // | '@' expr
2185                         // | '+' expr
2186                         // | '-' expr
2187                         // | '!' expr
2188                         // | '~' expr
2189                         // | T_INC rw_variable
2190                         // | T_DEC rw_variable
2191                         // | T_INT_CAST expr
2192                         // | T_DOUBLE_CAST expr
2193                         // | T_STRING_CAST expr
2194                         // | T_ARRAY_CAST expr
2195                         // | T_OBJECT_CAST expr
2196                         // | T_BOOL_CAST expr
2197                         // | T_UNSET_CAST expr
2198                         // | T_EXIT exit_expr
2199                         // | scalar
2200                         // | T_ARRAY '(' array_pair_list ')'
2201                         // | '`' encaps_list '`'
2202                         // | T_LIST '(' assignment_list ')' '=' expr
2203                         // | T_NEW class_name_reference ctor_arguments
2204                         // | variable '=' expr
2205                         // | variable '=' '&' variable
2206                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2207                         // | variable T_PLUS_EQUAL expr
2208                         // | variable T_MINUS_EQUAL expr
2209                         // | variable T_MUL_EQUAL expr
2210                         // | variable T_DIV_EQUAL expr
2211                         // | variable T_CONCAT_EQUAL expr
2212                         // | variable T_MOD_EQUAL expr
2213                         // | variable T_AND_EQUAL expr
2214                         // | variable T_OR_EQUAL expr
2215                         // | variable T_XOR_EQUAL expr
2216                         // | variable T_SL_EQUAL expr
2217                         // | variable T_SR_EQUAL expr
2218                         // | rw_variable T_INC
2219                         // | rw_variable T_DEC
2220                         // | expr T_BOOLEAN_OR expr
2221                         // | expr T_BOOLEAN_AND expr
2222                         // | expr T_LOGICAL_OR expr
2223                         // | expr T_LOGICAL_AND expr
2224                         // | expr T_LOGICAL_XOR expr
2225                         // | expr '|' expr
2226                         // | expr '&' expr
2227                         // | expr '^' expr
2228                         // | expr '.' expr
2229                         // | expr '+' expr
2230                         // | expr '-' expr
2231                         // | expr '*' expr
2232                         // | expr '/' expr
2233                         // | expr '%' expr
2234                         // | expr T_SL expr
2235                         // | expr T_SR expr
2236                         // | expr T_IS_IDENTICAL expr
2237                         // | expr T_IS_NOT_IDENTICAL expr
2238                         // | expr T_IS_EQUAL expr
2239                         // | expr T_IS_NOT_EQUAL expr
2240                         // | expr '<' expr
2241                         // | expr T_IS_SMALLER_OR_EQUAL expr
2242                         // | expr '>' expr
2243                         // | expr T_IS_GREATER_OR_EQUAL expr
2244                         // | expr T_INSTANCEOF class_name_reference
2245                         // | expr '?' expr ':' expr
2246                         if (Scanner.TRACE) {
2247                                 System.out.println("TRACE: expr_without_variable() PART 1");
2248                         }
2249                         switch (token) {
2250                         case TokenNameisset:
2251                                 // T_ISSET '(' isset_variables ')'
2252                                 getNextToken();
2253                                 if (token != TokenNameLPAREN) {
2254                                         throwSyntaxError("'(' expected after keyword 'isset'");
2255                                 }
2256                                 getNextToken();
2257                                 isset_variables();
2258                                 if (token != TokenNameRPAREN) {
2259                                         throwSyntaxError("')' expected after keyword 'isset'");
2260                                 }
2261                                 getNextToken();
2262                                 break;
2263                         case TokenNameempty:
2264                                 getNextToken();
2265                                 if (token != TokenNameLPAREN) {
2266                                         throwSyntaxError("'(' expected after keyword 'empty'");
2267                                 }
2268                                 getNextToken();
2269                                 variable(true, false);
2270                                 if (token != TokenNameRPAREN) {
2271                                         throwSyntaxError("')' expected after keyword 'empty'");
2272                                 }
2273                                 getNextToken();
2274                                 break;
2275                         case TokenNameeval:
2276                         case TokenNameinclude:
2277                         case TokenNameinclude_once:
2278                         case TokenNamerequire:
2279                         case TokenNamerequire_once:
2280                                 internal_functions_in_yacc();
2281                                 break;
2282                         // | '(' expr ')'
2283                         case TokenNameLPAREN:
2284                                 getNextToken();
2285                                 expr();
2286                                 if (token == TokenNameRPAREN) {
2287                                         getNextToken();
2288                                 } else {
2289                                         throwSyntaxError("')' expected in expression.");
2290                                 }
2291                                 break;
2292                         // | T_CLONE expr
2293                         // | T_PRINT expr
2294                         // | '@' expr
2295                         // | '+' expr
2296                         // | '-' expr
2297                         // | '!' expr
2298                         // | '~' expr
2299                         // | T_INT_CAST expr
2300                         // | T_DOUBLE_CAST expr
2301                         // | T_STRING_CAST expr
2302                         // | T_ARRAY_CAST expr
2303                         // | T_OBJECT_CAST expr
2304                         // | T_BOOL_CAST expr
2305                         // | T_UNSET_CAST expr
2306                         case TokenNameclone:
2307                         case TokenNameprint:
2308                         case TokenNameAT:
2309                         case TokenNamePLUS:
2310                         case TokenNameMINUS:
2311                         case TokenNameNOT:
2312                         case TokenNameTWIDDLE:
2313                         case TokenNameintCAST:
2314                         case TokenNamedoubleCAST:
2315                         case TokenNamestringCAST:
2316                         case TokenNamearrayCAST:
2317                         case TokenNameobjectCAST:
2318                         case TokenNameboolCAST:
2319                         case TokenNameunsetCAST:
2320                                 getNextToken();
2321                                 expr();
2322                                 break;
2323                         case TokenNameexit:
2324                                 getNextToken();
2325                                 exit_expr();
2326                                 break;
2327                         // scalar:
2328                         // T_STRING
2329                         // | T_STRING_VARNAME
2330                         // | class_constant
2331                         // | T_START_HEREDOC encaps_list T_END_HEREDOC
2332                         // | '`' encaps_list '`'
2333                         // | common_scalar
2334                         // | '`' encaps_list '`'
2335                         // case TokenNameEncapsedString0:
2336                         // scanner.encapsedStringStack.push(new Character('`'));
2337                         // getNextToken();
2338                         // try {
2339                         // if (token == TokenNameEncapsedString0) {
2340                         // } else {
2341                         // encaps_list();
2342                         // if (token != TokenNameEncapsedString0) {
2343                         // throwSyntaxError("\'`\' expected at end of string" + "(Found token: " +
2344                         // scanner.toStringAction(token) + " )");
2345                         // }
2346                         // }
2347                         // } finally {
2348                         // scanner.encapsedStringStack.pop();
2349                         // getNextToken();
2350                         // }
2351                         // break;
2352                         // // | '\'' encaps_list '\''
2353                         // case TokenNameEncapsedString1:
2354                         // scanner.encapsedStringStack.push(new Character('\''));
2355                         // getNextToken();
2356                         // try {
2357                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2358                         // if (token == TokenNameEncapsedString1) {
2359                         // expression = new
2360                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2361                         // exprSourceStart, scanner
2362                         // .getCurrentTokenEndPosition());
2363                         // } else {
2364                         // encaps_list();
2365                         // if (token != TokenNameEncapsedString1) {
2366                         // throwSyntaxError("\'\'\' expected at end of string" + "(Found token: "
2367                         // + scanner.toStringAction(token) + " )");
2368                         // } else {
2369                         // expression = new
2370                         // StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2371                         // exprSourceStart, scanner
2372                         // .getCurrentTokenEndPosition());
2373                         // }
2374                         // }
2375                         // } finally {
2376                         // scanner.encapsedStringStack.pop();
2377                         // getNextToken();
2378                         // }
2379                         // break;
2380                         // //| '"' encaps_list '"'
2381                         // case TokenNameEncapsedString2:
2382                         // scanner.encapsedStringStack.push(new Character('"'));
2383                         // getNextToken();
2384                         // try {
2385                         // exprSourceStart = scanner.getCurrentTokenStartPosition();
2386                         // if (token == TokenNameEncapsedString2) {
2387                         // expression = new
2388                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2389                         // exprSourceStart, scanner
2390                         // .getCurrentTokenEndPosition());
2391                         // } else {
2392                         // encaps_list();
2393                         // if (token != TokenNameEncapsedString2) {
2394                         // throwSyntaxError("'\"' expected at end of string" + "(Found token: " +
2395                         // scanner.toStringAction(token) + " )");
2396                         // } else {
2397                         // expression = new
2398                         // StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart),
2399                         // exprSourceStart, scanner
2400                         // .getCurrentTokenEndPosition());
2401                         // }
2402                         // }
2403                         // } finally {
2404                         // scanner.encapsedStringStack.pop();
2405                         // getNextToken();
2406                         // }
2407                         // break;
2408                         case TokenNameStringDoubleQuote:
2409                                 expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2410                                                 .getCurrentTokenEndPosition());
2411                                 common_scalar();
2412                                 break;
2413                         case TokenNameStringSingleQuote:
2414                                 expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2415                                                 .getCurrentTokenEndPosition());
2416                                 common_scalar();
2417                                 break;
2418                         case TokenNameIntegerLiteral:
2419                         case TokenNameDoubleLiteral:
2420                         case TokenNameStringInterpolated:
2421                         case TokenNameFILE:
2422                         case TokenNameLINE:
2423                         case TokenNameCLASS_C:
2424                         case TokenNameMETHOD_C:
2425                         case TokenNameFUNC_C:
2426                                 common_scalar();
2427                                 break;
2428                         case TokenNameHEREDOC:
2429                                 getNextToken();
2430                                 break;
2431                         case TokenNamearray:
2432                                 // T_ARRAY '(' array_pair_list ')'
2433                                 getNextToken();
2434                                 if (token == TokenNameLPAREN) {
2435                                         getNextToken();
2436                                         if (token == TokenNameRPAREN) {
2437                                                 getNextToken();
2438                                                 break;
2439                                         }
2440                                         array_pair_list();
2441                                         if (token != TokenNameRPAREN) {
2442                                                 throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2443                                         }
2444                                         getNextToken();
2445                                 } else {
2446                                         throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2447                                 }
2448                                 break;
2449                         case TokenNamelist:
2450                                 // | T_LIST '(' assignment_list ')' '=' expr
2451                                 getNextToken();
2452                                 if (token == TokenNameLPAREN) {
2453                                         getNextToken();
2454                                         assignment_list();
2455                                         if (token != TokenNameRPAREN) {
2456                                                 throwSyntaxError("')' expected after 'list' keyword.");
2457                                         }
2458                                         getNextToken();
2459                                         if (token != TokenNameEQUAL) {
2460                                                 throwSyntaxError("'=' expected after 'list' keyword.");
2461                                         }
2462                                         getNextToken();
2463                                         expr();
2464                                 } else {
2465                                         throwSyntaxError("'(' expected after 'list' keyword.");
2466                                 }
2467                                 break;
2468                         case TokenNamenew:
2469                                 // | T_NEW class_name_reference ctor_arguments
2470                                 getNextToken();
2471                                 Expression typeRef = class_name_reference();
2472                                 ctor_arguments();
2473                                 if (typeRef != null) {
2474                                         expression = typeRef;
2475                                 }
2476                                 break;
2477                         // | T_INC rw_variable
2478                         // | T_DEC rw_variable
2479                         case TokenNamePLUS_PLUS:
2480                         case TokenNameMINUS_MINUS:
2481                                 getNextToken();
2482                                 rw_variable();
2483                                 break;
2484                         // | variable '=' expr
2485                         // | variable '=' '&' variable
2486                         // | variable '=' '&' T_NEW class_name_reference ctor_arguments
2487                         // | variable T_PLUS_EQUAL expr
2488                         // | variable T_MINUS_EQUAL expr
2489                         // | variable T_MUL_EQUAL expr
2490                         // | variable T_DIV_EQUAL expr
2491                         // | variable T_CONCAT_EQUAL expr
2492                         // | variable T_MOD_EQUAL expr
2493                         // | variable T_AND_EQUAL expr
2494                         // | variable T_OR_EQUAL expr
2495                         // | variable T_XOR_EQUAL expr
2496                         // | variable T_SL_EQUAL expr
2497                         // | variable T_SR_EQUAL expr
2498                         // | rw_variable T_INC
2499                         // | rw_variable T_DEC
2500                         case TokenNameIdentifier:
2501                         case TokenNameVariable:
2502                         case TokenNameDOLLAR:
2503                                 boolean rememberedVar = false;
2504                                 Expression lhs = variable(true, true);
2505                                 if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
2506                                                 && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
2507                                                 && token != TokenNameDOT_EQUAL && token != TokenNameREMAINDER_EQUAL && token != TokenNameAND_EQUAL
2508                                                 && token != TokenNameOR_EQUAL && token != TokenNameXOR_EQUAL && token != TokenNameRIGHT_SHIFT_EQUAL
2509                                                 && token != TokenNameLEFT_SHIFT_EQUAL) {
2510                                         FieldReference ref = (FieldReference) lhs;
2511                                         if (!containsVariableSet(ref.token)) {
2512                                                 problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(), referenceContext,
2513                                                                 compilationUnit.compilationResult);
2514                                                 addVariableSet(ref.token);
2515                                         }
2516                                 }
2517
2518                                 switch (token) {
2519                                 case TokenNameEQUAL:
2520                                         if (lhs != null && lhs instanceof FieldReference) {
2521                                                 addVariableSet(((FieldReference) lhs).token);
2522                                         }
2523                                         getNextToken();
2524                                         if (token == TokenNameAND) {
2525                                                 getNextToken();
2526                                                 if (token == TokenNamenew) {
2527                                                         // | variable '=' '&' T_NEW class_name_reference
2528                                                         // ctor_arguments
2529                                                         getNextToken();
2530                                                         SingleTypeReference classRef = class_name_reference();
2531                                                         ctor_arguments();
2532                                                         if (classRef != null) {
2533                                                                 if (lhs != null && lhs instanceof FieldReference) {
2534                                                                         // example:
2535                                                                         // $var = & new Object();
2536                                                                         if (fMethodVariables != null) {
2537                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2538                                                                                 lhsInfo.reference = classRef;
2539                                                                                 lhsInfo.typeIdentifier = classRef.token;
2540                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2541                                                                                 rememberedVar = true;
2542                                                                         }
2543                                                                 }
2544                                                         }
2545                                                 } else {
2546                                                         Expression rhs = variable(false, false);
2547                                                         if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2548                                                                 // example:
2549                                                                 // $var = &$ref;
2550                                                                 if (fMethodVariables != null) {
2551                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2552                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2553                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2554                                                                                 lhsInfo.reference = rhsInfo.reference;
2555                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2556                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2557                                                                                 rememberedVar = true;
2558                                                                         }
2559                                                                 }
2560                                                         }
2561                                                 }
2562                                         } else {
2563                                                 Expression rhs = expr();
2564                                                 if (lhs != null && lhs instanceof FieldReference) {
2565                                                         if (rhs != null && rhs instanceof FieldReference) {
2566                                                                 // example:
2567                                                                 // $var = $ref;
2568                                                                 if (fMethodVariables != null) {
2569                                                                         VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2570                                                                         if (rhsInfo != null && rhsInfo.reference != null) {
2571                                                                                 VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2572                                                                                 lhsInfo.reference = rhsInfo.reference;
2573                                                                                 lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2574                                                                                 fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2575                                                                                 rememberedVar = true;
2576                                                                         }
2577                                                                 }
2578                                                         } else if (rhs != null && rhs instanceof SingleTypeReference) {
2579                                                                 // example:
2580                                                                 // $var = new Object();
2581                                                                 if (fMethodVariables != null) {
2582                                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2583                                                                         lhsInfo.reference = (SingleTypeReference) rhs;
2584                                                                         lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2585                                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2586                                                                         rememberedVar = true;
2587                                                                 }
2588                                                         }
2589                                                 }
2590                                         }
2591                                         if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2592                                                 if (fMethodVariables != null) {
2593                                                         VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2594                                                         fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2595                                                 }
2596                                         }
2597                                         break;
2598                                 case TokenNamePLUS_EQUAL:
2599                                 case TokenNameMINUS_EQUAL:
2600                                 case TokenNameMULTIPLY_EQUAL:
2601                                 case TokenNameDIVIDE_EQUAL:
2602                                 case TokenNameDOT_EQUAL:
2603                                 case TokenNameREMAINDER_EQUAL:
2604                                 case TokenNameAND_EQUAL:
2605                                 case TokenNameOR_EQUAL:
2606                                 case TokenNameXOR_EQUAL:
2607                                 case TokenNameRIGHT_SHIFT_EQUAL:
2608                                 case TokenNameLEFT_SHIFT_EQUAL:
2609                                         if (lhs != null && lhs instanceof FieldReference) {
2610                                                 addVariableSet(((FieldReference) lhs).token);
2611                                         }
2612                                         getNextToken();
2613                                         expr();
2614                                         break;
2615                                 case TokenNamePLUS_PLUS:
2616                                 case TokenNameMINUS_MINUS:
2617                                         getNextToken();
2618                                         break;
2619                                 default:
2620                                         if (!only_variable) {
2621                                                 throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2622                                         }
2623                                         if (lhs != null) {
2624                                                 expression = lhs;
2625                                         }
2626                                 }
2627                                 break;
2628                         default:
2629                                 if (token != TokenNameINLINE_HTML) {
2630                                         if (token > TokenNameKEYWORD) {
2631                                                 getNextToken();
2632                                                 break;
2633                                         } else {
2634                                                 // System.out.println(scanner.getCurrentTokenStartPosition());
2635                                                 // System.out.println(scanner.getCurrentTokenEndPosition());
2636
2637                                                 throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2638                                         }
2639                                 }
2640                                 return expression;
2641                         }
2642                         if (Scanner.TRACE) {
2643                                 System.out.println("TRACE: expr_without_variable() PART 2");
2644                         }
2645                         // | expr T_BOOLEAN_OR expr
2646                         // | expr T_BOOLEAN_AND expr
2647                         // | expr T_LOGICAL_OR expr
2648                         // | expr T_LOGICAL_AND expr
2649                         // | expr T_LOGICAL_XOR expr
2650                         // | expr '|' expr
2651                         // | expr '&' expr
2652                         // | expr '^' expr
2653                         // | expr '.' expr
2654                         // | expr '+' expr
2655                         // | expr '-' expr
2656                         // | expr '*' expr
2657                         // | expr '/' expr
2658                         // | expr '%' expr
2659                         // | expr T_SL expr
2660                         // | expr T_SR expr
2661                         // | expr T_IS_IDENTICAL expr
2662                         // | expr T_IS_NOT_IDENTICAL expr
2663                         // | expr T_IS_EQUAL expr
2664                         // | expr T_IS_NOT_EQUAL expr
2665                         // | expr '<' expr
2666                         // | expr T_IS_SMALLER_OR_EQUAL expr
2667                         // | expr '>' expr
2668                         // | expr T_IS_GREATER_OR_EQUAL expr
2669                         while (true) {
2670                                 switch (token) {
2671                                 case TokenNameOR_OR:
2672                                         getNextToken();
2673                                         expression = new OR_OR_Expression(expression, expr(), token);
2674                                         break;
2675                                 case TokenNameAND_AND:
2676                                         getNextToken();
2677                                         expression = new AND_AND_Expression(expression, expr(), token);
2678                                         break;
2679                                 case TokenNameEQUAL_EQUAL:
2680                                         getNextToken();
2681                                         expression = new EqualExpression(expression, expr(), token);
2682                                         break;
2683                                 case TokenNameand:
2684                                 case TokenNameor:
2685                                 case TokenNamexor:
2686                                 case TokenNameAND:
2687                                 case TokenNameOR:
2688                                 case TokenNameXOR:
2689                                 case TokenNameDOT:
2690                                 case TokenNamePLUS:
2691                                 case TokenNameMINUS:
2692                                 case TokenNameMULTIPLY:
2693                                 case TokenNameDIVIDE:
2694                                 case TokenNameREMAINDER:
2695                                 case TokenNameLEFT_SHIFT:
2696                                 case TokenNameRIGHT_SHIFT:
2697                                 case TokenNameEQUAL_EQUAL_EQUAL:
2698                                 case TokenNameNOT_EQUAL_EQUAL:
2699                                 case TokenNameNOT_EQUAL:
2700                                 case TokenNameLESS:
2701                                 case TokenNameLESS_EQUAL:
2702                                 case TokenNameGREATER:
2703                                 case TokenNameGREATER_EQUAL:
2704                                         getNextToken();
2705                                         expression = new BinaryExpression(expression, expr(), token);
2706                                         break;
2707                                 // | expr T_INSTANCEOF class_name_reference
2708                                 // | expr '?' expr ':' expr
2709                                 case TokenNameinstanceof:
2710                                         getNextToken();
2711                                         TypeReference classRef = class_name_reference();
2712                                         if (classRef != null) {
2713                                                 expression = new InstanceOfExpression(expression, classRef, OperatorIds.INSTANCEOF);
2714                                                 expression.sourceStart = exprSourceStart;
2715                                                 expression.sourceEnd = scanner.getCurrentTokenEndPosition();
2716                                         }
2717                                         break;
2718                                 case TokenNameQUESTION:
2719                                         getNextToken();
2720                                         Expression valueIfTrue = expr();
2721                                         if (token != TokenNameCOLON) {
2722                                                 throwSyntaxError("':' expected in conditional expression.");
2723                                         }
2724                                         getNextToken();
2725                                         Expression valueIfFalse = expr();
2726
2727                                         expression = new ConditionalExpression(expression, valueIfTrue, valueIfFalse);
2728                                         break;
2729                                 default:
2730                                         return expression;
2731                                 }
2732                         }
2733                 } catch (SyntaxError e) {
2734                         // try to find next token after expression with errors:
2735                         if (token == TokenNameSEMICOLON) {
2736                                 getNextToken();
2737                                 return expression;
2738                         }
2739                         if (token == TokenNameRBRACE || token == TokenNameRPAREN || token == TokenNameRBRACKET) {
2740                                 getNextToken();
2741                                 return expression;
2742                         }
2743                         throw e;
2744                 }
2745         }
2746
2747         private SingleTypeReference class_name_reference() {
2748                 // class_name_reference:
2749                 // T_STRING
2750                 // | dynamic_class_name_reference
2751                 SingleTypeReference ref = null;
2752                 if (Scanner.TRACE) {
2753                         System.out.println("TRACE: class_name_reference()");
2754                 }
2755                 if (token == TokenNameIdentifier) {
2756                         ref = new SingleTypeReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
2757                         getNextToken();
2758                 } else {
2759                         ref = null;
2760                         dynamic_class_name_reference();
2761                 }
2762                 return ref;
2763         }
2764
2765         private void dynamic_class_name_reference() {
2766                 // dynamic_class_name_reference:
2767                 // base_variable T_OBJECT_OPERATOR object_property
2768                 // dynamic_class_name_variable_properties
2769                 // | base_variable
2770                 if (Scanner.TRACE) {
2771                         System.out.println("TRACE: dynamic_class_name_reference()");
2772                 }
2773                 base_variable();
2774                 if (token == TokenNameMINUS_GREATER) {
2775                         getNextToken();
2776                         object_property();
2777                         dynamic_class_name_variable_properties();
2778                 }
2779         }
2780
2781         private void dynamic_class_name_variable_properties() {
2782                 // dynamic_class_name_variable_properties:
2783                 // dynamic_class_name_variable_properties
2784                 // dynamic_class_name_variable_property
2785                 // | /* empty */
2786                 if (Scanner.TRACE) {
2787                         System.out.println("TRACE: dynamic_class_name_variable_properties()");
2788                 }
2789                 while (token == TokenNameMINUS_GREATER) {
2790                         dynamic_class_name_variable_property();
2791                 }
2792         }
2793
2794         private void dynamic_class_name_variable_property() {
2795                 // dynamic_class_name_variable_property:
2796                 // T_OBJECT_OPERATOR object_property
2797                 if (Scanner.TRACE) {
2798                         System.out.println("TRACE: dynamic_class_name_variable_property()");
2799                 }
2800                 if (token == TokenNameMINUS_GREATER) {
2801                         getNextToken();
2802                         object_property();
2803                 }
2804         }
2805
2806         private void ctor_arguments() {
2807                 // ctor_arguments:
2808                 // /* empty */
2809                 // | '(' function_call_parameter_list ')'
2810                 if (token == TokenNameLPAREN) {
2811                         getNextToken();
2812                         if (token == TokenNameRPAREN) {
2813                                 getNextToken();
2814                                 return;
2815                         }
2816                         non_empty_function_call_parameter_list();
2817                         if (token != TokenNameRPAREN) {
2818                                 throwSyntaxError("')' expected in ctor_arguments.");
2819                         }
2820                         getNextToken();
2821                 }
2822         }
2823
2824         private void assignment_list() {
2825                 // assignment_list:
2826                 // assignment_list ',' assignment_list_element
2827                 // | assignment_list_element
2828                 while (true) {
2829                         assignment_list_element();
2830                         if (token != TokenNameCOMMA) {
2831                                 break;
2832                         }
2833                         getNextToken();
2834                 }
2835         }
2836
2837         private void assignment_list_element() {
2838                 // assignment_list_element:
2839                 // variable
2840                 // | T_LIST '(' assignment_list ')'
2841                 // | /* empty */
2842                 if (token == TokenNameVariable) {
2843                         variable(true, false);
2844                 } else if (token == TokenNameDOLLAR) {
2845                         variable(false, false);
2846                 } else {
2847                         if (token == TokenNamelist) {
2848                                 getNextToken();
2849                                 if (token == TokenNameLPAREN) {
2850                                         getNextToken();
2851                                         assignment_list();
2852                                         if (token != TokenNameRPAREN) {
2853                                                 throwSyntaxError("')' expected after 'list' keyword.");
2854                                         }
2855                                         getNextToken();
2856                                 } else {
2857                                         throwSyntaxError("'(' expected after 'list' keyword.");
2858                                 }
2859                         }
2860                 }
2861         }
2862
2863         private void array_pair_list() {
2864                 // array_pair_list:
2865                 // /* empty */
2866                 // | non_empty_array_pair_list possible_comma
2867                 non_empty_array_pair_list();
2868                 if (token == TokenNameCOMMA) {
2869                         getNextToken();
2870                 }
2871         }
2872
2873         private void non_empty_array_pair_list() {
2874                 // non_empty_array_pair_list:
2875                 // non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2876                 // | non_empty_array_pair_list ',' expr
2877                 // | expr T_DOUBLE_ARROW expr
2878                 // | expr
2879                 // | non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2880                 // | non_empty_array_pair_list ',' '&' w_variable
2881                 // | expr T_DOUBLE_ARROW '&' w_variable
2882                 // | '&' w_variable
2883                 while (true) {
2884                         if (token == TokenNameAND) {
2885                                 getNextToken();
2886                                 variable(true, false);
2887                         } else {
2888                                 expr();
2889                                 if (token == TokenNameAND) {
2890                                         getNextToken();
2891                                         variable(true, false);
2892                                 } else if (token == TokenNameEQUAL_GREATER) {
2893                                         getNextToken();
2894                                         if (token == TokenNameAND) {
2895                                                 getNextToken();
2896                                                 variable(true, false);
2897                                         } else {
2898                                                 expr();
2899                                         }
2900                                 }
2901                         }
2902                         if (token != TokenNameCOMMA) {
2903                                 return;
2904                         }
2905                         getNextToken();
2906                         if (token == TokenNameRPAREN) {
2907                                 return;
2908                         }
2909                 }
2910         }
2911
2912         // private void variableList() {
2913         // do {
2914         // variable();
2915         // if (token == TokenNameCOMMA) {
2916         // getNextToken();
2917         // } else {
2918         // break;
2919         // }
2920         // } while (true);
2921         // }
2922         private Expression variable_without_objects(boolean lefthandside, boolean ignoreVar) {
2923                 // variable_without_objects:
2924                 // reference_variable
2925                 // | simple_indirect_reference reference_variable
2926                 if (Scanner.TRACE) {
2927                         System.out.println("TRACE: variable_without_objects()");
2928                 }
2929                 while (token == TokenNameDOLLAR) {
2930                         getNextToken();
2931                 }
2932                 return reference_variable(lefthandside, ignoreVar);
2933         }
2934
2935         private Expression function_call(boolean lefthandside, boolean ignoreVar) {
2936                 // function_call:
2937                 // T_STRING '(' function_call_parameter_list ')'
2938                 // | class_constant '(' function_call_parameter_list ')'
2939                 // | static_member '(' function_call_parameter_list ')'
2940                 // | variable_without_objects '(' function_call_parameter_list ')'
2941                 char[] defineName = null;
2942                 char[] ident = null;
2943                 int startPos = 0;
2944                 int endPos = 0;
2945                 Expression ref = null;
2946                 if (Scanner.TRACE) {
2947                         System.out.println("TRACE: function_call()");
2948                 }
2949                 if (token == TokenNameIdentifier) {
2950                         ident = scanner.getCurrentIdentifierSource();
2951                         defineName = ident;
2952                         startPos = scanner.getCurrentTokenStartPosition();
2953                         endPos = scanner.getCurrentTokenEndPosition();
2954                         getNextToken();
2955                         switch (token) {
2956                         case TokenNamePAAMAYIM_NEKUDOTAYIM:
2957                                 // static member:
2958                                 defineName = null;
2959                                 getNextToken();
2960                                 if (token == TokenNameIdentifier) {
2961                                         // class _constant
2962                                         getNextToken();
2963                                 } else {
2964                                         // static member:
2965                                         variable_without_objects(true, false);
2966                                 }
2967                                 break;
2968                         }
2969                 } else {
2970                         ref = variable_without_objects(lefthandside, ignoreVar);
2971                 }
2972                 if (token != TokenNameLPAREN) {
2973                         if (defineName != null) {
2974                                 // does this identifier contain only uppercase characters?
2975                                 if (defineName.length == 3) {
2976                                         if (defineName[0] == 'd' && defineName[1] == 'i' && defineName[2] == 'e') {
2977                                                 defineName = null;
2978                                         }
2979                                 } else if (defineName.length == 4) {
2980                                         if (defineName[0] == 't' && defineName[1] == 'r' && defineName[2] == 'u' && defineName[3] == 'e') {
2981                                                 defineName = null;
2982                                         } else if (defineName[0] == 'n' && defineName[1] == 'u' && defineName[2] == 'l' && defineName[3] == 'l') {
2983                                                 defineName = null;
2984                                         }
2985                                 } else if (defineName.length == 5) {
2986                                         if (defineName[0] == 'f' && defineName[1] == 'a' && defineName[2] == 'l' && defineName[3] == 's' && defineName[4] == 'e') {
2987                                                 defineName = null;
2988                                         }
2989                                 }
2990                                 if (defineName != null) {
2991                                         for (int i = 0; i < defineName.length; i++) {
2992                                                 if (Character.isLowerCase(defineName[i])) {
2993                                                         problemReporter.phpUppercaseIdentifierWarning(startPos, endPos, referenceContext, compilationUnit.compilationResult);
2994                                                         break;
2995                                                 }
2996                                         }
2997                                 }
2998                         }
2999                         // TODO is this ok ?
3000                         return ref;
3001                         // throwSyntaxError("'(' expected in function call.");
3002                 }
3003                 getNextToken();
3004                 if (token == TokenNameRPAREN) {
3005                         getNextToken();
3006                         return ref;
3007                 }
3008                 non_empty_function_call_parameter_list();
3009                 if (token != TokenNameRPAREN) {
3010                         String functionName;
3011                         if (ident == null) {
3012                                 functionName = new String(" ");
3013                         } else {
3014                                 functionName = new String(ident);
3015                         }
3016                         throwSyntaxError("')' expected in function call (" + functionName + ").");
3017                 }
3018                 getNextToken();
3019                 return ref;
3020         }
3021
3022         // private void function_call_parameter_list() {
3023         // function_call_parameter_list:
3024         // non_empty_function_call_parameter_list { $$ = $1; }
3025         // | /* empty */
3026         // }
3027         private void non_empty_function_call_parameter_list() {
3028                 // non_empty_function_call_parameter_list:
3029                 // expr_without_variable
3030                 // | variable
3031                 // | '&' w_variable
3032                 // | non_empty_function_call_parameter_list ',' expr_without_variable
3033                 // | non_empty_function_call_parameter_list ',' variable
3034                 // | non_empty_function_call_parameter_list ',' '&' w_variable
3035                 if (Scanner.TRACE) {
3036                         System.out.println("TRACE: non_empty_function_call_parameter_list()");
3037                 }
3038                 while (true) {
3039                         if (token == TokenNameAND) {
3040                                 getNextToken();
3041                                 w_variable(true);
3042                         } else {
3043                                 // if (token == TokenNameIdentifier || token ==
3044                                 // TokenNameVariable
3045                                 // || token == TokenNameDOLLAR) {
3046                                 // variable();
3047                                 // } else {
3048                                 expr_without_variable(true);
3049                                 // }
3050                         }
3051                         if (token != TokenNameCOMMA) {
3052                                 break;
3053                         }
3054                         getNextToken();
3055                 }
3056         }
3057
3058         private void fully_qualified_class_name() {
3059                 if (token == TokenNameIdentifier) {
3060                         getNextToken();
3061                 } else {
3062                         throwSyntaxError("Class name expected.");
3063                 }
3064         }
3065
3066         private void static_member() {
3067                 // static_member:
3068                 // fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
3069                 // variable_without_objects
3070                 if (Scanner.TRACE) {
3071                         System.out.println("TRACE: static_member()");
3072                 }
3073                 fully_qualified_class_name();
3074                 if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
3075                         throwSyntaxError("'::' expected after class name (static_member).");
3076                 }
3077                 getNextToken();
3078                 variable_without_objects(false, false);
3079         }
3080
3081         private Expression base_variable_with_function_calls(boolean lefthandside, boolean ignoreVar) {
3082                 // base_variable_with_function_calls:
3083                 // base_variable
3084                 // | function_call
3085                 if (Scanner.TRACE) {
3086                         System.out.println("TRACE: base_variable_with_function_calls()");
3087                 }
3088                 return function_call(lefthandside, ignoreVar);
3089         }
3090
3091         private Expression base_variable() {
3092                 // base_variable:
3093                 // reference_variable
3094                 // | simple_indirect_reference reference_variable
3095                 // | static_member
3096                 Expression ref = null;
3097                 if (Scanner.TRACE) {
3098                         System.out.println("TRACE: base_variable()");
3099                 }
3100                 if (token == TokenNameIdentifier) {
3101                         static_member();
3102                 } else {
3103                         while (token == TokenNameDOLLAR) {
3104                                 getNextToken();
3105                         }
3106                         reference_variable(false, false);
3107                 }
3108                 return ref;
3109         }
3110
3111         // private void simple_indirect_reference() {
3112         // // simple_indirect_reference:
3113         // // '$'
3114         // //| simple_indirect_reference '$'
3115         // }
3116         private Expression reference_variable(boolean lefthandside, boolean ignoreVar) {
3117                 // reference_variable:
3118                 // reference_variable '[' dim_offset ']'
3119                 // | reference_variable '{' expr '}'
3120                 // | compound_variable
3121                 Expression ref = null;
3122                 if (Scanner.TRACE) {
3123                         System.out.println("TRACE: reference_variable()");
3124                 }
3125                 ref = compound_variable(lefthandside, ignoreVar);
3126                 while (true) {
3127                         if (token == TokenNameLBRACE) {
3128                                 ref = null;
3129                                 getNextToken();
3130                                 expr();
3131                                 if (token != TokenNameRBRACE) {
3132                                         throwSyntaxError("'}' expected in reference variable.");
3133                                 }
3134                                 getNextToken();
3135                         } else if (token == TokenNameLBRACKET) {
3136                                 if (ref != null && ref instanceof FieldReference) {
3137                                         FieldReference fref = (FieldReference) ref;
3138                                         addVariableSet(fref.token);
3139                                 }
3140                                 ref = null;
3141                                 getNextToken();
3142                                 if (token != TokenNameRBRACKET) {
3143                                         expr();
3144                                         // dim_offset();
3145                                         if (token != TokenNameRBRACKET) {
3146                                                 throwSyntaxError("']' expected in reference variable.");
3147                                         }
3148                                 }
3149                                 getNextToken();
3150                         } else {
3151                                 break;
3152                         }
3153                 }
3154                 return ref;
3155         }
3156
3157         private Expression compound_variable(boolean lefthandside, boolean ignoreVar) {
3158                 // compound_variable:
3159                 // T_VARIABLE
3160                 // | '$' '{' expr '}'
3161                 if (Scanner.TRACE) {
3162                         System.out.println("TRACE: compound_variable()");
3163                 }
3164                 if (token == TokenNameVariable) {
3165                         if (!lefthandside) {
3166                                 if (!containsVariableSet()) {
3167                                         // reportSyntaxError("The local variable " + new
3168                                         // String(scanner.getCurrentIdentifierSource())
3169                                         // + " may not have been initialized");
3170                                         problemReporter.uninitializedLocalVariable(new String(scanner.getCurrentIdentifierSource()), scanner
3171                                                         .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(), referenceContext,
3172                                                         compilationUnit.compilationResult);
3173                                 }
3174                         } else {
3175                                 if (!ignoreVar) {
3176                                         addVariableSet();
3177                                 }
3178                         }
3179                         FieldReference ref = new FieldReference(scanner.getCurrentIdentifierSource(), scanner.getCurrentTokenStartPosition());
3180                         getNextToken();
3181                         return ref;
3182                 } else {
3183                         // because of simple_indirect_reference
3184                         while (token == TokenNameDOLLAR) {
3185                                 getNextToken();
3186                         }
3187                         if (token != TokenNameLBRACE) {
3188                                 reportSyntaxError("'{' expected after compound variable token '$'.");
3189                                 return null;
3190                         }
3191                         getNextToken();
3192                         expr();
3193                         if (token != TokenNameRBRACE) {
3194                                 throwSyntaxError("'}' expected after compound variable token '$'.");
3195                         }
3196                         getNextToken();
3197                 }
3198                 return null;
3199         } // private void dim_offset() { // // dim_offset: // // /* empty */
3200
3201         // // | expr
3202         // expr();
3203         // }
3204         private void object_property() {
3205                 // object_property:
3206                 // object_dim_list
3207                 // | variable_without_objects
3208                 if (Scanner.TRACE) {
3209                         System.out.println("TRACE: object_property()");
3210                 }
3211                 if (token == TokenNameVariable || token == TokenNameDOLLAR) {
3212                         variable_without_objects(false, false);
3213                 } else {
3214                         object_dim_list();
3215                 }
3216         }
3217
3218         private void object_dim_list() {
3219                 // object_dim_list:
3220                 // object_dim_list '[' dim_offset ']'
3221                 // | object_dim_list '{' expr '}'
3222                 // | variable_name
3223                 if (Scanner.TRACE) {
3224                         System.out.println("TRACE: object_dim_list()");
3225                 }
3226                 variable_name();
3227                 while (true) {
3228                         if (token == TokenNameLBRACE) {
3229                                 getNextToken();
3230                                 expr();
3231                                 if (token != TokenNameRBRACE) {
3232                                         throwSyntaxError("'}' expected in object_dim_list.");
3233                                 }
3234                                 getNextToken();
3235                         } else if (token == TokenNameLBRACKET) {
3236                                 getNextToken();
3237                                 if (token == TokenNameRBRACKET) {
3238                                         getNextToken();
3239                                         continue;
3240                                 }
3241                                 expr();
3242                                 if (token != TokenNameRBRACKET) {
3243                                         throwSyntaxError("']' expected in object_dim_list.");
3244                                 }
3245                                 getNextToken();
3246                         } else {
3247                                 break;
3248                         }
3249                 }
3250         }
3251
3252         private void variable_name() {
3253                 // variable_name:
3254                 // T_STRING
3255                 // | '{' expr '}'
3256                 if (Scanner.TRACE) {
3257                         System.out.println("TRACE: variable_name()");
3258                 }
3259                 if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3260                         if (token > TokenNameKEYWORD) {
3261                                 // TODO show a warning "Keyword used as variable" ?
3262                         }
3263                         getNextToken();
3264                 } else {
3265                         if (token != TokenNameLBRACE) {
3266                                 throwSyntaxError("'{' expected in variable name.");
3267                         }
3268                         getNextToken();
3269                         expr();
3270                         if (token != TokenNameRBRACE) {
3271                                 throwSyntaxError("'}' expected in variable name.");
3272                         }
3273                         getNextToken();
3274                 }
3275         }
3276
3277         private void r_variable() {
3278                 variable(false, false);
3279         }
3280
3281         private void w_variable(boolean lefthandside) {
3282                 variable(lefthandside, false);
3283         }
3284
3285         private void rw_variable() {
3286                 variable(false, false);
3287         }
3288
3289         private Expression variable(boolean lefthandside, boolean ignoreVar) {
3290                 // variable:
3291                 // base_variable_with_function_calls T_OBJECT_OPERATOR
3292                 // object_property method_or_not variable_properties
3293                 // | base_variable_with_function_calls
3294                 Expression ref = base_variable_with_function_calls(lefthandside, ignoreVar);
3295                 if (token == TokenNameMINUS_GREATER) {
3296                         ref = null;
3297                         getNextToken();
3298                         object_property();
3299                         method_or_not();
3300                         variable_properties();
3301                 }
3302                 return ref;
3303         }
3304
3305         private void variable_properties() {
3306                 // variable_properties:
3307                 // variable_properties variable_property
3308                 // | /* empty */
3309                 while (token == TokenNameMINUS_GREATER) {
3310                         variable_property();
3311                 }
3312         }
3313
3314         private void variable_property() {
3315                 // variable_property:
3316                 // T_OBJECT_OPERATOR object_property method_or_not
3317                 if (Scanner.TRACE) {
3318                         System.out.println("TRACE: variable_property()");
3319                 }
3320                 if (token == TokenNameMINUS_GREATER) {
3321                         getNextToken();
3322                         object_property();
3323                         method_or_not();
3324                 } else {
3325                         throwSyntaxError("'->' expected in variable_property.");
3326                 }
3327         }
3328
3329         private void method_or_not() {
3330                 // method_or_not:
3331                 // '(' function_call_parameter_list ')'
3332                 // | /* empty */
3333                 if (Scanner.TRACE) {
3334                         System.out.println("TRACE: method_or_not()");
3335                 }
3336                 if (token == TokenNameLPAREN) {
3337                         getNextToken();
3338                         if (token == TokenNameRPAREN) {
3339                                 getNextToken();
3340                                 return;
3341                         }
3342                         non_empty_function_call_parameter_list();
3343                         if (token != TokenNameRPAREN) {
3344                                 throwSyntaxError("')' expected in method_or_not.");
3345                         }
3346                         getNextToken();
3347                 }
3348         }
3349
3350         private void exit_expr() {
3351                 // /* empty */
3352                 // | '(' ')'
3353                 // | '(' expr ')'
3354                 if (token != TokenNameLPAREN) {
3355                         return;
3356                 }
3357                 getNextToken();
3358                 if (token == TokenNameRPAREN) {
3359                         getNextToken();
3360                         return;
3361                 }
3362                 expr();
3363                 if (token != TokenNameRPAREN) {
3364                         throwSyntaxError("')' expected after keyword 'exit'");
3365                 }
3366                 getNextToken();
3367         }
3368
3369         // private void encaps_list() {
3370         // // encaps_list encaps_var
3371         // // | encaps_list T_STRING
3372         // // | encaps_list T_NUM_STRING
3373         // // | encaps_list T_ENCAPSED_AND_WHITESPACE
3374         // // | encaps_list T_CHARACTER
3375         // // | encaps_list T_BAD_CHARACTER
3376         // // | encaps_list '['
3377         // // | encaps_list ']'
3378         // // | encaps_list '{'
3379         // // | encaps_list '}'
3380         // // | encaps_list T_OBJECT_OPERATOR
3381         // // | /* empty */
3382         // while (true) {
3383         // switch (token) {
3384         // case TokenNameSTRING:
3385         // getNextToken();
3386         // break;
3387         // case TokenNameLBRACE:
3388         // // scanner.encapsedStringStack.pop();
3389         // getNextToken();
3390         // break;
3391         // case TokenNameRBRACE:
3392         // // scanner.encapsedStringStack.pop();
3393         // getNextToken();
3394         // break;
3395         // case TokenNameLBRACKET:
3396         // // scanner.encapsedStringStack.pop();
3397         // getNextToken();
3398         // break;
3399         // case TokenNameRBRACKET:
3400         // // scanner.encapsedStringStack.pop();
3401         // getNextToken();
3402         // break;
3403         // case TokenNameMINUS_GREATER:
3404         // // scanner.encapsedStringStack.pop();
3405         // getNextToken();
3406         // break;
3407         // case TokenNameVariable:
3408         // case TokenNameDOLLAR_LBRACE:
3409         // case TokenNameLBRACE_DOLLAR:
3410         // encaps_var();
3411         // break;
3412         // default:
3413         // char encapsedChar = ((Character)
3414         // scanner.encapsedStringStack.peek()).charValue();
3415         // if (encapsedChar == '$') {
3416         // scanner.encapsedStringStack.pop();
3417         // encapsedChar = ((Character)
3418         // scanner.encapsedStringStack.peek()).charValue();
3419         // switch (encapsedChar) {
3420         // case '`':
3421         // if (token == TokenNameEncapsedString0) {
3422         // return;
3423         // }
3424         // token = TokenNameSTRING;
3425         // continue;
3426         // case '\'':
3427         // if (token == TokenNameEncapsedString1) {
3428         // return;
3429         // }
3430         // token = TokenNameSTRING;
3431         // continue;
3432         // case '"':
3433         // if (token == TokenNameEncapsedString2) {
3434         // return;
3435         // }
3436         // token = TokenNameSTRING;
3437         // continue;
3438         // }
3439         // }
3440         // return;
3441         // }
3442         // }
3443         // }
3444
3445         // private void encaps_var() {
3446         // // T_VARIABLE
3447         // // | T_VARIABLE '[' encaps_var_offset ']'
3448         // // | T_VARIABLE T_OBJECT_OPERATOR T_STRING
3449         // // | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
3450         // // | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
3451         // // | T_CURLY_OPEN variable '}'
3452         // switch (token) {
3453         // case TokenNameVariable:
3454         // getNextToken();
3455         // if (token == TokenNameLBRACKET) {
3456         // getNextToken();
3457         // expr(); //encaps_var_offset();
3458         // if (token != TokenNameRBRACKET) {
3459         // throwSyntaxError("']' expected after variable.");
3460         // }
3461         // // scanner.encapsedStringStack.pop();
3462         // getNextToken();
3463         // // }
3464         // } else if (token == TokenNameMINUS_GREATER) {
3465         // getNextToken();
3466         // if (token != TokenNameIdentifier) {
3467         // throwSyntaxError("Identifier expected after '->'.");
3468         // }
3469         // // scanner.encapsedStringStack.pop();
3470         // getNextToken();
3471         // }
3472         // // else {
3473         // // // scanner.encapsedStringStack.pop();
3474         // // int tempToken = TokenNameSTRING;
3475         // // if (!scanner.encapsedStringStack.isEmpty()
3476         // // && (token == TokenNameEncapsedString0
3477         // // || token == TokenNameEncapsedString1
3478         // // || token == TokenNameEncapsedString2 || token ==
3479         // // TokenNameERROR)) {
3480         // // char encapsedChar = ((Character)
3481         // // scanner.encapsedStringStack.peek())
3482         // // .charValue();
3483         // // switch (token) {
3484         // // case TokenNameEncapsedString0 :
3485         // // if (encapsedChar == '`') {
3486         // // tempToken = TokenNameEncapsedString0;
3487         // // }
3488         // // break;
3489         // // case TokenNameEncapsedString1 :
3490         // // if (encapsedChar == '\'') {
3491         // // tempToken = TokenNameEncapsedString1;
3492         // // }
3493         // // break;
3494         // // case TokenNameEncapsedString2 :
3495         // // if (encapsedChar == '"') {
3496         // // tempToken = TokenNameEncapsedString2;
3497         // // }
3498         // // break;
3499         // // case TokenNameERROR :
3500         // // if (scanner.source[scanner.currentPosition - 1] == '\\') {
3501         // // scanner.currentPosition--;
3502         // // getNextToken();
3503         // // }
3504         // // break;
3505         // // }
3506         // // }
3507         // // token = tempToken;
3508         // // }
3509         // break;
3510         // case TokenNameDOLLAR_LBRACE:
3511         // getNextToken();
3512         // if (token == TokenNameDOLLAR_LBRACE) {
3513         // encaps_var();
3514         // } else if (token == TokenNameIdentifier) {
3515         // getNextToken();
3516         // if (token == TokenNameLBRACKET) {
3517         // getNextToken();
3518         // // if (token == TokenNameRBRACKET) {
3519         // // getNextToken();
3520         // // } else {
3521         // expr();
3522         // if (token != TokenNameRBRACKET) {
3523         // throwSyntaxError("']' expected after '${'.");
3524         // }
3525         // getNextToken();
3526         // // }
3527         // }
3528         // } else {
3529         // expr();
3530         // }
3531         // if (token != TokenNameRBRACE) {
3532         // throwSyntaxError("'}' expected.");
3533         // }
3534         // getNextToken();
3535         // break;
3536         // case TokenNameLBRACE_DOLLAR:
3537         // getNextToken();
3538         // if (token == TokenNameLBRACE_DOLLAR) {
3539         // encaps_var();
3540         // } else if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3541         // getNextToken();
3542         // if (token == TokenNameLBRACKET) {
3543         // getNextToken();
3544         // // if (token == TokenNameRBRACKET) {
3545         // // getNextToken();
3546         // // } else {
3547         // expr();
3548         // if (token != TokenNameRBRACKET) {
3549         // throwSyntaxError("']' expected.");
3550         // }
3551         // getNextToken();
3552         // // }
3553         // } else if (token == TokenNameMINUS_GREATER) {
3554         // getNextToken();
3555         // if (token != TokenNameIdentifier && token != TokenNameVariable) {
3556         // throwSyntaxError("String or Variable token expected.");
3557         // }
3558         // getNextToken();
3559         // if (token == TokenNameLBRACKET) {
3560         // getNextToken();
3561         // // if (token == TokenNameRBRACKET) {
3562         // // getNextToken();
3563         // // } else {
3564         // expr();
3565         // if (token != TokenNameRBRACKET) {
3566         // throwSyntaxError("']' expected after '${'.");
3567         // }
3568         // getNextToken();
3569         // // }
3570         // }
3571         // }
3572         // // if (token != TokenNameRBRACE) {
3573         // // throwSyntaxError("'}' expected after '{$'.");
3574         // // }
3575         // // // scanner.encapsedStringStack.pop();
3576         // // getNextToken();
3577         // } else {
3578         // expr();
3579         // if (token != TokenNameRBRACE) {
3580         // throwSyntaxError("'}' expected.");
3581         // }
3582         // // scanner.encapsedStringStack.pop();
3583         // getNextToken();
3584         // }
3585         // break;
3586         // }
3587         // }
3588
3589         // private void encaps_var_offset() {
3590         // // T_STRING
3591         // // | T_NUM_STRING
3592         // // | T_VARIABLE
3593         // switch (token) {
3594         // case TokenNameSTRING:
3595         // getNextToken();
3596         // break;
3597         // case TokenNameIntegerLiteral:
3598         // getNextToken();
3599         // break;
3600         // case TokenNameVariable:
3601         // getNextToken();
3602         // break;
3603         // case TokenNameIdentifier:
3604         // getNextToken();
3605         // break;
3606         // default:
3607         // throwSyntaxError("Variable or String token expected.");
3608         // break;
3609         // }
3610         // }
3611
3612         private void internal_functions_in_yacc() {
3613                 // int start = 0;
3614                 switch (token) {
3615                 // case TokenNameisset:
3616                 // // T_ISSET '(' isset_variables ')'
3617                 // getNextToken();
3618                 // if (token != TokenNameLPAREN) {
3619                 // throwSyntaxError("'(' expected after keyword 'isset'");
3620                 // }
3621                 // getNextToken();
3622                 // isset_variables();
3623                 // if (token != TokenNameRPAREN) {
3624                 // throwSyntaxError("')' expected after keyword 'isset'");
3625                 // }
3626                 // getNextToken();
3627                 // break;
3628                 // case TokenNameempty:
3629                 // // T_EMPTY '(' variable ')'
3630                 // getNextToken();
3631                 // if (token != TokenNameLPAREN) {
3632                 // throwSyntaxError("'(' expected after keyword 'empty'");
3633                 // }
3634                 // getNextToken();
3635                 // variable(false);
3636                 // if (token != TokenNameRPAREN) {
3637                 // throwSyntaxError("')' expected after keyword 'empty'");
3638                 // }
3639                 // getNextToken();
3640                 // break;
3641                 case TokenNameinclude:
3642                         // T_INCLUDE expr
3643                         checkFileName(token);
3644                         break;
3645                 case TokenNameinclude_once:
3646                         // T_INCLUDE_ONCE expr
3647                         checkFileName(token);
3648                         break;
3649                 case TokenNameeval:
3650                         // T_EVAL '(' expr ')'
3651                         getNextToken();
3652                         if (token != TokenNameLPAREN) {
3653                                 throwSyntaxError("'(' expected after keyword 'eval'");
3654                         }
3655                         getNextToken();
3656                         expr();
3657                         if (token != TokenNameRPAREN) {
3658                                 throwSyntaxError("')' expected after keyword 'eval'");
3659                         }
3660                         getNextToken();
3661                         break;
3662                 case TokenNamerequire:
3663                         // T_REQUIRE expr
3664                         checkFileName(token);
3665                         break;
3666                 case TokenNamerequire_once:
3667                         // T_REQUIRE_ONCE expr
3668                         checkFileName(token);
3669                         break;
3670                 }
3671         }
3672
3673         /**
3674          * Parse and check the include file name
3675          * 
3676          * @param includeToken
3677          */
3678         private void checkFileName(int includeToken) {
3679                 // <include-token> expr
3680                 int start = scanner.getCurrentTokenStartPosition();
3681                 boolean hasLPAREN = false;
3682                 getNextToken();
3683                 if (token == TokenNameLPAREN) {
3684                         hasLPAREN = true;
3685                         getNextToken();
3686                 }
3687                 Expression expression = expr();
3688                 if (hasLPAREN) {
3689                         if (token == TokenNameRPAREN) {
3690                                 getNextToken();
3691                         } else {
3692                                 throwSyntaxError("')' expected for keyword '" + scanner.toStringAction(includeToken) + "'");
3693                         }
3694                 }
3695                 char[] currTokenSource = scanner.getCurrentTokenSource(start);
3696                 IFile file = null;
3697                 if (scanner.compilationUnit != null) {
3698                         IResource resource = scanner.compilationUnit.getResource();
3699                         if (resource != null && resource instanceof IFile) {
3700                                 file = (IFile) resource;
3701                         }
3702                 }
3703                 char[][] tokens;
3704                 tokens = new char[1][];
3705                 tokens[0] = currTokenSource;
3706
3707                 ImportReference impt = new ImportReference(tokens, currTokenSource, start, scanner.getCurrentTokenEndPosition(), false);
3708                 impt.declarationSourceEnd = impt.sourceEnd;
3709                 impt.declarationEnd = impt.declarationSourceEnd;
3710                 // endPosition is just before the ;
3711                 impt.declarationSourceStart = start;
3712                 includesList.add(impt);
3713
3714                 if (expression instanceof StringLiteral) {
3715                         StringLiteral literal = (StringLiteral) expression;
3716                         char[] includeName = literal.source();
3717                         if (includeName.length == 0) {
3718                                 reportSyntaxError("Empty filename after keyword '" + scanner.toStringAction(includeToken) + "'", literal.sourceStart,
3719                                                 literal.sourceStart + 1);
3720                         }
3721                         String includeNameString = new String(includeName);
3722                         if (literal instanceof StringLiteralDQ) {
3723                                 if (includeNameString.indexOf('$') >= 0) {
3724                                         // assuming that the filename contains a variable => no filename check
3725                                         return;
3726                                 }
3727                         }
3728                         if (includeNameString.startsWith("http://")) {
3729                                 // assuming external include location
3730                                 return;
3731                         }
3732                         if (file != null) {
3733                                 // check the filename:
3734                                 // System.out.println(new String(compilationUnit.getFileName())+" - "+
3735                                 // expression.toStringExpression());
3736                                 IProject project = file.getProject();
3737                                 if (project != null) {
3738                                         IPath path = PHPFileUtil.determineFilePath(includeNameString, file, project);
3739
3740                                         if (path == null) {
3741                                                 // SyntaxError: "File: << >> doesn't exist in project."
3742                                                 String[] args = { expression.toStringExpression(), project.getLocation().toString() };
3743                                                 problemReporter.phpIncludeNotExistWarning(args, literal.sourceStart, literal.sourceEnd, referenceContext,
3744                                                                 compilationUnit.compilationResult);
3745                                         } else {
3746                                                 try {
3747                                                         String filePath = path.toString();
3748                                                         String ext = file.getRawLocation().getFileExtension();
3749                                                         int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
3750
3751                                                         impt.tokens = CharOperation.splitOn('/', filePath.toCharArray(), 0, filePath.length() - fileExtensionLength);
3752                                                         impt.setFile(PHPFileUtil.createFile(path, project));
3753                                                 } catch (Exception e) {
3754                                                         // the file is outside of the workspace
3755                                                 }
3756                                         }
3757                                 }
3758                         }
3759                 }
3760         }
3761
3762         private void isset_variables() {
3763                 // variable
3764                 // | isset_variables ','
3765                 if (token == TokenNameRPAREN) {
3766                         throwSyntaxError("Variable expected after keyword 'isset'");
3767                 }
3768                 while (true) {
3769                         variable(true, false);
3770                         if (token == TokenNameCOMMA) {
3771                                 getNextToken();
3772                         } else {
3773                                 break;
3774                         }
3775                 }
3776         }
3777
3778         private boolean common_scalar() {
3779                 // common_scalar:
3780                 // T_LNUMBER
3781                 // | T_DNUMBER
3782                 // | T_CONSTANT_ENCAPSED_STRING
3783                 // | T_LINE
3784                 // | T_FILE
3785                 // | T_CLASS_C
3786                 // | T_METHOD_C
3787                 // | T_FUNC_C
3788                 switch (token) {
3789                 case TokenNameIntegerLiteral:
3790                         getNextToken();
3791                         return true;
3792                 case TokenNameDoubleLiteral:
3793                         getNextToken();
3794                         return true;
3795                 case TokenNameStringDoubleQuote:
3796                         getNextToken();
3797                         return true;
3798                 case TokenNameStringSingleQuote:
3799                         getNextToken();
3800                         return true;
3801                 case TokenNameStringInterpolated:
3802                         getNextToken();
3803                         return true;
3804                 case TokenNameFILE:
3805                         getNextToken();
3806                         return true;
3807                 case TokenNameLINE:
3808                         getNextToken();
3809                         return true;
3810                 case TokenNameCLASS_C:
3811                         getNextToken();
3812                         return true;
3813                 case TokenNameMETHOD_C:
3814                         getNextToken();
3815                         return true;
3816                 case TokenNameFUNC_C:
3817                         getNextToken();
3818                         return true;
3819                 }
3820                 return false;
3821         }
3822
3823         private void scalar() {
3824                 // scalar:
3825                 // T_STRING
3826                 // | T_STRING_VARNAME
3827                 // | class_constant
3828                 // | common_scalar
3829                 // | '"' encaps_list '"'
3830                 // | '\'' encaps_list '\''
3831                 // | T_START_HEREDOC encaps_list T_END_HEREDOC
3832                 throwSyntaxError("Not yet implemented (scalar).");
3833         }
3834
3835         private void static_scalar() {
3836                 // static_scalar: /* compile-time evaluated scalars */
3837                 // common_scalar
3838                 // | T_STRING
3839                 // | '+' static_scalar
3840                 // | '-' static_scalar
3841                 // | T_ARRAY '(' static_array_pair_list ')'
3842                 // | static_class_constant
3843                 if (common_scalar()) {
3844                         return;
3845                 }
3846                 switch (token) {
3847                 case TokenNameIdentifier:
3848                         getNextToken();
3849                         // static_class_constant:
3850                         // T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3851                         if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3852                                 getNextToken();
3853                                 if (token == TokenNameIdentifier) {
3854                                         getNextToken();
3855                                 } else {
3856                                         throwSyntaxError("Identifier expected after '::' operator.");
3857                                 }
3858                         }
3859                         break;
3860                 case TokenNameEncapsedString0:
3861                         try {
3862                                 scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3863                                 while (scanner.currentCharacter != '`') {
3864                                         if (scanner.currentCharacter == '\\') {
3865                                                 scanner.currentPosition++;
3866                                         }
3867                                         scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3868                                 }
3869                                 getNextToken();
3870                         } catch (IndexOutOfBoundsException e) {
3871                                 throwSyntaxError("'`' expected at end of static string.");
3872                         }
3873                         break;
3874                 // case TokenNameEncapsedString1:
3875                 // try {
3876                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3877                 // while (scanner.currentCharacter != '\'') {
3878                 // if (scanner.currentCharacter == '\\') {
3879                 // scanner.currentPosition++;
3880                 // }
3881                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3882                 // }
3883                 // getNextToken();
3884                 // } catch (IndexOutOfBoundsException e) {
3885                 // throwSyntaxError("'\'' expected at end of static string.");
3886                 // }
3887                 // break;
3888                 // case TokenNameEncapsedString2:
3889                 // try {
3890                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3891                 // while (scanner.currentCharacter != '"') {
3892                 // if (scanner.currentCharacter == '\\') {
3893                 // scanner.currentPosition++;
3894                 // }
3895                 // scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3896                 // }
3897                 // getNextToken();
3898                 // } catch (IndexOutOfBoundsException e) {
3899                 // throwSyntaxError("'\"' expected at end of static string.");
3900                 // }
3901                 // break;
3902                 case TokenNameStringSingleQuote:
3903                         getNextToken();
3904                         break;
3905                 case TokenNameStringDoubleQuote:
3906                         getNextToken();
3907                         break;
3908                 case TokenNamePLUS:
3909                         getNextToken();
3910                         static_scalar();
3911                         break;
3912                 case TokenNameMINUS:
3913                         getNextToken();
3914                         static_scalar();
3915                         break;
3916                 case TokenNamearray:
3917                         getNextToken();
3918                         if (token != TokenNameLPAREN) {
3919                                 throwSyntaxError("'(' expected after keyword 'array'");
3920                         }
3921                         getNextToken();
3922                         if (token == TokenNameRPAREN) {
3923                                 getNextToken();
3924                                 break;
3925                         }
3926                         non_empty_static_array_pair_list();
3927                         if (token != TokenNameRPAREN) {
3928                                 throwSyntaxError("')' or ',' expected after keyword 'array'");
3929                         }
3930                         getNextToken();
3931                         break;
3932                 // case TokenNamenull :
3933                 // getNextToken();
3934                 // break;
3935                 // case TokenNamefalse :
3936                 // getNextToken();
3937                 // break;
3938                 // case TokenNametrue :
3939                 // getNextToken();
3940                 // break;
3941                 default:
3942                         throwSyntaxError("Static scalar/constant expected.");
3943                 }
3944         }
3945
3946         private void non_empty_static_array_pair_list() {
3947                 // non_empty_static_array_pair_list:
3948                 // non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3949                 // static_scalar
3950                 // | non_empty_static_array_pair_list ',' static_scalar
3951                 // | static_scalar T_DOUBLE_ARROW static_scalar
3952                 // | static_scalar
3953                 while (true) {
3954                         static_scalar();
3955                         if (token == TokenNameEQUAL_GREATER) {
3956                                 getNextToken();
3957                                 static_scalar();
3958                         }
3959                         if (token != TokenNameCOMMA) {
3960                                 break;
3961                         }
3962                         getNextToken();
3963                         if (token == TokenNameRPAREN) {
3964                                 break;
3965                         }
3966                 }
3967         }
3968
3969         // public void reportSyntaxError() { //int act, int currentKind, int
3970         // // stateStackTop) {
3971         // /* remember current scanner position */
3972         // int startPos = scanner.startPosition;
3973         // int currentPos = scanner.currentPosition;
3974         //    
3975         // this.checkAndReportBracketAnomalies(problemReporter());
3976         // /* reset scanner where it was */
3977         // scanner.startPosition = startPos;
3978         // scanner.currentPosition = currentPos;
3979         // }
3980
3981         public static final int RoundBracket = 0;
3982
3983         public static final int SquareBracket = 1;
3984
3985         public static final int CurlyBracket = 2;
3986
3987         public static final int BracketKinds = 3;
3988
3989         protected int[] nestedMethod; // the ptr is nestedType
3990
3991         protected int nestedType, dimensions;
3992
3993         // variable set stack
3994         final static int VariableStackIncrement = 10;
3995
3996         HashMap fTypeVariables = null;
3997
3998         HashMap fMethodVariables = null;
3999
4000         ArrayList fStackUnassigned = new ArrayList();
4001
4002         // ast stack
4003         final static int AstStackIncrement = 100;
4004
4005         protected int astPtr;
4006
4007         protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
4008
4009         protected int astLengthPtr;
4010
4011         protected int[] astLengthStack;
4012
4013         ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
4014
4015         public CompilationUnitDeclaration compilationUnit; /*
4016                                                                                                                                                                                                                          * the result from parse()
4017                                                                                                                                                                                                                          */
4018
4019         protected ReferenceContext referenceContext;
4020
4021         protected ProblemReporter problemReporter;
4022
4023         protected CompilerOptions options;
4024
4025         private ArrayList includesList;
4026
4027         // protected CompilationResult compilationResult;
4028         /**
4029          * Returns this parser's problem reporter initialized with its reference
4030          * context. Also it is assumed that a problem is going to be reported, so
4031          * initializes the compilation result's line positions.
4032          */
4033         public ProblemReporter problemReporter() {
4034                 if (scanner.recordLineSeparator) {
4035                         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4036                 }
4037                 problemReporter.referenceContext = referenceContext;
4038                 return problemReporter;
4039         }
4040
4041         /*
4042          * Reconsider the entire source looking for inconsistencies in {} () []
4043          */
4044         // public boolean checkAndReportBracketAnomalies(ProblemReporter
4045         // problemReporter) {
4046         // scanner.wasAcr = false;
4047         // boolean anomaliesDetected = false;
4048         // try {
4049         // char[] source = scanner.source;
4050         // int[] leftCount = { 0, 0, 0 };
4051         // int[] rightCount = { 0, 0, 0 };
4052         // int[] depths = { 0, 0, 0 };
4053         // int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10]
4054         // };
4055         // int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
4056         // int[][] rightPositions = new int[][] { new int[10], new int[10], new
4057         // int[10] };
4058         // int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10]
4059         // };
4060         // scanner.currentPosition = scanner.initialPosition; //starting
4061         // // point
4062         // // (first-zero-based
4063         // // char)
4064         // while (scanner.currentPosition < scanner.eofPosition) { //loop for
4065         // // jumping
4066         // // over
4067         // // comments
4068         // try {
4069         // // ---------Consume white space and handles
4070         // // startPosition---------
4071         // boolean isWhiteSpace;
4072         // do {
4073         // scanner.startPosition = scanner.currentPosition;
4074         // // if (((scanner.currentCharacter =
4075         // // source[scanner.currentPosition++]) == '\\') &&
4076         // // (source[scanner.currentPosition] == 'u')) {
4077         // // isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
4078         // // } else {
4079         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4080         // (scanner.currentCharacter == '\n'))) {
4081         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4082         // // only record line positions we have not
4083         // // recorded yet
4084         // scanner.pushLineSeparator();
4085         // }
4086         // }
4087         // isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
4088         // // }
4089         // } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
4090         // // -------consume token until } is found---------
4091         // switch (scanner.currentCharacter) {
4092         // case '{': {
4093         // int index = leftCount[CurlyBracket]++;
4094         // if (index == leftPositions[CurlyBracket].length) {
4095         // System.arraycopy(leftPositions[CurlyBracket], 0,
4096         // (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
4097         // System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] =
4098         // new int[index * 2]), 0, index);
4099         // }
4100         // leftPositions[CurlyBracket][index] = scanner.startPosition;
4101         // leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
4102         // }
4103         // break;
4104         // case '}': {
4105         // int index = rightCount[CurlyBracket]++;
4106         // if (index == rightPositions[CurlyBracket].length) {
4107         // System.arraycopy(rightPositions[CurlyBracket], 0,
4108         // (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
4109         // System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] =
4110         // new int[index * 2]), 0, index);
4111         // }
4112         // rightPositions[CurlyBracket][index] = scanner.startPosition;
4113         // rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4114         // }
4115         // break;
4116         // case '(': {
4117         // int index = leftCount[RoundBracket]++;
4118         // if (index == leftPositions[RoundBracket].length) {
4119         // System.arraycopy(leftPositions[RoundBracket], 0,
4120         // (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4121         // System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] =
4122         // new int[index * 2]), 0, index);
4123         // }
4124         // leftPositions[RoundBracket][index] = scanner.startPosition;
4125         // leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4126         // }
4127         // break;
4128         // case ')': {
4129         // int index = rightCount[RoundBracket]++;
4130         // if (index == rightPositions[RoundBracket].length) {
4131         // System.arraycopy(rightPositions[RoundBracket], 0,
4132         // (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4133         // System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] =
4134         // new int[index * 2]), 0, index);
4135         // }
4136         // rightPositions[RoundBracket][index] = scanner.startPosition;
4137         // rightDepths[RoundBracket][index] = --depths[RoundBracket];
4138         // }
4139         // break;
4140         // case '[': {
4141         // int index = leftCount[SquareBracket]++;
4142         // if (index == leftPositions[SquareBracket].length) {
4143         // System.arraycopy(leftPositions[SquareBracket], 0,
4144         // (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4145         // System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] =
4146         // new int[index * 2]), 0, index);
4147         // }
4148         // leftPositions[SquareBracket][index] = scanner.startPosition;
4149         // leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4150         // }
4151         // break;
4152         // case ']': {
4153         // int index = rightCount[SquareBracket]++;
4154         // if (index == rightPositions[SquareBracket].length) {
4155         // System.arraycopy(rightPositions[SquareBracket], 0,
4156         // (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4157         // System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket]
4158         // = new int[index * 2]), 0, index);
4159         // }
4160         // rightPositions[SquareBracket][index] = scanner.startPosition;
4161         // rightDepths[SquareBracket][index] = --depths[SquareBracket];
4162         // }
4163         // break;
4164         // case '\'': {
4165         // if (scanner.getNextChar('\\')) {
4166         // scanner.scanEscapeCharacter();
4167         // } else { // consume next character
4168         // scanner.unicodeAsBackSlash = false;
4169         // // if (((scanner.currentCharacter =
4170         // // source[scanner.currentPosition++]) ==
4171         // // '\\') &&
4172         // // (source[scanner.currentPosition] ==
4173         // // 'u')) {
4174         // // scanner.getNextUnicodeChar();
4175         // // } else {
4176         // if (scanner.withoutUnicodePtr != 0) {
4177         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4178         // scanner.currentCharacter;
4179         // }
4180         // // }
4181         // }
4182         // scanner.getNextChar('\'');
4183         // break;
4184         // }
4185         // case '"':
4186         // // consume next character
4187         // scanner.unicodeAsBackSlash = false;
4188         // // if (((scanner.currentCharacter =
4189         // // source[scanner.currentPosition++]) == '\\') &&
4190         // // (source[scanner.currentPosition] == 'u')) {
4191         // // scanner.getNextUnicodeChar();
4192         // // } else {
4193         // if (scanner.withoutUnicodePtr != 0) {
4194         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4195         // scanner.currentCharacter;
4196         // }
4197         // // }
4198         // while (scanner.currentCharacter != '"') {
4199         // if (scanner.currentCharacter == '\r') {
4200         // if (source[scanner.currentPosition] == '\n')
4201         // scanner.currentPosition++;
4202         // break; // the string cannot go further that
4203         // // the line
4204         // }
4205         // if (scanner.currentCharacter == '\n') {
4206         // break; // the string cannot go further that
4207         // // the line
4208         // }
4209         // if (scanner.currentCharacter == '\\') {
4210         // scanner.scanEscapeCharacter();
4211         // }
4212         // // consume next character
4213         // scanner.unicodeAsBackSlash = false;
4214         // // if (((scanner.currentCharacter =
4215         // // source[scanner.currentPosition++]) == '\\')
4216         // // && (source[scanner.currentPosition] == 'u'))
4217         // // {
4218         // // scanner.getNextUnicodeChar();
4219         // // } else {
4220         // if (scanner.withoutUnicodePtr != 0) {
4221         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4222         // scanner.currentCharacter;
4223         // }
4224         // // }
4225         // }
4226         // break;
4227         // case '/': {
4228         // int test;
4229         // if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4230         // // comment
4231         // //get the next char
4232         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4233         // '\\')
4234         // && (source[scanner.currentPosition] == 'u')) {
4235         // //-------------unicode traitement
4236         // // ------------
4237         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4238         // scanner.currentPosition++;
4239         // while (source[scanner.currentPosition] == 'u') {
4240         // scanner.currentPosition++;
4241         // }
4242         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4243         // 15 || c1 < 0
4244         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4245         // || c2 < 0
4246         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4247         // || c3 < 0
4248         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4249         // || c4 < 0) { //error
4250         // // don't
4251         // // care of the
4252         // // value
4253         // scanner.currentCharacter = 'A';
4254         // } //something different from \n and \r
4255         // else {
4256         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4257         // }
4258         // }
4259         // while (scanner.currentCharacter != '\r' && scanner.currentCharacter !=
4260         // '\n') {
4261         // //get the next char
4262         // scanner.startPosition = scanner.currentPosition;
4263         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4264         // '\\')
4265         // && (source[scanner.currentPosition] == 'u')) {
4266         // //-------------unicode traitement
4267         // // ------------
4268         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4269         // scanner.currentPosition++;
4270         // while (source[scanner.currentPosition] == 'u') {
4271         // scanner.currentPosition++;
4272         // }
4273         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4274         // 15 || c1 < 0
4275         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4276         // || c2 < 0
4277         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4278         // || c3 < 0
4279         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4280         // || c4 < 0) { //error
4281         // // don't
4282         // // care of the
4283         // // value
4284         // scanner.currentCharacter = 'A';
4285         // } //something different from \n
4286         // // and \r
4287         // else {
4288         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4289         // }
4290         // }
4291         // }
4292         // if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') ||
4293         // (scanner.currentCharacter == '\n'))) {
4294         // if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4295         // // only record line positions we
4296         // // have not recorded yet
4297         // scanner.pushLineSeparator();
4298         // if (this.scanner.taskTags != null) {
4299         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4300         // this.scanner
4301         // .getCurrentTokenEndPosition());
4302         // }
4303         // }
4304         // }
4305         // break;
4306         // }
4307         // if (test > 0) { //traditional and annotation
4308         // // comment
4309         // boolean star = false;
4310         // // consume next character
4311         // scanner.unicodeAsBackSlash = false;
4312         // // if (((scanner.currentCharacter =
4313         // // source[scanner.currentPosition++]) ==
4314         // // '\\') &&
4315         // // (source[scanner.currentPosition] ==
4316         // // 'u')) {
4317         // // scanner.getNextUnicodeChar();
4318         // // } else {
4319         // if (scanner.withoutUnicodePtr != 0) {
4320         // scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] =
4321         // scanner.currentCharacter;
4322         // }
4323         // // }
4324         // if (scanner.currentCharacter == '*') {
4325         // star = true;
4326         // }
4327         // //get the next char
4328         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4329         // '\\')
4330         // && (source[scanner.currentPosition] == 'u')) {
4331         // //-------------unicode traitement
4332         // // ------------
4333         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4334         // scanner.currentPosition++;
4335         // while (source[scanner.currentPosition] == 'u') {
4336         // scanner.currentPosition++;
4337         // }
4338         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4339         // 15 || c1 < 0
4340         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4341         // || c2 < 0
4342         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4343         // || c3 < 0
4344         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4345         // || c4 < 0) { //error
4346         // // don't
4347         // // care of the
4348         // // value
4349         // scanner.currentCharacter = 'A';
4350         // } //something different from * and /
4351         // else {
4352         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4353         // }
4354         // }
4355         // //loop until end of comment */
4356         // while ((scanner.currentCharacter != '/') || (!star)) {
4357         // star = scanner.currentCharacter == '*';
4358         // //get next char
4359         // if (((scanner.currentCharacter = source[scanner.currentPosition++]) ==
4360         // '\\')
4361         // && (source[scanner.currentPosition] == 'u')) {
4362         // //-------------unicode traitement
4363         // // ------------
4364         // int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4365         // scanner.currentPosition++;
4366         // while (source[scanner.currentPosition] == 'u') {
4367         // scanner.currentPosition++;
4368         // }
4369         // if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) >
4370         // 15 || c1 < 0
4371         // || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4372         // || c2 < 0
4373         // || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4374         // || c3 < 0
4375         // || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15
4376         // || c4 < 0) { //error
4377         // // don't
4378         // // care of the
4379         // // value
4380         // scanner.currentCharacter = 'A';
4381         // } //something different from * and
4382         // // /
4383         // else {
4384         // scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4385         // }
4386         // }
4387         // }
4388         // if (this.scanner.taskTags != null) {
4389         // this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(),
4390         // this.scanner.getCurrentTokenEndPosition());
4391         // }
4392         // break;
4393         // }
4394         // break;
4395         // }
4396         // default:
4397         // if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4398         // scanner.scanIdentifierOrKeyword(false);
4399         // break;
4400         // }
4401         // if (Character.isDigit(scanner.currentCharacter)) {
4402         // scanner.scanNumber(false);
4403         // break;
4404         // }
4405         // }
4406         // //-----------------end switch while
4407         // // try--------------------
4408         // } catch (IndexOutOfBoundsException e) {
4409         // break; // read until EOF
4410         // } catch (InvalidInputException e) {
4411         // return false; // no clue
4412         // }
4413         // }
4414         // if (scanner.recordLineSeparator) {
4415         // compilationUnit.compilationResult.lineSeparatorPositions =
4416         // scanner.getLineEnds();
4417         // }
4418         // // check placement anomalies against other kinds of brackets
4419         // for (int kind = 0; kind < BracketKinds; kind++) {
4420         // for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4421         // int start = leftPositions[kind][leftIndex]; // deepest
4422         // // first
4423         // // find matching closing bracket
4424         // int depth = leftDepths[kind][leftIndex];
4425         // int end = -1;
4426         // for (int i = 0; i < rightCount[kind]; i++) {
4427         // int pos = rightPositions[kind][i];
4428         // // want matching bracket further in source with same
4429         // // depth
4430         // if ((pos > start) && (depth == rightDepths[kind][i])) {
4431         // end = pos;
4432         // break;
4433         // }
4434         // }
4435         // if (end < 0) { // did not find a good closing match
4436         // problemReporter.unmatchedBracket(start, referenceContext,
4437         // compilationUnit.compilationResult);
4438         // return true;
4439         // }
4440         // // check if even number of opening/closing other brackets
4441         // // in between this pair of brackets
4442         // int balance = 0;
4443         // for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds);
4444         // otherKind++) {
4445         // for (int i = 0; i < leftCount[otherKind]; i++) {
4446         // int pos = leftPositions[otherKind][i];
4447         // if ((pos > start) && (pos < end))
4448         // balance++;
4449         // }
4450         // for (int i = 0; i < rightCount[otherKind]; i++) {
4451         // int pos = rightPositions[otherKind][i];
4452         // if ((pos > start) && (pos < end))
4453         // balance--;
4454         // }
4455         // if (balance != 0) {
4456         // problemReporter.unmatchedBracket(start, referenceContext,
4457         // compilationUnit.compilationResult); //bracket
4458         // // anomaly
4459         // return true;
4460         // }
4461         // }
4462         // }
4463         // // too many opening brackets ?
4464         // for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4465         // anomaliesDetected = true;
4466         // problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i -
4467         // 1], referenceContext,
4468         // compilationUnit.compilationResult);
4469         // }
4470         // // too many closing brackets ?
4471         // for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4472         // anomaliesDetected = true;
4473         // problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext,
4474         // compilationUnit.compilationResult);
4475         // }
4476         // if (anomaliesDetected)
4477         // return true;
4478         // }
4479         // return anomaliesDetected;
4480         // } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4481         // return anomaliesDetected;
4482         // } catch (NullPointerException e) { // jdk1.2.2 jit bug
4483         // return anomaliesDetected;
4484         // }
4485         // }
4486         protected void pushOnAstLengthStack(int pos) {
4487                 try {
4488                         astLengthStack[++astLengthPtr] = pos;
4489                 } catch (IndexOutOfBoundsException e) {
4490                         int oldStackLength = astLengthStack.length;
4491                         int[] oldPos = astLengthStack;
4492                         astLengthStack = new int[oldStackLength + StackIncrement];
4493                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4494                         astLengthStack[astLengthPtr] = pos;
4495                 }
4496         }
4497
4498         protected void pushOnAstStack(ASTNode node) {
4499                 /*
4500                  * add a new obj on top of the ast stack
4501                  */
4502                 try {
4503                         astStack[++astPtr] = node;
4504                 } catch (IndexOutOfBoundsException e) {
4505                         int oldStackLength = astStack.length;
4506                         ASTNode[] oldStack = astStack;
4507                         astStack = new ASTNode[oldStackLength + AstStackIncrement];
4508                         System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4509                         astPtr = oldStackLength;
4510                         astStack[astPtr] = node;
4511                 }
4512                 try {
4513                         astLengthStack[++astLengthPtr] = 1;
4514                 } catch (IndexOutOfBoundsException e) {
4515                         int oldStackLength = astLengthStack.length;
4516                         int[] oldPos = astLengthStack;
4517                         astLengthStack = new int[oldStackLength + AstStackIncrement];
4518                         System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4519                         astLengthStack[astLengthPtr] = 1;
4520                 }
4521         }
4522
4523         protected void resetModifiers() {
4524                 this.modifiers = AccDefault;
4525                 this.modifiersSourceStart = -1; // <-- see comment into
4526                 // modifiersFlag(int)
4527                 this.scanner.commentPtr = -1;
4528         }
4529
4530         protected void consumePackageDeclarationName(IFile file) {
4531                 // create a package name similar to java package names
4532                 String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4533                 String filePath = file.getRawLocation().toString();
4534                 String ext = file.getRawLocation().getFileExtension();
4535                 int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4536                 ImportReference impt;
4537                 char[][] tokens;
4538                 if (filePath.startsWith(projectPath)) {
4539                         tokens = CharOperation
4540                                         .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4541                 } else {
4542                         String name = file.getName();
4543                         tokens = new char[1][];
4544                         tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4545                 }
4546
4547                 this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4548
4549                 impt.declarationSourceStart = 0;
4550                 impt.declarationSourceEnd = 0;
4551                 impt.declarationEnd = 0;
4552                 // endPosition is just before the ;
4553
4554         }
4555
4556         public final static String[] GLOBALS = { "$this", "$_COOKIE", "$_ENV", "$_FILES", "$_GET", "$GLOBALS", "$_POST", "$_REQUEST",
4557                         "$_SESSION", "$_SERVER" };
4558
4559         /**
4560          * 
4561          */
4562         private void pushFunctionVariableSet() {
4563                 HashSet set = new HashSet();
4564                 if (fStackUnassigned.isEmpty()) {
4565                         for (int i = 0; i < GLOBALS.length; i++) {
4566                                 set.add(GLOBALS[i]);
4567                         }
4568                 }
4569                 fStackUnassigned.add(set);
4570         }
4571
4572         private void pushIfVariableSet() {
4573                 if (!fStackUnassigned.isEmpty()) {
4574                         HashSet set = new HashSet();
4575                         fStackUnassigned.add(set);
4576                 }
4577         }
4578
4579         private HashSet removeIfVariableSet() {
4580                 if (!fStackUnassigned.isEmpty()) {
4581                         return (HashSet) fStackUnassigned.remove(fStackUnassigned.size() - 1);
4582                 }
4583                 return null;
4584         }
4585
4586         /**
4587          * Returns the <i>set of assigned variables </i> returns null if no Set is
4588          * defined at the current scanner position
4589          */
4590         private HashSet peekVariableSet() {
4591                 if (!fStackUnassigned.isEmpty()) {
4592                         return (HashSet) fStackUnassigned.get(fStackUnassigned.size() - 1);
4593                 }
4594                 return null;
4595         }
4596
4597         /**
4598          * add the current identifier source to the <i>set of assigned variables </i>
4599          * 
4600          * @param set
4601          */
4602         private void addVariableSet(HashSet set) {
4603                 if (set != null) {
4604                         set.add(new String(scanner.getCurrentTokenSource()));
4605                 }
4606         }
4607
4608         /**
4609          * add the current identifier source to the <i>set of assigned variables </i>
4610          * 
4611          */
4612         private void addVariableSet() {
4613                 HashSet set = peekVariableSet();
4614                 if (set != null) {
4615                         set.add(new String(scanner.getCurrentTokenSource()));
4616                 }
4617         }
4618
4619         /**
4620          * add the current identifier source to the <i>set of assigned variables </i>
4621          * 
4622          */
4623         private void addVariableSet(char[] token) {
4624                 HashSet set = peekVariableSet();
4625                 if (set != null) {
4626                         set.add(new String(token));
4627                 }
4628         }
4629
4630         /**
4631          * check if the current identifier source is in the <i>set of assigned
4632          * variables </i> Returns true, if no set is defined for the current scanner
4633          * position
4634          * 
4635          */
4636         private boolean containsVariableSet() {
4637                 return containsVariableSet(scanner.getCurrentTokenSource());
4638         }
4639
4640         private boolean containsVariableSet(char[] token) {
4641
4642                 if (!fStackUnassigned.isEmpty()) {
4643                         HashSet set;
4644                         String str = new String(token);
4645                         for (int i = 0; i < fStackUnassigned.size(); i++) {
4646                                 set = (HashSet) fStackUnassigned.get(i);
4647                                 if (set.contains(str)) {
4648                                         return true;
4649                                 }
4650                         }
4651                         return false;
4652                 }
4653                 return true;
4654         }
4655 }