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.
- Alphabetic
- By Inheritance
- Parser
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def !~(p2: Parser[Token, Any]): Parser[Token, A]
- Annotations
- @inline()
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def *>[B](p2: => Parser[Token, B]): Parser[Token, B]
- def <*[B](p2: => Parser[Token, B]): Parser[Token, A]
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def ?: Parser[Token, Option[A]]
- def as[B](b: B): Parser[Token, B]
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def backtrack: Parser[Token, A]
- def between(left: Parser[Token, Any], right: Parser[Token, Any]): Parser[Token, A]
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- 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 the9
, then collect would fail the parser with a consuming result -char('9')
would not even be attempted. - def eitherOr[B](p2: Parser[Token, B]): Parser[Token, Either[B, A]]
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- 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 the9
, then filter would fail the parser with a consuming result -char('9')
would not even be attempted. - def filterNot(f: (A) => Boolean): Parser[Token, A]
- def flatMap[B](f: (A) => Parser[Token, B]): Parser[Token, B]
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- 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".
- def map[B](f: (A) => B): Parser[Token, B]
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def notFollowedBy(p2: Parser[Token, Any]): Parser[Token, A]
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def orElse[AA >: A](p2: => Parser[Token, AA]): Parser[Token, AA]
- def parse[Source](input: Source)(implicit at: AsTokens[Source, Token], sm: SourceMap[Token]): Result[Token, A]
- def rep: Parser[Token, List[A]]
- def rep0: Parser[Token, List[A]]
- def repSep[Sep](sep: Parser[Token, Sep]): Parser[Token, List[A]]
- def repSep0[Sep](sep: Parser[Token, Sep]): Parser[Token, List[A]]
- def surroundedBy(p: Parser[Token, Any]): Parser[Token, A]
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- 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 be1
.- the parser would then fail, because there is no digit to read.
- def void: Parser[Token, Unit]
- 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()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- def withFilter(f: (A) => Boolean): Parser[Token, A]
- def withPosition: Parser[Token, Parsed[A]]
- 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 parsingfoo
. A non-backtracking parser will not attemptbar
, 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
orbar
, but that we were parsingfoo
and found ana
where we expected ano
).
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 onstring("foo")
allowsparser
to attemptstring("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.
- def ~[B](p2: => Parser[Token, B]): Parser[Token, (A, B)]
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated