LazyParsley
This class exposes the commonly used combinators in Parsley. For a description of why the library is designed in this way, see: the Parsley wiki
This class exposes the commonly used combinators in Parsley. For a description of why the library is designed in this way, see: the Parsley wiki
- Value Params
- con
A conversion (if required) to turn
p
into a parser- p
The parser which serves as the method receiver
- Version
1.0.0
Value members
Concrete methods
This is the parser that corresponds to p *> pure(x)
or a more optimal version of p.map(_ => x)
.
It performs the parse action of the invokee but discards its result and then results the value x
instead
This is the parser that corresponds to p *> pure(x)
or a more optimal version of p.map(_ => x)
.
It performs the parse action of the invokee but discards its result and then results the value x
instead
- Value Params
- x
The value to be returned after the execution of the invokee
- Returns
A new parser which first parses the invokee, then results
x
This is the parser that corresponds to a more optimal version of p.map(_ => x => x) <*> q
. It performs
the parse action of both parsers, in order, but discards the result of the invokee.
This is the parser that corresponds to a more optimal version of p.map(_ => x => x) <*> q
. It performs
the parse action of both parsers, in order, but discards the result of the invokee.
- Value Params
- q
The parser whose result should be returned
- Returns
A new parser which first parses
p
, thenq
and returns the result ofq
This is the parser that corresponds to a more optimal version of p.map(x => _ => x) <*> q
. It performs
the parse action of both parsers, in order, but discards the result of the second parser.
This is the parser that corresponds to a more optimal version of p.map(x => _ => x) <*> q
. It performs
the parse action of both parsers, in order, but discards the result of the second parser.
- Value Params
- q
The parser who should be executed but then discarded
- Returns
A new parser which first parses
p
, thenq
and returns the result of thep
This combinator is defined as lift2((x, f) => f(x), p, f)
. It is pure syntactic sugar.
This combinator is defined as lift2((x, f) => f(x), p, f)
. It is pure syntactic sugar.
This is the Applicative application parser. The type of pf
is Parsley[A => B]
. Then, given a
Parsley[A]
, we can produce a Parsley[B]
by parsing pf
to retrieve f: A => B
, then parse px
to receive x: A
then return f(x): B
.
This is the Applicative application parser. The type of pf
is Parsley[A => B]
. Then, given a
Parsley[A]
, we can produce a Parsley[B]
by parsing pf
to retrieve f: A => B
, then parse px
to receive x: A
then return f(x): B
.
- Value Params
- px
A parser of type A, where the invokee is A => B
- Returns
A new parser which parses
pf
, thenpx
then applies the value returned bypx
to the function returned bypf
- Note
pure(f) <*> p
is subject to the same aggressive optimisations asmap
. When using impure functions the optimiser may decide to cache the result of the function execution, be sure to useunsafe
in order to prevent these optimisations.
This combinator, pronounced "sum", is similar to <|>
, except it allows the
types of either side of the combinator to vary by returning their result as
part of an Either
.
This combinator, pronounced "sum", is similar to <|>
, except it allows the
types of either side of the combinator to vary by returning their result as
part of an Either
.
- Value Params
- q
The parser to run if the invokee failed without consuming input
- Returns
the result of the parser which succeeded, if any
This combinator is defined as p <|> pure(x)
. It is pure syntactic sugar.
This combinator is defined as p <|> pure(x)
. It is pure syntactic sugar.
This is the traditional Alternative choice operator for parsers. Following the parsec semantics precisely,
this combinator first tries to parse the invokee. If this is successful, no further action is taken. If the
invokee failed without consuming input, then q
is parsed instead. If the invokee did parse input then the
whole parser fails. This is done to prevent space leaks and to give good error messages. If this behaviour
is not desired, use the <\>
combinator (or attempt(this) <|> q
) to parse q
regardless of how the
invokee failed.
This is the traditional Alternative choice operator for parsers. Following the parsec semantics precisely,
this combinator first tries to parse the invokee. If this is successful, no further action is taken. If the
invokee failed without consuming input, then q
is parsed instead. If the invokee did parse input then the
whole parser fails. This is done to prevent space leaks and to give good error messages. If this behaviour
is not desired, use the <\>
combinator (or attempt(this) <|> q
) to parse q
regardless of how the
invokee failed.
- Value Params
- q
The parser to run if the invokee failed without consuming input
- Returns
The value produced by the invokee if it was successful, or if it failed without consuming input, the possible result of parsing q.
This is the parser that corresponds to a more optimal version of (p <~> q).map(_._1)
. It performs
the parse action of both parsers, in order, but discards the result of the second parser.
This is the parser that corresponds to a more optimal version of (p <~> q).map(_._1)
. It performs
the parse action of both parsers, in order, but discards the result of the second parser.
- Value Params
- q
The parser who should be executed but then discarded
- Returns
A new parser which first parses
p
, thenq
and returns the result of thep
- Since
2.4.0
This parser corresponds to lift2((_, _), p, q)
. For now it is sugar, but in future may be more optimal
This parser corresponds to lift2((_, _), p, q)
. For now it is sugar, but in future may be more optimal
This casts the result of the parser into a new type B
. If the value returned by the parser
is castable to type B
, then this cast is performed. Otherwise the parser fails.
This casts the result of the parser into a new type B
. If the value returned by the parser
is castable to type B
, then this cast is performed. Otherwise the parser fails.
- Type Params
- B
The type to attempt to cast into
- Since
2.0.0
Attempts to first filter the parser to ensure that pf
is defined over it. If it is, then the function pf
is mapped over its result. Roughly the same as a filter
then a map
.
Attempts to first filter the parser to ensure that pf
is defined over it. If it is, then the function pf
is mapped over its result. Roughly the same as a filter
then a map
.
- Value Params
- pf
The partial function
- Returns
The result of applying
pf
to this parsers value (if possible), or fails- Since
2.0.0
Filter the value of a parser; if the value returned by the parser matches the predicate pred
then the
filter succeeded, otherwise the parser fails with an empty error
Filter the value of a parser; if the value returned by the parser matches the predicate pred
then the
filter succeeded, otherwise the parser fails with an empty error
- Value Params
- pred
The predicate that is tested against the parser result
- Returns
The result of the invokee if it passes the predicate
Filter the value of a parser; if the value returned by the parser does not match the predicate pred
then the
filter succeeded, otherwise the parser fails with an empty error
Filter the value of a parser; if the value returned by the parser does not match the predicate pred
then the
filter succeeded, otherwise the parser fails with an empty error
- Value Params
- pred
The predicate that is tested against the parser result
- Returns
The result of the invokee if it fails the predicate
This is the traditional Monadic binding operator for parsers. When the invokee produces a value, the function
f
is used to produce a new parser that continued the computation.
This is the traditional Monadic binding operator for parsers. When the invokee produces a value, the function
f
is used to produce a new parser that continued the computation.
- Value Params
- f
A function that produces the next parser
- Returns
The parser produces from the application of
f
on the result of the last parser- Note
There is significant overhead for using flatMap; if possible try to write parsers in an applicative style otherwise try and use the intrinsic parsers provided to replace the flatMap.
This combinator is an alias for flatMap(identity)
.
This combinator is an alias for flatMap(identity)
.
A fold for a parser: p.foldLeft(k)(f)
will try executing p
many times until it fails, combining the
results with left-associative application of f
with a k
on the left-most position
A fold for a parser: p.foldLeft(k)(f)
will try executing p
many times until it fails, combining the
results with left-associative application of f
with a k
on the left-most position
- Value Params
- f
combining function
- k
base case for iteration
- Returns
the result of folding the results of
p
withf
andk
- Example
val natural: Parsley[Int] = digit.foldLeft(0)((x, d) => x * 10 + d.toInt)
A fold for a parser: p.foldLeft1(k)(f)
will try executing p
many times until it fails, combining the
results with left-associative application of f
with a k
on the left-most position. It must parse p
at least once.
A fold for a parser: p.foldLeft1(k)(f)
will try executing p
many times until it fails, combining the
results with left-associative application of f
with a k
on the left-most position. It must parse p
at least once.
- Value Params
- f
combining function
- k
base case for iteration
- Returns
the result of folding the results of
p
withf
andk
- Since
2.1.0
- Example
val natural: Parsley[Int] = digit.foldLeft1(0)((x, d) => x * 10 + d.toInt)
A fold for a parser: p.foldRight(k)(f)
will try executing p
many times until it fails, combining the
results with right-associative application of f
with a k
at the right-most position
A fold for a parser: p.foldRight(k)(f)
will try executing p
many times until it fails, combining the
results with right-associative application of f
with a k
at the right-most position
- Value Params
- f
combining function
- k
base case for iteration
- Returns
the result of folding the results of
p
withf
andk
- Example
p.foldRight(Nil)(_::_) == many(p) //many is more efficient, however
A fold for a parser: p.foldRight1(k)(f)
will try executing p
many times until it fails, combining the
results with right-associative application of f
with a k
at the right-most position. It must parse p
at least once.
A fold for a parser: p.foldRight1(k)(f)
will try executing p
many times until it fails, combining the
results with right-associative application of f
with a k
at the right-most position. It must parse p
at least once.
- Value Params
- f
combining function
- k
base case for iteration
- Returns
the result of folding the results of
p
withf
andk
- Since
2.1.0
- Example
p.foldRight1(Nil)(_::_) == some(p) //some is more efficient, however
This is the functorial map operation for parsers. When the invokee produces a value, this value is fed through
the function f
.
This is the functorial map operation for parsers. When the invokee produces a value, this value is fed through
the function f
.
- Value Params
- f
The mutator to apply to the result of previous parse
- Returns
A new parser which parses the same input as the invokee but mutated by function
f
- Note
This is subject to aggressive optimisations assuming purity; the compiler is permitted to optimise such that the application of
f
actually only happens once at compile time. In order to preserve the behaviour of impure functions, consider using theunsafe
method before map;p.unsafe.map(f)
.
A reduction for a parser: p.reduceLeft(op)
will try executing p
many times until it fails, combining the
results with left-associative application of op
. It must parse p
at least once.
A reduction for a parser: p.reduceLeft(op)
will try executing p
many times until it fails, combining the
results with left-associative application of op
. It must parse p
at least once.
- Value Params
- op
combining function
- Returns
the result of reducing the results of
p
withop
- Since
2.3.0
A reduction for a parser: p.reduceLeft(op)
will try executing p
many times until it fails, combining the
results with left-associative application of op
. If there is no p
, it returns None
, otherwise it returns
Some(x)
where x
is the result of the reduction.
A reduction for a parser: p.reduceLeft(op)
will try executing p
many times until it fails, combining the
results with left-associative application of op
. If there is no p
, it returns None
, otherwise it returns
Some(x)
where x
is the result of the reduction.
- Value Params
- op
combining function
- Returns
the result of reducing the results of
p
withop
wrapped inSome
orNone
otherwise- Since
2.3.0
A reduction for a parser: p.reduceRight(op)
will try executing p
many times until it fails, combining the
results with right-associative application of op
. It must parse p
at least once.
A reduction for a parser: p.reduceRight(op)
will try executing p
many times until it fails, combining the
results with right-associative application of op
. It must parse p
at least once.
- Value Params
- op
combining function
- Returns
the result of reducing the results of
p
withop
- Since
2.3.0
A reduction for a parser: p.reduceRight(op)
will try executing p
many times until it fails, combining the
results with right-associative application of op
. If there is no p
, it returns None
, otherwise it returns
Some(x)
where x
is the result of the reduction.
A reduction for a parser: p.reduceRight(op)
will try executing p
many times until it fails, combining the
results with right-associative application of op
. If there is no p
, it returns None
, otherwise it returns
Some(x)
where x
is the result of the reduction.
- Value Params
- op
combining function
- Returns
the result of reducing the results of
p
withop
wrapped inSome
orNone
otherwise- Since
2.3.0
This is the parser that corresponds to a more optimal version of (p <~> q).map(_._2)
. It performs
the parse action of both parsers, in order, but discards the result of the invokee.
This is the parser that corresponds to a more optimal version of (p <~> q).map(_._2)
. It performs
the parse action of both parsers, in order, but discards the result of the invokee.
- Value Params
- q
The parser whose result should be returned
- Returns
A new parser which first parses
p
, thenq
and returns the result ofq
- Since
2.4.0
Deprecated methods
This combinator is defined as attempt(p) <|> q
. It is pure syntactic sugar.
This combinator is defined as attempt(p) <|> q
. It is pure syntactic sugar.
- Note
This combinator should not be used: operators without trailing colons in Scala are left-associative, but this operator was designed to be right associative. This means that where we might intend for
p <|> q <\> r
to meanp <|> attempt(q) <|> r
, it in fact meansattempt(p <|> q) <|> r
. While this will not break a parser, it hinders optimisation and may damage the quality of generated messages.- Deprecated