/** * Builds a {@link Parser} based on information in this {@link OperatorTable}. * * @param operand parser for the operands. * @return the expression parser. */ public Parser<T> build(Parser<? extends T> operand) { return buildExpressionParser(operand, operators()); }
static Parser<Integer> parser() { Parser.Reference<Integer> ref = Parser.newReference(); Parser<Integer> term = ref.lazy().between(isChar('('), isChar(')')).or(NUMBER); Parser<Integer> parser = new OperatorTable<Integer>() .prefix(op('-', NEG), 100) .infixl(op('+', PLUS), 10) .infixl(op('-', MINUS), 10) .infixl(op('*', MUL), 20) .infixl(op('/', DIV), 20) .infixl(op('%', MOD), 20) .build(term); ref.set(parser); return parser; } }
simpleMethodCall(lazy), lambdaExpression(lazy, statement), atom); Parser<Expression> parser = new OperatorTable<Expression>() .postfix(subscript(lazy), 200) .postfix(qualifiedMethodCall(lazy), 200) .postfix(qualifiedNew(lazy, classBody), 200) .postfix(METHOD_REFERENCE, 200) .postfix(CONSTRUCTOR_REFERENCE, 20) .postfix(QUALIFIED_EXPR, 200) .postfix(postfix(Operator.POST_INC), 200) .postfix(postfix(Operator.POST_DEC), 200) .prefix(prefix(Operator.INC), 190) .prefix(prefix(Operator.DEC), 190) .prefix(prefix(Operator.POSITIVE), 190) .prefix(prefix(Operator.NEGATIVE), 190) .prefix(prefix(Operator.INC), 190) .prefix(prefix(Operator.DEC), 190) .prefix(prefix(Operator.NOT), 190) .prefix(prefix(Operator.BITWISE_NOT), 190) .infixl(binary(Operator.MUL), 100) .infixl(binary(Operator.DIV), 100) .infixl(binary(Operator.MOD), 100) .infixl(binary(Operator.DIV), 100) .infixl(binary(Operator.PLUS), 90) .infixl(binary(Operator.MINUS), 90) .infixl(binary(Operator.LSHIFT), 80) .infixl(binary(Operator.RSHIFT), 80) .infixl(binary(Operator.UNSIGNED_RSHIFT), 80) .infixl(binary(Operator.LE), 70)
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; }
static Parser<Expression> arithmetic(Parser<Expression> atom) { Parser.Reference<Expression> reference = Parser.newReference(); Parser<Expression> operand = Parsers.or(paren(reference.lazy()), functionCall(reference.lazy()), atom); Parser<Expression> parser = new OperatorTable<Expression>() .infixl(binary("+", Op.PLUS), 10) .infixl(binary("-", Op.MINUS), 10) .infixl(binary("*", Op.MUL), 20) .infixl(binary("/", Op.DIV), 20) .infixl(binary("%", Op.MOD), 20) .prefix(unary("-", Op.NEG), 50) .build(operand); reference.set(parser); return parser; }