Skip to content

Commit

Permalink
Updated build
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Apr 29, 2024
1 parent 8b3a1be commit 6afcae8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 8 additions & 4 deletions doc/intro.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Kaleidoscope is a small library which provides pattern matching using regular
expressions, and extraction of capturing groups into values, which are typed
according to the repetition of the group. Patterns can be written inline,
directly in a `case` pattern, and do not need to be predefined.
Kaleidoscope is a small library to make pattern matching against strings more pleasant. Regular
expressions can be written directly in patterns, and capturing groups bound directly to variables,
typed according to the group's repetition. Here is an example:
```amok scala
case class Email(user: Text, domain: Text)
email match
case r"$user([^@]+)@$domain(.*)" => Email(name, domain)
```
9 changes: 6 additions & 3 deletions src/core/kaleidoscope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ object Kaleidoscope:

extractor(parts.head :: parts.tail.map("([^/\\\\]*)"+_))

def regex(sc: Expr[StringContext])(using Quotes): Expr[Any] = extractor(sc.value.get.parts.to(List))
def regex(sc: Expr[StringContext])(using Quotes): Expr[Any] =
extractor(sc.value.get.parts.to(List))

private def extractor(parts: List[String])(using Quotes): Expr[Any] =
import quotes.reflect.*
Expand All @@ -65,7 +66,9 @@ object Kaleidoscope:

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)

def unapply(scrutinee: Text): Boolean =
Regex.make(List(pattern))(using Unsafe).matches(scrutinee)

class RExtractor[ResultType](parts: Seq[String]):
def unapply(scrutinee: Text): ResultType =
Expand All @@ -75,4 +78,4 @@ object Kaleidoscope:
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]
else result2.map(Tuple.fromArray(_)).asInstanceOf[ResultType]

0 comments on commit 6afcae8

Please sign in to comment.