From 2386c3e876bf7e648c208f5738295f15a87c2fe1 Mon Sep 17 00:00:00 2001 From: khartlage Date: Sun, 17 Nov 2002 13:44:26 +0000 Subject: [PATCH] improved php parser (if-statement); but a lot of things don't work yet --- .../phpeclipse/phpeditor/PHPParser.java | 203 ++++++++++++++++---- .../phpeclipse/phpeditor/PHPParserAction.java | 4 +- .../phpeclipse/phpeditor/php/PHPKeywords.java | 6 +- 3 files changed, 169 insertions(+), 44 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java index e2f1dce..fd8bec2 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParser.java @@ -62,8 +62,10 @@ public class PHPParser extends PHPKeywords { final static int TT_DOTASSIGN = 48; final static int TT_SET = 49; - + final static int TT_REF = 50; final static int TT_FOREACH = 51; + final static int TT_AMPERSAND = 52; + final static int TT_DOLLARLISTOPEN = 53; final static int TT_ARGOPEN = 128; final static int TT_ARGCLOSE = 129; final static int TT_LISTOPEN = 130; @@ -97,17 +99,17 @@ public class PHPParser extends PHPKeywords { *@param sess Description of Parameter *@see */ - public PHPParser(String s, int rowCount) { + public PHPParser() { if (keywordMap == null) { keywordMap = new HashMap(); for (int i = 0; i < PHP_KEYWORS.length; i++) { keywordMap.put(PHP_KEYWORS[i], new Integer(PHP_KEYWORD_TOKEN[i])); } } - this.str = s; + this.str = ""; this.token = TT_EOF; this.chIndx = 0; - this.rowCount = rowCount; + this.rowCount = 1; this.columnCount = 0; getNextToken(); @@ -127,7 +129,7 @@ public class PHPParser extends PHPKeywords { break; } } - throw new SyntaxError(rowCount, chIndx - columnCount, str.substring(columnCount + 1, eol), error); + throw new SyntaxError(rowCount, chIndx - columnCount + 1, str.substring(columnCount, eol), error); } /** @@ -182,9 +184,7 @@ public class PHPParser extends PHPKeywords { chIndx++; // multi line comment: while (str.length() > chIndx) { - if (str.charAt(chIndx) == '*' && - (str.length() > (chIndx+1) ) && - str.charAt(chIndx+1) == '/') { + if (str.charAt(chIndx) == '*' && (str.length() > (chIndx + 1)) && str.charAt(chIndx + 1) == '/') { chIndx += 2; break; } @@ -216,9 +216,9 @@ public class PHPParser extends PHPKeywords { } } } - if (str.length() > chIndx) { - chIndx++; - } + // if (str.length() > chIndx) { + // chIndx++; + // } token = TT_INTERPOLATED_STRING; return; } else if (ch == '\'') { @@ -233,9 +233,9 @@ public class PHPParser extends PHPKeywords { } } } - if (str.length() > chIndx) { - chIndx++; - } + // if (str.length() > chIndx) { + // chIndx++; + // } token = TT_STRING_CONSTANT; return; } @@ -362,6 +362,12 @@ public class PHPParser extends PHPKeywords { break; } + if (str.charAt(chIndx) == '>') { + chIndx++; + token = TT_REF; + + break; + } } break; @@ -428,7 +434,8 @@ public class PHPParser extends PHPKeywords { case '|' : if (str.length() > chIndx) { - if (str.charAt(chIndx++) == '|') { + if (str.charAt(chIndx) == '|') { + chIndx++; token = TT_OR; break; @@ -438,10 +445,15 @@ public class PHPParser extends PHPKeywords { break; case '&' : if (str.length() > chIndx) { - if (str.charAt(chIndx++) == '&') { + if (str.charAt(chIndx) == '&') { + chIndx++; token = TT_AND; break; + } else { + token = TT_AMPERSAND; + + break; } } @@ -477,7 +489,6 @@ public class PHPParser extends PHPKeywords { void getIdentifier() { StringBuffer ident = new StringBuffer(); - ident.append(ch); ident.append(ch); if (ch == '$') { @@ -491,8 +502,9 @@ public class PHPParser extends PHPKeywords { getChar(); } identifier = ident.toString(); + chIndx--; - Integer i = (Integer) keywordMap.get(identifier); + Integer i = (Integer) keywordMap.get(identifier.toLowerCase()); if (i != null) { token = i.intValue(); } @@ -565,8 +577,7 @@ public class PHPParser extends PHPKeywords { } } } - - // token = TT_INT_NUMBER; + chIndx--; try { if (dFlag != ' ') { @@ -584,7 +595,15 @@ public class PHPParser extends PHPKeywords { } } - public void start() throws SyntaxError { + public void start(String s, int rowCount) throws SyntaxError { + // start up + this.str = s; + this.token = TT_EOF; + this.chIndx = 0; + this.rowCount = rowCount; + this.columnCount = 0; + getNextToken(); + statementList(); if (token != TT_EOF) { if (token == TT_ARGCLOSE) { @@ -597,17 +616,39 @@ public class PHPParser extends PHPKeywords { throwSyntaxError("too many closing ']'; end-of-file not reached"); } + if (token == TT_ARGOPEN) { + throwSyntaxError("read character '('; end-of-file not reached"); + } + if (token == TT_LISTOPEN) { + throwSyntaxError("read character '{'; end-of-file not reached"); + } + if (token == TT_PARTOPEN) { + throwSyntaxError("read character '['; end-of-file not reached"); + } + throwSyntaxError("end-of-file not reached"); } } public void statementList() { - statement(); + do { + statement(); + if ((token == TT_LISTCLOSE) + || (token == TT_elseif) + || (token == TT_endif) + || (token == TT_endfor) + || (token == TT_endforeach) + || (token == TT_endwhile) + || (token == TT_endswitch) + || (token == TT_EOF)) { + return; + } + } while (true); } public void statement() { - while (token != TT_UNDEFINED) { + while (token != TT_UNDEFINED && token != TT_EOF) { if (token > TT_KEYWORD) { if (token == TT_case) { getNextToken(); @@ -639,7 +680,8 @@ public class PHPParser extends PHPKeywords { return; } else if (token == TT_require || token == TT_require_once) { getNextToken(); - constant(); + //constant(); + expression(); if (token == TT_SEMICOLON) { getNextToken(); } else { @@ -859,22 +901,26 @@ public class PHPParser extends PHPKeywords { } - } else { - if (token == TT_LISTOPEN) { - getNextToken(); + } else if (token == TT_LISTOPEN) { + // compundStatement + getNextToken(); + if (token != TT_LISTCLOSE) { statementList(); - if (token == TT_LISTCLOSE) { - getNextToken(); - } else { - throwSyntaxError("'}' expected."); - } } - } - expression(); - if (token == TT_SEMICOLON) { - getNextToken(); + if (token == TT_LISTCLOSE) { + getNextToken(); + } else { + throwSyntaxError("'}' expected."); + } } else { - throwSyntaxError("';' expected after expression."); + if (token != TT_SEMICOLON) { + expression(); + } + if (token == TT_SEMICOLON) { + getNextToken(); + } else { + throwSyntaxError("';' expected after expression."); + } } } } @@ -888,8 +934,8 @@ public class PHPParser extends PHPKeywords { public void inclusionStatement() { } - public void compoundStatement() { - } + // public void compoundStatement() { + // } public void selectionStatement() { } @@ -913,6 +959,12 @@ public class PHPParser extends PHPKeywords { } public void ifStatement() { + // statement [else-statement] + statement(); + if (token == TT_else) { + getNextToken(); + statement(); + } } public void switchStatement() { @@ -966,7 +1018,79 @@ public class PHPParser extends PHPKeywords { } public void postfixExpression() { + switch (token) { + case TT_ARGOPEN : + getNextToken(); + expression(); + if (token != TT_ARGCLOSE) { + throwSyntaxError(") expected in postfix-expression."); + } + getNextToken(); + break; + case TT_DOUBLE_NUMBER : + getNextToken(); + break; + case TT_INT_NUMBER : + getNextToken(); + break; + case TT_VARIABLE : + getNextToken(); + break; + case TT_IDENTIFIER : + getNextToken(); + if (token == TT_ARGOPEN) { + getNextToken(); + if (token != TT_ARGCLOSE) { + expressionList(); + if (token != TT_ARGCLOSE) { + throwSyntaxError(") expected after identifier in postfix-expression."); + } + } + getNextToken(); + } + break; + } + boolean while_flag = true; + do { + switch (token) { + case TT_PARTOPEN : + getNextToken(); + expression(); + if (token != TT_PARTCLOSE) { + throwSyntaxError("] expected in postfix-expression."); + } + getNextToken(); + break; + case TT_REF : + switch (token) { + case TT_VARIABLE : + getNextToken(); + break; + case TT_IDENTIFIER : + getNextToken(); + break; + case TT_LISTOPEN : + getNextToken(); + expression(); + if (token != TT_LISTCLOSE) { + throwSyntaxError("] expected in postfix-expression."); + } + getNextToken(); + break; + default : + throwSyntaxError("Syntax error after '->' token."); + } + case TT_INCREMENT : + getNextToken(); + break; + case TT_DECREMENT : + getNextToken(); + break; + default : + while_flag = false; + } + } while (while_flag); } public void variableList() { @@ -991,4 +1115,5 @@ public class PHPParser extends PHPKeywords { public void constant() { } + } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java index a5ae10a..6882db1 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPParserAction.java @@ -286,8 +286,8 @@ public class PHPParserAction extends TextEditorAction { phpMode = false; // phpList.add(input.substring(startIndex, i-2)); try { - PHPParser parser = new PHPParser(input.substring(startIndex, i - 2), startLineNumber); - parser.start(); + PHPParser parser = new PHPParser(); + parser.start(input.substring(startIndex, i - 2), startLineNumber); } catch (SyntaxError err) { setMarker(err.getMessage(), err.getLine()); } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java index 9d33fef..f4f7ddd 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/php/PHPKeywords.java @@ -52,7 +52,7 @@ public class PHPKeywords { "extends", "empty", "array", - "isset", + // "isset", "echo", "var", "as", @@ -94,7 +94,7 @@ public class PHPKeywords { public final static int TT_extends = 1029; public final static int TT_empty = 1030; public final static int TT_array = 1031; - public final static int TT_isset = 1032; + // public final static int TT_isset = 1032; public final static int TT_echo = 1033; public final static int TT_var = 1034; public final static int TT_as = 1035; @@ -137,7 +137,7 @@ public class PHPKeywords { TT_extends, TT_empty, TT_array, - TT_isset, + // TT_isset, TT_echo, TT_var, TT_as, -- 1.7.1