aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source
diff options
context:
space:
mode:
authorEduardo Julian2017-04-01 18:38:42 -0400
committerEduardo Julian2017-04-01 18:38:42 -0400
commit129865dc11ee4441b71fe3e8539c01634f2f1df0 (patch)
treec180706df7f81308274abaa844c90591908b73ef /stdlib/source
parentae7c062bdf4ab8337f0eedae78b38df24e62822c (diff)
- Added a macro for pattern-matching using regular-expressions.
Diffstat (limited to 'stdlib/source')
-rw-r--r--stdlib/source/lux/lexer/regex.lux27
1 files changed, 25 insertions, 2 deletions
diff --git a/stdlib/source/lux/lexer/regex.lux b/stdlib/source/lux/lexer/regex.lux
index c41e38590..616f02086 100644
--- a/stdlib/source/lux/lexer/regex.lux
+++ b/stdlib/source/lux/lexer/regex.lux
@@ -9,7 +9,7 @@
(coll [list "" Fold<List> "List/" Monad<List>]))
[compiler #- run]
(macro [ast]
- [syntax #+ syntax:])
+ ["s" syntax #+ syntax:])
["&" lexer #+ Lexer Monad<Lexer>]))
## [Utils]
@@ -415,7 +415,7 @@
(:: Monad<Lexer> map product;right (re-alternative^ true re-scoped^ current-module)))
## [Syntax]
-(syntax: #export (regex [pattern syntax;text])
+(syntax: #export (regex [pattern s;text])
{#;doc (doc "Create lexers using regular-expression syntax."
"For example:"
@@ -484,3 +484,26 @@
(#;Right regex)
(wrap (list regex))
)))
+
+(syntax: #export (^regex [[pattern bindings] (s;form (s;seq s;text (s;opt s;any)))]
+ body
+ [branches (s;many s;any)])
+ {#;doc (doc "Allows you to test text against regular expressions."
+ (case some-text
+ (^regex "(\\d{3})-(\\d{3})-(\\d{4})"
+ [_ country-code area-code place-code])
+ do-some-thing-when-number
+
+ (^regex "\\w+")
+ do-some-thing-when-word
+
+ _
+ do-something-else))}
+ (do @
+ [g!temp (compiler;gensym "temp")]
+ (wrap (list& (` (^=> (~ g!temp)
+ [(&;run (~ g!temp) (regex (~ (ast;text pattern))))
+ (#;Right (~ (default g!temp
+ bindings)))]))
+ body
+ branches))))