/** * A {@link Parser} that sequentially runs {@code p1} and {@code p2} and collects the results in a * {@link Pair} object. Is equivalent to {@link #tuple(Parser, Parser)}. * * @deprecated Prefer to converting to your own object with a lambda. */ @Deprecated public static <A,B> Parser<Pair<A,B>> pair(Parser<? extends A> p1, Parser<? extends B> p2) { return sequence(p1, p2, Pair::new); }
/** * A {@link Parser} that sequentially runs 5 parser objects and collects the results in a * {@link Tuple5} object. * * @deprecated Prefer to converting to your own object with a lambda. */ @Deprecated public static <A,B,C,D,E> Parser<Tuple5<A,B,C,D,E>> tuple( Parser<? extends A> p1, Parser<? extends B> p2, Parser<? extends C> p3, Parser<? extends D> p4, Parser<? extends E> p5) { return sequence(p1, p2, p3, p4, p5, Tuple5::new); }
/** * A {@link Parser} that runs {@code op} for 0 or more times greedily, then runs {@code this}. * The {@link Function} objects returned from {@code op} are applied from right to left to the * return value of {@code p}. * * <p> {@code p.prefix(op)} is equivalent to {@code op* p} in EBNF. */ public final Parser<T> prefix(Parser<? extends Function<? super T, ? extends T>> op) { return Parsers.sequence(op.many(), this, Parser::applyPrefixOperators); }
/** A {@link Parser} that runs 4 parser objects sequentially. */ public static <T> Parser<T> sequence( Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<T> p4) { return sequence(p1, p2, p3, p4, InternalFunctors.<Object, Object, Object, T>lastOfFour()); }
/** * A {@link Parser} that sequentially executes {@code this} and then {@code parser}, whose return value is ignored. */ public final Parser<T> followedBy(Parser<?> parser) { return Parsers.sequence(this, parser, InternalFunctors.<T, Object>firstOfTwo()); }
/** * A {@link Parser} that runs 2 parser objects sequentially. {@code p1} is executed, * if it succeeds, {@code p2} is executed. */ public static <T> Parser<T> sequence(Parser<?> p1, Parser<T> p2) { return sequence(p1, p2, InternalFunctors.<Object, T>lastOfTwo()); }
static Parser<Expression> functionCall(Parser<Expression> param) { return Parsers.sequence( TerminalParser.QUALIFIED_NAME, paren(param.sepBy(TerminalParser.term(","))), FunctionExpression::new); }
/** * {@code (foo)} can be a parenthesized expression, or the prefix of a cast expression, * depending on whether there's an expression following. */ static final Parser<Expression> castOrExpression(Parser<Expression> expr) { Parser<Expression> explicitCast = Parsers.sequence(paren(TypeLiteralParser.TYPE_LITERAL), expr, CastExpression::new); return explicitCast.or(paren(expr)); }
private static Parser<UnaryOperator<Relation>> joinOn( Parser<JoinType> joinType, Parser<Relation> right, Parser<Expression> cond) { return Parsers.sequence( joinType, right, TerminalParser.term("on").next(cond), (t, r, c) -> l -> new JoinRelation(l, t, r, c)); }
/** * A scanner for a non-nestable block comment that starts with {@code begin} and ends with * {@code end}. * * @param begin begins a block comment * @param end ends a block comment * @param commented the commented pattern. * @return the Scanner for the block comment. */ public static Parser<Void> blockComment(Parser<Void> begin, Parser<Void> end, Parser<?> commented) { return Parsers.sequence(begin, end.not().next(commented).skipMany(), end); }
static Parser<Expression> notIn(Parser<Expression> expr) { return Parsers.sequence( expr, phrase("not in").next(tuple(expr)), (e, t) -> new BinaryExpression(e, Op.NOT_IN, t)); }
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); }
/** * A {@link Parser} that recognizes a sequence of tokens identified by {@code tokenNames}, as an * atomic step. */ public Parser<?> phrase(String... tokenNames) { Parser<?>[] wordParsers = new Parser<?>[tokenNames.length]; for (int i = 0; i < tokenNames.length; i++) { wordParsers[i] = token(tokenNames[i]); } String phrase = Strings.join(" ", tokenNames); return Parsers.sequence(wordParsers).atomic().retn(phrase).label(phrase); }
static Parser<UnaryOperator<Expression>> qualifiedNew(Parser<Expression> arg, Parser<DefBody> body) { return Parsers.sequence( phrase(". new").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL), argumentList(arg), body.optional(), (t, a, b) -> q -> new NewExpression(q, t, a, b)); }
static Parser<Statement> foreachStatement(Parser<Expression> expr, Parser<Statement> stmt) { return Parsers.sequence( phrase("for (").next(TypeLiteralParser.TYPE_LITERAL), Terminals.Identifier.PARSER, term(":").next(expr), term(")").next(stmt), ForeachStatement::new); }
static Parser<Expression> simpleCase(Parser<Expression> expr) { return Parsers.sequence( term("case").next(expr), whenThens(expr, expr), term("else").next(expr).optional().followedBy(term("end")), SimpleCaseExpression::new); }
static Parser<Declaration> interfaceDef(Parser<Modifier> mod, Parser<Member> member) { return Parsers.sequence( mod.many(), term("interface").next(Terminals.Identifier.PARSER), TYPE_PARAMETERS.optional(), term("extends").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), body(member), InterfaceDef::new); }
static Parser<Statement> ifStatement(Parser<Expression> expr, Parser<Statement> stmt) { return Parsers.sequence( between(phrase("if ("), expr, term(")")), stmt, Parsers.pair(between(phrase("else if ("), expr, term(")")), stmt).many(), term("else").next(stmt).optional(), IfStatement::new); }
static Parser<Expression> newArrayWithExplicitLength(Parser<Expression> expr) { return Parsers.sequence(term("new").next(TypeLiteralParser.TYPE_LITERAL), expr.between(term("["), term("]")), Parsers.between(term("{"), expr.sepBy(term(",")), term("}")).optional(), NewArrayExpression::new); }
static Parser<Member> constructorDef(Parser<Modifier> mod, Parser<Statement> stmt) { return Parsers.sequence( mod.many(), Terminals.Identifier.PARSER, term("(").next(StatementParser.parameter(mod).sepBy(term(","))).followedBy(term(")")), term("throws").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), StatementParser.blockStatement(stmt), ConstructorDef::new); }