X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/test/PHPParser.jj b/net.sourceforge.phpeclipse/src/test/PHPParser.jj index c348a5e..daf63e2 100644 --- a/net.sourceforge.phpeclipse/src/test/PHPParser.jj +++ b/net.sourceforge.phpeclipse/src/test/PHPParser.jj @@ -38,6 +38,7 @@ import net.sourceforge.phpeclipse.actions.PHPStartApacheAction; import net.sourceforge.phpeclipse.PHPeclipsePlugin; import net.sourceforge.phpdt.internal.compiler.ast.*; import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren; +import net.sourceforge.phpdt.internal.compiler.parser.Outlineable; import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo; /** @@ -49,9 +50,6 @@ import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo; */ public final class PHPParser extends PHPParserSuperclass { - /** The file that is parsed. */ - private static IFile fileToParse; - /** The current segment. */ private static OutlineableWithChildren currentSegment; @@ -82,8 +80,10 @@ public final class PHPParser extends PHPParserSuperclass { /** The cursor in expression stack. */ private static int nodePtr; + private static final boolean PARSER_DEBUG = false; + public final void setFileToParse(final IFile fileToParse) { - this.fileToParse = fileToParse; + PHPParser.fileToParse = fileToParse; } public PHPParser() { @@ -91,7 +91,7 @@ public final class PHPParser extends PHPParserSuperclass { public PHPParser(final IFile fileToParse) { this(new StringReader("")); - this.fileToParse = fileToParse; + PHPParser.fileToParse = fileToParse; } /** @@ -107,12 +107,12 @@ 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; @@ -149,6 +149,10 @@ public final class PHPParser extends PHPParserSuperclass { * @param e the ParseException */ private static void processParseException(final ParseException e) { + if (PARSER_DEBUG) { + e.printStackTrace(); + return; + } if (errorMessage == null) { PHPeclipsePlugin.log(e); errorMessage = "this exception wasn't handled by the parser please tell us how to reproduce it"; @@ -157,10 +161,11 @@ public final class PHPParser extends PHPParserSuperclass { } setMarker(e); errorMessage = null; + // if (PHPeclipsePlugin.DEBUG) PHPeclipsePlugin.log(e); } /** - * Create marker for the parse error + * Create marker for the parse error. * @param e the ParseException */ private static void setMarker(final ParseException e) { @@ -192,12 +197,12 @@ public final class PHPParser extends PHPParserSuperclass { final int indx, final int brIndx) throws CoreException { String current; - StringBuffer lineNumberBuffer = new StringBuffer(10); + final StringBuffer lineNumberBuffer = new StringBuffer(10); char ch; current = output.substring(indx, brIndx); if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) { - int onLine = current.indexOf("on line "); + final int onLine = current.indexOf("on line "); if (onLine != -1) { lineNumberBuffer.delete(0, lineNumberBuffer.length()); for (int i = onLine; i < current.length(); i++) { @@ -207,9 +212,9 @@ public final class PHPParser extends PHPParserSuperclass { } } - int lineNumber = Integer.parseInt(lineNumberBuffer.toString()); + final int lineNumber = Integer.parseInt(lineNumberBuffer.toString()); - Hashtable attributes = new Hashtable(); + final Hashtable attributes = new Hashtable(); current = current.replaceAll("\n", ""); current = current.replaceAll("", ""); @@ -269,24 +274,23 @@ public final class PHPParser extends PHPParserSuperclass { */ 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(); pushOnAstNodes(new HTMLCode(chars, htmlStart,currentPosition)); } - /** - * Create a new task. - */ + /** Create a new task. */ public static final void createNewTask() { final int currentPosition = SimpleCharStream.getPosition(); - final String todo = SimpleCharStream.currentBuffer.substring(currentPosition+1, + final String todo = SimpleCharStream.currentBuffer.substring(currentPosition-3, SimpleCharStream.currentBuffer.indexOf("\n", currentPosition)-1); + PHPeclipsePlugin.log(1,SimpleCharStream.currentBuffer.toString()); try { setMarker(fileToParse, - "todo : " + todo, + todo, SimpleCharStream.getBeginLine(), TASK, "Line "+SimpleCharStream.getBeginLine()); @@ -391,6 +395,7 @@ MORE : | | | +| | | "> | @@ -456,8 +461,8 @@ MORE : { | -| -| +| +| | | | @@ -498,39 +503,16 @@ MORE : <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > | | | )> -| -| -| +| +| +| } /* IDENTIFIERS */ TOKEN : { - < IDENTIFIER: (|) (||)* > + |) (||)* > | < #LETTER: ["a"-"z"] | ["A"-"Z"] @@ -657,7 +639,7 @@ PHPEchoBlock phpEchoBlock() : { final Expression expr; final int pos = SimpleCharStream.getPosition(); - PHPEchoBlock echoBlock; + final PHPEchoBlock echoBlock; } { expr = Expression() [ ] @@ -676,8 +658,7 @@ void Php() : 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; @@ -732,58 +713,61 @@ ClassDeclaration ClassDeclaration() : return classDeclaration;} } -void ClassBody(ClassDeclaration classDeclaration) : +void ClassBody(final ClassDeclaration classDeclaration) : {} { try { } catch (ParseException e) { - errorMessage = "unexpected token : '"+ e.currentToken.next.image + "', '{' expected"; + 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; + processParseException(e); } ( ClassBodyDeclaration(classDeclaration) )* try { } catch (ParseException e) { - errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', 'var', 'function' or '}' expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"'. 'var', 'function' or '}' expected"; errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } } /** * 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);} + method = MethodDeclaration() {method.analyzeCode(); + classDeclaration.addMethod(method);} | field = FieldDeclaration() {classDeclaration.addField(field);} } /** * A class field declaration : it's var VariableDeclarator() (, VariableDeclarator())*;. + * it is only used by ClassBodyDeclaration() */ FieldDeclaration FieldDeclaration() : { VariableDeclaration variableDeclaration; - VariableDeclaration[] list; + final VariableDeclaration[] list; final ArrayList arrayList = new ArrayList(); final int pos = SimpleCharStream.getPosition(); } { - variableDeclaration = VariableDeclarator() + variableDeclaration = VariableDeclaratorNoSuffix() {arrayList.add(variableDeclaration); outlineInfo.addVariable(new String(variableDeclaration.name));} - ( variableDeclaration = VariableDeclarator() + ( + variableDeclaration = VariableDeclaratorNoSuffix() {arrayList.add(variableDeclaration); outlineInfo.addVariable(new String(variableDeclaration.name));} )* @@ -805,6 +789,44 @@ FieldDeclaration FieldDeclaration() : currentSegment);} } +/** + * a strict variable declarator : there cannot be a suffix here. + */ +VariableDeclaration VariableDeclaratorNoSuffix() : +{ + final Token varName; + Expression initializer = null; + final int pos = SimpleCharStream.getPosition(); +} +{ + varName = + [ + + 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; + processParseException(e); + } + ] + { + if (initializer == null) { + return new VariableDeclaration(currentSegment, + varName.image.substring(1).toCharArray(), + pos, + SimpleCharStream.getPosition()); + } + return new VariableDeclaration(currentSegment, + varName.image.substring(1).toCharArray(), + initializer, + VariableDeclaration.EQUAL, + pos); + } +} + VariableDeclaration VariableDeclarator() : { final String varName; @@ -822,7 +844,7 @@ VariableDeclaration VariableDeclarator() : errorLevel = ERROR; errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; errorEnd = SimpleCharStream.getPosition() + 1; - throw e; + processParseException(e); } ] { @@ -835,6 +857,7 @@ VariableDeclaration VariableDeclarator() : return new VariableDeclaration(currentSegment, varName.toCharArray(), initializer, + VariableDeclaration.EQUAL, pos); } } @@ -845,24 +868,24 @@ VariableDeclaration VariableDeclarator() : */ String VariableDeclaratorId() : { - String expr; + final String var; Expression expression = null; - final StringBuffer buff = new StringBuffer(); final int pos = SimpleCharStream.getPosition(); ConstantIdentifier ex; } { try { - expr = Variable() - ( LOOKAHEAD(2) - {ex = new ConstantIdentifier(expr.toCharArray(), + var = Variable() + ( + LOOKAHEAD(2) + {ex = new ConstantIdentifier(var.toCharArray(), pos, SimpleCharStream.getPosition());} expression = VariableSuffix(ex) )* { if (expression == null) { - return expr; + return var; } return expression.toStringExpression(); } @@ -910,7 +933,7 @@ String Variable(): String VariableName(): { final StringBuffer buff; - String expr = null; + final String expr; Expression expression = null; final Token token; } @@ -976,12 +999,13 @@ Expression VariableInitializer() : ArrayVariableDeclaration ArrayVariable() : { -Expression expr,expr2; +final Expression expr,expr2; } { expr = Expression() - [ expr2 = Expression() - {return new ArrayVariableDeclaration(expr,expr2);} + [ + expr2 = Expression() + {return new ArrayVariableDeclaration(expr,expr2);} ] {return new ArrayVariableDeclaration(expr,SimpleCharStream.getPosition());} } @@ -992,16 +1016,20 @@ ArrayVariableDeclaration[] ArrayInitializer() : final ArrayList list = new ArrayList(); } { - [ expr = ArrayVariable() - {list.add(expr);} - ( LOOKAHEAD(2) expr = ArrayVariable() - {list.add(expr);} - )* - ] - [ {list.add(null);}] + + [ + expr = ArrayVariable() + {list.add(expr);} + ( LOOKAHEAD(2) expr = ArrayVariable() + {list.add(expr);} + )* + ] + [ + {list.add(null);} + ] { - ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()]; + final ArrayVariableDeclaration[] vars = new ArrayVariableDeclaration[list.size()]; list.toArray(vars); return vars;} } @@ -1062,12 +1090,13 @@ MethodDeclaration MethodDeclarator() : processParseException(e); } formalParameters = FormalParameters() - {return new MethodDeclaration(currentSegment, - identifierChar, - formalParameters, - reference != null, - pos, - SimpleCharStream.getPosition());} + {MethodDeclaration method = new MethodDeclaration(currentSegment, + identifierChar, + formalParameters, + reference != null, + pos, + SimpleCharStream.getPosition()); + return method;} } /** @@ -1089,13 +1118,14 @@ Hashtable FormalParameters() : errorEnd = SimpleCharStream.getPosition() + 1; processParseException(e); } - [ var = FormalParameter() - {parameters.put(new String(var.name),var);} - ( - var = FormalParameter() - {parameters.put(new String(var.name),var);} - )* - ] + [ + var = FormalParameter() + {parameters.put(new String(var.name),var);} + ( + var = FormalParameter() + {parameters.put(new String(var.name),var);} + )* + ] try { } catch (ParseException e) { @@ -1118,7 +1148,7 @@ VariableDeclaration FormalParameter() : Token token = null; } { - [token = ] variableDeclaration = VariableDeclarator() + [token = ] variableDeclaration = VariableDeclaratorNoSuffix() { if (token != null) { variableDeclaration.setReference(true); @@ -1152,64 +1182,86 @@ ConstantIdentifier Type() : Expression Expression() : { final Expression expr; + Expression initializer = null; + final int pos = SimpleCharStream.getPosition(); } { - expr = PrintExpression() {return expr;} -| expr = ListExpression() {return expr;} -| LOOKAHEAD(varAssignation()) - expr = varAssignation() {return expr;} -| expr = ConditionalExpression() {return expr;} + expr = ConditionalExpression() {return expr;} +| expr = ExpressionWBang() {return expr;} } -/** - * A Variable assignation. - * varName (an assign operator) any expression - */ -VarAssignation varAssignation() : +Expression ExpressionWBang() : { - String varName; - final Expression initializer; - final int assignOperator; + final Expression expr; final int pos = SimpleCharStream.getPosition(); } { - varName = VariableDeclaratorId() - assignOperator = AssignmentOperator() + expr = ExpressionWBang() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);} +| expr = ExpressionNoBang() {return expr;} +} + +Expression ExpressionNoBang() : +{ + Expression expr = null; + int assignOperator = -1; + String var; + final int pos = SimpleCharStream.getPosition(); +} +{ + expr = PrintExpression() {return expr;} +| expr = ListExpression() {return expr;} +| + var = VariableDeclaratorId() + [ + assignOperator = AssignmentOperator() try { - initializer = Expression() + expr = Expression() } catch (ParseException e) { if (errorMessage != null) { throw e; } - errorMessage = "expression expected"; + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; + errorEnd = SimpleCharStream.getPosition(); throw e; } - {return new VarAssignation(varName.toCharArray(), - initializer, - assignOperator, - pos, - SimpleCharStream.getPosition());} + ] + { + if (assignOperator == -1) { + return new VariableDeclaration(currentSegment, + var.toCharArray(), + pos, + SimpleCharStream.getPosition()); + } + return new VariableDeclaration(currentSegment, + var.toCharArray(), + expr, + assignOperator, + pos); + } + {return expr;} } +/** + * Any assignement operator. + * @return the assignement operator id + */ int AssignmentOperator() : {} { - {return VarAssignation.EQUAL;} -| {return VarAssignation.STAR_EQUAL;} -| {return VarAssignation.SLASH_EQUAL;} -| {return VarAssignation.REM_EQUAL;} -| {return VarAssignation.PLUS_EQUAL;} -| {return VarAssignation.MINUS_EQUAL;} -| {return VarAssignation.LSHIFT_EQUAL;} -| {return VarAssignation.RSIGNEDSHIFT_EQUAL;} -| {return VarAssignation.AND_EQUAL;} -| {return VarAssignation.XOR_EQUAL;} -| {return VarAssignation.OR_EQUAL;} -| {return VarAssignation.DOT_EQUAL;} -| {return VarAssignation.TILDE_EQUAL;} + {return VariableDeclaration.EQUAL;} +| {return VariableDeclaration.STAR_EQUAL;} +| {return VariableDeclaration.SLASH_EQUAL;} +| {return VariableDeclaration.REM_EQUAL;} +| {return VariableDeclaration.PLUS_EQUAL;} +| {return VariableDeclaration.MINUS_EQUAL;} +| {return VariableDeclaration.LSHIFT_EQUAL;} +| {return VariableDeclaration.RSIGNEDSHIFT_EQUAL;} +| {return VariableDeclaration.AND_EQUAL;} +| {return VariableDeclaration.XOR_EQUAL;} +| {return VariableDeclaration.OR_EQUAL;} +| {return VariableDeclaration.DOT_EQUAL;} +| {return VariableDeclaration.TILDE_EQUAL;} } Expression ConditionalExpression() : @@ -1239,7 +1291,8 @@ Expression ConditionalOrExpression() : ( {operator = OperatorIds.OR_OR;} | <_ORL> {operator = OperatorIds.ORL;} - ) expr2 = ConditionalAndExpression() + ) + expr2 = ConditionalAndExpression() { expr = new BinaryExpression(expr,expr2,operator); } @@ -1307,6 +1360,7 @@ Expression AndExpression() : { expr = EqualityExpression() ( + LOOKAHEAD(1) expr2 = EqualityExpression() {expr = new BinaryExpression(expr,expr2,OperatorIds.AND);} )* @@ -1389,6 +1443,7 @@ Expression AdditiveExpression() : { expr = MultiplicativeExpression() ( + LOOKAHEAD(1) ( {operator = OperatorIds.PLUS;} | {operator = OperatorIds.MINUS;} ) expr2 = MultiplicativeExpression() @@ -1428,7 +1483,7 @@ Expression MultiplicativeExpression() : */ Expression UnaryExpression() : { - Expression expr; + final Expression expr; final int pos = SimpleCharStream.getPosition(); } { @@ -1440,7 +1495,7 @@ Expression UnaryExpression() : Expression AtUnaryExpression() : { - Expression expr; + final Expression expr; final int pos = SimpleCharStream.getPosition(); } { @@ -1455,14 +1510,17 @@ Expression AtUnaryExpression() : Expression UnaryExpressionNoPrefix() : { - Expression expr; - int operator; + final Expression expr; + final int operator; final int pos = SimpleCharStream.getPosition(); } { - ( {operator = OperatorIds.PLUS;} - | {operator = OperatorIds.MINUS;}) - expr = UnaryExpression() + ( + {operator = OperatorIds.PLUS;} + | + {operator = OperatorIds.MINUS;} + ) + expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,operator,pos);} | expr = PreIncDecExpression() @@ -1480,20 +1538,22 @@ final int operator; final int pos = SimpleCharStream.getPosition(); } { - ( {operator = OperatorIds.PLUS_PLUS;} - | {operator = OperatorIds.MINUS_MINUS;}) - expr = PrimaryExpression() + ( + {operator = OperatorIds.PLUS_PLUS;} + | + {operator = OperatorIds.MINUS_MINUS;} + ) + expr = PrimaryExpression() {return new PrefixedUnaryExpression(expr,operator,pos);} } Expression UnaryExpressionNotPlusMinus() : { - Expression expr; + final Expression expr; final int pos = SimpleCharStream.getPosition(); } { - expr = UnaryExpression() {return new PrefixedUnaryExpression(expr,OperatorIds.NOT,pos);} -| LOOKAHEAD( (Type() | ) ) + LOOKAHEAD( (Type() | ) ) expr = CastExpression() {return expr;} | expr = PostfixExpression() {return expr;} | expr = Literal() {return expr;} @@ -1518,22 +1578,28 @@ final int pos = SimpleCharStream.getPosition(); } { - (type = Type() - | {type = new ConstantIdentifier(Types.ARRAY,pos,SimpleCharStream.getPosition());}) + ( + type = Type() + | + {type = new ConstantIdentifier(Types.ARRAY,pos,SimpleCharStream.getPosition());} + ) expr = UnaryExpression() {return new CastExpression(type,expr,pos,SimpleCharStream.getPosition());} } Expression PostfixExpression() : { - Expression expr; + final Expression expr; int operator = -1; final int pos = SimpleCharStream.getPosition(); } { expr = PrimaryExpression() - [ {operator = OperatorIds.PLUS_PLUS;} - | {operator = OperatorIds.MINUS_MINUS;}] + [ + {operator = OperatorIds.PLUS_PLUS;} + | + {operator = OperatorIds.MINUS_MINUS;} + ] { if (operator == -1) { return expr; @@ -1544,56 +1610,57 @@ Expression PostfixExpression() : Expression PrimaryExpression() : { - final Token identifier; Expression expr; + int assignOperator = -1; + final Token identifier; + final String var; final int pos = SimpleCharStream.getPosition(); } { - LOOKAHEAD(2) - identifier = expr = ClassIdentifier() - {expr = new ClassAccess(new ConstantIdentifier(identifier.image.toCharArray(), - pos, - SimpleCharStream.getPosition()), - expr, - ClassAccess.STATIC);} + identifier = + {expr = new ConstantIdentifier(token.image.toCharArray(), + pos, + SimpleCharStream.getPosition());} (expr = PrimarySuffix(expr))* {return expr;} | - expr = PrimaryPrefix() - (expr = PrimarySuffix(expr))* + expr = ArrayDeclarator() {return expr;} | - expr = ArrayDeclarator() + expr = ClassIdentifier() + {expr = new PrefixedUnaryExpression(expr,OperatorIds.NEW,pos);} + [expr = Arguments(expr)] {return expr;} } -ArrayInitializer ArrayDeclarator() : +AbstractSuffixExpression PrimarySuffix(final Expression prefix) : { - final ArrayVariableDeclaration[] vars; - final int pos = SimpleCharStream.getPosition(); + final AbstractSuffixExpression suffix; + final Expression expr; } { - vars = ArrayInitializer() - {return new ArrayInitializer(vars,pos,SimpleCharStream.getPosition());} + suffix = Arguments(prefix) {return suffix;} +| suffix = VariableSuffix(prefix) {return suffix;} +| expr = ClassIdentifier() + {suffix = new ClassAccess(prefix, + expr, + ClassAccess.STATIC); + return suffix;} } -Expression PrimaryPrefix() : +/** + * An array declarator. + * array(vars) + * @return an array + */ +ArrayInitializer ArrayDeclarator() : { - final Expression expr; - final Token token; - final String var; + final ArrayVariableDeclaration[] vars; final int pos = SimpleCharStream.getPosition(); } { - token = {return new ConstantIdentifier(token.image.toCharArray(), - pos, - SimpleCharStream.getPosition());} -| expr = ClassIdentifier() {return new PrefixedUnaryExpression(expr, - OperatorIds.NEW, - pos);} -| var = VariableDeclaratorId() {return new ConstantIdentifier(var.toCharArray(), - pos, - SimpleCharStream.getPosition());} + vars = ArrayInitializer() + {return new ArrayInitializer(vars,pos,SimpleCharStream.getPosition());} } PrefixedUnaryExpression classInstantiation() : @@ -1622,26 +1689,19 @@ ConstantIdentifier ClassIdentifier(): final String expr; final Token token; final int pos = SimpleCharStream.getPosition(); + final ConstantIdentifier type; } { token = {return new ConstantIdentifier(token.image.toCharArray(), pos, SimpleCharStream.getPosition());} +| type = Type() {return type;} | expr = VariableDeclaratorId() {return new ConstantIdentifier(expr.toCharArray(), pos, SimpleCharStream.getPosition());} } -AbstractSuffixExpression PrimarySuffix(Expression prefix) : -{ - final AbstractSuffixExpression expr; -} -{ - expr = Arguments(prefix) {return expr;} -| expr = VariableSuffix(prefix) {return expr;} -} - -AbstractSuffixExpression VariableSuffix(Expression prefix) : +AbstractSuffixExpression VariableSuffix(final Expression prefix) : { String expr = null; final int pos = SimpleCharStream.getPosition(); @@ -1695,7 +1755,7 @@ Literal Literal() : return new NullLiteral(pos-4,pos);} } -FunctionCall Arguments(Expression func) : +FunctionCall Arguments(final Expression func) : { Expression[] args = null; } @@ -1739,7 +1799,7 @@ final ArrayList list = new ArrayList(); } )* { - Expression[] arguments = new Expression[list.size()]; + final Expression[] arguments = new Expression[list.size()]; list.toArray(arguments); return arguments;} } @@ -1767,11 +1827,11 @@ Statement StatementNoBreak() : } } {return statement;} -| LOOKAHEAD(2) +| LOOKAHEAD(1) statement = LabeledStatement() {return statement;} | statement = Block() {return statement;} | statement = EmptyStatement() {return statement;} -| statement = StatementExpression() +/*| statement = StatementExpression() try { } catch (ParseException e) { @@ -1781,7 +1841,7 @@ Statement StatementNoBreak() : errorEnd = SimpleCharStream.getPosition() + 1; throw e; } - {return statement;} + {return statement;} */ | statement = SwitchStatement() {return statement;} | statement = IfStatement() {return statement;} | statement = WhileStatement() {return statement;} @@ -1798,6 +1858,66 @@ Statement StatementNoBreak() : return statement;} | statement = StaticStatement() {return statement;} | statement = GlobalStatement() {return statement;} +| statement = defineStatement() {currentSegment.add((Outlineable)statement);return statement;} +} + +Define defineStatement() : +{ + final int start = SimpleCharStream.getPosition(); + Expression defineName,defineValue; +} +{ + + try { + + } 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; + processParseException(e); + } + try { + defineName = Expression() + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; + errorLevel = ERROR; + errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = SimpleCharStream.getPosition() + 1; + throw e; + } + try { + + } 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; + processParseException(e); + } + try { + defineValue = Expression() + } catch (ParseException e) { + errorMessage = "unexpected token : '"+ e.currentToken.next.image +"', expression expected"; + errorLevel = ERROR; + errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; + errorEnd = SimpleCharStream.getPosition() + 1; + throw e; + } + try { + + } 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; + processParseException(e); + } + {return new Define(currentSegment, + defineName, + defineValue, + start, + SimpleCharStream.getPosition());} } /** @@ -1818,8 +1938,8 @@ Statement Statement() : HTMLBlock htmlBlock() : { final int startIndex = nodePtr; - AstNode[] blockNodes; - int nbNodes; + final AstNode[] blockNodes; + final int nbNodes; } { (phpEchoBlock())* @@ -1897,8 +2017,8 @@ PrintExpression PrintExpression() : 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(); } { @@ -1927,8 +2047,7 @@ ListExpression ListExpression() : errorEnd = SimpleCharStream.getPosition() + 1; throw e; } - expr = VariableDeclaratorId() - {list.add(expr);} + [expr = VariableDeclaratorId() {list.add(expr);}] )* try { @@ -1941,7 +2060,7 @@ ListExpression ListExpression() : } [ expression = Expression() { - String[] strings = new String[list.size()]; + final String[] strings = new String[list.size()]; list.toArray(strings); return new ListExpression(strings, expression, @@ -1949,7 +2068,7 @@ ListExpression ListExpression() : 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());} } @@ -1982,7 +2101,7 @@ EchoStatement EchoStatement() : throw e; } } - {Expression[] exprs = new Expression[expressions.size()]; + {final Expression[] exprs = new Expression[expressions.size()]; expressions.toArray(exprs); return new EchoStatement(exprs,pos);} } @@ -1991,8 +2110,8 @@ GlobalStatement GlobalStatement() : { final int pos = SimpleCharStream.getPosition(); String expr; - ArrayList vars = new ArrayList(); - GlobalStatement global; + final ArrayList vars = new ArrayList(); + final GlobalStatement global; } { @@ -2005,7 +2124,7 @@ GlobalStatement GlobalStatement() : try { { - String[] strings = new String[vars.size()]; + final String[] strings = new String[vars.size()]; vars.toArray(strings); global = new GlobalStatement(currentSegment, strings, @@ -2034,7 +2153,7 @@ StaticStatement StaticStatement() : try { { - String[] strings = new String[vars.size()]; + final String[] strings = new String[vars.size()]; vars.toArray(strings); return new StaticStatement(strings, pos, @@ -2094,7 +2213,7 @@ Block Block() : 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());} } @@ -2104,20 +2223,12 @@ Statement BlockStatement() : final Statement statement; } { - try { statement = Statement() {if (phpDocument == currentSegment) pushOnAstNodes(statement); return statement;} - } catch (ParseException e) { - if (errorMessage != null) throw e; - errorMessage = "statement expected"; - errorLevel = ERROR; - errorStart = SimpleCharStream.getPosition() - e.currentToken.next.image.length() + 1; - errorEnd = SimpleCharStream.getPosition() + 1; - throw e; - } | statement = ClassDeclaration() {return statement;} | statement = MethodDeclaration() {if (phpDocument == currentSegment) pushOnAstNodes(statement); currentSegment.add((MethodDeclaration) statement); + ((MethodDeclaration) statement).analyzeCode(); return statement;} } @@ -2132,6 +2243,7 @@ Statement BlockStatementNoBreak() : statement = StatementNoBreak() {return statement;} | statement = ClassDeclaration() {return statement;} | statement = MethodDeclaration() {currentSegment.add((MethodDeclaration) statement); + ((MethodDeclaration) statement).analyzeCode(); return statement;} } @@ -2145,7 +2257,7 @@ VariableDeclaration[] LocalVariableDeclaration() : {list.add(var);} ( var = LocalVariableDeclarator() {list.add(var);})* { - VariableDeclaration[] vars = new VariableDeclaration[list.size()]; + final VariableDeclaration[] vars = new VariableDeclaration[list.size()]; list.toArray(vars); return vars;} } @@ -2168,6 +2280,7 @@ VariableDeclaration LocalVariableDeclarator() : return new VariableDeclaration(currentSegment, varName.toCharArray(), initializer, + VariableDeclaration.EQUAL, pos); } } @@ -2182,23 +2295,21 @@ EmptyStatement EmptyStatement() : return new EmptyStatement(pos-1,pos);} } -Statement StatementExpression() : +Expression StatementExpression() : { - Expression expr,expr2; - int operator; + final Expression expr,expr2; + final int operator; } { expr = PreIncDecExpression() {return expr;} | expr = PrimaryExpression() - [ {return new PostfixedUnaryExpression(expr, + [ {return new PostfixedUnaryExpression(expr, OperatorIds.PLUS_PLUS, SimpleCharStream.getPosition());} - | {return new PostfixedUnaryExpression(expr, + | {return new PostfixedUnaryExpression(expr, OperatorIds.MINUS_MINUS, SimpleCharStream.getPosition());} - | operator = AssignmentOperator() expr2 = Expression() - {return new BinaryExpression(expr,expr2,operator);} ] {return expr;} } @@ -2256,7 +2367,7 @@ AbstractCase[] switchStatementBrace() : try { { - AbstractCase[] abcase = new AbstractCase[cases.size()]; + final AbstractCase[] abcase = new AbstractCase[cases.size()]; cases.toArray(abcase); return abcase;} } catch (ParseException e) { @@ -2302,7 +2413,7 @@ AbstractCase[] switchStatementColon(final int start, final int end) : try { { - AbstractCase[] abcase = new AbstractCase[cases.size()]; + final AbstractCase[] abcase = new AbstractCase[cases.size()]; cases.toArray(abcase); return abcase;} } catch (ParseException e) { @@ -2327,7 +2438,7 @@ AbstractCase switchLabel0() : | 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()); @@ -2403,8 +2514,8 @@ Break BreakStatement() : IfStatement IfStatement() : { final int pos = SimpleCharStream.getPosition(); - Expression condition; - IfStatement ifStatement; + final Expression condition; + final IfStatement ifStatement; } { condition = Condition("if") ifStatement = IfStatement0(condition, pos,pos+2) @@ -2439,18 +2550,18 @@ Expression Condition(final String keyword) : {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; } { @@ -2544,7 +2655,7 @@ IfStatement IfStatement0(Expression condition, final int start,final int end) : ElseIf ElseIfStatementColon() : { - Expression condition; + final Expression condition; Statement statement; final ArrayList list = new ArrayList(); final int pos = SimpleCharStream.getPosition(); @@ -2554,7 +2665,7 @@ ElseIf ElseIfStatementColon() : ( 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());} } @@ -2569,22 +2680,22 @@ Else ElseStatementColon() : ( 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(); } { 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());} } @@ -2632,7 +2743,7 @@ Statement WhileStatement0(final int start, final int end) : try { { - 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) { @@ -2743,9 +2854,9 @@ ForStatement ForStatement() : { final Token token; final int pos = SimpleCharStream.getPosition(); -Statement[] initializations = null; +Expression[] initializations = null; Expression condition = null; -Statement[] increments = null; +Expression[] increments = null; Statement action; final ArrayList list = new ArrayList(); final int startBlock, endBlock; @@ -2796,7 +2907,7 @@ final int startBlock, endBlock; try { { - 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) { @@ -2809,31 +2920,31 @@ final int startBlock, endBlock; ) } -Statement[] ForInit() : +Expression[] ForInit() : { - Statement[] statements; + final Expression[] exprs; } { LOOKAHEAD(LocalVariableDeclaration()) - statements = LocalVariableDeclaration() - {return statements;} + exprs = LocalVariableDeclaration() + {return exprs;} | - statements = StatementExpressionList() - {return statements;} + exprs = StatementExpressionList() + {return exprs;} } -Statement[] StatementExpressionList() : +Expression[] StatementExpressionList() : { final ArrayList list = new ArrayList(); - Statement expr; + final Expression expr; } { expr = StatementExpression() {list.add(expr);} ( StatementExpression() {list.add(expr);})* { - Statement[] stmtsArray = new Statement[list.size()]; - list.toArray(stmtsArray); - return stmtsArray;} + final Expression[] exprsArray = new Expression[list.size()]; + list.toArray(exprsArray); + return exprsArray;} } Continue ContinueStatement() :