Organized imports
[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();
198     throwSyntaxError(error, problemStartPosition, problemEndPosition + 1);
199   }
200
201   /**
202    * This method will throw the SyntaxError. It will add the good lines and columns to the Error
203    * 
204    * @param error
205    *          the error message
206    * @throws SyntaxError
207    *           the error raised
208    */
209   //  private void throwSyntaxError(String error, int startRow) {
210   //    throw new SyntaxError(startRow, 0, " ", error);
211   //  }
212   private void throwSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
213     if (referenceContext != null) {
214       problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
215           compilationUnit.compilationResult);
216     }
217     throw new SyntaxError(1, 0, " ", error);
218   }
219
220   private void reportSyntaxError(String error) {
221     int problemStartPosition = scanner.getCurrentTokenStartPosition();
222     int problemEndPosition = scanner.getCurrentTokenEndPosition();
223     reportSyntaxError(error, problemStartPosition, problemEndPosition + 1);
224   }
225
226   private void reportSyntaxError(String error, int problemStartPosition, int problemEndPosition) {
227     if (referenceContext != null) {
228       problemReporter.phpParsingError(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
229           compilationUnit.compilationResult);
230     }
231   }
232
233   private void reportSyntaxWarning(String error, int problemStartPosition, int problemEndPosition) {
234     if (referenceContext != null) {
235       problemReporter.phpParsingWarning(new String[] { error }, problemStartPosition, problemEndPosition, referenceContext,
236           compilationUnit.compilationResult);
237     }
238   }
239
240   /**
241    * gets the next token from input
242    */
243   private void getNextToken() {
244     try {
245       token = scanner.getNextToken();
246       if (Scanner.DEBUG) {
247         int currentEndPosition = scanner.getCurrentTokenEndPosition();
248         int currentStartPosition = scanner.getCurrentTokenStartPosition();
249         System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
250         System.out.println(scanner.toStringAction(token));
251       }
252     } catch (InvalidInputException e) {
253       token = TokenNameERROR;
254       String detailedMessage = e.getMessage();
255
256       if (detailedMessage == Scanner.UNTERMINATED_STRING) {
257         throwSyntaxError("Unterminated string.");
258       } else if (detailedMessage == Scanner.UNTERMINATED_COMMENT) {
259         throwSyntaxError("Unterminated commment.");
260       }
261     }
262     return;
263   }
264
265   public void init(String s) {
266     this.str = s;
267     this.token = TokenNameEOF;
268     this.includesList = new ArrayList();
269     //    this.chIndx = 0;
270     //    this.rowCount = 1;
271     //    this.columnCount = 0;
272     //    this.phpEnd = false;
273     //    this.phpMode = false;
274     /* scanner initialization */
275     scanner.setSource(s.toCharArray());
276     scanner.setPHPMode(false);
277     astPtr = 0;
278   }
279
280   protected void initialize(boolean phpMode) {
281     initialize(phpMode, null);
282   }
283
284   protected void initialize(boolean phpMode, IdentifierIndexManager indexManager) {
285     compilationUnit = null;
286     referenceContext = null;
287     this.includesList = new ArrayList();
288     //    this.indexManager = indexManager;
289     this.str = "";
290     this.token = TokenNameEOF;
291     //    this.chIndx = 0;
292     //    this.rowCount = 1;
293     //    this.columnCount = 0;
294     //    this.phpEnd = false;
295     //    this.phpMode = phpMode;
296     scanner.setPHPMode(phpMode);
297     astPtr = 0;
298   }
299
300   /**
301    * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt; &lt;/body&gt;'
302    */
303   public void parse(String s) {
304     parse(s, null);
305   }
306
307   /**
308    * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt; &lt;/body&gt;'
309    */
310   public void parse(String s, HashMap variables) {
311     fMethodVariables = variables;
312     fStackUnassigned = new ArrayList();
313     init(s);
314     parse();
315   }
316
317   /**
318    * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt; &lt;/body&gt;'
319    */
320   protected void parse() {
321     if (scanner.compilationUnit != null) {
322       IResource resource = scanner.compilationUnit.getResource();
323       if (resource != null && resource instanceof IFile) {
324         // set the package name
325         consumePackageDeclarationName((IFile) resource);
326       }
327     }
328     getNextToken();
329     do {
330       try {
331         if (token != TokenNameEOF && token != TokenNameERROR) {
332           statementList();
333         }
334         if (token != TokenNameEOF) {
335           if (token == TokenNameERROR) {
336             throwSyntaxError("Scanner error (Found unknown token: " + scanner.toStringAction(token) + ")");
337           }
338           if (token == TokenNameRPAREN) {
339             throwSyntaxError("Too many closing ')'; end-of-file not reached.");
340           }
341           if (token == TokenNameRBRACE) {
342             throwSyntaxError("Too many closing '}'; end-of-file not reached.");
343           }
344           if (token == TokenNameRBRACKET) {
345             throwSyntaxError("Too many closing ']'; end-of-file not reached.");
346           }
347           if (token == TokenNameLPAREN) {
348             throwSyntaxError("Read character '('; end-of-file not reached.");
349           }
350           if (token == TokenNameLBRACE) {
351             throwSyntaxError("Read character '{';  end-of-file not reached.");
352           }
353           if (token == TokenNameLBRACKET) {
354             throwSyntaxError("Read character '[';  end-of-file not reached.");
355           }
356           throwSyntaxError("End-of-file not reached.");
357         }
358         break;
359       } catch (SyntaxError sytaxErr1) {
360         break;
361         //        // if an error occured,
362         //        // try to find keywords 'abstract' 'final' 'class' or 'function'
363         //        // to parse the rest of the string
364         //        boolean tokenize = scanner.tokenizeStrings;
365         //        if (!tokenize) {
366         //          scanner.tokenizeStrings = true;
367         //        }
368         //        try {
369         //          while (token != TokenNameEOF) {
370         //            if (token == TokenNameabstract || token == TokenNamefinal || token == TokenNameclass || token == TokenNamefunction) {
371         //              break;
372         //            }
373         //            getNextToken();
374         //          }
375         //          if (token == TokenNameEOF) {
376         //            break;
377         //          }
378         //        } catch (SyntaxError sytaxErr2) {
379         //          break;
380         //        } finally {
381         //          scanner.tokenizeStrings = tokenize;
382         //        }
383       }
384     } while (true);
385
386     endParse(0);
387   }
388
389   /**
390    * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt; &lt;/body&gt;'
391    */
392   public void parseFunction(String s, HashMap variables) {
393     init(s);
394     scanner.phpMode = true;
395     parseFunction(variables);
396   }
397
398   /**
399    * Parses a string with php tags i.e. '&lt;body&gt; &lt;?php phpinfo() ?&gt; &lt;/body&gt;'
400    */
401   protected void parseFunction(HashMap variables) {
402     getNextToken();
403     boolean hasModifiers = member_modifiers();
404     if (token == TokenNamefunction) {
405       if (!hasModifiers) {
406         checkAndSetModifiers(AccPublic);
407       }
408       this.fMethodVariables = variables;
409
410       MethodDeclaration methodDecl = new MethodDeclaration(null);
411       methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
412       methodDecl.modifiers = this.modifiers;
413       methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
414       try {
415         getNextToken();
416         functionDefinition(methodDecl);
417       } catch (SyntaxError sytaxErr1) {
418         return;
419       } finally {
420         int sourceEnd = methodDecl.sourceEnd;
421         if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
422           sourceEnd = methodDecl.declarationSourceStart + 1;
423         }
424         methodDecl.sourceEnd = sourceEnd;
425         methodDecl.declarationSourceEnd = sourceEnd;
426       }
427     }
428   }
429
430   protected CompilationUnitDeclaration endParse(int act) {
431
432     this.lastAct = act;
433
434     //    if (currentElement != null) {
435     //      currentElement.topElement().updateParseTree();
436     //      if (VERBOSE_RECOVERY) {
437     //        System.out.print(Util.bind("parser.syntaxRecovery")); //$NON-NLS-1$
438     //        System.out.println("--------------------------"); //$NON-NLS-1$
439     //        System.out.println(compilationUnit);
440     //        System.out.println("----------------------------------"); //$NON-NLS-1$
441     //      }
442     //    } else {
443     if (diet & VERBOSE_RECOVERY) {
444       System.out.print(Util.bind("parser.regularParse")); //$NON-NLS-1$
445       System.out.println("--------------------------"); //$NON-NLS-1$
446       System.out.println(compilationUnit);
447       System.out.println("----------------------------------"); //$NON-NLS-1$
448     }
449     //    }
450     if (scanner.recordLineSeparator) {
451       compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
452     }
453     if (scanner.taskTags != null) {
454       for (int i = 0; i < scanner.foundTaskCount; i++) {
455         problemReporter().task(new String(scanner.foundTaskTags[i]), new String(scanner.foundTaskMessages[i]),
456             scanner.foundTaskPriorities[i] == null ? null : new String(scanner.foundTaskPriorities[i]),
457             scanner.foundTaskPositions[i][0], scanner.foundTaskPositions[i][1]);
458       }
459     }
460     compilationUnit.imports = new ImportReference[includesList.size()];
461     for (int i = 0; i < includesList.size(); i++) {
462       compilationUnit.imports[i] = (ImportReference) includesList.get(i);
463     }
464     return compilationUnit;
465   }
466
467   private Block statementList() {
468     boolean branchStatement = false;
469     Statement statement;
470     int blockStart = scanner.getCurrentTokenStartPosition();
471     ArrayList blockStatements = new ArrayList();
472     do {
473       try {
474         statement = statement();
475         blockStatements.add(statement);
476         if (branchStatement && statement != null) {
477           //          reportSyntaxError("Unreachable code", statement.sourceStart, statement.sourceEnd);
478           problemReporter.unreachableCode(new String(scanner.getCurrentIdentifierSource()), statement.sourceStart,
479               statement.sourceEnd, referenceContext, compilationUnit.compilationResult);
480         }
481         if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
482             || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
483             || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
484             || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
485           return createBlock(blockStart, blockStatements);
486         }
487         branchStatement = checkUnreachableStatements(statement);
488       } catch (SyntaxError sytaxErr1) {
489         // if an error occured,
490         // try to find keywords
491         // to parse the rest of the string
492         boolean tokenize = scanner.tokenizeStrings;
493         if (!tokenize) {
494           scanner.tokenizeStrings = true;
495         }
496         try {
497           while (token != TokenNameEOF) {
498             if ((token == TokenNameRBRACE) || (token == TokenNamecase) || (token == TokenNamedefault) || (token == TokenNameelse)
499                 || (token == TokenNameelseif) || (token == TokenNameendif) || (token == TokenNameendfor)
500                 || (token == TokenNameendforeach) || (token == TokenNameendwhile) || (token == TokenNameendswitch)
501                 || (token == TokenNameenddeclare) || (token == TokenNameEOF) || (token == TokenNameERROR)) {
502               return createBlock(blockStart, blockStatements);
503             }
504             if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
505                 || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
506                 || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameglobal
507                 || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction || token == TokenNamedeclare
508                 || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow || token == TokenNamefinal
509                 || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
510               break;
511             }
512             //            System.out.println(scanner.toStringAction(token));
513             getNextToken();
514             //            System.out.println(scanner.toStringAction(token));
515           }
516           if (token == TokenNameEOF) {
517             throw sytaxErr1;
518           }
519         } finally {
520           scanner.tokenizeStrings = tokenize;
521         }
522       }
523     } while (true);
524   }
525
526   /**
527    * @param statement
528    * @return
529    */
530   private boolean checkUnreachableStatements(Statement statement) {
531     if (statement instanceof ReturnStatement || statement instanceof ContinueStatement || statement instanceof BreakStatement) {
532       return true;
533     } else if (statement instanceof IfStatement && ((IfStatement) statement).checkUnreachable) {
534       return true;
535     }
536     return false;
537   }
538
539   /**
540    * @param blockStart
541    * @param blockStatements
542    * @return
543    */
544   private Block createBlock(int blockStart, ArrayList blockStatements) {
545     int blockEnd = scanner.getCurrentTokenEndPosition();
546     Block b = Block.EmptyWith(blockStart, blockEnd);
547     b.statements = new Statement[blockStatements.size()];
548     blockStatements.toArray(b.statements);
549     return b;
550   }
551
552   private void functionBody(MethodDeclaration methodDecl) {
553     // '{' [statement-list] '}'
554     if (token == TokenNameLBRACE) {
555       getNextToken();
556     } else {
557       methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
558       throwSyntaxError("'{' expected in compound-statement.");
559     }
560     if (token != TokenNameRBRACE) {
561       statementList();
562     }
563     if (token == TokenNameRBRACE) {
564       methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
565       getNextToken();
566     } else {
567       methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
568       throwSyntaxError("'}' expected in compound-statement.");
569     }
570   }
571
572   private Statement statement() {
573     Statement statement = null;
574     Expression expression;
575     int sourceStart = scanner.getCurrentTokenStartPosition();
576     int sourceEnd;
577     if (token == TokenNameif) {
578       // T_IF '(' expr ')' statement elseif_list else_single
579       // T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
580       getNextToken();
581       if (token == TokenNameLPAREN) {
582         getNextToken();
583       } else {
584         throwSyntaxError("'(' expected after 'if' keyword.");
585       }
586       expression = expr();
587       if (token == TokenNameRPAREN) {
588         getNextToken();
589       } else {
590         throwSyntaxError("')' expected after 'if' condition.");
591       }
592       // create basic IfStatement
593       IfStatement ifStatement = new IfStatement(expression, null, null, sourceStart, -1);
594       if (token == TokenNameCOLON) {
595         getNextToken();
596         ifStatementColon(ifStatement);
597       } else {
598         ifStatement(ifStatement);
599       }
600       return ifStatement;
601     } else if (token == TokenNameswitch) {
602       getNextToken();
603       if (token == TokenNameLPAREN) {
604         getNextToken();
605       } else {
606         throwSyntaxError("'(' expected after 'switch' keyword.");
607       }
608       expr();
609       if (token == TokenNameRPAREN) {
610         getNextToken();
611       } else {
612         throwSyntaxError("')' expected after 'switch' condition.");
613       }
614       switchStatement();
615       return statement;
616     } else if (token == TokenNamefor) {
617       getNextToken();
618       if (token == TokenNameLPAREN) {
619         getNextToken();
620       } else {
621         throwSyntaxError("'(' expected after 'for' keyword.");
622       }
623       if (token == TokenNameSEMICOLON) {
624         getNextToken();
625       } else {
626         expressionList();
627         if (token == TokenNameSEMICOLON) {
628           getNextToken();
629         } else {
630           throwSyntaxError("';' expected after 'for'.");
631         }
632       }
633       if (token == TokenNameSEMICOLON) {
634         getNextToken();
635       } else {
636         expressionList();
637         if (token == TokenNameSEMICOLON) {
638           getNextToken();
639         } else {
640           throwSyntaxError("';' expected after 'for'.");
641         }
642       }
643       if (token == TokenNameRPAREN) {
644         getNextToken();
645       } else {
646         expressionList();
647         if (token == TokenNameRPAREN) {
648           getNextToken();
649         } else {
650           throwSyntaxError("')' expected after 'for'.");
651         }
652       }
653       forStatement();
654       return statement;
655     } else if (token == TokenNamewhile) {
656       getNextToken();
657       if (token == TokenNameLPAREN) {
658         getNextToken();
659       } else {
660         throwSyntaxError("'(' expected after 'while' keyword.");
661       }
662       expr();
663       if (token == TokenNameRPAREN) {
664         getNextToken();
665       } else {
666         throwSyntaxError("')' expected after 'while' condition.");
667       }
668       whileStatement();
669       return statement;
670     } else if (token == TokenNamedo) {
671       getNextToken();
672       if (token == TokenNameLBRACE) {
673         getNextToken();
674         if (token != TokenNameRBRACE) {
675           statementList();
676         }
677         if (token == TokenNameRBRACE) {
678           getNextToken();
679         } else {
680           throwSyntaxError("'}' expected after 'do' keyword.");
681         }
682       } else {
683         statement();
684       }
685       if (token == TokenNamewhile) {
686         getNextToken();
687         if (token == TokenNameLPAREN) {
688           getNextToken();
689         } else {
690           throwSyntaxError("'(' expected after 'while' keyword.");
691         }
692         expr();
693         if (token == TokenNameRPAREN) {
694           getNextToken();
695         } else {
696           throwSyntaxError("')' expected after 'while' condition.");
697         }
698       } else {
699         throwSyntaxError("'while' expected after 'do' keyword.");
700       }
701       if (token == TokenNameSEMICOLON) {
702         getNextToken();
703       } else {
704         if (token != TokenNameINLINE_HTML) {
705           throwSyntaxError("';' expected after do-while statement.");
706         }
707         getNextToken();
708       }
709       return statement;
710     } else if (token == TokenNameforeach) {
711       getNextToken();
712       if (token == TokenNameLPAREN) {
713         getNextToken();
714       } else {
715         throwSyntaxError("'(' expected after 'foreach' keyword.");
716       }
717       expr();
718       if (token == TokenNameas) {
719         getNextToken();
720       } else {
721         throwSyntaxError("'as' expected after 'foreach' exxpression.");
722       }
723       //      variable();
724       foreach_variable();
725       foreach_optional_arg();
726       if (token == TokenNameEQUAL_GREATER) {
727         getNextToken();
728         variable(false, false);
729       }
730       if (token == TokenNameRPAREN) {
731         getNextToken();
732       } else {
733         throwSyntaxError("')' expected after 'foreach' expression.");
734       }
735       foreachStatement();
736       return statement;
737     } else if (token == TokenNamebreak) {
738       expression = null;
739       getNextToken();
740       if (token != TokenNameSEMICOLON) {
741         expression = expr();
742       }
743       if (token == TokenNameSEMICOLON) {
744         sourceEnd = scanner.getCurrentTokenEndPosition();
745         getNextToken();
746       } else {
747         if (token != TokenNameINLINE_HTML) {
748           throwSyntaxError("';' expected after 'break'.");
749         }
750         sourceEnd = scanner.getCurrentTokenEndPosition();
751         getNextToken();
752       }
753       return new BreakStatement(null, sourceStart, sourceEnd);
754     } else if (token == TokenNamecontinue) {
755       expression = null;
756       getNextToken();
757       if (token != TokenNameSEMICOLON) {
758         expression = expr();
759       }
760       if (token == TokenNameSEMICOLON) {
761         sourceEnd = scanner.getCurrentTokenEndPosition();
762         getNextToken();
763       } else {
764         if (token != TokenNameINLINE_HTML) {
765           throwSyntaxError("';' expected after 'continue'.");
766         }
767         sourceEnd = scanner.getCurrentTokenEndPosition();
768         getNextToken();
769       }
770       return new ContinueStatement(null, sourceStart, sourceEnd);
771     } else if (token == TokenNamereturn) {
772       expression = null;
773       getNextToken();
774       if (token != TokenNameSEMICOLON) {
775         expression = expr();
776       }
777       if (token == TokenNameSEMICOLON) {
778         sourceEnd = scanner.getCurrentTokenEndPosition();
779         getNextToken();
780       } else {
781         if (token != TokenNameINLINE_HTML) {
782           throwSyntaxError("';' expected after 'return'.");
783         }
784         sourceEnd = scanner.getCurrentTokenEndPosition();
785         getNextToken();
786       }
787       return new ReturnStatement(expression, sourceStart, sourceEnd);
788     } else if (token == TokenNameecho) {
789       getNextToken();
790       expressionList();
791       if (token == TokenNameSEMICOLON) {
792         getNextToken();
793       } else {
794         if (token != TokenNameINLINE_HTML) {
795           throwSyntaxError("';' expected after 'echo' statement.");
796         }
797         getNextToken();
798       }
799       return statement;
800     } else if (token == TokenNameINLINE_HTML) {
801       getNextToken();
802       return statement;
803       //    } else if (token == TokenNameprint) {
804       //      getNextToken();
805       //      expression();
806       //      if (token == TokenNameSEMICOLON) {
807       //        getNextToken();
808       //      } else {
809       //        if (token != TokenNameStopPHP) {
810       //          throwSyntaxError("';' expected after 'print' statement.");
811       //        }
812       //        getNextToken();
813       //      }
814       //      return;
815     } else if (token == TokenNameglobal) {
816       getNextToken();
817       global_var_list();
818       if (token == TokenNameSEMICOLON) {
819         getNextToken();
820       } else {
821         if (token != TokenNameINLINE_HTML) {
822           throwSyntaxError("';' expected after 'global' statement.");
823         }
824         getNextToken();
825       }
826       return statement;
827     } else if (token == TokenNamestatic) {
828       getNextToken();
829       static_var_list();
830       if (token == TokenNameSEMICOLON) {
831         getNextToken();
832       } else {
833         if (token != TokenNameINLINE_HTML) {
834           throwSyntaxError("';' expected after 'static' statement.");
835         }
836         getNextToken();
837       }
838       return statement;
839     } else if (token == TokenNameunset) {
840       getNextToken();
841       if (token == TokenNameLPAREN) {
842         getNextToken();
843       } else {
844         throwSyntaxError("'(' expected after 'unset' statement.");
845       }
846       unset_variables();
847       if (token == TokenNameRPAREN) {
848         getNextToken();
849       } else {
850         throwSyntaxError("')' expected after 'unset' statement.");
851       }
852       if (token == TokenNameSEMICOLON) {
853         getNextToken();
854       } else {
855         if (token != TokenNameINLINE_HTML) {
856           throwSyntaxError("';' expected after 'unset' statement.");
857         }
858         getNextToken();
859       }
860       return statement;
861     } else if (token == TokenNamefunction) {
862       MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
863       methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
864       methodDecl.modifiers = AccDefault;
865       methodDecl.type = MethodDeclaration.FUNCTION_DEFINITION;
866       try {
867         getNextToken();
868         functionDefinition(methodDecl);
869       } finally {
870         sourceEnd = methodDecl.sourceEnd;
871         if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
872           sourceEnd = methodDecl.declarationSourceStart + 1;
873         }
874         methodDecl.declarationSourceEnd = sourceEnd;
875         methodDecl.sourceEnd = sourceEnd;
876       }
877       return statement;
878     } else if (token == TokenNamedeclare) {
879       //T_DECLARE '(' declare_list ')' declare_statement
880       getNextToken();
881       if (token != TokenNameLPAREN) {
882         throwSyntaxError("'(' expected in 'declare' statement.");
883       }
884       getNextToken();
885       declare_list();
886       if (token != TokenNameRPAREN) {
887         throwSyntaxError("')' expected in 'declare' statement.");
888       }
889       getNextToken();
890       declare_statement();
891       return statement;
892     } else if (token == TokenNametry) {
893       getNextToken();
894       if (token != TokenNameLBRACE) {
895         throwSyntaxError("'{' expected in 'try' statement.");
896       }
897       getNextToken();
898       statementList();
899       if (token != TokenNameRBRACE) {
900         throwSyntaxError("'}' expected in 'try' statement.");
901       }
902       getNextToken();
903       return statement;
904     } else if (token == TokenNamecatch) {
905       getNextToken();
906       if (token != TokenNameLPAREN) {
907         throwSyntaxError("'(' expected in 'catch' statement.");
908       }
909       getNextToken();
910       fully_qualified_class_name();
911       if (token != TokenNameVariable) {
912         throwSyntaxError("Variable expected in 'catch' statement.");
913       }
914       getNextToken();
915       if (token != TokenNameRPAREN) {
916         throwSyntaxError("')' expected in 'catch' statement.");
917       }
918       getNextToken();
919       if (token != TokenNameLBRACE) {
920         throwSyntaxError("'{' expected in 'catch' statement.");
921       }
922       getNextToken();
923       if (token != TokenNameRBRACE) {
924         statementList();
925         if (token != TokenNameRBRACE) {
926           throwSyntaxError("'}' expected in 'catch' statement.");
927         }
928       }
929       getNextToken();
930       additional_catches();
931       return statement;
932     } else if (token == TokenNamethrow) {
933       getNextToken();
934       expr();
935       if (token == TokenNameSEMICOLON) {
936         getNextToken();
937       } else {
938         throwSyntaxError("';' expected after 'throw' exxpression.");
939       }
940       return statement;
941     } else if (token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
942       try {
943         TypeDeclaration typeDecl = new TypeDeclaration(this.compilationUnit.compilationResult);
944         typeDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
945         typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
946         typeDecl.name = new char[] { ' ' };
947         // default super class
948         typeDecl.superclass = new SingleTypeReference(TypeConstants.OBJECT, 0);
949         compilationUnit.types.add(typeDecl);
950         pushOnAstStack(typeDecl);
951         unticked_class_declaration_statement(typeDecl);
952       } finally {
953         // reduce stack:
954         astPtr--;
955         astLengthPtr--;
956       }
957       return statement;
958       //      } else {
959       //        throwSyntaxError("Unexpected keyword '" + keyword + "'");
960     } else if (token == TokenNameLBRACE) {
961       getNextToken();
962       if (token != TokenNameRBRACE) {
963         statement = statementList();
964       }
965       if (token == TokenNameRBRACE) {
966         getNextToken();
967         return statement;
968       } else {
969         throwSyntaxError("'}' expected.");
970       }
971     } else {
972       if (token != TokenNameSEMICOLON) {
973         expr();
974       }
975       if (token == TokenNameSEMICOLON) {
976         getNextToken();
977         return statement;
978       } else {
979         if (token == TokenNameRBRACE) {
980           reportSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
981         } else {
982           if (token != TokenNameINLINE_HTML && token != TokenNameEOF) {
983             throwSyntaxError("';' expected after expression (Found token: " + scanner.toStringAction(token) + ")");
984           }
985           getNextToken();
986         }
987       }
988     }
989     // may be null
990     return statement;
991   }
992
993   private void declare_statement() {
994     //  statement
995     //| ':' inner_statement_list T_ENDDECLARE ';'
996     //;
997     if (token == TokenNameCOLON) {
998       getNextToken();
999       // TODO: implement inner_statement_list();
1000       statementList();
1001       if (token != TokenNameenddeclare) {
1002         throwSyntaxError("'enddeclare' expected in 'declare' statement.");
1003       }
1004       getNextToken();
1005       if (token != TokenNameSEMICOLON) {
1006         throwSyntaxError("';' expected after 'enddeclare' keyword.");
1007       }
1008       getNextToken();
1009     } else {
1010       statement();
1011     }
1012   }
1013
1014   private void declare_list() {
1015     //  T_STRING '=' static_scalar
1016     //| declare_list ',' T_STRING '=' static_scalar
1017     while (true) {
1018       if (token != TokenNameIdentifier) {
1019         throwSyntaxError("Identifier expected in 'declare' list.");
1020       }
1021       getNextToken();
1022       if (token != TokenNameEQUAL) {
1023         throwSyntaxError("'=' expected in 'declare' list.");
1024       }
1025       getNextToken();
1026       static_scalar();
1027       if (token != TokenNameCOMMA) {
1028         break;
1029       }
1030       getNextToken();
1031     }
1032   }
1033
1034   private void additional_catches() {
1035     while (token == TokenNamecatch) {
1036       getNextToken();
1037       if (token != TokenNameLPAREN) {
1038         throwSyntaxError("'(' expected in 'catch' statement.");
1039       }
1040       getNextToken();
1041       fully_qualified_class_name();
1042       if (token != TokenNameVariable) {
1043         throwSyntaxError("Variable expected in 'catch' statement.");
1044       }
1045       getNextToken();
1046       if (token != TokenNameRPAREN) {
1047         throwSyntaxError("')' expected in 'catch' statement.");
1048       }
1049       getNextToken();
1050       if (token != TokenNameLBRACE) {
1051         throwSyntaxError("'{' expected in 'catch' statement.");
1052       }
1053       getNextToken();
1054       if (token != TokenNameRBRACE) {
1055         statementList();
1056       }
1057       if (token != TokenNameRBRACE) {
1058         throwSyntaxError("'}' expected in 'catch' statement.");
1059       }
1060       getNextToken();
1061     }
1062   }
1063
1064   private void foreach_variable() {
1065     //  w_variable
1066     //| '&' w_variable
1067     if (token == TokenNameAND) {
1068       getNextToken();
1069     }
1070     w_variable(true);
1071   }
1072
1073   private void foreach_optional_arg() {
1074     //  /* empty */
1075     //| T_DOUBLE_ARROW foreach_variable
1076     if (token == TokenNameEQUAL_GREATER) {
1077       getNextToken();
1078       foreach_variable();
1079     }
1080   }
1081
1082   private void global_var_list() {
1083     //  global_var_list:
1084     //  global_var_list ',' global_var
1085     //| global_var
1086     HashSet set = peekVariableSet();
1087     while (true) {
1088       global_var(set);
1089       if (token != TokenNameCOMMA) {
1090         break;
1091       }
1092       getNextToken();
1093     }
1094   }
1095
1096   private void global_var(HashSet set) {
1097     //global_var:
1098     //  T_VARIABLE
1099     //| '$' r_variable
1100     //| '$' '{' expr '}'
1101     if (token == TokenNameVariable) {
1102       if (fMethodVariables != null) {
1103         VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_GLOBAL_VAR);
1104         fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1105       }
1106       addVariableSet(set);
1107       getNextToken();
1108     } else if (token == TokenNameDOLLAR) {
1109       getNextToken();
1110       if (token == TokenNameLBRACE) {
1111         getNextToken();
1112         expr();
1113         if (token != TokenNameRBRACE) {
1114           throwSyntaxError("'}' expected in global variable.");
1115         }
1116         getNextToken();
1117       } else {
1118         r_variable();
1119       }
1120     }
1121   }
1122
1123   private void static_var_list() {
1124     //static_var_list:
1125     //  static_var_list ',' T_VARIABLE
1126     //| static_var_list ',' T_VARIABLE '=' static_scalar
1127     //| T_VARIABLE
1128     //| T_VARIABLE '=' static_scalar,
1129     HashSet set = peekVariableSet();
1130     while (true) {
1131       if (token == TokenNameVariable) {
1132         if (fMethodVariables != null) {
1133           VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_STATIC_VAR);
1134           fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1135         }
1136         addVariableSet(set);
1137         getNextToken();
1138         if (token == TokenNameEQUAL) {
1139           getNextToken();
1140           static_scalar();
1141         }
1142         if (token != TokenNameCOMMA) {
1143           break;
1144         }
1145         getNextToken();
1146       } else {
1147         break;
1148       }
1149     }
1150   }
1151
1152   private void unset_variables() {
1153     //    unset_variables:
1154     //                  unset_variable
1155     //          | unset_variables ',' unset_variable
1156     //    unset_variable:
1157     //                  variable
1158     while (true) {
1159       variable(false, false);
1160       if (token != TokenNameCOMMA) {
1161         break;
1162       }
1163       getNextToken();
1164     }
1165   }
1166
1167   private final void initializeModifiers() {
1168     this.modifiers = 0;
1169     this.modifiersSourceStart = -1;
1170   }
1171
1172   private final void checkAndSetModifiers(int flag) {
1173     this.modifiers |= flag;
1174     if (this.modifiersSourceStart < 0)
1175       this.modifiersSourceStart = this.scanner.startPosition;
1176   }
1177
1178   private void unticked_class_declaration_statement(TypeDeclaration typeDecl) {
1179     initializeModifiers();
1180     if (token == TokenNameinterface) {
1181       //      interface_entry T_STRING
1182       //                interface_extends_list
1183       //                '{' class_statement_list '}'
1184       checkAndSetModifiers(AccInterface);
1185       getNextToken();
1186       typeDecl.modifiers = this.modifiers;
1187       typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1188       typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1189       if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1190         typeDecl.name = scanner.getCurrentIdentifierSource();
1191         if (token > TokenNameKEYWORD) {
1192           problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1193               scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1194           //          throwSyntaxError("Don't use a keyword for interface declaration [" + scanner.toStringAction(token) + "].",
1195           //              typeDecl.sourceStart, typeDecl.sourceEnd);
1196         }
1197         getNextToken();
1198         interface_extends_list(typeDecl);
1199       } else {
1200         typeDecl.name = new char[] { ' ' };
1201         throwSyntaxError("Interface name expected after keyword 'interface'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1202         return;
1203       }
1204     } else {
1205       //      class_entry_type T_STRING extends_from
1206       //                implements_list
1207       //                '{' class_statement_list'}'
1208       class_entry_type();
1209       typeDecl.modifiers = this.modifiers;
1210       typeDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1211       typeDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1212       //identifier
1213       //identifier 'extends' identifier
1214       if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
1215         typeDecl.name = scanner.getCurrentIdentifierSource();
1216         if (token > TokenNameKEYWORD) {
1217           problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1218               scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1219           //          throwSyntaxError("Don't use a keyword for class declaration [" + scanner.toStringAction(token) + "].",
1220           //              typeDecl.sourceStart, typeDecl.sourceEnd);
1221         }
1222         getNextToken();
1223         //    extends_from:
1224         //              /* empty */
1225         //      | T_EXTENDS fully_qualified_class_name
1226         if (token == TokenNameextends) {
1227           interface_extends_list(typeDecl);
1228           //          getNextToken();
1229           //          if (token != TokenNameIdentifier) {
1230           //            throwSyntaxError("Class name expected after keyword
1231           // 'extends'.",
1232           //                scanner.getCurrentTokenStartPosition(), scanner
1233           //                    .getCurrentTokenEndPosition());
1234           //          }
1235         }
1236         implements_list(typeDecl);
1237       } else {
1238         typeDecl.name = new char[] { ' ' };
1239         throwSyntaxError("Class name expected after keyword 'class'.", typeDecl.sourceStart, typeDecl.sourceEnd);
1240         return;
1241       }
1242     }
1243     //  '{' class_statement_list '}'
1244     if (token == TokenNameLBRACE) {
1245       getNextToken();
1246       if (token != TokenNameRBRACE) {
1247         ArrayList list = new ArrayList();
1248         class_statement_list(list);
1249         typeDecl.fields = new FieldDeclaration[list.size()];
1250         for (int i = 0; i < list.size(); i++) {
1251           typeDecl.fields[i] = (FieldDeclaration) list.get(i);
1252         }
1253       }
1254       if (token == TokenNameRBRACE) {
1255         typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1256         getNextToken();
1257       } else {
1258         throwSyntaxError("'}' expected at end of class body.");
1259       }
1260     } else {
1261       throwSyntaxError("'{' expected at start of class body.");
1262     }
1263   }
1264
1265   private void class_entry_type() {
1266     //  T_CLASS
1267     //  | T_ABSTRACT T_CLASS
1268     //  | T_FINAL T_CLASS
1269     if (token == TokenNameclass) {
1270       getNextToken();
1271     } else if (token == TokenNameabstract) {
1272       checkAndSetModifiers(AccAbstract);
1273       getNextToken();
1274       if (token != TokenNameclass) {
1275         throwSyntaxError("Keyword 'class' expected after keyword 'abstract'.");
1276       }
1277       getNextToken();
1278     } else if (token == TokenNamefinal) {
1279       checkAndSetModifiers(AccFinal);
1280       getNextToken();
1281       if (token != TokenNameclass) {
1282         throwSyntaxError("Keyword 'class' expected after keyword 'final'.");
1283       }
1284       getNextToken();
1285     } else {
1286       throwSyntaxError("Keyword 'class' 'final' or 'abstract' expected");
1287     }
1288   }
1289
1290   private void class_extends(TypeDeclaration typeDecl) {
1291     //  /* empty */
1292     //  | T_EXTENDS interface_list
1293     if (token == TokenNameextends) {
1294       getNextToken();
1295
1296       if (token == TokenNameIdentifier) {
1297         getNextToken();
1298       } else {
1299         throwSyntaxError("Class name expected after keyword 'extends'.");
1300       }
1301     }
1302   }
1303
1304   private void interface_extends_list(TypeDeclaration typeDecl) {
1305     //  /* empty */
1306     //  | T_EXTENDS interface_list
1307     if (token == TokenNameextends) {
1308       getNextToken();
1309       interface_list();
1310     }
1311   }
1312
1313   private void implements_list(TypeDeclaration typeDecl) {
1314     //  /* empty */
1315     //  | T_IMPLEMENTS interface_list
1316     if (token == TokenNameimplements) {
1317       getNextToken();
1318       interface_list();
1319     }
1320   }
1321
1322   private void interface_list() {
1323     //  interface_list:
1324     //  fully_qualified_class_name
1325     //| interface_list ',' fully_qualified_class_name
1326     do {
1327       if (token == TokenNameIdentifier) {
1328         getNextToken();
1329       } else {
1330         throwSyntaxError("Interface name expected after keyword 'implements'.");
1331       }
1332       if (token != TokenNameCOMMA) {
1333         return;
1334       }
1335       getNextToken();
1336     } while (true);
1337   }
1338
1339   //  private void classBody(TypeDeclaration typeDecl) {
1340   //    //'{' [class-element-list] '}'
1341   //    if (token == TokenNameLBRACE) {
1342   //      getNextToken();
1343   //      if (token != TokenNameRBRACE) {
1344   //        class_statement_list();
1345   //      }
1346   //      if (token == TokenNameRBRACE) {
1347   //        typeDecl.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1348   //        getNextToken();
1349   //      } else {
1350   //        throwSyntaxError("'}' expected at end of class body.");
1351   //      }
1352   //    } else {
1353   //      throwSyntaxError("'{' expected at start of class body.");
1354   //    }
1355   //  }
1356   private void class_statement_list(ArrayList list) {
1357     do {
1358       try {
1359         class_statement(list);
1360         if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1361             || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1362             || token == TokenNameconst) {
1363           continue;
1364         }
1365         if (token == TokenNameRBRACE) {
1366           break;
1367         }
1368         throwSyntaxError("'}' at end of class statement.");
1369       } catch (SyntaxError sytaxErr1) {
1370         boolean tokenize = scanner.tokenizeStrings;
1371         if (!tokenize) {
1372           scanner.tokenizeStrings = true;
1373         }
1374         try {
1375           // if an error occured,
1376           // try to find keywords
1377           // to parse the rest of the string
1378           while (token != TokenNameEOF) {
1379             if (token == TokenNamepublic || token == TokenNameprotected || token == TokenNameprivate || token == TokenNamestatic
1380                 || token == TokenNameabstract || token == TokenNamefinal || token == TokenNamefunction || token == TokenNamevar
1381                 || token == TokenNameconst) {
1382               break;
1383             }
1384             //            System.out.println(scanner.toStringAction(token));
1385             getNextToken();
1386           }
1387           if (token == TokenNameEOF) {
1388             throw sytaxErr1;
1389           }
1390         } finally {
1391           scanner.tokenizeStrings = tokenize;
1392         }
1393       }
1394     } while (true);
1395   }
1396
1397   private void class_statement(ArrayList list) {
1398     //    class_statement:
1399     //          variable_modifiers class_variable_declaration ';'
1400     //  | class_constant_declaration ';'
1401     //  | method_modifiers T_FUNCTION is_reference T_STRING
1402     //    '(' parameter_list ')' method_body
1403     initializeModifiers();
1404     int declarationSourceStart = scanner.getCurrentTokenStartPosition();
1405
1406     if (token == TokenNamevar) {
1407       checkAndSetModifiers(AccPublic);
1408       problemReporter.phpVarDeprecatedWarning(scanner.getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition(),
1409           referenceContext, compilationUnit.compilationResult);
1410       getNextToken();
1411       class_variable_declaration(declarationSourceStart, list);
1412     } else if (token == TokenNameconst) {
1413       checkAndSetModifiers(AccFinal | AccPublic);
1414       class_constant_declaration(declarationSourceStart, list);
1415       if (token != TokenNameSEMICOLON) {
1416         throwSyntaxError("';' expected after class const declaration.");
1417       }
1418       getNextToken();
1419     } else {
1420       boolean hasModifiers = member_modifiers();
1421       if (token == TokenNamefunction) {
1422         if (!hasModifiers) {
1423           checkAndSetModifiers(AccPublic);
1424         }
1425         MethodDeclaration methodDecl = new MethodDeclaration(this.compilationUnit.compilationResult);
1426         methodDecl.declarationSourceStart = scanner.getCurrentTokenStartPosition();
1427         methodDecl.modifiers = this.modifiers;
1428         methodDecl.type = MethodDeclaration.METHOD_DEFINITION;
1429         try {
1430           getNextToken();
1431           functionDefinition(methodDecl);
1432         } finally {
1433           int sourceEnd = methodDecl.sourceEnd;
1434           if (sourceEnd <= 0 || methodDecl.declarationSourceStart > sourceEnd) {
1435             sourceEnd = methodDecl.declarationSourceStart + 1;
1436           }
1437           methodDecl.declarationSourceEnd = sourceEnd;
1438           methodDecl.sourceEnd = sourceEnd;
1439         }
1440       } else {
1441         if (!hasModifiers) {
1442           throwSyntaxError("'public' 'private' or 'protected' modifier expected for field declarations.");
1443         }
1444         class_variable_declaration(declarationSourceStart, list);
1445       }
1446     }
1447   }
1448
1449   private void class_constant_declaration(int declarationSourceStart, ArrayList list) {
1450     //  class_constant_declaration ',' T_STRING '=' static_scalar
1451     //  | T_CONST T_STRING '=' static_scalar
1452     if (token != TokenNameconst) {
1453       throwSyntaxError("'const' keyword expected in class declaration.");
1454     } else {
1455       getNextToken();
1456     }
1457     while (true) {
1458       if (token != TokenNameIdentifier) {
1459         throwSyntaxError("Identifier expected in class const declaration.");
1460       }
1461       FieldDeclaration fieldDeclaration = new FieldDeclaration(scanner.getCurrentIdentifierSource(), scanner
1462           .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1463       fieldDeclaration.modifiers = this.modifiers;
1464       fieldDeclaration.declarationSourceStart = declarationSourceStart;
1465       fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1466       fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1467       //        fieldDeclaration.type
1468       list.add(fieldDeclaration);
1469       getNextToken();
1470       if (token != TokenNameEQUAL) {
1471         throwSyntaxError("'=' expected in class const declaration.");
1472       }
1473       getNextToken();
1474       static_scalar();
1475       if (token != TokenNameCOMMA) {
1476         break; // while(true)-loop
1477       }
1478       getNextToken();
1479     }
1480   }
1481
1482   //  private void variable_modifiers() {
1483   //    // variable_modifiers:
1484   //    // non_empty_member_modifiers
1485   //    //| T_VAR
1486   //    initializeModifiers();
1487   //    if (token == TokenNamevar) {
1488   //      checkAndSetModifiers(AccPublic);
1489   //      reportSyntaxError(
1490   //          "Keyword 'var' is deprecated. Please use 'public' 'private' or
1491   // 'protected'
1492   // modifier for field declarations.",
1493   //          scanner.getCurrentTokenStartPosition(), scanner
1494   //              .getCurrentTokenEndPosition());
1495   //      getNextToken();
1496   //    } else {
1497   //      if (!member_modifiers()) {
1498   //        throwSyntaxError("'public' 'private' or 'protected' modifier expected for
1499   // field declarations.");
1500   //      }
1501   //    }
1502   //  }
1503   //  private void method_modifiers() {
1504   //    //method_modifiers:
1505   //    // /* empty */
1506   //    //| non_empty_member_modifiers
1507   //    initializeModifiers();
1508   //    if (!member_modifiers()) {
1509   //      checkAndSetModifiers(AccPublic);
1510   //    }
1511   //  }
1512   private boolean member_modifiers() {
1513     //  T_PUBLIC
1514     //| T_PROTECTED
1515     //| T_PRIVATE
1516     //| T_STATIC
1517     //| T_ABSTRACT
1518     //| T_FINAL
1519     boolean foundToken = false;
1520     while (true) {
1521       if (token == TokenNamepublic) {
1522         checkAndSetModifiers(AccPublic);
1523         getNextToken();
1524         foundToken = true;
1525       } else if (token == TokenNameprotected) {
1526         checkAndSetModifiers(AccProtected);
1527         getNextToken();
1528         foundToken = true;
1529       } else if (token == TokenNameprivate) {
1530         checkAndSetModifiers(AccPrivate);
1531         getNextToken();
1532         foundToken = true;
1533       } else if (token == TokenNamestatic) {
1534         checkAndSetModifiers(AccStatic);
1535         getNextToken();
1536         foundToken = true;
1537       } else if (token == TokenNameabstract) {
1538         checkAndSetModifiers(AccAbstract);
1539         getNextToken();
1540         foundToken = true;
1541       } else if (token == TokenNamefinal) {
1542         checkAndSetModifiers(AccFinal);
1543         getNextToken();
1544         foundToken = true;
1545       } else {
1546         break;
1547       }
1548     }
1549     return foundToken;
1550   }
1551
1552   private void class_variable_declaration(int declarationSourceStart, ArrayList list) {
1553     //    class_variable_declaration:
1554     //          class_variable_declaration ',' T_VARIABLE
1555     //  | class_variable_declaration ',' T_VARIABLE '=' static_scalar
1556     //  | T_VARIABLE
1557     //  | T_VARIABLE '=' static_scalar
1558     char[] classVariable;
1559     do {
1560       if (token == TokenNameVariable) {
1561         classVariable = scanner.getCurrentIdentifierSource();
1562         //  indexManager.addIdentifierInformation('v', classVariable, buf, -1, -1);
1563         FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner.getCurrentTokenStartPosition(), scanner
1564             .getCurrentTokenEndPosition());
1565         fieldDeclaration.modifiers = this.modifiers;
1566         fieldDeclaration.declarationSourceStart = declarationSourceStart;
1567         fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1568         fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1569         list.add(fieldDeclaration);
1570         if (fTypeVariables != null) {
1571           VariableInfo info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_CLASS_UNIT);
1572           fTypeVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1573         }
1574         getNextToken();
1575         if (token == TokenNameEQUAL) {
1576           getNextToken();
1577           static_scalar();
1578         }
1579       } else {
1580         //        if (token == TokenNamethis) {
1581         //          throwSyntaxError("'$this' not allowed after keyword 'public'
1582         // 'protected' 'private' 'var'.");
1583         //        }
1584         throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1585       }
1586       if (token != TokenNameCOMMA) {
1587         break;
1588       }
1589       getNextToken();
1590     } while (true);
1591     if (token != TokenNameSEMICOLON) {
1592       throwSyntaxError("';' expected after field declaration.");
1593     }
1594     getNextToken();
1595   }
1596
1597   private void functionDefinition(MethodDeclaration methodDecl) {
1598     boolean isAbstract = false;
1599     if (astPtr == 0) {
1600       if (compilationUnit != null) {
1601         compilationUnit.types.add(methodDecl);
1602       }
1603     } else {
1604       ASTNode node = astStack[astPtr];
1605       if (node instanceof TypeDeclaration) {
1606         TypeDeclaration typeDecl = ((TypeDeclaration) node);
1607         if (typeDecl.methods == null) {
1608           typeDecl.methods = new AbstractMethodDeclaration[] { methodDecl };
1609         } else {
1610           AbstractMethodDeclaration[] newMethods;
1611           System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 0,
1612               typeDecl.methods.length);
1613           newMethods[typeDecl.methods.length] = methodDecl;
1614           typeDecl.methods = newMethods;
1615         }
1616         if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1617           isAbstract = true;
1618         } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1619           isAbstract = true;
1620         }
1621       }
1622     }
1623     try {
1624       pushFunctionVariableSet();
1625       functionDeclarator(methodDecl);
1626       if (token == TokenNameSEMICOLON) {
1627         if (!isAbstract) {
1628           methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1629           throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1630         }
1631         getNextToken();
1632         return;
1633       }
1634       functionBody(methodDecl);
1635     } finally {
1636       if (!fStackUnassigned.isEmpty()) {
1637         fStackUnassigned.remove(fStackUnassigned.size() - 1);
1638       }
1639     }
1640   }
1641
1642   private void functionDeclarator(MethodDeclaration methodDecl) {
1643     //identifier '(' [parameter-list] ')'
1644     if (token == TokenNameAND) {
1645       getNextToken();
1646     }
1647     methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1648     methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1649     if (Scanner.isIdentifierOrKeyword(token)) {
1650       methodDecl.selector = scanner.getCurrentIdentifierSource();
1651       if (token > TokenNameKEYWORD) {
1652         problemReporter.phpKeywordWarning(new String[] { scanner.toStringAction(token) }, scanner.getCurrentTokenStartPosition(),
1653             scanner.getCurrentTokenEndPosition(), referenceContext, compilationUnit.compilationResult);
1654       }
1655       getNextToken();
1656       if (token == TokenNameLPAREN) {
1657         getNextToken();
1658       } else {
1659         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1660         throwSyntaxError("'(' expected in function declaration.");
1661       }
1662       if (token != TokenNameRPAREN) {
1663         parameter_list(methodDecl);
1664       }
1665       if (token != TokenNameRPAREN) {
1666         methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1667         throwSyntaxError("')' expected in function declaration.");
1668       } else {
1669         methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1670         getNextToken();
1671       }
1672     } else {
1673       methodDecl.selector = "<undefined>".toCharArray();
1674       methodDecl.sourceEnd = scanner.getCurrentTokenStartPosition() - 1;
1675       throwSyntaxError("Function name expected after keyword 'function'.");
1676     }
1677   }
1678
1679   //
1680   private void parameter_list(MethodDeclaration methodDecl) {
1681     //  non_empty_parameter_list
1682     //  | /* empty */
1683     non_empty_parameter_list(methodDecl, true);
1684   }
1685
1686   private void non_empty_parameter_list(MethodDeclaration methodDecl, boolean empty_allowed) {
1687     //  optional_class_type T_VARIABLE
1688     //  | optional_class_type '&' T_VARIABLE
1689     //  | optional_class_type '&' T_VARIABLE '=' static_scalar
1690     //  | optional_class_type T_VARIABLE '=' static_scalar
1691     //  | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1692     //  | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1693     //  | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1694     // static_scalar
1695     //  | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1696     // static_scalar
1697     char[] typeIdentifier = null;
1698     if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1699       HashSet set = peekVariableSet();
1700       while (true) {
1701         if (token == TokenNameIdentifier) {
1702           typeIdentifier = scanner.getCurrentIdentifierSource();
1703           getNextToken();
1704         }
1705         if (token == TokenNameAND) {
1706           getNextToken();
1707         }
1708         if (token == TokenNameVariable) {
1709           if (fMethodVariables != null) {
1710             VariableInfo info;
1711             if (methodDecl.type == MethodDeclaration.FUNCTION_DEFINITION) {
1712               info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_FUNCTION_DEFINITION);
1713             } else {
1714               info = new VariableInfo(scanner.getCurrentTokenStartPosition(), VariableInfo.LEVEL_METHOD_DEFINITION);
1715             }
1716             info.typeIdentifier = typeIdentifier;
1717             fMethodVariables.put(new String(scanner.getCurrentIdentifierSource()), info);
1718           }
1719           addVariableSet(set);
1720           getNextToken();
1721           if (token == TokenNameEQUAL) {
1722             getNextToken();
1723             static_scalar();
1724           }
1725         } else {
1726           throwSyntaxError("Variable expected in parameter list.");
1727         }
1728         if (token != TokenNameCOMMA) {
1729           break;
1730         }
1731         getNextToken();
1732       }
1733       return;
1734     }
1735     if (!empty_allowed) {
1736       throwSyntaxError("Identifier expected in parameter list.");
1737     }
1738   }
1739
1740   private void optional_class_type() {
1741     //  /* empty */
1742     //| T_STRING
1743   }
1744
1745   //  private void parameterDeclaration() {
1746   //    //variable
1747   //    //variable-reference
1748   //    if (token == TokenNameAND) {
1749   //      getNextToken();
1750   //      if (isVariable()) {
1751   //        getNextToken();
1752   //      } else {
1753   //        throwSyntaxError("Variable expected after reference operator '&'.");
1754   //      }
1755   //    }
1756   //    //variable '=' constant
1757   //    if (token == TokenNameVariable) {
1758   //      getNextToken();
1759   //      if (token == TokenNameEQUAL) {
1760   //        getNextToken();
1761   //        static_scalar();
1762   //      }
1763   //      return;
1764   //    }
1765   //    // if (token == TokenNamethis) {
1766   //    // throwSyntaxError("Reserved word '$this' not allowed in parameter
1767   //    // declaration.");
1768   //    // }
1769   //  }
1770
1771   private void labeledStatementList() {
1772     if (token != TokenNamecase && token != TokenNamedefault) {
1773       throwSyntaxError("'case' or 'default' expected.");
1774     }
1775     do {
1776       if (token == TokenNamecase) {
1777         getNextToken();
1778         expr(); //constant();
1779         if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1780           getNextToken();
1781           if (token == TokenNamecase || token == TokenNamedefault) {
1782             // empty case statement ?
1783             continue;
1784           }
1785           statementList();
1786         }
1787         //        else if (token == TokenNameSEMICOLON) {
1788         //          setMarker(
1789         //            "':' expected after 'case' keyword (Found token: " +
1790         // scanner.toStringAction(token) + ")",
1791         //            scanner.getCurrentTokenStartPosition(),
1792         //            scanner.getCurrentTokenEndPosition(),
1793         //            INFO);
1794         //          getNextToken();
1795         //          if (token == TokenNamecase) { // empty case statement ?
1796         //            continue;
1797         //          }
1798         //          statementList();
1799         //        }
1800         else {
1801           throwSyntaxError("':' character expected after 'case' constant (Found token: " + scanner.toStringAction(token) + ")");
1802         }
1803       } else { // TokenNamedefault
1804         getNextToken();
1805         if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1806           getNextToken();
1807           if (token == TokenNameRBRACE) {
1808             // empty default case
1809             break;
1810           }
1811           if (token != TokenNamecase) {
1812             statementList();
1813           }
1814         } else {
1815           throwSyntaxError("':' character expected after 'default'.");
1816         }
1817       }
1818     } while (token == TokenNamecase || token == TokenNamedefault);
1819   }
1820
1821   private void ifStatementColon(IfStatement iState) {
1822     //  T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
1823     HashSet assignedVariableSet = null;
1824     try {
1825       Block b = inner_statement_list();
1826       iState.thenStatement = b;
1827       checkUnreachable(iState, b);
1828     } finally {
1829       assignedVariableSet = removeIfVariableSet();
1830     }
1831     if (token == TokenNameelseif) {
1832       try {
1833         pushIfVariableSet();
1834         new_elseif_list(iState);
1835       } finally {
1836         HashSet set = removeIfVariableSet();
1837         if (assignedVariableSet != null) {
1838           assignedVariableSet.addAll(set);
1839         }
1840       }
1841     }
1842     try {
1843       pushIfVariableSet();
1844       new_else_single(iState);
1845     } finally {
1846       HashSet set = removeIfVariableSet();
1847       if (assignedVariableSet != null) {
1848         HashSet topSet = peekVariableSet();
1849         if (topSet != null) {
1850           topSet.addAll(set);
1851           topSet.addAll(assignedVariableSet);
1852         }
1853       }
1854     }
1855     if (token != TokenNameendif) {
1856       throwSyntaxError("'endif' expected.");
1857     }
1858     getNextToken();
1859     if (token != TokenNameSEMICOLON) {
1860       reportSyntaxError("';' expected after if-statement.");
1861       iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1862     } else {
1863       iState.sourceEnd = scanner.getCurrentTokenEndPosition();
1864       getNextToken();
1865     }
1866   }
1867
1868   private void ifStatement(IfStatement iState) {
1869     //  T_IF '(' expr ')' statement elseif_list else_single
1870     HashSet assignedVariableSet = null;
1871     try {
1872       pushIfVariableSet();
1873       Statement s = statement();
1874       iState.thenStatement = s;
1875       checkUnreachable(iState, s);
1876     } finally {
1877       assignedVariableSet = removeIfVariableSet();
1878     }
1879
1880     if (token == TokenNameelseif) {
1881       try {
1882         pushIfVariableSet();
1883         elseif_list(iState);
1884       } finally {
1885         HashSet set = removeIfVariableSet();
1886         if (assignedVariableSet != null) {
1887           assignedVariableSet.addAll(set);
1888         }
1889       }
1890     }
1891     try {
1892       pushIfVariableSet();
1893       else_single(iState);
1894     } finally {
1895       HashSet set = removeIfVariableSet();
1896       if (assignedVariableSet != null) {
1897         HashSet topSet = peekVariableSet();
1898         if (topSet != null) {
1899           topSet.addAll(set);
1900           topSet.addAll(assignedVariableSet);
1901         }
1902       }
1903     }
1904   }
1905
1906   private void elseif_list(IfStatement iState) {
1907     //  /* empty */
1908     //| elseif_list T_ELSEIF '(' expr ')' statement
1909     ArrayList conditionList = new ArrayList();
1910     ArrayList statementList = new ArrayList();
1911     Expression e;
1912     Statement s;
1913     while (token == TokenNameelseif) {
1914       getNextToken();
1915       if (token == TokenNameLPAREN) {
1916         getNextToken();
1917       } else {
1918         throwSyntaxError("'(' expected after 'elseif' keyword.");
1919       }
1920       e = expr();
1921       conditionList.add(e);
1922       if (token == TokenNameRPAREN) {
1923         getNextToken();
1924       } else {
1925         throwSyntaxError("')' expected after 'elseif' condition.");
1926       }
1927       s = statement();
1928       statementList.add(s);
1929       checkUnreachable(iState, s);
1930     }
1931     iState.elseifConditions = new Expression[conditionList.size()];
1932     iState.elseifStatements = new Statement[statementList.size()];
1933     conditionList.toArray(iState.elseifConditions);
1934     statementList.toArray(iState.elseifStatements);
1935   }
1936
1937   private void new_elseif_list(IfStatement iState) {
1938     //  /* empty */
1939     //| new_elseif_list T_ELSEIF '(' expr ')' ':' inner_statement_list
1940     ArrayList conditionList = new ArrayList();
1941     ArrayList statementList = new ArrayList();
1942     Expression e;
1943     Block b;
1944     while (token == TokenNameelseif) {
1945       getNextToken();
1946       if (token == TokenNameLPAREN) {
1947         getNextToken();
1948       } else {
1949         throwSyntaxError("'(' expected after 'elseif' keyword.");
1950       }
1951       e = expr();
1952       conditionList.add(e);
1953       if (token == TokenNameRPAREN) {
1954         getNextToken();
1955       } else {
1956         throwSyntaxError("')' expected after 'elseif' condition.");
1957       }
1958       if (token == TokenNameCOLON) {
1959         getNextToken();
1960       } else {
1961         throwSyntaxError("':' expected after 'elseif' keyword.");
1962       }
1963       b = inner_statement_list();
1964       statementList.add(b);
1965       checkUnreachable(iState, b);
1966     }
1967     iState.elseifConditions = new Expression[conditionList.size()];
1968     iState.elseifStatements = new Statement[statementList.size()];
1969     conditionList.toArray(iState.elseifConditions);
1970     statementList.toArray(iState.elseifStatements);
1971   }
1972
1973   private void else_single(IfStatement iState) {
1974     // /* empty */
1975     // T_ELSE statement
1976     if (token == TokenNameelse) {
1977       getNextToken();
1978       Statement s = statement();
1979       iState.elseStatement = s;
1980       checkUnreachable(iState, s);
1981     } else {
1982       iState.checkUnreachable = false;
1983     }
1984     iState.sourceEnd = scanner.getCurrentTokenStartPosition();
1985   }
1986
1987   private void new_else_single(IfStatement iState) {
1988     //  /* empty */
1989     //| T_ELSE ':' inner_statement_list
1990     if (token == TokenNameelse) {
1991       getNextToken();
1992       if (token == TokenNameCOLON) {
1993         getNextToken();
1994       } else {
1995         throwSyntaxError("':' expected after 'else' keyword.");
1996       }
1997       Block b = inner_statement_list();
1998       iState.elseStatement = b;
1999       checkUnreachable(iState, b);
2000     } else {
2001       iState.checkUnreachable = false;
2002     }
2003   }
2004
2005   private Block inner_statement_list() {
2006     //  inner_statement_list inner_statement
2007     //  /* empty */
2008     return statementList();
2009   }
2010
2011   /**
2012    * @param iState
2013    * @param b
2014    */
2015   private void checkUnreachable(IfStatement iState, Statement s) {
2016     if (s instanceof Block) {
2017       Block b = (Block) s;
2018       if (b.statements == null || b.statements.length == 0) {
2019         iState.checkUnreachable = false;
2020       } else {
2021         int off = b.statements.length - 1;
2022         if (!(b.statements[off] instanceof ReturnStatement) && !(b.statements[off] instanceof ContinueStatement)
2023             && !(b.statements[off] instanceof BreakStatement)) {
2024           if (!(b.statements[off] instanceof IfStatement) || !((IfStatement) b.statements[off]).checkUnreachable) {
2025             iState.checkUnreachable = false;
2026           }
2027         }
2028       }
2029     } else {
2030       if (!(s instanceof ReturnStatement) && !(s instanceof ContinueStatement) && !(s instanceof BreakStatement)) {
2031         if (!(s instanceof IfStatement) || !((IfStatement) s).checkUnreachable) {
2032           iState.checkUnreachable = false;
2033         }
2034       }
2035     }
2036   }
2037
2038   //  private void elseifStatementList() {
2039   //    do {
2040   //      elseifStatement();
2041   //      switch (token) {
2042   //      case TokenNameelse:
2043   //        getNextToken();
2044   //        if (token == TokenNameCOLON) {
2045   //          getNextToken();
2046   //          if (token != TokenNameendif) {
2047   //            statementList();
2048   //          }
2049   //          return;
2050   //        } else {
2051   //          if (token == TokenNameif) { //'else if'
2052   //            getNextToken();
2053   //          } else {
2054   //            throwSyntaxError("':' expected after 'else'.");
2055   //          }
2056   //        }
2057   //        break;
2058   //      case TokenNameelseif:
2059   //        getNextToken();
2060   //        break;
2061   //      default:
2062   //        return;
2063   //      }
2064   //    } while (true);
2065   //  }
2066
2067   //  private void elseifStatement() {
2068   //    if (token == TokenNameLPAREN) {
2069   //      getNextToken();
2070   //      expr();
2071   //      if (token != TokenNameRPAREN) {
2072   //        throwSyntaxError("')' expected in else-if-statement.");
2073   //      }
2074   //      getNextToken();
2075   //      if (token != TokenNameCOLON) {
2076   //        throwSyntaxError("':' expected in else-if-statement.");
2077   //      }
2078   //      getNextToken();
2079   //      if (token != TokenNameendif) {
2080   //        statementList();
2081   //      }
2082   //    }
2083   //  }
2084
2085   private void switchStatement() {
2086     if (token == TokenNameCOLON) {
2087       // ':' [labeled-statement-list] 'endswitch' ';'
2088       getNextToken();
2089       labeledStatementList();
2090       if (token != TokenNameendswitch) {
2091         throwSyntaxError("'endswitch' expected.");
2092       }
2093       getNextToken();
2094       if (token != TokenNameSEMICOLON) {
2095         throwSyntaxError("';' expected after switch-statement.");
2096       }
2097       getNextToken();
2098     } else {
2099       // '{' [labeled-statement-list] '}'
2100       if (token != TokenNameLBRACE) {
2101         throwSyntaxError("'{' expected in switch statement.");
2102       }
2103       getNextToken();
2104       if (token != TokenNameRBRACE) {
2105         labeledStatementList();
2106       }
2107       if (token != TokenNameRBRACE) {
2108         throwSyntaxError("'}' expected in switch statement.");
2109       }
2110       getNextToken();
2111     }
2112   }
2113
2114   private void forStatement() {
2115     if (token == TokenNameCOLON) {
2116       getNextToken();
2117       statementList();
2118       if (token != TokenNameendfor) {
2119         throwSyntaxError("'endfor' expected.");
2120       }
2121       getNextToken();
2122       if (token != TokenNameSEMICOLON) {
2123         throwSyntaxError("';' expected after for-statement.");
2124       }
2125       getNextToken();
2126     } else {
2127       statement();
2128     }
2129   }
2130
2131   private void whileStatement() {
2132     // ':' statement-list 'endwhile' ';'
2133     if (token == TokenNameCOLON) {
2134       getNextToken();
2135       statementList();
2136       if (token != TokenNameendwhile) {
2137         throwSyntaxError("'endwhile' expected.");
2138       }
2139       getNextToken();
2140       if (token != TokenNameSEMICOLON) {
2141         throwSyntaxError("';' expected after while-statement.");
2142       }
2143       getNextToken();
2144     } else {
2145       statement();
2146     }
2147   }
2148
2149   private void foreachStatement() {
2150     if (token == TokenNameCOLON) {
2151       getNextToken();
2152       statementList();
2153       if (token != TokenNameendforeach) {
2154         throwSyntaxError("'endforeach' expected.");
2155       }
2156       getNextToken();
2157       if (token != TokenNameSEMICOLON) {
2158         throwSyntaxError("';' expected after foreach-statement.");
2159       }
2160       getNextToken();
2161     } else {
2162       statement();
2163     }
2164   }
2165
2166   //  private void exitStatus() {
2167   //    if (token == TokenNameLPAREN) {
2168   //      getNextToken();
2169   //    } else {
2170   //      throwSyntaxError("'(' expected in 'exit-status'.");
2171   //    }
2172   //    if (token != TokenNameRPAREN) {
2173   //      expression();
2174   //    }
2175   //    if (token == TokenNameRPAREN) {
2176   //      getNextToken();
2177   //    } else {
2178   //      throwSyntaxError("')' expected after 'exit-status'.");
2179   //    }
2180   //  }
2181   private void expressionList() {
2182     do {
2183       expr();
2184       if (token == TokenNameCOMMA) {
2185         getNextToken();
2186       } else {
2187         break;
2188       }
2189     } while (true);
2190   }
2191
2192   private Expression expr() {
2193     //  r_variable
2194     //  | expr_without_variable
2195     //    if (token!=TokenNameEOF) {
2196     if (Scanner.TRACE) {
2197       System.out.println("TRACE: expr()");
2198     }
2199     return expr_without_variable(true);
2200     //    }
2201   }
2202
2203   private Expression expr_without_variable(boolean only_variable) {
2204     int exprSourceStart = scanner.getCurrentTokenStartPosition();
2205     int exprSourceEnd = scanner.getCurrentTokenEndPosition();
2206     Expression expression = new Expression();
2207     expression.sourceStart = exprSourceStart;
2208     // default, may be overwritten
2209     expression.sourceEnd = exprSourceEnd;
2210     try {
2211       //                internal_functions_in_yacc
2212       //        | T_CLONE expr
2213       //        | T_PRINT expr
2214       //        | '(' expr ')'
2215       //        | '@' expr
2216       //        | '+' expr
2217       //        | '-' expr
2218       //        | '!' expr
2219       //        | '~' expr
2220       //        | T_INC rw_variable
2221       //        | T_DEC rw_variable
2222       //        | T_INT_CAST expr
2223       //        | T_DOUBLE_CAST expr
2224       //        | T_STRING_CAST expr
2225       //        | T_ARRAY_CAST expr
2226       //        | T_OBJECT_CAST expr
2227       //        | T_BOOL_CAST expr
2228       //        | T_UNSET_CAST expr
2229       //        | T_EXIT exit_expr
2230       //        | scalar
2231       //        | T_ARRAY '(' array_pair_list ')'
2232       //        | '`' encaps_list '`'
2233       //        | T_LIST '(' assignment_list ')' '=' expr
2234       //        | T_NEW class_name_reference ctor_arguments
2235       //        | variable '=' expr
2236       //        | variable '=' '&' variable
2237       //        | variable '=' '&' T_NEW class_name_reference ctor_arguments
2238       //        | variable T_PLUS_EQUAL expr
2239       //        | variable T_MINUS_EQUAL expr
2240       //        | variable T_MUL_EQUAL expr
2241       //        | variable T_DIV_EQUAL expr
2242       //        | variable T_CONCAT_EQUAL expr
2243       //        | variable T_MOD_EQUAL expr
2244       //        | variable T_AND_EQUAL expr
2245       //        | variable T_OR_EQUAL expr
2246       //        | variable T_XOR_EQUAL expr
2247       //        | variable T_SL_EQUAL expr
2248       //        | variable T_SR_EQUAL expr
2249       //        | rw_variable T_INC
2250       //        | rw_variable T_DEC
2251       //        | expr T_BOOLEAN_OR expr
2252       //        | expr T_BOOLEAN_AND expr
2253       //        | expr T_LOGICAL_OR expr
2254       //        | expr T_LOGICAL_AND expr
2255       //        | expr T_LOGICAL_XOR expr
2256       //        | expr '|' expr
2257       //        | expr '&' expr
2258       //        | expr '^' expr
2259       //        | expr '.' expr
2260       //        | expr '+' expr
2261       //        | expr '-' expr
2262       //        | expr '*' expr
2263       //        | expr '/' expr
2264       //        | expr '%' expr
2265       //        | expr T_SL expr
2266       //        | expr T_SR expr
2267       //        | expr T_IS_IDENTICAL expr
2268       //        | expr T_IS_NOT_IDENTICAL expr
2269       //        | expr T_IS_EQUAL expr
2270       //        | expr T_IS_NOT_EQUAL expr
2271       //        | expr '<' expr
2272       //        | expr T_IS_SMALLER_OR_EQUAL expr
2273       //        | expr '>' expr
2274       //        | expr T_IS_GREATER_OR_EQUAL expr
2275       //        | expr T_INSTANCEOF class_name_reference
2276       //        | expr '?' expr ':' expr
2277       if (Scanner.TRACE) {
2278         System.out.println("TRACE: expr_without_variable() PART 1");
2279       }
2280       switch (token) {
2281       case TokenNameisset:
2282         //      T_ISSET '(' isset_variables ')'
2283         getNextToken();
2284         if (token != TokenNameLPAREN) {
2285           throwSyntaxError("'(' expected after keyword 'isset'");
2286         }
2287         getNextToken();
2288         isset_variables();
2289         if (token != TokenNameRPAREN) {
2290           throwSyntaxError("')' expected after keyword 'isset'");
2291         }
2292         getNextToken();
2293         break;
2294       case TokenNameempty:
2295         getNextToken();
2296         if (token != TokenNameLPAREN) {
2297           throwSyntaxError("'(' expected after keyword 'empty'");
2298         }
2299         getNextToken();
2300         variable(true, false);
2301         if (token != TokenNameRPAREN) {
2302           throwSyntaxError("')' expected after keyword 'empty'");
2303         }
2304         getNextToken();
2305         break;
2306       case TokenNameeval:
2307       case TokenNameinclude:
2308       case TokenNameinclude_once:
2309       case TokenNamerequire:
2310       case TokenNamerequire_once:
2311         internal_functions_in_yacc();
2312         break;
2313       //        | '(' expr ')'
2314       case TokenNameLPAREN:
2315         getNextToken();
2316         expr();
2317         if (token == TokenNameRPAREN) {
2318           getNextToken();
2319         } else {
2320           throwSyntaxError("')' expected in expression.");
2321         }
2322         break;
2323       //    | T_CLONE expr
2324       //    | T_PRINT expr
2325       //    | '@' expr
2326       //    | '+' expr
2327       //    | '-' expr
2328       //    | '!' expr
2329       //    | '~' expr
2330       //    | T_INT_CAST expr
2331       //        | T_DOUBLE_CAST expr
2332       //        | T_STRING_CAST expr
2333       //        | T_ARRAY_CAST expr
2334       //        | T_OBJECT_CAST expr
2335       //        | T_BOOL_CAST expr
2336       //        | T_UNSET_CAST expr
2337       case TokenNameclone:
2338       case TokenNameprint:
2339       case TokenNameAT:
2340       case TokenNamePLUS:
2341       case TokenNameMINUS:
2342       case TokenNameNOT:
2343       case TokenNameTWIDDLE:
2344       case TokenNameintCAST:
2345       case TokenNamedoubleCAST:
2346       case TokenNamestringCAST:
2347       case TokenNamearrayCAST:
2348       case TokenNameobjectCAST:
2349       case TokenNameboolCAST:
2350       case TokenNameunsetCAST:
2351         getNextToken();
2352         expr();
2353         break;
2354       case TokenNameexit:
2355         getNextToken();
2356         exit_expr();
2357         break;
2358       //  scalar:
2359       //        T_STRING
2360       //| T_STRING_VARNAME
2361       //| class_constant
2362       //| T_START_HEREDOC encaps_list T_END_HEREDOC
2363       //        | '`' encaps_list '`'
2364       //  | common_scalar
2365       //        | '`' encaps_list '`'
2366       case TokenNameEncapsedString0:
2367         scanner.encapsedStringStack.push(new Character('`'));
2368         getNextToken();
2369         try {
2370           if (token == TokenNameEncapsedString0) {
2371           } else {
2372             encaps_list();
2373             if (token != TokenNameEncapsedString0) {
2374               throwSyntaxError("\'`\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2375             }
2376           }
2377         } finally {
2378           scanner.encapsedStringStack.pop();
2379           getNextToken();
2380         }
2381         break;
2382       //      | '\'' encaps_list '\''
2383       case TokenNameEncapsedString1:
2384         scanner.encapsedStringStack.push(new Character('\''));
2385         getNextToken();
2386         try {
2387           exprSourceStart = scanner.getCurrentTokenStartPosition();
2388           if (token == TokenNameEncapsedString1) {
2389             expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2390                 .getCurrentTokenEndPosition());
2391           } else {
2392             encaps_list();
2393             if (token != TokenNameEncapsedString1) {
2394               throwSyntaxError("\'\'\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2395             } else {
2396               expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2397                   .getCurrentTokenEndPosition());
2398             }
2399           }
2400         } finally {
2401           scanner.encapsedStringStack.pop();
2402           getNextToken();
2403         }
2404         break;
2405       //| '"' encaps_list '"'
2406       case TokenNameEncapsedString2:
2407         scanner.encapsedStringStack.push(new Character('"'));
2408         getNextToken();
2409         try {
2410           exprSourceStart = scanner.getCurrentTokenStartPosition();
2411           if (token == TokenNameEncapsedString2) {
2412             expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2413                 .getCurrentTokenEndPosition());
2414           } else {
2415             encaps_list();
2416             if (token != TokenNameEncapsedString2) {
2417               throwSyntaxError("'\"' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2418             } else {
2419               expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(exprSourceStart), exprSourceStart, scanner
2420                   .getCurrentTokenEndPosition());
2421             }
2422           }
2423         } finally {
2424           scanner.encapsedStringStack.pop();
2425           getNextToken();
2426         }
2427         break;
2428       case TokenNameStringDoubleQuote:
2429         expression = new StringLiteralDQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2430             .getCurrentTokenEndPosition());
2431         common_scalar();
2432         break;
2433       case TokenNameStringSingleQuote:
2434         expression = new StringLiteralSQ(scanner.getCurrentStringLiteralSource(), scanner.getCurrentTokenStartPosition(), scanner
2435             .getCurrentTokenEndPosition());
2436         common_scalar();
2437         break;
2438       case TokenNameIntegerLiteral:
2439       case TokenNameDoubleLiteral:
2440       case TokenNameStringInterpolated:
2441       case TokenNameFILE:
2442       case TokenNameLINE:
2443       case TokenNameCLASS_C:
2444       case TokenNameMETHOD_C:
2445       case TokenNameFUNC_C:
2446         common_scalar();
2447         break;
2448       case TokenNameHEREDOC:
2449         getNextToken();
2450         break;
2451       case TokenNamearray:
2452         //    T_ARRAY '(' array_pair_list ')'
2453         getNextToken();
2454         if (token == TokenNameLPAREN) {
2455           getNextToken();
2456           if (token == TokenNameRPAREN) {
2457             getNextToken();
2458             break;
2459           }
2460           array_pair_list();
2461           if (token != TokenNameRPAREN) {
2462             throwSyntaxError("')' or ',' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2463           }
2464           getNextToken();
2465         } else {
2466           throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2467         }
2468         break;
2469       case TokenNamelist:
2470         //    | T_LIST '(' assignment_list ')' '=' expr
2471         getNextToken();
2472         if (token == TokenNameLPAREN) {
2473           getNextToken();
2474           assignment_list();
2475           if (token != TokenNameRPAREN) {
2476             throwSyntaxError("')' expected after 'list' keyword.");
2477           }
2478           getNextToken();
2479           if (token != TokenNameEQUAL) {
2480             throwSyntaxError("'=' expected after 'list' keyword.");
2481           }
2482           getNextToken();
2483           expr();
2484         } else {
2485           throwSyntaxError("'(' expected after 'list' keyword.");
2486         }
2487         break;
2488       case TokenNamenew:
2489         //      | T_NEW class_name_reference ctor_arguments
2490         getNextToken();
2491         Expression typeRef = class_name_reference();
2492         ctor_arguments();
2493         if (typeRef != null) {
2494           expression = typeRef;
2495         }
2496         break;
2497       //        | T_INC rw_variable
2498       //        | T_DEC rw_variable
2499       case TokenNamePLUS_PLUS:
2500       case TokenNameMINUS_MINUS:
2501         getNextToken();
2502         rw_variable();
2503         break;
2504       //        | variable '=' expr
2505       //        | variable '=' '&' variable
2506       //        | variable '=' '&' T_NEW class_name_reference ctor_arguments
2507       //        | variable T_PLUS_EQUAL expr
2508       //        | variable T_MINUS_EQUAL expr
2509       //        | variable T_MUL_EQUAL expr
2510       //        | variable T_DIV_EQUAL expr
2511       //        | variable T_CONCAT_EQUAL expr
2512       //        | variable T_MOD_EQUAL expr
2513       //        | variable T_AND_EQUAL expr
2514       //        | variable T_OR_EQUAL expr
2515       //        | variable T_XOR_EQUAL expr
2516       //        | variable T_SL_EQUAL expr
2517       //        | variable T_SR_EQUAL expr
2518       //        | rw_variable T_INC
2519       //        | rw_variable T_DEC
2520       case TokenNameIdentifier:
2521       case TokenNameVariable:
2522       case TokenNameDOLLAR:
2523         boolean rememberedVar = false; 
2524         Expression lhs = variable(true, true);
2525         if (lhs != null && lhs instanceof FieldReference && token != TokenNameEQUAL && token != TokenNamePLUS_EQUAL
2526             && token != TokenNameMINUS_EQUAL && token != TokenNameMULTIPLY_EQUAL && token != TokenNameDIVIDE_EQUAL
2527             && token != TokenNameDOT_EQUAL && token != TokenNameREMAINDER_EQUAL && token != TokenNameAND_EQUAL
2528             && token != TokenNameOR_EQUAL && token != TokenNameXOR_EQUAL && token != TokenNameRIGHT_SHIFT_EQUAL
2529             && token != TokenNameLEFT_SHIFT_EQUAL) {
2530           FieldReference ref = (FieldReference) lhs;
2531           if (!containsVariableSet(ref.token)) {
2532             problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(), referenceContext,
2533                 compilationUnit.compilationResult);
2534             addVariableSet(ref.token);
2535           }
2536         }
2537
2538         switch (token) {
2539         case TokenNameEQUAL:
2540           if (lhs != null && lhs instanceof FieldReference) {
2541             addVariableSet(((FieldReference) lhs).token);
2542           }
2543           //          if (lhsVar != null) {
2544           //            addVariableSet(lhsVar);
2545           //          }
2546           getNextToken();
2547           if (token == TokenNameAND) {
2548             getNextToken();
2549             if (token == TokenNamenew) {
2550               // | variable '=' '&' T_NEW class_name_reference
2551               // ctor_arguments
2552               getNextToken();
2553               SingleTypeReference classRef = class_name_reference();
2554               ctor_arguments();
2555               if (classRef != null) {
2556                 if (lhs != null && lhs instanceof FieldReference) {
2557                   // example:
2558                   // $var = & new Object();
2559                   if (fMethodVariables != null) {
2560                     VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2561                     lhsInfo.reference = classRef;
2562                     lhsInfo.typeIdentifier = classRef.token;
2563                     fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2564                     rememberedVar = true;
2565                   }
2566                 }
2567               }
2568             } else {
2569               Expression rhs = variable(false, false);
2570               if (rhs != null && rhs instanceof FieldReference && lhs != null && lhs instanceof FieldReference) {
2571                 // example:
2572                 // $var = &$ref;
2573                 if (fMethodVariables != null) {
2574                   VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2575                   if (rhsInfo != null && rhsInfo.reference != null) {
2576                     VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2577                     lhsInfo.reference = rhsInfo.reference;
2578                     lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2579                     fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2580                     rememberedVar = true;
2581                   }
2582                 }
2583               }
2584             }
2585           } else {
2586             Expression rhs = expr();
2587             if (lhs != null && lhs instanceof FieldReference) {
2588               if (rhs != null && rhs instanceof FieldReference) {
2589                 // example:
2590                 // $var = $ref;
2591                 if (fMethodVariables != null) {
2592                   VariableInfo rhsInfo = (VariableInfo) fMethodVariables.get(((FieldReference) rhs).token);
2593                   if (rhsInfo != null && rhsInfo.reference != null) {
2594                     VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2595                     lhsInfo.reference = rhsInfo.reference;
2596                     lhsInfo.typeIdentifier = rhsInfo.typeIdentifier;
2597                     fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2598                     rememberedVar = true;
2599                   }
2600                 }
2601               } else if (rhs != null && rhs instanceof SingleTypeReference) {
2602                 // example:
2603                 // $var = new Object();
2604                 if (fMethodVariables != null) {
2605                   VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2606                   lhsInfo.reference = (SingleTypeReference) rhs;
2607                   lhsInfo.typeIdentifier = ((SingleTypeReference) rhs).token;
2608                   fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2609                   rememberedVar = true;
2610                 }
2611               }
2612             }
2613           }
2614           if (rememberedVar == false && lhs != null && lhs instanceof FieldReference) {
2615             if (fMethodVariables != null) {
2616               VariableInfo lhsInfo = new VariableInfo(((FieldReference) lhs).sourceStart());
2617               fMethodVariables.put(new String(((FieldReference) lhs).token), lhsInfo);
2618             }
2619           }
2620           break;
2621         case TokenNamePLUS_EQUAL:
2622         case TokenNameMINUS_EQUAL:
2623         case TokenNameMULTIPLY_EQUAL:
2624         case TokenNameDIVIDE_EQUAL:
2625         case TokenNameDOT_EQUAL:
2626         case TokenNameREMAINDER_EQUAL:
2627         case TokenNameAND_EQUAL:
2628         case TokenNameOR_EQUAL:
2629         case TokenNameXOR_EQUAL:
2630         case TokenNameRIGHT_SHIFT_EQUAL:
2631         case TokenNameLEFT_SHIFT_EQUAL:
2632           if (lhs != null && lhs instanceof FieldReference) {
2633             addVariableSet(((FieldReference) lhs).token);
2634           }
2635           getNextToken();
2636           expr();
2637           break;
2638         case TokenNamePLUS_PLUS:
2639         case TokenNameMINUS_MINUS:
2640           getNextToken();
2641           break;
2642         default:
2643           if (!only_variable) {
2644             throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2645           }
2646           if (lhs != null) {
2647             expression = lhs;
2648           }
2649         }
2650         break;
2651       default:
2652         if (token != TokenNameINLINE_HTML) {
2653           if (token > TokenNameKEYWORD) {
2654             getNextToken();
2655             break;
2656           } else {
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 TokenNamePLUS:
3934       getNextToken();
3935       static_scalar();
3936       break;
3937     case TokenNameMINUS:
3938       getNextToken();
3939       static_scalar();
3940       break;
3941     case TokenNamearray:
3942       getNextToken();
3943       if (token != TokenNameLPAREN) {
3944         throwSyntaxError("'(' expected after keyword 'array'");
3945       }
3946       getNextToken();
3947       if (token == TokenNameRPAREN) {
3948         getNextToken();
3949         break;
3950       }
3951       non_empty_static_array_pair_list();
3952       if (token != TokenNameRPAREN) {
3953         throwSyntaxError("')' or ',' expected after keyword 'array'");
3954       }
3955       getNextToken();
3956       break;
3957     //      case TokenNamenull :
3958     //        getNextToken();
3959     //        break;
3960     //      case TokenNamefalse :
3961     //        getNextToken();
3962     //        break;
3963     //      case TokenNametrue :
3964     //        getNextToken();
3965     //        break;
3966     default:
3967       throwSyntaxError("Static scalar/constant expected.");
3968     }
3969   }
3970
3971   private void non_empty_static_array_pair_list() {
3972     //  non_empty_static_array_pair_list:
3973     //  non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3974     // static_scalar
3975     //| non_empty_static_array_pair_list ',' static_scalar
3976     //| static_scalar T_DOUBLE_ARROW static_scalar
3977     //| static_scalar
3978     while (true) {
3979       static_scalar();
3980       if (token == TokenNameEQUAL_GREATER) {
3981         getNextToken();
3982         static_scalar();
3983       }
3984       if (token != TokenNameCOMMA) {
3985         break;
3986       }
3987       getNextToken();
3988       if (token == TokenNameRPAREN) {
3989         break;
3990       }
3991     }
3992   }
3993
3994   public void reportSyntaxError() { //int act, int currentKind, int
3995     // stateStackTop) {
3996     /* remember current scanner position */
3997     int startPos = scanner.startPosition;
3998     int currentPos = scanner.currentPosition;
3999     //          String[] expectings;
4000     //          String tokenName = name[symbol_index[currentKind]];
4001     //fetch all "accurate" possible terminals that could recover the error
4002     //          int start, end = start = asi(stack[stateStackTop]);
4003     //          while (asr[end] != 0)
4004     //                  end++;
4005     //          int length = end - start;
4006     //          expectings = new String[length];
4007     //          if (length != 0) {
4008     //                  char[] indexes = new char[length];
4009     //                  System.arraycopy(asr, start, indexes, 0, length);
4010     //                  for (int i = 0; i < length; i++) {
4011     //                          expectings[i] = name[symbol_index[indexes[i]]];
4012     //                  }
4013     //          }
4014     //if the pb is an EOF, try to tell the user that they are some
4015     //          if (tokenName.equals(UNEXPECTED_EOF)) {
4016     //                  if (!this.checkAndReportBracketAnomalies(problemReporter())) {
4017     //                          char[] tokenSource;
4018     //                          try {
4019     //                                  tokenSource = this.scanner.getCurrentTokenSource();
4020     //                          } catch (Exception e) {
4021     //                                  tokenSource = new char[] {};
4022     //                          }
4023     //                          problemReporter().parseError(
4024     //                                  this.scanner.startPosition,
4025     //                                  this.scanner.currentPosition - 1,
4026     //                                  tokenSource,
4027     //                                  tokenName,
4028     //                                  expectings);
4029     //                  }
4030     //          } else { //the next test is HEAVILY grammar DEPENDENT.
4031     //                  if ((length == 14)
4032     //                          && (expectings[0] == "=") //$NON-NLS-1$
4033     //                          && (expectings[1] == "*=") //$NON-NLS-1$
4034     //                          && (expressionPtr > -1)) {
4035     //                                  switch(currentKind) {
4036     //                                          case TokenNameSEMICOLON:
4037     //                                          case TokenNamePLUS:
4038     //                                          case TokenNameMINUS:
4039     //                                          case TokenNameDIVIDE:
4040     //                                          case TokenNameREMAINDER:
4041     //                                          case TokenNameMULTIPLY:
4042     //                                          case TokenNameLEFT_SHIFT:
4043     //                                          case TokenNameRIGHT_SHIFT:
4044     //// case TokenNameUNSIGNED_RIGHT_SHIFT:
4045     //                                          case TokenNameLESS:
4046     //                                          case TokenNameGREATER:
4047     //                                          case TokenNameLESS_EQUAL:
4048     //                                          case TokenNameGREATER_EQUAL:
4049     //                                          case TokenNameEQUAL_EQUAL:
4050     //                                          case TokenNameNOT_EQUAL:
4051     //                                          case TokenNameXOR:
4052     //                                          case TokenNameAND:
4053     //                                          case TokenNameOR:
4054     //                                          case TokenNameOR_OR:
4055     //                                          case TokenNameAND_AND:
4056     //                                                  // the ; is not the expected token ==> it ends a statement when an
4057     // expression is not ended
4058     //                                                  problemReporter().invalidExpressionAsStatement(expressionStack[expressionPtr]);
4059     //                                                  break;
4060     //                                          case TokenNameRBRACE :
4061     //                                                  problemReporter().missingSemiColon(expressionStack[expressionPtr]);
4062     //                                                  break;
4063     //                                          default:
4064     //                                                  char[] tokenSource;
4065     //                                                  try {
4066     //                                                          tokenSource = this.scanner.getCurrentTokenSource();
4067     //                                                  } catch (Exception e) {
4068     //                                                          tokenSource = new char[] {};
4069     //                                                  }
4070     //                                                  problemReporter().parseError(
4071     //                                                          this.scanner.startPosition,
4072     //                                                          this.scanner.currentPosition - 1,
4073     //                                                          tokenSource,
4074     //                                                          tokenName,
4075     //                                                          expectings);
4076     //                                                  this.checkAndReportBracketAnomalies(problemReporter());
4077     //                                  }
4078     //                  } else {
4079     char[] tokenSource;
4080     try {
4081       tokenSource = this.scanner.getCurrentTokenSource();
4082     } catch (Exception e) {
4083       tokenSource = new char[] {};
4084     }
4085     //                          problemReporter().parseError(
4086     //                                  this.scanner.startPosition,
4087     //                                  this.scanner.currentPosition - 1,
4088     //                                  tokenSource,
4089     //                                  tokenName,
4090     //                                  expectings);
4091     this.checkAndReportBracketAnomalies(problemReporter());
4092     //                  }
4093     //          }
4094     /* reset scanner where it was */
4095     scanner.startPosition = startPos;
4096     scanner.currentPosition = currentPos;
4097   }
4098
4099   public static final int RoundBracket = 0;
4100
4101   public static final int SquareBracket = 1;
4102
4103   public static final int CurlyBracket = 2;
4104
4105   public static final int BracketKinds = 3;
4106
4107   protected int[] nestedMethod; //the ptr is nestedType
4108
4109   protected int nestedType, dimensions;
4110
4111   //variable set stack
4112   final static int VariableStackIncrement = 10;
4113
4114   HashMap fTypeVariables = null;
4115
4116   HashMap fMethodVariables = null;
4117
4118   ArrayList fStackUnassigned = new ArrayList();
4119
4120   //ast stack
4121   final static int AstStackIncrement = 100;
4122
4123   protected int astPtr;
4124
4125   protected ASTNode[] astStack = new ASTNode[AstStackIncrement];
4126
4127   protected int astLengthPtr;
4128
4129   protected int[] astLengthStack;
4130
4131   ASTNode[] noAstNodes = new ASTNode[AstStackIncrement];
4132
4133   public CompilationUnitDeclaration compilationUnit; /*
4134                                                       * the result from parse()
4135                                                       */
4136
4137   protected ReferenceContext referenceContext;
4138
4139   protected ProblemReporter problemReporter;
4140
4141   protected CompilerOptions options;
4142
4143   private ArrayList includesList;
4144
4145   //  protected CompilationResult compilationResult;
4146   /**
4147    * Returns this parser's problem reporter initialized with its reference context. Also it is assumed that a problem is going to be
4148    * reported, so initializes the compilation result's line positions.
4149    */
4150   public ProblemReporter problemReporter() {
4151     if (scanner.recordLineSeparator) {
4152       compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4153     }
4154     problemReporter.referenceContext = referenceContext;
4155     return problemReporter;
4156   }
4157
4158   /*
4159    * Reconsider the entire source looking for inconsistencies in {} () []
4160    */
4161   public boolean checkAndReportBracketAnomalies(ProblemReporter problemReporter) {
4162     scanner.wasAcr = false;
4163     boolean anomaliesDetected = false;
4164     try {
4165       char[] source = scanner.source;
4166       int[] leftCount = { 0, 0, 0 };
4167       int[] rightCount = { 0, 0, 0 };
4168       int[] depths = { 0, 0, 0 };
4169       int[][] leftPositions = new int[][] { new int[10], new int[10], new int[10] };
4170       int[][] leftDepths = new int[][] { new int[10], new int[10], new int[10] };
4171       int[][] rightPositions = new int[][] { new int[10], new int[10], new int[10] };
4172       int[][] rightDepths = new int[][] { new int[10], new int[10], new int[10] };
4173       scanner.currentPosition = scanner.initialPosition; //starting
4174       // point
4175       // (first-zero-based
4176       // char)
4177       while (scanner.currentPosition < scanner.eofPosition) { //loop for
4178         // jumping
4179         // over
4180         // comments
4181         try {
4182           // ---------Consume white space and handles
4183           // startPosition---------
4184           boolean isWhiteSpace;
4185           do {
4186             scanner.startPosition = scanner.currentPosition;
4187             //                                          if (((scanner.currentCharacter =
4188             // source[scanner.currentPosition++]) == '\\') &&
4189             // (source[scanner.currentPosition] == 'u')) {
4190             //                                                  isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
4191             //                                          } else {
4192             if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
4193               if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4194                 // only record line positions we have not
4195                 // recorded yet
4196                 scanner.pushLineSeparator();
4197               }
4198             }
4199             isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
4200             //                                          }
4201           } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
4202           // -------consume token until } is found---------
4203           switch (scanner.currentCharacter) {
4204           case '{': {
4205             int index = leftCount[CurlyBracket]++;
4206             if (index == leftPositions[CurlyBracket].length) {
4207               System.arraycopy(leftPositions[CurlyBracket], 0, (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
4208               System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] = new int[index * 2]), 0, index);
4209             }
4210             leftPositions[CurlyBracket][index] = scanner.startPosition;
4211             leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
4212           }
4213             break;
4214           case '}': {
4215             int index = rightCount[CurlyBracket]++;
4216             if (index == rightPositions[CurlyBracket].length) {
4217               System.arraycopy(rightPositions[CurlyBracket], 0, (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
4218               System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] = new int[index * 2]), 0, index);
4219             }
4220             rightPositions[CurlyBracket][index] = scanner.startPosition;
4221             rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
4222           }
4223             break;
4224           case '(': {
4225             int index = leftCount[RoundBracket]++;
4226             if (index == leftPositions[RoundBracket].length) {
4227               System.arraycopy(leftPositions[RoundBracket], 0, (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
4228               System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] = new int[index * 2]), 0, index);
4229             }
4230             leftPositions[RoundBracket][index] = scanner.startPosition;
4231             leftDepths[RoundBracket][index] = depths[RoundBracket]++;
4232           }
4233             break;
4234           case ')': {
4235             int index = rightCount[RoundBracket]++;
4236             if (index == rightPositions[RoundBracket].length) {
4237               System.arraycopy(rightPositions[RoundBracket], 0, (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
4238               System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] = new int[index * 2]), 0, index);
4239             }
4240             rightPositions[RoundBracket][index] = scanner.startPosition;
4241             rightDepths[RoundBracket][index] = --depths[RoundBracket];
4242           }
4243             break;
4244           case '[': {
4245             int index = leftCount[SquareBracket]++;
4246             if (index == leftPositions[SquareBracket].length) {
4247               System.arraycopy(leftPositions[SquareBracket], 0, (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
4248               System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] = new int[index * 2]), 0, index);
4249             }
4250             leftPositions[SquareBracket][index] = scanner.startPosition;
4251             leftDepths[SquareBracket][index] = depths[SquareBracket]++;
4252           }
4253             break;
4254           case ']': {
4255             int index = rightCount[SquareBracket]++;
4256             if (index == rightPositions[SquareBracket].length) {
4257               System.arraycopy(rightPositions[SquareBracket], 0, (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
4258               System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket] = new int[index * 2]), 0, index);
4259             }
4260             rightPositions[SquareBracket][index] = scanner.startPosition;
4261             rightDepths[SquareBracket][index] = --depths[SquareBracket];
4262           }
4263             break;
4264           case '\'': {
4265             if (scanner.getNextChar('\\')) {
4266               scanner.scanEscapeCharacter();
4267             } else { // consume next character
4268               scanner.unicodeAsBackSlash = false;
4269               //                                                                        if (((scanner.currentCharacter =
4270               // source[scanner.currentPosition++]) ==
4271               // '\\') &&
4272               // (source[scanner.currentPosition] ==
4273               // 'u')) {
4274               //                                                                                scanner.getNextUnicodeChar();
4275               //                                                                        } else {
4276               if (scanner.withoutUnicodePtr != 0) {
4277                 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4278               }
4279               //                                                                        }
4280             }
4281             scanner.getNextChar('\'');
4282             break;
4283           }
4284           case '"':
4285             // consume next character
4286             scanner.unicodeAsBackSlash = false;
4287             //                                                  if (((scanner.currentCharacter =
4288             // source[scanner.currentPosition++]) == '\\') &&
4289             // (source[scanner.currentPosition] == 'u')) {
4290             //                                                          scanner.getNextUnicodeChar();
4291             //                                                  } else {
4292             if (scanner.withoutUnicodePtr != 0) {
4293               scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4294             }
4295             //                                                  }
4296             while (scanner.currentCharacter != '"') {
4297               if (scanner.currentCharacter == '\r') {
4298                 if (source[scanner.currentPosition] == '\n')
4299                   scanner.currentPosition++;
4300                 break; // the string cannot go further that
4301                 // the line
4302               }
4303               if (scanner.currentCharacter == '\n') {
4304                 break; // the string cannot go further that
4305                 // the line
4306               }
4307               if (scanner.currentCharacter == '\\') {
4308                 scanner.scanEscapeCharacter();
4309               }
4310               // consume next character
4311               scanner.unicodeAsBackSlash = false;
4312               //                                                                if (((scanner.currentCharacter =
4313               // source[scanner.currentPosition++]) == '\\')
4314               // && (source[scanner.currentPosition] == 'u'))
4315               // {
4316               //                                                                        scanner.getNextUnicodeChar();
4317               //                                                                } else {
4318               if (scanner.withoutUnicodePtr != 0) {
4319                 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4320               }
4321               //                                                                }
4322             }
4323             break;
4324           case '/': {
4325             int test;
4326             if ((test = scanner.getNextChar('/', '*')) == 0) { //line
4327               // comment
4328               //get the next char
4329               if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4330                   && (source[scanner.currentPosition] == 'u')) {
4331                 //-------------unicode traitement
4332                 // ------------
4333                 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4334                 scanner.currentPosition++;
4335                 while (source[scanner.currentPosition] == 'u') {
4336                   scanner.currentPosition++;
4337                 }
4338                 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4339                     || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4340                     || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4341                     || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4342                   // don't
4343                   // care of the
4344                   // value
4345                   scanner.currentCharacter = 'A';
4346                 } //something different from \n and \r
4347                 else {
4348                   scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4349                 }
4350               }
4351               while (scanner.currentCharacter != '\r' && scanner.currentCharacter != '\n') {
4352                 //get the next char
4353                 scanner.startPosition = scanner.currentPosition;
4354                 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4355                     && (source[scanner.currentPosition] == 'u')) {
4356                   //-------------unicode traitement
4357                   // ------------
4358                   int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4359                   scanner.currentPosition++;
4360                   while (source[scanner.currentPosition] == 'u') {
4361                     scanner.currentPosition++;
4362                   }
4363                   if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4364                       || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4365                       || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4366                       || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4367                     // don't
4368                     // care of the
4369                     // value
4370                     scanner.currentCharacter = 'A';
4371                   } //something different from \n
4372                   // and \r
4373                   else {
4374                     scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4375                   }
4376                 }
4377               }
4378               if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
4379                 if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
4380                   // only record line positions we
4381                   // have not recorded yet
4382                   scanner.pushLineSeparator();
4383                   if (this.scanner.taskTags != null) {
4384                     this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner
4385                         .getCurrentTokenEndPosition());
4386                   }
4387                 }
4388               }
4389               break;
4390             }
4391             if (test > 0) { //traditional and annotation
4392               // comment
4393               boolean star = false;
4394               // consume next character
4395               scanner.unicodeAsBackSlash = false;
4396               //                                                                        if (((scanner.currentCharacter =
4397               // source[scanner.currentPosition++]) ==
4398               // '\\') &&
4399               // (source[scanner.currentPosition] ==
4400               // 'u')) {
4401               //                                                                                scanner.getNextUnicodeChar();
4402               //                                                                        } else {
4403               if (scanner.withoutUnicodePtr != 0) {
4404                 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
4405               }
4406               //                                                                        }
4407               if (scanner.currentCharacter == '*') {
4408                 star = true;
4409               }
4410               //get the next char
4411               if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4412                   && (source[scanner.currentPosition] == 'u')) {
4413                 //-------------unicode traitement
4414                 // ------------
4415                 int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4416                 scanner.currentPosition++;
4417                 while (source[scanner.currentPosition] == 'u') {
4418                   scanner.currentPosition++;
4419                 }
4420                 if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4421                     || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4422                     || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4423                     || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4424                   // don't
4425                   // care of the
4426                   // value
4427                   scanner.currentCharacter = 'A';
4428                 } //something different from * and /
4429                 else {
4430                   scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4431                 }
4432               }
4433               //loop until end of comment */
4434               while ((scanner.currentCharacter != '/') || (!star)) {
4435                 star = scanner.currentCharacter == '*';
4436                 //get next char
4437                 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
4438                     && (source[scanner.currentPosition] == 'u')) {
4439                   //-------------unicode traitement
4440                   // ------------
4441                   int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
4442                   scanner.currentPosition++;
4443                   while (source[scanner.currentPosition] == 'u') {
4444                     scanner.currentPosition++;
4445                   }
4446                   if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
4447                       || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
4448                       || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
4449                       || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
4450                     // don't
4451                     // care of the
4452                     // value
4453                     scanner.currentCharacter = 'A';
4454                   } //something different from * and
4455                   // /
4456                   else {
4457                     scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
4458                   }
4459                 }
4460               }
4461               if (this.scanner.taskTags != null) {
4462                 this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
4463               }
4464               break;
4465             }
4466             break;
4467           }
4468           default:
4469             if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
4470               scanner.scanIdentifierOrKeyword(false);
4471               break;
4472             }
4473             if (Character.isDigit(scanner.currentCharacter)) {
4474               scanner.scanNumber(false);
4475               break;
4476             }
4477           }
4478           //-----------------end switch while
4479           // try--------------------
4480         } catch (IndexOutOfBoundsException e) {
4481           break; // read until EOF
4482         } catch (InvalidInputException e) {
4483           return false; // no clue
4484         }
4485       }
4486       if (scanner.recordLineSeparator) {
4487         compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
4488       }
4489       // check placement anomalies against other kinds of brackets
4490       for (int kind = 0; kind < BracketKinds; kind++) {
4491         for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
4492           int start = leftPositions[kind][leftIndex]; // deepest
4493           // first
4494           // find matching closing bracket
4495           int depth = leftDepths[kind][leftIndex];
4496           int end = -1;
4497           for (int i = 0; i < rightCount[kind]; i++) {
4498             int pos = rightPositions[kind][i];
4499             // want matching bracket further in source with same
4500             // depth
4501             if ((pos > start) && (depth == rightDepths[kind][i])) {
4502               end = pos;
4503               break;
4504             }
4505           }
4506           if (end < 0) { // did not find a good closing match
4507             problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult);
4508             return true;
4509           }
4510           // check if even number of opening/closing other brackets
4511           // in between this pair of brackets
4512           int balance = 0;
4513           for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds); otherKind++) {
4514             for (int i = 0; i < leftCount[otherKind]; i++) {
4515               int pos = leftPositions[otherKind][i];
4516               if ((pos > start) && (pos < end))
4517                 balance++;
4518             }
4519             for (int i = 0; i < rightCount[otherKind]; i++) {
4520               int pos = rightPositions[otherKind][i];
4521               if ((pos > start) && (pos < end))
4522                 balance--;
4523             }
4524             if (balance != 0) {
4525               problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult); //bracket
4526               // anomaly
4527               return true;
4528             }
4529           }
4530         }
4531         // too many opening brackets ?
4532         for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
4533           anomaliesDetected = true;
4534           problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i - 1], referenceContext,
4535               compilationUnit.compilationResult);
4536         }
4537         // too many closing brackets ?
4538         for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
4539           anomaliesDetected = true;
4540           problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext, compilationUnit.compilationResult);
4541         }
4542         if (anomaliesDetected)
4543           return true;
4544       }
4545       return anomaliesDetected;
4546     } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
4547       return anomaliesDetected;
4548     } catch (NullPointerException e) { // jdk1.2.2 jit bug
4549       return anomaliesDetected;
4550     }
4551   }
4552
4553   protected void pushOnAstLengthStack(int pos) {
4554     try {
4555       astLengthStack[++astLengthPtr] = pos;
4556     } catch (IndexOutOfBoundsException e) {
4557       int oldStackLength = astLengthStack.length;
4558       int[] oldPos = astLengthStack;
4559       astLengthStack = new int[oldStackLength + StackIncrement];
4560       System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4561       astLengthStack[astLengthPtr] = pos;
4562     }
4563   }
4564
4565   protected void pushOnAstStack(ASTNode node) {
4566     /*
4567      * add a new obj on top of the ast stack
4568      */
4569     try {
4570       astStack[++astPtr] = node;
4571     } catch (IndexOutOfBoundsException e) {
4572       int oldStackLength = astStack.length;
4573       ASTNode[] oldStack = astStack;
4574       astStack = new ASTNode[oldStackLength + AstStackIncrement];
4575       System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
4576       astPtr = oldStackLength;
4577       astStack[astPtr] = node;
4578     }
4579     try {
4580       astLengthStack[++astLengthPtr] = 1;
4581     } catch (IndexOutOfBoundsException e) {
4582       int oldStackLength = astLengthStack.length;
4583       int[] oldPos = astLengthStack;
4584       astLengthStack = new int[oldStackLength + AstStackIncrement];
4585       System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
4586       astLengthStack[astLengthPtr] = 1;
4587     }
4588   }
4589
4590   protected void resetModifiers() {
4591     this.modifiers = AccDefault;
4592     this.modifiersSourceStart = -1; // <-- see comment into
4593     // modifiersFlag(int)
4594     this.scanner.commentPtr = -1;
4595   }
4596
4597   protected void consumePackageDeclarationName(IFile file) {
4598     // create a package name similar to java package names
4599     String projectPath = ProjectPrefUtil.getDocumentRoot(file.getProject()).toString();
4600     String filePath = file.getRawLocation().toString();
4601     String ext = file.getRawLocation().getFileExtension();
4602     int fileExtensionLength = ext == null ? 0 : ext.length() + 1;
4603     ImportReference impt;
4604     int length;
4605     char[][] tokens;
4606     if (filePath.startsWith(projectPath)) {
4607       tokens = CharOperation
4608           .splitOn('/', filePath.toCharArray(), projectPath.length() + 1, filePath.length() - fileExtensionLength);
4609     } else {
4610       String name = file.getName();
4611       tokens = new char[1][];
4612       tokens[0] = name.substring(0, name.length() - fileExtensionLength).toCharArray();
4613     }
4614
4615     this.compilationUnit.currentPackage = impt = new ImportReference(tokens, new char[0], 0, 0, true);
4616
4617     impt.declarationSourceStart = 0;
4618     impt.declarationSourceEnd = 0;
4619     impt.declarationEnd = 0;
4620     //endPosition is just before the ;
4621
4622   }
4623
4624   public final static String[] GLOBALS = {
4625       "$this",
4626       "$_COOKIE",
4627       "$_ENV",
4628       "$_FILES",
4629       "$_GET",
4630       "$GLOBALS",
4631       "$_POST",
4632       "$_REQUEST",
4633       "$_SESSION",
4634       "$_SERVER" };
4635
4636   /**
4637    *  
4638    */
4639   private void pushFunctionVariableSet() {
4640     HashSet set = new HashSet();
4641     if (fStackUnassigned.isEmpty()) {
4642       for (int i = 0; i < GLOBALS.length; i++) {
4643         set.add(GLOBALS[i]);
4644       }
4645     }
4646     fStackUnassigned.add(set);
4647   }
4648
4649   private void pushIfVariableSet() {
4650     if (!fStackUnassigned.isEmpty()) {
4651       HashSet set = new HashSet();
4652       fStackUnassigned.add(set);
4653     }
4654   }
4655
4656   private HashSet removeIfVariableSet() {
4657     if (!fStackUnassigned.isEmpty()) {
4658       return (HashSet) fStackUnassigned.remove(fStackUnassigned.size() - 1);
4659     }
4660     return null;
4661   }
4662
4663   /**
4664    * Returns the <i>set of assigned variables </i> returns null if no Set is defined at the current scanner position
4665    */
4666   private HashSet peekVariableSet() {
4667     if (!fStackUnassigned.isEmpty()) {
4668       return (HashSet) fStackUnassigned.get(fStackUnassigned.size() - 1);
4669     }
4670     return null;
4671   }
4672
4673   /**
4674    * add the current identifier source to the <i>set of assigned variables </i>
4675    * 
4676    * @param set
4677    */
4678   private void addVariableSet(HashSet set) {
4679     if (set != null) {
4680       set.add(new String(scanner.getCurrentTokenSource()));
4681     }
4682   }
4683
4684   /**
4685    * add the current identifier source to the <i>set of assigned variables </i>
4686    *  
4687    */
4688   private void addVariableSet() {
4689     HashSet set = peekVariableSet();
4690     if (set != null) {
4691       set.add(new String(scanner.getCurrentTokenSource()));
4692     }
4693   }
4694
4695   /**
4696    * add the current identifier source to the <i>set of assigned variables </i>
4697    *  
4698    */
4699   private void addVariableSet(char[] token) {
4700     HashSet set = peekVariableSet();
4701     if (set != null) {
4702       set.add(new String(token));
4703     }
4704   }
4705
4706   /**
4707    * check if the current identifier source is in the <i>set of assigned variables </i> Returns true, if no set is defined for the
4708    * current scanner position
4709    *  
4710    */
4711   private boolean containsVariableSet() {
4712     return containsVariableSet(scanner.getCurrentTokenSource());
4713     //    if (!fStackUnassigned.isEmpty()) {
4714     //      HashSet set;
4715     //      String str = new String(scanner.getCurrentTokenSource());
4716     //      for (int i = 0; i < fStackUnassigned.size(); i++) {
4717     //        set = (HashSet) fStackUnassigned.get(i);
4718     //        if (set.contains(str)) {
4719     //          return true;
4720     //        }
4721     //      }
4722     //      return false;
4723     //    }
4724     //    return true;
4725   }
4726
4727   private boolean containsVariableSet(char[] token) {
4728
4729     if (!fStackUnassigned.isEmpty()) {
4730       HashSet set;
4731       String str = new String(token);
4732       for (int i = 0; i < fStackUnassigned.size(); i++) {
4733         set = (HashSet) fStackUnassigned.get(i);
4734         if (set.contains(str)) {
4735           return true;
4736         }
4737       }
4738       return false;
4739     }
4740     return true;
4741   }
4742 }