diff options
author | Leah Neukirchen <leah@vuxu.org> | 2022-10-14 15:55:30 +0200 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2022-10-14 15:55:30 +0200 |
commit | d97ec1b4b18eebf91bf7584d2599b2a94886e1fa (patch) | |
tree | 06d1865840e4648a69703b9b722890c365f35ae0 | |
parent | 45bac62d126418ef0f2b91aa17fad6f324eb0e5e (diff) | |
download | mew-d97ec1b4b18eebf91bf7584d2599b2a94886e1fa.tar.gz mew-d97ec1b4b18eebf91bf7584d2599b2a94886e1fa.tar.xz mew-d97ec1b4b18eebf91bf7584d2599b2a94886e1fa.zip |
add ~?, gmatch
-rw-r--r-- | mew.scm | 27 | ||||
-rw-r--r-- | mew.svnwiki | 16 |
2 files changed, 42 insertions, 1 deletions
diff --git a/mew.scm b/mew.scm index 2d29195..e72a623 100644 --- a/mew.scm +++ b/mew.scm @@ -1,4 +1,4 @@ -(module mew (at dec def div empty? eof esc fin final for generic-for-each get gfix giterate inc keys keyvals len loc mod nth op prn puts rep str tbl while until vals -> ->>) +(module mew (at dec def div empty? eof esc fin final for generic-for-each get gfix giterate gmatch inc keys keyvals len loc mod nth op prn puts rep str tbl while until vals -> ->> ~?) (import-for-syntax matchable) (import scheme @@ -25,6 +25,7 @@ (rename (matchable) (match-lambda match-fun) (match-lambda* match-fun*))) + (reexport (chicken irregex)) (reexport (only (chicken base) @@ -263,4 +264,28 @@ (syntax-rules () ((_ . rest) (-> . rest)))) + + (def (~? str pat) + (let ((data (irregex-search pat str))) + (if data + (map (op irregex-match-substring data _) + (iota (inc (irregex-match-num-submatches data)))) + #f))) + + (def (gmatch pat str) + (let ((start 0)) + (lambda () + (if (>= start 0) + (let ((data (irregex-search pat str start))) + (if data + (begin + (set! start (irregex-match-end-index data 0)) + (if (> (irregex-match-num-submatches data) 0) + (map (op irregex-match-substring data _) + (iota (inc (irregex-match-num-submatches data)))) + (irregex-match-substring data 0))) + (begin + (set! start -1) + (eof)))) + (eof))))) ) diff --git a/mew.svnwiki b/mew.svnwiki index b82d29a..979869f 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -8,6 +8,7 @@ Mew re-exports SRFI-1 (List library), SRFI-69 (Basic hash tables), SRFI-158 (Generators and Accumulators), +{{(chicken irregex)}}, and {{matchable}}. == Definitions, bindings and assignments @@ -197,6 +198,21 @@ a value {{equal?}} to the preceding one, then stops. Run the generator {{<gen>}} until it stops and return its final value. +== Regular expressions + +<procedure>(?~ <str> <irx>)</procedure> + +Matches the string {{<str>}} against the irregex (string or sexp) {{<irx>}} +without anchoring. Returns false on no match, else a list of all +match data strings. + +<procedure>(gmatch <irx> <str>)</procedure> + +Returns a generator that for each match of the irregex {{<irx>}} in +the string {{<str>}} either yields the match, or a list of all +match data strings (if there are any). + + == Special syntax <syntax>(-> a -> b c -> d e f)</syntax> |