aboutsummaryrefslogtreecommitdiff
path: root/libs/regex.neb
diff options
context:
space:
mode:
authormryouse2022-07-13 04:04:50 +0000
committermryouse2022-07-13 04:04:50 +0000
commit52558379c85bef34f5a3b9984adc2ab55784ac27 (patch)
tree99b6783881c9f093094aef471d51d9c1e188e198 /libs/regex.neb
parent90926aeb2de71835d851a20d5e828066d1182c49 (diff)
initial commit of a regex match? function
Diffstat (limited to 'libs/regex.neb')
-rw-r--r--libs/regex.neb52
1 files changed, 52 insertions, 0 deletions
diff --git a/libs/regex.neb b/libs/regex.neb
new file mode 100644
index 0000000..e6bff34
--- /dev/null
+++ b/libs/regex.neb
@@ -0,0 +1,52 @@
+; regex
+; for doing regex matches
+
+(func match? :bool (pattern :string text :string)
+
+ (func match-one :bool (left right)
+ (in? left (list "" "." right)))
+
+ (func match-? :bool (pattern text)
+ (or
+ (and
+ (match-one (first-char pattern) (first-char text))
+ (match-inner (rest-char (rest-char pattern)) (rest-char text)))
+ (match-inner (rest-char (rest-char pattern)) text)))
+
+ (func match-* (pattern text)
+ (or
+ (and
+ (match-one (first-char pattern) (first-char text))
+ (match-inner pattern (rest-char text)))
+ (match-inner (rest-char (rest-char pattern)) text)))
+
+ (func match-inner (pattern text)
+ (branch
+ ((eq? "" pattern) #true)
+ ((and (eq? "$" pattern) (eq? "" text)) #true)
+ ((eq? "" text) #false)
+ ((and (>= (length pattern) 2) (eq? "?" (first-char (rest-char pattern))))
+ (match-? pattern text))
+ ((and (>= (length pattern) 2) (eq? "*" (first-char (rest-char pattern))))
+ (match-* pattern text))
+ (#true
+ (and
+ (match-one (first-char pattern) (first-char text))
+ (match-inner (rest-char pattern) (rest-char text))))))
+
+ (func search (pattern text)
+ (if (eq? "^" (first-char pattern))
+ (match-inner (rest-char pattern) text)
+ (block
+ (def progress text)
+ (def out (list))
+ (for-count (length text)
+ (redef out (append out (match-inner pattern progress)))
+ (redef progress (rest-char progress)))
+ (or
+ (eq? 0 (list-length out))
+ (if (eq? 1 (list-length out))
+ (first out)
+ (apply or out))))))
+
+ (search pattern text))