org.codehaus.jparsec
Class Parsers

java.lang.Object
  extended by org.codehaus.jparsec.Parsers

public final class Parsers
extends Object

Provides common Parser implementations.

Author:
Ben Yu

Field Summary
static Parser<Object> ANY_TOKEN
          A Parser that consumes a token.
static Parser<?> EOF
          Parser that succeeds only if EOF is met.
static Parser<Integer> INDEX
          A Parser that retrieves the current index in the source.
 
Method Summary
static
<T> Parser<T>
always()
          Parser that always succeeds.
static Parser<Object[]> array(Parser<?>... parsers)
          A Parser that sequentially runs parsers one by one and collects the return values in an array.
static
<T> Parser<T>
between(Parser<?> before, Parser<T> parser, Parser<?> after)
          Equivalent to Parser.between(Parser, Parser).
static
<T> Parser<T>
constant(T v)
          A Parser that always returns v regardless of input.
static
<T> Parser<T>
expect(String name)
          A Parser that fails and reports that name is logically expected.
static
<T> Parser<T>
fail(String message)
          A Parser that always fails with message.
static
<T> Parser<List<T>>
list(Iterable<? extends Parser<? extends T>> parsers)
          A Parser that sequentially runs parsers one by one and collects the return values in a List.
static
<T> Parser<T>
longer(Parser<? extends T> p1, Parser<? extends T> p2)
          A Parser that runs both p1 and p2 and selects the longer match.
static
<T> Parser<T>
longest(Iterable<? extends Parser<? extends T>> parsers)
          A Parser that runs every element of parsers and selects the longest match.
static
<T> Parser<T>
longest(Parser<? extends T>... parsers)
          A Parser that runs every element of parsers and selects the longest match.
static
<T> Parser<T>
never()
          Parser that always fails.
static
<T> Parser<T>
or(Iterable<? extends Parser<? extends T>> alternatives)
          A Parser that tries each alternative parser in alternatives.
static
<T> Parser<T>
or(Parser<? extends T>... alternatives)
          A Parser that tries each alternative parser in alternatives.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2)
          A Parser that tries 2 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3)
          A Parser that tries 3 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4)
          A Parser that tries 4 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4, Parser<? extends T> p5)
          A Parser that tries 5 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4, Parser<? extends T> p5, Parser<? extends T> p6)
          A Parser that tries 6 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4, Parser<? extends T> p5, Parser<? extends T> p6, Parser<? extends T> p7)
          A Parser that tries 7 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4, Parser<? extends T> p5, Parser<? extends T> p6, Parser<? extends T> p7, Parser<? extends T> p8)
          A Parser that tries 8 alternative parser objects.
static
<T> Parser<T>
or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3, Parser<? extends T> p4, Parser<? extends T> p5, Parser<? extends T> p6, Parser<? extends T> p7, Parser<? extends T> p8, Parser<? extends T> p9)
          A Parser that tries 9 alternative parser objects.
static
<A,B> Parser<Pair<A,B>>
pair(Parser<? extends A> p1, Parser<? extends B> p2)
          A Parser that sequentially runs p1 and p2 and collects the results in a Pair object.
static Parser<?> runnable(Runnable runnable)
          Deprecated. 
static Parser<Object> sequence(Iterable<? extends Parser<?>> parsers)
          A Parser that runs parsers sequentially and discards the return values.
static Parser<Object> sequence(Parser<?>... parsers)
          A Parser that runs parsers sequentially and discards the return values.
static
<T> Parser<T>
sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<?> p4, Parser<T> p5)
          A Parser that runs 5 parser objects sequentially.
static
<T> Parser<T>
sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<T> p4)
          A Parser that runs 4 parser objects sequentially.
static
<T> Parser<T>
sequence(Parser<?> p1, Parser<?> p2, Parser<T> p3)
          A Parser that runs 3 parser objects sequentially.
static
<T> Parser<T>
sequence(Parser<?> p1, Parser<T> p2)
          A Parser that runs 2 parser objects sequentially.
static
<A,B,T> Parser<T>
sequence(Parser<A> p1, Parser<B> p2, Map2<? super A,? super B,? extends T> map)
          A Parser that runs p1 and p2 sequentially and transforms the return values using map.
static
<A,B,C,T> Parser<T>
sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Map3<? super A,? super B,? super C,? extends T> map)
          A Parser that runs 3 parser objects sequentially and transforms the return values using map.
static
<A,B,C,D,T>
Parser<T>
sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Map4<? super A,? super B,? super C,? super D,? extends T> map)
          A Parser that runs 4 parser objects sequentially and transforms the return values using map.
static
<A,B,C,D,E,T>
Parser<T>
sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5, Map5<? super A,? super B,? super C,? super D,? super E,? extends T> map)
          A Parser that runs 5 parser objects sequentially and transforms the return values using map.
static
<T> Parser<T>
shorter(Parser<? extends T> p1, Parser<? extends T> p2)
          A Parser that runs both p1 and p2 and selects the shorter match.
static
<T> Parser<T>
shortest(Iterable<? extends Parser<? extends T>> parsers)
          A Parser that runs every element of parsers and selects the shortest match.
static
<T> Parser<T>
shortest(Parser<? extends T>... parsers)
          A Parser that runs every element of parsers and selects the shortest match.
static
<T> Parser<T>
token(TokenMap<? extends T> fromToken)
          Checks the current token with the fromToken object.
static
<T> Parser<T>
tokenType(Class<? extends T> type, String name)
          Checks whether the current token value is of type, in which case, the token value is returned and parse succeeds.
static
<A,B> Parser<Pair<A,B>>
tuple(Parser<? extends A> p1, Parser<? extends B> p2)
          A Parser that sequentially runs p1 and p2 and collects the results in a Pair object.
static
<A,B,C> Parser<Tuple3<A,B,C>>
tuple(Parser<? extends A> p1, Parser<? extends B> p2, Parser<? extends C> p3)
          A Parser that sequentially runs 3 parser objects and collects the results in a Tuple3 object.
static
<A,B,C,D> Parser<Tuple4<A,B,C,D>>
tuple(Parser<? extends A> p1, Parser<? extends B> p2, Parser<? extends C> p3, Parser<? extends D> p4)
          A Parser that sequentially runs 4 parser objects and collects the results in a Tuple4 object.
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)
          A Parser that sequentially runs 5 parser objects and collects the results in a Tuple5 object.
static
<T> Parser<T>
unexpected(String name)
          A Parser that fails and reports that name is logically unexpected.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final Parser<?> EOF
Parser that succeeds only if EOF is met. Fails otherwise.


ANY_TOKEN

public static final Parser<Object> ANY_TOKEN
A Parser that consumes a token. The token value is returned from the parser.


INDEX

public static final Parser<Integer> INDEX
A Parser that retrieves the current index in the source.

Method Detail

always

public static <T> Parser<T> always()
Parser that always succeeds.


never

public static <T> Parser<T> never()
Parser that always fails.


fail

public static <T> Parser<T> fail(String message)
A Parser that always fails with message.


runnable

@Deprecated
public static Parser<?> runnable(Runnable runnable)
Deprecated. 

A Parser that always succeeds and invokes runnable.


constant

public static <T> Parser<T> constant(T v)
A Parser that always returns v regardless of input.


sequence

public static <T> Parser<T> sequence(Parser<?> p1,
                                     Parser<T> p2)
A Parser that runs 2 parser objects sequentially. p1 is executed, if it succeeds, p2 is executed.


sequence

public static <T> Parser<T> sequence(Parser<?> p1,
                                     Parser<?> p2,
                                     Parser<T> p3)
A Parser that runs 3 parser objects sequentially.


sequence

public static <T> Parser<T> sequence(Parser<?> p1,
                                     Parser<?> p2,
                                     Parser<?> p3,
                                     Parser<T> p4)
A Parser that runs 4 parser objects sequentially.


sequence

public static <T> Parser<T> sequence(Parser<?> p1,
                                     Parser<?> p2,
                                     Parser<?> p3,
                                     Parser<?> p4,
                                     Parser<T> p5)
A Parser that runs 5 parser objects sequentially.


pair

public static <A,B> Parser<Pair<A,B>> pair(Parser<? extends A> p1,
                                           Parser<? extends B> p2)
A Parser that sequentially runs p1 and p2 and collects the results in a Pair object. Is equivalent to tuple(Parser, Parser).


tuple

public static <A,B> Parser<Pair<A,B>> tuple(Parser<? extends A> p1,
                                            Parser<? extends B> p2)
A Parser that sequentially runs p1 and p2 and collects the results in a Pair object. Is equivalent to pair(Parser, Parser).


tuple

public static <A,B,C> Parser<Tuple3<A,B,C>> tuple(Parser<? extends A> p1,
                                                  Parser<? extends B> p2,
                                                  Parser<? extends C> p3)
A Parser that sequentially runs 3 parser objects and collects the results in a Tuple3 object.


tuple

public static <A,B,C,D> Parser<Tuple4<A,B,C,D>> tuple(Parser<? extends A> p1,
                                                      Parser<? extends B> p2,
                                                      Parser<? extends C> p3,
                                                      Parser<? extends D> p4)
A Parser that sequentially runs 4 parser objects and collects the results in a Tuple4 object.


tuple

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)
A Parser that sequentially runs 5 parser objects and collects the results in a Tuple5 object.


array

public static Parser<Object[]> array(Parser<?>... parsers)
A Parser that sequentially runs parsers one by one and collects the return values in an array.


list

public static <T> Parser<List<T>> list(Iterable<? extends Parser<? extends T>> parsers)
A Parser that sequentially runs parsers one by one and collects the return values in a List.


between

public static <T> Parser<T> between(Parser<?> before,
                                    Parser<T> parser,
                                    Parser<?> after)
Equivalent to Parser.between(Parser, Parser). Use this to list the parsers in the natural order.


sequence

public static <A,B,T> Parser<T> sequence(Parser<A> p1,
                                         Parser<B> p2,
                                         Map2<? super A,? super B,? extends T> map)
A Parser that runs p1 and p2 sequentially and transforms the return values using map.


sequence

public static <A,B,C,T> Parser<T> sequence(Parser<A> p1,
                                           Parser<B> p2,
                                           Parser<C> p3,
                                           Map3<? super A,? super B,? super C,? extends T> map)
A Parser that runs 3 parser objects sequentially and transforms the return values using map.


sequence

public static <A,B,C,D,T> Parser<T> sequence(Parser<A> p1,
                                             Parser<B> p2,
                                             Parser<C> p3,
                                             Parser<D> p4,
                                             Map4<? super A,? super B,? super C,? super D,? extends T> map)
A Parser that runs 4 parser objects sequentially and transforms the return values using map.


sequence

public static <A,B,C,D,E,T> Parser<T> sequence(Parser<A> p1,
                                               Parser<B> p2,
                                               Parser<C> p3,
                                               Parser<D> p4,
                                               Parser<E> p5,
                                               Map5<? super A,? super B,? super C,? super D,? super E,? extends T> map)
A Parser that runs 5 parser objects sequentially and transforms the return values using map.


sequence

public static Parser<Object> sequence(Parser<?>... parsers)
A Parser that runs parsers sequentially and discards the return values.


sequence

public static Parser<Object> sequence(Iterable<? extends Parser<?>> parsers)
A Parser that runs parsers sequentially and discards the return values.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2)
A Parser that tries 2 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3)
A Parser that tries 3 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4)
A Parser that tries 4 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4,
                               Parser<? extends T> p5)
A Parser that tries 5 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4,
                               Parser<? extends T> p5,
                               Parser<? extends T> p6)
A Parser that tries 6 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4,
                               Parser<? extends T> p5,
                               Parser<? extends T> p6,
                               Parser<? extends T> p7)
A Parser that tries 7 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4,
                               Parser<? extends T> p5,
                               Parser<? extends T> p6,
                               Parser<? extends T> p7,
                               Parser<? extends T> p8)
A Parser that tries 8 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T> p1,
                               Parser<? extends T> p2,
                               Parser<? extends T> p3,
                               Parser<? extends T> p4,
                               Parser<? extends T> p5,
                               Parser<? extends T> p6,
                               Parser<? extends T> p7,
                               Parser<? extends T> p8,
                               Parser<? extends T> p9)
A Parser that tries 9 alternative parser objects. Fallback happens regardless of partial match.


or

public static <T> Parser<T> or(Parser<? extends T>... alternatives)
A Parser that tries each alternative parser in alternatives.

Different than alt(Parser[]), it requires all alternative parsers to have type T.


or

public static <T> Parser<T> or(Iterable<? extends Parser<? extends T>> alternatives)
A Parser that tries each alternative parser in alternatives.


longer

public static <T> Parser<T> longer(Parser<? extends T> p1,
                                   Parser<? extends T> p2)
A Parser that runs both p1 and p2 and selects the longer match. If both matches the same length, the first one is favored.


longest

public static <T> Parser<T> longest(Parser<? extends T>... parsers)
A Parser that runs every element of parsers and selects the longest match. If two matches have the same length, the first one is favored.


longest

public static <T> Parser<T> longest(Iterable<? extends Parser<? extends T>> parsers)
A Parser that runs every element of parsers and selects the longest match. If two matches have the same length, the first one is favored.


shorter

public static <T> Parser<T> shorter(Parser<? extends T> p1,
                                    Parser<? extends T> p2)
A Parser that runs both p1 and p2 and selects the shorter match. If both matches the same length, the first one is favored.


shortest

public static <T> Parser<T> shortest(Parser<? extends T>... parsers)
A Parser that runs every element of parsers and selects the shortest match. If two matches have the same length, the first one is favored.


shortest

public static <T> Parser<T> shortest(Iterable<? extends Parser<? extends T>> parsers)
A Parser that runs every element of parsers and selects the shortest match. If two matches have the same length, the first one is favored.


expect

public static <T> Parser<T> expect(String name)
A Parser that fails and reports that name is logically expected.


unexpected

public static <T> Parser<T> unexpected(String name)
A Parser that fails and reports that name is logically unexpected.


token

public static <T> Parser<T> token(TokenMap<? extends T> fromToken)
Checks the current token with the fromToken object. If the TokenMap.map(Token) method returns null, an unexpected token error occurs; if the method returns a non-null value, the value is returned and the parser succeeds.

Parameters:
fromToken - the FromToken object.
Returns:
the new Parser object.

tokenType

public static <T> Parser<T> tokenType(Class<? extends T> type,
                                      String name)
Checks whether the current token value is of type, in which case, the token value is returned and parse succeeds.

Parameters:
type - the expected token value type.
name - the name of what's logically expected.
Returns:
the new Parser object.


Copyright © 2014. All rights reserved.