ExpressionParser { static Parser<Expression> logical(Parser<Expression> expr) { Parser.Reference<Expression> ref = Parser.newReference(); Parser<Expression> parser = new OperatorTable<Expression>() .prefix(unary("not", Op.NOT), 30) .infixl(binary("and", Op.AND), 20) .infixl(binary("or", Op.OR), 10) .build(paren(ref.lazy()).or(expr)).label("logical expression"); ref.set(parser); return parser; }    }
@Test public void testLogical() { Parser<Expression> parser = ExpressionParser.logical(NUMBER); TerminalParserTest.assertParser(parser, "1", number(1)); TerminalParserTest.assertParser(parser, "(1)", number(1)); TerminalParserTest.assertParser(parser, "((1))", number(1)); TerminalParserTest.assertParser(parser, "not 1", new UnaryExpression(Op.NOT, number(1))); TerminalParserTest.assertParser(parser, "1 and 2", new BinaryExpression(number(1), Op.AND, number(2))); TerminalParserTest.assertParser(parser, "1 or 2", new BinaryExpression(number(1), Op.OR, number(2))); TerminalParserTest.assertParser(parser, "1 or 2 and 3", new BinaryExpression(number(1), Op.OR, new BinaryExpression(number(2), Op.AND, number(3)))); TerminalParserTest.assertParser(parser, "1 or NOT 2", new BinaryExpression(number(1), Op.OR, new UnaryExpression(Op.NOT, number(2)))); TerminalParserTest.assertParser(parser, "not 1 and 2", new BinaryExpression( new UnaryExpression(Op.NOT, number(1)), Op.AND, number(2))); }