name.rayrobdod.stringContextParserCombinator.typeclass
Implicit values used by branch combinators that allow combinations of input types to have more ergonomic return types.
Each of the typeclasses defined in this package fit into a matrix, where one dimension is which type of parser the typeclass is used with and the other dimension is the method that uses an instance of the type
method | Covariant (Interpolator) | Contravariant (Extractor) | Invariant (Parser) |
---|---|---|---|
andThen |
Sequenced | ContraSequenced | BiSequenced |
orElse |
Eithered | ContraEithered | BiEithered |
repeat |
Repeated | ContraRepeated | BiRepeated |
optionally |
Optionally | ContraOptionally | BiOptionally |
Thus, if you are only working with interpolators, then you'll only need to work with the unprefixed typeclasses.
Each of these traits has a companion object that defines a generic instance of the trait, and a few instances for more specific types. For instance, each typeclass includes a instance that will avoid wrapping scala.Unit
values in a collection or tuple.
Defining custom instances of these types is supported. Making custom given instances can significantly reduce the number of explicit map
calls required when writing a parser, however the usual advice with given instances applies: keep types specific, or keep the scope of a given instance to the minimum viable to prevent given instances from becoming confusing.
Attributes
Members list
Type members
Classlikes
Type parameters
- A
-
the first choice
- B
-
the second choice
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Eithered.scala
- Supertypes
Predefined implicit implementations of BiEithered and methods to create new BiEithereds
Predefined implicit implementations of BiEithered and methods to create new BiEithereds
Attributes
- Companion
- trait
- Source
- Eithered.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
BiEithered.type
Describes how to both represent and extract an optional value
Describes how to both represent and extract an optional value
Type parameters
- A
-
the optional input type
- Expr
-
the macro-level expression type
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Optionally.scala
- Supertypes
Predefined implicit implementations of BiOptionally and methods to create new BiOptionally
Predefined implicit implementations of BiOptionally and methods to create new BiOptionally
Attributes
- Companion
- trait
- Source
- Optionally.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
BiOptionally.type
Describes how to combine and break apart a repeated value
Describes how to combine and break apart a repeated value
Type parameters
- A
-
the repeated input elements
- Expr
-
the macro-level expression type
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Repeat.scala
- Supertypes
Predefined implicit implementations of BiRepeated
Predefined implicit implementations of BiRepeated
Attributes
- Companion
- trait
- Source
- Repeat.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
BiRepeated.type
Describes how to combine and separate two adjacent values
Describes how to combine and separate two adjacent values
Combining is defined by the methods inherited from Sequenced
, while separating is defined by the methods inherited from ContraSequenced
.
Type parameters
- A
-
the first value
- B
-
the second value
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Sequenced.scala
- Supertypes
Predefined implicit implementations of BiSequenced and methods to create new BiSequenceds
Predefined implicit implementations of BiSequenced and methods to create new BiSequenceds
Attributes
- Companion
- trait
- Source
- Sequenced.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
BiSequenced.type
Describes how to disambiguate the union of two types
Describes how to disambiguate the union of two types
The parser determines whether the left or right branch is taken. The return value's Expr[Boolean]
indicates whether the value matches the branch
Type parameters
- A
-
the first choice
- B
-
the second choice
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Eithered.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of ContraEithered and methods to create new ContraEithereds
Predefined implicit implementations of ContraEithered and methods to create new ContraEithereds
Attributes
- Companion
- trait
- Source
- Eithered.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ContraEithered.type
Describes how to extract an optional value
Describes how to extract an optional value
The parser determines whether the some or none branch is taken. The return values' Expr[Boolean]
indicates whether the value matches the branch
Type parameters
- A
-
the optional input type
- Expr
-
the macro-level expression type
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Optionally.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of ContraOptionally and methods to create new ContraOptionally
Predefined implicit implementations of ContraOptionally and methods to create new ContraOptionally
Attributes
- Companion
- trait
- Source
- Optionally.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ContraOptionally.type
Describes how to break apart a homogeneous sequence of zero-or-more values into its component parts.
Describes how to break apart a homogeneous sequence of zero-or-more values into its component parts.
The parser determines how many parts a value has The return value's Expr[Boolean]
indicates whether the value matches the branch
Type parameters
- A
-
the repeated input elements
- Expr
-
the macro-level expression type
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Repeat.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of ContraRepeated
Predefined implicit implementations of ContraRepeated
Attributes
- Companion
- trait
- Source
- Repeat.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ContraRepeated.type
Describes how to separate a value into two adjacent values
Describes how to separate a value into two adjacent values
Type parameters
- A
-
the first result
- B
-
the second result
- Z
-
the input value
Attributes
- See also
- Companion
- object
- Source
- Sequenced.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of ContraSequenced
Predefined implicit implementations of ContraSequenced
Attributes
- Companion
- trait
- Source
- Sequenced.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ContraSequenced.type
Describes how to represent a result that may be one of two results
Describes how to represent a result that may be one of two results
Below is example of defining and using a custom Eithered.
import java.io.File
import java.net.URI
import java.util.UUID
import name.rayrobdod.stringContextParserCombinator.Interpolator.idInterpolators._
import name.rayrobdod.stringContextParserCombinator.typeclass.Eithered
given Eithered[File, UUID, URI] with {
def left(f: File): URI = f.toURI
def right(id: UUID): URI = new URI("urn", "uuid:" + id.toString, null)
}
val uuidParser:Interpolator[UUID] = ofType[UUID]
val fileParser:Interpolator[File] = ofType[File]
val p:Interpolator[URI] = (fileParser:Interpolator[File]) orElse (uuidParser:Interpolator[UUID]) // using Eithered[File, UUID, URI]
p.interpolate(StringContext("", ""), new File("/tmp") :: Nil) // `file:///tmp`: URI
p.interpolate(StringContext("", ""), UUID.randomUUID() :: Nil) // `urn:uuid:429bf7eb-650e-4f8c-be3f-1420913a6bd7`: URI
Type parameters
- A
-
the first choice
- B
-
the second choice
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Eithered.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of Eithered and methods to create new Eithereds
Predefined implicit implementations of Eithered and methods to create new Eithereds
Attributes
- Companion
- trait
- Source
- Eithered.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Eithered.type
Describes how to represent an optional value
Describes how to represent an optional value
Type parameters
- A
-
the optional input type
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Optionally.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of Optionally and methods to create new Optionally
Predefined implicit implementations of Optionally and methods to create new Optionally
Attributes
- Companion
- trait
- Source
- Optionally.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Optionally.type
Describes how to combine a homogeneous sequence of zero-or-more values.
Describes how to combine a homogeneous sequence of zero-or-more values.
When a Repeated is used:
- first,
init
to create an initial value for the accumulator - then,
append
is called once for each component item in order, using the accumulator and the component item as parameters and returning the next accumulator value - lastly,
result
is called with the final accumulator value, and the result of this call is overall result.
init
will be called anew on each use, so it is possible to use a mutable accumulator by creating a new builder in the init
method and returning the acc
parameter in the append method.
Below is an example of implementing and using a custom Repeated
:
import name.rayrobdod.stringContextParserCombinator.Interpolator.charIn
import name.rayrobdod.stringContextParserCombinator.Interpolator.idInterpolators
import name.rayrobdod.stringContextParserCombinator.typeclass.Repeated
// define the marker types
case class Digit(value:Int)
case class Digits(value:Int)
// define the given instance
given Repeated[Digit, Digits] with {
type Acc = Int
def init():Acc = 0
def append(acc:Acc, elem:Digit):Acc = (acc * 10) + elem.value
def result(acc:Acc):Digits = new Digits(acc)
}
// create the parsers
val digit:idInterpolators.Interpolator[Digit] = charIn('0' to '9').map(x => Digit(x - '0'))
val digits:idInterpolators.Interpolator[Digits] = digit.repeat(1)// using Repeated[Digit, Digits]
// use the parser
digits.interpolate(StringContext("1234"), Nil) // Digits(1234): Digits
Type parameters
- A
-
the repeated input elements
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Repeat.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of Repeated
Predefined implicit implementations of Repeated
Attributes
- Companion
- trait
- Source
- Repeat.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Repeated.type
Describes how to combine two adjacent values into one value
Describes how to combine two adjacent values into one value
Below is example of defining and using a custom Sequenced:
import java.time._
import name.rayrobdod.stringContextParserCombinator.Interpolator.idInterpolators._
import name.rayrobdod.stringContextParserCombinator.typeclass.Sequenced
given Sequenced[LocalDate, LocalTime, LocalDateTime] with {
def aggregate(date:LocalDate, time:LocalTime):LocalDateTime = date.atTime(time)
}
val dateParser:Interpolator[LocalDate] = ofType[LocalDate]
val timeParser:Interpolator[LocalTime] = ofType[LocalTime]
val p: Interpolator[LocalDateTime] = dateParser andThen timeParser
p.interpolate(StringContext("", "", ""), LocalDate.of(2001, 02, 03) :: LocalTime.of(04, 05, 06) :: Nil) // `2001-02-03T04:05:06`: LocalDateTime
Type parameters
- A
-
the first input
- B
-
the second input
- Z
-
the result container
Attributes
- See also
- Companion
- object
- Source
- Sequenced.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
Predefined implicit implementations of Sequenced
Predefined implicit implementations of Sequenced
Attributes
- Companion
- trait
- Source
- Sequenced.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Sequenced.type
Associates an Expr type with the implicit types required to lift a value into an Expr. Support for Interpolator.mapToExpr
Associates an Expr type with the implicit types required to lift a value into an Expr. Support for Interpolator.mapToExpr
Attributes
- Companion
- object
- Source
- ToExprMapping.scala
- Supertypes
-
class Objecttrait Matchableclass Any
Predefined implicit implementations of ToExprMapping
Predefined implicit implementations of ToExprMapping
Attributes
- Companion
- trait
- Source
- ToExprMapping.scala
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ToExprMapping.type