implicit final class LazyParsley[P, +A] extends AnyRef
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
- Version
1.0.0
- Alphabetic
- By Inheritance
- LazyParsley
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def #>[B](x: B): Parsley[B]
This is the parser that corresponds to
p *> pure(x)
or a more optimal version ofp.map(_ => x)
.This is the parser that corresponds to
p *> pure(x)
or a more optimal version ofp.map(_ => x)
. It performs the parse action of the invokee but discards its result and then results the valuex
instead- x
The value to be returned after the execution of the invokee
- returns
A new parser which first parses the invokee, then results
x
- def *>[A_ >: A, B](q: => Parsley[B]): Parsley[B]
This is the parser that corresponds to a more optimal version of
p.map(_ => x => x) <*> q
.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.- q
The parser whose result should be returned
- returns
A new parser which first parses
p
, thenq
and returns the result ofq
- def <*[B](q: => Parsley[B]): Parsley[A]
This is the parser that corresponds to a more optimal version of
p.map(x => _ => x) <*> q
.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.- 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
- def <**>[B](pf: => Parsley[(A) => B]): Parsley[B]
This combinator is defined as
lift2((x, f) => f(x), p, f)
.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.
This is the Applicative application parser. The type of
pf
isParsley[A => B]
. Then, given aParsley[A]
, we can produce aParsley[B]
by parsingpf
to retrievef: A => B
, then parsepx
to receivex: A
then returnf(x): B
.- 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.
- def <+:>[B >: A](ps: => Parsley[Seq[B]]): Parsley[Seq[B]]
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 anEither
.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 anEither
.- q
The parser to run if the invokee 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)
.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)
. - def <|>[B >: A](q: => Parsley[B]): Parsley[B]
This is the traditional Alternative choice operator for parsers.
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 (orattempt(this) <|> q
) to parseq
regardless of how the invokee failed.- 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.
- def <~[B](q: Parsley[B]): Parsley[A]
This is the parser that corresponds to a more optimal version of
(p <~> q).map(_._1)
.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.- 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
- def <~>[A_ >: A, B](q: => Parsley[B]): Parsley[(A_, B)]
This parser corresponds to
lift2((_, _), p, q)
.This parser corresponds to
lift2((_, _), p, q)
. For now it is sugar, but in future may be more optimal - final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def >>=[B](f: (A) => Parsley[B]): Parsley[B]
This combinator is an alias for
flatMap
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def cast[B](implicit arg0: ClassTag[B]): Parsley[B]
This casts the result of the parser into a new type
B
.This casts the result of the parser into a new type
B
. If the value returned by the parser is castable to typeB
, then this cast is performed. Otherwise the parser fails.- B
The type to attempt to cast into
- Since
2.0.0
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def collect[B](pf: PartialFunction[A, B]): Parsley[B]
Attempts to first filter the parser to ensure that
pf
is defined over it.Attempts to first filter the parser to ensure that
pf
is defined over it. If it is, then the functionpf
is mapped over its result. Roughly the same as afilter
then amap
.- pf
The partial function
- returns
The result of applying
pf
to this parsers value (if possible), or fails
- Since
2.0.0
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- 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 errorFilter 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- pred
The predicate that is tested against the parser result
- returns
The result of the invokee 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 errorFilter 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- pred
The predicate that is tested against the parser result
- returns
The result of the invokee if it fails the predicate
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def flatMap[B](f: (A) => Parsley[B]): Parsley[B]
This is the traditional Monadic binding operator for parsers.
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.- 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)
. - def foldLeft[B](k: B)(f: (B, A) => B): Parsley[B]
A fold for a parser:
p.foldLeft(k)(f)
will try executingp
many times until it fails, combining the results with left-associative application off
with ak
on the left-most positionA fold for a parser:
p.foldLeft(k)(f)
will try executingp
many times until it fails, combining the results with left-associative application off
with ak
on the left-most position- k
base case for iteration
- f
combining function
- returns
the result of folding the results of
p
withf
andk
val natural: Parsley[Int] = digit.foldLeft(0)((x, d) => x * 10 + d.toInt)
Example: - def foldLeft1[B](k: B)(f: (B, A) => B): Parsley[B]
A fold for a parser:
p.foldLeft1(k)(f)
will try executingp
many times until it fails, combining the results with left-associative application off
with ak
on the left-most position.A fold for a parser:
p.foldLeft1(k)(f)
will try executingp
many times until it fails, combining the results with left-associative application off
with ak
on the left-most position. It must parsep
at least once.- k
base case for iteration
- f
combining function
- returns
the result of folding the results of
p
withf
andk
val natural: Parsley[Int] = digit.foldLeft1(0)((x, d) => x * 10 + d.toInt)
- Since
2.1.0
Example: - def foldRight[B](k: B)(f: (A, B) => B): Parsley[B]
A fold for a parser:
p.foldRight(k)(f)
will try executingp
many times until it fails, combining the results with right-associative application off
with ak
at the right-most positionA fold for a parser:
p.foldRight(k)(f)
will try executingp
many times until it fails, combining the results with right-associative application off
with ak
at the right-most position- k
base case for iteration
- f
combining function
- returns
the result of folding the results of
p
withf
andk
p.foldRight(Nil)(_::_) == many(p) //many is more efficient, however
Example: - def foldRight1[B](k: B)(f: (A, B) => B): Parsley[B]
A fold for a parser:
p.foldRight1(k)(f)
will try executingp
many times until it fails, combining the results with right-associative application off
with ak
at the right-most position.A fold for a parser:
p.foldRight1(k)(f)
will try executingp
many times until it fails, combining the results with right-associative application off
with ak
at the right-most position. It must parsep
at least once.- k
base case for iteration
- f
combining function
- returns
the result of folding the results of
p
withf
andk
p.foldRight1(Nil)(_::_) == some(p) //some is more efficient, however
- Since
2.1.0
Example: - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def getOrElse[B >: A](x: B): Parsley[B]
This combinator is an alias for
</>
. - def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def map[B](f: (A) => B): Parsley[B]
This is the functorial map operation for parsers.
This is the functorial map operation for parsers. When the invokee produces a value, this value is fed through the function
f
.- 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)
.
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def orElse[B >: A](q: => Parsley[B]): Parsley[B]
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 executingp
many times until it fails, combining the results with left-associative application ofop
.A reduction for a parser:
p.reduceLeft(op)
will try executingp
many times until it fails, combining the results with left-associative application ofop
. It must parsep
at least once.- op
combining function
- returns
the result of reducing the results of
p
withop
- Since
2.3.0
- def reduceLeftOption[B >: A](op: (B, A) => B): Parsley[Option[B]]
A reduction for a parser:
p.reduceLeft(op)
will try executingp
many times until it fails, combining the results with left-associative application ofop
.A reduction for a parser:
p.reduceLeft(op)
will try executingp
many times until it fails, combining the results with left-associative application ofop
. If there is nop
, it returnsNone
, otherwise it returnsSome(x)
wherex
is the result of the reduction.- op
combining function
- returns
the result of reducing the results of
p
withop
wrapped inSome
orNone
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 executingp
many times until it fails, combining the results with right-associative application ofop
.A reduction for a parser:
p.reduceRight(op)
will try executingp
many times until it fails, combining the results with right-associative application ofop
. It must parsep
at least once.- op
combining function
- returns
the result of reducing the results of
p
withop
- Since
2.3.0
- def reduceRightOption[B >: A](op: (A, B) => B): Parsley[Option[B]]
A reduction for a parser:
p.reduceRight(op)
will try executingp
many times until it fails, combining the results with right-associative application ofop
.A reduction for a parser:
p.reduceRight(op)
will try executingp
many times until it fails, combining the results with right-associative application ofop
. If there is nop
, it returnsNone
, otherwise it returnsSome(x)
wherex
is the result of the reduction.- op
combining function
- returns
the result of reducing the results of
p
withop
wrapped inSome
orNone
otherwise
- Since
2.3.0
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- 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)
.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.- 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 Value Members
- def <\>[B >: A](q: Parsley[B]): Parsley[B]
This combinator is defined as
attempt(p) <|> q
.This combinator is defined as
attempt(p) <|> q
. It is pure syntactic sugar.- Annotations
- @deprecated
- Deprecated
(Since version 3.0.1) This combinator is unfortunately misleading since it is left-associative. It will be removed in 4.0.0, use
attempt
with<|>
instead- 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.
This is the documentation for Parsley.
Package structure
The parsley package contains the
Parsley
class, as well as theResult
,Success
, andFailure
types. In addition to these, it also contains the following packages and "modules" (a module is defined as being an object which mocks a package):parsley.Parsley
contains the bulk of the core "function-style" combinators, as well as the implicit classes which enable the "method-style" combinators.parsley.combinator
contains many helpful combinators that simplify some common parser patterns.parsley.character
contains the combinators needed to read characters and strings, as well as combinators to match specific sub-sets of characters.parsley.debug
contains debugging combinators, helpful for identifying faults in parsers.parsley.io
contains extension methods to run parsers with input sourced from IO sources.parsley.expr
contains the following sub modules:parsley.expr.chain
contains combinators used in expression parsingparsley.expr.precedence
is a builder for expression parsers built on a precedence table.parsley.implicits
contains several implicits to add syntactic sugar to the combinators. These are sub-categorised into the following sub modules:parsley.implicits.character
contains implicits to allow you to use character and string literals as parsers.parsley.implicits.combinator
contains implicits related to combinators, such as the ability to make any parser into aParsley[Unit]
automatically.parsley.implicits.lift
enables postfix application of the lift combinator onto a function (or value).parsley.implicits.zipped
enables boths a reversed form of lift where the function appears on the right and is applied on a tuple (useful when type inference has failed) as well as a.zipped
method for building tuples out of several combinators.parsley.errors
contains modules to deal with error messages, their refinement and generation.parsley.errors.combinator
provides combinators that can be used to either produce more detailed errors as well as refine existing errors.parsley.lift
contains functions which lift functions that work on regular types to those which now combine the results of parsers returning those same types. these are ubiquitous.parsley.registers
contains combinators that interact with the context-sensitive functionality in the form of registers.parsley.token
contains theLexer
class that provides a host of helpful lexing combinators when provided with the description of a language.parsley.unsafe
contains unsafe (and not thread-safe) ways of speeding up the execution of a parser.