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;
*/
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;
}
}
}
+ 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);
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);
}
if (superclassName == null) {
classDeclaration = new ClassDeclaration(currentSegment,
className.image.toCharArray(),
- superclassName.image.toCharArray(),
pos,
0);
} else {
classDeclaration = new ClassDeclaration(currentSegment,
className.image.toCharArray(),
+ superclassName.image.toCharArray(),
pos,
0);
}
FieldDeclaration FieldDeclaration() :
{
VariableDeclaration variableDeclaration;
+ variableDeclarationPtr = 0;
+ variableDeclarationStack = new VariableDeclaration[AstStackIncrement];
+ VariableDeclaration[] list;
+ final int pos = SimpleCharStream.getPosition();
}
{
<VAR> variableDeclaration = VariableDeclarator()
{
+ pushOnVariableDeclarationStack(variableDeclaration);
outlineInfo.addVariable(new String(variableDeclaration.name));
- if (currentSegment != null) {
- currentSegment.add(variableDeclaration);
- }
+ currentSegment.add(variableDeclaration);
}
( <COMMA>
variableDeclaration = VariableDeclarator()
- {
- if (currentSegment != null) {
- currentSegment.add(variableDeclaration);
- }
- }
+ {pushOnVariableDeclarationStack(variableDeclaration);
+ outlineInfo.addVariable(new String(variableDeclaration.name));
+ currentSegment.add(variableDeclaration);}
)*
try {
<SEMICOLON>
errorEnd = jj_input_stream.getPosition() + 1;
throw e;
}
+
+ {list = new VariableDeclaration[variableDeclarationPtr];
+ System.arraycopy(variableDeclarationStack,0,list,0,variableDeclarationPtr);
+ return new FieldDeclaration(list,
+ pos,
+ SimpleCharStream.getPosition());}
}
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);
+ }
}
/**
]
[<COMMA> {list.add(null);}]
<RPAREN>
- {return (ArrayVariableDeclaration[]) list.toArray();}
+ {
+ ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
+ list.toArray(vars);
+ return vars;}
}
/**
ArgumentDeclaration[] ArgumentList() :
{
-Expression expr;
+ArgumentDeclaration arg;
final ArrayList list = new ArrayList();
+ArgumentDeclaration argument;
}
{
- expr = Expression()
- {list.add(expr);}
+ arg = argumentDeclaration()
+ {list.add(arg);}
( <COMMA>
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;
throw e;
}
)*
- {return (ArgumentDeclaration[]) list.toArray();}
+ {
+ ArgumentDeclaration[] args = new ArgumentDeclaration[list.size()];
+ list.toArray(args);
+ return args;}
}
+ArgumentDeclaration argumentDeclaration() :
+{
+ boolean reference = false;
+ String varName;
+ Expression initializer = null;
+ final int pos = SimpleCharStream.getPosition();
+}
+{
+ [<BIT_AND> {reference = true;}]
+ varName = VariableDeclaratorId()
+ [
+ <ASSIGN>
+ 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.
*/
throw e;
}
[ <ASSIGN> 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());}
}
/**
)*
try {
<SEMICOLON>
- {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";
)*
try {
<SEMICOLON>
- {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) {
(<COMMA> expr = VariableDeclarator() {vars.add(new String(expr.name));})*
try {
<SEMICOLON>
- {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) {
Block Block() :
{
final int pos = SimpleCharStream.getPosition();
+ Statement[] statements;
+ Statement statement;
+ final int startingPtr = statementPtr;
+ final int length;
}
{
try {
errorEnd = jj_input_stream.getPosition() + 1;
throw e;
}
- ( BlockStatement() | htmlBlock())*
+ ( statement = BlockStatement() {pushOnStatementStack(statement);}
+ | statement = htmlBlock() {pushOnStatementStack(statement);})*
try {
<RBRACE>
} catch (ParseException e) {
errorEnd = jj_input_stream.getPosition() + 1;
throw e;
}
+ {
+ length = statementPtr-startingPtr+1;
+ statements = new Statement[length];
+ System.arraycopy(variableDeclarationStack,startingPtr+1,statements,0,length);
+ statementPtr = startingPtr;
+ return new Block(statements,pos,SimpleCharStream.getPosition());}
}
Statement BlockStatement() :
var = LocalVariableDeclarator()
{list.add(var);}
( <COMMA> 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() [ <ASSIGN> init = Expression() ]
- {return new VariableDeclaration(varName.toCharArray(),init,pos);}
+ varName = VariableDeclaratorId() [ <ASSIGN> 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() :
( cas = switchLabel0() {cases.add(cas);})*
try {
<RBRACE>
- {return (AbstractCase[]) cases.toArray();}
+ {
+ AbstractCase[] abcase = new AbstractCase[cases.size()];
+ cases.toArray(abcase);
+ return abcase;}
} catch (ParseException e) {
errorMessage = "'}' expected";
errorLevel = ERROR;
}
try {
<SEMICOLON>
- {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;
( 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());}
}
/**
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;
}
{
<COLON>
+ {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 {
}
try {
<SEMICOLON>
- {return new IfStatement(condition,
- (ElseIf[]) elseifs.toArray(),
- elseStatement,
- pos,
- SimpleCharStream.getPosition());}
} catch (ParseException e) {
errorMessage = "';' expected after 'endif' keyword";
errorLevel = ERROR;
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)
<ELSE>
try {
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() :
<ELSEIF> condition = Condition("elseif")
<COLON> ( 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() :
{
<ELSE> <COLON> ( 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() :
}
{
<ELSEIF> 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() :
}
try {
<SEMICOLON>
- {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;
}
try {
<SEMICOLON>
- {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;
{
expr = StatementExpression() {list.add(expr);}
(<COMMA> StatementExpression() {list.add(expr);})*
- {return (Statement[]) list.toArray();}
+ {
+ Statement[] stmtsArray = new Statement[list.size()];
+ list.toArray(stmtsArray);
+ return stmtsArray;}
}
Continue ContinueStatement() :