From 52558379c85bef34f5a3b9984adc2ab55784ac27 Mon Sep 17 00:00:00 2001 From: mryouse Date: Wed, 13 Jul 2022 04:04:50 +0000 Subject: initial commit of a regex match? function --- libs/regex.neb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libs/regex.neb 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)) -- cgit v1.2.3