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.*;
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.
/** 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;
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;
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(new StringReader(strEval));
+ astStack = new AstNode[AstStackIncrement];
phpTest();
}
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.
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
+ astStack = new AstNode[AstStackIncrement];
phpFile();
}
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
+ astStack = new AstNode[AstStackIncrement];
try {
parse();
} catch (ParseException e) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
}
ReInit(stream);
+ astStack = new AstNode[AstStackIncrement];
try {
parse();
} catch (ParseException e) {
}
}
- public static final void parse() throws ParseException {
+ private static final void parse() throws ParseException {
phpFile();
}
}
| < STRING_1:
"\""
(
- ~["\""] | ~["{"]
- |
- "\\\""
+ ~["\"","{","}"]
+ | "\\\""
+ | "\\"
| "{" ~["\""] "}"
)*
"\""
| < STRING_2:
"'"
(
- ~["'"]
- |
- "\\'"
+ ~["'"]
+ | "\\'"
)*
"'"
"`"
(
~["`"]
- |
- "\\`"
+ | "\\`"
)*
"`"
>
{}
{
MethodDeclaration()
-|
- FieldDeclaration()
+| FieldDeclaration()
}
/**
{
<VAR> variableDeclaration = VariableDeclarator()
{
+ outlineInfo.addVariable(variableDeclaration.getVariable().getName());
if (currentSegment != null) {
currentSegment.add(variableDeclaration);
}
<ASSIGN>
try {
varValue = VariableInitializer()
- {return new PHPVarDeclaration(currentSegment,varName.substring(1),pos,varValue);}
+ {return new PHPVarDeclaration(currentSegment,varName,pos,varValue);}
} catch (ParseException e) {
errorMessage = "Literal expression expected in variable initializer";
errorLevel = ERROR;
}
{
try {
- expr = Variable()
- {buff.append(expr);}
- ( LOOKAHEAD(2) expr = VariableSuffix()
- {buff.append(expr);}
+ expr = Variable() {buff.append(expr);}
+ ( LOOKAHEAD(2)
+ expr = VariableSuffix() {buff.append(expr);}
)*
{return buff.toString();}
} catch (ParseException e) {
{
token = <DOLLAR_ID> [<LBRACE> expr = Expression() <RBRACE>]
{
- 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);
}
}
- return token.image;
+ return token.image.substring(1);
}
return token + "{" + expr + "}";
}
|
<DOLLAR> expr = VariableName()
- {return "$" + expr;}
+ {return expr;}
}
String VariableName():
functionToken = <FUNCTION>
try {
functionDeclaration = MethodDeclarator()
+ {outlineInfo.addVariable(functionDeclaration.getName());}
} catch (ParseException e) {
if (errorMessage != null) {
throw e;
[token = <BIT_AND>] variableDeclaration = VariableDeclarator()
{
if (token != null) {
- variableDeclaration.getVariable().setPrefix("@");
+ variableDeclaration.getVariable().setReference(true);
}
return variableDeclaration;
}
String Type() :
{}
{
- <STRING>
- {return "string";}
-|
- <BOOL>
- {return "bool";}
-|
- <BOOLEAN>
- {return "boolean";}
-|
- <REAL>
- {return "real";}
-|
- <DOUBLE>
- {return "double";}
-|
- <FLOAT>
- {return "float";}
-|
- <INT>
- {return "int";}
-|
- <INTEGER>
- {return "integer";}
-|
- <OBJECT>
- {return "object";}
+ <STRING> {return "string";}
+| <BOOL> {return "bool";}
+| <BOOLEAN> {return "boolean";}
+| <REAL> {return "real";}
+| <DOUBLE> {return "double";}
+| <FLOAT> {return "float";}
+| <INT> {return "int";}
+| <INTEGER> {return "integer";}
+| <OBJECT> {return "object";}
}
String Expression() :
final String expr2;
}
{
- expr = PrintExpression()
- {return expr;}
-|
- expr = ListExpression()
- {return expr;}
-|
- expr = ConditionalExpression()
- [
- assignOperator = AssignmentOperator()
+ expr = PrintExpression() {return expr;}
+| expr = ListExpression() {return expr;}
+| LOOKAHEAD(varAssignation())
+ expr = varAssignation() {return expr;}
+| expr = ConditionalExpression() {return expr;}
+}
+
+/**
+ * A Variable assignation.
+ * varName (an assign operator) any expression
+ */
+String varAssignation() :
+{
+ String varName,assignOperator,expr2;
+ PHPVarDeclaration variable;
+ final int pos = SimpleCharStream.getPosition();
+}
+{
+ varName = VariableDeclaratorId()
+ assignOperator = AssignmentOperator()
try {
expr2 = Expression()
- {return expr + assignOperator + expr2;}
} catch (ParseException e) {
if (errorMessage != null) {
throw e;
errorEnd = jj_input_stream.getPosition() + 1;
throw e;
}
- ]
- {return expr;}
+ {return varName + assignOperator + expr2;}
}
String AssignmentOperator() :
{}
{
- <ASSIGN>
-{return "=";}
-| <STARASSIGN>
-{return "*=";}
-| <SLASHASSIGN>
-{return "/=";}
-| <REMASSIGN>
-{return "%=";}
-| <PLUSASSIGN>
-{return "+=";}
-| <MINUSASSIGN>
-{return "-=";}
-| <LSHIFTASSIGN>
-{return "<<=";}
-| <RSIGNEDSHIFTASSIGN>
-{return ">>=";}
-| <ANDASSIGN>
-{return "&=";}
-| <XORASSIGN>
-{return "|=";}
-| <ORASSIGN>
-{return "|=";}
-| <DOTASSIGN>
-{return ".=";}
-| <TILDEEQUAL>
-{return "~=";}
+ <ASSIGN> {return "=";}
+| <STARASSIGN> {return "*=";}
+| <SLASHASSIGN> {return "/=";}
+| <REMASSIGN> {return "%=";}
+| <PLUSASSIGN> {return "+=";}
+| <MINUSASSIGN> {return "-=";}
+| <LSHIFTASSIGN> {return "<<=";}
+| <RSIGNEDSHIFTASSIGN> {return ">>=";}
+| <ANDASSIGN> {return "&=";}
+| <XORASSIGN> {return "|=";}
+| <ORASSIGN> {return "|=";}
+| <DOTASSIGN> {return ".=";}
+| <TILDEEQUAL> {return "~=";}
}
String ConditionalExpression() :
}
return token.image + expr;
}
-|
- (<AT> {buff.append("@");})* expr = UnaryExpressionNoPrefix()
+| (<AT> {buff.append("@");})*
+ expr = UnaryExpressionNoPrefix()
{return buff.append(expr).toString();}
}
final String expr;
}
{
- <BANG> expr = UnaryExpression()
- {return "!" + expr;}
-|
- LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
- expr = CastExpression()
- {return expr;}
-|
- expr = PostfixExpression()
- {return expr;}
-|
- expr = Literal()
- {return expr;}
-|
- <LPAREN> expr = Expression()
+ <BANG> expr = UnaryExpression() {return "!" + expr;}
+| LOOKAHEAD( <LPAREN> (Type() | <ARRAY>) <RPAREN> )
+ expr = CastExpression() {return expr;}
+| expr = PostfixExpression() {return expr;}
+| expr = Literal() {return expr;}
+| <LPAREN> expr = Expression()
try {
<RPAREN>
} 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;
throw e;
}
{return "("+expr+")";}
final Token token;
}
{
- token = <IDENTIFIER>
- {return token.image;}
-|
- <NEW> expr = ClassIdentifier()
- {
- return "new " + expr;
- }
-|
- expr = VariableDeclaratorId()
- {return expr;}
+ token = <IDENTIFIER> {return token.image;}
+| <NEW> expr = ClassIdentifier() {return "new " + expr;}
+| expr = VariableDeclaratorId() {return expr;}
}
String classInstantiation() :
final Token token;
}
{
- token = <IDENTIFIER>
- {return token.image;}
-|
- expr = VariableDeclaratorId()
- {return expr;}
+ token = <IDENTIFIER> {return token.image;}
+| expr = VariableDeclaratorId() {return expr;}
}
String PrimarySuffix() :
final String expr;
}
{
- expr = Arguments()
- {return expr;}
-|
- expr = VariableSuffix()
- {return expr;}
+ expr = Arguments() {return expr;}
+| expr = VariableSuffix() {return expr;}
}
String VariableSuffix() :
final Token token;
}
{
- token = <INTEGER_LITERAL>
- {return token.image;}
-|
- token = <FLOATING_POINT_LITERAL>
- {return token.image;}
-|
- token = <STRING_LITERAL>
- {return token.image;}
-|
- expr = BooleanLiteral()
- {return expr;}
-|
- <NULL>
- {return "null";}
+ token = <INTEGER_LITERAL> {return token.image;}
+| token = <FLOATING_POINT_LITERAL> {return token.image;}
+| token = <STRING_LITERAL> {return token.image;}
+| expr = BooleanLiteral() {return expr;}
+| <NULL> {return "null";}
}
String BooleanLiteral() :
{}
{
- <TRUE>
- {return "true";}
-|
- <FALSE>
- {return "false";}
+ <TRUE> {return "true";}
+| <FALSE> {return "false";}
}
String Arguments() :
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;
throw e;
}
- {
- buff.append(",").append(expr);
- }
+ {buff.append(",").append(expr);}
)*
{return buff.toString();}
}
/**
- * A Statement without break
+ * A Statement without break.
*/
void StatementNoBreak() :
{}
throw e;
}
}
-|
- LOOKAHEAD(2)
+| LOOKAHEAD(2)
LabeledStatement()
-|
- Block()
-|
- EmptyStatement()
-|
- StatementExpression()
+| Block()
+| EmptyStatement()
+| StatementExpression()
try {
<SEMICOLON>
} 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;
throw e;
}
-|
- SwitchStatement()
-|
- IfStatement()
-|
- WhileStatement()
-|
- DoStatement()
-|
- ForStatement()
-|
- ForeachStatement()
-|
- ContinueStatement()
-|
- ReturnStatement()
-|
- EchoStatement()
-|
- [<AT>] IncludeStatement()
-|
- StaticStatement()
-|
- GlobalStatement()
+| SwitchStatement()
+| IfStatement()
+| WhileStatement()
+| DoStatement()
+| ForStatement()
+| ForeachStatement()
+| ContinueStatement()
+| ReturnStatement()
+| EchoStatement()
+| [<AT>] IncludeStatement()
+| StaticStatement()
+| GlobalStatement()
}
/**
- * A Normal statement
+ * A Normal statement.
*/
void Statement() :
{}
{
StatementNoBreak()
-|
- BreakStatement()
+| BreakStatement()
}
+/**
+ * An html block inside a php syntax.
+ */
void htmlBlock() :
{}
{
- <PHPEND> (phpEchoBlock())* (<PHPSTARTLONG> | <PHPSTARTSHORT>)
+ <PHPEND> (phpEchoBlock())*
+ try {
+ (<PHPSTARTLONG> | <PHPSTARTSHORT>)
+ } catch (ParseException e) {
+ errorMessage = "End of file unexpected, '<?php' expected";
+ errorLevel = ERROR;
+ errorStart = jj_input_stream.getPosition();
+ errorEnd = jj_input_stream.getPosition();
+ throw e;
+ }
}
/**
| token = <REQUIRE_ONCE>
| token = <INCLUDE>
| token = <INCLUDE_ONCE> )
- expr = Expression()
+ try {
+ expr = Expression()
+ } catch (ParseException e) {
+ if (errorMessage != null) {
+ throw e;
+ }
+ errorMessage = "unexpected token '"+ e.currentToken.next.image+"', expression expected";
+ errorLevel = ERROR;
+ errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = jj_input_stream.getPosition() + 1;
+ throw e;
+ }
{
if (currentSegment != null) {
currentSegment.add(new PHPReqIncDeclaration(currentSegment, token.image,pos,expr));
} 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;
throw e;
}
}
String PrintExpression() :
{
- final StringBuffer buff = new StringBuffer("print ");
final String expr;
}
{
- <PRINT> expr = Expression()
- {
- buff.append(expr);
- return buff.toString();
- }
+ <PRINT> expr = Expression() {return "print " + expr;}
}
String ListExpression() :
} catch (ParseException e) {
errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' 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;
throw e;
}
[
} catch (ParseException e) {
errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', ',' 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;
throw e;
}
expr = VariableDeclaratorId()
}
/**
- * An echo statement is like this : echo anyexpression (, otherexpression)*
+ * An echo statement.
+ * echo anyexpression (, otherexpression)*
*/
void EchoStatement() :
{}
if (e.currentToken.next.kind != 4) {
errorMessage = "';' expected after 'echo' statement";
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;
throw e;
}
}
try {
<SEMICOLON>
} catch (ParseException e) {
- errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
+ 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;
try {
<SEMICOLON>
} catch (ParseException e) {
- errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. A ';' was expected";
+ 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;
{}
{
Statement()
-|
- ClassDeclaration()
-|
- MethodDeclaration()
+| ClassDeclaration()
+| MethodDeclaration()
}
/**
{}
{
StatementNoBreak()
-|
- ClassDeclaration()
-|
- MethodDeclaration()
+| ClassDeclaration()
+| MethodDeclaration()
}
void LocalVariableDeclaration() :
PreIncDecExpression()
|
PrimaryExpression()
- [
- <INCR>
- |
- <DECR>
- |
- AssignmentOperator() Expression()
- ]
+ [ <INCR>
+ | <DECR>
+ | AssignmentOperator() Expression() ]
}
void SwitchStatement() :
errorEnd = jj_input_stream.getPosition() + 1;
throw e;
}
- Expression()
+ try {
+ Expression()
+ } catch (ParseException e) {
+ if (errorMessage != null) {
+ throw e;
+ }
+ errorMessage = "expression expected";
+ errorLevel = ERROR;
+ errorStart = jj_input_stream.getPosition() - e.currentToken.next.image.length() + 1;
+ errorEnd = jj_input_stream.getPosition() + 1;
+ throw e;
+ }
try {
<RPAREN>
} catch (ParseException e) {