Skip to content

Instantly share code, notes, and snippets.

@michiakig
Created January 27, 2013 16:38
Show Gist options
  • Save michiakig/4649161 to your computer and use it in GitHub Desktop.
Save michiakig/4649161 to your computer and use it in GitHub Desktop.
(*
CM.make "$/regexp-lib.cm";
*)
val getc: string -> (char, int) StringCvt.reader =
fn s => fn i =>
if (i < String.size s)
then SOME(String.sub(s, i), i+1)
else NONE
val parse: string -> RegExpSyntax.syntax option =
fn s =>
case (AwkSyntax.scan (getc s)) 0 of
NONE => NONE
| SOME (re, _) => SOME re
val match =(* : (string * RegExpSyntax.syntax) -> (string * string) = *)
fn (s, re) =>
let
val re' = BackTrackEngine.compile re
val reader = getc s
in
BackTrackEngine.find re' reader
end
fun show {len=l,pos=p} =
"len=" ^ Int.toString l ^ ",pos=" ^ Int.toString p
val rec collectMatches: 'a MatchTree.match_tree -> 'a list =
fn MatchTree.Match (m, ms) =>
if null ms
then [m]
else m :: (List.concat (map collectMatches ms))
fun showMatches (s, re) =
let
in
case (match(s, re) 0) of
NONE => ""
| SOME => show p
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment