[use case]
Always returns this
, so that this
can be passed to Splitter#through
Always returns this
, so that this
can be passed to Splitter#through
Create a copy of this parser that will observe an early EOF upon entering a context matched by the
given matcher
.
Create a copy of this parser that will observe an early EOF upon entering a context matched by the
given matcher
. This is especially useful for creating followedBy
chains involving optional elements.
Normally, a parser for an optional item in some context will not finish until that context ends,
or until the item is encountered. So if the item is not present, followedBy
logic won't work
since the following parser/transformer will not see any events.
To make sure the leading parser can "fail fast", you can force it to end early if it encounters
a specific context, i.e. the context used by the parser/transformer being passed to .follwedBy
.
Example:
val preludeContext = * \ "prelude" val dataContext = * \ "data" for { prelude <- Splitter(preludeContext).firstOption[Prelude].beforeContext(dataContext).followedByStream data <- Splitter(dataContext).as[Data] } yield data
Impose expectations on the sequence of inputs to be received by handlers created by this parser.
Impose expectations on the sequence of inputs to be received by handlers created by this parser.
As this parser's handler receives an input, the input will be tested against the head of the expectations list.
If the test returns false
, the expectation is failed and the handler will throw an exception.
If the test returns true
, the expectation is satisfied, and the handler will advance to the next expectation.
If there are no more expectations left in the list (i.e. N inputs have satisfied the corresponding N expectations),
then all expectations have been met and inputs will be treated as normal by the handler.
If the handler receives an EOF before all expectations are met, it will throw an exception.
A sequence of label -> test
expectations imposed on inputs to this parser
A copy of this parser with expectations imposed on its inputs
Intermediate object for creating a sequence parser in which the result of this parser will be used to initialize a second parser as soon as it is available.
Intermediate object for creating a sequence parser in which the result of this parser will be used to initialize a second parser as soon as it is available.
In other words, the source (series of In
values) will be fed into this Parser until this
parser's handler returns a result of type Out
. At that point, the second parser (as
specified by using the apply
or flatMap
methods on the FollowedBy
returned by this method)
will be instantiated. Any relevent "stack events" (see Stackable
) will be replayed so the
second parser has the right context, and from that point on, all In
values will be sent
to the second parser. When that second parser returns a result, that result becomes the output
of the combined parser created by this.followedBy(out => makeSecondParser(out))
Examples:
val p1: Parser[A] = /* ... */ * def getP2(p1Result: A): Parser[B] = /* ... */ * val combined: Parser[B] = p1.followedBy(getP2) // alternative `flatMap` syntax val combined: Parser[B] = for { p1Result <- p1.followedBy p2Result <- getP2(p1Result) } yield p2Result
See interruptedBy
, which is useful when a transformer.parseFirstOption
must be followedBy
some other parser.
Intermediate object creating a transformer that depends on this parser.
Intermediate object creating a transformer that depends on this parser. Particularly useful in cases where one or more specific "info" elements precede a stream of other elements which require that "info" to be parsed.
Examples:
val p1: Parser[In, A] = /* ... */ * def getP2Stream(p1Result: A): Transformer[In, B] = /* ... */ * val combined: Transformer[In, B] = p1.andThenStream(getP2Stream) // alternative `flatMap` syntax val combined: Transformer[In, B] = for { p1Result <- p1.andThenStream p2Result <- getP2Stream(p1Result) } yield p2Result
See followedBy
for a general explanation of how the combination works.
See also, interruptedBy
, which is useful when a transformer.parseFirstOption
must be followedBy
some other transformer.
Create a copy of this parser that will treat a result from the interrupter
as an early EOF.
Create a copy of this parser that will treat a result from the interrupter
as an early EOF.
This is especially useful for creating followedBy
chains involving optional elements.
Normally, a parser for an optional item in some context will not finish until that context ends,
or until the item is encountered. So if the item is not present, followedBy
logic won't work
since the following parser/transformer will not see any events.
To make sure the leading parser can "fail fast", you can "interrupt" it, typically by creating
a parser that immediately returns a result upon entering a particular context, i.e. the context
in which the "following" parser will start. Parser#beforeContext
provides a convenience for
doing so.
Transform this Parser's results using a transformation function.
Transform this Parser's results using a transformation function.
The transformation function
A new Parser instance which computes its results by applying f
to the
results computed by this Parser
Combine this parser with the fallback
such that if either one fails but the other succeeds,
the result will be taken from the one that succeeds.
Combine this parser with the fallback
such that if either one fails but the other succeeds,
the result will be taken from the one that succeeds.
Note: if you want to have a chain of fallbacks, it will be more efficient to do
Parser.oneOf(p1, p2, p3, ...)
than doing p1 orElse p2 orElse p3 orElse ...
.
Parse the given source object by spawning a handler and feeding events from the source into that handler until it yields a result.
Parse the given source object by spawning a handler and feeding events from the source into that handler until it yields a result.
The source object type
An object that can be treated as a stream of In
events
Typeclass instance allowing instances of Src
to be treated as a stream of In
events
The parse result
Given a parser whose output type is a Try[T]
, return a new parser which will unwrap the Try
,
returning either the underlying T
for Success
, or throwing the caught exception for Failure
.
Given a parser whose output type is a Try[T]
, return a new parser which will unwrap the Try
,
returning either the underlying T
for Success
, or throwing the caught exception for Failure
.
This operation is the opposite of wrapSafe
.
The unwrapped type
Evidence that this parser's output type is a Try[T]
for some type T
A parser that unwraps the Try
returned by this parser
Creates a copy of this parser, but with a different toString
Creates a copy of this parser, but with a different toString
The new "name" (i.e. toString
) for this parser
A copy of this parser whose toString
returns the given name
Create a new parser to wrap this one, such that any exception thrown during parsing will be caught
and returned as a Failure
.
Create a new parser to wrap this one, such that any exception thrown during parsing will be caught
and returned as a Failure
.
A parser that will return a Failure instead of throwing an exception
[use case]
(parser: StringAdd).self
(parser: StringFormat).self
(parser: ArrowAssoc[Parser[In, Out]]).x
(Since version 2.10.0) Use leftOfArrow
instead
(parser: Ensuring[Parser[In, Out]]).x
(Since version 2.10.0) Use resultOfEnsuring
instead
An immutable object that can be used to create
Handler
s.