DeclarationParser { static Parser<Member> methodDef( Parser<Modifier> mod, Parser<Expression> defaultValue, Parser<Statement> stmt) { return Parsers.sequence( mod.many(), TYPE_PARAMETERS.optional(), TypeLiteralParser.TYPE_LITERAL, Terminals.Identifier.PARSER, term("(").next(StatementParser.parameter(mod).sepBy(term(","))).followedBy(term(")")), term("throws").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), term("default").next(ExpressionParser.arrayInitializerOrRegularExpression(defaultValue)) .optional(), Parsers.or( StatementParser.blockStatement(stmt), term(";").retn((BlockStatement) null)), MethodDef::new); }  static Parser<Program> program(); static Program parse(String source); static Program parse(URL url);  }
@Test public void testMethodDef() { Parser<Member> parser = DeclarationParser.methodDef( StatementParser.SYSTEM_MODIFIER, IDENTIFIER, StatementParser.BREAK); TerminalParserTest.assertResult(parser, "public static void f();", MethodDef.class, "public static void f();"); TerminalParserTest.assertResult(parser, "String f() default foo;", MethodDef.class, "String f() default foo;"); TerminalParserTest.assertResult(parser, "void f() throws E;", MethodDef.class, "void f() throws E;"); TerminalParserTest.assertResult(parser, "void f() throws E, F<T>;", MethodDef.class, "void f() throws E, F<T>;"); TerminalParserTest.assertFailure(parser, "void f() throws", 1, 16); TerminalParserTest.assertFailure(parser, "void f() throws E[];", 1, 18); TerminalParserTest.assertResult(parser, "void f() {}", MethodDef.class, "void f() {}"); TerminalParserTest.assertResult(parser, "void f() {break; break;}", MethodDef.class, "void f() {break; break;}"); TerminalParserTest.assertResult(parser, "void f(int i) {}", MethodDef.class, "void f(int i) {}"); TerminalParserTest.assertResult(parser, "void f(final int i, List<Foo> l) {}", MethodDef.class, "void f(final int i, List<Foo> l) {}"); TerminalParserTest.assertResult(parser, "<K, V extends K> void f(int i) {}", MethodDef.class, "<K, V extends K> void f(int i) {}"); }