Packages

  • package root

    This is the documentation for Parsley.

    This is the documentation for Parsley.

    Package structure

    The parsley package contains the Parsley class, as well as the Result, Success, and Failure 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.
    • 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.extension contains syntactic sugar combinators exposed as implicit classes.
    • 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 parsing
      • parsley.expr.precedence is a builder for expression parsers built on a precedence table.
      • parsley.expr.infix contains combinators used in expression parsing, but with more permissive types than their equivalents in chain.
      • parsley.expr.mixed contains combinators that can be used for expression parsing, but where different fixities may be mixed on the same level: this is rare in practice.
    • 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 a Parsley[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.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.ap contains functions which allow for the application of a parser returning a function to several parsers returning each of the argument types.
    • parsley.registers contains combinators that interact with the context-sensitive functionality in the form of registers.
    • parsley.token contains the Lexer class that provides a host of helpful lexing combinators when provided with the description of a language.
    • parsley.position contains parsers for extracting position information.
    • parsley.genericbridges contains some basic implementations of the Parser Bridge pattern (see Design Patterns for Parser Combinators in Scala, or the parsley wiki): these can be used before more specialised generic bridge traits can be constructed.
    Definition Classes
    root
  • package parsley
    Definition Classes
    root
  • package errors

    This package contains various functionality relating to the generation and formatting of error messages.

    This package contains various functionality relating to the generation and formatting of error messages.

    In particular, it includes a collection of combinators for improving error messages within the parser, including labelling and providing additional information. It also contains combinators that can be used to valid data produced by a parser, to ensure it conforms to expected invariances, producing good quality error messages if this is not the case. Finally, this package contains ways of changing the formatting of error messages: this can either be changing how the default String-based errors are formatted, or by injectiing Parsley's errors into a custom error object.

    Definition Classes
    parsley
  • package expr

    This package contains various functionality relating to the parsing of expressions..

    This package contains various functionality relating to the parsing of expressions..

    This includes the "chain" combinators, which tackle the left-recursion problem and allow for the parsing and combining of operators with values. It also includes functionality for constructing larger precedence tables, which may even vary the type of each layer in the table, allowing for strongly-typed expression parsing.

    Definition Classes
    parsley
  • package implicits

    This package contains various functionality that involve Scala's implicits mechanism.

    This package contains various functionality that involve Scala's implicits mechanism.

    This includes conversions from scala literals into parsers, as well as enabling new syntax on regular Scala values (such as Parsley's lift or zipped syntax). Automatic conversion to Parsley[Unit] is also supported within this package.

    Definition Classes
    parsley
  • package token

    This package provides a wealth of functionality for performing common lexing tasks.

    This package provides a wealth of functionality for performing common lexing tasks.

    It is organised as follows:

    • the main parsing functionality is accessed via Lexer, which provides implementations for the combinators found in the sub-packages given a LexicalDesc.
    • the descriptions sub-package is how a lexical structure can be described, providing the configuration that alters the behaviour of the parsers produced by the Lexer.
    • the other sub-packages contain the high-level interfaces that the Lexer exposes, which can be used to pass whitespace-aware and non-whitespace-aware combinators around in a uniform way.
    • the predicate module contains functionality to help define boolean predicates on characters or unicode codepoints.
    Definition Classes
    parsley
  • Failure
  • Parsley
  • Result
  • Success
  • ap
  • character
  • combinator
  • debug
  • extension
  • genericbridges
  • io
  • lift
  • position
  • registers

final class Parsley[+A] extends AnyVal

This is the class that encapsulates the act of parsing and running an object of this class with parse will parse the string given as input to parse.

Source
Parsley.scala
Version

4.0.0

Note

In order to construct an object of this class you must use the combinators; the class itself is opaque.

Linear Supertypes
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. Parsley
  2. AnyVal
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    Any
  2. final def ##: Int
    Definition Classes
    Any
  3. def #>[B](x: B): Parsley[B]

    This combinator, pronounced "as", replaces the result of this parser, ignoring the old result.

    This combinator, pronounced "as", replaces the result of this parser, ignoring the old result.

    Similar to map, except the old result of this parser is not required to compute the new result. This is useful when the result is a constant value (or function!). Functionally the same as this *> pure(x) or this.map(_ => x).

    In Haskell, this combinator is known as ($>).

    x

    the new result of this parser.

    returns

    a new parser that behaves the same as this parser, but always succeeds with x as the result.

    Example:
    1. scala> import parsley.character.string
      scala> (string("true") #> true).parse("true")
      val res0 = Success(true)
  4. def *>[B](q: => Parsley[B]): Parsley[B]

    This combinator, pronounced "then", first parses this parser then parses q: if both succeed then the result of q is returned.

    This combinator, pronounced "then", first parses this parser then parses q: if both succeed then the result of q is returned.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then y is returned and x is ignored. If either fail then the entire combinator fails.

    Identical to ~>: *> is more common in Haskell, whereas ~> is more common in Scala.

    q

    the parser to run second, which returns the result of this combinator.

    returns

    a parser that sequences this parser with q and returns q's result.

    Example:
    1. scala> import parsley.character.char
      scala> ('a' *> 'b').parse("ab")
      val res0 = Success('b')
  5. def <*[B](q: => Parsley[B]): Parsley[A]

    This combinator, pronounced "then discard", first parses this parser then parses q: if both succeed then the result of this parser is returned.

    This combinator, pronounced "then discard", first parses this parser then parses q: if both succeed then the result of this parser is returned.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then x is returned and y is ignored. If either fail then the entire combinator fails.

    Identical to <~: <* is more common in Haskell, whereas <~ is more common in Scala.

    q

    the parser to run second, which returns the result of this combinator.

    returns

    a parser that sequences this parser with q and returns this parser's result.

    Example:
    1. scala> import parsley.character.char
      scala> ('a' <* 'b').parse("ab")
      val res0 = Success('a')
  6. def <**>[B](pf: => Parsley[(A) => B]): Parsley[B]

    This combinator, pronounced "reverse ap", first parses this parser then parses pf: if both succeed then the value returned by this parser is applied to the function returned by pf.

    This combinator, pronounced "reverse ap", first parses this parser then parses pf: if both succeed then the value returned by this parser is applied to the function returned by pf.

    First, this parser is ran, yielding a value x on success, then pf is ran, yielding a function f on success. If both are successful, then f(x) is returned. If either fail then the entire combinator fails.

    Compared with <*>, this combinator is useful for left-factoring: when two branches of a parser share a common prefix, this can often be factored out; but the result of that factored prefix may be required to help generate the results of each branch. In this case, the branches can return functions that, when given the factored result, can produce the original results from before the factoring.

    pf

    the parser to run second, which returns a function this parser's result can be applied to.

    returns

    a parser that sequences this parser with pf and combines their results with function application.

    Example:
    1.  // this has a common prefix "term" and requires backtracking
      val expr1 = attempt(lift2(Add, term <* char('+'), expr2)) <|> term
      // common prefix factored out, and branches return a function to recombine
      val expr2 = term <**> (char('+') *> expr2.map(y => Add(_, y)) </> (identity[Expr] _))
    Note

    equivalent to

    lift2((x, f) => f(x), this, pf)
  7. def <*>[B, C](px: => Parsley[B])(implicit ev: <:<[A, (B) => C]): Parsley[C]

    This combinator, pronounced "ap", first parses this parser then parses px: if both succeed then the function returned by this parser is applied to the value returned by px.

    This combinator, pronounced "ap", first parses this parser then parses px: if both succeed then the function returned by this parser is applied to the value returned by px.

    The implicit (compiler-provided) evidence proves that this parser really has type Parsley[B => C]. First, this parser is ran, yielding a function f on success, then px is ran, yielding a value x on success. If both are successful, then f(x) is returned. If either fail then the entire combinator fails.

    px

    the parser to run second, which returns a value applicable to this parser's result.

    ev

    witnesses that the type of this parser, A, is actually B => C.

    returns

    a parser that sequences this parser with px and combines their results with function application.

    Example:
    1. scala> import parsley.Parsley, parsley.character.char
      scala> val sign: Parsley[Int => Int] = char('+') #> (identity[Int] _) <|> char('-') #> (x => -x)
      scala> val nat: Parsley[Int] = ..
      scala> val int = sign <*> nat
      scala> int.parse("-7")
      val res0 = Success(-7)
      scala> int.parse("+42")
      val res1 = Success(42)
      scala> int.parse("2")
      val res2 = Failure(..) // `sign` failed: no + or -
      scala> int.parse("-a")
      val res3 = Failure(..) // `nat` failed
    Note

    equivalent to lift2((f, x) => f(x), this, px).

  8. def <+:>[Aʹ >: A](ps: => Parsley[Seq[]]): Parsley[Seq[]]

    This combinator, pronounced "prepend", first parses this parser then parses ps: if both succeed the result of this parser is prepended onto the result of ps.

    This combinator, pronounced "prepend", first parses this parser then parses ps: if both succeed the result of this parser is prepended onto the result of ps.

    First, this parser is ran, yielding x on success, then ps is ran, yielding xs on success. If both are successful then x +: xs is returned. If either fail then the entire combinator fails.

    the type of the elements in the result sequence, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    ps

    the parser to run second, which returns a sequence.

    returns

    a parser that sequences this parser with ps and prepends its result onto ps result.

    Example:
    1. def some[A](p: Parsley[A]): Parsley[List[A]] = {
          p <+:> many(p) // since List[A] <: Seq[A]
      }
    Note

    equivalent to

    lift2(_ +: _, this, ps)
  9. def <+>[B](q: Parsley[B]): Parsley[Either[A, B]]

    This combinator, pronounced "sum", wraps this parser's result in Left if it succeeds, and parses q if it failed without consuming input, wrapping the result in Right.

    This combinator, pronounced "sum", wraps this parser's result in Left if it succeeds, and parses q if it failed without consuming input, wrapping the result in Right.

    If this parser is successful, then its result is wrapped up using Left(_) and no further action is taken. Otherwise, if this parser fails without consuming input, then q is parsed instead and its result is wrapped up using Right(_). If this parser fails having consumed input, this combinator fails. This is functionally equivalent to this.map(Left(_)) <|> q.map(Right(_)).

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    q

    the parser to run if this parser fails having not consumed input.

    returns

    a parser which either parses this parser or parses q projecting their results into an Either[A, B].

    Example:
    1. scala> import parsley.character.char
      scala> val p = string("abc") <+> char("xyz")
      scala> p.parse("abc")
      val res0 = Success(Left("abc"))
      scala> p.parse("xyz")
      val res1 = Success(Right("xyz"))
      scala> p.parse("ab")
      val res2 = Failure(..) // first parser consumed an 'a'!
  10. def </>[Aʹ >: A](x: ): Parsley[]

    This combinator, pronounced "or constant", returns x if this parser fails without consuming input.

    This combinator, pronounced "or constant", returns x if this parser fails without consuming input.

    If this parser is successful, then this combinator is successful and no further action is taken. Otherwise, if this parser fails without consuming input, then x is unconditionally returned. If this parser fails having consumed input, this combinator fails. Functionally the same as this <|> pure(x).

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    the type of x, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    x

    the value to return if this parser fails having not consumed input.

    returns

    a parser which either parses this parser or returns x.

    Example:
    1. scala> import parsley.character.string
      scala> val p = string("aa") </> "b"
      scala> p.parse("aa")
      val res0 = Success("a")
      scala> p.parse("xyz")
      val res1 = Success("b")
      scala> p.parse("ab")
      val res2 = Failure(..) // first parser consumed an 'a'!
  11. def <::>[Aʹ >: A](ps: => Parsley[List[]]): Parsley[List[]]

    This combinator, pronounced "cons", first parses this parser then parses ps: if both succeed the result of this parser is prepended onto the result of ps.

    This combinator, pronounced "cons", first parses this parser then parses ps: if both succeed the result of this parser is prepended onto the result of ps.

    First, this parser is ran, yielding x on success, then ps is ran, yielding xs on success. If both are successful then x :: xs is returned. If either fail then the entire combinator fails.

    the type of the elements in the result list, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    ps

    the parser to run second, which returns a list.

    returns

    a parser that sequences this parser with ps and prepends its result onto ps result.

    Example:
    1. def some[A](p: Parsley[A]): Parsley[List[A]] = {
          p <::> many(p)
      }
    Note

    equivalent to

    lift2(_ :: _, this, ps)
  12. def <|>[Aʹ >: A](q: Parsley[]): Parsley[]

    This combinator, pronounced "or", parses q if this parser fails without consuming input.

    This combinator, pronounced "or", parses q if this parser fails without consuming input.

    If this parser is successful, then this combinator is successful and no further action is taken. Otherwise, if this parser fails without consuming input, then q is parsed instead. If this parser fails having consumed input, this combinator fails.

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    the type returned by q, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    q

    the parser to run if this parser fails having not consumed input.

    returns

    a parser which either parses this parser or parses q.

    Example:
    1. scala> import parsley.character.string
      scala> val p = string("a") <|> string("b")
      scala> p.parse("a")
      val res0 = Success("a")
      scala> p.parse("b")
      val res1 = Success("b")
      scala> val q = string("ab") <|> string("ac")
      scala> q.parse("ac")
      val res2 = Failure(..) // first parser consumed an 'a'!
  13. def <~[B](q: => Parsley[B]): Parsley[A]

    This combinator, pronounced "then discard", first parses this parser then parses q: if both succeed then the result of this parser is returned.

    This combinator, pronounced "then discard", first parses this parser then parses q: if both succeed then the result of this parser is returned.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then x is returned and y is ignored. If either fail then the entire combinator fails.

    Identical to <*: <* is more common in Haskell, whereas <~ is more common in Scala.

    q

    the parser to run second, which returns the result of this combinator.

    returns

    a parser that sequences this parser with q and returns this parser's result.

    Example:
    1. scala> import parsley.character.char
      scala> ('a' <~ 'b').parse("ab")
      val res0 = Success('a')
    Since

    2.4.0

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

    This combinator, pronounced "zip", first parses this parser then parses q: if both succeed the result of this parser is paired with the result of q.

    This combinator, pronounced "zip", first parses this parser then parses q: if both succeed the result of this parser is paired with the result of q.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then (x, y) is returned. If either fail then the entire combinator fails.

    q

    the parser to run second.

    returns

    a parser that sequences this parser with q and pairs their results together.

    Example:
    1. scala> import parsley.character.char
      scala> val p = char('a') <~> char('b')
      scala> p.parse("ab")
      val res0 = Success(('a', 'b'))
      scala> p.parse("b")
      val res1 = Failure(..)
      scala> p.parse("a")
      val res2 = Failure(..)
    Note

    equivalent to

    lift2((_, _), this, q)
  15. final def ==(arg0: Any): Boolean
    Definition Classes
    Any
  16. def >>=[B](f: (A) => Parsley[B]): Parsley[B]

    This combinator, pronounced "bind", first performs this parser, then uses its result to generate and execute a new parser.

    This combinator, pronounced "bind", first performs this parser, then uses its result to generate and execute a new parser.

    First, this combinator runs this parser. If it succeeded, the result x is given to the function f to produce a new parser f(x). This new parser is then executed and its result returned. If either of this parser or the new parser fails, then the entire combinator fails.

    This is a very powerful combinator (Turing-powerful, in fact): it is only necessary (and not all the time) to use this for context-sensitive parsing. The idea is that it can make any form of decision based on the result of a parser, allowing it to perform a different parser for each possible output of another without exhaustively enumerating them all.

    f

    the function that produces the next parser.

    returns

    a new parser, which sequences this parser with the parser generated from its result.

    Example:
    1. // this is an inefficient implementation, but does work
      def filter(pred: A => Boolean): Parsley[A] = {
          this >>= { x =>
              if (pred(x)) pure(x)
              else empty
          }
      }
    Note

    there is significant overhead for using >>=: if possible try to avoid using it! This is because Parsley will need to generate, process, and compile each parser produced by the combinator during parse-time.

  17. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  18. def collect[B](pf: PartialFunction[A, B]): Parsley[B]

    This combinator applies a partial function pf to the result of this parser if its result is defined for pf, failing if it is not.

    This combinator applies a partial function pf to the result of this parser if its result is defined for pf, failing if it is not.

    First, parse this parser. If it succeeds, test whether its result x is in the domain of the partial function pf. If it is defined for pf, return pf(x). If it is undefined, or this parser failed, then this combinator fails. Equivalent to a filter followed by a map.

    pf

    the partial function used to both filter the result of this parser and transform it.

    returns

    a parser that returns the result of this parser applied to pf, if possible.

    Example:
    1. scala> val int: Parsley[Int] = ..
      scala> val safeDiv: Parsley[Int] = (int <~> char(' ') *> int).collect {
        case (x, y) if y != 0 => x / y
      }
      scala> safeDiv.parse("7 0")
      val res0 = Failure(..) // y cannot be 0!
      scala> safeDiv.parse("10 2")
      val res1 = Success(5)
    Since

    2.0.0

    Note

    when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if amend had been used) and the caret will span the entire successful parse of this parser.

    See also

    collectMsg(String, String*) and collectMsg(A => Seq[String]) for versions that can produce custom error messages on failure.

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

    This combinator filters the result of this parser using a given predicate, succeeding only if the predicate returns true.

    This combinator filters the result of this parser using a given predicate, succeeding only if the predicate returns true.

    First, parse this parser. If it succeeds then take its result x and apply it to the predicate pred. If pred(x) is true, then return x. Otherwise, the combinator fails.

    pred

    the predicate that is tested against the parser result.

    returns

    a parser that returns the result of this parser if it passes the predicate.

    Example:
    1. scala> import parsley.character.letter
      scala> val keywords = Set("if", "then", "else")
      scala> val identifier = some(letter).map(_.mkString)
                                          .filter(!keywords.contains(_))
      scala> identifier.parse("hello")
      val res0 = Success("hello")
      scala> idenfitier.parse("if")
      val res1 = Failure(..)
    Note

    when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if amend had been used) and the caret will span the entire successful parse of this parser.

    See also

    filterOut for a version which can produce custom reasons on failure.

    guardAgainst for a version which can produce custom error messages on failure.

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

    This combinator filters the result of this parser using a given predicate, succeeding only if the predicate returns false.

    This combinator filters the result of this parser using a given predicate, succeeding only if the predicate returns false.

    First, parse this parser. If it succeeds then take its result x and apply it to the predicate pred. If pred(x) is false, then return x. Otherwise, the combinator fails.

    pred

    the predicate that is tested against the parser result.

    returns

    a parser that returns the result of this parser if it fails the predicate.

    Example:
    1. scala> import parsley.character.letter
      scala> val keywords = Set("if", "then", "else")
      scala> val identifier = some(letter).map(_.mkString)
                                          .filterNot(keywords.contains(_))
      scala> identifier.parse("hello")
      val res0 = Success("hello")
      scala> idenfitier.parse("if")
      val res1 = Failure(..)
    Note

    when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if amend had been used) and the caret will span the entire successful parse of this parser.

    See also

    filterOut for a version that can produce custom reasons on failure.

    guardAgainst for a version that can produce custom error messages on failure.

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

    This combinator first performs this parser, then uses its result to generate and execute a new parser.

    This combinator first performs this parser, then uses its result to generate and execute a new parser.

    First, this combinator runs this parser. If it succeeded, the result x is given to the function f to produce a new parser f(x). This new parser is then executed and its result returned. If either of this parser or the new parser fails, then the entire combinator fails.

    This is a very powerful combinator (Turing-powerful, in fact): it is only necessary (and not all the time) to use this for context-sensitive parsing. The idea is that it can make any form of decision based on the result of a parser, allowing it to perform a different parser for each possible output of another without exhaustively enumerating them all.

    In Haskell, this combinator is known as (>>=) (pronounced "bind").

    f

    the function that produces the next parser.

    returns

    a new parser, which sequences this parser with the parser generated from its result.

    Example:
    1. // this is an inefficient implementation, but does work
      def filter(pred: A => Boolean): Parsley[A] = {
          this.flatMap { x =>
              if (pred(x)) pure(x)
              else empty
          }
      }
    Note

    there is significant overhead for using flatMap: if possible try to avoid using it! This is because Parsley will need to generate, process, and compile each parser produced by the combinator during parse-time.

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

    This combinator collapses two layers of parsing structure into one.

    This combinator collapses two layers of parsing structure into one.

    The implicit (compiler-provided) evidence proves that this parser really has type Parsley[Parsley[B]]. First, this parser is executed, which, if successful, will produce a new parser p. The parser p is then executed, and its result is returned. If either this parser or p fail, then the entire combinator fails.

    This is a very powerful combinator (Turing-powerful, in fact): it is only necessary (and not all the time) to use this for context-sensitive parsing. The idea is that it can allow a parser to return any other parser as its result: this allows for an implementation of a later parser to be picked by an earlier one.

    In Haskell, this combinator is known as join.

    ev

    witnesses that the result type of this parser, A, is really Parsley[B].

    returns

    a new parser, which sequences this parser with its result parser.

    Example:
    1. imagine a parser for RegEx that first reads the description of the regular expression, then runs that against the remaining input string. It is possible to implement the parser for the regular expression itself as a Parsley[Parsley[Unit]], which returns a parser that matches the regular expression. This can then be used to parse the remaining input by using flatten to incorporate it into the parser again:

      scala> val regexDesc: Parsley[Parsley[Unit]] = ..
      // let's suppose "#" is the delimeter between expression and input
      scala> val regex: Parsley[Unit] = (regexDesc <* char('#')).flatten
      scala> regex.parse("a.(c*)#abccc")
      val res0 = Success(())
      scala> regex.parse("a.(c*)#a")
      val res1 = Failure(..)
    Note

    there is significant overhead for using flatten: if possible try to avoid using it! This is because Parsley will need to generate, process, and compile each parser produced by the combinator during parse-time.

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

    This combinator will parse this parser zero or more times combining the results with the function f and base value k from the left.

    This combinator will parse this parser zero or more times combining the results with the function f and base value k from the left.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a left-to-right fashion using the function f: the accumulation is initialised with the value k. If this parser does fail at any point having consumed input, this combinator will fail.

    k

    initial accumulation value.

    f

    function to apply to each iteration's accumulator.

    returns

    a parser which parses this parser many times and folds the results together with f and k left-associatively.

    Example:
    1. // this is not an efficient implementation of stringOfMany
      def stringOfMany(pc: Parsley[Char]): Parsley[String] = {
          pc.foldLeft("")(_ + _)
      }
  24. def foldLeft1[B](k: B)(f: (B, A) => B): Parsley[B]

    This combinator will parse this parser one or more times combining the results with the function f and base value k from the left.

    This combinator will parse this parser one or more times combining the results with the function f and base value k from the left.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a left-to-right fashion using the function f: the accumulation is initialised with the value k. If this parser does fail at any point having consumed input, this combinator will fail.

    k

    initial accumulation value.

    f

    function to apply to each iteration's accumulator.

    returns

    a parser which parses this parser some times and folds the results together with f and k left-associatively.

    Example:
    1. val natural: Parsley[Int] = digit.foldLeft1(0)((x, d) => x * 10 + d.toInt)
    Since

    2.1.0

  25. def foldRight[B](k: B)(f: (A, B) => B): Parsley[B]

    This combinator will parse this parser zero or more times combining the results with the function f and base value k from the right.

    This combinator will parse this parser zero or more times combining the results with the function f and base value k from the right.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a right-to-left fashion using the function f: the right-most value provided to f is the value k. If this parser does fail at any point having consumed input, this combinator will fail.

    k

    value to use when this parser no longer succeeds.

    f

    function to apply to each value produced by this parser, starting at the right.

    returns

    a parser which parses this parser many times and folds the results together with f and k right-associatively.

    Example:
    1. // in reality, many is implemented more efficiently
      def many[A](p: Parsley[A]) = {
          p.foldRight(List.empty[A])(_ :: _)
      }
  26. def foldRight1[B](k: B)(f: (A, B) => B): Parsley[B]

    This combinator will parse this parser one or more times combining the results with the function f and base value k from the right.

    This combinator will parse this parser one or more times combining the results with the function f and base value k from the right.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a right-to-left fashion using the function f: the right-most value provided to f is the value k. If this parser does fail at any point having consumed input, this combinator will fail.

    k

    value to use when this parser no longer succeeds.

    f

    function to apply to each value produced by this parser, starting at the right.

    returns

    a parser which parses this parser some times and folds the results together with f and k right-associatively.

    Example:
    1. // in reality, some is implemented more efficiently
      def some[A](p: Parsley[A]) = {
          p.foldRight1(List.empty[A])(_ :: _)
      }
    Since

    2.1.0

  27. def force(): Unit

    Forces the compilation of a parser as opposed to the regular lazy evaluation.

  28. def getClass(): Class[_ <: AnyVal]
    Definition Classes
    AnyVal → Any
  29. def getOrElse[Aʹ >: A](x: ): Parsley[]

    This combinator returns x if this parser fails without consuming input.

    This combinator returns x if this parser fails without consuming input.

    If this parser is successful, then this combinator is successful and no further action is taken. Otherwise, if this parser fails without consuming input, then x is unconditionally returned. If this parser fails having consumed input, this combinator fails. Functionally the same as this <|> pure(x).

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    Example:
    1. scala> import parsley.character.string
      scala> val p = string("aa").getOrElse("b")
      scala> p.parse("aa")
      val res0 = Success("a")
      scala> p.parse("xyz")
      val res1 = Success("b")
      scala> p.parse("ab")
      val res2 = Failure(..) // first parser consumed an 'a'!
    Note

    just an alias for </>

  30. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  31. def map[B](f: (A) => B): Parsley[B]

    This combinator allows the result of this parser to be changed using a given function.

    This combinator allows the result of this parser to be changed using a given function.

    When this parser succeeds, map(f) will adjust its result using the function f, which can potentially change its type. This can be used to build more complex results from parsers, instead of just characters or strings.

    In Haskell, this combinator is known as fmap or (<$>).

    f

    the function to apply to the result of the parse

    returns

    a new parser that behaves the same as this parser, but with the given function f applied to its result.

    Example:
    1. scala> import parsley.character.digit
      scala> digit.map(_.asDigit).parse("7")
      val res0 = Success(7)
    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).

  32. def mapFilter[B](f: (A) => Option[B]): Parsley[B]

    This combinator applies a function f to the result of this parser: if it returns a Some(y), y is returned, otherwise the parser fails.

    This combinator applies a function f to the result of this parser: if it returns a Some(y), y is returned, otherwise the parser fails.

    First, parse this parser. If it succeeds, apply the function f to the result x. If f(x) returns Some(y), return y. If f(x) returns None, or this parser failed then this combinator fails. Is a more efficient way of performing a filter and map at the same time.

    f

    the function used to both filter the result of this parser and transform it.

    returns

    a parser that returns the result of this parser applied to f, if it yields a value.

    Example:
    1. scala> val int: Parsley[Int] = ..
      scala> val safeDiv: Parsley[Int] = (int <~> char(' ') *> int).collect {
        case (x, y) if y != 0 => Some(x / y)
        case _ => None
      }
      scala> safeDiv.parse("7 0")
      val res0 = Failure(..) // y cannot be 0!
      scala> safeDiv.parse("10 2")
      val res1 = Success(5)
    Since

    4.0.0

    Note

    when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if amend had been used) and the caret will span the entire successful parse of this parser.

  33. def orElse[Aʹ >: A](q: Parsley[]): Parsley[]

    This combinator parses q if this parser fails without consuming input.

    This combinator parses q if this parser fails without consuming input.

    If this parser is successful, then this combinator is successful and no further action is taken. Otherwise, if this parser fails without consuming input, then q is parsed instead. If this parser fails having consumed input, this combinator fails.

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    the type returned by q, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    q

    the parser to run if this parser fails having not consumed input.

    returns

    a parser which either parses this parser or parses q.

    Example:
    1. scala> import parsley.character.string
      scala> val p = string("a").orElse(string("b"))
      scala> p.parse("a")
      val res0 = Success("a")
      scala> p.parse("b")
      val res1 = Success("b")
      scala> val q = string("ab").orElse(string("ac"))
      scala> q.parse("ac")
      val res2 = Failure(..) // first parser consumed an 'a'!
    Since

    4.0.0

    Note

    just an alias for <|>.

  34. def overflows(): Unit

    Provides an indicator that this parser will likely stack-overflow and so a stack-safe construction should be used when "compiling" this parser.

  35. def parse[Err](input: String)(implicit arg0: ErrorBuilder[Err]): Result[Err, A]

    This method is responsible for actually executing parsers.

    This method is responsible for actually executing parsers. Given an input array, will parse the string with the parser. The result is either a Success or a Failure.

    input

    The input to run against

    returns

    Either a success with a value of type A or a failure with error message

    Since

    3.0.0

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

    This combinator will parse this parser one or more times combining the results left-associatively with the function op.

    This combinator will parse this parser one or more times combining the results left-associatively with the function op.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a left-to-right fashion using the function op. If this parser does fail at any point having consumed input, this combinator will fail.

    op

    function to apply to each value produced by this parser, starting from the left.

    returns

    a parser which parses this parser some times and folds the results together with op left-associatively.

    Example:
    1. stmt.reduceLeft(Seq(_, _))
    Since

    2.3.0

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

    This combinator will parse this parser zero or more times combining the results left-associatively with the function op.

    This combinator will parse this parser zero or more times combining the results left-associatively with the function op.

    This parser will continue to be parsed until it fails having not consumed input. If this parser succeeded at least once, all of the results generated by the successful parses are then combined in a left-to-right fashion using the function op and returned in a Some. If no successful parses occurred, then None is returned. If this parser does fail at any point having consumed input, this combinator will fail.

    op

    function to apply to each value produced by this parser, starting from the left.

    returns

    a parser which parses this parser many times and folds the results together with op left-associatively or returns None if no parses occured.

    Example:
    1. arg.reduceLeftOption(Sep(_, _))
    Since

    2.3.0

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

    This combinator will parse this parser one or more times combining the results right-associatively with the function op.

    This combinator will parse this parser one or more times combining the results right-associatively with the function op.

    This parser will continue to be parsed until it fails having not consumed input. All of the results generated by the successful parses are then combined in a right-to-left fashion using the function op. If this parser does fail at any point having consumed input, this combinator will fail.

    op

    function to apply to each value produced by this parser, starting from the right.

    returns

    a parser which parses this parser some times and folds the results together with op right-associatively.

    Example:
    1. stmt.reduceRight(Seq(_, _))
    Since

    2.3.0

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

    This combinator will parse this parser zero or more times combining the results right-associatively with the function op.

    This combinator will parse this parser zero or more times combining the results right-associatively with the function op.

    This parser will continue to be parsed until it fails having not consumed input. If this parser succeeded at least once, all of the results generated by the successful parses are then combined in a right-to-left fashion using the function op and returned in a Some. If no successful parses occurred, then None is returned. If this parser does fail at any point having consumed input, this combinator will fail.

    op

    function to apply to each value produced by this parser, starting from the right.

    returns

    a parser which parses this parser many times and folds the results together with op right-associatively or returns None if no parses occured.

    Example:
    1. arg.reduceRightOption(Sep(_, _))
    Since

    2.3.0

  40. def toString(): String
    Definition Classes
    Any
  41. def unsafe(): Parsley[A]

    Using this method signifies that the parser it is invoked on is impure and any optimisations which assume purity are disabled.

  42. def void: Parsley[Unit]

    Replaces the result of this parser with ().

    Replaces the result of this parser with ().

    This combinator is useful when the result of this parser is not required, and the type must be Parsley[Unit]. Functionally the same as this #> ().

    returns

    a new parser that behaves the same as this parser, but always returns () on success.

  43. def withFilter(pred: (A) => Boolean): Parsley[A]

    This is an alias for p.filter(pred).

    This is an alias for p.filter(pred). It is needed to support for-comprehension syntax with ifs.

    Since

    4.0.0

    See also

    filter for more information.

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

    This combinator first parses this parser then parses q: if both succeed the result of this parser is paired with the result of q.

    This combinator first parses this parser then parses q: if both succeed the result of this parser is paired with the result of q.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then (x, y) is returned. If either fail then the entire combinator fails.

    q

    the parser to run second.

    returns

    a parser that sequences this parser with q and pairs their results together.

    Example:
    1. scala> import parsley.character.char
      scala> val p = char('a').zip(char('b'))
      scala> p.parse("ab")
      val res0 = Success(('a', 'b'))
      scala> p.parse("b")
      val res1 = Failure(..)
      scala> p.parse("a")
      val res2 = Failure(..)
    Since

    2.3.0

    Note

    alias for <~>.

  45. def |[Aʹ >: A](q: Parsley[]): Parsley[]

    This combinator, pronounced "or", parses q if this parser fails without consuming input.

    This combinator, pronounced "or", parses q if this parser fails without consuming input.

    If this parser is successful, then this combinator is successful and no further action is taken. Otherwise, if this parser fails without consuming input, then q is parsed instead. If this parser fails having consumed input, this combinator fails.

    The reason for this behaviour is that it prevents space leaks and improves error messages. If this behaviour is not desired, use attempt(this) to rollback any input consumed on failure.

    the type returned by q, which must be a supertype of the result type of this parser: this allows for weakening of the result type.

    q

    the parser to run if this parser fails having not consumed input.

    returns

    a parser which either parses this parser or parses q.

    Example:
    1. scala> import parsley.character.string
      scala> val p = string("a") | string("b")
      scala> p.parse("a")
      val res0 = Success("a")
      scala> p.parse("b")
      val res1 = Success("b")
      scala> val q = string("ab") | string("ac")
      scala> q.parse("ac")
      val res2 = Failure(..) // first parser consumed an 'a'!
    Since

    4.0.0

    Note

    just an alias for <|>.

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

    This combinator, pronounced "then", first parses this parser then parses q: if both succeed then the result of q is returned.

    This combinator, pronounced "then", first parses this parser then parses q: if both succeed then the result of q is returned.

    First, this parser is ran, yielding x on success, then q is ran, yielding y on success. If both are successful then y is returned and x is ignored. If either fail then the entire combinator fails.

    Identical to *>: *> is more common in Haskell, whereas ~> is more common in Scala.

    q

    the parser to run second, which returns the result of this combinator.

    returns

    a parser that sequences this parser with q and returns q's result.

    Example:
    1. scala> import parsley.character.char
      scala> ('a' ~> 'b').parse("ab")
      val res0 = Success('b')
    Since

    2.4.0

Inherited from AnyVal

Inherited from Any

Running Parsers

These methods allow for a parser to be executed.

Result Changing Combinators

These combinators change the result of the parser they are called on into a value of a different type. This new result value may or may not be derived from the previous result.

Branching Combinators

These combinators allow for parsing one alternative or another. All of these combinators are left-biased, which means that the left-hand side of the combinator is tried first: the right-hand side of the combinator will only be tried when the left-hand one failed (and did not consume input in the process).

Sequencing Combinators

These combinators all combine two parsers in sequence. The receiver of the combinator will be executed first, then the argument second. The results of both parsers are combined in some way (depending on the individual combinator). If one of the parsers fails, the combinator as a whole fails.

Filtering Combinators

These combinators perform filtering on the results of a parser. This means that, given the result of a parser, they will perform some function on that result, and the success of that function effects whether or not the parser fails.

Folding Combinators

These combinators repeatedly execute a parser (at least zero or one times depending on the specific combinator) until it fails. The results of the successes are then combined together using a folding function. An initial value for the accumulation may be given (for the folds), or the first successful result is the initial accumulator (for the reduces). These are implemented efficiently and do not need to construct any intermediate list with which to store the results.

Expensive Sequencing Combinators

These combinators can sequence two parsers, where the first parser's result influences the structure of the second one. This may be because the second parser is generated from the result of the first, or that the first parser returns the second parser. Either way, the second parser cannot be known until runtime, when the first parser has been executed: this means that Parsley is forced to compile the second parser during parse-time, which is very expensive to do repeatedly. These combinators are only needed in exceptional circumstances, and should be avoided otherwise.

Special Methods

These are methods that should be rarely needed.

Ungrouped