cross-platform match function in re-flex

This commit is contained in:
2021-10-27 15:59:20 -04:00
parent 598891af43
commit e635d60c55
7 changed files with 58 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
test.js

View File

@@ -1 +1,7 @@
(print "Hello world!")
(let [match (R.match "a" "12abc")]
(case match
((object
left "12"
match "a"
right "bc"))
(otherwise (throw "bad match result"))))

View File

@@ -2,6 +2,21 @@ package re_flex;
import kiss.Prelude;
import kiss.List;
#if hxnodejs
import js.lib.RegExp;
import haxe.DynamicAccess;
#end
typedef RMatch = {
left:String,
match:String,
right:String,
#if hxnodejs
groups:Array<String>,
namedGroups:DynamicAccess<String,String>,
#end
group:Int->String
};
@:build(kiss.Kiss.build())
class R {}

View File

@@ -27,3 +27,32 @@
[])
(true
(for _ (range (- max min)) (oneOf expr "")))))))
// TODO could memoize compiled regexes
(function :Null<RMatch> match [:String regex :String textToSearch &opt :String flags]
(#if hxnodejs
(ifLet [:RegExpMatch match (.exec (new RegExp regex /* TODO use or for this once it's fixed */ (if flags flags "")) textToSearch)]
(object
left
(textToSearch.substr 0 match.index)
match
(first match)
right
(textToSearch.substr (+ match.index .length (first match)))
group
->idx (nth match idx)
groups
match
namedGroups
match.groups))
(let [r (new EReg regex /*TODO use or for this once fixed */(if flags flags ""))]
(if (r.match textToSearch)
(object
left
(r.matchedLeft)
match
(r.matched 0)
right
(r.matchedRight)
group
->idx (r.matched idx))))))

5
test-js.hxml Normal file
View File

@@ -0,0 +1,5 @@
-lib kiss
-cp src
--main re_flex.Main
--js test.js
--cmd node test.js

View File

@@ -1,3 +1,3 @@
#! /bin/bash
haxe build.hxml
haxe test-interp.hxml && haxe test-js.hxml