LazyParsley

final implicit class LazyParsley[P, +A](p: => P)(implicit con: P => Parsley[A])

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 parameters:
con

A conversion (if required) to turn p into a parser

p

The parser which serves as the method receiver

Version:

3.0.0

class Object
trait Matchable
class Any

Value members

Concrete methods

def #>[B](x: B): Parsley[B]

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 receiver 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 receiver but discards its result and then results the value x instead

Value parameters:
x

The value to be returned after the execution of the receiver

Returns:

A new parser which first parses the receiver, then results x

def *>[A_ >: A, B](q: => Parsley[B]): Parsley[B]

This is the parser that corresponds to a more optimal version of lift2(_ => x => x, p, q). It performs the parse action of both parsers, in order, but discards the result of the receiver.

This is the parser that corresponds to a more optimal version of lift2(_ => x => x, p, q). It performs the parse action of both parsers, in order, but discards the result of the receiver.

Value parameters:
q

The parser whose result should be returned

Returns:

A new parser which first parses p, then q and returns the result of q

def <*[B](q: => Parsley[B]): Parsley[A]

This is the parser that corresponds to a more optimal version of lift2(x => _ => x, p, 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 lift2(x => _ => x, p, q). It performs the parse action of both parsers, in order, but discards the result of the second parser.

Value parameters:
q

The parser who should be executed but then discarded

Returns:

A new parser which first parses p, then q and returns the result of the p

def <**>[B](pf: => Parsley[A => B]): Parsley[B]

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.

def <*>[B, C](px: => Parsley[B])(implicit ev: P <:< Parsley[B => C]): Parsley[C]

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 parameters:
px

A parser of type A, where the receiver is A => B

Returns:

A new parser which parses pf, then px then applies the value returned by px to the function returned by pf

Note:

pure(f) <*> p is subject to the same aggressive optimisations as map. When using impure functions the optimiser may decide to cache the result of the function execution, be sure to use unsafe in order to prevent these optimisations.

def <+:>[B >: A](ps: => Parsley[Seq[B]]): Parsley[Seq[B]]

This parser corresponds to lift2(_+:_, p, ps).

This parser corresponds to lift2(_+:_, p, ps).

def <+>[B](q: Parsley[B]): Parsley[Either[A, B]]

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 parameters:
q

The parser to run if the receiver failed without consuming input

Returns:

the result of the parser which succeeded, if any

def </>[B >: A](x: B): Parsley[B]

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.

def <::>[B >: A](ps: => Parsley[List[B]]): Parsley[List[B]]

This parser corresponds to lift2(_::_, p, ps).

This parser corresponds to lift2(_::_, p, ps).

def <|>[B >: A](q: => Parsley[B]): Parsley[B]

This is the traditional choice operator for parsers. Following the parsec semantics precisely, this combinator first tries to parse the receiver. If this is successful, no further action is taken. If the receiver failed without consuming input, then q is parsed instead. If the receiver 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 attempt(this) <|> q to parse q regardless of how the receiver failed.

This is the traditional choice operator for parsers. Following the parsec semantics precisely, this combinator first tries to parse the receiver. If this is successful, no further action is taken. If the receiver failed without consuming input, then q is parsed instead. If the receiver 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 attempt(this) <|> q to parse q regardless of how the receiver failed.

Value parameters:
q

The parser to run if the receiver failed without consuming input

Returns:

The value produced by the receiver if it was successful, or if it failed without consuming input, the possible result of parsing q.

def <~[B](q: => Parsley[B]): Parsley[A]

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 parameters:
q

The parser who should be executed but then discarded

Returns:

A new parser which first parses p, then q and returns the result of the p

Since:

2.4.0

def <~>[A_ >: A, B](q: => Parsley[B]): Parsley[(A_, B)]

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

def >>=[B](f: A => Parsley[B]): Parsley[B]

This combinator is an alias for flatMap

This combinator is an alias for flatMap

def cast[B : ClassTag]: Parsley[B]

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 parameters:
B

The type to attempt to cast into

Since:

2.0.0

def collect[B](pf: PartialFunction[A, B]): Parsley[B]

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 parameters:
pf

The partial function

Returns:

The result of applying pf to this parsers value (if possible), or fails

Since:

2.0.0

def filter(pred: A => Boolean): Parsley[A]

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 parameters:
pred

The predicate that is tested against the parser result

Returns:

The result of the receiver if it passes the predicate

def filterNot(pred: A => Boolean): Parsley[A]

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 parameters:
pred

The predicate that is tested against the parser result

Returns:

The result of the receiver if it fails the predicate

def flatMap[B](f: A => Parsley[B]): Parsley[B]

This is the traditional Monadic binding operator for parsers. When the receiver produces a value, the function f is used to produce a new parser that continues the computation - this new parser is then executed.

This is the traditional Monadic binding operator for parsers. When the receiver produces a value, the function f is used to produce a new parser that continues the computation - this new parser is then executed.

Value parameters:
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.

def flatten[B](implicit ev: A <:< Parsley[B]): Parsley[B]

This combinator is an alias for flatMap(identity).

This combinator is an alias for flatMap(identity).

def foldLeft[B](k: B)(f: (B, A) => B): Parsley[B]

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 parameters:
f

combining function

k

base case for iteration

Returns:

the result of folding the results of p with f and k

Example:
val natural: Parsley[Int] = digit.foldLeft(0)((x, d) => x * 10 + d.toInt)
def foldLeft1[B](k: B)(f: (B, A) => B): Parsley[B]

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 parameters:
f

combining function

k

base case for iteration

Returns:

the result of folding the results of p with f and k

Since:

2.1.0

Example:
val natural: Parsley[Int] = digit.foldLeft1(0)((x, d) => x * 10 + d.toInt)
def foldRight[B](k: B)(f: (A, B) => B): Parsley[B]

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 parameters:
f

combining function

k

base case for iteration

Returns:

the result of folding the results of p with f and k

Example:
p.foldRight(Nil)(_::_) == many(p) //many is more efficient, however
def foldRight1[B](k: B)(f: (A, B) => B): Parsley[B]

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 parameters:
f

combining function

k

base case for iteration

Returns:

the result of folding the results of p with f and k

Since:

2.1.0

Example:
p.foldRight1(Nil)(_::_) == some(p) //some is more efficient, however
def getOrElse[B >: A](x: B): Parsley[B]

This combinator is an alias for </>.

This combinator is an alias for </>.

def map[B](f: A => B): Parsley[B]

This is the functorial map operation for parsers. When the receiver produces a value, this value is fed through the function f.

This is the functorial map operation for parsers. When the receiver produces a value, this value is fed through the function f.

Value parameters:
f

The mutator to apply to the result of previous parse

Returns:

A new parser which parses the same input as the receiver 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 the unsafe method before map; p.unsafe.map(f).

def orElse[B >: A](q: => Parsley[B]): Parsley[B]

This combinator is an alias for <|>.

This combinator is an alias for <|>.

def reduceLeft[B >: A](op: (B, A) => B): Parsley[B]

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 parameters:
op

combining function

Returns:

the result of reducing the results of p with op

Since:

2.3.0

def reduceLeftOption[B >: A](op: (B, A) => B): Parsley[Option[B]]

A reduction for a parser: p.reduceLeftOption(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.reduceLeftOption(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 parameters:
op

combining function

Returns:

the result of reducing the results of p with op wrapped in Some or None otherwise

Since:

2.3.0

def reduceRight[B >: A](op: (A, B) => B): Parsley[B]

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 parameters:
op

combining function

Returns:

the result of reducing the results of p with op

Since:

2.3.0

def reduceRightOption[B >: A](op: (A, B) => B): Parsley[Option[B]]

A reduction for a parser: p.reduceRightOption(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.reduceRightOption(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 parameters:
op

combining function

Returns:

the result of reducing the results of p with op wrapped in Some or None otherwise

Since:

2.3.0

def zip[A_ >: A, B](q: => Parsley[B]): Parsley[(A_, B)]

This combinator is an alias for <~>

This combinator is an alias for <~>

Since:

2.3.0

def ~>[B](q: => Parsley[B]): Parsley[B]

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 receiver.

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 receiver.

Value parameters:
q

The parser whose result should be returned

Returns:

A new parser which first parses p, then q and returns the result of q

Since:

2.4.0

Deprecated methods

@deprecated("This combinator is unfortunately misleading since it is left-associative. It will be removed in 4.0.0, use `attempt` with `<|>` instead", "3.0.1")
def <\>[B >: A](q: Parsley[B]): Parsley[B]

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 mean p <|> attempt(q) <|> r, it in fact means attempt(p <|> q) <|> r. While this will not break a parser, it hinders optimisation and may damage the quality of generated messages.

Deprecated