Fixed bug in CodeFormatter; for partial formatting select complete <?php ... ?> range
[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     char[] classVariable;
1395     do {
1396       if (token == TokenNameVariable) {
1397         classVariable = scanner.getCurrentIdentifierSource();
1398       //  indexManager.addIdentifierInformation('v', classVariable, buf, -1, -1);
1399         FieldDeclaration fieldDeclaration = new FieldDeclaration(classVariable, scanner
1400             .getCurrentTokenStartPosition(), scanner.getCurrentTokenEndPosition());
1401         fieldDeclaration.modifiers = this.modifiers;
1402         fieldDeclaration.declarationSourceStart = declarationSourceStart;
1403         fieldDeclaration.declarationSourceEnd = scanner.getCurrentTokenEndPosition();
1404         fieldDeclaration.modifiersSourceStart = declarationSourceStart;
1405         //        fieldDeclaration.type
1406         list.add(fieldDeclaration);
1407         getNextToken();
1408         if (token == TokenNameEQUAL) {
1409           getNextToken();
1410           static_scalar();
1411         }
1412       } else {
1413         //        if (token == TokenNamethis) {
1414         //          throwSyntaxError("'$this' not allowed after keyword 'public'
1415         // 'protected' 'private' 'var'.");
1416         //        }
1417         throwSyntaxError("Variable expected after keyword 'public' 'protected' 'private' 'var'.");
1418       }
1419       if (token != TokenNameCOMMA) {
1420         break;
1421       }
1422       getNextToken();
1423     } while (true);
1424     if (token != TokenNameSEMICOLON) {
1425       throwSyntaxError("';' expected after field declaration.");
1426     }
1427     getNextToken();
1428   }
1429   private void functionDefinition(MethodDeclaration methodDecl) {
1430     boolean isAbstract = false;
1431     if (astPtr == 0) {
1432       compilationUnit.types.add(methodDecl);
1433     } else {
1434       AstNode node = astStack[astPtr];
1435       if (node instanceof TypeDeclaration) {
1436         TypeDeclaration typeDecl = ((TypeDeclaration) node);
1437         if (typeDecl.methods == null) {
1438           typeDecl.methods = new AbstractMethodDeclaration[]{methodDecl};
1439         } else {
1440           AbstractMethodDeclaration[] newMethods;
1441           System.arraycopy(typeDecl.methods, 0, newMethods = new AbstractMethodDeclaration[typeDecl.methods.length + 1], 1,
1442               typeDecl.methods.length);
1443           newMethods[0] = methodDecl;
1444           typeDecl.methods = newMethods;
1445         }
1446         if ((typeDecl.modifiers & AccAbstract) == AccAbstract) {
1447           isAbstract = true;
1448         } else if ((typeDecl.modifiers & AccInterface) == AccInterface) {
1449           isAbstract = true;
1450         }
1451       }
1452     }
1453     functionDeclarator(methodDecl);
1454     if (token == TokenNameSEMICOLON) {
1455       if (!isAbstract) {
1456         throwSyntaxError("Body declaration expected for method: " + new String(methodDecl.selector));
1457       }
1458       getNextToken();
1459       return;
1460     }
1461     functionBody(methodDecl);
1462   }
1463   private void functionDeclarator(MethodDeclaration methodDecl) {
1464     //identifier '(' [parameter-list] ')'
1465     if (token == TokenNameAND) {
1466       getNextToken();
1467     }
1468     if (token == TokenNameIdentifier) {
1469       methodDecl.sourceStart = scanner.getCurrentTokenStartPosition();
1470       methodDecl.sourceEnd = scanner.getCurrentTokenEndPosition();
1471       methodDecl.selector = scanner.getCurrentIdentifierSource();
1472       getNextToken();
1473       if (token == TokenNameLPAREN) {
1474         getNextToken();
1475       } else {
1476         throwSyntaxError("'(' expected in function declaration.");
1477       }
1478       if (token != TokenNameRPAREN) {
1479         parameter_list();
1480       }
1481       if (token != TokenNameRPAREN) {
1482         throwSyntaxError("')' expected in function declaration.");
1483       } else {
1484         methodDecl.bodyStart = scanner.getCurrentTokenEndPosition() + 1;
1485         getNextToken();
1486       }
1487     } else {
1488       if (token > TokenNameKEYWORD) {
1489         throwSyntaxError("Don't use keyword for function declaration [" + token + "].");
1490       }
1491       throwSyntaxError("Function name expected after keyword 'function'.");
1492     }
1493   }
1494   //
1495   private void parameter_list() {
1496     //  non_empty_parameter_list
1497     //  | /* empty */
1498     non_empty_parameter_list(true);
1499   }
1500   private void non_empty_parameter_list(boolean empty_allowed) {
1501     //  optional_class_type T_VARIABLE
1502     //  | optional_class_type '&' T_VARIABLE
1503     //  | optional_class_type '&' T_VARIABLE '=' static_scalar
1504     //  | optional_class_type T_VARIABLE '=' static_scalar
1505     //  | non_empty_parameter_list ',' optional_class_type T_VARIABLE
1506     //  | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE
1507     //  | non_empty_parameter_list ',' optional_class_type '&' T_VARIABLE '='
1508     // static_scalar
1509     //  | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
1510     // static_scalar
1511     if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
1512       while (true) {
1513         if (token == TokenNameIdentifier) {
1514           getNextToken();
1515         }
1516         if (token == TokenNameAND) {
1517           getNextToken();
1518         }
1519         if (token == TokenNameVariable) {
1520           getNextToken();
1521           if (token == TokenNameEQUAL) {
1522             getNextToken();
1523             static_scalar();
1524           }
1525         } else {
1526           throwSyntaxError("Variable expected in parameter list.");
1527         }
1528         if (token != TokenNameCOMMA) {
1529           break;
1530         }
1531         getNextToken();
1532       }
1533       return;
1534     }
1535     if (!empty_allowed) {
1536       throwSyntaxError("Identifier expected in parameter list.");
1537     }
1538   }
1539   private void optional_class_type() {
1540     //  /* empty */
1541     //| T_STRING
1542   }
1543   private void parameterDeclaration() {
1544     //variable
1545     //variable-reference
1546     if (token == TokenNameAND) {
1547       getNextToken();
1548       if (isVariable()) {
1549         getNextToken();
1550       } else {
1551         throwSyntaxError("Variable expected after reference operator '&'.");
1552       }
1553     }
1554     //variable '=' constant
1555     if (token == TokenNameVariable) {
1556       getNextToken();
1557       if (token == TokenNameEQUAL) {
1558         getNextToken();
1559         static_scalar();
1560       }
1561       return;
1562     }
1563     //    if (token == TokenNamethis) {
1564     //      throwSyntaxError("Reserved word '$this' not allowed in parameter
1565     // declaration.");
1566     //    }
1567   }
1568   private void labeledStatementList() {
1569     if (token != TokenNamecase && token != TokenNamedefault) {
1570       throwSyntaxError("'case' or 'default' expected.");
1571     }
1572     do {
1573       if (token == TokenNamecase) {
1574         getNextToken();
1575         expr(); //constant();
1576         if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
1577           getNextToken();
1578           if (token == TokenNamecase || token == TokenNamedefault) {
1579             // empty case statement ?
1580             continue;
1581           }
1582           statementList();
1583         }
1584         //        else if (token == TokenNameSEMICOLON) {
1585         //          setMarker(
1586         //            "':' expected after 'case' keyword (Found token: " +
1587         // scanner.toStringAction(token) + ")",
1588         //            scanner.getCurrentTokenStartPosition(),
1589         //            scanner.getCurrentTokenEndPosition(),
1590         //            INFO);
1591         //          getNextToken();
1592         //          if (token == TokenNamecase) { // empty case statement ?
1593         //            continue;
1594         //          }
1595         //          statementList();
1596         //        }
1597         else {
1598           throwSyntaxError("':' character after 'case' constant expected (Found token: " + scanner.toStringAction(token) + ")");
1599         }
1600       } else { // TokenNamedefault
1601         getNextToken();
1602         if (token == TokenNameCOLON) {
1603           getNextToken();
1604           if (token == TokenNameRBRACE) {
1605             // empty default case
1606             break;
1607           }
1608           statementList();
1609         } else {
1610           throwSyntaxError("':' character after 'default' expected.");
1611         }
1612       }
1613     } while (token == TokenNamecase || token == TokenNamedefault);
1614   }
1615   //  public void labeledStatement() {
1616   //    if (token == TokenNamecase) {
1617   //      getNextToken();
1618   //      constant();
1619   //      if (token == TokenNameDDOT) {
1620   //        getNextToken();
1621   //        statement();
1622   //      } else {
1623   //        throwSyntaxError("':' character after 'case' constant expected.");
1624   //      }
1625   //      return;
1626   //    } else if (token == TokenNamedefault) {
1627   //      getNextToken();
1628   //      if (token == TokenNameDDOT) {
1629   //        getNextToken();
1630   //        statement();
1631   //      } else {
1632   //        throwSyntaxError("':' character after 'default' expected.");
1633   //      }
1634   //      return;
1635   //    }
1636   //  }
1637   //  public void expressionStatement() {
1638   //  }
1639   //  private void inclusionStatement() {
1640   //  }
1641   //  public void compoundStatement() {
1642   //  }
1643   //  public void selectionStatement() {
1644   //  }
1645   //
1646   //  public void iterationStatement() {
1647   //  }
1648   //
1649   //  public void jumpStatement() {
1650   //  }
1651   //
1652   //  public void outputStatement() {
1653   //  }
1654   //
1655   //  public void scopeStatement() {
1656   //  }
1657   //
1658   //  public void flowStatement() {
1659   //  }
1660   //
1661   //  public void definitionStatement() {
1662   //  }
1663   private void ifStatement() {
1664     // ':' statement-list [elseif-list] [else-colon-statement] 'endif' ';'
1665     if (token == TokenNameCOLON) {
1666       getNextToken();
1667       if (token != TokenNameendif) {
1668         statementList();
1669         switch (token) {
1670           case TokenNameelse :
1671             getNextToken();
1672             if (token == TokenNameCOLON) {
1673               getNextToken();
1674               if (token != TokenNameendif) {
1675                 statementList();
1676               }
1677             } else {
1678               if (token == TokenNameif) { //'else if'
1679                 getNextToken();
1680                 elseifStatementList();
1681               } else {
1682                 throwSyntaxError("':' expected after 'else'.");
1683               }
1684             }
1685             break;
1686           case TokenNameelseif :
1687             getNextToken();
1688             elseifStatementList();
1689             break;
1690         }
1691       }
1692       if (token != TokenNameendif) {
1693         throwSyntaxError("'endif' expected.");
1694       }
1695       getNextToken();
1696       if (token != TokenNameSEMICOLON) {
1697         throwSyntaxError("';' expected after if-statement.");
1698       }
1699       getNextToken();
1700     } else {
1701       // statement [else-statement]
1702       statement(TokenNameEOF);
1703       if (token == TokenNameelseif) {
1704         getNextToken();
1705         if (token == TokenNameLPAREN) {
1706           getNextToken();
1707         } else {
1708           throwSyntaxError("'(' expected after 'elseif' keyword.");
1709         }
1710         expr();
1711         if (token == TokenNameRPAREN) {
1712           getNextToken();
1713         } else {
1714           throwSyntaxError("')' expected after 'elseif' condition.");
1715         }
1716         ifStatement();
1717       } else if (token == TokenNameelse) {
1718         getNextToken();
1719         statement(TokenNameEOF);
1720       }
1721     }
1722   }
1723   private void elseifStatementList() {
1724     do {
1725       elseifStatement();
1726       switch (token) {
1727         case TokenNameelse :
1728           getNextToken();
1729           if (token == TokenNameCOLON) {
1730             getNextToken();
1731             if (token != TokenNameendif) {
1732               statementList();
1733             }
1734             return;
1735           } else {
1736             if (token == TokenNameif) { //'else if'
1737               getNextToken();
1738             } else {
1739               throwSyntaxError("':' expected after 'else'.");
1740             }
1741           }
1742           break;
1743         case TokenNameelseif :
1744           getNextToken();
1745           break;
1746         default :
1747           return;
1748       }
1749     } while (true);
1750   }
1751   private void elseifStatement() {
1752     if (token == TokenNameLPAREN) {
1753       getNextToken();
1754       expr();
1755       if (token != TokenNameRPAREN) {
1756         throwSyntaxError("')' expected in else-if-statement.");
1757       }
1758       getNextToken();
1759       if (token != TokenNameCOLON) {
1760         throwSyntaxError("':' expected in else-if-statement.");
1761       }
1762       getNextToken();
1763       if (token != TokenNameendif) {
1764         statementList();
1765       }
1766     }
1767   }
1768   private void switchStatement() {
1769     if (token == TokenNameCOLON) {
1770       // ':' [labeled-statement-list] 'endswitch' ';'
1771       getNextToken();
1772       labeledStatementList();
1773       if (token != TokenNameendswitch) {
1774         throwSyntaxError("'endswitch' expected.");
1775       }
1776       getNextToken();
1777       if (token != TokenNameSEMICOLON) {
1778         throwSyntaxError("';' expected after switch-statement.");
1779       }
1780       getNextToken();
1781     } else {
1782       // '{' [labeled-statement-list] '}'
1783       if (token != TokenNameLBRACE) {
1784         throwSyntaxError("'{' expected in switch statement.");
1785       }
1786       getNextToken();
1787       if (token != TokenNameRBRACE) {
1788         labeledStatementList();
1789       }
1790       if (token != TokenNameRBRACE) {
1791         throwSyntaxError("'}' expected in switch statement.");
1792       }
1793       getNextToken();
1794     }
1795   }
1796   private void forStatement() {
1797     if (token == TokenNameCOLON) {
1798       getNextToken();
1799       statementList();
1800       if (token != TokenNameendfor) {
1801         throwSyntaxError("'endfor' expected.");
1802       }
1803       getNextToken();
1804       if (token != TokenNameSEMICOLON) {
1805         throwSyntaxError("';' expected after for-statement.");
1806       }
1807       getNextToken();
1808     } else {
1809       statement(TokenNameEOF);
1810     }
1811   }
1812   private void whileStatement() {
1813     // ':' statement-list 'endwhile' ';'
1814     if (token == TokenNameCOLON) {
1815       getNextToken();
1816       statementList();
1817       if (token != TokenNameendwhile) {
1818         throwSyntaxError("'endwhile' expected.");
1819       }
1820       getNextToken();
1821       if (token != TokenNameSEMICOLON) {
1822         throwSyntaxError("';' expected after while-statement.");
1823       }
1824       getNextToken();
1825     } else {
1826       statement(TokenNameEOF);
1827     }
1828   }
1829   private void foreachStatement() {
1830     if (token == TokenNameCOLON) {
1831       getNextToken();
1832       statementList();
1833       if (token != TokenNameendforeach) {
1834         throwSyntaxError("'endforeach' expected.");
1835       }
1836       getNextToken();
1837       if (token != TokenNameSEMICOLON) {
1838         throwSyntaxError("';' expected after foreach-statement.");
1839       }
1840       getNextToken();
1841     } else {
1842       statement(TokenNameEOF);
1843     }
1844   }
1845   //  private void exitStatus() {
1846   //    if (token == TokenNameLPAREN) {
1847   //      getNextToken();
1848   //    } else {
1849   //      throwSyntaxError("'(' expected in 'exit-status'.");
1850   //    }
1851   //    if (token != TokenNameRPAREN) {
1852   //      expression();
1853   //    }
1854   //    if (token == TokenNameRPAREN) {
1855   //      getNextToken();
1856   //    } else {
1857   //      throwSyntaxError("')' expected after 'exit-status'.");
1858   //    }
1859   //  }
1860   private void expressionList() {
1861     do {
1862       expr();
1863       if (token == TokenNameCOMMA) {
1864         getNextToken();
1865       } else {
1866         break;
1867       }
1868     } while (true);
1869   }
1870   private void expr() {
1871     //  r_variable
1872     //  | expr_without_variable
1873     //    if (token!=TokenNameEOF) {
1874     if (Scanner.TRACE) {
1875       System.out.println("TRACE: expr()");
1876     }
1877     expr_without_variable(true);
1878     //    }
1879   }
1880   private void expr_without_variable(boolean only_variable) {
1881     //          internal_functions_in_yacc
1882     //  | T_CLONE expr
1883     //  | T_PRINT expr
1884     //  | '(' expr ')'
1885     //  | '@' expr
1886     //  | '+' expr
1887     //  | '-' expr
1888     //  | '!' expr
1889     //  | '~' expr
1890     //  | T_INC rw_variable
1891     //  | T_DEC rw_variable
1892     //  | T_INT_CAST expr
1893     //  | T_DOUBLE_CAST expr
1894     //  | T_STRING_CAST expr
1895     //  | T_ARRAY_CAST expr
1896     //  | T_OBJECT_CAST expr
1897     //  | T_BOOL_CAST expr
1898     //  | T_UNSET_CAST expr
1899     //  | T_EXIT exit_expr
1900     //  | scalar
1901     //  | T_ARRAY '(' array_pair_list ')'
1902     //  | '`' encaps_list '`'
1903     //  | T_LIST '(' assignment_list ')' '=' expr
1904     //  | T_NEW class_name_reference ctor_arguments
1905     //  | variable '=' expr
1906     //  | variable '=' '&' variable
1907     //  | variable '=' '&' T_NEW class_name_reference ctor_arguments
1908     //  | variable T_PLUS_EQUAL expr
1909     //  | variable T_MINUS_EQUAL expr
1910     //  | variable T_MUL_EQUAL expr
1911     //  | variable T_DIV_EQUAL expr
1912     //  | variable T_CONCAT_EQUAL expr
1913     //  | variable T_MOD_EQUAL expr
1914     //  | variable T_AND_EQUAL expr
1915     //  | variable T_OR_EQUAL expr
1916     //  | variable T_XOR_EQUAL expr
1917     //  | variable T_SL_EQUAL expr
1918     //  | variable T_SR_EQUAL expr
1919     //  | rw_variable T_INC
1920     //  | rw_variable T_DEC
1921     //  | expr T_BOOLEAN_OR expr
1922     //  | expr T_BOOLEAN_AND expr
1923     //  | expr T_LOGICAL_OR expr
1924     //  | expr T_LOGICAL_AND expr
1925     //  | expr T_LOGICAL_XOR expr
1926     //  | expr '|' expr
1927     //  | expr '&' expr
1928     //  | expr '^' expr
1929     //  | expr '.' expr
1930     //  | expr '+' expr
1931     //  | expr '-' expr
1932     //  | expr '*' expr
1933     //  | expr '/' expr
1934     //  | expr '%' expr
1935     //  | expr T_SL expr
1936     //  | expr T_SR expr
1937     //  | expr T_IS_IDENTICAL expr
1938     //  | expr T_IS_NOT_IDENTICAL expr
1939     //  | expr T_IS_EQUAL expr
1940     //  | expr T_IS_NOT_EQUAL expr
1941     //  | expr '<' expr
1942     //  | expr T_IS_SMALLER_OR_EQUAL expr
1943     //  | expr '>' expr
1944     //  | expr T_IS_GREATER_OR_EQUAL expr
1945     //  | expr T_INSTANCEOF class_name_reference
1946     //  | expr '?' expr ':' expr
1947     if (Scanner.TRACE) {
1948       System.out.println("TRACE: expr_without_variable() PART 1");
1949     }
1950     switch (token) {
1951       case TokenNameisset :
1952       case TokenNameempty :
1953       case TokenNameeval :
1954       case TokenNameinclude :
1955       case TokenNameinclude_once :
1956       case TokenNamerequire :
1957       case TokenNamerequire_once :
1958         internal_functions_in_yacc();
1959         break;
1960       //        | '(' expr ')'
1961       case TokenNameLPAREN :
1962         getNextToken();
1963         expr();
1964         if (token == TokenNameRPAREN) {
1965           getNextToken();
1966         } else {
1967           throwSyntaxError("')' expected in expression.");
1968         }
1969         break;
1970       //    | T_CLONE expr
1971       //    | T_PRINT expr
1972       //    | '@' expr
1973       //    | '+' expr
1974       //    | '-' expr
1975       //    | '!' expr
1976       //    | '~' expr
1977       //    | T_INT_CAST expr
1978       //        | T_DOUBLE_CAST expr
1979       //        | T_STRING_CAST expr
1980       //        | T_ARRAY_CAST expr
1981       //        | T_OBJECT_CAST expr
1982       //        | T_BOOL_CAST expr
1983       //        | T_UNSET_CAST expr
1984       case TokenNameclone :
1985       case TokenNameprint :
1986       case TokenNameAT :
1987       case TokenNamePLUS :
1988       case TokenNameMINUS :
1989       case TokenNameNOT :
1990       case TokenNameTWIDDLE :
1991       case TokenNameintCAST :
1992       case TokenNamedoubleCAST :
1993       case TokenNamestringCAST :
1994       case TokenNamearrayCAST :
1995       case TokenNameobjectCAST :
1996       case TokenNameboolCAST :
1997       case TokenNameunsetCAST :
1998         getNextToken();
1999         expr();
2000         break;
2001       case TokenNameexit :
2002         getNextToken();
2003         exit_expr();
2004         break;
2005       //  scalar:
2006       //        T_STRING
2007       //| T_STRING_VARNAME
2008       //| class_constant
2009       //| T_START_HEREDOC encaps_list T_END_HEREDOC
2010       //        | '`' encaps_list '`'
2011       //  | common_scalar
2012       //        | '`' encaps_list '`'
2013       case TokenNameEncapsedString0 :
2014         scanner.encapsedStringStack.push(new Character('`'));
2015         getNextToken();
2016         try {
2017           if (token == TokenNameEncapsedString0) {
2018           } else {
2019             encaps_list();
2020             if (token != TokenNameEncapsedString0) {
2021               throwSyntaxError("\'`\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2022             }
2023           }
2024         } finally {
2025           scanner.encapsedStringStack.pop();
2026           getNextToken();
2027         }
2028         break;
2029       //      | '\'' encaps_list '\''
2030       case TokenNameEncapsedString1 :
2031         scanner.encapsedStringStack.push(new Character('\''));
2032         getNextToken();
2033         try {
2034           if (token == TokenNameEncapsedString1) {
2035           } else {
2036             encaps_list();
2037             if (token != TokenNameEncapsedString1) {
2038               throwSyntaxError("\'\'\' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2039             }
2040           }
2041         } finally {
2042           scanner.encapsedStringStack.pop();
2043           getNextToken();
2044         }
2045         break;
2046       //| '"' encaps_list '"'
2047       case TokenNameEncapsedString2 :
2048         scanner.encapsedStringStack.push(new Character('"'));
2049         getNextToken();
2050         try {
2051           if (token == TokenNameEncapsedString2) {
2052           } else {
2053             encaps_list();
2054             if (token != TokenNameEncapsedString2) {
2055               throwSyntaxError("'\"' expected at end of string" + "(Found token: " + scanner.toStringAction(token) + " )");
2056             }
2057           }
2058         } finally {
2059           scanner.encapsedStringStack.pop();
2060           getNextToken();
2061         }
2062         break;
2063       case TokenNameIntegerLiteral :
2064       case TokenNameDoubleLiteral :
2065       case TokenNameStringDoubleQuote :
2066       case TokenNameStringSingleQuote :
2067       case TokenNameStringInterpolated :
2068       case TokenNameFILE :
2069       case TokenNameLINE :
2070       case TokenNameCLASS_C :
2071       case TokenNameMETHOD_C :
2072       case TokenNameFUNC_C :
2073         common_scalar();
2074         break;
2075       case TokenNameHEREDOC :
2076         getNextToken();
2077         break;
2078       case TokenNamearray :
2079         //    T_ARRAY '(' array_pair_list ')'
2080         getNextToken();
2081         if (token == TokenNameLPAREN) {
2082           getNextToken();
2083           if (token == TokenNameRPAREN) {
2084             getNextToken();
2085             break;
2086           }
2087           array_pair_list();
2088           if (token != TokenNameRPAREN) {
2089             throwSyntaxError("')' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2090           }
2091           getNextToken();
2092         } else {
2093           throwSyntaxError("'(' expected after keyword 'array'" + "(Found token: " + scanner.toStringAction(token) + ")");
2094         }
2095         break;
2096       case TokenNamelist :
2097         //    | T_LIST '(' assignment_list ')' '=' expr
2098         getNextToken();
2099         if (token == TokenNameLPAREN) {
2100           getNextToken();
2101           assignment_list();
2102           if (token != TokenNameRPAREN) {
2103             throwSyntaxError("')' expected after 'list' keyword.");
2104           }
2105           getNextToken();
2106           if (token != TokenNameEQUAL) {
2107             throwSyntaxError("'=' expected after 'list' keyword.");
2108           }
2109           getNextToken();
2110           expr();
2111         } else {
2112           throwSyntaxError("'(' expected after 'list' keyword.");
2113         }
2114         break;
2115       case TokenNamenew :
2116         //      | T_NEW class_name_reference ctor_arguments
2117         getNextToken();
2118         class_name_reference();
2119         ctor_arguments();
2120         break;
2121       //        | T_INC rw_variable
2122       //        | T_DEC rw_variable
2123       case TokenNamePLUS_PLUS :
2124       case TokenNameMINUS_MINUS :
2125         getNextToken();
2126         rw_variable();
2127         break;
2128       //        | variable '=' expr
2129       //        | variable '=' '&' variable
2130       //        | variable '=' '&' T_NEW class_name_reference ctor_arguments
2131       //        | variable T_PLUS_EQUAL expr
2132       //        | variable T_MINUS_EQUAL expr
2133       //        | variable T_MUL_EQUAL expr
2134       //        | variable T_DIV_EQUAL expr
2135       //        | variable T_CONCAT_EQUAL expr
2136       //        | variable T_MOD_EQUAL expr
2137       //        | variable T_AND_EQUAL expr
2138       //        | variable T_OR_EQUAL expr
2139       //        | variable T_XOR_EQUAL expr
2140       //        | variable T_SL_EQUAL expr
2141       //        | variable T_SR_EQUAL expr
2142       //        | rw_variable T_INC
2143       //        | rw_variable T_DEC
2144       case TokenNameIdentifier :
2145       case TokenNameVariable :
2146       case TokenNameDOLLAR :
2147         variable();
2148         switch (token) {
2149           case TokenNameEQUAL :
2150             getNextToken();
2151             if (token == TokenNameAND) {
2152               getNextToken();
2153               if (token == TokenNamenew) {
2154                 // | variable '=' '&' T_NEW class_name_reference
2155                 // ctor_arguments
2156                 getNextToken();
2157                 class_name_reference();
2158                 ctor_arguments();
2159               } else {
2160                 variable();
2161               }
2162             } else {
2163               expr();
2164             }
2165             break;
2166           case TokenNamePLUS_EQUAL :
2167           case TokenNameMINUS_EQUAL :
2168           case TokenNameMULTIPLY_EQUAL :
2169           case TokenNameDIVIDE_EQUAL :
2170           case TokenNameDOT_EQUAL :
2171           case TokenNameREMAINDER_EQUAL :
2172           case TokenNameAND_EQUAL :
2173           case TokenNameOR_EQUAL :
2174           case TokenNameXOR_EQUAL :
2175           case TokenNameRIGHT_SHIFT_EQUAL :
2176           case TokenNameLEFT_SHIFT_EQUAL :
2177             getNextToken();
2178             expr();
2179             break;
2180           case TokenNamePLUS_PLUS :
2181           case TokenNameMINUS_MINUS :
2182             getNextToken();
2183             break;
2184           default :
2185             if (!only_variable) {
2186               throwSyntaxError("Variable expression not allowed (found token '" + scanner.toStringAction(token) + "').");
2187             }
2188         }
2189         break;
2190       default :
2191         if (token != TokenNameINLINE_HTML) {
2192           if (token > TokenNameKEYWORD) {
2193             getNextToken();
2194             break;
2195           } else {
2196             throwSyntaxError("Error in expression (found token '" + scanner.toStringAction(token) + "').");
2197           }
2198         }
2199         return;
2200     }
2201     if (Scanner.TRACE) {
2202       System.out.println("TRACE: expr_without_variable() PART 2");
2203     }
2204     //  | expr T_BOOLEAN_OR expr
2205     //  | expr T_BOOLEAN_AND expr
2206     //  | expr T_LOGICAL_OR expr
2207     //  | expr T_LOGICAL_AND expr
2208     //  | expr T_LOGICAL_XOR expr
2209     //  | expr '|' expr
2210     //  | expr '&' expr
2211     //  | expr '^' expr
2212     //  | expr '.' expr
2213     //  | expr '+' expr
2214     //  | expr '-' expr
2215     //  | expr '*' expr
2216     //  | expr '/' expr
2217     //  | expr '%' expr
2218     //  | expr T_SL expr
2219     //  | expr T_SR expr
2220     //  | expr T_IS_IDENTICAL expr
2221     //  | expr T_IS_NOT_IDENTICAL expr
2222     //  | expr T_IS_EQUAL expr
2223     //  | expr T_IS_NOT_EQUAL expr
2224     //  | expr '<' expr
2225     //  | expr T_IS_SMALLER_OR_EQUAL expr
2226     //  | expr '>' expr
2227     //  | expr T_IS_GREATER_OR_EQUAL expr
2228     while (true) {
2229       switch (token) {
2230         case TokenNameOR_OR :
2231         case TokenNameAND_AND :
2232         case TokenNameand :
2233         case TokenNameor :
2234         case TokenNamexor :
2235         case TokenNameAND :
2236         case TokenNameOR :
2237         case TokenNameXOR :
2238         case TokenNameDOT :
2239         case TokenNamePLUS :
2240         case TokenNameMINUS :
2241         case TokenNameMULTIPLY :
2242         case TokenNameDIVIDE :
2243         case TokenNameREMAINDER :
2244         case TokenNameLEFT_SHIFT :
2245         case TokenNameRIGHT_SHIFT :
2246         case TokenNameEQUAL_EQUAL_EQUAL :
2247         case TokenNameNOT_EQUAL_EQUAL :
2248         case TokenNameEQUAL_EQUAL :
2249         case TokenNameNOT_EQUAL :
2250         case TokenNameLESS :
2251         case TokenNameLESS_EQUAL :
2252         case TokenNameGREATER :
2253         case TokenNameGREATER_EQUAL :
2254           getNextToken();
2255           expr();
2256           break;
2257         //  | expr T_INSTANCEOF class_name_reference
2258         //      | expr '?' expr ':' expr
2259         case TokenNameinstanceof :
2260           getNextToken();
2261           class_name_reference();
2262           break;
2263         case TokenNameQUESTION :
2264           getNextToken();
2265           expr();
2266           if (token == TokenNameCOLON) {
2267             getNextToken();
2268             expr();
2269           }
2270           break;
2271         default :
2272           return;
2273       }
2274     }
2275   }
2276   private void class_name_reference() {
2277     //  class_name_reference:
2278     //  T_STRING
2279     //| dynamic_class_name_reference
2280     if (Scanner.TRACE) {
2281       System.out.println("TRACE: class_name_reference()");
2282     }
2283     if (token == TokenNameIdentifier) {
2284       getNextToken();
2285     } else {
2286       dynamic_class_name_reference();
2287     }
2288   }
2289   private void dynamic_class_name_reference() {
2290     //dynamic_class_name_reference:
2291     //  base_variable T_OBJECT_OPERATOR object_property
2292     // dynamic_class_name_variable_properties
2293     //| base_variable
2294     if (Scanner.TRACE) {
2295       System.out.println("TRACE: dynamic_class_name_reference()");
2296     }
2297     base_variable();
2298     if (token == TokenNameMINUS_GREATER) {
2299       getNextToken();
2300       object_property();
2301       dynamic_class_name_variable_properties();
2302     }
2303   }
2304   private void dynamic_class_name_variable_properties() {
2305     //  dynamic_class_name_variable_properties:
2306     //                  dynamic_class_name_variable_properties
2307     // dynamic_class_name_variable_property
2308     //          | /* empty */
2309     if (Scanner.TRACE) {
2310       System.out.println("TRACE: dynamic_class_name_variable_properties()");
2311     }
2312     while (token == TokenNameMINUS_GREATER) {
2313       dynamic_class_name_variable_property();
2314     }
2315   }
2316   private void dynamic_class_name_variable_property() {
2317     //  dynamic_class_name_variable_property:
2318     //  T_OBJECT_OPERATOR object_property
2319     if (Scanner.TRACE) {
2320       System.out.println("TRACE: dynamic_class_name_variable_property()");
2321     }
2322     if (token == TokenNameMINUS_GREATER) {
2323       getNextToken();
2324       object_property();
2325     }
2326   }
2327   private void ctor_arguments() {
2328     //  ctor_arguments:
2329     //  /* empty */
2330     //| '(' function_call_parameter_list ')'
2331     if (token == TokenNameLPAREN) {
2332       getNextToken();
2333       if (token == TokenNameRPAREN) {
2334         getNextToken();
2335         return;
2336       }
2337       non_empty_function_call_parameter_list();
2338       if (token != TokenNameRPAREN) {
2339         throwSyntaxError("')' expected in ctor_arguments.");
2340       }
2341       getNextToken();
2342     }
2343   }
2344   private void assignment_list() {
2345     //  assignment_list:
2346     //  assignment_list ',' assignment_list_element
2347     //| assignment_list_element
2348     while (true) {
2349       assignment_list_element();
2350       if (token != TokenNameCOMMA) {
2351         break;
2352       }
2353       getNextToken();
2354     }
2355   }
2356   private void assignment_list_element() {
2357     //assignment_list_element:
2358     //  variable
2359     //| T_LIST '(' assignment_list ')'
2360     //| /* empty */
2361     if (token == TokenNameVariable || token == TokenNameDOLLAR) {
2362       variable();
2363     } else {
2364       if (token == TokenNamelist) {
2365         getNextToken();
2366         if (token == TokenNameLPAREN) {
2367           getNextToken();
2368           assignment_list();
2369           if (token != TokenNameRPAREN) {
2370             throwSyntaxError("')' expected after 'list' keyword.");
2371           }
2372           getNextToken();
2373         } else {
2374           throwSyntaxError("'(' expected after 'list' keyword.");
2375         }
2376       }
2377     }
2378   }
2379   private void array_pair_list() {
2380     //  array_pair_list:
2381     //  /* empty */
2382     //| non_empty_array_pair_list possible_comma
2383     non_empty_array_pair_list();
2384     if (token == TokenNameCOMMA) {
2385       getNextToken();
2386     }
2387   }
2388   private void non_empty_array_pair_list() {
2389     //non_empty_array_pair_list:
2390     //  non_empty_array_pair_list ',' expr T_DOUBLE_ARROW expr
2391     //| non_empty_array_pair_list ',' expr
2392     //| expr T_DOUBLE_ARROW expr
2393     //| expr
2394     //| non_empty_array_pair_list ',' expr T_DOUBLE_ARROW '&' w_variable
2395     //| non_empty_array_pair_list ',' '&' w_variable
2396     //| expr T_DOUBLE_ARROW '&' w_variable
2397     //| '&' w_variable
2398     while (true) {
2399       if (token == TokenNameAND) {
2400         getNextToken();
2401         variable();
2402       } else {
2403         expr();
2404         if (token == TokenNameAND) {
2405           getNextToken();
2406           variable();
2407         } else if (token == TokenNameEQUAL_GREATER) {
2408           getNextToken();
2409           if (token == TokenNameAND) {
2410             getNextToken();
2411             variable();
2412           } else {
2413             expr();
2414           }
2415         }
2416       }
2417       if (token != TokenNameCOMMA) {
2418         return;
2419       }
2420       getNextToken();
2421       if (token == TokenNameRPAREN) {
2422         return;
2423       }
2424     }
2425   }
2426   //  private void variableList() {
2427   //    do {
2428   //      variable();
2429   //      if (token == TokenNameCOMMA) {
2430   //        getNextToken();
2431   //      } else {
2432   //        break;
2433   //      }
2434   //    } while (true);
2435   //  }
2436   private void variable_without_objects() {
2437     //  variable_without_objects:
2438     //                  reference_variable
2439     //          | simple_indirect_reference reference_variable
2440     if (Scanner.TRACE) {
2441       System.out.println("TRACE: variable_without_objects()");
2442     }
2443     while (token == TokenNameDOLLAR) {
2444       getNextToken();
2445     }
2446     reference_variable();
2447   }
2448   private void function_call() {
2449     //  function_call:
2450     //  T_STRING '(' function_call_parameter_list ')'
2451     //| class_constant '(' function_call_parameter_list ')'
2452     //| static_member '(' function_call_parameter_list ')'
2453     //| variable_without_objects '(' function_call_parameter_list ')'
2454     if (Scanner.TRACE) {
2455       System.out.println("TRACE: function_call()");
2456     }
2457     if (token == TokenNameIdentifier) {
2458       getNextToken();
2459       switch (token) {
2460         case TokenNamePAAMAYIM_NEKUDOTAYIM :
2461           // static member:
2462           getNextToken();
2463           if (token == TokenNameIdentifier) {
2464             // class _constant
2465             getNextToken();
2466           } else {
2467             //        static member:
2468             variable_without_objects();
2469           }
2470           break;
2471       }
2472     } else {
2473       variable_without_objects();
2474     }
2475     if (token != TokenNameLPAREN) {
2476       // TODO is this ok ?
2477       return;
2478       //      throwSyntaxError("'(' expected in function call.");
2479     }
2480     getNextToken();
2481     if (token == TokenNameRPAREN) {
2482       getNextToken();
2483       return;
2484     }
2485     non_empty_function_call_parameter_list();
2486     if (token != TokenNameRPAREN) {
2487       throwSyntaxError("')' expected in function call.");
2488     }
2489     getNextToken();
2490   }
2491   //  private void function_call_parameter_list() {
2492   //    function_call_parameter_list:
2493   //            non_empty_function_call_parameter_list { $$ = $1; }
2494   //    | /* empty */
2495   //  }
2496   private void non_empty_function_call_parameter_list() {
2497     //non_empty_function_call_parameter_list:
2498     //          expr_without_variable
2499     //  | variable
2500     //  | '&' w_variable
2501     //  | non_empty_function_call_parameter_list ',' expr_without_variable
2502     //  | non_empty_function_call_parameter_list ',' variable
2503     //  | non_empty_function_call_parameter_list ',' '&' w_variable
2504     if (Scanner.TRACE) {
2505       System.out.println("TRACE: non_empty_function_call_parameter_list()");
2506     }
2507     while (true) {
2508       if (token == TokenNameAND) {
2509         getNextToken();
2510         w_variable();
2511       } else {
2512         //        if (token == TokenNameIdentifier || token ==
2513         // TokenNameVariable
2514         //            || token == TokenNameDOLLAR) {
2515         //          variable();
2516         //        } else {
2517         expr_without_variable(true);
2518         //        }
2519       }
2520       if (token != TokenNameCOMMA) {
2521         break;
2522       }
2523       getNextToken();
2524     }
2525   }
2526   private void fully_qualified_class_name() {
2527     if (token == TokenNameIdentifier) {
2528       getNextToken();
2529     } else {
2530       throwSyntaxError("Class name expected.");
2531     }
2532   }
2533   private void static_member() {
2534     //  static_member:
2535     //  fully_qualified_class_name T_PAAMAYIM_NEKUDOTAYIM
2536     // variable_without_objects
2537     if (Scanner.TRACE) {
2538       System.out.println("TRACE: static_member()");
2539     }
2540     fully_qualified_class_name();
2541     if (token != TokenNamePAAMAYIM_NEKUDOTAYIM) {
2542       throwSyntaxError("'::' expected after class name (static_member).");
2543     }
2544     getNextToken();
2545     variable_without_objects();
2546   }
2547   private void base_variable_with_function_calls() {
2548     //  base_variable_with_function_calls:
2549     //  base_variable
2550     //| function_call
2551     boolean functionCall = false;
2552     if (Scanner.TRACE) {
2553       System.out.println("TRACE: base_variable_with_function_calls()");
2554     }
2555     //    if (token == TokenNameIdentifier) {
2556     //      functionCall = true;
2557     //    } else if (token == TokenNameVariable) {
2558     //      int tempToken = token;
2559     //      int tempPosition = scanner.currentPosition;
2560     //      getNextToken();
2561     //      if (token == TokenNameLPAREN) {
2562     //        functionCall = true;
2563     //      }
2564     //      token = tempToken;
2565     //      scanner.currentPosition = tempPosition;
2566     //      scanner.phpMode = true;
2567     //    }
2568     //    if (functionCall) {
2569     function_call();
2570     //    } else {
2571     //      base_variable();
2572     //    }
2573   }
2574   private void base_variable() {
2575     //  base_variable:
2576     //                  reference_variable
2577     //          | simple_indirect_reference reference_variable
2578     //          | static_member
2579     if (Scanner.TRACE) {
2580       System.out.println("TRACE: base_variable()");
2581     }
2582     if (token == TokenNameIdentifier) {
2583       static_member();
2584     } else {
2585       while (token == TokenNameDOLLAR) {
2586         getNextToken();
2587       }
2588       reference_variable();
2589     }
2590   }
2591   //  private void simple_indirect_reference() {
2592   //    // simple_indirect_reference:
2593   //    // '$'
2594   //    //| simple_indirect_reference '$'
2595   //  }
2596   private void reference_variable() {
2597     //  reference_variable:
2598     //                  reference_variable '[' dim_offset ']'
2599     //          | reference_variable '{' expr '}'
2600     //          | compound_variable
2601     if (Scanner.TRACE) {
2602       System.out.println("TRACE: reference_variable()");
2603     }
2604     compound_variable();
2605     while (true) {
2606       if (token == TokenNameLBRACE) {
2607         getNextToken();
2608         expr();
2609         if (token != TokenNameRBRACE) {
2610           throwSyntaxError("'}' expected in reference variable.");
2611         }
2612         getNextToken();
2613       } else if (token == TokenNameLBRACKET) {
2614         getNextToken();
2615         if (token != TokenNameRBRACKET) {
2616           expr();
2617           //        dim_offset();
2618           if (token != TokenNameRBRACKET) {
2619             throwSyntaxError("']' expected in reference variable.");
2620           }
2621         }
2622         getNextToken();
2623       } else {
2624         break;
2625       }
2626     }
2627   }
2628   private void compound_variable() {
2629     //  compound_variable:
2630     //                  T_VARIABLE
2631     //          | '$' '{' expr '}'
2632     if (Scanner.TRACE) {
2633       System.out.println("TRACE: compound_variable()");
2634     }
2635     if (token == TokenNameVariable) {
2636       getNextToken();
2637     } else {
2638       // because of simple_indirect_reference
2639       while (token == TokenNameDOLLAR) {
2640         getNextToken();
2641       }
2642       if (token != TokenNameLBRACE) {
2643         throwSyntaxError("'{' expected after compound variable token '$'.");
2644       }
2645       getNextToken();
2646       expr();
2647       if (token != TokenNameRBRACE) {
2648         throwSyntaxError("'}' expected after compound variable token '$'.");
2649       }
2650       getNextToken();
2651     }
2652   }
2653   //  private void dim_offset() {
2654   //    // dim_offset:
2655   //    // /* empty */
2656   //    // | expr
2657   //    expr();
2658   //  }
2659   private void object_property() {
2660     //  object_property:
2661     //  object_dim_list
2662     //| variable_without_objects
2663     if (Scanner.TRACE) {
2664       System.out.println("TRACE: object_property()");
2665     }
2666     if (token == TokenNameVariable || token == TokenNameDOLLAR) {
2667       variable_without_objects();
2668     } else {
2669       object_dim_list();
2670     }
2671   }
2672   private void object_dim_list() {
2673     //object_dim_list:
2674     //  object_dim_list '[' dim_offset ']'
2675     //| object_dim_list '{' expr '}'
2676     //| variable_name
2677     if (Scanner.TRACE) {
2678       System.out.println("TRACE: object_dim_list()");
2679     }
2680     variable_name();
2681     while (true) {
2682       if (token == TokenNameLBRACE) {
2683         getNextToken();
2684         expr();
2685         if (token != TokenNameRBRACE) {
2686           throwSyntaxError("'}' expected in object_dim_list.");
2687         }
2688         getNextToken();
2689       } else if (token == TokenNameLBRACKET) {
2690         getNextToken();
2691         if (token == TokenNameRBRACKET) {
2692           getNextToken();
2693           continue;
2694         }
2695         expr();
2696         if (token != TokenNameRBRACKET) {
2697           throwSyntaxError("']' expected in object_dim_list.");
2698         }
2699         getNextToken();
2700       } else {
2701         break;
2702       }
2703     }
2704   }
2705   private void variable_name() {
2706     //variable_name:
2707     //  T_STRING
2708     //| '{' expr '}'
2709     if (Scanner.TRACE) {
2710       System.out.println("TRACE: variable_name()");
2711     }
2712     if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
2713       if (token > TokenNameKEYWORD) {
2714         // TODO show a warning "Keyword used as variable" ?
2715       }
2716       getNextToken();
2717     } else {
2718       if (token != TokenNameLBRACE) {
2719         throwSyntaxError("'{' expected in variable name.");
2720       }
2721       getNextToken();
2722       expr();
2723       if (token != TokenNameRBRACE) {
2724         throwSyntaxError("'}' expected in variable name.");
2725       }
2726       getNextToken();
2727     }
2728   }
2729   private void r_variable() {
2730     variable();
2731   }
2732   private void w_variable() {
2733     variable();
2734   }
2735   private void rw_variable() {
2736     variable();
2737   }
2738   private void variable() {
2739     //    variable:
2740     //          base_variable_with_function_calls T_OBJECT_OPERATOR
2741     //                  object_property method_or_not variable_properties
2742     //  | base_variable_with_function_calls
2743     base_variable_with_function_calls();
2744     if (token == TokenNameMINUS_GREATER) {
2745       getNextToken();
2746       object_property();
2747       method_or_not();
2748       variable_properties();
2749     }
2750     //    if (token == TokenNameDOLLAR_LBRACE) {
2751     //      getNextToken();
2752     //      expr();
2753     //      ;
2754     //      if (token != TokenNameRBRACE) {
2755     //        throwSyntaxError("'}' expected after indirect variable token '${'.");
2756     //      }
2757     //      getNextToken();
2758     //    } else {
2759     //      if (token == TokenNameVariable) {
2760     //        getNextToken();
2761     //        if (token == TokenNameLBRACKET) {
2762     //          getNextToken();
2763     //          expr();
2764     //          if (token != TokenNameRBRACKET) {
2765     //            throwSyntaxError("']' expected in variable-list.");
2766     //          }
2767     //          getNextToken();
2768     //        } else if (token == TokenNameEQUAL) {
2769     //          getNextToken();
2770     //          static_scalar();
2771     //        }
2772     //      } else {
2773     //        throwSyntaxError("$-variable expected in variable-list.");
2774     //      }
2775     //    }
2776   }
2777   private void variable_properties() {
2778     //  variable_properties:
2779     //                  variable_properties variable_property
2780     //          | /* empty */
2781     while (token == TokenNameMINUS_GREATER) {
2782       variable_property();
2783     }
2784   }
2785   private void variable_property() {
2786     //  variable_property:
2787     //                  T_OBJECT_OPERATOR object_property method_or_not
2788     if (Scanner.TRACE) {
2789       System.out.println("TRACE: variable_property()");
2790     }
2791     if (token == TokenNameMINUS_GREATER) {
2792       getNextToken();
2793       object_property();
2794       method_or_not();
2795     } else {
2796       throwSyntaxError("'->' expected in variable_property.");
2797     }
2798   }
2799   private void method_or_not() {
2800     //  method_or_not:
2801     //                  '(' function_call_parameter_list ')'
2802     //          | /* empty */
2803     if (Scanner.TRACE) {
2804       System.out.println("TRACE: method_or_not()");
2805     }
2806     if (token == TokenNameLPAREN) {
2807       getNextToken();
2808       if (token == TokenNameRPAREN) {
2809         getNextToken();
2810         return;
2811       }
2812       non_empty_function_call_parameter_list();
2813       if (token != TokenNameRPAREN) {
2814         throwSyntaxError("')' expected in method_or_not.");
2815       }
2816       getNextToken();
2817     }
2818   }
2819   private void exit_expr() {
2820     //  /* empty */
2821     //  | '(' ')'
2822     //  | '(' expr ')'
2823     if (token != TokenNameLPAREN) {
2824       return;
2825     }
2826     getNextToken();
2827     if (token == TokenNameRPAREN) {
2828       getNextToken();
2829       return;
2830     }
2831     expr();
2832     if (token != TokenNameRPAREN) {
2833       throwSyntaxError("')' expected after keyword 'exit'");
2834     }
2835     getNextToken();
2836   }
2837   private void encaps_list() {
2838     //                  encaps_list encaps_var
2839     //          | encaps_list T_STRING
2840     //          | encaps_list T_NUM_STRING
2841     //          | encaps_list T_ENCAPSED_AND_WHITESPACE
2842     //          | encaps_list T_CHARACTER
2843     //          | encaps_list T_BAD_CHARACTER
2844     //          | encaps_list '['
2845     //          | encaps_list ']'
2846     //          | encaps_list '{'
2847     //          | encaps_list '}'
2848     //          | encaps_list T_OBJECT_OPERATOR
2849     //          | /* empty */
2850     while (true) {
2851       switch (token) {
2852         case TokenNameSTRING :
2853           getNextToken();
2854           break;
2855         case TokenNameLBRACE :
2856           //          scanner.encapsedStringStack.pop();
2857           getNextToken();
2858           break;
2859         case TokenNameRBRACE :
2860           //          scanner.encapsedStringStack.pop();
2861           getNextToken();
2862           break;
2863         case TokenNameLBRACKET :
2864           //          scanner.encapsedStringStack.pop();
2865           getNextToken();
2866           break;
2867         case TokenNameRBRACKET :
2868           //          scanner.encapsedStringStack.pop();
2869           getNextToken();
2870           break;
2871         case TokenNameMINUS_GREATER :
2872           //          scanner.encapsedStringStack.pop();
2873           getNextToken();
2874           break;
2875         case TokenNameVariable :
2876         case TokenNameDOLLAR_LBRACE :
2877         case TokenNameCURLY_OPEN :
2878           encaps_var();
2879           break;
2880         //        case TokenNameDOLLAR :
2881         //          getNextToken();
2882         //          if (token == TokenNameLBRACE) {
2883         //            token = TokenNameDOLLAR_LBRACE;
2884         //            encaps_var();
2885         //          }
2886         //          break;
2887         default :
2888           char encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
2889           if (encapsedChar == '$') {
2890             scanner.encapsedStringStack.pop();
2891             encapsedChar = ((Character) scanner.encapsedStringStack.peek()).charValue();
2892             switch (encapsedChar) {
2893               case '`' :
2894                 if (token == TokenNameEncapsedString0) {
2895                   return;
2896                 }
2897                 token = TokenNameSTRING;
2898                 continue;
2899               case '\'' :
2900                 if (token == TokenNameEncapsedString1) {
2901                   return;
2902                 }
2903                 token = TokenNameSTRING;
2904                 continue;
2905               case '"' :
2906                 if (token == TokenNameEncapsedString2) {
2907                   return;
2908                 }
2909                 token = TokenNameSTRING;
2910                 continue;
2911             }
2912           }
2913           return;
2914       }
2915     }
2916   }
2917   private void encaps_var() {
2918     //                  T_VARIABLE
2919     //          | T_VARIABLE '[' encaps_var_offset ']'
2920     //          | T_VARIABLE T_OBJECT_OPERATOR T_STRING
2921     //          | T_DOLLAR_OPEN_CURLY_BRACES expr '}'
2922     //          | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '[' expr ']' '}'
2923     //          | T_CURLY_OPEN variable '}'
2924     switch (token) {
2925       case TokenNameVariable :
2926         getNextToken();
2927         if (token == TokenNameLBRACKET) {
2928           getNextToken();
2929           //          if (token == TokenNameRBRACKET) {
2930           //            getNextToken();
2931           //          } else {
2932           expr(); //encaps_var_offset();
2933           if (token != TokenNameRBRACKET) {
2934             throwSyntaxError("']' expected after variable.");
2935           }
2936           //          scanner.encapsedStringStack.pop();
2937           getNextToken();
2938           //          }
2939         } else if (token == TokenNameMINUS_GREATER) {
2940           getNextToken();
2941           if (token != TokenNameIdentifier) {
2942             throwSyntaxError("Identifier expected after '->'.");
2943           }
2944           //          scanner.encapsedStringStack.pop();
2945           getNextToken();
2946         }
2947         //        else {
2948         //          // scanner.encapsedStringStack.pop();
2949         //          int tempToken = TokenNameSTRING;
2950         //          if (!scanner.encapsedStringStack.isEmpty()
2951         //              && (token == TokenNameEncapsedString0
2952         //                  || token == TokenNameEncapsedString1
2953         //                  || token == TokenNameEncapsedString2 || token ==
2954         // TokenNameERROR)) {
2955         //            char encapsedChar = ((Character)
2956         // scanner.encapsedStringStack.peek())
2957         //                .charValue();
2958         //            switch (token) {
2959         //              case TokenNameEncapsedString0 :
2960         //                if (encapsedChar == '`') {
2961         //                  tempToken = TokenNameEncapsedString0;
2962         //                }
2963         //                break;
2964         //              case TokenNameEncapsedString1 :
2965         //                if (encapsedChar == '\'') {
2966         //                  tempToken = TokenNameEncapsedString1;
2967         //                }
2968         //                break;
2969         //              case TokenNameEncapsedString2 :
2970         //                if (encapsedChar == '"') {
2971         //                  tempToken = TokenNameEncapsedString2;
2972         //                }
2973         //                break;
2974         //              case TokenNameERROR :
2975         //                if (scanner.source[scanner.currentPosition - 1] == '\\') {
2976         //                  scanner.currentPosition--;
2977         //                  getNextToken();
2978         //                }
2979         //                break;
2980         //            }
2981         //          }
2982         //          token = tempToken;
2983         //        }
2984         break;
2985       case TokenNameDOLLAR_LBRACE :
2986         getNextToken();
2987         if (token == TokenNameIdentifier) {
2988           getNextToken();
2989           if (token == TokenNameLBRACKET) {
2990             getNextToken();
2991             //            if (token == TokenNameRBRACKET) {
2992             //              getNextToken();
2993             //            } else {
2994             expr();
2995             if (token != TokenNameRBRACKET) {
2996               throwSyntaxError("']' expected after '${'.");
2997             }
2998             getNextToken();
2999             //            }
3000           }
3001           if (token != TokenNameRBRACE) {
3002             throwSyntaxError("'}' expected after '${'.");
3003           }
3004           //          scanner.encapsedStringStack.pop();
3005           getNextToken();
3006         } else {
3007           expr();
3008           if (token != TokenNameRBRACE) {
3009             throwSyntaxError("'}' expected.");
3010           }
3011           //          scanner.encapsedStringStack.pop();
3012           getNextToken();
3013         }
3014         break;
3015       case TokenNameCURLY_OPEN :
3016         getNextToken();
3017         if (token == TokenNameIdentifier || token > TokenNameKEYWORD) {
3018           getNextToken();
3019           if (token == TokenNameLBRACKET) {
3020             getNextToken();
3021             //            if (token == TokenNameRBRACKET) {
3022             //              getNextToken();
3023             //            } else {
3024             expr();
3025             if (token != TokenNameRBRACKET) {
3026               throwSyntaxError("']' expected after '{$'.");
3027             }
3028             getNextToken();
3029             //            }
3030           } else if (token == TokenNameMINUS_GREATER) {
3031             getNextToken();
3032             if (token != TokenNameIdentifier) {
3033               throwSyntaxError("String token expected.");
3034             }
3035             getNextToken();
3036           }
3037           //          if (token != TokenNameRBRACE) {
3038           //            throwSyntaxError("'}' expected after '{$'.");
3039           //          }
3040           //          // scanner.encapsedStringStack.pop();
3041           //          getNextToken();
3042         } else {
3043           expr();
3044           if (token != TokenNameRBRACE) {
3045             throwSyntaxError("'}' expected.");
3046           }
3047           //          scanner.encapsedStringStack.pop();
3048           getNextToken();
3049         }
3050         break;
3051     }
3052   }
3053   private void encaps_var_offset() {
3054     //                  T_STRING
3055     //          | T_NUM_STRING
3056     //          | T_VARIABLE
3057     switch (token) {
3058       case TokenNameSTRING :
3059         getNextToken();
3060         break;
3061       case TokenNameIntegerLiteral :
3062         getNextToken();
3063         break;
3064       case TokenNameVariable :
3065         getNextToken();
3066         break;
3067       case TokenNameIdentifier :
3068         getNextToken();
3069         break;
3070       default :
3071         throwSyntaxError("Variable or String token expected.");
3072         break;
3073     }
3074   }
3075   private void internal_functions_in_yacc() {
3076     int start = 0;
3077     ImportReference impt = null;
3078     switch (token) {
3079       case TokenNameisset :
3080         //      T_ISSET '(' isset_variables ')'
3081         getNextToken();
3082         if (token != TokenNameLPAREN) {
3083           throwSyntaxError("'(' expected after keyword 'isset'");
3084         }
3085         getNextToken();
3086         isset_variables();
3087         if (token != TokenNameRPAREN) {
3088           throwSyntaxError("')' expected after keyword 'isset'");
3089         }
3090         getNextToken();
3091         break;
3092       case TokenNameempty :
3093         //      T_EMPTY '(' variable ')'
3094         getNextToken();
3095         if (token != TokenNameLPAREN) {
3096           throwSyntaxError("'(' expected after keyword 'empty'");
3097         }
3098         getNextToken();
3099         variable();
3100         if (token != TokenNameRPAREN) {
3101           throwSyntaxError("')' expected after keyword 'empty'");
3102         }
3103         getNextToken();
3104         break;
3105       case TokenNameinclude :
3106         //T_INCLUDE expr
3107         start = scanner.getCurrentTokenStartPosition();
3108         getNextToken();
3109         expr();
3110
3111         impt = new ImportReference(scanner.getCurrentTokenSource(start), start, scanner.getCurrentTokenEndPosition(), false);
3112         impt.declarationSourceEnd = impt.sourceEnd;
3113         impt.declarationEnd = impt.declarationSourceEnd;
3114         //endPosition is just before the ;
3115         impt.declarationSourceStart = start;
3116         includesList.add(impt);
3117         break;
3118       case TokenNameinclude_once :
3119         //      T_INCLUDE_ONCE expr
3120         start = scanner.getCurrentTokenStartPosition();
3121         getNextToken();
3122         expr();
3123         impt = new ImportReference(scanner.getCurrentTokenSource(start), start, scanner.getCurrentTokenEndPosition(), false);
3124         impt.declarationSourceEnd = impt.sourceEnd;
3125         impt.declarationEnd = impt.declarationSourceEnd;
3126         //endPosition is just before the ;
3127         impt.declarationSourceStart = start;
3128         includesList.add(impt);
3129         break;
3130       case TokenNameeval :
3131         //      T_EVAL '(' expr ')'
3132         getNextToken();
3133         if (token != TokenNameLPAREN) {
3134           throwSyntaxError("'(' expected after keyword 'eval'");
3135         }
3136         getNextToken();
3137         expr();
3138         if (token != TokenNameRPAREN) {
3139           throwSyntaxError("')' expected after keyword 'eval'");
3140         }
3141         getNextToken();
3142         break;
3143       case TokenNamerequire :
3144         //T_REQUIRE expr
3145         start = scanner.getCurrentTokenStartPosition();
3146         getNextToken();
3147         expr();
3148         impt = new ImportReference(scanner.getCurrentTokenSource(start), start, scanner.getCurrentTokenEndPosition(), false);
3149         impt.declarationSourceEnd = impt.sourceEnd;
3150         impt.declarationEnd = impt.declarationSourceEnd;
3151         //endPosition is just before the ;
3152         impt.declarationSourceStart = start;
3153         includesList.add(impt);
3154         break;
3155       case TokenNamerequire_once :
3156         //      T_REQUIRE_ONCE expr
3157         start = scanner.getCurrentTokenStartPosition();
3158         getNextToken();
3159         expr();
3160         impt = new ImportReference(scanner.getCurrentTokenSource(start), start, scanner.getCurrentTokenEndPosition(), false);
3161         impt.declarationSourceEnd = impt.sourceEnd;
3162         impt.declarationEnd = impt.declarationSourceEnd;
3163         //endPosition is just before the ;
3164         impt.declarationSourceStart = start;
3165         includesList.add(impt);
3166         break;
3167     }
3168   }
3169   private void isset_variables() {
3170     //  variable
3171     //  | isset_variables ','
3172     if (token == TokenNameRPAREN) {
3173       throwSyntaxError("Variable expected after keyword 'isset'");
3174     }
3175     while (true) {
3176       variable();
3177       if (token == TokenNameCOMMA) {
3178         getNextToken();
3179       } else {
3180         break;
3181       }
3182     }
3183   }
3184   private boolean common_scalar() {
3185     //  common_scalar:
3186     //  T_LNUMBER
3187     //  | T_DNUMBER
3188     //  | T_CONSTANT_ENCAPSED_STRING
3189     //  | T_LINE
3190     //  | T_FILE
3191     //  | T_CLASS_C
3192     //  | T_METHOD_C
3193     //  | T_FUNC_C
3194     switch (token) {
3195       case TokenNameIntegerLiteral :
3196         getNextToken();
3197         return true;
3198       case TokenNameDoubleLiteral :
3199         getNextToken();
3200         return true;
3201       case TokenNameStringDoubleQuote :
3202         getNextToken();
3203         return true;
3204       case TokenNameStringSingleQuote :
3205         getNextToken();
3206         return true;
3207       case TokenNameStringInterpolated :
3208         getNextToken();
3209         return true;
3210       case TokenNameFILE :
3211         getNextToken();
3212         return true;
3213       case TokenNameLINE :
3214         getNextToken();
3215         return true;
3216       case TokenNameCLASS_C :
3217         getNextToken();
3218         return true;
3219       case TokenNameMETHOD_C :
3220         getNextToken();
3221         return true;
3222       case TokenNameFUNC_C :
3223         getNextToken();
3224         return true;
3225     }
3226     return false;
3227   }
3228   private void scalar() {
3229     //  scalar:
3230     //  T_STRING
3231     //| T_STRING_VARNAME
3232     //| class_constant
3233     //| common_scalar
3234     //| '"' encaps_list '"'
3235     //| '\'' encaps_list '\''
3236     //| T_START_HEREDOC encaps_list T_END_HEREDOC
3237     throwSyntaxError("Not yet implemented (scalar).");
3238   }
3239   private void static_scalar() {
3240     //    static_scalar: /* compile-time evaluated scalars */
3241     //          common_scalar
3242     //  | T_STRING
3243     //  | '+' static_scalar
3244     //  | '-' static_scalar
3245     //  | T_ARRAY '(' static_array_pair_list ')'
3246     //  | static_class_constant
3247     if (common_scalar()) {
3248       return;
3249     }
3250     switch (token) {
3251       case TokenNameIdentifier :
3252         getNextToken();
3253         //        static_class_constant:
3254         //              T_STRING T_PAAMAYIM_NEKUDOTAYIM T_STRING
3255         if (token == TokenNamePAAMAYIM_NEKUDOTAYIM) {
3256           getNextToken();
3257           if (token == TokenNameIdentifier) {
3258             getNextToken();
3259           } else {
3260             throwSyntaxError("Identifier expected after '::' operator.");
3261           }
3262         }
3263         break;
3264       case TokenNameEncapsedString0 :
3265         try {
3266           scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3267           while (scanner.currentCharacter != '`') {
3268             if (scanner.currentCharacter == '\\') {
3269               scanner.currentPosition++;
3270             }
3271             scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3272           }
3273           getNextToken();
3274         } catch (IndexOutOfBoundsException e) {
3275           throwSyntaxError("'`' expected at end of static string.");
3276         }
3277         break;
3278       case TokenNameEncapsedString1 :
3279         try {
3280           scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3281           while (scanner.currentCharacter != '\'') {
3282             if (scanner.currentCharacter == '\\') {
3283               scanner.currentPosition++;
3284             }
3285             scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3286           }
3287           getNextToken();
3288         } catch (IndexOutOfBoundsException e) {
3289           throwSyntaxError("'\'' expected at end of static string.");
3290         }
3291         break;
3292       case TokenNameEncapsedString2 :
3293         try {
3294           scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3295           while (scanner.currentCharacter != '"') {
3296             if (scanner.currentCharacter == '\\') {
3297               scanner.currentPosition++;
3298             }
3299             scanner.currentCharacter = scanner.source[scanner.currentPosition++];
3300           }
3301           getNextToken();
3302         } catch (IndexOutOfBoundsException e) {
3303           throwSyntaxError("'\"' expected at end of static string.");
3304         }
3305         break;
3306       case TokenNamePLUS :
3307         getNextToken();
3308         static_scalar();
3309         break;
3310       case TokenNameMINUS :
3311         getNextToken();
3312         static_scalar();
3313         break;
3314       case TokenNamearray :
3315         getNextToken();
3316         if (token != TokenNameLPAREN) {
3317           throwSyntaxError("'(' expected after keyword 'array'");
3318         }
3319         getNextToken();
3320         if (token == TokenNameRPAREN) {
3321           getNextToken();
3322           break;
3323         }
3324         non_empty_static_array_pair_list();
3325         if (token != TokenNameRPAREN) {
3326           throwSyntaxError("')' expected after keyword 'array'");
3327         }
3328         getNextToken();
3329         break;
3330       //      case TokenNamenull :
3331       //        getNextToken();
3332       //        break;
3333       //      case TokenNamefalse :
3334       //        getNextToken();
3335       //        break;
3336       //      case TokenNametrue :
3337       //        getNextToken();
3338       //        break;
3339       default :
3340         throwSyntaxError("Static scalar/constant expected.");
3341     }
3342   }
3343   private void non_empty_static_array_pair_list() {
3344     //  non_empty_static_array_pair_list:
3345     //  non_empty_static_array_pair_list ',' static_scalar T_DOUBLE_ARROW
3346     // static_scalar
3347     //| non_empty_static_array_pair_list ',' static_scalar
3348     //| static_scalar T_DOUBLE_ARROW static_scalar
3349     //| static_scalar
3350     while (true) {
3351       static_scalar();
3352       if (token == TokenNameEQUAL_GREATER) {
3353         getNextToken();
3354         static_scalar();
3355       }
3356       if (token != TokenNameCOMMA) {
3357         break;
3358       }
3359       getNextToken();
3360       if (token == TokenNameRPAREN) {
3361         break;
3362       }
3363     }
3364   }
3365   public void reportSyntaxError() { //int act, int currentKind, int
3366     // stateStackTop) {
3367     /* remember current scanner position */
3368     int startPos = scanner.startPosition;
3369     int currentPos = scanner.currentPosition;
3370     //          String[] expectings;
3371     //          String tokenName = name[symbol_index[currentKind]];
3372     //fetch all "accurate" possible terminals that could recover the error
3373     //          int start, end = start = asi(stack[stateStackTop]);
3374     //          while (asr[end] != 0)
3375     //                  end++;
3376     //          int length = end - start;
3377     //          expectings = new String[length];
3378     //          if (length != 0) {
3379     //                  char[] indexes = new char[length];
3380     //                  System.arraycopy(asr, start, indexes, 0, length);
3381     //                  for (int i = 0; i < length; i++) {
3382     //                          expectings[i] = name[symbol_index[indexes[i]]];
3383     //                  }
3384     //          }
3385     //if the pb is an EOF, try to tell the user that they are some
3386     //          if (tokenName.equals(UNEXPECTED_EOF)) {
3387     //                  if (!this.checkAndReportBracketAnomalies(problemReporter())) {
3388     //                          char[] tokenSource;
3389     //                          try {
3390     //                                  tokenSource = this.scanner.getCurrentTokenSource();
3391     //                          } catch (Exception e) {
3392     //                                  tokenSource = new char[] {};
3393     //                          }
3394     //                          problemReporter().parseError(
3395     //                                  this.scanner.startPosition,
3396     //                                  this.scanner.currentPosition - 1,
3397     //                                  tokenSource,
3398     //                                  tokenName,
3399     //                                  expectings);
3400     //                  }
3401     //          } else { //the next test is HEAVILY grammar DEPENDENT.
3402     //                  if ((length == 14)
3403     //                          && (expectings[0] == "=") //$NON-NLS-1$
3404     //                          && (expectings[1] == "*=") //$NON-NLS-1$
3405     //                          && (expressionPtr > -1)) {
3406     //                                  switch(currentKind) {
3407     //                                          case TokenNameSEMICOLON:
3408     //                                          case TokenNamePLUS:
3409     //                                          case TokenNameMINUS:
3410     //                                          case TokenNameDIVIDE:
3411     //                                          case TokenNameREMAINDER:
3412     //                                          case TokenNameMULTIPLY:
3413     //                                          case TokenNameLEFT_SHIFT:
3414     //                                          case TokenNameRIGHT_SHIFT:
3415     //// case TokenNameUNSIGNED_RIGHT_SHIFT:
3416     //                                          case TokenNameLESS:
3417     //                                          case TokenNameGREATER:
3418     //                                          case TokenNameLESS_EQUAL:
3419     //                                          case TokenNameGREATER_EQUAL:
3420     //                                          case TokenNameEQUAL_EQUAL:
3421     //                                          case TokenNameNOT_EQUAL:
3422     //                                          case TokenNameXOR:
3423     //                                          case TokenNameAND:
3424     //                                          case TokenNameOR:
3425     //                                          case TokenNameOR_OR:
3426     //                                          case TokenNameAND_AND:
3427     //                                                  // the ; is not the expected token ==> it ends a statement when an
3428     // expression is not ended
3429     //                                                  problemReporter().invalidExpressionAsStatement(expressionStack[expressionPtr]);
3430     //                                                  break;
3431     //                                          case TokenNameRBRACE :
3432     //                                                  problemReporter().missingSemiColon(expressionStack[expressionPtr]);
3433     //                                                  break;
3434     //                                          default:
3435     //                                                  char[] tokenSource;
3436     //                                                  try {
3437     //                                                          tokenSource = this.scanner.getCurrentTokenSource();
3438     //                                                  } catch (Exception e) {
3439     //                                                          tokenSource = new char[] {};
3440     //                                                  }
3441     //                                                  problemReporter().parseError(
3442     //                                                          this.scanner.startPosition,
3443     //                                                          this.scanner.currentPosition - 1,
3444     //                                                          tokenSource,
3445     //                                                          tokenName,
3446     //                                                          expectings);
3447     //                                                  this.checkAndReportBracketAnomalies(problemReporter());
3448     //                                  }
3449     //                  } else {
3450     char[] tokenSource;
3451     try {
3452       tokenSource = this.scanner.getCurrentTokenSource();
3453     } catch (Exception e) {
3454       tokenSource = new char[]{};
3455     }
3456     //                          problemReporter().parseError(
3457     //                                  this.scanner.startPosition,
3458     //                                  this.scanner.currentPosition - 1,
3459     //                                  tokenSource,
3460     //                                  tokenName,
3461     //                                  expectings);
3462     this.checkAndReportBracketAnomalies(problemReporter());
3463     //                  }
3464     //          }
3465     /* reset scanner where it was */
3466     scanner.startPosition = startPos;
3467     scanner.currentPosition = currentPos;
3468   }
3469   public static final int RoundBracket = 0;
3470   public static final int SquareBracket = 1;
3471   public static final int CurlyBracket = 2;
3472   public static final int BracketKinds = 3;
3473   protected int[] nestedMethod; //the ptr is nestedType
3474   protected int nestedType, dimensions;
3475   //ast stack
3476   final static int AstStackIncrement = 100;
3477   protected int astPtr;
3478   protected AstNode[] astStack = new AstNode[AstStackIncrement];
3479   protected int astLengthPtr;
3480   protected int[] astLengthStack;
3481   AstNode[] noAstNodes = new AstNode[AstStackIncrement];
3482   public CompilationUnitDeclaration compilationUnit; /*
3483                                                       * the result from parse()
3484                                                       */
3485   protected ReferenceContext referenceContext;
3486   protected ProblemReporter problemReporter;
3487   protected CompilerOptions options;
3488   private ArrayList includesList;
3489   //  protected CompilationResult compilationResult;
3490   /**
3491    * Returns this parser's problem reporter initialized with its reference
3492    * context. Also it is assumed that a problem is going to be reported, so
3493    * initializes the compilation result's line positions.
3494    */
3495   public ProblemReporter problemReporter() {
3496     if (scanner.recordLineSeparator) {
3497       compilationUnit.compilationResult.lineSeparatorPositions = scanner.getLineEnds();
3498     }
3499     problemReporter.referenceContext = referenceContext;
3500     return problemReporter;
3501   }
3502   /*
3503    * Reconsider the entire source looking for inconsistencies in {} () []
3504    */
3505   public boolean checkAndReportBracketAnomalies(ProblemReporter problemReporter) {
3506     scanner.wasAcr = false;
3507     boolean anomaliesDetected = false;
3508     try {
3509       char[] source = scanner.source;
3510       int[] leftCount = {0, 0, 0};
3511       int[] rightCount = {0, 0, 0};
3512       int[] depths = {0, 0, 0};
3513       int[][] leftPositions = new int[][]{new int[10], new int[10], new int[10]};
3514       int[][] leftDepths = new int[][]{new int[10], new int[10], new int[10]};
3515       int[][] rightPositions = new int[][]{new int[10], new int[10], new int[10]};
3516       int[][] rightDepths = new int[][]{new int[10], new int[10], new int[10]};
3517       scanner.currentPosition = scanner.initialPosition; //starting
3518       // point
3519       // (first-zero-based
3520       // char)
3521       while (scanner.currentPosition < scanner.eofPosition) { //loop for
3522         // jumping
3523         // over
3524         // comments
3525         try {
3526           // ---------Consume white space and handles
3527           // startPosition---------
3528           boolean isWhiteSpace;
3529           do {
3530             scanner.startPosition = scanner.currentPosition;
3531             //                                          if (((scanner.currentCharacter =
3532             // source[scanner.currentPosition++]) == '\\') &&
3533             // (source[scanner.currentPosition] == 'u')) {
3534             //                                                  isWhiteSpace = scanner.jumpOverUnicodeWhiteSpace();
3535             //                                          } else {
3536             if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
3537               if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
3538                 // only record line positions we have not
3539                 // recorded yet
3540                 scanner.pushLineSeparator();
3541               }
3542             }
3543             isWhiteSpace = CharOperation.isWhitespace(scanner.currentCharacter);
3544             //                                          }
3545           } while (isWhiteSpace && (scanner.currentPosition < scanner.eofPosition));
3546           // -------consume token until } is found---------
3547           switch (scanner.currentCharacter) {
3548             case '{' : {
3549               int index = leftCount[CurlyBracket]++;
3550               if (index == leftPositions[CurlyBracket].length) {
3551                 System.arraycopy(leftPositions[CurlyBracket], 0, (leftPositions[CurlyBracket] = new int[index * 2]), 0, index);
3552                 System.arraycopy(leftDepths[CurlyBracket], 0, (leftDepths[CurlyBracket] = new int[index * 2]), 0, index);
3553               }
3554               leftPositions[CurlyBracket][index] = scanner.startPosition;
3555               leftDepths[CurlyBracket][index] = depths[CurlyBracket]++;
3556             }
3557               break;
3558             case '}' : {
3559               int index = rightCount[CurlyBracket]++;
3560               if (index == rightPositions[CurlyBracket].length) {
3561                 System.arraycopy(rightPositions[CurlyBracket], 0, (rightPositions[CurlyBracket] = new int[index * 2]), 0, index);
3562                 System.arraycopy(rightDepths[CurlyBracket], 0, (rightDepths[CurlyBracket] = new int[index * 2]), 0, index);
3563               }
3564               rightPositions[CurlyBracket][index] = scanner.startPosition;
3565               rightDepths[CurlyBracket][index] = --depths[CurlyBracket];
3566             }
3567               break;
3568             case '(' : {
3569               int index = leftCount[RoundBracket]++;
3570               if (index == leftPositions[RoundBracket].length) {
3571                 System.arraycopy(leftPositions[RoundBracket], 0, (leftPositions[RoundBracket] = new int[index * 2]), 0, index);
3572                 System.arraycopy(leftDepths[RoundBracket], 0, (leftDepths[RoundBracket] = new int[index * 2]), 0, index);
3573               }
3574               leftPositions[RoundBracket][index] = scanner.startPosition;
3575               leftDepths[RoundBracket][index] = depths[RoundBracket]++;
3576             }
3577               break;
3578             case ')' : {
3579               int index = rightCount[RoundBracket]++;
3580               if (index == rightPositions[RoundBracket].length) {
3581                 System.arraycopy(rightPositions[RoundBracket], 0, (rightPositions[RoundBracket] = new int[index * 2]), 0, index);
3582                 System.arraycopy(rightDepths[RoundBracket], 0, (rightDepths[RoundBracket] = new int[index * 2]), 0, index);
3583               }
3584               rightPositions[RoundBracket][index] = scanner.startPosition;
3585               rightDepths[RoundBracket][index] = --depths[RoundBracket];
3586             }
3587               break;
3588             case '[' : {
3589               int index = leftCount[SquareBracket]++;
3590               if (index == leftPositions[SquareBracket].length) {
3591                 System.arraycopy(leftPositions[SquareBracket], 0, (leftPositions[SquareBracket] = new int[index * 2]), 0, index);
3592                 System.arraycopy(leftDepths[SquareBracket], 0, (leftDepths[SquareBracket] = new int[index * 2]), 0, index);
3593               }
3594               leftPositions[SquareBracket][index] = scanner.startPosition;
3595               leftDepths[SquareBracket][index] = depths[SquareBracket]++;
3596             }
3597               break;
3598             case ']' : {
3599               int index = rightCount[SquareBracket]++;
3600               if (index == rightPositions[SquareBracket].length) {
3601                 System.arraycopy(rightPositions[SquareBracket], 0, (rightPositions[SquareBracket] = new int[index * 2]), 0, index);
3602                 System.arraycopy(rightDepths[SquareBracket], 0, (rightDepths[SquareBracket] = new int[index * 2]), 0, index);
3603               }
3604               rightPositions[SquareBracket][index] = scanner.startPosition;
3605               rightDepths[SquareBracket][index] = --depths[SquareBracket];
3606             }
3607               break;
3608             case '\'' : {
3609               if (scanner.getNextChar('\\')) {
3610                 scanner.scanEscapeCharacter();
3611               } else { // consume next character
3612                 scanner.unicodeAsBackSlash = false;
3613                 //                                                                      if (((scanner.currentCharacter =
3614                 // source[scanner.currentPosition++]) ==
3615                 // '\\') &&
3616                 // (source[scanner.currentPosition] ==
3617                 // 'u')) {
3618                 //                                                                              scanner.getNextUnicodeChar();
3619                 //                                                                      } else {
3620                 if (scanner.withoutUnicodePtr != 0) {
3621                   scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3622                 }
3623                 //                                                                      }
3624               }
3625               scanner.getNextChar('\'');
3626               break;
3627             }
3628             case '"' :
3629               // consume next character
3630               scanner.unicodeAsBackSlash = false;
3631               //                                                        if (((scanner.currentCharacter =
3632               // source[scanner.currentPosition++]) == '\\') &&
3633               // (source[scanner.currentPosition] == 'u')) {
3634               //                                                                scanner.getNextUnicodeChar();
3635               //                                                        } else {
3636               if (scanner.withoutUnicodePtr != 0) {
3637                 scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3638               }
3639               //                                                        }
3640               while (scanner.currentCharacter != '"') {
3641                 if (scanner.currentCharacter == '\r') {
3642                   if (source[scanner.currentPosition] == '\n')
3643                     scanner.currentPosition++;
3644                   break; // the string cannot go further that
3645                   // the line
3646                 }
3647                 if (scanner.currentCharacter == '\n') {
3648                   break; // the string cannot go further that
3649                   // the line
3650                 }
3651                 if (scanner.currentCharacter == '\\') {
3652                   scanner.scanEscapeCharacter();
3653                 }
3654                 // consume next character
3655                 scanner.unicodeAsBackSlash = false;
3656                 //                                                              if (((scanner.currentCharacter =
3657                 // source[scanner.currentPosition++]) == '\\')
3658                 // && (source[scanner.currentPosition] == 'u'))
3659                 // {
3660                 //                                                                      scanner.getNextUnicodeChar();
3661                 //                                                              } else {
3662                 if (scanner.withoutUnicodePtr != 0) {
3663                   scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3664                 }
3665                 //                                                              }
3666               }
3667               break;
3668             case '/' : {
3669               int test;
3670               if ((test = scanner.getNextChar('/', '*')) == 0) { //line
3671                 // comment
3672                 //get the next char
3673                 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3674                     && (source[scanner.currentPosition] == 'u')) {
3675                   //-------------unicode traitement
3676                   // ------------
3677                   int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3678                   scanner.currentPosition++;
3679                   while (source[scanner.currentPosition] == 'u') {
3680                     scanner.currentPosition++;
3681                   }
3682                   if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3683                       || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3684                       || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3685                       || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3686                                                                                                                // don't
3687                     // care of the
3688                     // value
3689                     scanner.currentCharacter = 'A';
3690                   } //something different from \n and \r
3691                   else {
3692                     scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3693                   }
3694                 }
3695                 while (scanner.currentCharacter != '\r' && scanner.currentCharacter != '\n') {
3696                   //get the next char
3697                   scanner.startPosition = scanner.currentPosition;
3698                   if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3699                       && (source[scanner.currentPosition] == 'u')) {
3700                     //-------------unicode traitement
3701                     // ------------
3702                     int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3703                     scanner.currentPosition++;
3704                     while (source[scanner.currentPosition] == 'u') {
3705                       scanner.currentPosition++;
3706                     }
3707                     if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3708                         || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3709                         || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3710                         || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3711                                                                                                                  // don't
3712                       // care of the
3713                       // value
3714                       scanner.currentCharacter = 'A';
3715                     } //something different from \n
3716                     // and \r
3717                     else {
3718                       scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3719                     }
3720                   }
3721                 }
3722                 if (scanner.recordLineSeparator && ((scanner.currentCharacter == '\r') || (scanner.currentCharacter == '\n'))) {
3723                   if (scanner.lineEnds[scanner.linePtr] < scanner.startPosition) {
3724                     // only record line positions we
3725                     // have not recorded yet
3726                     scanner.pushLineSeparator();
3727                     if (this.scanner.taskTags != null) {
3728                       this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner
3729                           .getCurrentTokenEndPosition());
3730                     }
3731                   }
3732                 }
3733                 break;
3734               }
3735               if (test > 0) { //traditional and annotation
3736                 // comment
3737                 boolean star = false;
3738                 // consume next character
3739                 scanner.unicodeAsBackSlash = false;
3740                 //                                                                      if (((scanner.currentCharacter =
3741                 // source[scanner.currentPosition++]) ==
3742                 // '\\') &&
3743                 // (source[scanner.currentPosition] ==
3744                 // 'u')) {
3745                 //                                                                              scanner.getNextUnicodeChar();
3746                 //                                                                      } else {
3747                 if (scanner.withoutUnicodePtr != 0) {
3748                   scanner.withoutUnicodeBuffer[++scanner.withoutUnicodePtr] = scanner.currentCharacter;
3749                 }
3750                 //                                                                      }
3751                 if (scanner.currentCharacter == '*') {
3752                   star = true;
3753                 }
3754                 //get the next char
3755                 if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3756                     && (source[scanner.currentPosition] == 'u')) {
3757                   //-------------unicode traitement
3758                   // ------------
3759                   int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3760                   scanner.currentPosition++;
3761                   while (source[scanner.currentPosition] == 'u') {
3762                     scanner.currentPosition++;
3763                   }
3764                   if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3765                       || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3766                       || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3767                       || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3768                                                                                                                // don't
3769                     // care of the
3770                     // value
3771                     scanner.currentCharacter = 'A';
3772                   } //something different from * and /
3773                   else {
3774                     scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3775                   }
3776                 }
3777                 //loop until end of comment */
3778                 while ((scanner.currentCharacter != '/') || (!star)) {
3779                   star = scanner.currentCharacter == '*';
3780                   //get next char
3781                   if (((scanner.currentCharacter = source[scanner.currentPosition++]) == '\\')
3782                       && (source[scanner.currentPosition] == 'u')) {
3783                     //-------------unicode traitement
3784                     // ------------
3785                     int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
3786                     scanner.currentPosition++;
3787                     while (source[scanner.currentPosition] == 'u') {
3788                       scanner.currentPosition++;
3789                     }
3790                     if ((c1 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c1 < 0
3791                         || (c2 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c2 < 0
3792                         || (c3 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c3 < 0
3793                         || (c4 = Character.getNumericValue(source[scanner.currentPosition++])) > 15 || c4 < 0) { //error
3794                                                                                                                  // don't
3795                       // care of the
3796                       // value
3797                       scanner.currentCharacter = 'A';
3798                     } //something different from * and
3799                     // /
3800                     else {
3801                       scanner.currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
3802                     }
3803                   }
3804                 }
3805                 if (this.scanner.taskTags != null) {
3806                   this.scanner.checkTaskTag(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
3807                 }
3808                 break;
3809               }
3810               break;
3811             }
3812             default :
3813               if (Scanner.isPHPIdentifierStart(scanner.currentCharacter)) {
3814                 scanner.scanIdentifierOrKeyword(false);
3815                 break;
3816               }
3817               if (Character.isDigit(scanner.currentCharacter)) {
3818                 scanner.scanNumber(false);
3819                 break;
3820               }
3821           }
3822           //-----------------end switch while
3823           // try--------------------
3824         } catch (IndexOutOfBoundsException e) {
3825           break; // read until EOF
3826         } catch (InvalidInputException e) {
3827           return false; // no clue
3828         }
3829       }
3830       if (scanner.recordLineSeparator) {
3831         //                              compilationUnit.compilationResult.lineSeparatorPositions =
3832         // scanner.getLineEnds();
3833       }
3834       // check placement anomalies against other kinds of brackets
3835       for (int kind = 0; kind < BracketKinds; kind++) {
3836         for (int leftIndex = leftCount[kind] - 1; leftIndex >= 0; leftIndex--) {
3837           int start = leftPositions[kind][leftIndex]; // deepest
3838           // first
3839           // find matching closing bracket
3840           int depth = leftDepths[kind][leftIndex];
3841           int end = -1;
3842           for (int i = 0; i < rightCount[kind]; i++) {
3843             int pos = rightPositions[kind][i];
3844             // want matching bracket further in source with same
3845             // depth
3846             if ((pos > start) && (depth == rightDepths[kind][i])) {
3847               end = pos;
3848               break;
3849             }
3850           }
3851           if (end < 0) { // did not find a good closing match
3852             problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult);
3853             return true;
3854           }
3855           // check if even number of opening/closing other brackets
3856           // in between this pair of brackets
3857           int balance = 0;
3858           for (int otherKind = 0; (balance == 0) && (otherKind < BracketKinds); otherKind++) {
3859             for (int i = 0; i < leftCount[otherKind]; i++) {
3860               int pos = leftPositions[otherKind][i];
3861               if ((pos > start) && (pos < end))
3862                 balance++;
3863             }
3864             for (int i = 0; i < rightCount[otherKind]; i++) {
3865               int pos = rightPositions[otherKind][i];
3866               if ((pos > start) && (pos < end))
3867                 balance--;
3868             }
3869             if (balance != 0) {
3870               problemReporter.unmatchedBracket(start, referenceContext, compilationUnit.compilationResult); //bracket
3871               // anomaly
3872               return true;
3873             }
3874           }
3875         }
3876         // too many opening brackets ?
3877         for (int i = rightCount[kind]; i < leftCount[kind]; i++) {
3878           anomaliesDetected = true;
3879           problemReporter.unmatchedBracket(leftPositions[kind][leftCount[kind] - i - 1], referenceContext,
3880               compilationUnit.compilationResult);
3881         }
3882         // too many closing brackets ?
3883         for (int i = leftCount[kind]; i < rightCount[kind]; i++) {
3884           anomaliesDetected = true;
3885           problemReporter.unmatchedBracket(rightPositions[kind][i], referenceContext, compilationUnit.compilationResult);
3886         }
3887         if (anomaliesDetected)
3888           return true;
3889       }
3890       return anomaliesDetected;
3891     } catch (ArrayStoreException e) { // jdk1.2.2 jit bug
3892       return anomaliesDetected;
3893     } catch (NullPointerException e) { // jdk1.2.2 jit bug
3894       return anomaliesDetected;
3895     }
3896   }
3897   protected void pushOnAstLengthStack(int pos) {
3898     try {
3899       astLengthStack[++astLengthPtr] = pos;
3900     } catch (IndexOutOfBoundsException e) {
3901       int oldStackLength = astLengthStack.length;
3902       int[] oldPos = astLengthStack;
3903       astLengthStack = new int[oldStackLength + StackIncrement];
3904       System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
3905       astLengthStack[astLengthPtr] = pos;
3906     }
3907   }
3908   protected void pushOnAstStack(AstNode node) {
3909     /*
3910      * add a new obj on top of the ast stack
3911      */
3912     try {
3913       astStack[++astPtr] = node;
3914     } catch (IndexOutOfBoundsException e) {
3915       int oldStackLength = astStack.length;
3916       AstNode[] oldStack = astStack;
3917       astStack = new AstNode[oldStackLength + AstStackIncrement];
3918       System.arraycopy(oldStack, 0, astStack, 0, oldStackLength);
3919       astPtr = oldStackLength;
3920       astStack[astPtr] = node;
3921     }
3922     try {
3923       astLengthStack[++astLengthPtr] = 1;
3924     } catch (IndexOutOfBoundsException e) {
3925       int oldStackLength = astLengthStack.length;
3926       int[] oldPos = astLengthStack;
3927       astLengthStack = new int[oldStackLength + AstStackIncrement];
3928       System.arraycopy(oldPos, 0, astLengthStack, 0, oldStackLength);
3929       astLengthStack[astLengthPtr] = 1;
3930     }
3931   }
3932
3933   protected void resetModifiers() {
3934     this.modifiers = AccDefault;
3935     this.modifiersSourceStart = -1; // <-- see comment into
3936     // modifiersFlag(int)
3937     this.scanner.commentPtr = -1;
3938   }
3939 }