sealed abstract class Parser[+A] extends AnyRef
Parser[A] attempts to extract an A
value from the given input,
potentially moving its offset forward in the process.
When calling parse
, one of three outcomes occurs:
- Success: The parser consumes zero-or-more characters of input and successfully extracts a value. The input offset will be moved forward by the number of characters consumed.
- Epsilon failure: The parser fails to extract a value without consuming any characters of input. The input offset will not be changed.
- Arresting failure: The parser fails to extract a value but does consume one-or-more characters of input. The input offset will be moved forward by the number of characters consumed and all parsing will stop (unless a higher-level parser backtracks).
Operations such as x.orElse(y)
will only consider parser y
if
x
returns an epsilon failure; these methods cannot recover from
an arresting failure. Arresting failures can be "rewound" using
methods such as x.backtrack
(which converts arresting failures
from x
into epsilon failures), or softProduct(x, y)
(which can
rewind successful parses by x
that are followed by epsilon
failures for y
).
Rewinding tends to make error reporting more difficult and can lead to exponential parser behavior it is not the default behavior.
- Source
- Parser.scala
- Alphabetic
- By Inheritance
- Parser
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
-
abstract
def
parseMut(state: State): A
Internal (mutable) parsing method.
Internal (mutable) parsing method.
This method should only be called internally by parser instances.
- Attributes
- protected
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
?: Parser[Option[A]]
Convert epsilon failures into None values.
Convert epsilon failures into None values.
Normally if a parser fails to consume any input it fails with an epsilon failure. The
?
method converts these failures into None values (and wraps other values inSome(_)
).If the underlying parser failed with other errors, this parser will still fail.
-
def
as[B](b: B): Parser[B]
Replaces parsed values with the given value.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
backtrack: Parser[A]
If this parser fails to match, rewind the offset to the starting point before moving on to other parser.
If this parser fails to match, rewind the offset to the starting point before moving on to other parser.
This method converts arresting failures into epsilon failures, which includes rewinding the offset to that used before parsing began.
This method will most often be used before calling methods such as
orElse
,~
, orflatMap
which involve a subsequent parser picking up where this one left off. -
def
between(b: Parser[Any], c: Parser[Any]): Parser[A]
Use this parser to parse between values.
Use this parser to parse between values.
Parses
b
followed bythis
andc
. Returns only the values extracted bythis
parser. -
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
filter(fn: (A) ⇒ Boolean): Parser[A]
If the predicate is not true, fail you may want .filter(fn).backtrack so if the filter fn fails you can fall through in an oneOf or orElse
If the predicate is not true, fail you may want .filter(fn).backtrack so if the filter fn fails you can fall through in an oneOf or orElse
Without the backtrack, a failure of the function will be an arresting failure.
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
flatMap[B](fn: (A) ⇒ Parser[B]): Parser[B]
Dynamically construct the next parser based on the previously parsed value.
Dynamically construct the next parser based on the previously parsed value.
Using
flatMap
is very expensive. When possible, you should prefer to use methods such as~
,*>
, or<*
when possible, since these are much more efficient. -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
map[B](fn: (A) ⇒ B): Parser[B]
Transform parsed values using the given function.
Transform parsed values using the given function.
This parser will match the same inputs as the underlying parser, using the given function
f
to transform the values the underlying parser produces.If the underlying value is ignored (e.g.
map(_ => ...)
) callingvoid
beforemap
will improve the efficiency of the parser. -
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[A1 >: A](that: Parser[A1]): Parser[A1]
If this parser fails to parse its input with an epsilon error, try the given parser instead.
If this parser fails to parse its input with an epsilon error, try the given parser instead.
If this parser fails with an arresting error, the next parser won't be tried.
Backtracking may be used on the left parser to allow the right one to pick up after any error, resetting any state that was modified by the left parser.
-
final
def
parse(str: String): Either[Error, (String, A)]
Attempt to parse an
A
value out ofstr
.Attempt to parse an
A
value out ofstr
.This method will either return a failure, or else the remaining string and the parsed value.
To require the entire input to be consumed, see
parseAll
. -
final
def
parseAll(str: String): Either[Error, A]
Attempt to parse all of the input
str
into anA
value.Attempt to parse all of the input
str
into anA
value.This method will return a failure unless all of
str
is consumed during parsing.p.parseAll(s)
is equivalent to(p <* Parser.end).parse(s).map(_._2)
. -
def
peek: Parser[Unit]
Return a parser that succeeds (consuming nothing and extracting nothing) if the current parser would also succeed.
Return a parser that succeeds (consuming nothing and extracting nothing) if the current parser would also succeed.
This parser expects the underlying parser to succeed, and will unconditionally backtrack after running it.
-
def
soft: Soft[A]
Wrap this parser in a helper class, to enable backtracking during composition.
Wrap this parser in a helper class, to enable backtracking during composition.
This wrapper changes the behavior of
~
,<*
and*>
. Normally no backtracking occurs. Usingsoft
on the left-hand side will enable backtracking if the right-hand side returns an epsilon failure (but not in any other case).For example,
(x ~ y)
will never backtrack. But with(x.soft ~ y)
, ifx
parses successfully, andy
returns an epsilon failure, the parser will "rewind" to the point beforex
began. -
def
string: Parser[String]
Return the string matched by this parser.
Return the string matched by this parser.
When parsing an input string that the underlying parser matches, this parser will return the matched substring instead of any value that the underlying parser would have returned. It will still match exactly the same inputs as the original parser.
This method is very efficient: similarly to
void
, we can avoid allocating results to return. -
def
surroundedBy(b: Parser[Any]): Parser[A]
Use this parser to parse surrounded by values.
Use this parser to parse surrounded by values.
This is the same as
between(b, b)
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
unary_!: Parser[Unit]
Return a parser that succeeds (consuming nothing, and extracting nothing) if the current parser would fail.
Return a parser that succeeds (consuming nothing, and extracting nothing) if the current parser would fail.
This parser expects the underlying parser to fail, and will unconditionally backtrack after running it.
-
def
void: Parser[Unit]
Parse without capturing values.
Parse without capturing values.
Calling
void
on a parser can be a significant optimization -- it allows the parser to avoid allocating results to return.Other methods like
as
,*>
, and<*
usevoid
internally to discard allocations, since they will ignore the original parsed result. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
with1: With1[A]
Wrap this parser in a helper class, enabling better composition with
Parser1
values.Wrap this parser in a helper class, enabling better composition with
Parser1
values.For example, with
p: Parser[Int]
andp1: Parser[Double]
:val a1: Parser[(Int, Double)] = p ~ p1 val a2: Parser1[(Int, Double)] = p.with1 ~ p1
val b1: Parser[Double] = p *> p1 val b2: Parser1[Double] = p.with1 *> p1
val c1: Parser[Int] = p <* p1 val c2: Parser1[Int] = p.with1 <* p1
Without using
with1
, these methods will returnParser
values since they are not known to returnParser1
values instead. -
def
~[B](that: Parser[B]): Parser[(A, B)]
Sequence another parser after this one, combining both results into a tuple.
Sequence another parser after this one, combining both results into a tuple.
This combinator returns a product of parsers. If this parser successfully produces an
A
value, the other parser is run on the remaining input to try to produce aB
value.If either parser produces an error the result is an error. Otherwise both extracted values are combined into a tuple.