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