Class Parsers

java.lang.Object
org.jparsec.Parsers

public final class Parsers extends Object
Provides common Parser implementations.
Author:
Ben Yu
  • Field Details

    • 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

      @Deprecated public static final Parser<Integer> INDEX
      Deprecated.
      Use SOURCE_LOCATION instead.
      A Parser that retrieves the current index in the source.
    • SOURCE_LOCATION

      public static final Parser<SourceLocation> SOURCE_LOCATION
      A Parser that returns the current location in the source.

      Because SourceLocation.getLine() and SourceLocation.getColumn() take amortized log(n) time, it's more efficient to avoid calling them until the entire source has been parsed successfully. In other words, avoid SOURCE_LOCATION.map(SourceLocation::getLine) or anything similar.

      SourceLocation#getIndex can be called any time.

      Since:
      3.1
  • Method Details

    • 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

      @Deprecated public static <A,B> Parser<Pair<A,B>> pair(Parser<? extends A> p1, Parser<? extends B> p2)
      Deprecated.
      Prefer to converting to your own object with a lambda.
      A Parser that sequentially runs p1 and p2 and collects the results in a Pair object. Is equivalent to tuple(Parser, Parser).
    • tuple

      @Deprecated public static <A,B> Parser<Pair<A,B>> tuple(Parser<? extends A> p1, Parser<? extends B> p2)
      Deprecated.
      Prefer to converting to your own object with a lambda.
      A Parser that sequentially runs p1 and p2 and collects the results in a Pair object. Is equivalent to pair(Parser, Parser).
    • tuple

      @Deprecated public static <A,B,C> Parser<Tuple3<A,B,C>> tuple(Parser<? extends A> p1, Parser<? extends B> p2, Parser<? extends C> p3)
      Deprecated.
      Prefer to converting to your own object with a lambda.
      A Parser that sequentially runs 3 parser objects and collects the results in a Tuple3 object.
    • tuple

      @Deprecated 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)
      Deprecated.
      Prefer to converting to your own object with a lambda.
      A Parser that sequentially runs 4 parser objects and collects the results in a Tuple4 object.
    • tuple

      @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)
      Deprecated.
      Prefer to converting to your own object with a lambda.
      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, BiFunction<? 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 <A,B,C,D,E,F,T> Parser<T> sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5, Parser<F> p6, Map6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? extends T> map)
      A Parser that runs 6 parser objects sequentially and transforms the return values using map.
      Since:
      3.0
    • sequence

      public static <A,B,C,D,E,F,G,T> Parser<T> sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5, Parser<F> p6, Parser<G> p7, Map7<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? extends T> map)
      A Parser that runs 7 parser objects sequentially and transforms the return values using map.
      Since:
      3.0
    • sequence

      public static <A,B,C,D,E,F,G,H,T> Parser<T> sequence(Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Parser<E> p5, Parser<F> p6, Parser<G> p7, Parser<H> p8, Map8<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H, ? extends T> map)
      A Parser that runs 7 parser objects sequentially and transforms the return values using map.
      Since:
      3.0
    • 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.