StatementParser { static Parser<Statement> ifStatement(Parser<Expression> expr, Parser<Statement> stmt) { return Parsers.sequence( between(phrase("if ("), expr, term(")")), stmt, Parsers.pair(between(phrase("else if ("), expr, term(")")), stmt).many(), term("else").next(stmt).optional(), IfStatement::new); }    }
@Test public void testIfStatement() { Parser<Statement> parser = StatementParser.ifStatement(ExpressionParser.IDENTIFIER, SIMPLE_STATEMENT); TerminalParserTest.assertResult(parser, "if (foo) bar;", IfStatement.class, "if (foo) bar;"); TerminalParserTest.assertResult(parser, "if (foo) bar; else baz;", IfStatement.class, "if (foo) bar; else baz;"); TerminalParserTest.assertResult(parser, "if (foo) bar; else if(baz) baz;", IfStatement.class, "if (foo) bar; else if (baz) baz;"); }