X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.jj b/net.sourceforge.phpeclipse/src/test/PHPParser.jj index 823b11e..628b52a 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -83,6 +83,12 @@ public final class PHPParser extends PHPParserSuperclass { private static AstNode[] nodes; /** The cursor in expression stack. */ private static int nodePtr; + private static VariableDeclaration[] variableDeclarationStack; + private static int variableDeclarationPtr; + private static Statement[] statementStack; + private static int statementPtr; + private static ElseIf[] elseIfStack; + private static int elseIfPtr; public final void setFileToParse(final IFile fileToParse) { this.fileToParse = fileToParse; @@ -101,7 +107,11 @@ public final class PHPParser extends PHPParserSuperclass { */ private static final void init() { nodes = new AstNode[AstStackIncrement]; + statementStack = new Statement[AstStackIncrement]; + elseIfStack = new ElseIf[AstStackIncrement]; nodePtr = -1; + statementPtr = -1; + elseIfPtr = -1; htmlStart = 0; } @@ -122,6 +132,45 @@ public final class PHPParser extends PHPParserSuperclass { } } + private static final void pushOnVariableDeclarationStack(VariableDeclaration var) { + try { + variableDeclarationStack[++variableDeclarationPtr] = var; + } catch (IndexOutOfBoundsException e) { + int oldStackLength = variableDeclarationStack.length; + VariableDeclaration[] oldStack = variableDeclarationStack; + variableDeclarationStack = new VariableDeclaration[oldStackLength + AstStackIncrement]; + System.arraycopy(oldStack, 0, variableDeclarationStack, 0, oldStackLength); + variableDeclarationPtr = oldStackLength; + variableDeclarationStack[variableDeclarationPtr] = var; + } + } + + private static final void pushOnStatementStack(Statement statement) { + try { + statementStack[++statementPtr] = statement; + } catch (IndexOutOfBoundsException e) { + int oldStackLength = statementStack.length; + Statement[] oldStack = statementStack; + statementStack = new Statement[oldStackLength + AstStackIncrement]; + System.arraycopy(oldStack, 0, statementStack, 0, oldStackLength); + statementPtr = oldStackLength; + statementStack[statementPtr] = statement; + } + } + + private static final void pushOnElseIfStack(ElseIf elseIf) { + try { + elseIfStack[++elseIfPtr] = elseIf; + } catch (IndexOutOfBoundsException e) { + int oldStackLength = elseIfStack.length; + ElseIf[] oldStack = elseIfStack; + elseIfStack = new ElseIf[oldStackLength + AstStackIncrement]; + System.arraycopy(oldStack, 0, elseIfStack, 0, oldStackLength); + elseIfPtr = oldStackLength; + elseIfStack[elseIfPtr] = elseIf; + } + } + public static final void phpParserTester(final String strEval) throws CoreException, ParseException { PHPParserTokenManager.SwitchTo(PHPParserTokenManager.PHPPARSING); final StringReader stream = new StringReader(strEval); @@ -168,7 +217,9 @@ public final class PHPParser extends PHPParserSuperclass { init(); try { parse(); - //PHPeclipsePlugin.log(1,phpDocument.toString()); + phpDocument = new PHPDocument(null); + phpDocument.nodes = nodes; + PHPeclipsePlugin.log(1,phpDocument.toString()); } catch (ParseException e) { processParseException(e); } @@ -518,29 +569,29 @@ MORE : /* LITERALS */ TOKEN : { - < INTEGER_LITERAL: + (["l","L"])? | (["l","L"])? | (["l","L"])? > | - < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > + <#DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | - < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > + <#HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | - < #OCTAL_LITERAL: "0" (["0"-"7"])* > + <#OCTAL_LITERAL: "0" (["0"-"7"])* > | - < FLOATING_POINT_LITERAL: + )? (["f","F","d","D"])? | "." (["0"-"9"])+ ()? (["f","F","d","D"])? | (["0"-"9"])+ (["f","F","d","D"])? | (["0"-"9"])+ ()? ["f","F","d","D"] > | - < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > + <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | - < STRING_LITERAL: ( | | )> -| < STRING_1: + | | )> +| -| < STRING_2: +| -| < STRING_3: +| variableDeclaration = VariableDeclarator() - { - outlineInfo.addVariable(new String(variableDeclaration.name)); - if (currentSegment != null) { - currentSegment.add(variableDeclaration); - } - } - ( - variableDeclaration = VariableDeclarator() - { - if (currentSegment != null) { - currentSegment.add(variableDeclaration); - } - } + {arrayList.add(variableDeclaration); + outlineInfo.addVariable(new String(variableDeclaration.name)); + currentSegment.add(variableDeclaration);} + ( variableDeclaration = VariableDeclarator() + {arrayList.add(variableDeclaration); + outlineInfo.addVariable(new String(variableDeclaration.name)); + currentSegment.add(variableDeclaration);} )* try { @@ -845,6 +893,12 @@ FieldDeclaration FieldDeclaration() : errorEnd = jj_input_stream.getPosition() + 1; throw e; } + + {list = new VariableDeclaration[arrayList.size()]; + arrayList.toArray(list); + return new FieldDeclaration(list, + pos, + SimpleCharStream.getPosition());} } VariableDeclaration VariableDeclarator() : @@ -867,10 +921,18 @@ VariableDeclaration VariableDeclarator() : throw e; } ] - {return new VariableDeclaration(currentSegment, + { + if (initializer == null) { + return new VariableDeclaration(currentSegment, varName.toCharArray(), - initializer, - pos);} + pos, + jj_input_stream.getPosition()); + } + return new VariableDeclaration(currentSegment, + varName.toCharArray(), + initializer, + pos); + } } /** @@ -998,12 +1060,14 @@ Expression VariableInitializer() : ArrayVariableDeclaration ArrayVariable() : { -Expression expr; -Expression expr2 = null; +Expression expr,expr2; } { - expr = Expression() [ expr2 = Expression()] + expr = Expression() + [ expr2 = Expression() {return new ArrayVariableDeclaration(expr,expr2);} + ] + {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());} } ArrayVariableDeclaration[] ArrayInitializer() : @@ -1020,7 +1084,10 @@ ArrayVariableDeclaration[] ArrayInitializer() : ] [ {list.add(null);}] - {return (ArrayVariableDeclaration[]) list.toArray();} + { + ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()]; + list.toArray(vars); + return vars;} } /** @@ -1039,9 +1106,7 @@ MethodDeclaration MethodDeclaration() : functionDeclaration = MethodDeclarator() {outlineInfo.addVariable(new String(functionDeclaration.name));} } catch (ParseException e) { - if (errorMessage != null) { - throw e; - } + if (errorMessage != null) throw e; errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function identifier expected"; errorLevel = ERROR; errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; @@ -1079,8 +1144,7 @@ MethodDeclaration MethodDeclarator() : final int pos = SimpleCharStream.getPosition(); } { - [ reference = ] - identifier = + [reference = ] identifier = formalParameters = FormalParameters() {return new MethodDeclaration(currentSegment, identifier.image.toCharArray(), @@ -1437,9 +1501,7 @@ Expression MultiplicativeExpression() : try { expr = UnaryExpression() } catch (ParseException e) { - if (errorMessage != null) { - throw e; - } + if (errorMessage != null) throw e; errorMessage = "unexpected token '"+e.currentToken.next.image+"'"; errorLevel = ERROR; errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; @@ -1468,8 +1530,7 @@ Expression UnaryExpression() : expr = UnaryExpressionNoPrefix() {return new PrefixedUnaryExpression(expr,OperatorIds.AND,pos);} | - expr = AtUnaryExpression() - {return expr;} + expr = AtUnaryExpression() {return expr;} } Expression AtUnaryExpression() : @@ -1748,18 +1809,24 @@ ArgumentDeclaration[] args = null; {return new FunctionCall(func,args,SimpleCharStream.getPosition());} } +/** + * An argument list is a list of arguments separated by comma : + * argumentDeclaration() (, argumentDeclaration)* + * @return an array of arguments + */ ArgumentDeclaration[] ArgumentList() : { -Expression expr; +ArgumentDeclaration arg; final ArrayList list = new ArrayList(); +ArgumentDeclaration argument; } { - expr = Expression() - {list.add(expr);} + arg = argumentDeclaration() + {list.add(arg);} ( try { - expr = Expression() - {list.add(expr);} + arg = argumentDeclaration() + {list.add(arg);} } catch (ParseException e) { errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. An expression expected after a comma in argument list"; errorLevel = ERROR; @@ -1768,10 +1835,51 @@ final ArrayList list = new ArrayList(); throw e; } )* - {return (ArgumentDeclaration[]) list.toArray();} + { + ArgumentDeclaration[] args = new ArgumentDeclaration[list.size()]; + list.toArray(args); + return args;} } /** + * Here is an argument declaration. + * It's [&]$variablename[=variableInitializer] + */ +ArgumentDeclaration argumentDeclaration() : +{ + boolean reference = false; + String varName; + Expression initializer = null; + final int pos = SimpleCharStream.getPosition(); +} +{ + [ {reference = true;}] + varName = VariableDeclaratorId() + [ + + try { + initializer = VariableInitializer() + } catch (ParseException e) { + errorMessage = "Literal expression expected in variable initializer"; + errorLevel = ERROR; + errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = jj_input_stream.getPosition() + 1; + throw e; + } + ] + { + if (initializer == null) { + return new ArgumentDeclaration(varName.toCharArray(), + reference, + pos); + } + return new ArgumentDeclaration(varName.toCharArray(), + reference, + initializer, + pos); + } +} +/** * A Statement without break. */ Statement StatementNoBreak() : @@ -1860,7 +1968,7 @@ HTMLBlock htmlBlock() : throw e; } { - nbNodes = nodePtr-startIndex; + nbNodes = nodePtr-startIndex - 1; blockNodes = new AstNode[nbNodes]; System.arraycopy(nodes,startIndex,blockNodes,0,nbNodes); return new HTMLBlock(nodes);} @@ -1967,8 +2075,18 @@ ListExpression ListExpression() : throw e; } [ expression = Expression() - {return new ListExpression((String[]) list.toArray(),expression,pos,SimpleCharStream.getPosition());}] - {return new ListExpression((String[]) list.toArray(),null,pos,SimpleCharStream.getPosition());} + { + String[] strings = new String[list.size()]; + list.toArray(strings); + return new ListExpression(strings, + expression, + pos, + SimpleCharStream.getPosition());} + ] + { + String[] strings = new String[list.size()]; + list.toArray(strings); + return new ListExpression(strings,pos,SimpleCharStream.getPosition());} } /** @@ -1990,7 +2108,10 @@ EchoStatement EchoStatement() : )* try { - {return new EchoStatement((Expression[]) expressions.toArray(),pos);} + { + Expression[] exprs = new Expression[expressions.size()]; + expressions.toArray(exprs); + return new EchoStatement(exprs,pos);} } catch (ParseException e) { if (e.currentToken.next.kind != 4) { errorMessage = "';' expected after 'echo' statement"; @@ -2019,10 +2140,13 @@ GlobalStatement GlobalStatement() : )* try { - {global = new GlobalStatement(currentSegment, - (String[]) vars.toArray(), - pos, - SimpleCharStream.getPosition()); + { + String[] strings = new String[vars.size()]; + vars.toArray(strings); + global = new GlobalStatement(currentSegment, + strings, + pos, + SimpleCharStream.getPosition()); currentSegment.add(global); return global;} } catch (ParseException e) { @@ -2045,7 +2169,10 @@ StaticStatement StaticStatement() : ( expr = VariableDeclarator() {vars.add(new String(expr.name));})* try { - {return new StaticStatement((String[])vars.toArray(), + { + String[] strings = new String[vars.size()]; + vars.toArray(strings); + return new StaticStatement(strings, pos, SimpleCharStream.getPosition());} } catch (ParseException e) { @@ -2078,6 +2205,8 @@ LabeledStatement LabeledStatement() : Block Block() : { final int pos = SimpleCharStream.getPosition(); + final ArrayList list = new ArrayList(); + Statement statement; } { try { @@ -2089,7 +2218,8 @@ Block Block() : errorEnd = jj_input_stream.getPosition() + 1; throw e; } - ( BlockStatement() | htmlBlock())* + ( statement = BlockStatement() {list.add(statement);} + | statement = htmlBlock() {list.add(statement);})* try { } catch (ParseException e) { @@ -2099,6 +2229,10 @@ Block Block() : errorEnd = jj_input_stream.getPosition() + 1; throw e; } + { + Statement[] statements = new Statement[list.size()]; + list.toArray(statements); + return new Block(statements,pos,SimpleCharStream.getPosition());} } Statement BlockStatement() : @@ -2133,18 +2267,32 @@ VariableDeclaration[] LocalVariableDeclaration() : var = LocalVariableDeclarator() {list.add(var);} ( var = LocalVariableDeclarator() {list.add(var);})* - {return (VariableDeclaration[]) list.toArray();} + { + VariableDeclaration[] vars = new VariableDeclaration[list.size()]; + list.toArray(vars); + return vars;} } VariableDeclaration LocalVariableDeclarator() : { final String varName; - Expression init = null; + Expression initializer = null; final int pos = SimpleCharStream.getPosition(); } { - varName = VariableDeclaratorId() [ init = Expression() ] - {return new VariableDeclaration(varName.toCharArray(),init,pos);} + varName = VariableDeclaratorId() [ initializer = Expression() ] + { + if (initializer == null) { + return new VariableDeclaration(currentSegment, + varName.toCharArray(), + pos, + jj_input_stream.getPosition()); + } + return new VariableDeclaration(currentSegment, + varName.toCharArray(), + initializer, + pos); + } } EmptyStatement EmptyStatement() : @@ -2159,19 +2307,23 @@ EmptyStatement EmptyStatement() : Statement StatementExpression() : { - Expression expr; + Expression expr,expr2; + int operator; } { expr = PreIncDecExpression() {return expr;} | expr = PrimaryExpression() - [ {expr = new PostfixedUnaryExpression(expr, + [ {return new PostfixedUnaryExpression(expr, OperatorIds.PLUS_PLUS, SimpleCharStream.getPosition());} - | {expr = new PostfixedUnaryExpression(expr, + | {return new PostfixedUnaryExpression(expr, OperatorIds.MINUS_MINUS, SimpleCharStream.getPosition());} - | AssignmentOperator() Expression() ] + | operator = AssignmentOperator() expr2 = Expression() + {return new BinaryExpression(expr,expr2,operator);} + ] + {return expr;} } SwitchStatement SwitchStatement() : @@ -2226,7 +2378,10 @@ AbstractCase[] switchStatementBrace() : ( cas = switchLabel0() {cases.add(cas);})* try { - {return (AbstractCase[]) cases.toArray();} + { + AbstractCase[] abcase = new AbstractCase[cases.size()]; + cases.toArray(abcase); + return abcase;} } catch (ParseException e) { errorMessage = "'}' expected"; errorLevel = ERROR; @@ -2269,7 +2424,10 @@ AbstractCase[] switchStatementColon(final int start, final int end) : } try { - {return (AbstractCase[]) cases.toArray();} + { + AbstractCase[] abcase = new AbstractCase[cases.size()]; + cases.toArray(abcase); + return abcase;} } catch (ParseException e) { errorMessage = "';' expected after 'endswitch' keyword"; errorLevel = ERROR; @@ -2291,10 +2449,13 @@ AbstractCase switchLabel0() : ( statement = BlockStatementNoBreak() {stmts.add(statement);} | statement = htmlBlock() {stmts.add(statement);})* [ statement = BreakStatement() {stmts.add(statement);}] - {if (expr == null) {//it's a default - return new DefaultCase((Statement[]) stmts.toArray(),pos,SimpleCharStream.getPosition()); + { + Statement[] stmtsArray = new Statement[stmts.size()]; + stmts.toArray(stmtsArray); + if (expr == null) {//it's a default + return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition()); } - return new Case(expr,(Statement[]) stmts.toArray(),pos,SimpleCharStream.getPosition());} + return new Case(expr,stmtsArray,pos,SimpleCharStream.getPosition());} } /** @@ -2405,17 +2566,23 @@ Expression Condition(final String keyword) : IfStatement IfStatement0(Expression condition, final int start,final int end) : { Statement statement; + Statement stmt; + final Statement[] statementsArray; ElseIf elseifStatement; Else elseStatement = null; - ArrayList stmts = new ArrayList(); - ArrayList elseifs = new ArrayList(); + ArrayList stmts; + final ArrayList elseIfList = new ArrayList(); + ElseIf[] elseIfs; int pos = SimpleCharStream.getPosition(); + int endStatements; } { + {stmts = new ArrayList();} ( statement = Statement() {stmts.add(statement);} | statement = htmlBlock() {stmts.add(statement);})* - (elseifStatement = ElseIfStatementColon() {elseifs.add(elseifStatement);})* + {endStatements = SimpleCharStream.getPosition();} + (elseifStatement = ElseIfStatementColon() {elseIfList.add(elseifStatement);})* [elseStatement = ElseStatementColon()] {try { @@ -2439,11 +2606,6 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) : } try { - {return new IfStatement(condition, - (ElseIf[]) elseifs.toArray(), - elseStatement, - pos, - SimpleCharStream.getPosition());} } catch (ParseException e) { errorMessage = "';' expected after 'endif' keyword"; errorLevel = ERROR; @@ -2451,10 +2613,31 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) : errorEnd = jj_input_stream.getPosition() + 1; throw e; } + { + elseIfs = new ElseIf[elseIfList.size()]; + elseIfList.toArray(elseIfs); + if (stmts.size() == 1) { + return new IfStatement(condition, + (Statement) stmts.get(0), + elseIfs, + elseStatement, + pos, + SimpleCharStream.getPosition()); + } else { + statementsArray = new Statement[stmts.size()]; + stmts.toArray(statementsArray); + return new IfStatement(condition, + new Block(statementsArray,pos,endStatements), + elseIfs, + elseStatement, + pos, + SimpleCharStream.getPosition()); + } + } + | - (statement = Statement() | statement = htmlBlock()) - {stmts.add(statement);} - ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseifs.add(elseifStatement);})* + (stmt = Statement() | stmt = htmlBlock()) + ( LOOKAHEAD(1) elseifStatement = ElseIfStatement() {elseIfList.add(elseifStatement);})* [ LOOKAHEAD(1) try { @@ -2472,11 +2655,15 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) : throw e; } ] - {return new IfStatement(condition, - (ElseIf[]) elseifs.toArray(), - elseStatement, - pos, - SimpleCharStream.getPosition());} + { + elseIfs = new ElseIf[elseIfList.size()]; + elseIfList.toArray(elseIfs); + return new IfStatement(condition, + stmt, + elseIfs, + elseStatement, + pos, + SimpleCharStream.getPosition());} } ElseIf ElseIfStatementColon() : @@ -2490,7 +2677,10 @@ ElseIf ElseIfStatementColon() : condition = Condition("elseif") ( statement = Statement() {list.add(statement);} | statement = htmlBlock() {list.add(statement);})* - {return new ElseIf(condition,(Statement[]) list.toArray(),pos,SimpleCharStream.getPosition());} + { + Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());} } Else ElseStatementColon() : @@ -2502,7 +2692,10 @@ Else ElseStatementColon() : { ( statement = Statement() {list.add(statement);} | statement = htmlBlock() {list.add(statement);})* - {return new Else((Statement[]) list.toArray(),pos,SimpleCharStream.getPosition());} + { + Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return new Else(stmtsArray,pos,SimpleCharStream.getPosition());} } ElseIf ElseIfStatement() : @@ -2514,7 +2707,10 @@ ElseIf ElseIfStatement() : } { condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/} - {return new ElseIf(condition,(Statement[]) list.toArray(),pos,SimpleCharStream.getPosition());} + { + Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());} } WhileStatement WhileStatement() : @@ -2559,7 +2755,10 @@ Statement WhileStatement0(final int start, final int end) : } try { - {return new Block((Statement[]) stmts.toArray(),pos,SimpleCharStream.getPosition());} + { + Statement[] stmtsArray = new Statement[stmts.size()]; + stmts.toArray(stmtsArray); + return new Block(stmtsArray,pos,SimpleCharStream.getPosition());} } catch (ParseException e) { errorMessage = "';' expected after 'endwhile' keyword"; errorLevel = ERROR; @@ -2721,7 +2920,10 @@ final int startBlock, endBlock; } try { - {return new ForStatement(initializations,condition,increments,new Block((Statement[])list.toArray(),startBlock,endBlock),pos,SimpleCharStream.getPosition());} + { + Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return new ForStatement(initializations,condition,increments,new Block(stmtsArray,startBlock,endBlock),pos,SimpleCharStream.getPosition());} } catch (ParseException e) { errorMessage = "';' expected after 'endfor' keyword"; errorLevel = ERROR; @@ -2753,7 +2955,10 @@ Statement[] StatementExpressionList() : { expr = StatementExpression() {list.add(expr);} ( StatementExpression() {list.add(expr);})* - {return (Statement[]) list.toArray();} + { + Statement[] stmtsArray = new Statement[list.size()]; + list.toArray(stmtsArray); + return stmtsArray;} } Continue ContinueStatement() :