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