Packages

trait Parser[Token, +A] extends AnyRef

Parses a sequence of Token into an A.

The companion object provides standard parsers from which to start building larger ones. In particular, it contains the necessary tools to start writing a string parser, such as char and string.

In order to provide better error messages, developers are encouraged to use label to describe the kind of thing a parser will produce - a digit, for example, or an array, or...

An important thing to realise is that parsers are non-backtracking by default. See the | documentation for detailed information on the consequences of this design choice.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Parser
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def run(state: State[Token]): Result[Token, A]
    Attributes
    protected

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. def !~(p2: Parser[Token, Any]): Parser[Token, A]
    Annotations
    @inline()
  3. final def ##: Int
    Definition Classes
    AnyRef → Any
  4. def *>[B](p2: => Parser[Token, B]): Parser[Token, B]
  5. def <*[B](p2: => Parser[Token, B]): Parser[Token, A]
  6. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  7. def ?: Parser[Token, Option[A]]
  8. def as[B](b: B): Parser[Token, B]
  9. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  10. def backtrack: Parser[Token, A]
  11. def between(left: Parser[Token, Any], right: Parser[Token, Any]): Parser[Token, A]
  12. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  13. def collect[B](f: PartialFunction[A, B]): Parser[Token, B]

    A filter and a map rolled into one.

    A filter and a map rolled into one.

    For similar reasons to filter, such parsers are backtracking. Consider the following example:

    val parser = digit.collect {
      case c if c != '9' => c.toInt
    } | char('9').as(9)
    
    parser.parse("9")

    If collect didn't turn the parser backtracking, this would fail, even though 9 is valid input: digit would succeed and consume the 9, then collect would fail the parser with a consuming result - char('9') would not even be attempted.

  14. def eitherOr[B](p2: Parser[Token, B]): Parser[Token, Either[B, A]]
  15. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  17. def filter(f: (A) => Boolean): Parser[Token, A]

    Fails any parser that does not match the specified predicate.

    Fails any parser that does not match the specified predicate.

    Note that such parsers are, of necessity, backtracking. Consider the following admitedly silly example:

    val parser = digit.filter(_ != '9') | char('9')
    parser.parse("9")

    If filter didn't turn digit backtracking, then this parser would fail, even though 9 is perfectly valid input for it. digit would succeed and consume the 9, then filter would fail the parser with a consuming result - char('9') would not even be attempted.

  18. def filterNot(f: (A) => Boolean): Parser[Token, A]
  19. def flatMap[B](f: (A) => Parser[Token, B]): Parser[Token, B]
  20. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  21. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  22. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  23. def label(label: String): Parser[Token, A]

    Sets the label of this parser.

    Sets the label of this parser.

    A parser's label is a simple description of what they're expecting to parse. Typically, a parser who means to parse a "true / false" value would have a boolean label.

    This allows us to provide meaningful error messages, where instead of saying "expected [, { or true", we can have "expected array, object or boolean".

  24. def map[B](f: (A) => B): Parser[Token, B]
  25. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  26. def notFollowedBy(p2: Parser[Token, Any]): Parser[Token, A]
  27. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  28. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  29. def orElse[AA >: A](p2: => Parser[Token, AA]): Parser[Token, AA]
  30. def parse[Source](input: Source)(implicit at: AsTokens[Source, Token], sm: SourceMap[Token]): Result[Token, A]
  31. def rep: Parser[Token, List[A]]
  32. def rep0: Parser[Token, List[A]]
  33. def repSep[Sep](sep: Parser[Token, Sep]): Parser[Token, List[A]]
  34. def repSep0[Sep](sep: Parser[Token, Sep]): Parser[Token, List[A]]
  35. def surroundedBy(p: Parser[Token, Any]): Parser[Token, A]
  36. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  37. def toString(): String
    Definition Classes
    AnyRef → Any
  38. def unary_!: Parser[Token, Unit]

    Fails when this parser succeeds, and succeeds when it fails.

    Fails when this parser succeeds, and succeeds when it fails.

    The returned parser will always be backtracking. Consider the following example:

    val parser = (string("foo") <* !char('1')) ~ digit
    
    parser.parse("foo2")

    If !char('1') was non-backtracking, then:

    • 2 would be consumed and confirmed to not be 1.
    • the parser would then fail, because there is no digit to read.
  39. def void: Parser[Token, Unit]
  40. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  41. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  42. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  43. def withFilter(f: (A) => Boolean): Parser[Token, A]
  44. def withPosition: Parser[Token, Parsed[A]]
  45. def |[AA >: A](p2: => Parser[Token, AA]): Parser[Token, AA]

    Attempts either this parser or the specified one.

    Attempts either this parser or the specified one.

    Note that this is non-backtracking: the alternative parser will only be tried if this parser is non-consuming.

    Consider the following example:

    val parser =  string("foo") | string("bar")
    parser.parse("foa")

    It's perfectly impossible for bar to be a valid match here, and we know that as soon as we've started successfuly parsing foo. A non-backtracking parser will not attempt bar, which yields:

    • performance improvement (we're not trying parses that we know will fail).
    • better error messages (the error isn't that we were expecting foo or bar, but that we were parsing foo and found an a where we expected an o).

    It is sometimes necessary to override that behaviour. Take the following example:

    val parser = string("foo") | string("foa")
    parser.parse("foa")

    We want this to succeed, but since string("foo") is non-backtracking, string("foa") will not be attempted. In these scenarios, calling backtrack on string("foo") allows parser to attempt string("foa")

    Finally, consider the following:

    val parser = string("foo").backtrack | string("bar")
    parser.parse("bar")

    This will succeed: non-consuming successes will still result in the alternative parser being attempted, to try and find the first result that actually consumes data.

  46. def ~[B](p2: => Parser[Token, B]): Parser[Token, (A, B)]

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from AnyRef

Inherited from Any

Ungrouped