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