Packages

  • package root
    Definition Classes
    root
  • package io
    Definition Classes
    root
  • package dylemma
    Definition Classes
    io
  • package spac

    SPaC (short for "Streaming Parser Combinators") is a library for building stream consumers in a declarative style, specialized for tree-like data types like XML and JSON.

    SPaC (short for "Streaming Parser Combinators") is a library for building stream consumers in a declarative style, specialized for tree-like data types like XML and JSON.

    Many utilities for handling XML and JSON data involve parsing the entire "document" to some DOM model, then inspecting and transforming that model to extract information. The downside to these utilities is that when the document is very large, the DOM may not fit in memory. The workaround for this type of problem is to treat the document as a stream of "events", e.g. "StartElement" and "EndElement" for XML, or "StartObject" and "EndObject" for JSON. The downside to this workaround is that writing code to handle these streams can be complicated and error-prone, especially when the DOM is complicated.

    SPaC's goal is to drastically simplify the process of creating code to handle these streams.

    This package contains the "core" SPaC traits; Parser, Transformer, Splitter, and ContextMatcher.

    See the xml and json subpackages (provided by the xml-spac and json-spac libraries respectively) for specific utilities related to handling XML and JSON event streams.

    Definition Classes
    dylemma
  • package interop
    Definition Classes
    spac
  • package json

    This package provides extensions to the core "spac" library which allow for the handling of JSON data.

    This package provides extensions to the core "spac" library which allow for the handling of JSON data.

    Rather than creating explicit classes that extend Parser, Transformer, and Splitter, this package provides type aliases and implicit extensions. For example, JsonParser[A] is just a type alias for Parser[JsonEvent, A], and JsonParser is just a call to Parser[JsonEvent].

    Implicit JsonParsers are available for each of the JSON primitive types:

    • string
    • number (expressed as Int, Long, Float, or Double)
    • boolean
    • null (expressed as None.type)

    Helpers are available for parsing JSON arrays and objects:

    • JsonParser.listOf[A] to parse an array where each value is an A
    • JsonParser.objectOf[A] to parse an object where the value for each field an A
    • JsonParser.objectOfNullable[A] to parse an object where the value for each field is either null or an A, filtering out the nulls
    • JsonParser.fieldOf[A](fieldName) to parse a specific field from an object

    A DSL for creating json-specific ContextMatchers is provided to make it more convenient to call Splitter.json. For example:

    Splitter.json("foo" \ "bar").as[String].parseFirst

    Can be used to capture rootJson.foo.bar as a String in

    {
      "foo": {
        "bar": "hello"
      }
    }

    To "split" values inside arrays, index-related context matchers are available, e.g.

    Splitter.json("foo" \ anyIndex).as[Int].parseToList

    Can be used to capture each of the numbers in the "foo" array in

    {
      "foo": [1, 2, 3]
    }

    A note about JsonEvents in spac: JSON doesn't have any explicit markers for when a field ends, or when an array index starts or ends; those context changes are essentially inferred by the presence of some other event. For example, instead of a "field end" event, typically there will be either a new "field start" or a token representing the end of the current object. With spac, splitters and context matchers generally operate under the assumption that a "stack push" event (like a field start) will eventually be followed by a corresponding "stack pop" event (i.e. field end).

    To allow for this, these "inferred" events (FieldEnd, IndexStart, IndexEnd) are explicitly represented as JsonEvents in the stream being parsed. Keep this in mind when creating JSON ContextMatchers:

    • field-related matchers will match a stack like case ObjectStart :: FieldStart(_) :: _
    • index-related matchers will match a stack like case ArrayStart :: IndexStart(_) :: _
    Definition Classes
    spac
  • package xml

    This package provides extensions to the core "spac" library which allow for the handling of XML data.

    This package provides extensions to the core "spac" library which allow for the handling of XML data.

    Rather than creating explicit classes that extend Parser, Transformer, and Splitter, this package provides type aliases and implicit extensions. For example, XmlParser[A] is just a type alias for Parser[XmlEvent, A], and XmlParser is just a call to Parser[XmlEvent].

    Three main Parser methods are added to Parser[XmlEvent] via the XmlParserApplyOps implicit class:

    • XmlParser.forText - for capturing raw text
    • XmlParser.attr - for capturing mandatory attributes from elements
    • XmlParser.attrOpt - for capturing optional attributes from elements

    One main Splitter constructor method is added to Splitter via the XmlSplitterApplyOps implicit class:

    • Splitter.xml - for creating splitters based on an inspection of an "element stack"

    Three main Splitter member methods are added to Splitter[XmlEvent, C] via the XmlSplitterOps implicit class:

    • .attr - alias for .joinBy(XmlParser.attr(...))
    • .attrOpt - alias for .joinBy(XmlParser.attrOpt(...))
    • .text - alias for .joinBy(XmlParser.forText)

    A DSL for creating xml-specific ContextMatchers is provided to make it more convenient to call Splitter.xml. For example:

    Splitter.xml("things" \ "thing").attr("foo").parseToList

    Can be used to capture a list of the "foo" attributes in the <thing> elements in

    <things>
       <thing foo="hello" />
       <thing foo="Goodbye">
          <extra>junk</extra>
       </thing>
    </thing>
    Definition Classes
    spac
  • CallerPos
  • ContextChange
  • ContextLocation
  • ContextMatcher
  • ContextPop
  • ContextPush
  • ContextTrace
  • HasLocation
  • LowPriorityTypeReduceImplicits
  • Parser
  • ParserApplyWithBoundInput
  • Signal
  • SingleItemContextMatcher
  • Source
  • SpacException
  • SpacTraceElement
  • Splitter
  • SplitterApplyWithBoundInput
  • StackInterpretation
  • StackLike
  • Transformer
  • TransformerApplyWithBoundInput
  • TypeReduce
  • Unconsable

trait Transformer[-In, +Out] extends AnyRef

Primary "spac" abstraction which represents a transformation stage for a stream of data events

Transformers effectively transform a stream of In events into a stream of Out events. The actual stream handling logic is defined by a Transformer.Handler, which a Transformer is responsible for constructing. Handlers may be internally-mutable, and so they are generally only constructed by other handlers. Transformers themselves are immutable, acting as "handler factories", and so they may be freely reused.

A transformer may choose to abort in response to any input event, as well as emit any number of outputs in response to an input event or the EOF signal.

In

The incoming event type

Out

The outgoing event type

Source
Transformer.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. Transformer
  2. AnyRef
  3. Any
Implicitly
  1. by TransformerKVParsingOps
  2. by TransformerParsingOps
  3. by any2stringadd
  4. by StringFormat
  5. by Ensuring
  6. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def newHandler: Handler[In, Out]

    Transformer's main abstract method; constructs a new Handler representing this transformer's logic.

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

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toany2stringadd[Transformer[In, Out]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (Transformer[In, Out], B)
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toArrowAssoc[Transformer[In, Out]] performed by method ArrowAssoc in scala.Predef.This conversion will take place only if Out is a subclass of (Nothing, Nothing) (Out <: (Nothing, Nothing)).
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def cast[Out2](implicit ev: <:<[Out, Out2]): Transformer[In, Out2]

    Returns this transformer, but with a different view of the Out type.

    Returns this transformer, but with a different view of the Out type. The Out <:< Out2 implicit evidence is used to make sure the asInstanceOf cast is safe. This is mostly useful when you know you have a transformer that yields a tuple or some kind of type constructor.

  8. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  9. def collect[Out2](pf: PartialFunction[Out, Out2]): Transformer[In, Out2]

    Creates a new transformer which filters and maps the outputs from this transformer

    Creates a new transformer which filters and maps the outputs from this transformer

    Out2

    Result type of the pf

    pf

    Partial function responsible for the filtering and mapping of outputs from this transformer

    returns

    The filteried and mapped transformer

  10. def drain: Parser[In, Unit]

    Convenience for this into Parser.drain

  11. def ensuring(cond: (Transformer[In, Out]) => Boolean, msg: => Any): Transformer[In, Out]
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toEnsuring[Transformer[In, Out]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: (Transformer[In, Out]) => Boolean): Transformer[In, Out]
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toEnsuring[Transformer[In, Out]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: Boolean, msg: => Any): Transformer[In, Out]
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toEnsuring[Transformer[In, Out]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: Boolean): Transformer[In, Out]
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toEnsuring[Transformer[In, Out]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  15. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  17. def filter(predicate: (Out) => Boolean): Transformer[In, Out]

    Creates a new transformer which filters the outputs from this transformer.

    Creates a new transformer which filters the outputs from this transformer.

    predicate

    A function which decides whether an output from this transformer should be emitted from the returned transformer. true means emit, false means skip.

    returns

    The filtered transformer

  18. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  19. def formatted(fmtstr: String): String
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toStringFormat[Transformer[In, Out]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  20. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  21. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  22. def into[Out2](parser: Parser[Out, Out2]): Parser[In, Out2]

    Attach this transformer to a parser, creating a new parser that encapsulates the pair.

    Attach this transformer to a parser, creating a new parser that encapsulates the pair. Values emitted from this transformer will be passed as inputs to the parser, and the resulting output from the parser will be yielded as output by the combined parser.

  23. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  24. def map[Out2](f: (Out) => Out2): Transformer[In, Out2]

    Creates a new transformer which applies the transformation function f to each of this transformer's outputs.

    Creates a new transformer which applies the transformation function f to each of this transformer's outputs.

    Out2

    The transformation output type

    f

    A transformation function

    returns

    The mapped transformer

  25. def mapFlatten[Out2](f: (Out) => Iterable[Out2]): Transformer[In, Out2]

    Creates a new transformer which transforms the outputs of this transformer via the given function f, emitting each individual value from the output of that function in order before continuing.

    Creates a new transformer which transforms the outputs of this transformer via the given function f, emitting each individual value from the output of that function in order before continuing.

    Out2

    The transformed output type

    f

    A function that transforms outputs from this transformer into a collection of other outputs

    returns

    A new transformer which emits any number of transformed outputs based on outputs from this transformer

  26. def merge[In2 <: In, Out2 >: Out](that: Transformer[In2, Out2]): Transformer[In2, Out2]

    Like mergeEither, but when both sides have a common output type.

    Like mergeEither, but when both sides have a common output type. This is a less-roundabout way of doing .mergeEither(right).map(_.merge). The same order-of-operations rules apply as with mergeEither, where this transformer "goes first" for each input.

    In2

    Contravariance-friendly version of In

    Out2

    Common output type between this and that

    that

    Another transformer

    returns

    The merged transformer

  27. def mergeEither[In2 <: In, Out2](right: Transformer[In2, Out2]): Transformer[In2, Either[Out, Out2]]

    Creates a new transformer which sends inputs to both this transformer and the right transformer.

    Creates a new transformer which sends inputs to both this transformer and the right transformer. Whenever either this or right emit a value, that value will be emitted from the returned transformer, wrapped as a Left or Right depending on which underlying transformer emitted it. For each individual input, the resulting values emitted by this transformer will be emitted before the resulting values emitted by the right transformer.

    In2

    Contravariance-friendly version of In

    Out2

    The output type of the right transformer

    right

    Another transformer

    returns

    The merged transformer

  28. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  29. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  30. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  31. def parseAsFold[Out2](init: Out2)(f: (Out2, Out) => Out2): Parser[In, Out2]

    Convenience for this into Parser.fold(init)(f)

  32. def parseFirst(implicit A: TypeName[Out]): Parser[In, Out]

    Convenience for this into Parser.first

    Convenience for this into Parser.first

    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toTransformerParsingOps[In, Out] performed by method TransformerParsingOps in io.dylemma.spac.Transformer.
    Definition Classes
    TransformerParsingOps
  33. def parseFirstOpt: Parser[In, Option[Out]]

    Convenience for this into Parser.firstOpt

  34. def parseTap(f: (Out) => Unit): Parser[In, Unit]

    Convenience for this into Parser.tap(f)

  35. def parseToList: Parser[In, List[Out]]

    Convenience for this into Parser.toList

  36. def parseToMap: Parser[In, Map[K, V]]

    Convenience for this into Parser.toMap[K, V]

    Convenience for this into Parser.toMap[K, V]

    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toTransformerKVParsingOps[In, K, V] performed by method TransformerKVParsingOps in io.dylemma.spac.Transformer.This conversion will take place only if Out is a subclass of (K, V) (Out <: (K, V)).
    Definition Classes
    TransformerKVParsingOps
  37. def scan[Out2](init: Out2)(op: (Out2, Out) => Out2): Transformer[In, Out2]

    Creates a new transformer which folds outputs from this transformer into a "state" which is emitted each time.

    Creates a new transformer which folds outputs from this transformer into a "state" which is emitted each time.

    Out2

    The type of the scan "state"

    init

    The initial "state"

    op

    State update function; this is called for each Out emitted by this transformer, and the result is emitted by the combined transformer in addition to becoming the next "state"

    returns

    The new transformer

  38. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  39. def through[Out2](next: Transformer[Out, Out2]): Transformer[In, Out2]

    Attach this transformer to the next transformer, creating a single transformer that encapsulates the pair.

    Attach this transformer to the next transformer, creating a single transformer that encapsulates the pair. Values emitted from this transformer will be passed as inputs to the next transformer, and the resulting outputs from the next transformer are emitted as outputs from the combined transformer.

  40. def toString(): String
    Definition Classes
    AnyRef → Any
  41. def transform(source: Source[In])(implicit pos: CallerPos): Source[Out]

    Applies this transformer's logic to a Source, returning a new Source which yields values emitted by this transformer when run on the underlying iterator.

    Applies this transformer's logic to a Source, returning a new Source which yields values emitted by this transformer when run on the underlying iterator.

    source

    A Source

    pos

    Captures the call site for the top level SpacTraceElement

    returns

    A wrapped version of source, transformed via this transformer

  42. def transform(itr: Iterator[In])(implicit pos: CallerPos): Iterator[Out]

    Applies this transformer's logic to an iterator, returning a new Iterator which yields values emitted by this transformer when run on the underlying itr.

    Applies this transformer's logic to an iterator, returning a new Iterator which yields values emitted by this transformer when run on the underlying itr.

    itr

    An iterator

    returns

    A wrapped version of itr, transformed via this transformer

  43. def upcast[In2 <: In, Out2 >: Out]: Transformer[In2, Out2]

    Returns this transformer, but with less restricted In / Out types.

    Returns this transformer, but with less restricted In / Out types.

    In2

    A subtype of In

    Out2

    A supertype of Out

    returns

    This transformer (not a copy, it's actually literally this)

  44. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  45. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  46. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  47. def withFilter(predicate: (Out) => Boolean): Transformer[In, Out]

    Alias for filter, used under the hood by for-comprehensions

    Alias for filter, used under the hood by for-comprehensions

    predicate

    The filtering function

    returns

    The filtered transformer

  48. def withName(name: String): Transformer[In, Out]

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

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

    name

    The new "name" (i.e. toString for this transformer

    returns

    A copy of this transformer whose toString returns the given name

Deprecated Value Members

  1. def >>[Out2](parser: Parser[Out, Out2]): Parser[In, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) Due to troubles with operator precedence and type inference, this operator is being phased out in favor of into

  2. def >>[Out2](next: Transformer[Out, Out2]): Transformer[In, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) Due to troubles with operator precedence and type inference, this operator is being phased out in favor of through

  3. def andThen[Out2](next: Transformer[Out, Out2]): Transformer[In, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to through

  4. def parallel[In2 <: In, Out2 >: Out](that: Transformer[In2, Out2]): Transformer[In2, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to merge

  5. def parallelEither[In2 <: In, Out2](right: Transformer[In2, Out2]): Transformer[In2, Either[Out, Out2]]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to mergeEither

  6. def parseFirstOption: Parser[In, Option[Out]]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to parseFirstOpt

  7. def parseForeach(f: (Out) => Any): Parser[In, Unit]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to parseTap

  8. def parseWith[Out2](parser: Parser[Out, Out2], setDebugName: Option[String]): Parser[In, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) Use the single-argument version of into, then call withName on the resulting parser

  9. def parseWith[Out2](parser: Parser[Out, Out2]): Parser[In, Out2]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) Use into instead

  10. def sink: Parser[In, Unit]
    Annotations
    @deprecated
    Deprecated

    (Since version v0.9) This method is being renamed to drain

  11. def [B](y: B): (Transformer[In, Out], B)
    Implicit
    This member is added by an implicit conversion from Transformer[In, Out] toArrowAssoc[Transformer[In, Out]] performed by method ArrowAssoc in scala.Predef.This conversion will take place only if Out is a subclass of (Nothing, Nothing) (Out <: (Nothing, Nothing)).
    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.

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion TransformerKVParsingOps fromTransformer[In, Out] to TransformerKVParsingOps[In, K, V]

Inherited by implicit conversion TransformerParsingOps fromTransformer[In, Out] to TransformerParsingOps[In, Out]

Inherited by implicit conversion any2stringadd fromTransformer[In, Out] to any2stringadd[Transformer[In, Out]]

Inherited by implicit conversion StringFormat fromTransformer[In, Out] to StringFormat[Transformer[In, Out]]

Inherited by implicit conversion Ensuring fromTransformer[In, Out] to Ensuring[Transformer[In, Out]]

Inherited by implicit conversion ArrowAssoc fromTransformer[In, Out] to ArrowAssoc[Transformer[In, Out]]

Abstract Members

Applying a Transformer to a Stream

Transformation / Combinator Methods

Conversions to Parser

Ungrouped