From af9e2da6d34fe22f431d1334eee0d1d081e1d16d Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Wed, 30 Nov 2011 08:55:35 +0100 Subject: [PATCH] Simplified the statement parser, fixed a few quirks and adjusted a test case. --- .../worldedit/expression/parser/Parser.java | 41 ++++++++----------- .../worldedit/expression/ExpressionTest.java | 2 +- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java b/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java index 17a74a0a1..69a920505 100644 --- a/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java +++ b/src/main/java/com/sk89q/worldedit/expression/parser/Parser.java @@ -88,10 +88,8 @@ public class Parser { private RValue parseStatements(boolean singleStatement) throws ParserException { List statements = new ArrayList(); - loop: while (true) { - if (position >= tokens.size()) { - break; - } + loop: while (position < tokens.size()) { + boolean expectSemicolon = false; final Token current = peek(); switch (current.id()) { @@ -102,9 +100,6 @@ public class Parser { consumeCharacter('}'); - if (singleStatement) { - break loop; - } break; case '}': @@ -148,6 +143,8 @@ public class Parser { final RValue condition = parseBracket(); statements.add(new While(current.getPosition(), condition, body, true)); + + expectSemicolon = true; break; } @@ -178,7 +175,6 @@ public class Parser { if (!(variable instanceof LValue)) { throw new ParserException(variableToken.getPosition(), "Expected variable"); } - ++position; final Token equalsToken = peek(); @@ -194,7 +190,7 @@ public class Parser { final RValue body = parseStatements(true); statements.add(new SimpleFor(current.getPosition(), (LValue) variable, first, last, body)); - } + } // switch (keyword.charAt(0)) break; } @@ -212,36 +208,33 @@ public class Parser { ++position; statements.add(new Return(current.getPosition(), parseExpression(true))); - if (peek().id() == ';') { - ++position; - break; - } else { - break loop; - } + expectSemicolon = true; + break; default: throw new ParserException(current.getPosition(), "Unimplemented keyword '" + keyword + "'"); } - if (singleStatement) { - break loop; - } break; default: statements.add(parseExpression(true)); + expectSemicolon = true; + } // switch (current.id()) + + if (expectSemicolon) { if (peek().id() == ';') { ++position; - if (singleStatement) { - break loop; - } - break; } else { - break loop; + break; } } - } + + if (singleStatement) { + break; + } + } // while (position < tokens.size()) switch (statements.size()) { case 0: diff --git a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java index 19651c924..22b4d5501 100644 --- a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java +++ b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java @@ -90,7 +90,7 @@ public class ExpressionTest { @Test public void testWhile() throws ExpressionException { assertEquals(5, simpleEval("c=5; a=0; while (c > 0) { ++a; --c; } a"), 0); - assertEquals(5, simpleEval("c=5; a=0; do { ++a; --c; } while (c > 0) a"), 0); + assertEquals(5, simpleEval("c=5; a=0; do { ++a; --c; } while (c > 0); a"), 0); } @Test