StatementParser { static Parser<Statement> tryStatement(Parser<Modifier> mod, Parser<Statement> stmt) { Parser<BlockStatement> block = blockStatement(stmt); return Parsers.sequence( term("try").next(block), Parsers.sequence( term("catch").next(between(term("("), parameter(mod), term(")"))), block, TryStatement.CatchBlock::new).many(), term("finally").next(block).optional(), TryStatement::new); }    }
@Test public void testTryStatement() { Parser<Statement> parser = StatementParser.tryStatement(StatementParser.SYSTEM_MODIFIER, SIMPLE_STATEMENT); TerminalParserTest.assertResult(parser, "try {foo;} catch(E e) {bar;}", TryStatement.class, "try {foo;} catch (E e) {bar;}"); TerminalParserTest.assertResult(parser, "try {foo;} catch(E e) {bar;}catch(E2 e) {baz;}", TryStatement.class, "try {foo;} catch (E e) {bar;} catch (E2 e) {baz;}"); TerminalParserTest.assertResult(parser, "try {foo;} finally{bar;}", TryStatement.class, "try {foo;} finally {bar;}"); TerminalParserTest.assertResult(parser, "try {foo;} catch(E e){bar;}finally{bar;}", TryStatement.class, "try {foo;} catch (E e) {bar;} finally {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.assertFailure(parser, "try{foo;}catch(E e1, E e2){bar;}", 1, 20); }