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