diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..602eb8e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test.js \ No newline at end of file diff --git a/src/re_flex/Main.kiss b/src/re_flex/Main.kiss index d441299..994c4b2 100644 --- a/src/re_flex/Main.kiss +++ b/src/re_flex/Main.kiss @@ -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")))) diff --git a/src/re_flex/R.hx b/src/re_flex/R.hx index a84aba2..bf72769 100644 --- a/src/re_flex/R.hx +++ b/src/re_flex/R.hx @@ -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, + namedGroups:DynamicAccess, + #end + group:Int->String +}; @:build(kiss.Kiss.build()) class R {} diff --git a/src/re_flex/R.kiss b/src/re_flex/R.kiss index 2c9c2da..a3099bc 100644 --- a/src/re_flex/R.kiss +++ b/src/re_flex/R.kiss @@ -27,3 +27,32 @@ []) (true (for _ (range (- max min)) (oneOf expr ""))))))) + +// TODO could memoize compiled regexes +(function :Null 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)))))) \ No newline at end of file diff --git a/build.hxml b/test-interp.hxml similarity index 100% rename from build.hxml rename to test-interp.hxml diff --git a/test-js.hxml b/test-js.hxml new file mode 100644 index 0000000..2790ab0 --- /dev/null +++ b/test-js.hxml @@ -0,0 +1,5 @@ +-lib kiss +-cp src +--main re_flex.Main +--js test.js +--cmd node test.js \ No newline at end of file diff --git a/test.sh b/test.sh index 0ee8ae9..eaa0d2d 100644 --- a/test.sh +++ b/test.sh @@ -1,3 +1,3 @@ #! /bin/bash -haxe build.hxml \ No newline at end of file +haxe test-interp.hxml && haxe test-js.hxml \ No newline at end of file