Package org.jparsec
Class Parsers
- java.lang.Object
-
- org.jparsec.Parsers
-
public final class Parsers extends java.lang.ObjectProvides commonParserimplementations.
-
-
Field Summary
Fields Modifier and Type Field Description private static ParserALWAYSstatic Parser<java.lang.Object>ANY_TOKENAParserthat consumes a token.static Parser<?>EOFParserthat succeeds only if EOF is met.(package private) static Parser<java.lang.Boolean>FALSEstatic Parser<java.lang.Integer>INDEXDeprecated.UseSOURCE_LOCATIONinstead.private static ParserNEVERstatic Parser<SourceLocation>SOURCE_LOCATIONAParserthat returns the current location in the source.(package private) static Parser<java.lang.Boolean>TRUE
-
Constructor Summary
Constructors Modifier Constructor Description privateParsers()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static Parser<java.lang.Object>alt(Parser<?>... alternatives)Allows the overloads of "or()" to call the varargs version of "or" with no ambiguity.static <T> Parser<T>always()Parserthat always succeeds.static Parser<java.lang.Object[]>array(Parser<?>... parsers)AParserthat sequentially runsparsersone by one and collects the return values in an array.static <T> Parser<T>between(Parser<?> before, Parser<T> parser, Parser<?> after)Equivalent toParser.between(Parser, Parser).static <T> Parser<T>constant(T v)AParserthat always returnsvregardless of input.(package private) static Parser<?>eof(java.lang.String message)AParserthat succeeds only if EOF is met.static <T> Parser<T>expect(java.lang.String name)AParserthat fails and reports thatnameis logically expected.static <T> Parser<T>fail(java.lang.String message)AParserthat always fails withmessage.static <T> Parser<java.util.List<T>>list(java.lang.Iterable<? extends Parser<? extends T>> parsers)static <T> Parser<T>longer(Parser<? extends T> p1, Parser<? extends T> p2)static <T> Parser<T>longest(java.lang.Iterable<? extends Parser<? extends T>> parsers)AParserthat runs every element ofparsersand selects the longest match.static <T> Parser<T>longest(Parser<? extends T>... parsers)AParserthat runs every element ofparsersand selects the longest match.(package private) static <T> Parser<T>nested(Parser<Token[]> lexer, Parser<? extends T> parser)static <T> Parser<T>never()Parserthat always fails.static <T> Parser<T>or(java.lang.Iterable<? extends Parser<? extends T>> alternatives)AParserthat tries each alternative parser inalternatives.static <T> Parser<T>or(Parser<? extends T>... alternatives)AParserthat tries each alternative parser inalternatives.static <T> Parser<T>or(Parser<? extends T> p1, Parser<? extends T> p2)AParserthat tries 2 alternative parser objects.static <T> Parser<T>or(Parser<? extends T> p1, Parser<? extends T> p2, Parser<? extends T> p3)AParserthat 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)AParserthat 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)AParserthat 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)AParserthat 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)AParserthat 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)AParserthat 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)AParserthat tries 9 alternative parser objects.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.static Parser<?>runnable(java.lang.Runnable runnable)Deprecated.static Parser<java.lang.Object>sequence(java.lang.Iterable<? extends Parser<?>> parsers)AParserthat runsparserssequentially and discards the return values.static Parser<java.lang.Object>sequence(Parser<?>... parsers)AParserthat runsparserssequentially and discards the return values.static <T> Parser<T>sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<?> p4, Parser<T> p5)AParserthat runs 5 parser objects sequentially.static <T> Parser<T>sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<T> p4)AParserthat runs 4 parser objects sequentially.static <T> Parser<T>sequence(Parser<?> p1, Parser<?> p2, Parser<T> p3)AParserthat runs 3 parser objects sequentially.static <T> Parser<T>sequence(Parser<?> p1, Parser<T> p2)AParserthat runs 2 parser objects sequentially.static <A,B,T>
Parser<T>sequence(Parser<A> p1, Parser<B> p2, java.util.function.BiFunction<? super A,? super B,? extends T> 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)AParserthat runs 3 parser objects sequentially and transforms the return values usingmap.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)AParserthat runs 4 parser objects sequentially and transforms the return values usingmap.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)AParserthat runs 5 parser objects sequentially and transforms the return values usingmap.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)AParserthat runs 6 parser objects sequentially and transforms the return values usingmap.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)AParserthat runs 7 parser objects sequentially and transforms the return values usingmap.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)AParserthat runs 7 parser objects sequentially and transforms the return values usingmap.static <T> Parser<T>shorter(Parser<? extends T> p1, Parser<? extends T> p2)static <T> Parser<T>shortest(java.lang.Iterable<? extends Parser<? extends T>> parsers)AParserthat runs every element ofparsersand selects the shortest match.static <T> Parser<T>shortest(Parser<? extends T>... parsers)AParserthat runs every element ofparsersand selects the shortest match.(package private) static <T> Parser<T>[]toArray(java.lang.Iterable<? extends Parser<? extends T>> parsers)We always convertIterableto an array to avoid the cost of creating a new object each time the parser runs.private static <T> Parser<T>[]toArray(java.util.Collection<? extends Parser<? extends T>> parsers)(package private) static <T> Parser<T>[]toArrayWithIteration(java.lang.Iterable<? extends Parser<? extends T>> parsers)static <T> Parser<T>token(TokenMap<? extends T> fromToken)Checks the current token with thefromTokenobject.(package private) static Parser<Token[]>tokens(Parser<? extends java.util.Collection<Token>> parser)Converts a parser of a collection ofTokento a parser of an array ofToken.static <T> Parser<T>tokenType(java.lang.Class<? extends T> type, java.lang.String name)Checks whether the current token value is oftype, 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)Deprecated.Prefer to converting to your own object with a lambda.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.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.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.static <T> Parser<T>unexpected(java.lang.String name)AParserthat fails and reports thatnameis logically unexpected.
-
-
-
Field Detail
-
ANY_TOKEN
public static final Parser<java.lang.Object> ANY_TOKEN
AParserthat consumes a token. The token value is returned from the parser.
-
INDEX
@Deprecated public static final Parser<java.lang.Integer> INDEX
Deprecated.UseSOURCE_LOCATIONinstead.AParserthat retrieves the current index in the source.
-
SOURCE_LOCATION
public static final Parser<SourceLocation> SOURCE_LOCATION
AParserthat returns the current location in the source.Because
SourceLocation.getLine()andSourceLocation.getColumn()take amortizedlog(n)time, it's more efficient to avoid calling them until the entire source has been parsed successfully. In other words, avoidSOURCE_LOCATION.map(SourceLocation::getLine)or anything similar.SourceLocation#getIndexcan be called any time.- Since:
- 3.1
-
ALWAYS
private static final Parser ALWAYS
-
NEVER
private static final Parser NEVER
-
TRUE
static final Parser<java.lang.Boolean> TRUE
-
FALSE
static final Parser<java.lang.Boolean> FALSE
-
-
Method Detail
-
eof
static Parser<?> eof(java.lang.String message)
AParserthat succeeds only if EOF is met. Fails withmessageotherwise.
-
fail
public static <T> Parser<T> fail(java.lang.String message)
AParserthat always fails withmessage.
-
runnable
@Deprecated public static Parser<?> runnable(java.lang.Runnable runnable)
Deprecated.AParserthat always succeeds and invokesrunnable.
-
tokens
static Parser<Token[]> tokens(Parser<? extends java.util.Collection<Token>> parser)
Converts a parser of a collection ofTokento a parser of an array ofToken.
-
nested
static <T> Parser<T> nested(Parser<Token[]> lexer, Parser<? extends T> parser)
AParserthat takes as input the array ofTokenreturned fromlexer, and feeds the tokens as input intoparser.It fails if either
lexerorparserfails.- Parameters:
lexer- the lexer object that returns an array of Tok objects.parser- the token level parser object.- Returns:
- the new Parser object.
-
constant
public static <T> Parser<T> constant(T v)
AParserthat always returnsvregardless of input.
-
sequence
public static <T> Parser<T> sequence(Parser<?> p1, Parser<?> p2, Parser<T> p3)
AParserthat runs 3 parser objects sequentially.
-
sequence
public static <T> Parser<T> sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<T> p4)
AParserthat runs 4 parser objects sequentially.
-
sequence
public static <T> Parser<T> sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<?> p4, Parser<T> p5)
AParserthat 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.AParserthat sequentially runsp1andp2and collects the results in aPairobject. Is equivalent totuple(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.AParserthat sequentially runsp1andp2and collects the results in aPairobject. Is equivalent topair(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.
-
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.
-
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.
-
array
public static Parser<java.lang.Object[]> array(Parser<?>... parsers)
AParserthat sequentially runsparsersone by one and collects the return values in an array.
-
list
public static <T> Parser<java.util.List<T>> list(java.lang.Iterable<? extends Parser<? extends T>> parsers)
-
between
public static <T> Parser<T> between(Parser<?> before, Parser<T> parser, Parser<?> after)
Equivalent toParser.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, java.util.function.BiFunction<? super A,? super B,? extends T> 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)
AParserthat runs 3 parser objects sequentially and transforms the return values usingmap.
-
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)
AParserthat runs 4 parser objects sequentially and transforms the return values usingmap.
-
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)
AParserthat runs 5 parser objects sequentially and transforms the return values usingmap.
-
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)
AParserthat runs 6 parser objects sequentially and transforms the return values usingmap.- 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)
AParserthat runs 7 parser objects sequentially and transforms the return values usingmap.- 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)
AParserthat runs 7 parser objects sequentially and transforms the return values usingmap.- Since:
- 3.0
-
sequence
public static Parser<java.lang.Object> sequence(Parser<?>... parsers)
AParserthat runsparserssequentially and discards the return values.
-
sequence
public static Parser<java.lang.Object> sequence(java.lang.Iterable<? extends Parser<?>> parsers)
AParserthat runsparserssequentially and discards the return values.
-
or
public static <T> Parser<T> or(Parser<? extends T> p1, Parser<? extends T> p2)
AParserthat 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)
AParserthat 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)
AParserthat 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)
AParserthat 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)
AParserthat 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)
AParserthat 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)
AParserthat 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)
AParserthat tries 9 alternative parser objects. Fallback happens regardless of partial match.
-
or
public static <T> Parser<T> or(Parser<? extends T>... alternatives)
AParserthat tries each alternative parser inalternatives.Different than
alt(Parser[]), it requires all alternative parsers to have typeT.
-
or
public static <T> Parser<T> or(java.lang.Iterable<? extends Parser<? extends T>> alternatives)
AParserthat tries each alternative parser inalternatives.
-
alt
private static Parser<java.lang.Object> alt(Parser<?>... alternatives)
Allows the overloads of "or()" to call the varargs version of "or" with no ambiguity.
-
longer
public static <T> Parser<T> longer(Parser<? extends T> p1, Parser<? extends T> p2)
AParserthat runs bothp1andp2and 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)
AParserthat runs every element ofparsersand selects the longest match. If two matches have the same length, the first one is favored.
-
longest
public static <T> Parser<T> longest(java.lang.Iterable<? extends Parser<? extends T>> parsers)
AParserthat runs every element ofparsersand 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)
AParserthat runs bothp1andp2and 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)
AParserthat runs every element ofparsersand selects the shortest match. If two matches have the same length, the first one is favored.
-
shortest
public static <T> Parser<T> shortest(java.lang.Iterable<? extends Parser<? extends T>> parsers)
AParserthat runs every element ofparsersand selects the shortest match. If two matches have the same length, the first one is favored.
-
expect
public static <T> Parser<T> expect(java.lang.String name)
AParserthat fails and reports thatnameis logically expected.
-
unexpected
public static <T> Parser<T> unexpected(java.lang.String name)
AParserthat fails and reports thatnameis logically unexpected.
-
token
public static <T> Parser<T> token(TokenMap<? extends T> fromToken)
Checks the current token with thefromTokenobject. If theTokenMap.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- theFromTokenobject.- Returns:
- the new Parser object.
-
tokenType
public static <T> Parser<T> tokenType(java.lang.Class<? extends T> type, java.lang.String name)
Checks whether the current token value is oftype, 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.
-
toArrayWithIteration
static <T> Parser<T>[] toArrayWithIteration(java.lang.Iterable<? extends Parser<? extends T>> parsers)
-
toArray
static <T> Parser<T>[] toArray(java.lang.Iterable<? extends Parser<? extends T>> parsers)
We always convertIterableto an array to avoid the cost of creating a new object each time the parser runs.
-
-