ParserDelay

io.dylemma.spac.impl.ParserDelay
class ParserDelay[A](result: () => A) extends Stateless[Any, A]

Attributes

Graph
Supertypes
trait Stateless[Any, A]
trait Handler[Any, A]
trait Parser[Any, A]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def finish(): A

Signal the end of the data stream to this handler, forcing it to generate a result. Handlers may throw exceptions in response to this, such as a handler which wants the first event from an empty stream.

Signal the end of the data stream to this handler, forcing it to generate a result. Handlers may throw exceptions in response to this, such as a handler which wants the first event from an empty stream.

Further calls to step or finish after the first call to finish will result in undefined behavior. The general assumption is that a handler should be discarded after its finish method is called.

Attributes

Returns

the final result of this parser

def step(in: Any): Either[A, Handler[Any, A]]

Advance the state of this handler by accepting a single input of type In. If doing so would cause this parser to complete, return a Left containing the output. Otherwise, return a Right containing the next parser state.

Advance the state of this handler by accepting a single input of type In. If doing so would cause this parser to complete, return a Left containing the output. Otherwise, return a Right containing the next parser state.

Handlers are assumed to be internally-mutable, so it is acceptable to simply update some internal state and then return Right(this), although in some cases it will be desirable to return a separate handler entirely.

Value parameters

in

A single input event from a data stream

Attributes

Returns

If the input would finish the parser, return a Left containing the result. Otherwise, return a Right containing a Handler which represents the next parsing state. The handler in a Right may be this handler, or a completely separate one.

Inherited methods

final def &&[In2](that: Parser[In2, Boolean])(implicit booleanOut: A <:< Boolean): Parser[In2, Boolean]

Operator version of and

Operator version of and

Attributes

See also

and

Inherited from:
Parser
def and[In2](that: Parser[In2, Boolean])(implicit booleanOut: A <:< Boolean): Parser[In2, Boolean]

Combines this parser with another, creating a new parser that will yield false if either inner parser yields false, or true if both inner parsers yield true.

Combines this parser with another, creating a new parser that will yield false if either inner parser yields false, or true if both inner parsers yield true.

The returned parser has "eager" semantics, where it will abort as soon as it can. For example, if this parser yields false before that parser encounters an input that would cause it to throw an exception, the returned parser will return false and abort, never encountering the error. For non-eager semantics, use Parser's Applicative to combine the two parsers, e.g. (this, that).mapN(_ && _)

Type parameters

In2

Upper bound for this and that's input type

Value parameters

booleanOut

Evidence that this parser's output type is Boolean

that

Another parser with a Boolean output type

Attributes

Returns

An eagerly-evaluating conjunction of the two parsers

Inherited from:
Parser
def asTopLevelHandler(caller: SpacTraceElement): Handler[In, Out]

Wraps this handler as a "top level" handler, which will inject a SpacTraceElement (representing the current input or the "EOF" signal) to any exception is thrown by this handler when calling its step or finish methods.

Wraps this handler as a "top level" handler, which will inject a SpacTraceElement (representing the current input or the "EOF" signal) to any exception is thrown by this handler when calling its step or finish methods.

Used internally by Parser's parse methods.

Attributes

Inherited from:
Handler
def asTransformer: Transformer[In, Out]

Represent this parser as a Transformer which emits this parser's result

Represent this parser as a Transformer which emits this parser's result

Attributes

Inherited from:
Parser
def attempt: Parser[In, Either[Throwable, Out]]

Like wrapSafe, but represents exceptions as Left and successful results as Right

Like wrapSafe, but represents exceptions as Left and successful results as Right

Attributes

Inherited from:
Parser
def beforeContext[I2, StackElem](matcher: ContextMatcher[StackElem, Any])(implicit stackable: StackLike[I2, StackElem], pos: CallerPos): Parser[I2, Out]

Specialization of interruptedBy for stack-like input types, such that an interruption will occur upon entering a stack context that can be matched by the given matcher.

Specialization of interruptedBy for stack-like input types, such that an interruption will occur upon entering a stack context that can be matched by the given matcher.

Example:

val preludeContext = * \ "prelude"
val dataContext = * \ "data"
for {
 prelude <- Splitter(preludeContext).firstOption[Prelude].beforeContext(dataContext).followedByStream
 data <- Splitter(dataContext).as[Data]
} yield data

Type parameters

I2

Subtype of In, or just In (to satisfy contravariance of Parser's In type)

StackElem

Specialization of the In type for when it represents a stack push or pop

Value parameters

matcher

A matching function that operates on a context stack

stackable

Interprets the inputs as stack push/pop events to accumulate a context stack

Attributes

Returns

A parser which will perform an early finish() when a matching context is encountered

Inherited from:
Parser
def expectInputs[I2](expectations: List[(String, I2 => Boolean)]): Parser[I2, Out]

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.

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.

Value parameters

expectations

A sequence of label -> test expectations imposed on inputs to this parser

Attributes

Returns

A copy of this parser with expectations imposed on its inputs

Inherited from:
Parser
def interruptedBy[I2](interrupter: Parser[I2, Any]): Parser[I2, Out]

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.

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 followUp 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.

Note that if the interrupter throws an exception, that exception will not be caught. If your interrupter might throw, pass interrupter.wrapSafe instead to swallow the exception.

Type parameters

I2

Subtype of In, or just In (to satisfy contravariance of Parser's In type)

Value parameters

interrupter

A parser which will be run in parallel with this parser, and whose result will be treated as an early EOF for this parser, forcing an early call to finish().

Attributes

Returns

A parser which will perform an early finish() call when the interrupter produces a result.

Inherited from:
Parser
def map[Out2](f: A => Out2): Parser[In, Out2]

Create a copy of this Parser whose result is transformed by the given function f.

Create a copy of this Parser whose result is transformed by the given function f.

Type parameters

Out2

The new parser's result type

Value parameters

f

Result transformation function

Attributes

Inherited from:
Parser
def newHandler: Stateless.this.type

Parser's main abstract method; constructs a new Handler representing this parser's logic. Parsers are expected to be immutable, but Handlers may be internally-mutable.

Parser's main abstract method; constructs a new Handler representing this parser's logic. Parsers are expected to be immutable, but Handlers may be internally-mutable.

Attributes

Inherited from:
Stateless
def or[In2](that: Parser[In2, Boolean])(implicit booleanOut: A <:< Boolean): Parser[In2, Boolean]

Combines this parser with another, creating a new parser that will yield true if either inner parser yields true, or false if both inner parsers yield false.

Combines this parser with another, creating a new parser that will yield true if either inner parser yields true, or false if both inner parsers yield false.

The returned parser has "eager" semantics, where it will abort as soon as it can. For example, if this parser yields a true before that parser encounters an input that would cause it to throw an exception, the returned parser will return true and abort, never encountering the error. For non-eager semantics, use Parser's Applicative to combine the two parsers, e.g. (this, that).mapN(_ || _)

Type parameters

In2

Upper bound for this and that's input type

Value parameters

booleanOut

Evidence that this parser's output type is Boolean

that

Another parser with a Boolean output type

Attributes

Returns

An eagerly-evaluating disjunction of the two parsers

Inherited from:
Parser
def orElse[In2, Out2 >: A](fallback: Parser[In2, Out2]): Parser[In2, Out2]

Combine this parser with the fallback such that failures from the underlying parsers will be ignored as long as at least one succeeds. The result will be the result of whichever underlying parser succeeds first. If all of the underlying parsers fail, a SpacException.FallbackChainFailure will be thrown by the returned parser's handler.

Combine this parser with the fallback such that failures from the underlying parsers will be ignored as long as at least one succeeds. The result will be the result of whichever underlying parser succeeds first. If all of the underlying parsers fail, a SpacException.FallbackChainFailure will be thrown by the returned parser's handler.

Type parameters

In2

Subtype of In, or just In (to satisfy Parser's contravariance on the In type)

Out2

Supertype of Out, or just Out (to satisfy Parser's covariance on the Out type)

Value parameters

fallback

another parser of the same(ish) type as this one

Attributes

Returns

A new parser that will succeed if either this parser or the fallback succeed

Inherited from:
Parser
def parse(source: Source[Any])(implicit pos: CallerPos): Out

Consume the given source to produce an output or possibly throw a SpacException.

Consume the given source to produce an output or possibly throw a SpacException.

The Source[A] type is like Iterable[A] but uses the "lender" pattern to acquire the iterator and close any resources associated with the iterator after the iterator is consumed.

XML and JSON-specific Source constructors are provided by the "parser backend" libraries i.e. xml-spac-javax and json-spac-jackson.

Value parameters

pos

Captures the caller filename and line number, used to fill in the 'spac trace' if the parser throws an exception

source

An object that can provide a series of In values, e.g. XmlEvent or JsonEvent

Attributes

Returns

The parser result based on the given source

Inherited from:
Parser
def parse(inputs: Iterator[Any])(implicit pos: CallerPos): Out

Consume the given inputs iterator to produce an output or possibly throw a SpacException.

Consume the given inputs iterator to produce an output or possibly throw a SpacException.

After calling this method, the inputs should be discarded, since consuming an Iterator is a destructive operation.

Value parameters

inputs

A series of In values, e.g. XmlEvent or JsonEvent

pos

Captures the caller filename and line number, used to fill in the 'spac trace' if the parser throws an exception

Attributes

Returns

The parser result based on the given inputs

Inherited from:
Parser
def rethrow[T](implicit ev: A <:< Either[Throwable, T]): Parser[In, T]

Like unwrapSafe, but rethrows exceptions from Left or returns results from Right. This operation is the opposite of attempt.

Like unwrapSafe, but rethrows exceptions from Left or returns results from Right. This operation is the opposite of attempt.

Attributes

Inherited from:
Parser
def start(methodName: String)(implicit pos: CallerPos): Handler[In, Out]

Low-level consumer method: creates a new handler and binds the caller position for its SpacTraceElement.

Low-level consumer method: creates a new handler and binds the caller position for its SpacTraceElement.

Used internally by the parse methods. Start with this method if you have some sequence-like datatype that doesn't provide an Iterator.

This is just a convenience for newHandler.asTopLevelhandler which helps construct a useful SpacTraceElement.

Value parameters

methodName

The method name used to construct the SpacTraceElement for the handler. Defaults to "start"

pos

Captures the caller filename and line number, used to fill in the 'spac trace' if the parser throws an exception

Attributes

Returns

A parser handler that can be used to eventually produce a result by calling its step and/or finish methods

Inherited from:
Parser
def stepMany[C[_], In2](inputs: C[In2])(implicit C: Unconsable[C]): Either[(Out, C[In2]), Handler[In, Out]]

Convenience function to call step on a sequence of inputs all at once. If the step returns a result, this method will return a Left containing that result and the remainder of the inputs that were not consumed. If the inputs run out before the handler returns a result from a step, this method will return a Right containing the latest state of the handler. This method will not call the handler's finish().

Convenience function to call step on a sequence of inputs all at once. If the step returns a result, this method will return a Left containing that result and the remainder of the inputs that were not consumed. If the inputs run out before the handler returns a result from a step, this method will return a Right containing the latest state of the handler. This method will not call the handler's finish().

In general, you won't call this method directly. Instead, use one of the Parser trait's parse methods.

Type parameters

C

An Unconsable collection, i.e. List or cats.data.Chain

In2

Subtype of In, or In (to satisfy contravariance)

Value parameters

C

Evidence that the inputs has a head/tail split operation

inputs

A sequence of inputs

Attributes

Returns

Either the handler's result paired with the remaining inputs, or the new handler state

Inherited from:
Handler
def unwrapSafe[T](implicit ev: A <:< Try[T]): Parser[In, T]

Creates a copy of this parser which unwraps the resulting Try, throwing an exception if the result was a Failure. This operation is the opposite of wrapSafe.

Creates a copy of this parser which unwraps the resulting Try, throwing an exception if the result was a Failure. This operation is the opposite of wrapSafe.

Attributes

Inherited from:
Parser
def upcast[Out2](implicit ev: A <:< Out2): Parser[In, Out2]

Returns this parser, with the output type widened to Out2, which is some supertype of Out. Uses asInstanceOf rather than creating a new parser.

Returns this parser, with the output type widened to Out2, which is some supertype of Out. Uses asInstanceOf rather than creating a new parser.

Attributes

Inherited from:
Parser
def withName(name: String): Parser[In, Out]

Creates a copy of this parser, but with a different toString

Creates a copy of this parser, but with a different toString

Value parameters

name

The new "name" (i.e. toString) for this parser

Attributes

Returns

A copy of this parser whose toString returns the given name

Inherited from:
Parser
def wrapSafe: Parser[In, Try[Out]]

Create a copy of this Parser whose handler will catch NonFatal exceptions thrown by the underlying logic. Caught exceptions will be yielded as a Failure output. Normal results will be wrapped in Success.

Create a copy of this Parser whose handler will catch NonFatal exceptions thrown by the underlying logic. Caught exceptions will be yielded as a Failure output. Normal results will be wrapped in Success.

Attributes

Returns

A copy of this parser that will return a Failure instead of throwing exceptions

Inherited from:
Parser
final def ||[In2](that: Parser[In2, Boolean])(implicit booleanOut: A <:< Boolean): Parser[In2, Boolean]

Operator version of or

Operator version of or

Attributes

See also

or

Inherited from:
Parser