From 129865dc11ee4441b71fe3e8539c01634f2f1df0 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 1 Apr 2017 18:38:42 -0400 Subject: - Added a macro for pattern-matching using regular-expressions. --- lux-mode/lux-mode.el | 2 +- stdlib/source/lux/lexer/regex.lux | 27 +++++++++++++++++++++++++-- stdlib/test/test/lux/lexer/regex.lux | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lux-mode/lux-mode.el b/lux-mode/lux-mode.el index 4ca40f257..93d30c767 100644 --- a/lux-mode/lux-mode.el +++ b/lux-mode/lux-mode.el @@ -221,7 +221,7 @@ Called by `imenu--generic-function'." "exec" "let" "let%" "if" "cond" "do" "be" "open" "loop" "recur" "comment" "list" "list&" "io" "vector" "tree" "get@" "set@" "update@" "|>" "|>." "<|" "_$" "$_" "~" "~@" "~'" "::" ":::" "default" "assume" "|" "&" "->" "All" "Ex" "Rec" "host" "$" "type" - "^" "^or" "^slots" "^stream&" "^=>" "^~" "^@" "^template" "^open" "^|>" + "^" "^or" "^slots" "^=>" "^~" "^@" "^template" "^open" "^|>" "^stream&" "^regex" "bin" "oct" "hex" "@pre" "@post" "sig" "struct" "derive" 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/" Monad])) [compiler #- run] (macro [ast] - [syntax #+ syntax:]) + ["s" syntax #+ syntax:]) ["&" lexer #+ Lexer Monad])) ## [Utils] @@ -415,7 +415,7 @@ (:: Monad 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)))) diff --git a/stdlib/test/test/lux/lexer/regex.lux b/stdlib/test/test/lux/lexer/regex.lux index c5c21df2a..1a21111f8 100644 --- a/stdlib/test/test/lux/lexer/regex.lux +++ b/stdlib/test/test/lux/lexer/regex.lux @@ -267,3 +267,19 @@ (&;regex "(\\d{3})-((\\d{3})-(\\d{4}))|b(.)d") "809-345-6789"))) )) + +(test: "Pattern-matching" + [sample1 (R;text +3) + sample2 (R;text +3) + sample3 (R;text +4)] + (case (format sample1 "-" sample2 "-" sample3) + (&;^regex "(.{3})-(.{3})-(.{4})" + [_ match1 match2 match3]) + (assert "Can pattern-match using regular-expressions." + (and (T/= sample1 match1) + (T/= sample2 match2) + (T/= sample3 match3))) + + _ + (assert "Cannot pattern-match using regular-expressions." + false))) -- cgit v1.2.3