* given with JavaCC. You can get JavaCC at http://www.webgain.com
* You can test the parser with the PHPParserTestCase2.java
* @author Matthieu Casanova
+ * @version $Reference: 1.0$
*/
public final class PHPParser extends PHPParserSuperclass {
* Add an php node on the stack.
* @param node the node that will be added to the stack
*/
- private static final void pushOnAstNodes(AstNode node) {
+ private static final void pushOnAstNodes(final AstNode node) {
try {
nodes[++nodePtr] = node;
} catch (IndexOutOfBoundsException e) {
- int oldStackLength = nodes.length;
- AstNode[] oldStack = nodes;
+ final int oldStackLength = nodes.length;
+ final AstNode[] oldStack = nodes;
nodes = new AstNode[oldStackLength + AstStackIncrement];
System.arraycopy(oldStack, 0, nodes, 0, oldStackLength);
nodePtr = oldStackLength;
*/
public static final void createNewHTMLCode() {
final int currentPosition = SimpleCharStream.getPosition();
- if (currentPosition == htmlStart) {
+ if (currentPosition == htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) {
return;
}
final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
<#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
|
<STRING_LITERAL: (<STRING_1> | <STRING_2> | <STRING_3>)>
-| <STRING_1: "\"" ( ~["\""] | "\\\"" | "\\" )* "\"">
-| <STRING_2: "'" ( ~["'"] | "\\'" )* "'">
-| <STRING_3: "`" ( ~["`"] | "\\`" )* "`">
+| <STRING_1: "\"" ( ~["\"","\\"] | "\\" ~[] )* "\"">
+| <STRING_2: "'" ( ~["'","\\"] | "\\" ~[] )* "'">
+| <STRING_3: "`" ( ~["`","\\"] | "\\" ~[] )* "`">
}
/* IDENTIFIERS */
{
final Expression expr;
final int pos = SimpleCharStream.getPosition();
- PHPEchoBlock echoBlock;
+ final PHPEchoBlock echoBlock;
}
{
<PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] <PHPEND>
ClassDeclaration ClassDeclaration() :
{
final ClassDeclaration classDeclaration;
- final Token className;
- Token superclassName = null;
+ final Token className,superclassName;
final int pos;
char[] classNameImage = SYNTAX_ERROR_CHAR;
char[] superclassNameImage = null;
return classDeclaration;}
}
-void ClassBody(ClassDeclaration classDeclaration) :
+void ClassBody(final ClassDeclaration classDeclaration) :
{}
{
try {
/**
* A class can contain only methods and fields.
*/
-void ClassBodyDeclaration(ClassDeclaration classDeclaration) :
+void ClassBodyDeclaration(final ClassDeclaration classDeclaration) :
{
- MethodDeclaration method;
- FieldDeclaration field;
+ final MethodDeclaration method;
+ final FieldDeclaration field;
}
{
method = MethodDeclaration() {classDeclaration.addMethod(method);}
FieldDeclaration FieldDeclaration() :
{
VariableDeclaration variableDeclaration;
- VariableDeclaration[] list;
+ final VariableDeclaration[] list;
final ArrayList arrayList = new ArrayList();
final int pos = SimpleCharStream.getPosition();
}
*/
String VariableDeclaratorId() :
{
- String expr;
+ final String expr;
Expression expression = null;
- final StringBuffer buff = new StringBuffer();
final int pos = SimpleCharStream.getPosition();
ConstantIdentifier ex;
}
String VariableName():
{
final StringBuffer buff;
- String expr = null;
+ final String expr;
Expression expression = null;
final Token token;
}
ArrayVariableDeclaration ArrayVariable() :
{
-Expression expr,expr2;
+final Expression expr,expr2;
}
{
expr = Expression()
[<COMMA> {list.add(null);}]
<RPAREN>
{
- ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
+ final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()];
list.toArray(vars);
return vars;}
}
Expression Expression() :
{
final Expression expr;
+ final int pos = SimpleCharStream.getPosition();
+}
+{
+ <BANG> expr = Expression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
+| expr = ExpressionNoBang() {return expr;}
+}
+
+Expression ExpressionNoBang() :
+{
+ final Expression expr;
}
{
expr = PrintExpression() {return expr;}
*/
VarAssignation varAssignation() :
{
- String varName;
+ final String varName;
final Expression initializer;
final int assignOperator;
final int pos = SimpleCharStream.getPosition();
*/
Expression UnaryExpression() :
{
- Expression expr;
+ final Expression expr;
final int pos = SimpleCharStream.getPosition();
}
{
Expression AtUnaryExpression() :
{
- Expression expr;
+ final Expression expr;
final int pos = SimpleCharStream.getPosition();
}
{
Expression UnaryExpressionNoPrefix() :
{
- Expression expr;
- int operator;
+ final Expression expr;
+ final int operator;
final int pos = SimpleCharStream.getPosition();
}
{
Expression UnaryExpressionNotPlusMinus() :
{
- Expression expr;
- final int pos = SimpleCharStream.getPosition();
+ final Expression expr;
}
{
- <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
-| LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
+// <BANG> expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);}
+ LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
expr = CastExpression() {return expr;}
| expr = PostfixExpression() {return expr;}
| expr = Literal() {return expr;}
Expression PostfixExpression() :
{
- Expression expr;
+ final Expression expr;
int operator = -1;
final int pos = SimpleCharStream.getPosition();
}
SimpleCharStream.getPosition());}
}
-AbstractSuffixExpression PrimarySuffix(Expression prefix) :
+AbstractSuffixExpression PrimarySuffix(final Expression prefix) :
{
final AbstractSuffixExpression expr;
}
| expr = VariableSuffix(prefix) {return expr;}
}
-AbstractSuffixExpression VariableSuffix(Expression prefix) :
+AbstractSuffixExpression VariableSuffix(final Expression prefix) :
{
String expr = null;
final int pos = SimpleCharStream.getPosition();
return new NullLiteral(pos-4,pos);}
}
-FunctionCall Arguments(Expression func) :
+FunctionCall Arguments(final Expression func) :
{
Expression[] args = null;
}
}
)*
{
- Expression[] arguments = new Expression[list.size()];
+ final Expression[] arguments = new Expression[list.size()];
list.toArray(arguments);
return arguments;}
}
HTMLBlock htmlBlock() :
{
final int startIndex = nodePtr;
- AstNode[] blockNodes;
- int nbNodes;
+ final AstNode[] blockNodes;
+ final int nbNodes;
}
{
<PHPEND> (phpEchoBlock())*
ListExpression ListExpression() :
{
String expr = null;
- Expression expression = null;
- ArrayList list = new ArrayList();
+ final Expression expression;
+ final ArrayList list = new ArrayList();
final int pos = SimpleCharStream.getPosition();
}
{
errorEnd = SimpleCharStream.getPosition() + 1;
throw e;
}
- expr = VariableDeclaratorId()
- {list.add(expr);}
+ [expr = VariableDeclaratorId() {list.add(expr);}]
)*
try {
<RPAREN>
}
[ <ASSIGN> expression = Expression()
{
- String[] strings = new String[list.size()];
+ final String[] strings = new String[list.size()];
list.toArray(strings);
return new ListExpression(strings,
expression,
SimpleCharStream.getPosition());}
]
{
- String[] strings = new String[list.size()];
+ final String[] strings = new String[list.size()];
list.toArray(strings);
return new ListExpression(strings,pos,SimpleCharStream.getPosition());}
}
throw e;
}
}
- {Expression[] exprs = new Expression[expressions.size()];
+ {final Expression[] exprs = new Expression[expressions.size()];
expressions.toArray(exprs);
return new EchoStatement(exprs,pos);}
}
{
final int pos = SimpleCharStream.getPosition();
String expr;
- ArrayList vars = new ArrayList();
- GlobalStatement global;
+ final ArrayList vars = new ArrayList();
+ final GlobalStatement global;
}
{
<GLOBAL>
try {
<SEMICOLON>
{
- String[] strings = new String[vars.size()];
+ final String[] strings = new String[vars.size()];
vars.toArray(strings);
global = new GlobalStatement(currentSegment,
strings,
try {
<SEMICOLON>
{
- String[] strings = new String[vars.size()];
+ final String[] strings = new String[vars.size()];
vars.toArray(strings);
return new StaticStatement(strings,
pos,
throw e;
}
{
- Statement[] statements = new Statement[list.size()];
+ final Statement[] statements = new Statement[list.size()];
list.toArray(statements);
return new Block(statements,pos,SimpleCharStream.getPosition());}
}
{list.add(var);}
( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
{
- VariableDeclaration[] vars = new VariableDeclaration[list.size()];
+ final VariableDeclaration[] vars = new VariableDeclaration[list.size()];
list.toArray(vars);
return vars;}
}
Expression StatementExpression() :
{
- Expression expr,expr2;
- int operator;
+ final Expression expr,expr2;
+ final int operator;
}
{
expr = PreIncDecExpression() {return expr;}
try {
<RBRACE>
{
- AbstractCase[] abcase = new AbstractCase[cases.size()];
+ final AbstractCase[] abcase = new AbstractCase[cases.size()];
cases.toArray(abcase);
return abcase;}
} catch (ParseException e) {
try {
<SEMICOLON>
{
- AbstractCase[] abcase = new AbstractCase[cases.size()];
+ final AbstractCase[] abcase = new AbstractCase[cases.size()];
cases.toArray(abcase);
return abcase;}
} catch (ParseException e) {
| statement = htmlBlock() {stmts.add(statement);})*
[ statement = BreakStatement() {stmts.add(statement);}]
{
- Statement[] stmtsArray = new Statement[stmts.size()];
+ final Statement[] stmtsArray = new Statement[stmts.size()];
stmts.toArray(stmtsArray);
if (expr == null) {//it's a default
return new DefaultCase(stmtsArray,pos,SimpleCharStream.getPosition());
IfStatement IfStatement() :
{
final int pos = SimpleCharStream.getPosition();
- Expression condition;
- IfStatement ifStatement;
+ final Expression condition;
+ final IfStatement ifStatement;
}
{
<IF> condition = Condition("if") ifStatement = IfStatement0(condition, pos,pos+2)
{return condition;}
}
-IfStatement IfStatement0(Expression condition, final int start,final int end) :
+IfStatement IfStatement0(final Expression condition, final int start,final int end) :
{
Statement statement;
- Statement stmt;
+ final Statement stmt;
final Statement[] statementsArray;
ElseIf elseifStatement;
Else elseStatement = null;
- ArrayList stmts;
+ final ArrayList stmts;
final ArrayList elseIfList = new ArrayList();
- ElseIf[] elseIfs;
+ final ElseIf[] elseIfs;
int pos = SimpleCharStream.getPosition();
- int endStatements;
+ final int endStatements;
}
{
<COLON>
ElseIf ElseIfStatementColon() :
{
- Expression condition;
+ final Expression condition;
Statement statement;
final ArrayList list = new ArrayList();
final int pos = SimpleCharStream.getPosition();
<COLON> ( statement = Statement() {list.add(statement);}
| statement = htmlBlock() {list.add(statement);})*
{
- Statement[] stmtsArray = new Statement[list.size()];
+ final Statement[] stmtsArray = new Statement[list.size()];
list.toArray(stmtsArray);
return new ElseIf(condition,stmtsArray ,pos,SimpleCharStream.getPosition());}
}
<ELSE> <COLON> ( statement = Statement() {list.add(statement);}
| statement = htmlBlock() {list.add(statement);})*
{
- Statement[] stmtsArray = new Statement[list.size()];
+ final Statement[] stmtsArray = new Statement[list.size()];
list.toArray(stmtsArray);
return new Else(stmtsArray,pos,SimpleCharStream.getPosition());}
}
ElseIf ElseIfStatement() :
{
- Expression condition;
- Statement statement;
+ final Expression condition;
+ final Statement statement;
final ArrayList list = new ArrayList();
final int pos = SimpleCharStream.getPosition();
}
{
<ELSEIF> condition = Condition("elseif") statement = Statement() {list.add(statement);/*todo:do better*/}
{
- Statement[] stmtsArray = new Statement[list.size()];
+ final Statement[] stmtsArray = new Statement[list.size()];
list.toArray(stmtsArray);
return new ElseIf(condition,stmtsArray,pos,SimpleCharStream.getPosition());}
}
try {
<SEMICOLON>
{
- Statement[] stmtsArray = new Statement[stmts.size()];
+ final Statement[] stmtsArray = new Statement[stmts.size()];
stmts.toArray(stmtsArray);
return new Block(stmtsArray,pos,SimpleCharStream.getPosition());}
} catch (ParseException e) {
try {
<SEMICOLON>
{
- Statement[] stmtsArray = new Statement[list.size()];
+ final 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) {
Expression[] ForInit() :
{
- Expression[] exprs;
+ final Expression[] exprs;
}
{
LOOKAHEAD(LocalVariableDeclaration())
Expression[] StatementExpressionList() :
{
final ArrayList list = new ArrayList();
- Expression expr;
+ final Expression expr;
}
{
expr = StatementExpression() {list.add(expr);}
(<COMMA> StatementExpression() {list.add(expr);})*
{
- Expression[] exprsArray = new Expression[list.size()];
+ final Expression[] exprsArray = new Expression[list.size()];
list.toArray(exprsArray);
return exprsArray;}
}