Package at.jku.ssw.fp.sect06_3
Interface Parser<E>
-
- Type Parameters:
E
- the type of parse result.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Parser<E> extends Function<FList<String>,Result<E>>
Functional interface for parsers.- Author:
- Herbert Praehofer
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description static <R> Parser<R>
alt(Parser<? extends R>... alternatives)
Creates a parser which tries to apply one of the given alternative parsers.static <R> Parser<R>
alt(Supplier<Parser<? extends R>>... pSuppliers)
Creates a parser which tries to apply one of the alternative parsers.default Parser<E>
filter(Predicate<? super E> pred)
Creates a new parser from this parser which is only successful if the parse result fulfills the given predicate.default <R> Parser<R>
flatMap(Function<? super E,? extends Parser<? extends R>> f)
The monadic flatMap operator for parsers.static <R> Parser<FList<R>>
fmultRec(Parser<R> parser)
Recursive variant ofmult(Parser)
.default <R> Parser<R>
map(Function<E,R> f)
Creates a new parser from this parser where the parse result is mapped by the given function.static <R> Parser<FList<R>>
mult(Parser<R> parser)
Creates a parser which applies the given parser as long as it is successful.static <E> Parser<E>
of(E e)
Creates a parser which parses the given element as success without consuming a token.static <R> Parser<Optional<R>>
opt(Parser<R> parser)
Creates a parser which optionally applies the given parser.default <S> Parser<Pair<E,S>>
seq(Parser<S> second)
Creates a new parser which applies this parser and the second parser and combined both results in aPair
.static Parser<String>
token(String tkn)
A parser parsing a token if it is equal to the given string.
-
-
-
Method Detail
-
of
static <E> Parser<E> of(E e)
Creates a parser which parses the given element as success without consuming a token.- Type Parameters:
E
- the type of the parse result- Parameters:
e
- the element representing the parse result- Returns:
- the parser object
-
flatMap
default <R> Parser<R> flatMap(Function<? super E,? extends Parser<? extends R>> f)
The monadic flatMap operator for parsers.- Type Parameters:
R
- the return type of the flatMap- Parameters:
f
- the mapping function- Returns:
- the new parser
-
seq
default <S> Parser<Pair<E,S>> seq(Parser<S> second)
Creates a new parser which applies this parser and the second parser and combined both results in aPair
. Uses theflatMap(Function)
monadic operator.- Type Parameters:
S
- the type of the parse result of the second parser- Parameters:
second
- the second parser- Returns:
- the combined parser
-
filter
default Parser<E> filter(Predicate<? super E> pred)
Creates a new parser from this parser which is only successful if the parse result fulfills the given predicate.- Parameters:
pred
- the predicate to test the parse result- Returns:
- the new parser
-
map
default <R> Parser<R> map(Function<E,R> f)
Creates a new parser from this parser where the parse result is mapped by the given function. Uses theflatMap(Function)
monadic operator.- Type Parameters:
R
- the type of the parse result of the new parser- Parameters:
f
- the mapping function- Returns:
- the new parser
-
alt
@SafeVarargs static <R> Parser<R> alt(Parser<? extends R>... alternatives)
Creates a parser which tries to apply one of the given alternative parsers.- Type Parameters:
R
- the common type of the parse results of the alternative parsers.- Parameters:
alternatives
- the alternative parsers- Returns:
- the new parser
-
alt
@SafeVarargs static <R> Parser<R> alt(Supplier<Parser<? extends R>>... pSuppliers)
Creates a parser which tries to apply one of the alternative parsers. The alternative parsers are created by the given supplier functions.- Type Parameters:
R
- the common type of the parse results of the alternative parsers.- Parameters:
pSuppliers
- the supplier functions for the alternative parsers- Returns:
- the new parser
-
mult
static <R> Parser<FList<R>> mult(Parser<R> parser)
Creates a parser which applies the given parser as long as it is successful. The parse result is a list of the parse results of the multiple applications.- Type Parameters:
R
- the type of the parse results of the given parser- Parameters:
parser
- the parser to apply multiple times- Returns:
- the parser applying the given parser multiple times
-
opt
static <R> Parser<Optional<R>> opt(Parser<R> parser)
Creates a parser which optionally applies the given parser. The result of the created parser is an optional. If the parser is successful, then the parse result is returned in an optional; otherwise the optional is empty and no token is consumed. The parser is always successful but possibly with an empty result.- Type Parameters:
R
- the type of the parse result- Parameters:
parser
- the parser to optionally apply- Returns:
- the optional parser
-
token
static Parser<String> token(String tkn)
A parser parsing a token if it is equal to the given string.- Parameters:
tkn
- the token to parse- Returns:
- the parser parsing a token which is equal to
tkn
.
-
fmultRec
static <R> Parser<FList<R>> fmultRec(Parser<R> parser)
Recursive variant ofmult(Parser)
.- Type Parameters:
R
- the type of the parse results of the given parser- Parameters:
parser
- the parser to apply multiple times- Returns:
- the parser applying the given parser multiple times
-
-