StatementParser { static Parser<Statement> statement(Parser<Expression> expr) { Parser.Reference<Statement> ref = Parser.newReference(); Parser<Statement> lazy = ref.lazy(); @SuppressWarnings("unchecked") Parser<Statement> parser = Parsers.or( returnStatement(expr), BREAK, CONTINUE, blockStatement(lazy), foreachStatement(expr, lazy), forStatement(expr, lazy), whileStatement(expr, lazy), doWhileStatement(lazy, expr), ifStatement(expr, lazy), switchStatement(expr, lazy), tryStatement(modifier(expr), lazy), throwStatement(expr), synchronizedBlock(lazy), assertStatement(expr), varStatement(expr), thisCall(expr), superCall(expr), expression(expr), NOP).prefix(LABEL).label("statement"); ref.set(parser); return parser; }    }
@Test public void testStatement() { Parser<Statement> parser = StatementParser.statement(ExpressionParser.IDENTIFIER); TerminalParserTest.assertResult(parser, ";", NopStatement.class, ";"); TerminalParserTest.assertResult(parser, "foo: bar;", LabelStatement.class, "foo: bar;"); TerminalParserTest.assertResult(parser, "foo: bar: baz;", LabelStatement.class, "foo: bar: baz;"); TerminalParserTest.assertResult(parser, "break foo;", BreakStatement.class, "break foo;"); TerminalParserTest.assertResult(parser, "continue foo;", ContinueStatement.class, "continue foo;"); TerminalParserTest.assertResult(parser, "return foo;", ReturnStatement.class, "return foo;"); TerminalParserTest.assertResult(parser, "foo;", ExpressionStatement.class, "foo;"); TerminalParserTest.assertResult(parser, "{foo;}", BlockStatement.class, "{foo;}"); TerminalParserTest.assertResult(parser, "synchronized {foo;}", SynchronizedBlockStatement.class, "synchronized {foo;}"); TerminalParserTest.assertResult(parser, "while(foo) bar;", WhileStatement.class, "while (foo) bar;"); TerminalParserTest.assertResult(parser, "do bar;while(foo);", DoWhileStatement.class, "do bar; while (foo);"); TerminalParserTest.assertResult(parser, "if (foo) bar; else if(baz) baz;", IfStatement.class, "if (foo) bar; else if (baz) baz;"); TerminalParserTest.assertResult(parser, "switch (foo) { case foo: case bar: baz; default: x;}", SwitchStatement.class, "switch (foo) { case foo: case bar: baz; default: x;}"); TerminalParserTest.assertResult(parser, "for(Foo<Bar>[][] foo : foos) bar;", ForeachStatement.class, "for (Foo<Bar>[][] foo : foos) bar;"); TerminalParserTest.assertResult(parser, "for(int i=m, j=n;cond;a,b)foo;", ForStatement.class, "for (int i = m, j = n;cond;a, b) foo;"); TerminalParserTest.assertResult(parser, "for(init1,init2;;)foo;", ForStatement.class, "for (init1, init2;;) foo;"); TerminalParserTest.assertResult(parser, "final int[] a1={}, a2, a3={m, n};", VarStatement.class, "final int[] a1 = {}, a2, a3 = {m, n};"); TerminalParserTest.assertResult(parser, "assert foo : bar;", AssertStatement.class, "assert foo : bar;"); TerminalParserTest.assertResult(parser, "try {foo;} catch(E e){bar;}catch(E e2){bar2;}finally{bar;}", TryStatement.class, "try {foo;} catch (E e) {bar;} catch (E e2) {bar2;} finally {bar;}"); TerminalParserTest.assertResult(parser, "throw foo;", ThrowStatement.class, "throw foo;"); TerminalParserTest.assertResult(parser, "this(foo, bar);", ThisCallStatement.class, "this(foo, bar);"); TerminalParserTest.assertResult(parser, "super(foo, bar);", SuperCallStatement.class, "super(foo, bar);"); }