trait Parser[-In, +Out] extends AnyRef
Primary "spac" abstraction which represents a sink for data events.
Parsers are responsible for interpreting a stream of In events as a single result of type Out.
The actual interpretation is performed by a Parser.Handler which the Parser is responsible for constructing.
Handlers may be internally-mutable, and so they are generally only constructed by the parse helper methods or by other handlers.
Parsers themselves are immutable, acting as "handler factories", and so they may be freely reused.
A parser differs from typical "fold" operations in that it may choose to abort early with a result, leaving the remainder of the data stream untouched.
- In
event/input type
- Out
result type
- Self Type
- Parser[In, Out]
- Source
- Parser.scala
- Grouped
- Alphabetic
- By Inheritance
- Parser
- AnyRef
- Any
- by ParserFollowedByOps
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Abstract Value Members
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (Parser[In, Out], B)
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def asTransformer: Transformer[In, Out]
Represent this parser as a
Transformerwhich emits this parser's result - def attempt: Parser[In, Either[Throwable, Out]]
Like
wrapSafe, but represents exceptions asLeftand successful results asRight - def beforeContext[I2 <: In, StackElem](matcher: ContextMatcher[StackElem, Any])(implicit stackable: StackLike[I2, StackElem], pos: CallerPos): Parser[I2, Out]
Specialization of
interruptedByfor stack-like input types, such that an interruption will occur upon entering a stack context that can be matched by the givenmatcher.Specialization of
interruptedByfor stack-like input types, such that an interruption will occur upon entering a stack context that can be matched by the givenmatcher.Example:
val preludeContext = * \ "prelude" val dataContext = * \ "data" for { prelude <- Splitter(preludeContext).firstOption[Prelude].beforeContext(dataContext).followedByStream data <- Splitter(dataContext).as[Data] } yield data
- I2
Subtype of
In, or justIn(to satisfy contravariance of Parser'sIntype)- StackElem
Specialization of the
Intype for when it represents a stack push or pop- matcher
A matching function that operates on a context stack
- stackable
Interprets the inputs as stack push/pop events to accumulate a context stack
- returns
A parser which will perform an early
finish()when a matching context is encountered
- def clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def ensuring(cond: (Parser[In, Out]) => Boolean, msg: => Any): Parser[In, Out]
- def ensuring(cond: (Parser[In, Out]) => Boolean): Parser[In, Out]
- def ensuring(cond: Boolean, msg: => Any): Parser[In, Out]
- def ensuring(cond: Boolean): Parser[In, Out]
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def expectInputs[I2 <: In](expectations: List[(String, (I2) => Boolean)]): Parser[I2, Out]
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 returnstrue, 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.- expectations
A sequence of
label -> testexpectations imposed on inputs to this parser- returns
A copy of this parser with expectations imposed on its inputs
- def finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def followedBy: FollowedBy[In, Out, Parser]
Intermediate object for creating a sequenced 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 sequenced 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
Invalues) will be fed into this Parser until this parser's handler returns a result of typeOut. At that point, the second parser (as specified by using theapplyorflatMapmethods on theFollowedByreturned by this method) will be instantiated. Any relevant "stack events" (seeStackable) will be replayed so the second parser has the right context, and from that point on, allInvalues will be sent to the second parser. When that second parser returns a result, that result becomes the output of the combined parser created bythis.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 Parser's
interruptedBy, which is useful when atransformer.parseFirstOptionmust befollowedBysome other parser.- Implicit
- This member is added by an implicit conversion from Parser[In, Out] toParserFollowedByOps[In, Out] performed by method ParserFollowedByOps in io.dylemma.spac.Parser.
- Definition Classes
- ParserFollowedByOps
- def followedByParser: FollowedBy[In, Out, Parser]
Alias for
followedBy, for use when Cat'sApplyOpsgets in the way with its own uselessfollowedBymethod.Alias for
followedBy, for use when Cat'sApplyOpsgets in the way with its own uselessfollowedBymethod.- Implicit
- This member is added by an implicit conversion from Parser[In, Out] toParserFollowedByOps[In, Out] performed by method ParserFollowedByOps in io.dylemma.spac.Parser.
- Definition Classes
- ParserFollowedByOps
- def followedByStream: FollowedBy[In, Out, Transformer]
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
followedByfor a general explanation of how the combination works.See also,
interruptedBy, which is useful when atransformer.parseFirstOptionmust befollowedBysome other transformer.- Implicit
- This member is added by an implicit conversion from Parser[In, Out] toParserFollowedByOps[In, Out] performed by method ParserFollowedByOps in io.dylemma.spac.Parser.
- Definition Classes
- ParserFollowedByOps
- def formatted(fmtstr: String): String
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def interruptedBy[I2 <: In](interrupter: Parser[I2, Any]): Parser[I2, Out]
Create a copy of this parser that will treat a result from the
interrupteras an early EOF.Create a copy of this parser that will treat a result from the
interrupteras an early EOF. This is especially useful for creatingfollowedBychains 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,
followedBylogic won't work since thefollowUpparser/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#beforeContextprovides a convenience for doing so.Note that if the
interrupterthrows an exception, that exception will not be caught. If your interrupter might throw, passinterrupter.wrapSafeinstead to swallow the exception.- I2
Subtype of
In, or justIn(to satisfy contravariance of Parser'sIntype)- 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().- returns
A parser which will perform an early
finish()call when theinterrupterproduces a result.
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def map[Out2](f: (Out) => 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.- Out2
The new parser's result type
- f
Result transformation function
- 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[In2 <: In, Out2 >: Out](fallback: Parser[In2, Out2]): Parser[In2, Out2]
Combine this parser with the
fallbacksuch that failures from the underlying parsers will be ignored as long as at least one succeeds.Combine this parser with the
fallbacksuch 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, aSpacException.FallbackChainFailurewill be thrown by the returned parser's handler.- In2
Subtype of
In, or justIn(to satisfy Parser's contravariance on theIntype)- Out2
Supertype of
Out, or justOut(to satisfy Parser's covariance on theOuttype)- fallback
another parser of the same(ish) type as this one
- returns
A new parser that will succeed if either this parser or the fallback succeed
- def parse(source: Source[In])(implicit pos: CallerPos): Out
Consume the given
sourceto produce an output or possibly throw aSpacException.Consume the given
sourceto produce an output or possibly throw aSpacException.The
Source[A]type is likeIterable[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
Sourceconstructors are provided by the "parser backend" libraries i.e.xml-spac-javaxandjson-spac-jackson.- source
An object that can provide a series of
Invalues, e.g.XmlEventorJsonEvent- pos
Captures the caller filename and line number, used to fill in the 'spac trace' if the parser throws an exception
- returns
The parser result based on the given
source
- def parse(inputs: Iterator[In])(implicit pos: CallerPos): Out
Consume the given
inputsiterator to produce an output or possibly throw aSpacException.Consume the given
inputsiterator to produce an output or possibly throw aSpacException.After calling this method, the
inputsshould be discarded, since consuming an Iterator is a destructive operation.- inputs
A series of
Invalues, e.g.XmlEventorJsonEvent- pos
Captures the caller filename and line number, used to fill in the 'spac trace' if the parser throws an exception
- returns
The parser result based on the given
inputs
- Annotations
- @throws(scala.this.throws.<init>$default$1[io.dylemma.spac.SpacException[_]])
- def rethrow[T](implicit ev: <:<[Out, Either[Throwable, T]]): Parser[In, T]
Like
unwrapSafe, but rethrows exceptions fromLeftor returns results fromRight.Like
unwrapSafe, but rethrows exceptions fromLeftor returns results fromRight. This operation is the opposite ofattempt. - def start(methodName: String = "start")(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
parsemethods. Start with this method if you have some sequence-like datatype that doesn't provide anIterator.This is just a convenience for
newHandler.asTopLevelhandlerwhich helps construct a useful SpacTraceElement.- 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
- returns
A parser handler that can be used to eventually produce a result by calling its
stepand/orfinishmethods
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unwrapSafe[T](implicit ev: <:<[Out, Try[T]]): Parser[In, T]
Creates a copy of this parser which unwraps the resulting
Try, throwing an exception if the result was aFailure.Creates a copy of this parser which unwraps the resulting
Try, throwing an exception if the result was aFailure. This operation is the opposite ofwrapSafe. - def upcast[Out2](implicit ev: <:<[Out, Out2]): Parser[In, Out2]
Returns this parser, with the output type widened to
Out2, which is some supertype ofOut.Returns this parser, with the output type widened to
Out2, which is some supertype ofOut. UsesasInstanceOfrather than creating a new parser. - final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- 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()
- def withName(name: String): Parser[In, Out]
Creates a copy of this parser, but with a different
toStringCreates a copy of this parser, but with a different
toString- name
The new "name" (i.e.
toString) for this parser- returns
A copy of this parser whose
toStringreturns the givenname
- def wrapSafe: Parser[In, Try[Out]]
Create a copy of this Parser whose handler will catch NonFatal exceptions thrown by the underlying logic.
Create a copy of this Parser whose handler will catch NonFatal exceptions thrown by the underlying logic. Caught exceptions will be yielded as a
Failureoutput. Normal results will be wrapped inSuccess.- returns
A copy of this parser that will return a
Failureinstead of throwing exceptions
Deprecated Value Members
- def →[B](y: B): (Parser[In, Out], B)
- Implicit
- This member is added by an implicit conversion from Parser[In, Out] toArrowAssoc[Parser[In, Out]] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @deprecated
- Deprecated
(Since version 2.13.0) Use
->instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.