1 /**********************************************************************
2 Copyright (c) 2002 Klaus Hartlage - www.eclipseproject.de
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 Klaus Hartlage - www.eclipseproject.de
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
13 import java.text.MessageFormat;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Hashtable;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPKeywords;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.ui.texteditor.MarkerUtilities;
28 public class PHPParser extends PHPKeywords {
29 // strings for external parser call
30 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
31 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
33 public static final int ERROR = 2;
34 public static final int WARNING = 1;
35 public static final int INFO = 0;
36 private IFile fileToParse;
37 private ArrayList phpList;
39 private int currentPHPString;
40 private boolean phpEnd;
42 private static HashMap keywordMap = null;
50 // row counter for syntax errors:
52 // column counter for syntax errors:
63 final static int TT_EOF = 0;
64 final static int TT_UNDEFINED = 1;
66 final static int TT_MOD = 30;
67 final static int TT_NOT = 31;
68 final static int TT_DOT = 32;
69 final static int TT_POW = 33;
70 final static int TT_DIV = 34;
71 final static int TT_MULTIPLY = 35;
72 final static int TT_SUBTRACT = 36;
73 final static int TT_ADD = 37;
74 final static int TT_EQUAL = 38;
75 final static int TT_UNEQUAL = 39;
76 final static int TT_GREATER = 40;
77 final static int TT_GREATEREQUAL = 41;
78 final static int TT_LESS = 42;
79 final static int TT_LESSEQUAL = 43;
80 final static int TT_AND = 44;
81 final static int TT_OR = 45;
82 final static int TT_HASH = 46;
83 final static int TT_DDOT = 47;
84 final static int TT_DOTASSIGN = 48;
86 final static int TT_ASSIGN = 49;
87 final static int TT_REF = 50;
88 final static int TT_FOREACH = 51;
89 final static int TT_AMPERSAND = 52;
90 final static int TT_DOLLARLISTOPEN = 53;
91 final static int TT_TILDE = 54;
92 final static int TT_TILDEASSIGN = 55;
93 final static int TT_MODASSIGN = 56;
94 final static int TT_POWASSIGN = 57;
95 final static int TT_RSHIFTASSIGN = 58;
96 final static int TT_LSHIFTASSIGN = 59;
97 final static int TT_ANDASSIGN = 60;
98 final static int TT_QUESTIONMARK = 61;
99 final static int TT_DDOT2 = 62;
100 final static int TT_AT = 63;
101 // final static int TT_HEREDOC = 64;
103 final static int TT_DOLLAROPEN = 127;
104 final static int TT_ARGOPEN = 128;
105 final static int TT_ARGCLOSE = 129;
106 final static int TT_LISTOPEN = 130;
107 final static int TT_LISTCLOSE = 131;
108 final static int TT_PARTOPEN = 132;
109 final static int TT_PARTCLOSE = 133;
110 final static int TT_COMMA = 134;
112 final static int TT_STRING = 136;
113 final static int TT_IDENTIFIER = 138;
114 final static int TT_DIGIT = 139;
115 final static int TT_SEMICOLON = 140;
116 final static int TT_SLOT = 141;
117 final static int TT_SLOTSEQUENCE = 142;
118 final static int TT_DECREMENT = 144;
119 final static int TT_INCREMENT = 145;
120 final static int TT_ADDTO = 146;
121 final static int TT_DIVIDEBY = 147;
122 final static int TT_SUBTRACTFROM = 148;
123 final static int TT_TIMESBY = 149;
124 final static int TT_VARIABLE = 150;
125 final static int TT_INT_NUMBER = 151;
126 final static int TT_DOUBLE_NUMBER = 152;
127 final static int TT_INTERPOLATED_STRING = 153;
128 final static int TT_STRING_CONSTANT = 154;
130 final static int TT_LSHIFT = 155;
131 final static int TT_RSHIFT = 156;
132 final static int TT_EX_EQUAL = 157;
133 final static int TT_EX_UNEQUAL = 158;
134 final static int TT_LINE = 159;
135 // final static int TT_AT = 153; // @
140 *@param sess Description of Parameter
143 public PHPParser(IFile fileToParse) {
144 if (keywordMap == null) {
145 keywordMap = new HashMap();
146 for (int i = 0; i < PHP_KEYWORS.length; i++) {
147 keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i]));
150 this.currentPHPString = 0;
151 this.fileToParse = fileToParse;
157 this.columnCount = 0;
164 * Create marker for the parse error
166 private void setMarker(String message, int lineNumber, int errorLevel) throws CoreException {
167 setMarker(fileToParse, message, lineNumber, errorLevel);
170 public static void setMarker(IFile file, String message, int lineNumber, int errorLevel) throws CoreException {
172 Hashtable attributes = new Hashtable();
173 MarkerUtilities.setMessage(attributes, message);
174 switch (errorLevel) {
176 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
179 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
182 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
185 MarkerUtilities.setLineNumber(attributes, lineNumber);
186 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
189 private void throwSyntaxError(String error) {
191 if (str.length() < chIndx) {
194 // read until end-of-line
196 while (str.length() > eol) {
197 ch = str.charAt(eol++);
203 throw new SyntaxError(rowCount, chIndx - columnCount + 1, str.substring(columnCount, eol), error);
206 private void throwSyntaxError(String error, int startRow) {
208 throw new SyntaxError(startRow, 0, " ", error);
212 * Method Declaration.
216 private void getChar() {
217 if (str.length() > chIndx) {
218 ch = str.charAt(chIndx++);
223 chIndx = str.length() + 1;
230 * gets the next token from input
232 private void getNextToken() throws CoreException {
235 while (str.length() > chIndx) {
236 ch = str.charAt(chIndx++);
237 token = TT_UNDEFINED;
240 columnCount = chIndx;
241 continue; // while loop
243 if (str.length() == chIndx) {
246 if (!Character.isWhitespace(ch)) {
248 if (str.length() > chIndx) {
249 if (str.charAt(chIndx) == '{') {
251 token = TT_DOLLAROPEN;
258 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_') || (ch == '$')) {
262 if (ch >= '0' && ch <= '9') {
267 if (str.length() > chIndx) {
268 if (str.charAt(chIndx) == '/') {
270 // read comment until end of line:
271 while ((str.length() > chIndx) && (str.charAt(chIndx) != '\n')) {
275 } else if (str.charAt(chIndx) == '*') {
277 // multi line comment:
278 while (str.length() > chIndx) {
279 if (str.charAt(chIndx) == '*' && (str.length() > (chIndx + 1)) && str.charAt(chIndx + 1) == '/') {
283 ch = str.charAt(chIndx++);
286 columnCount = chIndx;
292 } else if (ch == '#') {
293 // read comment until end of line:
294 while ((str.length() > chIndx) && (str.charAt(chIndx) != '\n')) {
298 } else if (ch == '"') {
299 // read string until end
300 boolean openString = true;
301 while (str.length() > chIndx) {
302 ch = str.charAt(chIndx++);
304 if (str.length() > chIndx) {
305 ch = str.charAt(chIndx++);
307 } else if (ch == '"') {
310 } else if (ch == '\n') {
312 columnCount = chIndx;
316 throwSyntaxError("Open string character '\"' at end of file.");
318 token = TT_INTERPOLATED_STRING;
320 } else if (ch == '\'') {
321 // read string until end
322 boolean openString = true;
323 int startRow = rowCount;
324 while (str.length() > chIndx) {
325 ch = str.charAt(chIndx++);
327 if (str.length() > chIndx) {
328 ch = str.charAt(chIndx++);
330 } else if (ch == '\'') {
333 } else if (ch == '\n') {
335 columnCount = chIndx;
339 throwSyntaxError("Open string character \"'\" at end of file.", startRow);
341 token = TT_STRING_CONSTANT;
343 } else if (ch == '`') {
344 // read string until end
345 boolean openString = true;
346 int startRow = rowCount;
347 while (str.length() > chIndx) {
348 ch = str.charAt(chIndx++);
350 if (str.length() > chIndx) {
351 ch = str.charAt(chIndx++);
353 } else if (ch == '`') {
356 } else if (ch == '\n') {
358 columnCount = chIndx;
362 throwSyntaxError("Open string character \"`\" at end of file.", startRow);
364 setMarker("Other string delimiters prefered (found \"`\").", rowCount, PHPParser.INFO);
365 token = TT_STRING_CONSTANT;
384 token = TT_LISTCLOSE;
392 token = TT_PARTCLOSE;
400 token = TT_QUESTIONMARK;
407 if (str.length() > chIndx) {
408 if (str.charAt(chIndx) == '=') {
410 token = TT_TILDEASSIGN;
418 if (str.length() > chIndx) {
419 if (str.charAt(chIndx) == '=') {
421 token = TT_DOTASSIGN;
434 if (str.length() > chIndx) {
435 if (str.charAt(chIndx) == '=') {
437 token = TT_MODASSIGN;
444 token = TT_SEMICOLON;
449 if (str.length() > chIndx) {
450 if (str.charAt(chIndx) == '=') {
452 token = TT_POWASSIGN;
461 if (str.length() > chIndx) {
462 if (str.charAt(chIndx) == '=') {
473 if (str.length() > chIndx) {
474 if (str.charAt(chIndx) == '*') {
480 if (str.charAt(chIndx) == '=') {
491 if (str.length() > chIndx) {
492 if (str.charAt(chIndx) == '+') {
494 token = TT_INCREMENT;
498 if (str.charAt(chIndx) == '=') {
508 if (str.length() > chIndx) {
509 if (str.charAt(chIndx) == '-') {
511 token = TT_DECREMENT;
515 if (str.charAt(chIndx) == '=') {
517 token = TT_SUBTRACTFROM;
521 if (str.charAt(chIndx) == '>') {
533 if (str.length() > chIndx) {
534 ch = str.charAt(chIndx);
539 if (str.length() > chIndx) {
540 ch = str.charAt(chIndx);
561 if (str.length() > chIndx) {
562 if (str.charAt(chIndx) == '=') {
565 if (str.length() > chIndx) {
566 ch = str.charAt(chIndx);
570 token = TT_EX_UNEQUAL;
581 if (str.length() > chIndx) {
582 if (str.charAt(chIndx) == '=') {
584 token = TT_GREATEREQUAL;
587 if (str.charAt(chIndx) == '>') {
590 if (str.length() > chIndx) {
591 if (str.charAt(chIndx) == '=') {
593 token = TT_RSHIFTASSIGN;
605 if (str.length() > chIndx) {
606 if (str.charAt(chIndx) == '=') {
608 token = TT_LESSEQUAL;
612 if (str.charAt(chIndx) == '<') {
615 if (str.charAt(chIndx) == '<') {
617 int startRow = rowCount;
618 if (str.length() > chIndx) {
620 ch = str.charAt(++chIndx);
621 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_')) {
624 token = TT_STRING_CONSTANT;
625 while (str.length() > chIndx) {
626 ch = str.charAt(chIndx++);
628 if (str.length() >= chIndx + identifier.length()) {
629 if (str.substring(chIndx, chIndx + identifier.length()).equals(identifier)) {
630 chIndx += identifier.length();
638 throwSyntaxError("Open heredoc syntax after operator '<<<'.", startRow);
639 } else if (str.charAt(chIndx) == '=') {
641 token = TT_LSHIFTASSIGN;
653 if (str.length() > chIndx) {
654 if (str.charAt(chIndx) == '|') {
664 token = TT_AMPERSAND;
665 if (str.length() > chIndx) {
666 if (str.charAt(chIndx) == '&') {
671 if (str.charAt(chIndx) == '=') {
673 token = TT_ANDASSIGN;
682 if (str.length() > chIndx) {
683 if (str.charAt(chIndx) == ':') {
698 throwSyntaxError("unexpected character: '" + ch + "'");
701 if (token == TT_UNDEFINED) {
702 throwSyntaxError("token not found");
709 chIndx = str.length() + 1;
714 if (phpList != null) {
715 if (currentPHPString < phpList.size()) {
716 token = TT_UNDEFINED;
717 temp = (PHPString) phpList.get(currentPHPString++);
718 this.str = temp.getPHPString();
721 this.rowCount = temp.getLineNumber();
722 this.columnCount = 0;
726 token = TT_UNDEFINED;
732 private void getIdentifier() {
733 StringBuffer ident = new StringBuffer();
739 token = TT_IDENTIFIER;
742 while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || (ch == '_')) {
746 identifier = ident.toString();
749 Integer i = (Integer) keywordMap.get(identifier.toLowerCase());
751 token = i.intValue();
755 private void getNumber() {
756 StringBuffer inum = new StringBuffer();
765 // determine number conversions:
766 if (firstCh == '0') {
795 if (numFormat == 16) {
796 while ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
801 while ((ch >= '0' && ch <= '9') || (ch == '.') || (ch == 'E') || (ch == 'e')) {
802 if ((ch == '.') || (ch == 'E') || (ch == 'e')) {
803 if (ch == '.' && dFlag != ' ') {
806 if ((dFlag == 'E') || (dFlag == 'e')) {
812 if ((ch == '-') || (ch == '+')) {
826 doubleNumber = new Double(inum.toString());
827 token = TT_DOUBLE_NUMBER;
830 longNumber = Long.valueOf(inum.toString(), numFormat);
831 token = TT_INT_NUMBER;
835 } catch (Throwable e) {
836 throwSyntaxError("Number format error: " + inum.toString());
840 public void htmlParse(String input) {
842 int startLineNumber = 1;
846 boolean phpMode = false;
847 boolean phpFound = false;
849 phpList = new ArrayList();
850 currentPHPString = 0;
854 while (i < input.length()) {
855 ch = input.charAt(i++);
859 if ((!phpMode) && ch == '<') {
860 ch2 = input.charAt(i++);
862 ch2 = input.charAt(i++);
863 if (Character.isWhitespace(ch2)) {
868 startLineNumber = lineNumber;
870 } else if (ch2 == 'p') {
871 ch2 = input.charAt(i++);
873 ch2 = input.charAt(i++);
878 startLineNumber = lineNumber;
884 } else if (ch2 == 'P') {
885 ch2 = input.charAt(i++);
887 ch2 = input.charAt(i++);
892 startLineNumber = lineNumber;
905 if (ch == '/' && i < input.length()) {
906 ch2 = input.charAt(i++);
908 while (i < input.length()) {
909 ch = input.charAt(i++);
910 if (ch == '?' && i < input.length()) {
911 ch2 = input.charAt(i++);
915 phpList.add(new PHPString(input.substring(startIndex, i - 2), startLineNumber));
919 } else if (ch == '\n') {
925 } else if (ch2 == '*') {
926 // multi-line comment
927 while (i < input.length()) {
928 ch = input.charAt(i++);
931 } else if (ch == '*' && i < input.length()) {
932 ch2 = input.charAt(i++);
943 } else if (ch == '#') {
944 while (i < input.length()) {
945 ch = input.charAt(i++);
946 if (ch == '?' && i < input.length()) {
947 ch2 = input.charAt(i++);
951 phpList.add(new PHPString(input.substring(startIndex, i - 2), startLineNumber));
955 } else if (ch == '\n') {
961 } else if (ch == '"') {
963 while (i < input.length()) {
964 ch = input.charAt(i++);
967 } else if (ch == '\\' && i < input.length()) { // escape
969 } else if (ch == '"') {
974 } else if (ch == '\'') {
976 while (i < input.length()) {
977 ch = input.charAt(i++);
980 } else if (ch == '\\' && i < input.length()) { // escape
982 } else if (ch == '\'') {
989 if (ch == '?' && i < input.length()) {
990 ch2 = input.charAt(i++);
994 phpList.add(new PHPString(input.substring(startIndex, i - 2), startLineNumber));
1003 setMarker("No PHP source code found.", lineNumber, PHPParser.INFO);
1006 setMarker("Open PHP tag at end of file.", lineNumber, PHPParser.INFO);
1007 phpList.add(new PHPString(input.substring(startIndex, i - 2), startLineNumber));
1009 // for (int j=0;j<phpList.size();j++) {
1010 // String temp = ((PHPString)phpList.get(j)).getPHPString();
1011 // int startIndx = temp.length()-10;
1012 // if (startIndx<0) {
1015 // System.out.println(temp.substring(startIndx)+"?>");
1019 // for(int j=0;j<phpList.size();j++) {
1020 // temp = (PHPString) phpList.get(j);
1021 // parser.start(temp.getPHPString(), temp.getLineNumber());
1024 } catch (CoreException e) {
1028 public void phpParse(String s, int rowCount) throws CoreException {
1033 if (phpList.size() != 0) {
1034 this.str = ((PHPString) phpList.get(currentPHPString++)).getPHPString();
1037 this.token = TT_EOF;
1039 this.rowCount = rowCount;
1040 this.columnCount = 0;
1041 this.phpEnd = false;
1043 if (token != TT_EOF && token != TT_UNDEFINED) {
1046 if (token != TT_EOF && token != TT_UNDEFINED) {
1047 if (token == TT_ARGCLOSE) {
1048 throwSyntaxError("too many closing ')'; end-of-file not reached");
1050 if (token == TT_LISTCLOSE) {
1051 throwSyntaxError("too many closing '}'; end-of-file not reached");
1053 if (token == TT_PARTCLOSE) {
1054 throwSyntaxError("too many closing ']'; end-of-file not reached");
1057 if (token == TT_ARGOPEN) {
1058 throwSyntaxError("read character '('; end-of-file not reached");
1060 if (token == TT_LISTOPEN) {
1061 throwSyntaxError("read character '{'; end-of-file not reached");
1063 if (token == TT_PARTOPEN) {
1064 throwSyntaxError("read character '['; end-of-file not reached");
1067 throwSyntaxError("end-of-file not reached");
1069 } catch (SyntaxError err) {
1073 setMarker(err.getMessage(), err.getLine(), ERROR);
1078 private void statementList() throws CoreException {
1081 if ((token == TT_LISTCLOSE)
1082 || (token == TT_case)
1083 || (token == TT_default)
1084 || (token == TT_elseif)
1085 || (token == TT_endif)
1086 || (token == TT_endfor)
1087 || (token == TT_endforeach)
1088 || (token == TT_endwhile)
1089 || (token == TT_endswitch)
1090 || (token == TT_EOF)
1091 || (token == TT_UNDEFINED)) {
1097 private void compoundStatement() throws CoreException {
1098 // '{' [statement-list] '}'
1099 if (token == TT_LISTOPEN) {
1102 throwSyntaxError("'{' expected in compound-statement.");
1104 if (token != TT_LISTCLOSE) {
1107 if (token == TT_LISTCLOSE) {
1110 throwSyntaxError("'}' expected in compound-statement.");
1114 private void statement() throws CoreException {
1115 // if (token > TT_KEYWORD && token != TT_list && token != TT_new) {
1116 String keyword = identifier;
1117 if (token == TT_include || token == TT_include_once) {
1120 if (token == TT_SEMICOLON) {
1124 throwSyntaxError("';' character after 'include' or 'include_once' expected.");
1128 } else if (token == TT_require || token == TT_require_once) {
1132 if (token == TT_SEMICOLON) {
1136 throwSyntaxError("';' character after 'require' or 'require_once' expected.");
1140 } else if (token == TT_if) {
1142 if (token == TT_ARGOPEN) {
1145 throwSyntaxError("'(' expected after 'if' keyword.");
1148 if (token == TT_ARGCLOSE) {
1151 throwSyntaxError("')' expected after 'if' condition.");
1156 } else if (token == TT_switch) {
1158 if (token == TT_ARGOPEN) {
1161 throwSyntaxError("'(' expected after 'switch' keyword.");
1164 if (token == TT_ARGCLOSE) {
1167 throwSyntaxError("')' expected after 'switch' condition.");
1171 } else if (token == TT_for) {
1173 if (token == TT_ARGOPEN) {
1176 throwSyntaxError("'(' expected after 'for' keyword.");
1178 if (token == TT_SEMICOLON) {
1182 if (token == TT_SEMICOLON) {
1185 throwSyntaxError("';' expected after 'for'.");
1188 if (token == TT_SEMICOLON) {
1192 if (token == TT_SEMICOLON) {
1195 throwSyntaxError("';' expected after 'for'.");
1198 if (token == TT_ARGCLOSE) {
1202 if (token == TT_ARGCLOSE) {
1205 throwSyntaxError("')' expected after 'for'.");
1210 } else if (token == TT_while) {
1212 if (token == TT_ARGOPEN) {
1215 throwSyntaxError("'(' expected after 'while' keyword.");
1218 if (token == TT_ARGCLOSE) {
1221 throwSyntaxError("')' expected after 'while' condition.");
1225 } else if (token == TT_do) {
1227 if (token == TT_LISTOPEN) {
1230 throwSyntaxError("'{' expected after 'do' keyword.");
1232 if (token != TT_LISTCLOSE) {
1235 if (token == TT_LISTCLOSE) {
1238 throwSyntaxError("'}' expected after 'do' keyword.");
1240 if (token == TT_while) {
1242 if (token == TT_ARGOPEN) {
1245 throwSyntaxError("'(' expected after 'while' keyword.");
1248 if (token == TT_ARGCLOSE) {
1251 throwSyntaxError("')' expected after 'while' condition.");
1254 throwSyntaxError("'while' expected after 'do' keyword.");
1256 if (token == TT_SEMICOLON) {
1260 throwSyntaxError("';' expected after do-while statement.");
1264 } else if (token == TT_foreach) {
1266 if (token == TT_ARGOPEN) {
1269 throwSyntaxError("'(' expected after 'foreach' keyword.");
1272 if (token == TT_as) {
1275 throwSyntaxError("'as' expected after 'foreach' exxpression.");
1278 if (token == TT_FOREACH) {
1282 if (token == TT_ARGCLOSE) {
1285 throwSyntaxError("')' expected after 'foreach' expression.");
1290 } else if (token == TT_continue || token == TT_break || token == TT_return) {
1292 if (token != TT_SEMICOLON) {
1295 if (token == TT_SEMICOLON) {
1299 throwSyntaxError("';' expected after 'continue', 'break' or 'return'.");
1304 } else if (token == TT_echo) {
1307 if (token == TT_SEMICOLON) {
1311 throwSyntaxError("';' expected after 'echo' statement.");
1315 // } else if (token == TT_print) {
1318 // if (token == TT_SEMICOLON) {
1322 // throwSyntaxError("';' expected after 'print' statement.");
1327 } else if (token == TT_global || token == TT_static) {
1330 if (token == TT_SEMICOLON) {
1334 throwSyntaxError("';' expected after 'global' or 'static' statement.");
1339 // } else if (token == TT_unset) {
1341 // if (token == TT_ARGOPEN) {
1344 // throwSyntaxError("'(' expected after 'unset' keyword.");
1347 // if (token == TT_ARGCLOSE) {
1350 // throwSyntaxError("')' expected after 'unset' statement.");
1352 // if (token == TT_SEMICOLON) {
1356 // throwSyntaxError("';' expected after 'unset' statement.");
1361 // } else if (token == TT_exit || token == TT_die) {
1363 // if (token != TT_SEMICOLON) {
1366 // if (token == TT_SEMICOLON) {
1370 // throwSyntaxError("';' expected after 'exit' or 'die' statement.");
1375 } else if (token == TT_define) {
1377 if (token == TT_ARGOPEN) {
1380 throwSyntaxError("'(' expected after 'define' keyword.");
1383 if (token == TT_COMMA) {
1386 throwSyntaxError("',' expected after first 'define' constant.");
1389 if (token == TT_COMMA) {
1393 if (token == TT_ARGCLOSE) {
1396 throwSyntaxError("')' expected after 'define' statement.");
1398 if (token == TT_SEMICOLON) {
1402 throwSyntaxError("';' expected after 'define' statement.");
1406 } else if (token == TT_function) {
1408 functionDefinition();
1410 } else if (token == TT_class) {
1416 // throwSyntaxError("Unexpected keyword '" + keyword + "'");
1417 } else if (token == TT_LISTOPEN) {
1418 // compoundStatement
1420 if (token != TT_LISTCLOSE) {
1423 if (token == TT_LISTCLOSE) {
1427 throwSyntaxError("'}' expected.");
1430 if (token != TT_SEMICOLON) {
1433 if (token == TT_SEMICOLON) {
1438 throwSyntaxError("';' expected after expression.");
1444 private void classDeclarator() throws CoreException {
1446 //identifier 'extends' identifier
1447 if (token == TT_IDENTIFIER) {
1449 if (token == TT_extends) {
1451 if (token == TT_IDENTIFIER) {
1454 throwSyntaxError("Class name expected after keyword 'extends'.");
1458 throwSyntaxError("Class name expected after keyword 'class'.");
1462 private void classBody() throws CoreException {
1463 //'{' [class-element-list] '}'
1464 if (token == TT_LISTOPEN) {
1466 if (token != TT_LISTCLOSE) {
1469 if (token == TT_LISTCLOSE) {
1472 throwSyntaxError("'}' expected at end of class body.");
1475 throwSyntaxError("'{' expected at start of class body.");
1479 private void classElementList() throws CoreException {
1482 } while (token == TT_function || token == TT_var);
1485 private void classElement() throws CoreException {
1487 //function-definition
1488 if (token == TT_function) {
1490 functionDefinition();
1491 } else if (token == TT_var) {
1495 throwSyntaxError("'function' or 'var' expected.");
1499 private void classProperty() throws CoreException {
1500 //'var' variable ';'
1501 //'var' variable '=' constant ';'
1503 if (token == TT_VARIABLE) {
1505 if (token == TT_ASSIGN) {
1510 throwSyntaxError("Variable expected after keyword 'var'.");
1512 if (token != TT_COMMA) {
1517 if (token == TT_SEMICOLON) {
1520 throwSyntaxError("';' expected after variable declaration.");
1524 private void functionDefinition() throws CoreException {
1525 functionDeclarator();
1526 compoundStatement();
1529 private void functionDeclarator() throws CoreException {
1530 //identifier '(' [parameter-list] ')'
1531 if (token == TT_AMPERSAND) {
1534 if (token == TT_IDENTIFIER) {
1536 if (token == TT_ARGOPEN) {
1539 throwSyntaxError("'(' expected in function declaration.");
1541 if (token != TT_ARGCLOSE) {
1544 if (token != TT_ARGCLOSE) {
1545 throwSyntaxError("')' expected in function declaration.");
1552 private void parameterList() throws CoreException {
1553 //parameter-declaration
1554 //parameter-list ',' parameter-declaration
1556 parameterDeclaration();
1557 if (token != TT_COMMA) {
1564 private void parameterDeclaration() throws CoreException {
1566 //variable-reference
1567 if (token == TT_AMPERSAND) {
1569 if (token == TT_VARIABLE) {
1572 throwSyntaxError("Variable expected after reference operator '&'.");
1575 //variable '=' constant
1576 if (token == TT_VARIABLE) {
1578 if (token == TT_ASSIGN) {
1586 private void labeledStatementList() throws CoreException {
1587 if (token != TT_case && token != TT_default) {
1588 throwSyntaxError("'case' or 'default' expected.");
1591 if (token == TT_case) {
1594 if (token == TT_DDOT) {
1596 if (token == TT_case || token == TT_default) { // empty case statement ?
1600 } else if (token == TT_SEMICOLON) {
1601 setMarker("':' expected after 'case' keyword found ';'.", rowCount, PHPParser.INFO);
1603 if (token == TT_case) { // empty case statement ?
1608 throwSyntaxError("':' character after 'case' constant expected.");
1610 } else { // TT_default
1612 if (token == TT_DDOT) {
1616 throwSyntaxError("':' character after 'default' expected.");
1619 } while (token == TT_case || token == TT_default);
1622 // public void labeledStatement() {
1623 // if (token == TT_case) {
1626 // if (token == TT_DDOT) {
1630 // throwSyntaxError("':' character after 'case' constant expected.");
1633 // } else if (token == TT_default) {
1635 // if (token == TT_DDOT) {
1639 // throwSyntaxError("':' character after 'default' expected.");
1645 // public void expressionStatement() {
1648 // private void inclusionStatement() {
1651 // public void compoundStatement() {
1654 // public void selectionStatement() {
1657 // public void iterationStatement() {
1660 // public void jumpStatement() {
1663 // public void outputStatement() {
1666 // public void scopeStatement() {
1669 // public void flowStatement() {
1672 // public void definitionStatement() {
1675 private void ifStatement() throws CoreException {
1676 // ':' statement-list [elseif-list] [else-colon-statement] 'endif' ';'
1677 if (token == TT_DDOT) {
1683 if (token == TT_DDOT) {
1687 if (token == TT_if) { //'else if'
1689 elseifStatementList();
1691 throwSyntaxError("':' expected after 'else'.");
1697 elseifStatementList();
1701 if (token != TT_endif) {
1702 throwSyntaxError("'endif' expected.");
1705 if (token != TT_SEMICOLON) {
1706 throwSyntaxError("';' expected after if-statement.");
1710 // statement [else-statement]
1712 if (token == TT_elseif) {
1714 if (token == TT_ARGOPEN) {
1717 throwSyntaxError("'(' expected after 'elseif' keyword.");
1720 if (token == TT_ARGCLOSE) {
1723 throwSyntaxError("')' expected after 'elseif' condition.");
1726 } else if (token == TT_else) {
1733 private void elseifStatementList() throws CoreException {
1739 if (token == TT_DDOT) {
1744 if (token == TT_if) { //'else if'
1747 throwSyntaxError("':' expected after 'else'.");
1760 private void elseifStatement() throws CoreException {
1761 if (token == TT_ARGOPEN) {
1764 if (token != TT_ARGOPEN) {
1765 throwSyntaxError("')' expected in else-if-statement.");
1768 if (token != TT_DDOT) {
1769 throwSyntaxError("':' expected in else-if-statement.");
1776 private void switchStatement() throws CoreException {
1777 if (token == TT_DDOT) {
1778 // ':' [labeled-statement-list] 'endswitch' ';'
1780 labeledStatementList();
1781 if (token != TT_endswitch) {
1782 throwSyntaxError("'endswitch' expected.");
1785 if (token != TT_SEMICOLON) {
1786 throwSyntaxError("';' expected after switch-statement.");
1790 // '{' [labeled-statement-list] '}'
1791 if (token != TT_LISTOPEN) {
1792 throwSyntaxError("'{' expected in switch statement.");
1795 if (token != TT_LISTCLOSE) {
1796 labeledStatementList();
1798 if (token != TT_LISTCLOSE) {
1799 throwSyntaxError("'}' expected in switch statement.");
1806 private void forStatement() throws CoreException {
1807 if (token == TT_DDOT) {
1810 if (token != TT_endfor) {
1811 throwSyntaxError("'endfor' expected.");
1814 if (token != TT_SEMICOLON) {
1815 throwSyntaxError("';' expected after for-statement.");
1823 private void whileStatement() throws CoreException {
1824 // ':' statement-list 'endwhile' ';'
1825 if (token == TT_DDOT) {
1828 if (token != TT_endwhile) {
1829 throwSyntaxError("'endwhile' expected.");
1832 if (token != TT_SEMICOLON) {
1833 throwSyntaxError("';' expected after while-statement.");
1841 private void foreachStatement() throws CoreException {
1842 if (token == TT_DDOT) {
1845 if (token != TT_endforeach) {
1846 throwSyntaxError("'endforeach' expected.");
1849 if (token != TT_SEMICOLON) {
1850 throwSyntaxError("';' expected after foreach-statement.");
1858 private void exitStatus() throws CoreException {
1859 if (token == TT_ARGOPEN) {
1862 throwSyntaxError("'(' expected in 'exit-status'.");
1864 if (token != TT_ARGCLOSE) {
1867 if (token == TT_ARGCLOSE) {
1870 throwSyntaxError("')' expected after 'exit-status'.");
1874 private void expressionList() throws CoreException {
1877 if (token == TT_COMMA) {
1885 private void expression() throws CoreException {
1886 // if (token == TT_STRING_CONSTANT || token == TT_INTERPOLATED_STRING) {
1889 logicalinclusiveorExpression();
1890 // while (token != TT_SEMICOLON) {
1896 private void postfixExpression() throws CoreException {
1898 boolean castFlag = false;
1913 case TT_STRING_CONSTANT :
1916 case TT_INTERPOLATED_STRING :
1921 if (token == TT_IDENTIFIER) {
1922 // check if identifier is a type:
1924 String str = identifier.toLowerCase();
1925 for (int i = 0; i < PHP_TYPES.length; i++) {
1926 if (PHP_TYPES[i].equals(str)) {
1933 if (token != TT_ARGCLOSE) {
1934 throwSyntaxError(") expected after cast-type '" + ident + "'.");
1944 if (token != TT_ARGCLOSE) {
1945 throwSyntaxError(") expected in postfix-expression.");
1949 case TT_DOUBLE_NUMBER :
1952 case TT_INT_NUMBER :
1955 case TT_DOLLAROPEN :
1958 if (token != TT_LISTCLOSE) {
1959 throwSyntaxError("'}' expected after indirect variable token '${'.");
1966 if (token == TT_LISTOPEN) {
1969 if (token != TT_LISTCLOSE) {
1970 throwSyntaxError("'}' expected after variable '" + ident + "' in variable-expression.");
1973 } else if (token == TT_ARGOPEN) {
1975 if (token != TT_ARGCLOSE) {
1977 if (token != TT_ARGCLOSE) {
1978 throwSyntaxError("')' expected after variable '" + ident + "' in postfix-expression.");
1984 case TT_IDENTIFIER :
1987 if (token == TT_ARGOPEN) {
1989 if (token != TT_ARGCLOSE) {
1991 if (token != TT_ARGCLOSE) {
1992 throwSyntaxError("')' expected after identifier '" + ident + "' in postfix-expression.");
2001 // if (token == TT_SEMICOLON) {
2005 // throwSyntaxError("';' expected after 'print' statement.");
2011 if (token == TT_ARGOPEN) {
2013 if (token == TT_COMMA) {
2017 if (token != TT_ARGCLOSE) {
2018 throwSyntaxError("')' expected after 'list' keyword.");
2021 // if (token == TT_SET) {
2023 // logicalinclusiveorExpression();
2026 throwSyntaxError("'(' expected after 'list' keyword.");
2031 // if (token != TT_SEMICOLON) {
2034 // if (token == TT_SEMICOLON) {
2038 // throwSyntaxError("';' expected after 'exit' expression.");
2044 // if (token != TT_SEMICOLON) {
2047 // if (token == TT_SEMICOLON) {
2051 // throwSyntaxError("';' expected after 'die' expression.");
2058 // if (token == TT_ARGOPEN) {
2060 // if (token == TT_COMMA) {
2063 // expressionList();
2064 // if (token != TT_ARGCLOSE) {
2065 // throwSyntaxError("')' expected after 'list' keyword.");
2068 // if (token == TT_SET) {
2070 // logicalinclusiveorExpression();
2073 // throwSyntaxError("'(' expected after 'list' keyword.");
2077 boolean while_flag = true;
2083 if (token != TT_PARTCLOSE) {
2084 throwSyntaxError("] expected in postfix-expression.");
2088 case TT_DDOT2 : // ::
2091 if (token > TT_KEYWORD) {
2093 setMarker("Avoid using keyword '" + ident + "' as variable name.", rowCount, PHPParser.INFO);
2095 if (token == TT_ARGOPEN) {
2098 if (token != TT_ARGCLOSE) {
2099 throwSyntaxError(") expected after identifier '" + ident + "'.");
2109 // if (token == TT_ARGOPEN) {
2111 // expressionList();
2112 // if (token != TT_ARGCLOSE) {
2113 // throwSyntaxError(") expected after variable '" + ident + "'.");
2118 case TT_IDENTIFIER :
2121 if (token == TT_ARGOPEN) {
2124 if (token != TT_ARGCLOSE) {
2125 throwSyntaxError(") expected after identifier '" + ident + "'.");
2133 if (token != TT_LISTCLOSE) {
2134 throwSyntaxError("} expected in postfix-expression.");
2139 throwSyntaxError("Syntax error after '->' token.");
2153 } while (while_flag);
2156 private void unaryExpression() throws CoreException {
2166 // '@' '&' '*' '+' '-' '~' '!'
2196 postfixExpression();
2200 private void castExpression() throws CoreException {
2201 // if (token == TT_ARGOPEN) {
2204 // if (token != TT_ARGCLOSE) {
2205 // throwSyntaxError(") expected after cast-expression.");
2212 private void typeName() throws CoreException {
2213 //'string' 'unset' 'array' 'object'
2215 //'real' 'double' 'float'
2218 if (token == TT_IDENTIFIER) {
2220 String str = identifier.toLowerCase();
2222 for (int i = 0; i < PHP_TYPES.length; i++) {
2223 if (PHP_TYPES[i].equals(str)) {
2228 throwSyntaxError("Expected type cast '( <type-name> )'; Got '" + ident + "'.");
2231 private void assignExpression() throws CoreException {
2233 if (token == TT_ASSIGN) { // =
2235 logicalinclusiveorExpression();
2236 } else if (token == TT_DOTASSIGN) { // .=
2238 logicalinclusiveorExpression();
2239 } else if (token == TT_FOREACH) { // =>
2241 logicalinclusiveorExpression();
2242 } else if (token == TT_ADDTO) { // +=
2244 logicalinclusiveorExpression();
2245 } else if (token == TT_SUBTRACTFROM) { // -=
2247 logicalinclusiveorExpression();
2248 } else if (token == TT_TIMESBY) { // *=
2250 logicalinclusiveorExpression();
2251 } else if (token == TT_DIVIDEBY) { // *=
2253 logicalinclusiveorExpression();
2254 } else if (token == TT_MODASSIGN) { // %=
2256 logicalinclusiveorExpression();
2257 } else if (token == TT_ANDASSIGN) { // &=
2259 logicalinclusiveorExpression();
2260 } else if (token == TT_POWASSIGN) { // ^=
2262 logicalinclusiveorExpression();
2263 } else if (token == TT_LSHIFTASSIGN) { // <<=
2265 logicalinclusiveorExpression();
2266 } else if (token == TT_RSHIFTASSIGN) { // >>=
2268 logicalinclusiveorExpression();
2269 } else if (token == TT_TILDEASSIGN) { // ~=
2271 logicalinclusiveorExpression();
2275 private void multiplicativeExpression() throws CoreException {
2278 if (token != TT_MULTIPLY && token != TT_DIV && token != TT_MOD) {
2285 private void concatenationExpression() throws CoreException {
2287 multiplicativeExpression();
2288 if (token != TT_DOT) {
2295 private void additiveExpression() throws CoreException {
2297 concatenationExpression();
2298 if (token != TT_ADD && token != TT_SUBTRACT) {
2305 private void shiftExpression() throws CoreException {
2307 additiveExpression();
2308 if (token != TT_LSHIFT && token != TT_RSHIFT) {
2315 private void relationalExpression() throws CoreException {
2318 if (token != TT_LESS && token != TT_GREATER && token != TT_LESSEQUAL && token != TT_GREATEREQUAL) {
2325 private void identicalExpression() throws CoreException {
2327 relationalExpression();
2328 if (token != TT_EX_EQUAL && token != TT_EX_UNEQUAL) {
2335 private void equalityExpression() throws CoreException {
2337 identicalExpression();
2338 if (token != TT_EQUAL && token != TT_UNEQUAL) {
2345 private void ternaryExpression() throws CoreException {
2346 equalityExpression();
2347 if (token == TT_QUESTIONMARK) {
2350 if (token == TT_DDOT) {
2354 throwSyntaxError("':' expected in ternary operator '? :'.");
2359 private void andExpression() throws CoreException {
2361 ternaryExpression();
2362 if (token != TT_AMPERSAND) {
2369 private void exclusiveorExpression() throws CoreException {
2372 if (token != TT_POW) {
2379 private void inclusiveorExpression() throws CoreException {
2381 exclusiveorExpression();
2382 if (token != TT_LINE) {
2389 private void booleanandExpression() throws CoreException {
2391 inclusiveorExpression();
2392 if (token != TT_AND) {
2399 private void booleanorExpression() throws CoreException {
2401 booleanandExpression();
2402 if (token != TT_OR) {
2409 private void logicalandExpression() throws CoreException {
2411 booleanorExpression();
2412 if (token != TT_and) {
2419 private void logicalexclusiveorExpression() throws CoreException {
2421 logicalandExpression();
2422 if (token != TT_xor) {
2429 private void logicalinclusiveorExpression() throws CoreException {
2431 logicalexclusiveorExpression();
2432 if (token != TT_or) {
2439 // public void assignmentExpression() {
2440 // if (token == TT_VARIABLE) {
2442 // if (token == TT_SET) {
2444 // logicalinclusiveorExpression();
2447 // logicalinclusiveorExpression();
2451 private void variableList() throws CoreException {
2454 if (token == TT_COMMA) {
2462 private void variable() throws CoreException {
2463 if (token == TT_DOLLAROPEN) {
2467 if (token != TT_LISTCLOSE) {
2468 throwSyntaxError("'}' expected after indirect variable token '${'.");
2472 if (token == TT_VARIABLE) {
2474 if (token == TT_PARTOPEN) {
2477 if (token != TT_PARTCLOSE) {
2478 throwSyntaxError("']' expected in variable-list.");
2481 } else if (token == TT_ASSIGN) {
2486 throwSyntaxError("$-variable expected in variable-list.");
2491 private void constant() throws CoreException {
2497 case TT_DOUBLE_NUMBER :
2500 case TT_INT_NUMBER :
2504 throwSyntaxError("Constant expected after '+' presign.");
2510 case TT_DOUBLE_NUMBER :
2513 case TT_INT_NUMBER :
2517 throwSyntaxError("Constant expected after '-' presign.");
2529 case TT_IDENTIFIER :
2532 if (token == TT_ARGOPEN) {
2534 if (token != TT_ARGCLOSE) {
2536 if (token != TT_ARGCLOSE) {
2537 throwSyntaxError("')' expected after identifier '" + ident + "' in postfix-expression.");
2543 case TT_STRING_CONSTANT :
2546 case TT_INTERPOLATED_STRING :
2549 case TT_DOUBLE_NUMBER :
2552 case TT_INT_NUMBER :
2556 throwSyntaxError("Constant expected.");
2561 * Call the php parse command ( php -l -f <filename> )
2562 * and create markers according to the external parser output
2564 public static void phpExternalParse(IFile file) {
2565 //IFile file = (IFile) resource;
2566 IPath path = file.getFullPath();
2567 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
2568 String filename = file.getLocation().toString();
2570 String[] arguments = { filename };
2571 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
2572 String command = form.format(arguments);
2574 String parserResult = PHPStartApacheAction.execute(command, "External parser: ");
2577 // parse the buffer to find the errors and warnings
2578 createMarkers(parserResult, file);
2579 } catch (CoreException e) {
2584 * Create markers according to the external parser output
2586 private static void createMarkers(String output, IFile file) throws CoreException {
2587 // delete all markers
2588 file.deleteMarkers(IMarker.PROBLEM, false, 0);
2592 boolean flag = true;
2593 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
2594 // newer php error output (tested with 4.2.3)
2595 scanLine(output, file, indx, brIndx);
2600 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
2601 // older php error output (tested with 4.2.3)
2602 scanLine(output, file, indx, brIndx);
2608 private static void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
2610 String outLineNumberString;
2611 StringBuffer lineNumberBuffer = new StringBuffer(10);
2613 current = output.substring(indx, brIndx);
2615 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
2616 int onLine = current.indexOf("on line <b>");
2618 lineNumberBuffer.delete(0, lineNumberBuffer.length());
2619 for (int i = onLine; i < current.length(); i++) {
2620 ch = current.charAt(i);
2621 if ('0' <= ch && '9' >= ch) {
2622 lineNumberBuffer.append(ch);
2626 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
2628 Hashtable attributes = new Hashtable();
2630 current = current.replaceAll("\n", "");
2631 current = current.replaceAll("<b>", "");
2632 current = current.replaceAll("</b>", "");
2633 MarkerUtilities.setMessage(attributes, current);
2635 if (current.indexOf(PARSE_ERROR_STRING) != -1)
2636 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
2637 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
2638 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
2640 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
2641 MarkerUtilities.setLineNumber(attributes, lineNumber);
2642 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);