0637cf1a4d1a0fc0b832e54073c7f1c71bb8394d
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParser.jj
1
2 options {
3   LOOKAHEAD = 1;
4   CHOICE_AMBIGUITY_CHECK = 2;
5   OTHER_AMBIGUITY_CHECK = 1;
6   STATIC = true;
7   DEBUG_PARSER = false;
8   DEBUG_LOOKAHEAD = false;
9   DEBUG_TOKEN_MANAGER = false;
10   OPTIMIZE_TOKEN_MANAGER = false;
11   ERROR_REPORTING = true;
12   JAVA_UNICODE_ESCAPE = false;
13   UNICODE_INPUT = false;
14   IGNORE_CASE = true;
15   USER_TOKEN_MANAGER = false;
16   USER_CHAR_STREAM = false;
17   BUILD_PARSER = true;
18   BUILD_TOKEN_MANAGER = true;
19   SANITY_CHECK = true;
20   FORCE_LA_CHECK = false;
21   COMMON_TOKEN_ACTION = true;
22 }
23
24 PARSER_BEGIN(PHPParser)
25 package test;
26
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.core.resources.IMarker;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.ui.texteditor.MarkerUtilities;
31 import org.eclipse.jface.preference.IPreferenceStore;
32
33 import java.util.Hashtable;
34 import java.util.ArrayList;
35 import java.io.StringReader;
36 import java.io.*;
37 import java.text.MessageFormat;
38
39 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
40 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
41 import net.sourceforge.phpdt.internal.compiler.ast.*;
42 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
43 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
44 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
45 import net.sourceforge.phpdt.internal.corext.Assert;
46
47 /**
48  * A new php parser.
49  * This php parser is inspired by the Java 1.2 grammar example
50  * given with JavaCC. You can get JavaCC at http://www.webgain.com
51  * You can test the parser with the PHPParserTestCase2.java
52  * @author Matthieu Casanova
53  */
54 public final class PHPParser extends PHPParserSuperclass {
55
56 //todo : fix the variables names bug
57 //todo : handle tilde operator
58
59
60   /** The current segment. */
61   private static OutlineableWithChildren currentSegment;
62
63   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
64   private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
65   static PHPOutlineInfo outlineInfo;
66
67   /** The error level of the current ParseException. */
68   private static int errorLevel = ERROR;
69   /** The message of the current ParseException. If it's null it's because the parse exception wasn't handled */
70   private static String errorMessage;
71
72   private static int errorStart = -1;
73   private static int errorEnd = -1;
74   private static PHPDocument phpDocument;
75
76   private static final String SYNTAX_ERROR_CHAR = "syntax error";
77   /**
78    * The point where html starts.
79    * It will be used by the token manager to create HTMLCode objects
80    */
81   public static int htmlStart;
82
83   //ast stack
84   private final static int AstStackIncrement = 100;
85   /** The stack of node. */
86   private static AstNode[] nodes;
87   /** The cursor in expression stack. */
88   private static int nodePtr;
89
90   public static final boolean PARSER_DEBUG = false;
91
92   public final void setFileToParse(final IFile fileToParse) {
93     PHPParser.fileToParse = fileToParse;
94   }
95
96   public PHPParser() {
97   }
98
99   public PHPParser(final IFile fileToParse) {
100     this(new StringReader(""));
101     PHPParser.fileToParse = fileToParse;
102   }
103
104   public static final void phpParserTester(final String strEval) throws ParseException {
105     final StringReader stream = new StringReader(strEval);
106     if (jj_input_stream == null) {
107       jj_input_stream = new SimpleCharStream(stream, 1, 1);
108     }
109     ReInit(new StringReader(strEval));
110     init();
111     phpDocument = new PHPDocument(null,"_root".toCharArray());
112     currentSegment = phpDocument;
113     outlineInfo = new PHPOutlineInfo(null, currentSegment);
114     PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING);
115     phpTest();
116   }
117
118   public static final void htmlParserTester(final File fileName) throws FileNotFoundException, ParseException {
119     final Reader stream = new FileReader(fileName);
120     if (jj_input_stream == null) {
121       jj_input_stream = new SimpleCharStream(stream, 1, 1);
122     }
123     ReInit(stream);
124     init();
125     phpDocument = new PHPDocument(null,"_root".toCharArray());
126     currentSegment = phpDocument;
127     outlineInfo = new PHPOutlineInfo(null, currentSegment);
128     phpFile();
129   }
130
131   public static final void htmlParserTester(final String strEval) throws ParseException {
132     final StringReader stream = new StringReader(strEval);
133     if (jj_input_stream == null) {
134       jj_input_stream = new SimpleCharStream(stream, 1, 1);
135     }
136     ReInit(stream);
137     init();
138     phpDocument = new PHPDocument(null,"_root".toCharArray());
139     currentSegment = phpDocument;
140     outlineInfo = new PHPOutlineInfo(null, currentSegment);
141     phpFile();
142   }
143
144   /**
145    * Reinitialize the parser.
146    */
147   private static final void init() {
148     nodes = new AstNode[AstStackIncrement];
149     nodePtr = -1;
150     htmlStart = 0;
151   }
152
153   /**
154    * Add an php node on the stack.
155    * @param node the node that will be added to the stack
156    */
157   private static final void pushOnAstNodes(final AstNode node) {
158     try {
159       nodes[++nodePtr] = node;
160     } catch (IndexOutOfBoundsException e) {
161       final int oldStackLength = nodes.length;
162       final AstNode[] oldStack = nodes;
163       nodes = new AstNode[oldStackLength + AstStackIncrement];
164       System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
165       nodePtr = oldStackLength;
166       nodes[nodePtr] = node;
167     }
168   }
169
170   public final PHPOutlineInfo parseInfo(final Object parent, final String s) {
171     phpDocument = new PHPDocument(parent,"_root".toCharArray());
172     currentSegment = phpDocument;
173     outlineInfo = new PHPOutlineInfo(parent, currentSegment);
174     final StringReader stream = new StringReader(s);
175     if (jj_input_stream == null) {
176       jj_input_stream = new SimpleCharStream(stream, 1, 1);
177     }
178     ReInit(stream);
179     init();
180     try {
181       parse();
182       phpDocument.nodes = new AstNode[nodes.length];
183       System.arraycopy(nodes,0,phpDocument.nodes,0,nodes.length);
184       if (PHPeclipsePlugin.DEBUG) {
185         PHPeclipsePlugin.log(1,phpDocument.toString());
186       }
187     } catch (ParseException e) {
188       processParseException(e);
189     }
190     return outlineInfo;
191   }
192
193   /**
194    * This function will throw the exception if we are in debug mode
195    * and process it if we are in production mode.
196    * this should be fast since the PARSER_DEBUG is static final so the difference will be at compile time
197    * @param e the exception
198    * @throws ParseException the thrown exception
199    */
200   private static void processParseExceptionDebug(final ParseException e) throws ParseException {
201     if (PARSER_DEBUG) {
202       throw e;
203     }
204     processParseException(e);
205   }
206   /**
207    * This method will process the parse exception.
208    * If the error message is null, the parse exception wasn't catched and a trace is written in the log
209    * @param e the ParseException
210    */
211   private static void processParseException(final ParseException e) {
212     if (errorMessage == null) {
213       PHPeclipsePlugin.log(e);
214       errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it";
215       errorStart = e.currentToken.sourceStart;
216       errorEnd   = e.currentToken.sourceEnd;
217     }
218     setMarker(e);
219     errorMessage = null;
220   //  if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e);
221   }
222
223   /**
224    * Create marker for the parse error.
225    * @param e the ParseException
226    */
227   private static void setMarker(final ParseException e) {
228     try {
229       if (errorStart == -1) {
230         setMarker(fileToParse,
231                   errorMessage,
232                   e.currentToken.sourceStart,
233                   e.currentToken.sourceEnd,
234                   errorLevel,
235                   "Line " + e.currentToken.beginLine+", "+e.currentToken.sourceStart+":"+e.currentToken.sourceEnd);
236       } else {
237         setMarker(fileToParse,
238                   errorMessage,
239                   errorStart,
240                   errorEnd,
241                   errorLevel,
242                   "Line " + e.currentToken.beginLine+", "+errorStart+":"+errorEnd);
243         errorStart = -1;
244         errorEnd = -1;
245       }
246     } catch (CoreException e2) {
247       PHPeclipsePlugin.log(e2);
248     }
249   }
250
251   private static void scanLine(final String output,
252                                final IFile file,
253                                final int indx,
254                                final int brIndx) throws CoreException {
255     String current;
256     final StringBuffer lineNumberBuffer = new StringBuffer(10);
257     char ch;
258     current = output.substring(indx, brIndx);
259
260     if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
261       final int onLine = current.indexOf("on line <b>");
262       if (onLine != -1) {
263         lineNumberBuffer.delete(0, lineNumberBuffer.length());
264         for (int i = onLine; i < current.length(); i++) {
265           ch = current.charAt(i);
266           if ('0' <= ch && '9' >= ch) {
267             lineNumberBuffer.append(ch);
268           }
269         }
270
271         final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
272
273         final Hashtable attributes = new Hashtable();
274
275         current = current.replaceAll("\n", "");
276         current = current.replaceAll("<b>", "");
277         current = current.replaceAll("</b>", "");
278         MarkerUtilities.setMessage(attributes, current);
279
280         if (current.indexOf(PARSE_ERROR_STRING) != -1)
281           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
282         else if (current.indexOf(PARSE_WARNING_STRING) != -1)
283           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
284         else
285           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
286         MarkerUtilities.setLineNumber(attributes, lineNumber);
287         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
288       }
289     }
290   }
291
292   public final void parse(final String s) {
293     final StringReader stream = new StringReader(s);
294     if (jj_input_stream == null) {
295       jj_input_stream = new SimpleCharStream(stream, 1, 1);
296     }
297     ReInit(stream);
298     init();
299     try {
300       parse();
301     } catch (ParseException e) {
302       processParseException(e);
303     }
304   }
305
306   /**
307    * Call the php parse command ( php -l -f &lt;filename&gt; )
308    * and create markers according to the external parser output
309    */
310   public static void phpExternalParse(final IFile file) {
311     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
312     final String filename = file.getLocation().toString();
313
314     final String[] arguments = { filename };
315     final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
316     final String command = form.format(arguments);
317
318     final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
319
320     try {
321       // parse the buffer to find the errors and warnings
322       createMarkers(parserResult, file);
323     } catch (CoreException e) {
324       PHPeclipsePlugin.log(e);
325     }
326   }
327
328   /**
329    * Put a new html block in the stack.
330    */
331   public static final void createNewHTMLCode() {
332     final int currentPosition = token.sourceStart;
333     if (currentPosition == htmlStart ||
334           currentPosition < htmlStart ||
335           currentPosition > SimpleCharStream.currentBuffer.length()) {
336       return;
337     }
338     final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,
339                                                                   currentPosition).toCharArray();
340     pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition));
341   }
342
343   /** Create a new task. */
344   public static final void createNewTask(final int todoStart) {
345     final String  todo = SimpleCharStream.currentBuffer.substring(todoStart,
346                                                                   SimpleCharStream.currentBuffer.indexOf("\n",
347                                                                                                          todoStart)-1);
348     if (!PARSER_DEBUG) {
349       try {
350         setMarker(fileToParse,
351                   todo,
352                   SimpleCharStream.getBeginLine(),
353                   TASK,
354                   "Line "+SimpleCharStream.getBeginLine());
355       } catch (CoreException e) {
356         PHPeclipsePlugin.log(e);
357       }
358     }
359   }
360
361   private static final void parse() throws ParseException {
362           phpFile();
363   }
364 }
365
366 PARSER_END(PHPParser)
367
368 TOKEN_MGR_DECLS:
369 {
370   // CommonTokenAction: use the begins/ends fields added to the Jack
371   // CharStream class to set corresponding fields in each Token (which was
372   // also extended with new fields). By default Jack doesn't supply absolute
373   // offsets, just line/column offsets
374   static void CommonTokenAction(Token t) {
375     t.sourceStart = input_stream.beginOffset;
376     t.sourceEnd = input_stream.endOffset;
377   } // CommonTokenAction
378 } // TOKEN_MGR_DECLS
379
380 <DEFAULT> TOKEN :
381 {
382   <PHPSTARTSHORT : "<?">    : PHPPARSING
383 | <PHPSTARTLONG  : "<?php"> : PHPPARSING
384 | <PHPECHOSTART  : "<?=">   : PHPPARSING
385 }
386
387 <PHPPARSING, IN_SINGLE_LINE_COMMENT,IN_VARIABLE> TOKEN :
388 {
389   <PHPEND :"?>"> : DEFAULT
390 }
391
392 /* Skip any character if we are not in php mode */
393 <DEFAULT> SKIP :
394 {
395  < ~[] >
396 }
397
398
399 /* WHITE SPACE */
400 <PHPPARSING> SKIP :
401 {
402   " "
403 | "\t"
404 | "\n"
405 | "\r"
406 | "\f"
407 }
408
409 <IN_VARIABLE> SPECIAL_TOKEN :
410 {
411   " " : PHPPARSING
412 | "\t" : PHPPARSING
413 | "\n" : PHPPARSING
414 | "\r" : PHPPARSING
415 | "\f" : PHPPARSING
416 }
417 /* COMMENTS */
418 <PHPPARSING> SPECIAL_TOKEN :
419 {
420   "//" : IN_SINGLE_LINE_COMMENT
421 | "#"  : IN_SINGLE_LINE_COMMENT
422 | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
423 | "/*" : IN_MULTI_LINE_COMMENT
424 }
425
426 <IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
427 {
428   <SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : PHPPARSING
429 | < ~[] >
430 }
431
432 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
433 {
434  "todo"
435 }
436
437 void todo() :
438 {Token todoToken;}
439 {
440   todoToken = "TODO" {createNewTask(todoToken.sourceStart);}
441 }
442 <IN_FORMAL_COMMENT> SPECIAL_TOKEN :
443 {
444   "*/" : PHPPARSING
445 }
446
447 <IN_MULTI_LINE_COMMENT> SPECIAL_TOKEN :
448 {
449   "*/" : PHPPARSING
450 }
451
452 <IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>
453 MORE :
454 {
455   < ~[] >
456 }
457
458 /* KEYWORDS */
459 <PHPPARSING> TOKEN :
460 {
461   <CLASS    : "class">
462 | <FUNCTION : "function">
463 | <VAR      : "var">
464 | <IF       : "if">
465 | <ELSEIF   : "elseif">
466 | <ELSE     : "else">
467 | <ARRAY    : "array">
468 | <BREAK    : "break">
469 | <LIST     : "list">
470 }
471
472 /* LANGUAGE CONSTRUCT */
473 <PHPPARSING> TOKEN :
474 {
475   <PRINT              : "print">
476 | <ECHO               : "echo">
477 | <INCLUDE            : "include">
478 | <REQUIRE            : "require">
479 | <INCLUDE_ONCE       : "include_once">
480 | <REQUIRE_ONCE       : "require_once">
481 | <GLOBAL             : "global">
482 | <DEFINE             : "define">
483 | <STATIC             : "static">
484 }
485
486 <PHPPARSING,IN_VARIABLE> TOKEN :
487 {
488   <CLASSACCESS        : "->"> : PHPPARSING
489 | <STATICCLASSACCESS  : "::"> : PHPPARSING
490 | <ARRAYASSIGN        : "=>"> : PHPPARSING
491 }
492
493 /* RESERVED WORDS AND LITERALS */
494
495 <PHPPARSING> TOKEN :
496 {
497   <CASE     : "case">
498 | <CONST    : "const">
499 | <CONTINUE : "continue">
500 | <_DEFAULT : "default">
501 | <DO       : "do">
502 | <EXTENDS  : "extends">
503 | <FOR      : "for">
504 | <GOTO     : "goto">
505 | <NEW      : "new">
506 | <NULL     : "null">
507 | <RETURN   : "return">
508 | <SUPER    : "super">
509 | <SWITCH   : "switch">
510 | <THIS     : "this">
511 | <TRUE     : "true">
512 | <FALSE    : "false">
513 | <WHILE    : "while">
514 | <ENDWHILE : "endwhile">
515 | <ENDSWITCH: "endswitch">
516 | <ENDIF    : "endif">
517 | <ENDFOR   : "endfor">
518 | <FOREACH  : "foreach">
519 | <AS       : "as" >
520 }
521
522 /* TYPES */
523 <PHPPARSING> TOKEN :
524 {
525   <STRING  : "string">
526 | <OBJECT  : "object">
527 | <BOOL    : "bool">
528 | <BOOLEAN : "boolean">
529 | <REAL    : "real">
530 | <DOUBLE  : "double">
531 | <FLOAT   : "float">
532 | <INT     : "int">
533 | <INTEGER : "integer">
534 }
535
536 //Misc token
537 <PHPPARSING,IN_VARIABLE> TOKEN :
538 {
539   <AT                 : "@"> : PHPPARSING
540 | <BANG               : "!"> : PHPPARSING
541 | <TILDE              : "~"> : PHPPARSING
542 | <HOOK               : "?"> : PHPPARSING
543 | <COLON              : ":"> : PHPPARSING
544 }
545
546 /* OPERATORS */
547 <PHPPARSING,IN_VARIABLE> TOKEN :
548 {
549   <OR_OR              : "||"> : PHPPARSING
550 | <AND_AND            : "&&"> : PHPPARSING
551 | <PLUS_PLUS          : "++"> : PHPPARSING
552 | <MINUS_MINUS        : "--"> : PHPPARSING
553 | <PLUS               : "+"> : PHPPARSING
554 | <MINUS              : "-"> : PHPPARSING
555 | <STAR               : "*"> : PHPPARSING
556 | <SLASH              : "/"> : PHPPARSING
557 | <BIT_AND            : "&"> : PHPPARSING
558 | <BIT_OR             : "|"> : PHPPARSING
559 | <XOR                : "^"> : PHPPARSING
560 | <REMAINDER          : "%">  : PHPPARSING
561 | <LSHIFT             : "<<"> : PHPPARSING
562 | <RSIGNEDSHIFT       : ">>"> : PHPPARSING
563 | <RUNSIGNEDSHIFT     : ">>>"> : PHPPARSING
564 | <_ORL               : "OR"> : PHPPARSING
565 | <_ANDL              : "AND"> : PHPPARSING
566 }
567
568 /* LITERALS */
569 <PHPPARSING> TOKEN :
570 {
571   <INTEGER_LITERAL:
572         <DECIMAL_LITERAL> (["l","L"])?
573       | <HEX_LITERAL> (["l","L"])?
574       | <OCTAL_LITERAL> (["l","L"])?
575   >
576 |
577   <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
578 |
579   <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
580 |
581   <#OCTAL_LITERAL: "0" (["0"-"7"])* >
582 |
583   <FLOATING_POINT_LITERAL:
584         (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
585       | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
586       | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
587       | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
588   >
589 |
590   <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
591 |
592   <STRING_LITERAL: (<STRING_2> | <STRING_3>)>
593 //|   <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
594 |   <STRING_2: "'"  ( ~["'","\\"]  | "\\" ~[] )* "'">
595 |   <STRING_3: "`"  ( ~["`","\\"]  | "\\" ~[] )* "`">
596 }
597
598 <IN_STRING,DOLLAR_IN_STRING> SKIP :
599 {
600   <ESCAPED : ("\\" ~[])> : IN_STRING
601 }
602
603 <PHPPARSING> TOKEN :
604 {
605   <DOUBLEQUOTE : "\""> : IN_STRING
606 }
607
608
609 <IN_STRING> TOKEN :
610 {
611   <DOLLARS : "$"> : DOLLAR_IN_STRING
612 }
613
614 <IN_STRING,DOLLAR_IN_STRING> TOKEN :
615 {
616   <DOUBLEQUOTE2 : "\""> : PHPPARSING
617 }
618
619 <DOLLAR_IN_STRING> TOKEN :
620 {
621   <LBRACE1 : "{"> : DOLLAR_IN_STRING_EXPR
622 }
623
624 <IN_STRING> SPECIAL_TOKEN :
625 {
626     <"{"> : SKIPSTRING
627 }
628
629 <SKIPSTRING> SPECIAL_TOKEN :
630 {
631     <"}"> : IN_STRING
632 }
633
634 <SKIPSTRING> SKIP :
635 {
636     <~[]>
637 }
638
639 <DOLLAR_IN_STRING_EXPR> TOKEN :
640 {
641   <RBRACE1 : "}"> : DOLLAR_IN_STRING
642 }
643
644 <DOLLAR_IN_STRING_EXPR> TOKEN :
645 {
646   <ID : (~["}"])*>
647 }
648
649 <IN_STRING> SKIP :
650 {
651   <~[]>
652 }
653
654 <DOLLAR_IN_STRING_EXPR,IN_STRING> SKIP :
655 {
656   <~[]>
657 }
658 /* IDENTIFIERS */
659
660
661 <PHPPARSING,IN_VARIABLE> TOKEN : {<DOLLAR : "$"> : IN_VARIABLE}
662
663
664 <PHPPARSING, IN_VARIABLE, DOLLAR_IN_STRING> TOKEN :
665 {
666   <IDENTIFIER: (<LETTER>|<SPECIAL>) (<LETTER>|<DIGIT>|<SPECIAL>)* >
667 |
668   < #LETTER:
669       ["a"-"z"] | ["A"-"Z"]
670   >
671 |
672   < #DIGIT:
673       ["0"-"9"]
674   >
675 |
676   < #SPECIAL:
677     "_" | ["\u007f"-"\u00ff"]
678   >
679 }
680
681 <DOLLAR_IN_STRING> SPECIAL_TOKEN :
682 {
683  < ~[] > : IN_STRING
684 }
685 /* SEPARATORS */
686
687 <PHPPARSING,IN_VARIABLE> TOKEN :
688 {
689   <LPAREN    : "("> : PHPPARSING
690 | <RPAREN    : ")"> : PHPPARSING
691 | <LBRACE    : "{"> : PHPPARSING
692 | <RBRACE    : "}"> : PHPPARSING
693 | <LBRACKET  : "["> : PHPPARSING
694 | <RBRACKET  : "]"> : PHPPARSING
695 | <SEMICOLON : ";"> : PHPPARSING
696 | <COMMA     : ","> : PHPPARSING
697 | <DOT       : "."> : PHPPARSING
698 }
699
700
701 /* COMPARATOR */
702 <PHPPARSING,IN_VARIABLE> TOKEN :
703 {
704   <GT                 : ">"> : PHPPARSING
705 | <LT                 : "<"> : PHPPARSING
706 | <EQUAL_EQUAL        : "=="> : PHPPARSING
707 | <LE                 : "<="> : PHPPARSING
708 | <GE                 : ">="> : PHPPARSING
709 | <NOT_EQUAL          : "!="> : PHPPARSING
710 | <DIF                : "<>"> : PHPPARSING
711 | <BANGDOUBLEEQUAL    : "!=="> : PHPPARSING
712 | <TRIPLEEQUAL        : "==="> : PHPPARSING
713 }
714
715 /* ASSIGNATION */
716 <PHPPARSING,IN_VARIABLE> TOKEN :
717 {
718   <ASSIGN             : "="> : PHPPARSING
719 | <PLUSASSIGN         : "+="> : PHPPARSING
720 | <MINUSASSIGN        : "-="> : PHPPARSING
721 | <STARASSIGN         : "*="> : PHPPARSING
722 | <SLASHASSIGN        : "/="> : PHPPARSING
723 | <ANDASSIGN          : "&="> : PHPPARSING
724 | <ORASSIGN           : "|="> : PHPPARSING
725 | <XORASSIGN          : "^="> : PHPPARSING
726 | <DOTASSIGN          : ".="> : PHPPARSING
727 | <REMASSIGN          : "%="> : PHPPARSING
728 | <TILDEEQUAL         : "~="> : PHPPARSING
729 | <LSHIFTASSIGN       : "<<="> : PHPPARSING
730 | <RSIGNEDSHIFTASSIGN : ">>="> : PHPPARSING
731 }
732
733 void phpTest() :
734 {}
735 {
736   Php()
737   <EOF>
738 }
739
740 void phpFile() :
741 {}
742 {
743   try {
744     (PhpBlock())*
745     {PHPParser.createNewHTMLCode();}
746   } catch (TokenMgrError e) {
747     PHPeclipsePlugin.log(e);
748     errorStart   = SimpleCharStream.beginOffset;
749     errorEnd     = SimpleCharStream.endOffset;
750     errorMessage = e.getMessage();
751     errorLevel   = ERROR;
752     throw generateParseException();
753   }
754 }
755
756 /**
757  * A php block is a <?= expression [;]?>
758  * or <?php somephpcode ?>
759  * or <? somephpcode ?>
760  */
761 void PhpBlock() :
762 {
763   final PHPEchoBlock phpEchoBlock;
764   final Token token,phpEnd;
765 }
766 {
767   phpEchoBlock = phpEchoBlock()
768   {pushOnAstNodes(phpEchoBlock);}
769 |
770   [   <PHPSTARTLONG>
771     | token = <PHPSTARTSHORT>
772     {try {
773       setMarker(fileToParse,
774                 "You should use '<?php' instead of '<?' it will avoid some problems with XML",
775                 token.sourceStart,
776                 token.sourceEnd,
777                 INFO,
778                 "Line " + token.beginLine);
779     } catch (CoreException e) {
780       PHPeclipsePlugin.log(e);
781     }}
782   ]
783   {PHPParser.createNewHTMLCode();}
784   Php()
785   try {
786     phpEnd = <PHPEND>
787    {htmlStart = phpEnd.sourceEnd;}
788   } catch (ParseException e) {
789     errorMessage = "'?>' expected";
790     errorLevel   = ERROR;
791     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
792     errorEnd   = SimpleCharStream.getPosition() + 1;
793     processParseExceptionDebug(e);
794   }
795 }
796
797 PHPEchoBlock phpEchoBlock() :
798 {
799   final Expression expr;
800   final PHPEchoBlock echoBlock;
801   final Token token, token2;
802 }
803 {
804   token = <PHPECHOSTART> {PHPParser.createNewHTMLCode();}
805   expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
806   {
807   htmlStart = token2.sourceEnd;
808
809   echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
810   pushOnAstNodes(echoBlock);
811   return echoBlock;}
812 }
813
814 void Php() :
815 {}
816 {
817   (BlockStatement())*
818 }
819
820 ClassDeclaration ClassDeclaration() :
821 {
822   final ClassDeclaration classDeclaration;
823   Token className = null;
824   final Token superclassName, token, extendsToken;
825   String classNameImage = SYNTAX_ERROR_CHAR;
826   String superclassNameImage = null;
827   final int classEnd;
828 }
829 {
830   token = <CLASS>
831   try {
832     className = <IDENTIFIER>
833     {classNameImage = className.image;}
834   } catch (ParseException e) {
835     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
836     errorLevel   = ERROR;
837     errorStart   = token.sourceEnd+1;
838     errorEnd     = token.sourceEnd+1;
839     processParseExceptionDebug(e);
840   }
841   [
842     extendsToken = <EXTENDS>
843     try {
844       superclassName = <IDENTIFIER>
845       {superclassNameImage = superclassName.image;}
846     } catch (ParseException e) {
847       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', identifier expected";
848       errorLevel   = ERROR;
849       errorStart = extendsToken.sourceEnd+1;
850       errorEnd   = extendsToken.sourceEnd+1;
851       processParseExceptionDebug(e);
852       superclassNameImage = SYNTAX_ERROR_CHAR;
853     }
854   ]
855   {
856     int start, end;
857     if (className == null) {
858       start = token.sourceStart;
859       end = token.sourceEnd;
860     } else {
861       start = className.sourceStart;
862       end = className.sourceEnd;
863     }
864     if (superclassNameImage == null) {
865
866       classDeclaration = new ClassDeclaration(currentSegment,
867                                               classNameImage,
868                                               start,
869                                               end);
870     } else {
871       classDeclaration = new ClassDeclaration(currentSegment,
872                                               classNameImage,
873                                               superclassNameImage,
874                                               start,
875                                               end);
876     }
877       currentSegment.add(classDeclaration);
878       currentSegment = classDeclaration;
879   }
880   classEnd = ClassBody(classDeclaration)
881   {currentSegment = (OutlineableWithChildren) currentSegment.getParent();
882    classDeclaration.sourceEnd = classEnd;
883    pushOnAstNodes(classDeclaration);
884    return classDeclaration;}
885 }
886
887 int ClassBody(final ClassDeclaration classDeclaration) :
888 {
889 Token token;
890 }
891 {
892   try {
893     <LBRACE>
894   } catch (ParseException e) {
895     errorMessage = "unexpected token : '"+ e.currentToken.next.image + "'. '{' expected";
896     errorLevel   = ERROR;
897     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
898     errorEnd   = SimpleCharStream.getPosition() + 1;
899     processParseExceptionDebug(e);
900   }
901   ( ClassBodyDeclaration(classDeclaration) )*
902   try {
903     token = <RBRACE>
904     {return token.sourceEnd;}
905   } catch (ParseException e) {
906     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected";
907     errorLevel   = ERROR;
908     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
909     errorEnd   = SimpleCharStream.getPosition() + 1;
910     processParseExceptionDebug(e);
911     return PHPParser.token.sourceEnd;
912   }
913 }
914
915 /**
916  * A class can contain only methods and fields.
917  */
918 void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
919 {
920   final MethodDeclaration method;
921   final FieldDeclaration field;
922 }
923 {
924   method = MethodDeclaration() {method.analyzeCode();
925                                 classDeclaration.addMethod(method);}
926 | field = FieldDeclaration()   {classDeclaration.addField(field);}
927 }
928
929 /**
930  * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;.
931  * it is only used by ClassBodyDeclaration()
932  */
933 FieldDeclaration FieldDeclaration() :
934 {
935   VariableDeclaration variableDeclaration;
936   final VariableDeclaration[] list;
937   final ArrayList arrayList = new ArrayList();
938   final Token token;
939   Token token2 = null;
940   int pos;
941 }
942 {
943   token = <VAR> variableDeclaration = VariableDeclaratorNoSuffix()
944   {
945     arrayList.add(variableDeclaration);
946     outlineInfo.addVariable(variableDeclaration.name());
947     pos = variableDeclaration.sourceEnd;
948   }
949   (
950     <COMMA> variableDeclaration = VariableDeclaratorNoSuffix()
951       {
952         arrayList.add(variableDeclaration);
953         outlineInfo.addVariable(variableDeclaration.name());
954         pos = variableDeclaration.sourceEnd;
955       }
956   )*
957   try {
958     token2 = <SEMICOLON>
959   } catch (ParseException e) {
960     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected after variable declaration";
961     errorLevel   = ERROR;
962     errorStart   = pos+1;
963     errorEnd     = pos+1;
964     processParseExceptionDebug(e);
965   }
966
967   {list = new VariableDeclaration[arrayList.size()];
968    arrayList.toArray(list);
969    int end;
970    if (token2 == null) {
971      end = list[list.length-1].sourceEnd;
972    } else {
973      end = token2.sourceEnd;
974    }
975    return new FieldDeclaration(list,
976                                token.sourceStart,
977                                end,
978                                currentSegment);}
979 }
980
981 /**
982  * a strict variable declarator : there cannot be a suffix here.
983  * It will be used by fields and formal parameters
984  */
985 VariableDeclaration VariableDeclaratorNoSuffix() :
986 {
987   final Token token, lbrace,rbrace;
988   Expression expr, initializer = null;
989   Token assignToken;
990   Variable variable;
991 }
992 {
993   <DOLLAR>
994   (
995      token = <IDENTIFIER>
996      {variable = new Variable(token.image,token.sourceStart,token.sourceEnd);}
997    |
998      lbrace = <LBRACE> expr = Expression() rbrace = <RBRACE>
999      {variable = new Variable(expr,lbrace.sourceStart,rbrace.sourceEnd);}
1000   )
1001   [
1002     assignToken = <ASSIGN>
1003     try {
1004       initializer = VariableInitializer()
1005     } catch (ParseException e) {
1006       errorMessage = "Literal expression expected in variable initializer";
1007       errorLevel   = ERROR;
1008       errorStart = assignToken.sourceEnd +1;
1009       errorEnd   = assignToken.sourceEnd +1;
1010       processParseExceptionDebug(e);
1011     }
1012   ]
1013   {
1014   if (initializer == null) {
1015     return new VariableDeclaration(currentSegment,
1016                                    variable,
1017                                    variable.sourceStart,
1018                                    variable.sourceEnd);
1019   }
1020   return new VariableDeclaration(currentSegment,
1021                                  variable,
1022                                  initializer,
1023                                  VariableDeclaration.EQUAL,
1024                                  variable.sourceStart);
1025   }
1026 }
1027
1028 /**
1029  * this will be used by static statement
1030  */
1031 VariableDeclaration VariableDeclarator() :
1032 {
1033   final AbstractVariable variable;
1034   Expression initializer = null;
1035   final Token token;
1036 }
1037 {
1038   variable = VariableDeclaratorId()
1039   [
1040     token = <ASSIGN>
1041     try {
1042       initializer = VariableInitializer()
1043     } catch (ParseException e) {
1044       errorMessage = "Literal expression expected in variable initializer";
1045       errorLevel   = ERROR;
1046       errorStart = token.sourceEnd+1;
1047       errorEnd   = token.sourceEnd+1;
1048       processParseExceptionDebug(e);
1049     }
1050   ]
1051   {
1052   if (initializer == null) {
1053     return new VariableDeclaration(currentSegment,
1054                                    variable,
1055                                    variable.sourceStart,
1056                                    variable.sourceEnd);
1057   }
1058     return new VariableDeclaration(currentSegment,
1059                                    variable,
1060                                    initializer,
1061                                    VariableDeclaration.EQUAL,
1062                                    variable.sourceStart);
1063   }
1064 }
1065
1066 /**
1067  * A Variable name.
1068  * @return the variable name (with suffix)
1069  */
1070 AbstractVariable VariableDeclaratorId() :
1071 {
1072   final Variable var;
1073   AbstractVariable expression = null;
1074 }
1075 {
1076   try {
1077     var = Variable()
1078     (
1079       LOOKAHEAD(2)
1080       expression = VariableSuffix(var)
1081     )*
1082     {
1083      if (expression == null) {
1084        return var;
1085      }
1086      return expression;
1087     }
1088   } catch (ParseException e) {
1089     errorMessage = "'$' expected for variable identifier";
1090     errorLevel   = ERROR;
1091     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1092     errorEnd   = SimpleCharStream.getPosition() + 1;
1093     throw e;
1094   }
1095 }
1096
1097 Variable Variable() :
1098 {
1099   Variable variable = null;
1100   final Token token;
1101 }
1102 {
1103   token = <DOLLAR> variable = Var()
1104   {
1105     return variable;
1106   }
1107 }
1108
1109 Variable Var() :
1110 {
1111   Variable variable = null;
1112   final Token token,token2;
1113   ConstantIdentifier constant;
1114   Expression expression;
1115 }
1116 {
1117   token = <DOLLAR> variable = Var()
1118   {return new Variable(variable,variable.sourceStart,variable.sourceEnd);}
1119 |
1120   token = <LBRACE> expression = Expression() token2 = <RBRACE>
1121   {
1122    return new Variable(expression,
1123                        token.sourceStart,
1124                        token2.sourceEnd);
1125   }
1126 |
1127   token = <IDENTIFIER>
1128   {return new Variable(token.image,token.sourceStart,token.sourceEnd);}
1129 }
1130
1131 Expression VariableInitializer() :
1132 {
1133   final Expression expr;
1134   final Token token, token2;
1135 }
1136 {
1137   expr = Literal()
1138   {return expr;}
1139 |
1140   token2 = <MINUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1141   {return new PrefixedUnaryExpression(new NumberLiteral(token),
1142                                       OperatorIds.MINUS,
1143                                       token2.sourceStart);}
1144 |
1145   token2 = <PLUS> (token = <INTEGER_LITERAL> | token = <FLOATING_POINT_LITERAL>)
1146   {return new PrefixedUnaryExpression(new NumberLiteral(token),
1147                                       OperatorIds.PLUS,
1148                                       token2.sourceStart);}
1149 |
1150   expr = ArrayDeclarator()
1151   {return expr;}
1152 |
1153   token = <IDENTIFIER>
1154   {return new ConstantIdentifier(token);}
1155 }
1156
1157 ArrayVariableDeclaration ArrayVariable() :
1158 {
1159 final Expression expr,expr2;
1160 }
1161 {
1162   expr = Expression()
1163   [
1164     <ARRAYASSIGN> expr2 = Expression()
1165     {return new ArrayVariableDeclaration(expr,expr2);}
1166   ]
1167   {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());}
1168 }
1169
1170 ArrayVariableDeclaration[] ArrayInitializer() :
1171 {
1172   ArrayVariableDeclaration expr;
1173   final ArrayList list = new ArrayList();
1174 }
1175 {
1176   <LPAREN>
1177     [
1178       expr = ArrayVariable()
1179       {list.add(expr);}
1180       ( LOOKAHEAD(2) <COMMA> expr = ArrayVariable()
1181       {list.add(expr);}
1182       )*
1183     ]
1184     [
1185       <COMMA> {list.add(null);}
1186     ]
1187   <RPAREN>
1188   {
1189   final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
1190   list.toArray(vars);
1191   return vars;}
1192 }
1193
1194 /**
1195  * A Method Declaration.
1196  * <b>function</b> MetodDeclarator() Block()
1197  */
1198 MethodDeclaration MethodDeclaration() :
1199 {
1200   final MethodDeclaration functionDeclaration;
1201   final Block block;
1202   final OutlineableWithChildren seg = currentSegment;
1203   final Token token;
1204 }
1205 {
1206   token = <FUNCTION>
1207   try {
1208     functionDeclaration = MethodDeclarator(token.sourceStart)
1209     {outlineInfo.addVariable(functionDeclaration.name);}
1210   } catch (ParseException e) {
1211     if (errorMessage != null)  throw e;
1212     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1213     errorLevel   = ERROR;
1214     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
1215     errorEnd   = SimpleCharStream.getPosition() + 1;
1216     throw e;
1217   }
1218   {currentSegment = functionDeclaration;}
1219   block = Block()
1220   {functionDeclaration.statements = block.statements;
1221    currentSegment = seg;
1222    return functionDeclaration;}
1223 }
1224
1225 /**
1226  * A MethodDeclarator.
1227  * [&] IDENTIFIER(parameters ...).
1228  * @return a function description for the outline
1229  */
1230 MethodDeclaration MethodDeclarator(final int start) :
1231 {
1232   Token identifier = null;
1233   Token reference = null;
1234   final Hashtable formalParameters = new Hashtable();
1235   String identifierChar = SYNTAX_ERROR_CHAR;
1236   int end = start;
1237 }
1238 {
1239   [reference = <BIT_AND> {end = reference.sourceEnd;}]
1240   try {
1241     identifier = <IDENTIFIER>
1242     {
1243       identifierChar = identifier.image;
1244       end = identifier.sourceEnd;
1245     }
1246   } catch (ParseException e) {
1247     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected";
1248     errorLevel   = ERROR;
1249     errorStart = e.currentToken.sourceEnd;
1250     errorEnd   = e.currentToken.next.sourceStart;
1251     processParseExceptionDebug(e);
1252   }
1253   end = FormalParameters(formalParameters)
1254   {
1255   int nameStart, nameEnd;
1256   if (identifier == null) {
1257     if (reference == null) {
1258       nameStart = start + 9;
1259       nameEnd = start + 10;
1260     } else {
1261       nameStart = reference.sourceEnd + 1;
1262       nameEnd = reference.sourceEnd + 2;
1263     }
1264   } else {
1265       nameStart = identifier.sourceStart;
1266       nameEnd = identifier.sourceEnd;
1267   }
1268   return new MethodDeclaration(currentSegment,
1269                                identifierChar,
1270                                formalParameters,
1271                                reference != null,
1272                                nameStart,
1273                                nameEnd,
1274                                start,
1275                                end);
1276   }
1277 }
1278
1279 /**
1280  * FormalParameters follows method identifier.
1281  * (FormalParameter())
1282  */
1283 int FormalParameters(final Hashtable parameters) :
1284 {
1285   VariableDeclaration var;
1286   final Token token;
1287   Token tok = PHPParser.token;
1288   int end = tok.sourceEnd;
1289 }
1290 {
1291   try {
1292   tok = <LPAREN>
1293   {end = tok.sourceEnd;}
1294   } catch (ParseException e) {
1295     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected after function identifier";
1296     errorLevel   = ERROR;
1297     errorStart = e.currentToken.next.sourceStart;
1298     errorEnd   = e.currentToken.next.sourceEnd;
1299     processParseExceptionDebug(e);
1300   }
1301   [
1302     var = FormalParameter()
1303     {parameters.put(var.name(),var);end = var.sourceEnd;}
1304     (
1305       <COMMA> var = FormalParameter()
1306       {parameters.put(var.name(),var);end = var.sourceEnd;}
1307     )*
1308   ]
1309   try {
1310     token = <RPAREN>
1311     {end = token.sourceEnd;}
1312   } catch (ParseException e) {
1313     errorMessage = "')' expected";
1314     errorLevel   = ERROR;
1315     errorStart = e.currentToken.next.sourceStart;
1316     errorEnd   = e.currentToken.next.sourceEnd;
1317     processParseExceptionDebug(e);
1318   }
1319  {return end;}
1320 }
1321
1322 /**
1323  * A formal parameter.
1324  * $varname[=value] (,$varname[=value])
1325  */
1326 VariableDeclaration FormalParameter() :
1327 {
1328   final VariableDeclaration variableDeclaration;
1329   Token token = null;
1330 }
1331 {
1332   [token = <BIT_AND>] variableDeclaration = VariableDeclaratorNoSuffix()
1333   {
1334     if (token != null) {
1335       variableDeclaration.setReference(true);
1336     }
1337     return variableDeclaration;}
1338 }
1339
1340 ConstantIdentifier Type() :
1341 {final Token token;}
1342 {
1343   token = <STRING>    {return new ConstantIdentifier(token);}
1344 | token = <BOOL>      {return new ConstantIdentifier(token);}
1345 | token = <BOOLEAN>   {return new ConstantIdentifier(token);}
1346 | token = <REAL>      {return new ConstantIdentifier(token);}
1347 | token = <DOUBLE>    {return new ConstantIdentifier(token);}
1348 | token = <FLOAT>     {return new ConstantIdentifier(token);}
1349 | token = <INT>       {return new ConstantIdentifier(token);}
1350 | token = <INTEGER>   {return new ConstantIdentifier(token);}
1351 | token = <OBJECT>    {return new ConstantIdentifier(token);}
1352 }
1353
1354 Expression Expression() :
1355 {
1356   final Expression expr;
1357   Expression initializer = null;
1358   int assignOperator = -1;
1359 }
1360 {
1361   LOOKAHEAD(1)
1362   expr = ConditionalExpression()
1363   [
1364     assignOperator = AssignmentOperator()
1365     try {
1366       initializer = Expression()
1367     } catch (ParseException e) {
1368       if (errorMessage != null) {
1369         throw e;
1370       }
1371       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1372       errorLevel   = ERROR;
1373       errorEnd   = SimpleCharStream.getPosition();
1374       throw e;
1375     }
1376   ]
1377   {
1378     if (assignOperator != -1) {// todo : change this, very very bad :(
1379         if (expr instanceof AbstractVariable) {
1380           return new VariableDeclaration(currentSegment,
1381                                          (AbstractVariable) expr,
1382                                          initializer,
1383                                          expr.sourceStart,
1384                                          initializer.sourceEnd);
1385         }
1386         String varName = expr.toStringExpression().substring(1);
1387         return new VariableDeclaration(currentSegment,
1388                                        new Variable(varName,
1389                                                     expr.sourceStart,
1390                                                     expr.sourceEnd),
1391                                        expr.sourceStart,
1392                                        initializer.sourceEnd);
1393     }
1394     return expr;
1395   }
1396 | expr = ExpressionWBang()       {return expr;}
1397 }
1398
1399 Expression ExpressionWBang() :
1400 {
1401   final Expression expr;
1402   final Token token;
1403 }
1404 {
1405   token = <BANG> expr = ExpressionWBang()
1406   {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1407 | expr = ExpressionNoBang() {return expr;}
1408 }
1409
1410 Expression ExpressionNoBang() :
1411 {
1412   Expression expr;
1413 }
1414 {
1415   expr = ListExpression()    {return expr;}
1416 |
1417   expr = PrintExpression()   {return expr;}
1418 }
1419
1420 /**
1421  * Any assignement operator.
1422  * @return the assignement operator id
1423  */
1424 int AssignmentOperator() :
1425 {}
1426 {
1427   <ASSIGN>             {return VariableDeclaration.EQUAL;}
1428 | <STARASSIGN>         {return VariableDeclaration.STAR_EQUAL;}
1429 | <SLASHASSIGN>        {return VariableDeclaration.SLASH_EQUAL;}
1430 | <REMASSIGN>          {return VariableDeclaration.REM_EQUAL;}
1431 | <PLUSASSIGN>         {return VariableDeclaration.PLUS_EQUAL;}
1432 | <MINUSASSIGN>        {return VariableDeclaration.MINUS_EQUAL;}
1433 | <LSHIFTASSIGN>       {return VariableDeclaration.LSHIFT_EQUAL;}
1434 | <RSIGNEDSHIFTASSIGN> {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;}
1435 | <ANDASSIGN>          {return VariableDeclaration.AND_EQUAL;}
1436 | <XORASSIGN>          {return VariableDeclaration.XOR_EQUAL;}
1437 | <ORASSIGN>           {return VariableDeclaration.OR_EQUAL;}
1438 | <DOTASSIGN>          {return VariableDeclaration.DOT_EQUAL;}
1439 | <TILDEEQUAL>         {return VariableDeclaration.TILDE_EQUAL;}
1440 }
1441
1442 Expression ConditionalExpression() :
1443 {
1444   final Expression expr;
1445   Expression expr2 = null;
1446   Expression expr3 = null;
1447 }
1448 {
1449   expr = ConditionalOrExpression() [ <HOOK> expr2 = Expression() <COLON> expr3 = ConditionalExpression() ]
1450 {
1451   if (expr3 == null) {
1452     return expr;
1453   }
1454   return new ConditionalExpression(expr,expr2,expr3);
1455 }
1456 }
1457
1458 Expression ConditionalOrExpression() :
1459 {
1460   Expression expr,expr2;
1461   int operator;
1462 }
1463 {
1464   expr = ConditionalAndExpression()
1465   (
1466     (
1467         <OR_OR> {operator = OperatorIds.OR_OR;}
1468       | <_ORL>  {operator = OperatorIds.ORL;}
1469     )
1470     expr2 = ConditionalAndExpression()
1471     {
1472       expr = new BinaryExpression(expr,expr2,operator);
1473     }
1474   )*
1475   {return expr;}
1476 }
1477
1478 Expression ConditionalAndExpression() :
1479 {
1480   Expression expr,expr2;
1481   int operator;
1482 }
1483 {
1484   expr = ConcatExpression()
1485   (
1486   (  <AND_AND> {operator = OperatorIds.AND_AND;}
1487    | <_ANDL>   {operator = OperatorIds.ANDL;})
1488    expr2 = ConcatExpression() {expr = new BinaryExpression(expr,expr2,operator);}
1489   )*
1490   {return expr;}
1491 }
1492
1493 Expression ConcatExpression() :
1494 {
1495   Expression expr,expr2;
1496 }
1497 {
1498   expr = InclusiveOrExpression()
1499   (
1500     <DOT> expr2 = InclusiveOrExpression()
1501     {expr = new BinaryExpression(expr,expr2,OperatorIds.DOT);}
1502   )*
1503   {return expr;}
1504 }
1505
1506 Expression InclusiveOrExpression() :
1507 {
1508   Expression expr,expr2;
1509 }
1510 {
1511   expr = ExclusiveOrExpression()
1512   (<BIT_OR> expr2 = ExclusiveOrExpression()
1513    {expr = new BinaryExpression(expr,expr2,OperatorIds.OR);}
1514   )*
1515   {return expr;}
1516 }
1517
1518 Expression ExclusiveOrExpression() :
1519 {
1520   Expression expr,expr2;
1521 }
1522 {
1523   expr = AndExpression()
1524   (
1525     <XOR> expr2 = AndExpression()
1526     {expr = new BinaryExpression(expr,expr2,OperatorIds.XOR);}
1527   )*
1528   {return expr;}
1529 }
1530
1531 Expression AndExpression() :
1532 {
1533   Expression expr,expr2;
1534 }
1535 {
1536   expr = EqualityExpression()
1537   (
1538     LOOKAHEAD(1)
1539     <BIT_AND> expr2 = EqualityExpression()
1540     {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);}
1541   )*
1542   {return expr;}
1543 }
1544
1545 Expression EqualityExpression() :
1546 {
1547   Expression expr,expr2;
1548   int operator;
1549   Token token;
1550 }
1551 {
1552   expr = RelationalExpression()
1553   (
1554   (   token = <EQUAL_EQUAL>      {operator = OperatorIds.EQUAL_EQUAL;}
1555     | token = <DIF>              {operator = OperatorIds.DIF;}
1556     | token = <NOT_EQUAL>        {operator = OperatorIds.DIF;}
1557     | token = <BANGDOUBLEEQUAL>  {operator = OperatorIds.BANG_EQUAL_EQUAL;}
1558     | token = <TRIPLEEQUAL>      {operator = OperatorIds.EQUAL_EQUAL_EQUAL;}
1559   )
1560   try {
1561     expr2 = RelationalExpression()
1562   } catch (ParseException e) {
1563     if (errorMessage != null) {
1564       throw e;
1565     }
1566     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
1567     errorLevel   = ERROR;
1568     errorStart = token.sourceEnd +1;
1569     errorEnd   = token.sourceEnd +1;
1570     expr2 = new ConstantIdentifier(SYNTAX_ERROR_CHAR,token.sourceEnd +1,token.sourceEnd +1);
1571     processParseExceptionDebug(e);
1572   }
1573   {
1574     expr = new BinaryExpression(expr,expr2,operator);
1575   }
1576   )*
1577   {return expr;}
1578 }
1579
1580 Expression RelationalExpression() :
1581 {
1582   Expression expr,expr2;
1583   int operator;
1584 }
1585 {
1586   expr = ShiftExpression()
1587   (
1588   ( <LT> {operator = OperatorIds.LESS;}
1589   | <GT> {operator = OperatorIds.GREATER;}
1590   | <LE> {operator = OperatorIds.LESS_EQUAL;}
1591   | <GE> {operator = OperatorIds.GREATER_EQUAL;})
1592    expr2 = ShiftExpression()
1593   {expr = new BinaryExpression(expr,expr2,operator);}
1594   )*
1595   {return expr;}
1596 }
1597
1598 Expression ShiftExpression() :
1599 {
1600   Expression expr,expr2;
1601   int operator;
1602 }
1603 {
1604   expr = AdditiveExpression()
1605   (
1606   ( <LSHIFT>         {operator = OperatorIds.LEFT_SHIFT;}
1607   | <RSIGNEDSHIFT>   {operator = OperatorIds.RIGHT_SHIFT;}
1608   | <RUNSIGNEDSHIFT> {operator = OperatorIds.UNSIGNED_RIGHT_SHIFT;})
1609   expr2 = AdditiveExpression()
1610   {expr = new BinaryExpression(expr,expr2,operator);}
1611   )*
1612   {return expr;}
1613 }
1614
1615 Expression AdditiveExpression() :
1616 {
1617   Expression expr,expr2;
1618   int operator;
1619 }
1620 {
1621   expr = MultiplicativeExpression()
1622   (
1623     LOOKAHEAD(1)
1624      ( <PLUS>  {operator = OperatorIds.PLUS;}
1625      | <MINUS> {operator = OperatorIds.MINUS;}
1626   )
1627    expr2 = MultiplicativeExpression()
1628   {expr = new BinaryExpression(expr,expr2,operator);}
1629    )*
1630   {return expr;}
1631 }
1632
1633 Expression MultiplicativeExpression() :
1634 {
1635   Expression expr,expr2;
1636   int operator;
1637 }
1638 {
1639   try {
1640     expr = UnaryExpression()
1641   } catch (ParseException e) {
1642     if (errorMessage != null) throw e;
1643     errorMessage = "unexpected token '"+e.currentToken.next.image+"'";
1644     errorLevel   = ERROR;
1645     errorStart = PHPParser.token.sourceStart;
1646     errorEnd   = PHPParser.token.sourceEnd;
1647     throw e;
1648   }
1649   (
1650    (  <STAR>      {operator = OperatorIds.MULTIPLY;}
1651     | <SLASH>     {operator = OperatorIds.DIVIDE;}
1652     | <REMAINDER> {operator = OperatorIds.REMAINDER;})
1653     expr2 = UnaryExpression()
1654     {expr = new BinaryExpression(expr,expr2,operator);}
1655   )*
1656   {return expr;}
1657 }
1658
1659 /**
1660  * An unary expression starting with @, & or nothing
1661  */
1662 Expression UnaryExpression() :
1663 {
1664   final Expression expr;
1665 }
1666 {
1667  /* <BIT_AND> expr = UnaryExpressionNoPrefix()             //why did I had that ?
1668   {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);}
1669 |      */
1670   expr = AtNotTildeUnaryExpression() {return expr;}
1671 }
1672
1673 Expression AtNotTildeUnaryExpression() :
1674 {
1675   final Expression expr;
1676   final Token token;
1677 }
1678 {
1679   token = <AT>
1680   expr = AtNotTildeUnaryExpression()
1681   {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1682 |
1683   token = <TILDE>
1684   expr = AtNotTildeUnaryExpression()
1685   {return new PrefixedUnaryExpression(expr,OperatorIds.TWIDDLE,token.sourceStart);}
1686 |
1687   token = <BANG>
1688   expr = AtNotUnaryExpression()
1689   {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1690 |
1691   expr = UnaryExpressionNoPrefix()
1692   {return expr;}
1693 }
1694
1695 /**
1696  * An expression prefixed (or not) by one or more @ and !.
1697  * @return the expression
1698  */
1699 Expression AtNotUnaryExpression() :
1700 {
1701   final Expression expr;
1702   final Token token;
1703 }
1704 {
1705   token = <AT>
1706   expr = AtNotUnaryExpression()
1707   {return new PrefixedUnaryExpression(expr,OperatorIds.AT,token.sourceStart);}
1708 |
1709   token = <BANG>
1710   expr = AtNotUnaryExpression()
1711   {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,token.sourceStart);}
1712 |
1713   expr = UnaryExpressionNoPrefix()
1714   {return expr;}
1715 }
1716
1717 Expression UnaryExpressionNoPrefix() :
1718 {
1719   final Expression expr;
1720   final Token token;
1721 }
1722 {
1723   token = <PLUS> expr = AtNotTildeUnaryExpression()   {return new PrefixedUnaryExpression(expr,
1724                                                                                      OperatorIds.PLUS,
1725                                                                                      token.sourceStart);}
1726 |
1727   token = <MINUS> expr = AtNotTildeUnaryExpression()  {return new PrefixedUnaryExpression(expr,
1728                                                                                      OperatorIds.MINUS,
1729                                                                                      token.sourceStart);}
1730 |
1731   expr = PreIncDecExpression()
1732   {return expr;}
1733 |
1734   expr = UnaryExpressionNotPlusMinus()
1735   {return expr;}
1736 }
1737
1738
1739 Expression PreIncDecExpression() :
1740 {
1741 final Expression expr;
1742 final int operator;
1743 final Token token;
1744 }
1745 {
1746   (
1747       token = <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
1748     |
1749       token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1750   )
1751   expr = PrimaryExpression()
1752   {return new PrefixedUnaryExpression(expr,operator,token.sourceStart);}
1753 }
1754
1755 Expression UnaryExpressionNotPlusMinus() :
1756 {
1757   final Expression expr;
1758 }
1759 {
1760   LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
1761   expr = CastExpression()         {return expr;}
1762 | expr = PostfixExpression()      {return expr;}
1763 | expr = Literal()                {return expr;}
1764 | <LPAREN> expr = Expression()
1765   try {
1766     <RPAREN>
1767   } catch (ParseException e) {
1768     errorMessage = "')' expected";
1769     errorLevel   = ERROR;
1770     errorStart   = expr.sourceEnd +1;
1771     errorEnd     = expr.sourceEnd +1;
1772     processParseExceptionDebug(e);
1773   }
1774   {return expr;}
1775 }
1776
1777 CastExpression CastExpression() :
1778 {
1779 final ConstantIdentifier type;
1780 final Expression expr;
1781 final Token token,token1;
1782 }
1783 {
1784   token1 = <LPAREN>
1785   (
1786       type = Type()
1787     |
1788       token = <ARRAY> {type = new ConstantIdentifier(token);}
1789   )
1790   <RPAREN> expr = UnaryExpression()
1791   {return new CastExpression(type,expr,token1.sourceStart,expr.sourceEnd);}
1792 }
1793
1794 Expression PostfixExpression() :
1795 {
1796   final Expression expr;
1797   int operator = -1;
1798   Token token = null;
1799 }
1800 {
1801   expr = PrimaryExpression()
1802   [
1803       token = <PLUS_PLUS>   {operator = OperatorIds.PLUS_PLUS;}
1804     |
1805       token = <MINUS_MINUS> {operator = OperatorIds.MINUS_MINUS;}
1806   ]
1807   {
1808     if (operator == -1) {
1809       return expr;
1810     }
1811     return new PostfixedUnaryExpression(expr,operator,token.sourceEnd);
1812   }
1813 }
1814
1815 Expression PrimaryExpression() :
1816 {
1817   Expression expr;
1818   Token token = null;
1819 }
1820 {
1821   [token = <BIT_AND>] expr = refPrimaryExpression(token)
1822   {return expr;}
1823 |
1824   expr = ArrayDeclarator()
1825   {return expr;}
1826 }
1827
1828 Expression refPrimaryExpression(final Token reference) :
1829 {
1830   Expression expr;
1831   Expression expr2 = null;
1832   final Token identifier;
1833 }
1834 {
1835   identifier = <IDENTIFIER>
1836   {
1837     expr = new ConstantIdentifier(identifier);
1838   }
1839   (
1840     <STATICCLASSACCESS> expr2 = ClassIdentifier()
1841     {expr = new ClassAccess(expr,
1842                             expr2,
1843                             ClassAccess.STATIC);}
1844   )*
1845   [ expr2 = Arguments(expr) ]
1846   {
1847     if (expr2 == null) {
1848       if (reference != null) {
1849         ParseException e = generateParseException();
1850         errorMessage = "you cannot use a constant by reference";
1851         errorLevel   = ERROR;
1852         errorStart   = reference.sourceStart;
1853         errorEnd     = reference.sourceEnd;
1854         processParseExceptionDebug(e);
1855       }
1856       return expr;
1857     }
1858     return expr2;
1859   }
1860 |
1861   expr = VariableDeclaratorId()  //todo use the reference parameter ...
1862   [ expr = Arguments(expr) ]
1863   {return expr;}
1864 |
1865   token = <NEW>
1866   expr = ClassIdentifier()
1867   {
1868     int start;
1869     if (reference == null) {
1870       start = token.sourceStart;
1871     } else {
1872       start = reference.sourceStart;
1873     }
1874     expr = new ClassInstantiation(expr,
1875                                   reference != null,
1876                                   start);
1877   }
1878   [ expr = Arguments(expr) ]
1879   {return expr;}
1880 }
1881
1882 /**
1883  * An array declarator.
1884  * array(vars)
1885  * @return an array
1886  */
1887 ArrayInitializer ArrayDeclarator() :
1888 {
1889   final ArrayVariableDeclaration[] vars;
1890   final Token token;
1891 }
1892 {
1893   token = <ARRAY> vars = ArrayInitializer()
1894   {return new ArrayInitializer(vars,
1895                                token.sourceStart,
1896                                PHPParser.token.sourceEnd);}
1897 }
1898
1899 Expression ClassIdentifier():
1900 {
1901   final Expression expr;
1902   final Token token;
1903 }
1904 {
1905   token = <IDENTIFIER>          {return new ConstantIdentifier(token);}
1906 | expr = Type()                 {return expr;}
1907 | expr = VariableDeclaratorId() {return expr;}
1908 }
1909
1910 /**
1911  * Used by Variabledeclaratorid and primarysuffix
1912  */
1913 AbstractVariable VariableSuffix(final AbstractVariable prefix) :
1914 {
1915   Expression expression = null;
1916   final Token classAccessToken,lbrace,rbrace;
1917   Token token;
1918   int pos;
1919 }
1920 {
1921   classAccessToken = <CLASSACCESS>
1922   try {
1923     (
1924       lbrace = <LBRACE> expression = Expression() rbrace = <RBRACE>
1925                 {
1926                  expression = new Variable(expression,
1927                                            lbrace.sourceStart,
1928                                            rbrace.sourceEnd);
1929                 }
1930       |
1931         token = <IDENTIFIER>
1932         {expression = new ConstantIdentifier(token.image,token.sourceStart,token.sourceEnd);}
1933       |
1934         expression = Variable()
1935     )
1936   } catch (ParseException e) {
1937     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
1938     errorLevel   = ERROR;
1939     errorStart = classAccessToken.sourceEnd +1;
1940     errorEnd   = classAccessToken.sourceEnd +1;
1941     processParseExceptionDebug(e);
1942   }
1943   {return new ClassAccess(prefix,
1944                           expression,
1945                           ClassAccess.NORMAL);}
1946 |
1947   token = <LBRACKET> {pos = token.sourceEnd+1;}
1948   [  expression = Expression() {pos = expression.sourceEnd+1;}
1949    | expression = Type()       {pos = expression.sourceEnd+1;}]  //Not good
1950   try {
1951     token = <RBRACKET>
1952     {pos = token.sourceEnd;}
1953   } catch (ParseException e) {
1954     errorMessage = "']' expected";
1955     errorLevel   = ERROR;
1956     errorStart = pos;
1957     errorEnd   = pos;
1958     processParseExceptionDebug(e);
1959   }
1960   {return new ArrayDeclarator(prefix,expression,pos);}
1961 |
1962   token = <LBRACE> {pos = token.sourceEnd+1;}
1963   [  expression = Expression() {pos = expression.sourceEnd+1;}
1964    | expression = Type()       {pos = expression.sourceEnd+1;}]  //Not good
1965   try {
1966     token = <RBRACE>
1967     {pos = token.sourceEnd;}
1968   } catch (ParseException e) {
1969     errorMessage = "']' expected";
1970     errorLevel   = ERROR;
1971     errorStart = pos;
1972     errorEnd   = pos;
1973     processParseExceptionDebug(e);
1974   }
1975   {return new ArrayDeclarator(prefix,expression,pos);}//todo : check braces here
1976 }
1977
1978 Literal Literal() :
1979 {
1980   final Token token;
1981   StringLiteral literal;
1982 }
1983 {
1984   token = <INTEGER_LITERAL>        {return new NumberLiteral(token);}
1985 | token = <FLOATING_POINT_LITERAL> {return new NumberLiteral(token);}
1986 | token = <STRING_LITERAL>         {return new StringLiteral(token);}
1987 | token = <TRUE>                   {return new TrueLiteral(token);}
1988 | token = <FALSE>                  {return new FalseLiteral(token);}
1989 | token = <NULL>                   {return new NullLiteral(token);}
1990 | literal = evaluableString()        {return literal;}
1991 }
1992
1993 StringLiteral evaluableString() :
1994 {
1995   ArrayList list = new ArrayList();
1996   Token start,end;
1997   Token token,lbrace,rbrace;
1998   AbstractVariable var;
1999   Expression expr;
2000 }
2001 {
2002   start = <DOUBLEQUOTE>
2003   (
2004    <DOLLARS>
2005        (
2006         token = <IDENTIFIER> {list.add(new Variable(token.image,
2007                                                     token.sourceStart,
2008                                                     token.sourceEnd));}
2009         |
2010          lbrace = <LBRACE1>
2011          token = <ID>
2012          {list.add(new Variable(token.image,
2013                                 token.sourceStart,
2014                                 token.sourceEnd));}
2015          rbrace = <RBRACE1>
2016        )
2017    )*
2018   end = <DOUBLEQUOTE2>
2019   {
2020   AbstractVariable[] vars = new AbstractVariable[list.size()];
2021   list.toArray(vars);
2022   return new StringLiteral(SimpleCharStream.currentBuffer.substring(start.sourceEnd,end.sourceStart),
2023                            start.sourceStart,
2024                            end.sourceEnd,
2025                            vars);
2026   }
2027 }
2028
2029 FunctionCall Arguments(final Expression func) :
2030 {
2031 Expression[] args = null;
2032 final Token token;
2033 }
2034 {
2035   <LPAREN> [ args = ArgumentList() ]
2036   try {
2037     token = <RPAREN>
2038     {return new FunctionCall(func,args,token.sourceEnd);}
2039   } catch (ParseException e) {
2040     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected to close the argument list";
2041     errorLevel   = ERROR;
2042     errorStart = args[args.length-1].sourceEnd+1;
2043     errorEnd   = args[args.length-1].sourceEnd+1;
2044     processParseExceptionDebug(e);
2045   }
2046   {return new FunctionCall(func,args,args[args.length-1].sourceEnd);}
2047 }
2048
2049 /**
2050  * An argument list is a list of arguments separated by comma :
2051  * argumentDeclaration() (, argumentDeclaration)*
2052  * @return an array of arguments
2053  */
2054 Expression[] ArgumentList() :
2055 {
2056 Expression arg;
2057 final ArrayList list = new ArrayList();
2058 int pos;
2059 Token token;
2060 }
2061 {
2062   arg = Expression()
2063   {list.add(arg);pos = arg.sourceEnd;}
2064   ( token = <COMMA> {pos = token.sourceEnd;}
2065       try {
2066         arg = Expression()
2067         {list.add(arg);
2068          pos = arg.sourceEnd;}
2069       } catch (ParseException e) {
2070         errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list";
2071         errorLevel   = ERROR;
2072         errorStart   = pos+1;
2073         errorEnd     = pos+1;
2074         processParseException(e);
2075       }
2076    )*
2077    {
2078    final Expression[] arguments = new Expression[list.size()];
2079    list.toArray(arguments);
2080    return arguments;}
2081 }
2082
2083 /**
2084  * A Statement without break.
2085  * @return a statement
2086  */
2087 Statement StatementNoBreak() :
2088 {
2089   final Statement statement;
2090   Token token = null;
2091 }
2092 {
2093   LOOKAHEAD(2)
2094   statement = expressionStatement()     {return statement;}
2095 | LOOKAHEAD(1)
2096   statement = LabeledStatement()        {return statement;}
2097 | statement = Block()                   {return statement;}
2098 | statement = EmptyStatement()          {return statement;}
2099 | statement = SwitchStatement()         {return statement;}
2100 | statement = IfStatement()             {return statement;}
2101 | statement = WhileStatement()          {return statement;}
2102 | statement = DoStatement()             {return statement;}
2103 | statement = ForStatement()            {return statement;}
2104 | statement = ForeachStatement()        {return statement;}
2105 | statement = ContinueStatement()       {return statement;}
2106 | statement = ReturnStatement()         {return statement;}
2107 | statement = EchoStatement()           {return statement;}
2108 | [token=<AT>] statement = IncludeStatement()
2109   {if (token != null) {
2110     ((InclusionStatement)statement).silent = true;
2111     statement.sourceStart = token.sourceStart;
2112   }
2113   return statement;}
2114 | statement = StaticStatement()         {return statement;}
2115 | statement = GlobalStatement()         {return statement;}
2116 | statement = defineStatement()         {currentSegment.add((Outlineable)statement);return statement;}
2117 }
2118
2119 /**
2120  * A statement expression.
2121  * expression ;
2122  * @return an expression
2123  */
2124 Statement expressionStatement() :
2125 {
2126   final Statement statement;
2127   final Token token;
2128 }
2129 {
2130   statement = Expression()
2131   try {
2132     token = <SEMICOLON>
2133     {statement.sourceEnd = token.sourceEnd;}
2134   } catch (ParseException e) {
2135     if (e.currentToken.next.kind != PHPParserConstants.PHPEND) {
2136       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2137       errorLevel   = ERROR;
2138       errorStart = statement.sourceEnd+1;
2139       errorEnd   = statement.sourceEnd+1;
2140       processParseExceptionDebug(e);
2141     }
2142   }
2143   {return statement;}
2144 }
2145
2146 Define defineStatement() :
2147 {
2148   Expression defineName,defineValue;
2149   final Token defineToken;
2150   Token token;
2151   int pos;
2152 }
2153 {
2154   defineToken = <DEFINE> {pos = defineToken.sourceEnd+1;}
2155   try {
2156     token = <LPAREN>
2157     {pos = token.sourceEnd+1;}
2158   } catch (ParseException e) {
2159     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2160     errorLevel   = ERROR;
2161     errorStart   = pos;
2162     errorEnd     = pos;
2163     processParseExceptionDebug(e);
2164   }
2165   try {
2166     defineName = Expression()
2167     {pos = defineName.sourceEnd+1;}
2168   } catch (ParseException e) {
2169     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2170     errorLevel   = ERROR;
2171     errorStart   = pos;
2172     errorEnd     = pos;
2173     processParseExceptionDebug(e);
2174     defineName = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2175   }
2176   try {
2177     token = <COMMA>
2178     {pos = defineName.sourceEnd+1;}
2179   } catch (ParseException e) {
2180     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2181     errorLevel   = ERROR;
2182     errorStart   = pos;
2183     errorEnd     = pos;
2184     processParseExceptionDebug(e);
2185   }
2186   try {
2187     defineValue = Expression()
2188     {pos = defineValue.sourceEnd+1;}
2189   } catch (ParseException e) {
2190     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected";
2191     errorLevel   = ERROR;
2192     errorStart   = pos;
2193     errorEnd     = pos;
2194     processParseExceptionDebug(e);
2195     defineValue = new StringLiteral(SYNTAX_ERROR_CHAR,pos,pos);
2196   }
2197   try {
2198     token = <RPAREN>
2199     {pos = token.sourceEnd+1;}
2200   } catch (ParseException e) {
2201     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2202     errorLevel   = ERROR;
2203     errorStart   = pos;
2204     errorEnd     = pos;
2205     processParseExceptionDebug(e);
2206   }
2207   {return new Define(currentSegment,
2208                      defineName,
2209                      defineValue,
2210                      defineToken.sourceStart,
2211                      pos);}
2212 }
2213
2214 /**
2215  * A Normal statement.
2216  */
2217 Statement Statement() :
2218 {
2219   final Statement statement;
2220 }
2221 {
2222   statement = StatementNoBreak() {return statement;}
2223 | statement = BreakStatement()   {return statement;}
2224 }
2225
2226 /**
2227  * An html block inside a php syntax.
2228  */
2229 HTMLBlock htmlBlock() :
2230 {
2231   final int startIndex = nodePtr;
2232   final AstNode[] blockNodes;
2233   final int nbNodes;
2234   final Token phpEnd;
2235 }
2236 {
2237   phpEnd = <PHPEND>
2238   {htmlStart = phpEnd.sourceEnd;}
2239   (phpEchoBlock())*
2240   try {
2241     (<PHPSTARTLONG> | <PHPSTARTSHORT>)
2242     {PHPParser.createNewHTMLCode();}
2243   } catch (ParseException e) {
2244     errorMessage = "unexpected end of file , '<?php' expected";
2245     errorLevel   = ERROR;
2246     errorStart   = SimpleCharStream.getPosition();
2247     errorEnd     = SimpleCharStream.getPosition();
2248     throw e;
2249   }
2250   {
2251   nbNodes    = nodePtr - startIndex;
2252   if (nbNodes == 0) {
2253     return null;
2254   }
2255   blockNodes = new AstNode[nbNodes];
2256   System.arraycopy(nodes,startIndex+1,blockNodes,0,nbNodes);
2257   nodePtr = startIndex;
2258   return new HTMLBlock(blockNodes);}
2259 }
2260
2261 /**
2262  * An include statement. It's "include" an expression;
2263  */
2264 InclusionStatement IncludeStatement() :
2265 {
2266   Expression expr;
2267   final int keyword;
2268   final InclusionStatement inclusionStatement;
2269   final Token token, token2;
2270   int pos;
2271 }
2272 {
2273       (  token = <REQUIRE>      {keyword = InclusionStatement.REQUIRE;pos=token.sourceEnd;}
2274        | token = <REQUIRE_ONCE> {keyword = InclusionStatement.REQUIRE_ONCE;pos=token.sourceEnd;}
2275        | token = <INCLUDE>      {keyword = InclusionStatement.INCLUDE;pos=token.sourceEnd;}
2276        | token = <INCLUDE_ONCE> {keyword = InclusionStatement.INCLUDE_ONCE;pos=token.sourceEnd;})
2277   try {
2278     expr = Expression()
2279     {pos = expr.sourceEnd;}
2280   } catch (ParseException e) {
2281     if (errorMessage != null) {
2282       throw e;
2283     }
2284     errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
2285     errorLevel   = ERROR;
2286     errorStart   = e.currentToken.next.sourceStart;
2287     errorEnd     = e.currentToken.next.sourceEnd;
2288     expr = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2289     processParseExceptionDebug(e);
2290   }
2291   try {
2292     token2 = <SEMICOLON>
2293     {pos=token2.sourceEnd;}
2294   } catch (ParseException e) {
2295     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
2296     errorLevel   = ERROR;
2297     errorStart   = e.currentToken.next.sourceStart;
2298     errorEnd     = e.currentToken.next.sourceEnd;
2299     processParseExceptionDebug(e);
2300   }
2301   {
2302    inclusionStatement = new InclusionStatement(currentSegment,
2303                                                keyword,
2304                                                expr,
2305                                                token.sourceStart,
2306                                                pos);
2307    currentSegment.add(inclusionStatement);
2308    return inclusionStatement;
2309   }
2310 }
2311
2312 PrintExpression PrintExpression() :
2313 {
2314   final Expression expr;
2315   final Token printToken;
2316 }
2317 {
2318   token = <PRINT> expr = Expression()
2319   {return new PrintExpression(expr,token.sourceStart,expr.sourceEnd);}
2320 }
2321
2322 ListExpression ListExpression() :
2323 {
2324   Expression expr = null;
2325   final Expression expression;
2326   final ArrayList list = new ArrayList();
2327   int pos;
2328   final Token listToken, rParen;
2329   Token token;
2330 }
2331 {
2332   listToken = <LIST> {pos = listToken.sourceEnd;}
2333   try {
2334     token = <LPAREN> {pos = token.sourceEnd;}
2335   } catch (ParseException e) {
2336     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
2337     errorLevel   = ERROR;
2338     errorStart   = listToken.sourceEnd+1;
2339     errorEnd     = listToken.sourceEnd+1;
2340     processParseExceptionDebug(e);
2341   }
2342   [
2343     expr = VariableDeclaratorId()
2344     {list.add(expr);pos = expr.sourceEnd;}
2345   ]
2346   {if (expr == null) list.add(null);}
2347   (
2348     try {
2349       token = <COMMA>
2350       {pos = token.sourceEnd;}
2351     } catch (ParseException e) {
2352       errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' expected";
2353       errorLevel   = ERROR;
2354       errorStart   = pos+1;
2355       errorEnd     = pos+1;
2356       processParseExceptionDebug(e);
2357     }
2358     [expr = VariableDeclaratorId() {list.add(expr);pos = expr.sourceEnd;}]
2359   )*
2360   try {
2361     rParen = <RPAREN>
2362     {pos = rParen.sourceEnd;}
2363   } catch (ParseException e) {
2364     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ')' expected";
2365     errorLevel   = ERROR;
2366     errorStart = pos+1;
2367     errorEnd   = pos+1;
2368       processParseExceptionDebug(e);
2369   }
2370   [ <ASSIGN> expression = Expression()
2371     {
2372     final AbstractVariable[] vars = new AbstractVariable[list.size()];
2373     list.toArray(vars);
2374     return new ListExpression(vars,
2375                               expression,
2376                               listToken.sourceStart,
2377                               expression.sourceEnd);}
2378   ]
2379   {
2380     final AbstractVariable[] vars = new AbstractVariable[list.size()];
2381     list.toArray(vars);
2382     return new ListExpression(vars,listToken.sourceStart,pos);}
2383 }
2384
2385 /**
2386  * An echo statement.
2387  * echo anyexpression (, otherexpression)*
2388  */
2389 EchoStatement EchoStatement() :
2390 {
2391   final ArrayList expressions = new ArrayList();
2392   Expression expr;
2393   Token token;
2394   Token token2 = null;
2395 }
2396 {
2397   token = <ECHO> expr = Expression()
2398   {expressions.add(expr);}
2399   (
2400     <COMMA> expr = Expression()
2401     {expressions.add(expr);}
2402   )*
2403   try {
2404     token2 = <SEMICOLON>
2405   } catch (ParseException e) {
2406     if (e.currentToken.next.kind != 4) {
2407       errorMessage = "';' expected after 'echo' statement";
2408       errorLevel   = ERROR;
2409       errorStart   = e.currentToken.sourceEnd;
2410       errorEnd     = e.currentToken.sourceEnd;
2411       processParseExceptionDebug(e);
2412     }
2413   }
2414   {
2415    final Expression[] exprs = new Expression[expressions.size()];
2416    expressions.toArray(exprs);
2417    if (token2 == null) {
2418      return new EchoStatement(exprs,token.sourceStart, exprs[exprs.length-1].sourceEnd);
2419    }
2420    return new EchoStatement(exprs,token.sourceStart, token2.sourceEnd);
2421    }
2422 }
2423
2424 GlobalStatement GlobalStatement() :
2425 {
2426    Variable expr;
2427    final ArrayList vars = new ArrayList();
2428    final GlobalStatement global;
2429    final Token token, token2;
2430    int pos;
2431 }
2432 {
2433   token = <GLOBAL>
2434     expr = Variable()
2435     {vars.add(expr);pos = expr.sourceEnd+1;}
2436   (<COMMA>
2437     expr = Variable()
2438     {vars.add(expr);pos = expr.sourceEnd+1;}
2439   )*
2440   try {
2441     token2 = <SEMICOLON>
2442     {pos = token2.sourceEnd+1;}
2443   } catch (ParseException e) {
2444     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2445     errorLevel   = ERROR;
2446     errorStart = pos;
2447     errorEnd   = pos;
2448     processParseExceptionDebug(e);
2449   }
2450     {
2451     final Variable[] variables = new Variable[vars.size()];
2452     vars.toArray(variables);
2453     global = new GlobalStatement(currentSegment,
2454                                  variables,
2455                                  token.sourceStart,
2456                                  pos);
2457     currentSegment.add(global);
2458     return global;}
2459 }
2460
2461 StaticStatement StaticStatement() :
2462 {
2463   final ArrayList vars = new ArrayList();
2464   VariableDeclaration expr;
2465   final Token token, token2;
2466   int pos;
2467 }
2468 {
2469   token = <STATIC> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2470   (
2471     <COMMA> expr = VariableDeclarator() {vars.add(expr);pos = expr.sourceEnd+1;}
2472   )*
2473   try {
2474     token2 = <SEMICOLON>
2475     {pos = token2.sourceEnd+1;}
2476   } catch (ParseException e) {
2477     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. a ';' was expected";
2478     errorLevel   = ERROR;
2479     errorStart = pos;
2480     errorEnd   = pos;
2481     processParseException(e);
2482   }
2483     {
2484     final VariableDeclaration[] variables = new VariableDeclaration[vars.size()];
2485     vars.toArray(variables);
2486     return new StaticStatement(variables,
2487                                token.sourceStart,
2488                                pos);}
2489 }
2490
2491 LabeledStatement LabeledStatement() :
2492 {
2493   final Token label;
2494   final Statement statement;
2495 }
2496 {
2497   label = <IDENTIFIER> <COLON> statement = Statement()
2498   {return new LabeledStatement(label.image,statement,label.sourceStart,statement.sourceEnd);}
2499 }
2500
2501 /**
2502  * A Block is
2503  * {
2504  * statements
2505  * }.
2506  * @return a block
2507  */
2508 Block Block() :
2509 {
2510   final ArrayList list = new ArrayList();
2511   Statement statement;
2512   final Token token, token2;
2513   int pos,start;
2514 }
2515 {
2516   try {
2517     token = <LBRACE>
2518     {pos = token.sourceEnd+1;start=token.sourceStart;}
2519   } catch (ParseException e) {
2520     errorMessage = "'{' expected";
2521     errorLevel   = ERROR;
2522     pos = PHPParser.token.sourceEnd+1;
2523     start=pos;
2524     errorStart = pos;
2525     errorEnd   = pos;
2526     processParseExceptionDebug(e);
2527   }
2528   ( statement = BlockStatement() {list.add(statement);pos = statement.sourceEnd+1;}
2529   | statement = htmlBlock()      {if (statement != null) {
2530                                     list.add(statement);
2531                                     pos = statement.sourceEnd+1;
2532                                   }
2533                                   pos = PHPParser.token.sourceEnd+1;
2534                                  }
2535   )*
2536   try {
2537     token2 = <RBRACE>
2538     {pos = token2.sourceEnd+1;}
2539   } catch (ParseException e) {
2540     errorMessage = "unexpected token : '"+ e.currentToken.image +"', '}' expected";
2541     errorLevel   = ERROR;
2542     errorStart = pos;
2543     errorEnd   = pos;
2544     processParseExceptionDebug(e);
2545   }
2546   {
2547   final Statement[] statements = new Statement[list.size()];
2548   list.toArray(statements);
2549   return new Block(statements,start,pos);}
2550 }
2551
2552 Statement BlockStatement() :
2553 {
2554   final Statement statement;
2555 }
2556 {
2557   try {
2558     statement = Statement()         {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2559                                      return statement;}
2560   } catch (ParseException e) {
2561     errorMessage = "unexpected token : '"+ e.currentToken.image +"', a statement was expected";
2562     errorLevel   = ERROR;
2563     errorStart = e.currentToken.sourceStart;
2564     errorEnd   = e.currentToken.sourceEnd;
2565     throw e;
2566   }
2567 | statement = ClassDeclaration()  {return statement;}
2568 | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement);
2569                                    currentSegment.add((MethodDeclaration) statement);
2570                                    ((MethodDeclaration) statement).analyzeCode();
2571                                    return statement;}
2572 }
2573
2574 /**
2575  * A Block statement that will not contain any 'break'
2576  */
2577 Statement BlockStatementNoBreak() :
2578 {
2579   final Statement statement;
2580 }
2581 {
2582   statement = StatementNoBreak()  {return statement;}
2583 | statement = ClassDeclaration()  {return statement;}
2584 | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement);
2585                                    ((MethodDeclaration) statement).analyzeCode();
2586                                    return statement;}
2587 }
2588
2589 /**
2590  * used only by ForInit()
2591  */
2592 Expression[] LocalVariableDeclaration() :
2593 {
2594   final ArrayList list = new ArrayList();
2595   Expression var;
2596 }
2597 {
2598   var = Expression()
2599   {list.add(var);}
2600   ( <COMMA> var = Expression() {list.add(var);})*
2601   {
2602     final Expression[] vars = new Expression[list.size()];
2603     list.toArray(vars);
2604     return vars;
2605   }
2606 }
2607
2608 /**
2609  * used only by LocalVariableDeclaration().
2610  */
2611 VariableDeclaration LocalVariableDeclarator() :
2612 {
2613   final Variable varName;
2614   Expression initializer = null;
2615 }
2616 {
2617   varName = Variable() [ <ASSIGN> initializer = Expression() ]
2618   {
2619    if (initializer == null) {
2620     return new VariableDeclaration(currentSegment,
2621                                    varName,
2622                                    varName.sourceStart,
2623                                    varName.sourceEnd);
2624    }
2625     return new VariableDeclaration(currentSegment,
2626                                    varName,
2627                                    initializer,
2628                                    VariableDeclaration.EQUAL,
2629                                    varName.sourceStart);
2630   }
2631 }
2632
2633 EmptyStatement EmptyStatement() :
2634 {
2635   final Token token;
2636 }
2637 {
2638   token = <SEMICOLON>
2639   {return new EmptyStatement(token.sourceStart,token.sourceEnd);}
2640 }
2641
2642 /**
2643  * used only by StatementExpressionList() which is used only by ForInit() and ForStatement()
2644  */
2645 Expression StatementExpression() :
2646 {
2647   final Expression expr;
2648   final Token operator;
2649 }
2650 {
2651   expr = PreIncDecExpression() {return expr;}
2652 |
2653   expr = PrimaryExpression()
2654   [ operator = <PLUS_PLUS> {return new PostfixedUnaryExpression(expr,
2655                                                                 OperatorIds.PLUS_PLUS,
2656                                                                 operator.sourceEnd);}
2657   | operator = <MINUS_MINUS> {return new PostfixedUnaryExpression(expr,
2658                                                                   OperatorIds.MINUS_MINUS,
2659                                                                   operator.sourceEnd);}
2660   ]
2661   {return expr;}
2662 }
2663
2664 SwitchStatement SwitchStatement() :
2665 {
2666   Expression variable;
2667   final AbstractCase[] cases;
2668   final Token switchToken,lparenToken,rparenToken;
2669   int pos;
2670 }
2671 {
2672   switchToken = <SWITCH> {pos = switchToken.sourceEnd+1;}
2673   try {
2674     lparenToken = <LPAREN>
2675     {pos = lparenToken.sourceEnd+1;}
2676   } catch (ParseException e) {
2677     errorMessage = "'(' expected after 'switch'";
2678     errorLevel   = ERROR;
2679     errorStart = pos;
2680     errorEnd   = pos;
2681     processParseExceptionDebug(e);
2682   }
2683   try {
2684     variable = Expression() {pos = variable.sourceEnd+1;}
2685   } catch (ParseException e) {
2686     if (errorMessage != null) {
2687       throw e;
2688     }
2689     errorMessage = "expression expected";
2690     errorLevel   = ERROR;
2691     errorStart = pos;
2692     errorEnd   = pos;
2693     processParseExceptionDebug(e);
2694     variable = new ConstantIdentifier(SYNTAX_ERROR_CHAR,pos,pos);
2695   }
2696   try {
2697     rparenToken = <RPAREN> {pos = rparenToken.sourceEnd+1;}
2698   } catch (ParseException e) {
2699     errorMessage = "')' expected";
2700     errorLevel   = ERROR;
2701     errorStart = pos;
2702     errorEnd   = pos;
2703     processParseExceptionDebug(e);
2704   }
2705   (  cases = switchStatementBrace()
2706    | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
2707   {return new SwitchStatement(variable,
2708                               cases,
2709                               switchToken.sourceStart,
2710                               PHPParser.token.sourceEnd);}
2711 }
2712
2713 AbstractCase[] switchStatementBrace() :
2714 {
2715   AbstractCase cas;
2716   final ArrayList cases = new ArrayList();
2717   Token token;
2718   int pos;
2719 }
2720 {
2721   token = <LBRACE> {pos = token.sourceEnd;}
2722  ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2723   try {
2724     token = <RBRACE>
2725     {pos = token.sourceEnd;}
2726   } catch (ParseException e) {
2727     errorMessage = "'}' expected";
2728     errorLevel   = ERROR;
2729     errorStart = pos+1;
2730     errorEnd   = pos+1;
2731     processParseExceptionDebug(e);
2732   }
2733   {
2734     final AbstractCase[] abcase = new AbstractCase[cases.size()];
2735     cases.toArray(abcase);
2736     return abcase;
2737   }
2738 }
2739
2740 /**
2741  * A Switch statement with : ... endswitch;
2742  * @param start the begin offset of the switch
2743  * @param end the end offset of the switch
2744  */
2745 AbstractCase[] switchStatementColon(final int start, final int end) :
2746 {
2747   AbstractCase cas;
2748   final ArrayList cases = new ArrayList();
2749   Token token;
2750   int pos;
2751 }
2752 {
2753   token = <COLON> {pos = token.sourceEnd;}
2754   {try {
2755   setMarker(fileToParse,
2756             "Ugly syntax detected, you should switch () {...} instead of switch (): ... enswitch;",
2757             start,
2758             end,
2759             INFO,
2760             "Line " + token.beginLine);
2761   } catch (CoreException e) {
2762     PHPeclipsePlugin.log(e);
2763   }}
2764   ( cas = switchLabel0() {cases.add(cas);pos = cas.sourceEnd;})*
2765   try {
2766     token = <ENDSWITCH> {pos = token.sourceEnd;}
2767   } catch (ParseException e) {
2768     errorMessage = "'endswitch' expected";
2769     errorLevel   = ERROR;
2770     errorStart = pos+1;
2771     errorEnd   = pos+1;
2772     processParseExceptionDebug(e);
2773   }
2774   try {
2775     token = <SEMICOLON> {pos = token.sourceEnd;}
2776   } catch (ParseException e) {
2777     errorMessage = "';' expected after 'endswitch' keyword";
2778     errorLevel   = ERROR;
2779     errorStart = pos+1;
2780     errorEnd   = pos+1;
2781     processParseExceptionDebug(e);
2782   }
2783   {
2784     final AbstractCase[] abcase = new AbstractCase[cases.size()];
2785     cases.toArray(abcase);
2786     return abcase;
2787   }
2788 }
2789
2790 AbstractCase switchLabel0() :
2791 {
2792   final Expression expr;
2793   Statement statement;
2794   final ArrayList stmts = new ArrayList();
2795   final Token token = PHPParser.token;
2796 }
2797 {
2798   expr = SwitchLabel()
2799   ( statement = BlockStatementNoBreak() {stmts.add(statement);}
2800   | statement = htmlBlock()             {if (statement != null) {stmts.add(statement);}}
2801   | statement = BreakStatement()        {stmts.add(statement);})*
2802   //[ statement = BreakStatement()        {stmts.add(statement);}]
2803   {
2804     final int listSize = stmts.size();
2805     final Statement[] stmtsArray = new Statement[listSize];
2806     stmts.toArray(stmtsArray);
2807     if (expr == null) {//it's a default
2808       return new DefaultCase(stmtsArray,token.sourceStart,stmtsArray[listSize-1].sourceEnd);
2809     }
2810     if (listSize != 0) {
2811       return new Case(expr,stmtsArray,expr.sourceStart,stmtsArray[listSize-1].sourceEnd);
2812     } else {
2813       return new Case(expr,stmtsArray,expr.sourceStart,expr.sourceEnd);
2814     }
2815   }
2816 }
2817
2818 /**
2819  * A SwitchLabel.
2820  * case Expression() :
2821  * default :
2822  * @return the if it was a case and null if not
2823  */
2824 Expression SwitchLabel() :
2825 {
2826   final Expression expr;
2827 }
2828 {
2829   token = <CASE>
2830   try {
2831     expr = Expression()
2832   } catch (ParseException e) {
2833     if (errorMessage != null) throw e;
2834     errorMessage = "expression expected after 'case' keyword";
2835     errorLevel   = ERROR;
2836     errorStart = token.sourceEnd +1;
2837     errorEnd   = token.sourceEnd +1;
2838     throw e;
2839   }
2840   try {
2841     token = <COLON>
2842     {return expr;}
2843   } catch (ParseException e) {
2844     errorMessage = "':' expected after case expression";
2845     errorLevel   = ERROR;
2846     errorStart = expr.sourceEnd+1;
2847     errorEnd   = expr.sourceEnd+1;
2848     processParseExceptionDebug(e);
2849   }
2850 |
2851   token = <_DEFAULT>
2852   try {
2853     <COLON>
2854     {return null;}
2855   } catch (ParseException e) {
2856     errorMessage = "':' expected after 'default' keyword";
2857     errorLevel   = ERROR;
2858     errorStart = token.sourceEnd+1;
2859     errorEnd   = token.sourceEnd+1;
2860     processParseExceptionDebug(e);
2861   }
2862 }
2863
2864 Break BreakStatement() :
2865 {
2866   Expression expression = null;
2867   final Token token, token2;
2868   int pos;
2869 }
2870 {
2871   token = <BREAK> {pos = token.sourceEnd+1;}
2872   [ expression = Expression() {pos = expression.sourceEnd+1;}]
2873   try {
2874     token2 = <SEMICOLON>
2875     {pos = token2.sourceEnd;}
2876   } catch (ParseException e) {
2877     errorMessage = "';' expected after 'break' keyword";
2878     errorLevel   = ERROR;
2879     errorStart = pos;
2880     errorEnd   = pos;
2881     processParseExceptionDebug(e);
2882   }
2883   {return new Break(expression, token.sourceStart, pos);}
2884 }
2885
2886 IfStatement IfStatement() :
2887 {
2888   final Expression condition;
2889   final IfStatement ifStatement;
2890   Token token;
2891 }
2892 {
2893   token = <IF> condition = Condition("if")
2894   ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
2895   {return ifStatement;}
2896 }
2897
2898
2899 Expression Condition(final String keyword) :
2900 {
2901   final Expression condition;
2902 }
2903 {
2904   try {
2905     <LPAREN>
2906   } catch (ParseException e) {
2907     errorMessage = "'(' expected after " + keyword + " keyword";
2908     errorLevel   = ERROR;
2909     errorStart = PHPParser.token.sourceEnd + 1;
2910     errorEnd   = PHPParser.token.sourceEnd + 1;
2911     processParseExceptionDebug(e);
2912   }
2913   condition = Expression()
2914   try {
2915      <RPAREN>
2916   } catch (ParseException e) {
2917     errorMessage = "')' expected after " + keyword + " keyword";
2918     errorLevel   = ERROR;
2919     errorStart = condition.sourceEnd+1;
2920     errorEnd   = condition.sourceEnd+1;
2921     processParseExceptionDebug(e);
2922   }
2923   {return condition;}
2924 }
2925
2926 IfStatement IfStatement0(final Expression condition, final int start,final int end) :
2927 {
2928   Statement statement;
2929   final Statement stmt;
2930   final Statement[] statementsArray;
2931   ElseIf elseifStatement;
2932   Else elseStatement = null;
2933   final ArrayList stmts;
2934   final ArrayList elseIfList = new ArrayList();
2935   final ElseIf[] elseIfs;
2936   int pos = SimpleCharStream.getPosition();
2937   final int endStatements;
2938 }
2939 {
2940   <COLON>
2941   {stmts = new ArrayList();}
2942   (  statement = Statement() {stmts.add(statement);}
2943    | statement = htmlBlock() {if (statement != null) {stmts.add(statement);}})*
2944    {endStatements = SimpleCharStream.getPosition();}
2945    (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})*
2946    [elseStatement = ElseStatementColon()]
2947
2948   {try {
2949   setMarker(fileToParse,
2950             "Ugly syntax detected, you should if () {...} instead of if (): ... endif;",
2951             start,
2952             end,
2953             INFO,
2954             "Line " + token.beginLine);
2955   } catch (CoreException e) {
2956     PHPeclipsePlugin.log(e);
2957   }}
2958   try {
2959     <ENDIF>
2960   } catch (ParseException e) {
2961     errorMessage = "'endif' expected";
2962     errorLevel   = ERROR;
2963     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2964     errorEnd   = SimpleCharStream.getPosition() + 1;
2965     throw e;
2966   }
2967   try {
2968     <SEMICOLON>
2969   } catch (ParseException e) {
2970     errorMessage = "';' expected after 'endif' keyword";
2971     errorLevel   = ERROR;
2972     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
2973     errorEnd   = SimpleCharStream.getPosition() + 1;
2974     throw e;
2975   }
2976     {
2977     elseIfs = new ElseIf[elseIfList.size()];
2978     elseIfList.toArray(elseIfs);
2979     if (stmts.size() == 1) {
2980       return new IfStatement(condition,
2981                              (Statement) stmts.get(0),
2982                               elseIfs,
2983                               elseStatement,
2984                               pos,
2985                               SimpleCharStream.getPosition());
2986     } else {
2987       statementsArray = new Statement[stmts.size()];
2988       stmts.toArray(statementsArray);
2989       return new IfStatement(condition,
2990                              new Block(statementsArray,pos,endStatements),
2991                              elseIfs,
2992                              elseStatement,
2993                              pos,
2994                              SimpleCharStream.getPosition());
2995     }
2996     }
2997
2998 |
2999   (stmt = Statement() | stmt = htmlBlock())
3000   ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})*
3001   [ LOOKAHEAD(1)
3002     <ELSE>
3003     try {
3004       {pos = SimpleCharStream.getPosition();}
3005       statement = Statement()
3006       {elseStatement = new Else(statement,pos,SimpleCharStream.getPosition());}
3007     } catch (ParseException e) {
3008       if (errorMessage != null) {
3009         throw e;
3010       }
3011       errorMessage = "unexpected token '"+e.currentToken.next.image+"', a statement was expected";
3012       errorLevel   = ERROR;
3013       errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3014       errorEnd   = SimpleCharStream.getPosition() + 1;
3015       throw e;
3016     }
3017   ]
3018   {
3019     elseIfs = new ElseIf[elseIfList.size()];
3020     elseIfList.toArray(elseIfs);
3021     return new IfStatement(condition,
3022                            stmt,
3023                            elseIfs,
3024                            elseStatement,
3025                            pos,
3026                            SimpleCharStream.getPosition());}
3027 }
3028
3029 ElseIf ElseIfStatementColon() :
3030 {
3031   final Expression condition;
3032   Statement statement;
3033   final ArrayList list = new ArrayList();
3034   final Token elseifToken;
3035 }
3036 {
3037   elseifToken = <ELSEIF> condition = Condition("elseif")
3038   <COLON> (  statement = Statement() {list.add(statement);}
3039            | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3040   {
3041   final int sizeList = list.size();
3042   final Statement[] stmtsArray = new Statement[sizeList];
3043   list.toArray(stmtsArray);
3044   return new ElseIf(condition,stmtsArray ,
3045                     elseifToken.sourceStart,
3046                     stmtsArray[sizeList-1].sourceEnd);}
3047 }
3048
3049 Else ElseStatementColon() :
3050 {
3051   Statement statement;
3052   final ArrayList list = new ArrayList();
3053   final Token elseToken;
3054 }
3055 {
3056   elseToken = <ELSE> <COLON> (  statement = Statement() {list.add(statement);}
3057                   | statement = htmlBlock() {if (statement != null) {list.add(statement);}})*
3058   {
3059   final int sizeList = list.size();
3060   final Statement[] stmtsArray = new Statement[sizeList];
3061   list.toArray(stmtsArray);
3062   return new Else(stmtsArray,elseToken.sourceStart,stmtsArray[sizeList-1].sourceEnd);}
3063 }
3064
3065 ElseIf ElseIfStatement() :
3066 {
3067   final Expression condition;
3068   //final Statement statement;
3069   final Token elseifToken;
3070   final Statement[] statement = new Statement[1];
3071 }
3072 {
3073   elseifToken = <ELSEIF> condition = Condition("elseif") statement[0] = Statement()
3074   {
3075   return new ElseIf(condition,statement,elseifToken.sourceStart,statement[0].sourceEnd);}
3076 }
3077
3078 WhileStatement WhileStatement() :
3079 {
3080   final Expression condition;
3081   final Statement action;
3082   final Token whileToken;
3083 }
3084 {
3085   whileToken = <WHILE>
3086     condition = Condition("while")
3087     action    = WhileStatement0(whileToken.sourceStart,whileToken.sourceEnd)
3088     {return new WhileStatement(condition,action,whileToken.sourceStart,action.sourceEnd);}
3089 }
3090
3091 Statement WhileStatement0(final int start, final int end) :
3092 {
3093   Statement statement;
3094   final ArrayList stmts = new ArrayList();
3095   final int pos = SimpleCharStream.getPosition();
3096 }
3097 {
3098   <COLON> (statement = Statement() {stmts.add(statement);})*
3099   {try {
3100   setMarker(fileToParse,
3101             "Ugly syntax detected, you should while () {...} instead of while (): ... endwhile;",
3102             start,
3103             end,
3104             INFO,
3105             "Line " + token.beginLine);
3106   } catch (CoreException e) {
3107     PHPeclipsePlugin.log(e);
3108   }}
3109   try {
3110     <ENDWHILE>
3111   } catch (ParseException e) {
3112     errorMessage = "'endwhile' expected";
3113     errorLevel   = ERROR;
3114     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3115     errorEnd   = SimpleCharStream.getPosition() + 1;
3116     throw e;
3117   }
3118   try {
3119     <SEMICOLON>
3120     {
3121     final Statement[] stmtsArray = new Statement[stmts.size()];
3122     stmts.toArray(stmtsArray);
3123     return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
3124   } catch (ParseException e) {
3125     errorMessage = "';' expected after 'endwhile' keyword";
3126     errorLevel   = ERROR;
3127     errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
3128     errorEnd   = SimpleCharStream.getPosition() + 1;
3129     throw e;
3130   }
3131 |
3132   statement = Statement()
3133   {return statement;}
3134 }
3135
3136 DoStatement DoStatement() :
3137 {
3138   final Statement action;
3139   final Expression condition;
3140   final Token token;
3141   Token token2 = null;
3142 }
3143 {
3144   token = <DO> action = Statement() <WHILE> condition = Condition("while")
3145   try {
3146     token2 = <SEMICOLON>
3147   } catch (ParseException e) {
3148     errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
3149     errorLevel   = ERROR;
3150     errorStart = condition.sourceEnd+1;
3151     errorEnd   = condition.sourceEnd+1;
3152     processParseExceptionDebug(e);
3153   }
3154   {
3155     if (token2 == null) {
3156       return new DoStatement(condition,action,token.sourceStart,condition.sourceEnd);
3157     }
3158     return new DoStatement(condition,action,token.sourceStart,token2.sourceEnd);
3159   }
3160 }
3161
3162 ForeachStatement ForeachStatement() :
3163 {
3164   Statement statement = null;
3165   Expression expression = null;
3166   ArrayVariableDeclaration variable = null;
3167   Token foreachToken;
3168   Token lparenToken = null;
3169   Token asToken = null;
3170   Token rparenToken = null;
3171   int pos;
3172 }
3173 {
3174   foreachToken = <FOREACH>
3175   try {
3176     lparenToken = <LPAREN>
3177     {pos = lparenToken.sourceEnd+1;}
3178   } catch (ParseException e) {
3179     errorMessage = "'(' expected after 'foreach' keyword";
3180     errorLevel   = ERROR;
3181     errorStart = foreachToken.sourceEnd+1;
3182     errorEnd   = foreachToken.sourceEnd+1;
3183     processParseExceptionDebug(e);
3184     {pos = foreachToken.sourceEnd+1;}
3185   }
3186   try {
3187     expression = Expression()
3188     {pos = expression.sourceEnd+1;}
3189   } catch (ParseException e) {
3190     errorMessage = "variable expected";
3191     errorLevel   = ERROR;
3192     errorStart = pos;
3193     errorEnd   = pos;
3194     processParseExceptionDebug(e);
3195   }
3196   try {
3197     asToken = <AS>
3198     {pos = asToken.sourceEnd+1;}
3199   } catch (ParseException e) {
3200     errorMessage = "'as' expected";
3201     errorLevel   = ERROR;
3202     errorStart = pos;
3203     errorEnd   = pos;
3204     processParseExceptionDebug(e);
3205   }
3206   try {
3207     variable = ArrayVariable()
3208     {pos = variable.sourceEnd+1;}
3209   } catch (ParseException e) {
3210     if (errorMessage != null) throw e;
3211     errorMessage = "variable expected";
3212     errorLevel   = ERROR;
3213     errorStart = pos;
3214     errorEnd   = pos;
3215     processParseExceptionDebug(e);
3216   }
3217   try {
3218     rparenToken = <RPAREN>
3219     {pos = rparenToken.sourceEnd+1;}
3220   } catch (ParseException e) {
3221     errorMessage = "')' expected after 'foreach' keyword";
3222     errorLevel   = ERROR;
3223     errorStart = pos;
3224     errorEnd   = pos;
3225     processParseExceptionDebug(e);
3226   }
3227   try {
3228     statement = Statement()
3229     {pos = rparenToken.sourceEnd+1;}
3230   } catch (ParseException e) {
3231     if (errorMessage != null) throw e;
3232     errorMessage = "statement expected";
3233     errorLevel   = ERROR;
3234     errorStart = pos;
3235     errorEnd   = pos;
3236     processParseExceptionDebug(e);
3237   }
3238   {return new ForeachStatement(expression,
3239                                variable,
3240                                statement,
3241                                foreachToken.sourceStart,
3242                                statement.sourceEnd);}
3243
3244 }
3245
3246 /**
3247  * a for declaration.
3248  * @return a node representing the for statement
3249  */
3250 ForStatement ForStatement() :
3251 {
3252 final Token token,tokenEndFor,token2,tokenColon;
3253 int pos;
3254 Expression[] initializations = null;
3255 Expression condition = null;
3256 Expression[] increments = null;
3257 Statement action;
3258 final ArrayList list = new ArrayList();
3259 }
3260 {
3261   token = <FOR>
3262   try {
3263     <LPAREN>
3264   } catch (ParseException e) {
3265     errorMessage = "'(' expected after 'for' keyword";
3266     errorLevel   = ERROR;
3267     errorStart = token.sourceEnd;
3268     errorEnd   = token.sourceEnd +1;
3269     processParseExceptionDebug(e);
3270   }
3271      [ initializations = ForInit() ] <SEMICOLON>
3272      [ condition = Expression() ] <SEMICOLON>
3273      [ increments = StatementExpressionList() ] <RPAREN>
3274     (
3275       action = Statement()
3276       {return new ForStatement(initializations,
3277                                condition,
3278                                increments,
3279                                action,
3280                                token.sourceStart,
3281                                action.sourceEnd);}
3282     |
3283       tokenColon = <COLON> {pos = tokenColon.sourceEnd+1;}
3284       (action = Statement() {list.add(action);pos = action.sourceEnd+1;})*
3285       {
3286         try {
3287         setMarker(fileToParse,
3288                   "Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
3289                   token.sourceStart,
3290                   token.sourceEnd,
3291                   INFO,
3292                   "Line " + token.beginLine);
3293         } catch (CoreException e) {
3294           PHPeclipsePlugin.log(e);
3295         }
3296       }
3297       try {
3298         tokenEndFor = <ENDFOR>
3299         {pos = tokenEndFor.sourceEnd+1;}
3300       } catch (ParseException e) {
3301         errorMessage = "'endfor' expected";
3302         errorLevel   = ERROR;
3303         errorStart = pos;
3304         errorEnd   = pos;
3305         processParseExceptionDebug(e);
3306       }
3307       try {
3308         token2 = <SEMICOLON>
3309         {pos = token2.sourceEnd+1;}
3310       } catch (ParseException e) {
3311         errorMessage = "';' expected after 'endfor' keyword";
3312         errorLevel   = ERROR;
3313         errorStart = pos;
3314         errorEnd   = pos;
3315         processParseExceptionDebug(e);
3316       }
3317       {
3318       final Statement[] stmtsArray = new Statement[list.size()];
3319       list.toArray(stmtsArray);
3320       return new ForStatement(initializations,
3321                               condition,
3322                               increments,
3323                               new Block(stmtsArray,
3324                                         stmtsArray[0].sourceStart,
3325                                         stmtsArray[stmtsArray.length-1].sourceEnd),
3326                               token.sourceStart,
3327                               pos);}
3328     )
3329 }
3330
3331 Expression[] ForInit() :
3332 {
3333   final Expression[] exprs;
3334 }
3335 {
3336   LOOKAHEAD(LocalVariableDeclaration())
3337   exprs = LocalVariableDeclaration()
3338   {return exprs;}
3339 |
3340   exprs = StatementExpressionList()
3341   {return exprs;}
3342 }
3343
3344 Expression[] StatementExpressionList() :
3345 {
3346   final ArrayList list = new ArrayList();
3347   final Expression expr;
3348 }
3349 {
3350   expr = Expression()   {list.add(expr);}
3351   (<COMMA> Expression() {list.add(expr);})*
3352   {
3353     final Expression[] exprsArray = new Expression[list.size()];
3354     list.toArray(exprsArray);
3355     return exprsArray;
3356   }
3357 }
3358
3359 Continue ContinueStatement() :
3360 {
3361   Expression expr = null;
3362   final Token token;
3363   Token token2 = null;
3364 }
3365 {
3366   token = <CONTINUE> [ expr = Expression() ]
3367   try {
3368     token2 = <SEMICOLON>
3369   } catch (ParseException e) {
3370     errorMessage = "';' expected after 'continue' statement";
3371     errorLevel   = ERROR;
3372     if (expr == null) {
3373       errorStart = token.sourceEnd+1;
3374       errorEnd   = token.sourceEnd+1;
3375     } else {
3376       errorStart = expr.sourceEnd+1;
3377       errorEnd   = expr.sourceEnd+1;
3378     }
3379     processParseExceptionDebug(e);
3380   }
3381   {
3382     if (token2 == null) {
3383       if (expr == null) {
3384         return new Continue(expr,token.sourceStart,token.sourceEnd);
3385       }
3386       return new Continue(expr,token.sourceStart,expr.sourceEnd);
3387     }
3388     return new Continue(expr,token.sourceStart,token2.sourceEnd);
3389   }
3390 }
3391
3392 ReturnStatement ReturnStatement() :
3393 {
3394   Expression expr = null;
3395   final Token token;
3396   Token token2 = null;
3397 }
3398 {
3399   token = <RETURN> [ expr = Expression() ]
3400   try {
3401     token2 = <SEMICOLON>
3402   } catch (ParseException e) {
3403     errorMessage = "';' expected after 'return' statement";
3404     errorLevel   = ERROR;
3405     if (expr == null) {
3406       errorStart = token.sourceEnd+1;
3407       errorEnd   = token.sourceEnd+1;
3408     } else {
3409       errorStart = expr.sourceEnd+1;
3410       errorEnd   = expr.sourceEnd+1;
3411     }
3412     processParseExceptionDebug(e);
3413   }
3414   {
3415     if (token2 == null) {
3416       if (expr == null) {
3417         return new ReturnStatement(expr,token.sourceStart,token.sourceEnd);
3418       }
3419       return new ReturnStatement(expr,token.sourceStart,expr.sourceEnd);
3420     }
3421     return new ReturnStatement(expr,token.sourceStart,token2.sourceEnd);
3422   }
3423 }
3424