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