/** * A {@link Parser} that runs {@code this} for 0 or more times delimited and terminated by * {@code delim}. * * <p>The return values are collected in a {@link List}. */ public final Parser<List<T>> endBy(Parser<?> delim) { return followedBy(delim).many(); }
/** * 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} for right-associative infix operator. Runs {@code this} for the left operand, * and then runs {@code op} and {@code this} for the operator and the right operand for * 0 or more times greedily. * The {@link BiFunction} objects returned from {@code op} are applied from right to left to the * return values of {@code this}, if any. For example: {@code a + b + c + d} is evaluated as * {@code a + (b + (c + d))}. * * <p> {@code p.infixr(op)} is equivalent to {@code p (op p)*} in EBNF. */ public final Parser<T> infixr(Parser<? extends BiFunction<? super T, ? super T, ? extends T>> op) { Parser<Rhs<T>> rhs = Parsers.sequence(op, this, Rhs<T>::new); return Parsers.sequence(this, rhs.many(), Parser::applyInfixrOperators); }
static Parser<ParameterDef> parameter(Parser<Modifier> mod) { return Parsers.sequence( mod.many(), TypeLiteralParser.TYPE_LITERAL, term("...").succeeds(), Terminals.Identifier.PARSER, ParameterDef::new); }
/** * A {@link Parser} for left-associative infix operator. Runs {@code this} for the left operand, and then runs * {@code operator} and {@code this} for the operator and the right operand for 0 or more times greedily. * The {@link BiFunction} objects returned from {@code operator} are applied from left to right to the * return values of {@code this}, if any. For example: * {@code a + b + c + d} is evaluated as {@code (((a + b)+c)+d)}. * * <p> {@code p.infixl(op)} is equivalent to {@code p (op p)*} in EBNF. */ public final Parser<T> infixl( Parser<? extends BiFunction<? super T, ? super T, ? extends T>> operator) { BiFunction<BiFunction<? super T, ? super T, ? extends T>, T, Function<? super T, ? extends T>> rightToLeft = (op, r) -> l -> op.apply(l, r); return next(first -> Parsers.sequence(operator, this, rightToLeft) .many() .map(maps -> applyInfixOperators(first, maps))); }
static <T> T parse(Parser<T> parser, String source) { return parser.from(INDENTATION.lexer(TOKENIZER, Indentation.WHITESPACES.or(COMMENT).many())) .parse(source); } }
/** * A {@link Parser} that matches this parser zero or many times * until the given parser succeeds. The input that matches the given parser * will not be consumed. The input that matches this parser will * be collected in a list that will be returned by this function. * * @since 2.2 */ public final Parser<List<T>> until(Parser<?> parser) { return parser.not().next(this).many().followedBy(parser.peek()); }
static Parser<Declaration> annotationDef(Parser<Modifier> mod, Parser<Member> member) { return Parsers.sequence( mod.many(), phrase("@ interface").next(Terminals.Identifier.PARSER), body(member), AnnotationDef::new); }
static Parser<BlockStatement> blockStatement(Parser<Statement> stmt) { return between(term("{"), stmt.many(), term("}")).map(BlockStatement::new); }
static Parser<Member> fieldDef(Parser<Expression> initializer) { return Parsers.sequence( StatementParser.modifier(initializer).many(), TypeLiteralParser.TYPE_LITERAL, Terminals.Identifier.PARSER, term("=").next(ExpressionParser.arrayInitializerOrRegularExpression(initializer)) .optional(), term(";"), (modifiers, type, name, value, __) -> new FieldDef(modifiers, type, name ,value)); }
static Parser<DefBody> body(Parser<Member> member) { Parser<Member> empty = term(";").retn(null); return Parsers.between(term("{"), empty.or(member).many().map(DeclarationParser::removeNulls), term("}")) .map(DefBody::new); }
static Parser<Declaration> enumDef(Parser<Expression> expr, Parser<Member> member) { Parser<EnumDef.Value> enumValue = Parsers.sequence( Terminals.Identifier.PARSER, between(term("("), expr.sepBy(term(",")), term(")")) .optional(), between(term("{"), member.many(), term("}")).optional(), EnumDef.Value::new); return Parsers.sequence( StatementParser.modifier(expr).many(), term("enum").next(Terminals.Identifier.PARSER), term("implements").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), term("{").next(enumValue.sepBy(term(","))), term(";").next(member.many()).optional().followedBy(term("}")), EnumDef::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<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); }
static Parser<Declaration> classDef(Parser<Modifier> mod, Parser<Member> member) { return Parsers.sequence( mod.many(), term("class").next(Terminals.Identifier.PARSER), TYPE_PARAMETERS.optional(), term("extends").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL).optional(), term("implements").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), body(member), ClassDef::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); }
static Parser<Statement> tryStatement(Parser<Modifier> mod, Parser<Statement> stmt) { Parser<BlockStatement> block = blockStatement(stmt); return Parsers.sequence( term("try").next(block), Parsers.sequence( term("catch").next(between(term("("), parameter(mod), term(")"))), block, TryStatement.CatchBlock::new).many(), term("finally").next(block).optional(), TryStatement::new); }
static Parser<Statement> switchStatement(Parser<Expression> expr, Parser<Statement> stmt) { return Parsers.sequence( between(phrase("switch ("), expr, phrase(") {")), Parsers.pair(between(term("case"), expr, term(":")), stmt.optional()).many(), phrase("default :").next(stmt.optional()).optional().followedBy(term("}")), SwitchStatement::new); }
static Parser<Member> methodDef( Parser<Modifier> mod, Parser<Expression> defaultValue, Parser<Statement> stmt) { return Parsers.sequence( mod.many(), TYPE_PARAMETERS.optional(), TypeLiteralParser.TYPE_LITERAL, Terminals.Identifier.PARSER, term("(").next(StatementParser.parameter(mod).sepBy(term(","))).followedBy(term(")")), term("throws").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL.sepBy1(term(","))).optional(), term("default").next(ExpressionParser.arrayInitializerOrRegularExpression(defaultValue)) .optional(), Parsers.or( StatementParser.blockStatement(stmt), term(";").retn((BlockStatement) null)), MethodDef::new); }