@Override public Parser<T> label(String name) { return Parser.this.label(name).atomic(); } @Override boolean apply(ParseContext ctxt) {
/** * 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); }
/** * A {@link Parser} that succeeds only if the {@link Token} objects in the {@link List} are * adjacent. */ public static Parser<Token> adjacent(Parser<List<Token>> parser, final Parser<?> otherwise) { return parser.next(tokens -> { if (tokens.isEmpty()) return Parsers.always(); int offset = tokens.get(0).index(); for (Token token : tokens) { if (token.index() != offset) { return otherwise; } offset += token.length(); } return Parsers.always(); }).atomic().source().token(); }
static Parser<Annotation> annotation(Parser<Expression> expr) { Parser<Annotation.Element> element = Parsers.sequence( Terminals.Identifier.PARSER.followedBy(term("=")).atomic().optional(), ExpressionParser.arrayInitializerOrRegularExpression(expr), Annotation.Element::new); return Parsers.sequence( term("@").next(TypeLiteralParser.ELEMENT_TYPE_LITERAL), paren(element.sepBy(term(","))).optional(), Annotation::new); }