Skip to content

Commit

Permalink
"Hide" Extractor as RExtractor inside Kaleidoscope object
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Mar 18, 2024
1 parent d9de945 commit 2d2da7e
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/core/kaleidoscope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ extension (inline ctx: StringContext)
transparent inline def r: Any = ${Kaleidoscope.regex('ctx)}
transparent inline def g: Any = ${Kaleidoscope.glob('ctx)}

class NoExtraction(pattern: String):
inline def apply(): Regex = Regex.make(List(pattern))(using Unsafe)
def unapply(scrutinee: Text): Boolean = Regex.make(List(pattern))(using Unsafe).matches(scrutinee)

class Extractor[ResultType](parts: Seq[String]):
def unapply(scrutinee: Text): ResultType =
val result = Regex.make(parts)(using Unsafe).matchGroups(scrutinee)

// FIXME: [#39] Stop using `Array` when capture checking is working again
val result2 = result.asInstanceOf[Option[Array[Text | List[Text] | Option[Text]]]]

if parts.length == 2 then result2.map(_.head).asInstanceOf[ResultType]
else result2.map(Tuple.fromArray(_)).asInstanceOf[ResultType]

object Kaleidoscope:
given Realm = realm"kaleidoscope"

Expand Down Expand Up @@ -75,4 +61,18 @@ object Kaleidoscope:

if types.length == 0 then '{NoExtraction(${Expr(parts.head)})}
else (tupleType.asType: @unchecked) match
case '[resultType] => '{Extractor[Option[resultType]](${Expr(parts)})}
case '[resultType] => '{RExtractor[Option[resultType]](${Expr(parts)})}

class NoExtraction(pattern: String):
inline def apply(): Regex = Regex.make(List(pattern))(using Unsafe)
def unapply(scrutinee: Text): Boolean = Regex.make(List(pattern))(using Unsafe).matches(scrutinee)

class RExtractor[ResultType](parts: Seq[String]):
def unapply(scrutinee: Text): ResultType =
val result = Regex.make(parts)(using Unsafe).matchGroups(scrutinee)

// FIXME: [#39] Stop using `Array` when capture checking is working again
val result2 = result.asInstanceOf[Option[Array[Text | List[Text] | Option[Text]]]]

if parts.length == 2 then result2.map(_.head).asInstanceOf[ResultType]
else result2.map(Tuple.fromArray(_)).asInstanceOf[ResultType]

0 comments on commit 2d2da7e

Please sign in to comment.