StatementParser { static Parser<Annotation> annotation(Parser<Expression> expr) { Parser<Annotation.Element> element = Parsers.sequence( Terminals.Identifier.PARSER.followedBy(term("=")).atomic().optional(), ExpressionParser.arrayInitializerOrRegularExpression(expr), Annotation.Element::new); return Parsers.sequence( term("@").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL), paren(element.sepBy(term(","))).optional(), Annotation::new); }    }
@Test public void testAnnotation() { Parser<Annotation> parser = StatementParser.annotation(ExpressionParser.IDENTIFIER); TerminalParserTest.assertResult(parser, "@Foo", Annotation.class, "@Foo"); TerminalParserTest.assertResult(parser, "@org.codehaus.jparsec.Foo", Annotation.class, "@org.codehaus.jparsec.Foo"); TerminalParserTest.assertResult(parser, "@Foo()", Annotation.class, "@Foo()"); TerminalParserTest.assertResult(parser, "@Foo(foo)", Annotation.class, "@Foo(foo)"); TerminalParserTest.assertResult(parser, "@Foo(foo=bar)", Annotation.class, "@Foo(foo=bar)"); TerminalParserTest.assertResult(parser, "@Foo(foo={bar})", Annotation.class, "@Foo(foo={bar})"); TerminalParserTest.assertResult(parser, "@Foo(foo={bar}, a=b)", Annotation.class, "@Foo(foo={bar}, a=b)"); TerminalParserTest.assertFailure(parser, "Foo", 1, 1); TerminalParserTest.assertFailure(parser, "@Foo({{foo}})", 1, 7); }