StatementParser { static Parser<Statement> varStatement(Parser<Expression> expr) { Parser<Expression> initializer = term("=").next(ExpressionParser.arrayInitializerOrRegularExpression(expr)); Parser<VarStatement.Var> var = Parsers.sequence( Terminals.Identifier.PARSER, initializer.optional(), VarStatement.Var::new); return Parsers.sequence( modifier(expr).many(), TypeLiteralParser.TYPE_LITERAL, var.sepBy1(term(",")).followedBy(term(";")), VarStatement::new); }    }
@Test public void testVarStatement() { Parser<Statement> parser = StatementParser.varStatement(ExpressionParser.IDENTIFIER); TerminalParserTest.assertResult(parser, "int i;", VarStatement.class, "int i;"); TerminalParserTest.assertResult(parser, "int i=n;", VarStatement.class, "int i = n;"); TerminalParserTest.assertResult(parser, "final int i=n;", VarStatement.class, "final int i = n;"); TerminalParserTest.assertResult(parser, "final int[] a1={}, a2, a3={m, n};", VarStatement.class, "final int[] a1 = {}, a2, a3 = {m, n};"); }