StatementParser { static Parser<Statement> switchStatement(Parser<Expression> expr, Parser<Statement> stmt) { return Parsers.sequence( between(phrase("switch ("), expr, phrase(") {")), Parsers.pair(between(term("case"), expr, term(":")), stmt.optional()).many(), phrase("default :").next(stmt.optional()).optional().followedBy(term("}")), SwitchStatement::new); }    }
@Test public void testSwitchStatement() { Parser<Statement> parser = StatementParser.switchStatement(ExpressionParser.IDENTIFIER, SIMPLE_STATEMENT); TerminalParserTest.assertResult(parser, "switch (foo) {}", SwitchStatement.class, "switch (foo) {}"); TerminalParserTest.assertResult(parser, "switch (foo) { default:}", SwitchStatement.class, "switch (foo) {}"); TerminalParserTest.assertResult(parser, "switch (foo) { case foo:}", SwitchStatement.class, "switch (foo) { case foo:}"); TerminalParserTest.assertResult(parser, "switch (foo) { case foo: case bar: baz;}", SwitchStatement.class, "switch (foo) { case foo: case bar: baz;}"); TerminalParserTest.assertResult(parser, "switch (foo) { case foo: case bar: baz; default: x;}", SwitchStatement.class, "switch (foo) { case foo: case bar: baz; default: x;}"); }