+
options {
LOOKAHEAD = 1;
CHOICE_AMBIGUITY_CHECK = 2;
*/
public final class PHPParser extends PHPParserSuperclass {
+//todo : fix the variables names bug
+//todo : handle tilde operator
+
+
/** The current segment. */
private static OutlineableWithChildren currentSegment;
/** The cursor in expression stack. */
private static int nodePtr;
- private static final boolean PARSER_DEBUG = false;
+ private static final boolean PARSER_DEBUG = true;
public final void setFileToParse(final IFile fileToParse) {
PHPParser.fileToParse = fileToParse;
* Put a new html block in the stack.
*/
public static final void createNewHTMLCode() {
- final int currentPosition = SimpleCharStream.getPosition();
- if (currentPosition == htmlStart || currentPosition > SimpleCharStream.currentBuffer.length()) {
+ final int currentPosition = token.sourceStart;
+ if (currentPosition == htmlStart ||
+ currentPosition > SimpleCharStream.currentBuffer.length()) {
return;
}
final char[] chars = SimpleCharStream.currentBuffer.substring(htmlStart,currentPosition+1).toCharArray();
/** Create a new task. */
public static final void createNewTask() {
- final int currentPosition = SimpleCharStream.getPosition();
+ final int currentPosition = token.sourceStart;
final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3,
SimpleCharStream.currentBuffer.indexOf("\n",
currentPosition)-1);
- PHPeclipsePlugin.log(1,SimpleCharStream.currentBuffer.toString());
- try {
- setMarker(fileToParse,
- todo,
- SimpleCharStream.getBeginLine(),
- TASK,
- "Line "+SimpleCharStream.getBeginLine());
- } catch (CoreException e) {
- PHPeclipsePlugin.log(e);
+ if (!PARSER_DEBUG) {
+ try {
+ setMarker(fileToParse,
+ todo,
+ SimpleCharStream.getBeginLine(),
+ TASK,
+ "Line "+SimpleCharStream.getBeginLine());
+ } catch (CoreException e) {
+ PHPeclipsePlugin.log(e);
+ }
}
}
<PHPPARSING, IN_SINGLE_LINE_COMMENT> TOKEN :
{
- <PHPEND :"?>"> {PHPParser.htmlStart = SimpleCharStream.getPosition();} : DEFAULT
+ <PHPEND :"?>"> {PHPParser.htmlStart = PHPParser.token.sourceEnd;} : DEFAULT
}
/* Skip any character if we are not in php mode */
*/
void PhpBlock() :
{
- final int start = SimpleCharStream.getPosition();
final PHPEchoBlock phpEchoBlock;
+ final Token token;
}
{
phpEchoBlock = phpEchoBlock()
{pushOnAstNodes(phpEchoBlock);}
|
[ <PHPSTARTLONG>
- | <PHPSTARTSHORT>
+ | token = <PHPSTARTSHORT>
{try {
setMarker(fileToParse,
"You should use '<?php' instead of '<?' it will avoid some problems with XML",
- start,
- SimpleCharStream.getPosition(),
+ token.sourceStart,
+ token.sourceEnd,
INFO,
"Line " + token.beginLine);
} catch (CoreException e) {
{
token = <PHPECHOSTART> expr = Expression() [ <SEMICOLON> ] token2 = <PHPEND>
{
- echoBlock = new PHPEchoBlock(expr,token.sourceStart,token.sourceEnd);
+ echoBlock = new PHPEchoBlock(expr,token.sourceStart,token2.sourceEnd);
pushOnAstNodes(echoBlock);
return echoBlock;}
}
VariableDeclaration variableDeclaration;
final VariableDeclaration[] list;
final ArrayList arrayList = new ArrayList();
- final int pos = SimpleCharStream.getPosition();
final Token token;
Token token2 = null;
}
{
final Token varName;
Expression initializer = null;
+ Token assignToken;
}
{
varName = <DOLLAR_ID>
[
- <ASSIGN>
+ assignToken = <ASSIGN>
try {
initializer = VariableInitializer()
} catch (ParseException e) {
errorMessage = "Literal expression expected in variable initializer";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
+ errorStart = assignToken.sourceEnd +1;
+ errorEnd = assignToken.sourceEnd +1;
processParseExceptionDebug(e);
}
]
if (initializer == null) {
return new VariableDeclaration(currentSegment,
new Variable(varName.image.substring(1),
- varName.sourceStart,
- varName.sourceEnd),
- varName.sourceStart,
- varName.sourceEnd);
+ varName.sourceStart+1,
+ varName.sourceEnd+1),
+ varName.sourceStart+1,
+ varName.sourceEnd+1);
}
return new VariableDeclaration(currentSegment,
new Variable(varName.image.substring(1),
- varName.sourceStart,
- varName.sourceEnd),
+ varName.sourceStart+1,
+ varName.sourceEnd+1),
initializer,
VariableDeclaration.EQUAL,
- varName.sourceStart);
+ varName.sourceStart+1);
}
}
{
final Variable var;
AbstractVariable expression = null;
- final int pos = SimpleCharStream.getPosition();
}
{
try {
{
if (expression == null) {
return new Variable(token.image.substring(1),
- token.sourceStart,
- token.sourceEnd);
+ token.sourceStart+1,
+ token.sourceEnd+1);
}
String s = expression.toStringExpression();
buff = new StringBuffer(token.image.length()+s.length()+2);
buff.append(s);
buff.append("}");
s = buff.toString();
- return new Variable(s,token.sourceStart,token.sourceEnd);
+ return new Variable(s,token.sourceStart+1,token.sourceEnd+1);
}
|
token = <DOLLAR>
token = <DOLLAR_ID> [variable = Var(token)]
{
if (variable == null) {
- return new Variable(token.image.substring(1),token.sourceStart,token.sourceEnd);
+ return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
}
final StringBuffer buff = new StringBuffer();
buff.append(token.image.substring(1));
buff.append(variable.toStringExpression());
- return new Variable(buff.toString(),token.sourceStart,variable.sourceEnd);
+ return new Variable(buff.toString(),token.sourceStart+1,variable.sourceEnd+1);
}
|
token = <DOLLAR> variable = Var(token)
{
token = <DOLLAR_ID> [variable = Var(token)]
{if (variable == null) {
- return new Variable(token.image.substring(1),token.sourceStart,token.sourceEnd);
+ return new Variable(token.image.substring(1),token.sourceStart+1,token.sourceEnd+1);
}
final StringBuffer buff = new StringBuffer();
buff.append(token.image.substring(1));
{
final StringBuffer buff;
String expr;
- final Variable var;
Expression expression = null;
final Token token;
Token token2 = null;
- int pos;
}
{
token = <LBRACE> expression = Expression() token2 = <RBRACE>
buff.append("{");
buff.append(expr);
buff.append("}");
- pos = SimpleCharStream.getPosition();
expr = buff.toString();
return new ConstantIdentifier(expr,
token.sourceStart,
token = <DOLLAR_ID>
{
return new Variable(token.image,
- token.sourceStart,
- token.sourceEnd);
+ token.sourceStart+1,
+ token.sourceEnd+1);
} */
}
{return expr;}
}
-
Expression UnaryExpressionNoPrefix() :
{
final Expression expr;
Expression PrimaryExpression() :
{
- Expression expr = null;
+ Expression expr;
Token token = null;
}
{
Expression refPrimaryExpression(final Token reference) :
{
- Expression expr = null;
+ Expression expr;
Expression expr2 = null;
- int assignOperator = -1;
final Token identifier;
- final String var;
}
{
identifier = <IDENTIFIER>
{
final Expression expr;
final Token token;
- final ConstantIdentifier type;
}
{
token = <IDENTIFIER> {return new ConstantIdentifier(token);}
*/
AbstractVariable VariableSuffix(final AbstractVariable prefix) :
{
- Variable expr = null;
- final int pos = SimpleCharStream.getPosition();
Expression expression = null;
+ final Token classAccessToken;
}
{
- <CLASSACCESS>
+ classAccessToken = <CLASSACCESS>
try {
- expression = VariableName()
+ ( expression = VariableName() | expression = Variable() )
} catch (ParseException e) {
errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', function call or field access expected";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
- throw e;
+ errorStart = classAccessToken.sourceEnd +1;
+ errorEnd = classAccessToken.sourceEnd +1;
+ processParseExceptionDebug(e);
}
{return new ClassAccess(prefix,
expression,
final Expression expression;
final ArrayList list = new ArrayList();
final int pos = SimpleCharStream.getPosition();
+ final Token listToken;
}
{
- <LIST>
+ listToken = <LIST>
try {
<LPAREN>
} catch (ParseException e) {
errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', '(' expected";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
- throw e;
+ errorStart = listToken.sourceEnd+1;
+ errorEnd = listToken.sourceEnd+1;
+ processParseExceptionDebug(e);
}
[
expr = VariableDeclaratorId()
}
[ <ASSIGN> expression = Expression()
{
- final Variable[] vars = new Variable[list.size()];
+ final AbstractVariable[] vars = new AbstractVariable[list.size()];
list.toArray(vars);
return new ListExpression(vars,
expression,
/**
* used only by ForInit()
*/
-VariableDeclaration[] LocalVariableDeclaration() :
+Expression[] LocalVariableDeclaration() :
{
final ArrayList list = new ArrayList();
- VariableDeclaration var;
+ Expression var;
}
{
- var = LocalVariableDeclarator()
+ var = Expression()
{list.add(var);}
- ( <COMMA> var = LocalVariableDeclarator() {list.add(var);})*
+ ( <COMMA> var = Expression() {list.add(var);})*
{
- final VariableDeclaration[] vars = new VariableDeclaration[list.size()];
+ final Expression[] vars = new Expression[list.size()];
list.toArray(vars);
- return vars;}
+ return vars;
+ }
}
/**
*/
Expression StatementExpression() :
{
- final Expression expr,expr2;
- final int operator;
+ final Expression expr;
}
{
expr = PreIncDecExpression() {return expr;}
final Expression variable;
final AbstractCase[] cases;
final int pos = SimpleCharStream.getPosition();
+ final Token switchToken;
}
{
- <SWITCH>
+ switchToken = <SWITCH>
try {
<LPAREN>
} catch (ParseException e) {
errorEnd = SimpleCharStream.getPosition() + 1;
throw e;
}
- (cases = switchStatementBrace() | cases = switchStatementColon(pos, pos + 6))
- {return new SwitchStatement(variable,cases,pos,SimpleCharStream.getPosition());}
+ (cases = switchStatementBrace() | cases = switchStatementColon(switchToken.sourceStart, switchToken.sourceEnd))
+ {return new SwitchStatement(variable,cases,switchToken.sourceStart,SimpleCharStream.getPosition());}
}
AbstractCase[] switchStatementBrace() :
if (errorMessage != null) throw e;
errorMessage = "expression expected after 'case' keyword";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
+ errorStart = token.sourceEnd +1;
+ errorEnd = token.sourceEnd +1;
throw e;
}
try {
- <COLON>
+ token = <COLON>
{return expr;}
} catch (ParseException e) {
errorMessage = "':' expected after case expression";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
- throw e;
+ errorStart = expr.sourceEnd+1;
+ errorEnd = expr.sourceEnd+1;
+ processParseExceptionDebug(e);
}
|
token = <_DEFAULT>
} catch (ParseException e) {
errorMessage = "':' expected after 'default' keyword";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
- throw e;
+ errorStart = token.sourceEnd+1;
+ errorEnd = token.sourceEnd+1;
+ processParseExceptionDebug(e);
}
}
IfStatement IfStatement() :
{
- final int pos = SimpleCharStream.getPosition();
final Expression condition;
final IfStatement ifStatement;
Token token;
}
{
- token = <IF> condition = Condition("if") ifStatement = IfStatement0(condition,
- token.sourceStart,token.sourceStart+2)
+ token = <IF> condition = Condition("if")
+ ifStatement = IfStatement0(condition,token.sourceStart,token.sourceEnd)
{return ifStatement;}
}
}
+/**
+ * a for declaration.
+ * @return a node representing the for statement
+ */
ForStatement ForStatement() :
{
final Token token,token2;
Expression[] increments = null;
Statement action;
final ArrayList list = new ArrayList();
-final int startBlock, endBlock;
}
{
token = <FOR>
} catch (ParseException e) {
errorMessage = "'(' expected after 'for' keyword";
errorLevel = ERROR;
- errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
- errorEnd = SimpleCharStream.getPosition() + 1;
- throw e;
+ errorStart = token.sourceEnd;
+ errorEnd = token.sourceEnd +1;
+ processParseExceptionDebug(e);
}
[ initializations = ForInit() ] <SEMICOLON>
[ condition = Expression() ] <SEMICOLON>
action.sourceEnd);}
|
<COLON>
- {startBlock = SimpleCharStream.getPosition();}
(action = Statement() {list.add(action);})*
{
try {
setMarker(fileToParse,
"Ugly syntax detected, you should for () {...} instead of for (): ... endfor;",
- pos,
- pos+token.image.length(),
+ token.sourceStart,
+ token.sourceEnd,
INFO,
"Line " + token.beginLine);
} catch (CoreException e) {
PHPeclipsePlugin.log(e);
}
}
- {endBlock = SimpleCharStream.getPosition();}
try {
<ENDFOR>
} catch (ParseException e) {
final Expression expr;
}
{
- expr = StatementExpression() {list.add(expr);}
- (<COMMA> StatementExpression() {list.add(expr);})*
+ expr = Expression() {list.add(expr);}
+ (<COMMA> Expression() {list.add(expr);})*
{
final Expression[] exprsArray = new Expression[list.size()];
list.toArray(exprsArray);
errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1;
errorEnd = SimpleCharStream.getPosition() + 1;
throw e;
- }
-}
\ No newline at end of file
+ }
+}
+