X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.java b/net.sourceforge.phpeclipse/src/test/PHPParser.java index 43f38f3..bc7c112 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.java +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.java @@ -8,7 +8,6 @@ import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.jface.preference.IPreferenceStore; import java.util.Hashtable; -import java.util.ArrayList; import java.util.Enumeration; import java.io.StringReader; import java.io.*; @@ -17,6 +16,7 @@ import java.text.MessageFormat; import net.sourceforge.phpeclipse.actions.PHPStartApacheAction; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpdt.internal.compiler.parser.*; +import net.sourceforge.phpdt.internal.compiler.ast.*; /** * A new php parser. @@ -30,14 +30,15 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon /** The file that is parsed. */ private static IFile fileToParse; - /** The current segment */ + /** The current segment. */ private static PHPSegmentWithChildren currentSegment; private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$ private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$ - PHPOutlineInfo outlineInfo; + static PHPOutlineInfo outlineInfo; private static PHPFunctionDeclaration currentFunction; + private static boolean assigning; /** The error level of the current ParseException. */ private static int errorLevel = ERROR; @@ -47,13 +48,20 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon private static int errorStart = -1; private static int errorEnd = -1; - public PHPParser() { - } + //ast stack + private final static int AstStackIncrement = 100; + /** The stack of node. */ + private static AstNode[] astStack; + /** The cursor in expression stack. */ + private static int expressionPtr; public final void setFileToParse(final IFile fileToParse) { this.fileToParse = fileToParse; } + public PHPParser() { + } + public PHPParser(final IFile fileToParse) { this(new StringReader("")); this.fileToParse = fileToParse; @@ -66,6 +74,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_input_stream = new SimpleCharStream(stream, 1, 1); } ReInit(new StringReader(strEval)); + astStack = new AstNode[AstStackIncrement]; phpTest(); } @@ -76,6 +85,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_input_stream = new SimpleCharStream(stream, 1, 1); } ReInit(stream); + astStack = new AstNode[AstStackIncrement]; phpFile(); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use Options | File Templates. @@ -88,6 +98,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_input_stream = new SimpleCharStream(stream, 1, 1); } ReInit(stream); + astStack = new AstNode[AstStackIncrement]; phpFile(); } @@ -99,6 +110,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_input_stream = new SimpleCharStream(stream, 1, 1); } ReInit(stream); + astStack = new AstNode[AstStackIncrement]; try { parse(); } catch (ParseException e) { @@ -223,6 +235,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_input_stream = new SimpleCharStream(stream, 1, 1); } ReInit(stream); + astStack = new AstNode[AstStackIncrement]; try { parse(); } catch (ParseException e) { @@ -252,7 +265,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon } } - public static final void parse() throws ParseException { + private static final void parse() throws ParseException { phpFile(); } @@ -604,6 +617,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon PHPVarDeclaration variableDeclaration; jj_consume_token(VAR); variableDeclaration = VariableDeclarator(); + outlineInfo.addVariable(variableDeclaration.getVariable().getName()); if (currentSegment != null) { currentSegment.add(variableDeclaration); } @@ -643,7 +657,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_consume_token(ASSIGN); try { varValue = VariableInitializer(); - {if (true) return new PHPVarDeclaration(currentSegment,varName.substring(1),pos,varValue);} + {if (true) return new PHPVarDeclaration(currentSegment,varName,pos,varValue);} } catch (ParseException e) { errorMessage = "Literal expression expected in variable initializer"; errorLevel = ERROR; @@ -665,7 +679,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon final StringBuffer buff = new StringBuffer(); try { expr = Variable(); - buff.append(expr); + buff.append(expr); label_5: while (true) { if (jj_2_1(2)) { @@ -674,7 +688,7 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon break label_5; } expr = VariableSuffix(); - buff.append(expr); + buff.append(expr); } {if (true) return buff.toString();} } catch (ParseException e) { @@ -703,21 +717,21 @@ public final class PHPParser extends PHPParserSuperclass implements PHPParserCon jj_la1[11] = jj_gen; ; } - if (expr == null) { + if (expr == null && !assigning) { if (currentFunction != null) { PHPVarDeclaration var = currentFunction.getParameter(token.image.substring(1)); if (var != null) { var.getVariable().setUsed(true); } } - {if (true) return token.image;} + {if (true) return token.image.substring(1);} } {if (true) return token + "{" + expr + "}";} break; case DOLLAR: jj_consume_token(DOLLAR); expr = VariableName(); - {if (true) return "$" + expr;} + {if (true) return expr;} break; default: jj_la1[12] = jj_gen; @@ -938,6 +952,7 @@ final StringBuffer buff = new StringBuffer("("); functionToken = jj_consume_token(FUNCTION); try { functionDeclaration = MethodDeclarator(); + outlineInfo.addVariable(functionDeclaration.getName()); } catch (ParseException e) { if (errorMessage != null) { {if (true) throw e;} @@ -1075,7 +1090,7 @@ final StringBuffer buff = new StringBuffer("("); } variableDeclaration = VariableDeclarator(); if (token != null) { - variableDeclaration.getVariable().setPrefix("@"); + variableDeclaration.getVariable().setReference(true); } {if (true) return variableDeclaration;} throw new Error("Missing return statement in function"); @@ -1085,39 +1100,39 @@ final StringBuffer buff = new StringBuffer("("); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STRING: jj_consume_token(STRING); - {if (true) return "string";} + {if (true) return "string";} break; case BOOL: jj_consume_token(BOOL); - {if (true) return "bool";} + {if (true) return "bool";} break; case BOOLEAN: jj_consume_token(BOOLEAN); - {if (true) return "boolean";} + {if (true) return "boolean";} break; case REAL: jj_consume_token(REAL); - {if (true) return "real";} + {if (true) return "real";} break; case DOUBLE: jj_consume_token(DOUBLE); - {if (true) return "double";} + {if (true) return "double";} break; case FLOAT: jj_consume_token(FLOAT); - {if (true) return "float";} + {if (true) return "float";} break; case INT: jj_consume_token(INT); - {if (true) return "int";} + {if (true) return "int";} break; case INTEGER: jj_consume_token(INTEGER); - {if (true) return "integer";} + {if (true) return "integer";} break; case OBJECT: jj_consume_token(OBJECT); - {if (true) return "object";} + {if (true) return "object";} break; default: jj_la1[25] = jj_gen; @@ -1134,51 +1149,64 @@ final StringBuffer buff = new StringBuffer("("); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PRINT: expr = PrintExpression(); - {if (true) return expr;} + {if (true) return expr;} break; case LIST: expr = ListExpression(); - {if (true) return expr;} + {if (true) return expr;} break; - case ARRAY: - case NEW: - case NULL: - case TRUE: - case FALSE: - case INTEGER_LITERAL: - case FLOATING_POINT_LITERAL: - case STRING_LITERAL: - case IDENTIFIER: - case LPAREN: - case AT: - case DOLLAR: - case BANG: - case INCR: - case DECR: - case PLUS: - case MINUS: - case BIT_AND: - case DOLLAR_ID: - expr = ConditionalExpression(); - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case ASSIGN: - case PLUSASSIGN: - case MINUSASSIGN: - case STARASSIGN: - case SLASHASSIGN: - case ANDASSIGN: - case ORASSIGN: - case XORASSIGN: - case DOTASSIGN: - case REMASSIGN: - case TILDEEQUAL: - case LSHIFTASSIGN: - case RSIGNEDSHIFTASSIGN: - assignOperator = AssignmentOperator(); - try { - expr2 = Expression(); - {if (true) return expr + assignOperator + expr2;} - } catch (ParseException e) { + default: + jj_la1[26] = jj_gen; + if (jj_2_3(2147483647)) { + expr = varAssignation(); + {if (true) return expr;} + } else { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ARRAY: + case NEW: + case NULL: + case TRUE: + case FALSE: + case INTEGER_LITERAL: + case FLOATING_POINT_LITERAL: + case STRING_LITERAL: + case IDENTIFIER: + case LPAREN: + case AT: + case DOLLAR: + case BANG: + case INCR: + case DECR: + case PLUS: + case MINUS: + case BIT_AND: + case DOLLAR_ID: + expr = ConditionalExpression(); + {if (true) return expr;} + break; + default: + jj_la1[27] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + } + } + throw new Error("Missing return statement in function"); + } + +/** + * A Variable assignation. + * varName (an assign operator) any expression + */ + static final public String varAssignation() throws ParseException { + String varName,assignOperator,expr2; + PHPVarDeclaration variable; + final int pos = SimpleCharStream.getPosition(); + varName = VariableDeclaratorId(); + assignOperator = AssignmentOperator(); + try { + expr2 = Expression(); + } catch (ParseException e) { if (errorMessage != null) { {if (true) throw e;} } @@ -1187,19 +1215,8 @@ final StringBuffer buff = new StringBuffer("("); errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = jj_input_stream.getPosition() + 1; {if (true) throw e;} - } - break; - default: - jj_la1[26] = jj_gen; - ; - } - {if (true) return expr;} - break; - default: - jj_la1[27] = jj_gen; - jj_consume_token(-1); - throw new ParseException(); } + {if (true) return varName + assignOperator + expr2;} throw new Error("Missing return statement in function"); } @@ -1207,55 +1224,55 @@ final StringBuffer buff = new StringBuffer("("); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case ASSIGN: jj_consume_token(ASSIGN); - {if (true) return "=";} + {if (true) return "=";} break; case STARASSIGN: jj_consume_token(STARASSIGN); - {if (true) return "*=";} + {if (true) return "*=";} break; case SLASHASSIGN: jj_consume_token(SLASHASSIGN); - {if (true) return "/=";} + {if (true) return "/=";} break; case REMASSIGN: jj_consume_token(REMASSIGN); - {if (true) return "%=";} + {if (true) return "%=";} break; case PLUSASSIGN: jj_consume_token(PLUSASSIGN); - {if (true) return "+=";} + {if (true) return "+=";} break; case MINUSASSIGN: jj_consume_token(MINUSASSIGN); - {if (true) return "-=";} + {if (true) return "-=";} break; case LSHIFTASSIGN: jj_consume_token(LSHIFTASSIGN); - {if (true) return "<<=";} + {if (true) return "<<=";} break; case RSIGNEDSHIFTASSIGN: jj_consume_token(RSIGNEDSHIFTASSIGN); - {if (true) return ">>=";} + {if (true) return ">>=";} break; case ANDASSIGN: jj_consume_token(ANDASSIGN); - {if (true) return "&=";} + {if (true) return "&=";} break; case XORASSIGN: jj_consume_token(XORASSIGN); - {if (true) return "|=";} + {if (true) return "|=";} break; case ORASSIGN: jj_consume_token(ORASSIGN); - {if (true) return "|=";} + {if (true) return "|=";} break; case DOTASSIGN: jj_consume_token(DOTASSIGN); - {if (true) return ".=";} + {if (true) return ".=";} break; case TILDEEQUAL: jj_consume_token(TILDEEQUAL); - {if (true) return "~=";} + {if (true) return "~=";} break; default: jj_la1[28] = jj_gen; @@ -1818,13 +1835,13 @@ final Token token; case BANG: jj_consume_token(BANG); expr = UnaryExpression(); - {if (true) return "!" + expr;} + {if (true) return "!" + expr;} break; default: jj_la1[53] = jj_gen; - if (jj_2_3(2147483647)) { + if (jj_2_4(2147483647)) { expr = CastExpression(); - {if (true) return expr;} + {if (true) return expr;} } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case ARRAY: @@ -1833,7 +1850,7 @@ final Token token; case DOLLAR: case DOLLAR_ID: expr = PostfixExpression(); - {if (true) return expr;} + {if (true) return expr;} break; case NULL: case TRUE: @@ -1842,7 +1859,7 @@ final Token token; case FLOATING_POINT_LITERAL: case STRING_LITERAL: expr = Literal(); - {if (true) return expr;} + {if (true) return expr;} break; case LPAREN: jj_consume_token(LPAREN); @@ -1852,8 +1869,8 @@ final Token token; } catch (ParseException e) { errorMessage = "')' expected"; errorLevel = ERROR; - errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = jj_input_stream.getPosition() + 1; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; {if (true) throw e;} } {if (true) return "("+expr+")";} @@ -1933,7 +1950,7 @@ final String type, expr; final Token identifier; String expr; final StringBuffer buff = new StringBuffer(); - if (jj_2_4(2)) { + if (jj_2_5(2)) { identifier = jj_consume_token(IDENTIFIER); jj_consume_token(STATICCLASSACCESS); expr = ClassIdentifier(); @@ -2006,17 +2023,17 @@ final String type, expr; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case IDENTIFIER: token = jj_consume_token(IDENTIFIER); - {if (true) return token.image;} + {if (true) return token.image;} break; case NEW: jj_consume_token(NEW); expr = ClassIdentifier(); - {if (true) return "new " + expr;} + {if (true) return "new " + expr;} break; case DOLLAR: case DOLLAR_ID: expr = VariableDeclaratorId(); - {if (true) return expr;} + {if (true) return expr;} break; default: jj_la1[61] = jj_gen; @@ -2055,12 +2072,12 @@ final String type, expr; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case IDENTIFIER: token = jj_consume_token(IDENTIFIER); - {if (true) return token.image;} + {if (true) return token.image;} break; case DOLLAR: case DOLLAR_ID: expr = VariableDeclaratorId(); - {if (true) return expr;} + {if (true) return expr;} break; default: jj_la1[63] = jj_gen; @@ -2075,12 +2092,12 @@ final String type, expr; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LPAREN: expr = Arguments(); - {if (true) return expr;} + {if (true) return expr;} break; case CLASSACCESS: case LBRACKET: expr = VariableSuffix(); - {if (true) return expr;} + {if (true) return expr;} break; default: jj_la1[64] = jj_gen; @@ -2212,24 +2229,24 @@ final String type, expr; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case INTEGER_LITERAL: token = jj_consume_token(INTEGER_LITERAL); - {if (true) return token.image;} + {if (true) return token.image;} break; case FLOATING_POINT_LITERAL: token = jj_consume_token(FLOATING_POINT_LITERAL); - {if (true) return token.image;} + {if (true) return token.image;} break; case STRING_LITERAL: token = jj_consume_token(STRING_LITERAL); - {if (true) return token.image;} + {if (true) return token.image;} break; case TRUE: case FALSE: expr = BooleanLiteral(); - {if (true) return expr;} + {if (true) return expr;} break; case NULL: jj_consume_token(NULL); - {if (true) return "null";} + {if (true) return "null";} break; default: jj_la1[68] = jj_gen; @@ -2243,11 +2260,11 @@ final String type, expr; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case TRUE: jj_consume_token(TRUE); - {if (true) return "true";} + {if (true) return "true";} break; case FALSE: jj_consume_token(FALSE); - {if (true) return "false";} + {if (true) return "false";} break; default: jj_la1[69] = jj_gen; @@ -2323,23 +2340,23 @@ final StringBuffer buff = new StringBuffer(); try { expr = Expression(); } catch (ParseException e) { - errorMessage = "expression expected after a comma in argument list"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list"; errorLevel = ERROR; - errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = jj_input_stream.getPosition() + 1; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; {if (true) throw e;} } - buff.append(",").append(expr); + buff.append(",").append(expr); } {if (true) return buff.toString();} throw new Error("Missing return statement in function"); } /** - * A Statement without break + * A Statement without break. */ static final public void StatementNoBreak() throws ParseException { - if (jj_2_5(2)) { + if (jj_2_6(2)) { Expression(); try { jj_consume_token(SEMICOLON); @@ -2352,7 +2369,7 @@ final StringBuffer buff = new StringBuffer(); {if (true) throw e;} } } - } else if (jj_2_6(2)) { + } else if (jj_2_7(2)) { LabeledStatement(); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { @@ -2375,8 +2392,8 @@ final StringBuffer buff = new StringBuffer(); } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected"; errorLevel = ERROR; - errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = jj_input_stream.getPosition() + 1; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; {if (true) throw e;} } break; @@ -2437,7 +2454,7 @@ final StringBuffer buff = new StringBuffer(); } /** - * A Normal statement + * A Normal statement. */ static final public void Statement() throws ParseException { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { @@ -2491,6 +2508,9 @@ final StringBuffer buff = new StringBuffer(); } } +/** + * An html block inside a php syntax. + */ static final public void htmlBlock() throws ParseException { jj_consume_token(PHPEND); label_23: @@ -2505,17 +2525,25 @@ final StringBuffer buff = new StringBuffer(); } phpEchoBlock(); } - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case PHPSTARTLONG: - jj_consume_token(PHPSTARTLONG); - break; - case PHPSTARTSHORT: - jj_consume_token(PHPSTARTSHORT); - break; - default: - jj_la1[76] = jj_gen; - jj_consume_token(-1); - throw new ParseException(); + try { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case PHPSTARTLONG: + jj_consume_token(PHPSTARTLONG); + break; + case PHPSTARTSHORT: + jj_consume_token(PHPSTARTSHORT); + break; + default: + jj_la1[76] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + } catch (ParseException e) { + errorMessage = "End of file unexpected, ' jj_gen) { @@ -6381,6 +6450,7 @@ final int pos = jj_input_stream.getPosition(); case 4: jj_3_5(); break; case 5: jj_3_6(); break; case 6: jj_3_7(); break; + case 7: jj_3_8(); break; } } p = p.next;