Package org.jparsec
Class Parsers
java.lang.Object
org.jparsec.Parsers
Provides common
Parser
implementations.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAllows the overloads of "or()" to call the varargs version of "or" with no ambiguity.static <T> Parser
<T> always()
Parser
that always succeeds.AParser
that sequentially runsparsers
one by one and collects the return values in an array.static <T> Parser
<T> Equivalent toParser.between(Parser, Parser)
.static <T> Parser
<T> constant
(T v) AParser
that always returnsv
regardless of input.(package private) static Parser
<?> AParser
that succeeds only if EOF is met.static <T> Parser
<T> AParser
that fails and reports thatname
is logically expected.static <T> Parser
<T> AParser
that always fails withmessage
.static <T> Parser
<T> static <T> Parser
<T> AParser
that runs every element ofparsers
and selects the longest match.static <T> Parser
<T> AParser
that runs every element ofparsers
and selects the longest match.(package private) static <T> Parser
<T> static <T> Parser
<T> never()
Parser
that always fails.static <T> Parser
<T> AParser
that tries each alternative parser inalternatives
.static <T> Parser
<T> AParser
that tries each alternative parser inalternatives
.static <T> Parser
<T> AParser
that tries 2 alternative parser objects.static <T> Parser
<T> AParser
that tries 3 alternative parser objects.static <T> Parser
<T> AParser
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) AParser
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) AParser
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) AParser
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) AParser
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) AParser
that tries 9 alternative parser objects.Deprecated.Prefer to converting to your own object with a lambda.static Parser
<?> Deprecated.AParser
that runsparsers
sequentially and discards the return values.AParser
that runsparsers
sequentially and discards the return values.static <T> Parser
<T> AParser
that runs 5 parser objects sequentially.static <T> Parser
<T> AParser
that runs 4 parser objects sequentially.static <T> Parser
<T> AParser
that runs 3 parser objects sequentially.static <T> Parser
<T> AParser
that runs 2 parser objects sequentially.static <A,
B, T> Parser <T> sequence
(Parser<A> p1, Parser<B> p2, 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) AParser
that 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) AParser
that 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) AParser
that 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) AParser
that 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) AParser
that 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) AParser
that runs 7 parser objects sequentially and transforms the return values usingmap
.static <T> Parser
<T> static <T> Parser
<T> AParser
that runs every element ofparsers
and selects the shortest match.static <T> Parser
<T> AParser
that runs every element ofparsers
and selects the shortest match.(package private) static <T> Parser<T>[]
We always convertIterable
to an array to avoid the cost of creating a new object each time the parser runs.private static <T> Parser<T>[]
toArray
(Collection<? extends Parser<? extends T>> parsers) (package private) static <T> Parser<T>[]
toArrayWithIteration
(Iterable<? extends Parser<? extends T>> parsers) static <T> Parser
<T> Checks the current token with thefromToken
object.tokens
(Parser<? extends Collection<Token>> parser) Converts a parser of a collection ofToken
to a parser of an array ofToken
.static <T> Parser
<T> Checks whether the current token value is oftype
, in which case, the token value is returned and parse succeeds.Deprecated.Prefer to converting to your own object with a lambda.Deprecated.Prefer to converting to your own object with a lambda.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
(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
(String name) AParser
that fails and reports thatname
is logically unexpected.
-
Field Details
-
EOF
Parser
that succeeds only if EOF is met. Fails otherwise. -
ANY_TOKEN
AParser
that consumes a token. The token value is returned from the parser. -
INDEX
Deprecated.UseSOURCE_LOCATION
instead.AParser
that retrieves the current index in the source. -
SOURCE_LOCATION
AParser
that 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#getIndex
can be called any time.- Since:
- 3.1
-
ALWAYS
-
NEVER
-
TRUE
-
FALSE
-
-
Constructor Details
-
Parsers
private Parsers()
-
-
Method Details
-
always
Parser
that always succeeds. -
never
Parser
that always fails. -
eof
AParser
that succeeds only if EOF is met. Fails withmessage
otherwise. -
fail
AParser
that always fails withmessage
. -
runnable
Deprecated.AParser
that always succeeds and invokesrunnable
. -
tokens
Converts a parser of a collection ofToken
to a parser of an array ofToken
. -
nested
AParser
that takes as input the array ofToken
returned fromlexer
, and feeds the tokens as input intoparser
.It fails if either
lexer
orparser
fails.- Parameters:
lexer
- the lexer object that returns an array of Tok objects.parser
- the token level parser object.- Returns:
- the new Parser object.
-
constant
AParser
that always returnsv
regardless of input. -
sequence
-
sequence
AParser
that runs 3 parser objects sequentially. -
sequence
AParser
that runs 4 parser objects sequentially. -
sequence
public static <T> Parser<T> sequence(Parser<?> p1, Parser<?> p2, Parser<?> p3, Parser<?> p4, Parser<T> p5) AParser
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.AParser
that sequentially runsp1
andp2
and collects the results in aPair
object. 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.AParser
that sequentially runsp1
andp2
and collects the results in aPair
object. Is equivalent topair(Parser, Parser)
. -
tuple
@Deprecated public static <A,B, Parser<Tuple3<A,C> B, tupleC>> (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, Parser<Tuple4<A,C, D> B, tupleC, D>> (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, Parser<Tuple5<A,C, D, E> B, tupleC, D, E>> (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
AParser
that sequentially runsparsers
one by one and collects the return values in an array. -
list
-
between
Equivalent toParser.between(Parser, Parser)
. Use this to list the parsers in the natural order. -
sequence
public static <A,B, Parser<T> sequenceT> (Parser<A> p1, Parser<B> p2, BiFunction<? super A, ? super B, ? extends T> map) -
sequence
public static <A,B, Parser<T> sequenceC, T> (Parser<A> p1, Parser<B> p2, Parser<C> p3, Map3<? super A, ? super B, ? super C, ? extends T> map) AParser
that runs 3 parser objects sequentially and transforms the return values usingmap
. -
sequence
public static <A,B, Parser<T> sequenceC, D, T> (Parser<A> p1, Parser<B> p2, Parser<C> p3, Parser<D> p4, Map4<? super A, ? super B, ? super C, ? super D, ? extends T> map) AParser
that runs 4 parser objects sequentially and transforms the return values usingmap
. -
sequence
public static <A,B, Parser<T> sequenceC, D, E, T> (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) AParser
that runs 5 parser objects sequentially and transforms the return values usingmap
. -
sequence
public static <A,B, Parser<T> sequenceC, D, E, F, T> (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) AParser
that runs 6 parser objects sequentially and transforms the return values usingmap
.- Since:
- 3.0
-
sequence
public static <A,B, Parser<T> sequenceC, D, E, F, G, T> (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) AParser
that runs 7 parser objects sequentially and transforms the return values usingmap
.- Since:
- 3.0
-
sequence
public static <A,B, Parser<T> sequenceC, D, E, F, G, H, T> (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) AParser
that runs 7 parser objects sequentially and transforms the return values usingmap
.- Since:
- 3.0
-
sequence
AParser
that runsparsers
sequentially and discards the return values. -
sequence
AParser
that runsparsers
sequentially and discards the return values. -
or
AParser
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) AParser
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) AParser
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) AParser
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) AParser
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) AParser
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) AParser
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) AParser
that tries 9 alternative parser objects. Fallback happens regardless of partial match. -
or
AParser
that tries each alternative parser inalternatives
.Different than
alt(Parser[])
, it requires all alternative parsers to have typeT
. -
or
AParser
that tries each alternative parser inalternatives
. -
alt
Allows the overloads of "or()" to call the varargs version of "or" with no ambiguity. -
longer
AParser
that runs bothp1
andp2
and selects the longer match. If both matches the same length, the first one is favored. -
longest
AParser
that runs every element ofparsers
and selects the longest match. If two matches have the same length, the first one is favored. -
longest
AParser
that runs every element ofparsers
and selects the longest match. If two matches have the same length, the first one is favored. -
shorter
AParser
that runs bothp1
andp2
and selects the shorter match. If both matches the same length, the first one is favored. -
shortest
AParser
that runs every element ofparsers
and selects the shortest match. If two matches have the same length, the first one is favored. -
shortest
AParser
that runs every element ofparsers
and selects the shortest match. If two matches have the same length, the first one is favored. -
expect
AParser
that fails and reports thatname
is logically expected. -
unexpected
AParser
that fails and reports thatname
is logically unexpected. -
token
Checks the current token with thefromToken
object. 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
- theFromToken
object.- Returns:
- the new Parser object.
-
tokenType
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
-
toArray
We always convertIterable
to an array to avoid the cost of creating a new object each time the parser runs. -
toArray
-
SOURCE_LOCATION
instead.