diff options
author | Ori Bernstein <ori@eigenstate.org> | 2015-05-01 22:07:13 -0700 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2015-05-01 22:07:29 -0700 |
commit | 2160d782ffa10f48473a36105c342323a0b7f26c (patch) | |
tree | d2581627161dc93652d9c58308bb7e63ea9c7ce0 | |
parent | f80db1cc005bd118f5e6910be4fa305290832a54 (diff) | |
download | mc-2160d782ffa10f48473a36105c342323a0b7f26c.tar.gz |
Add regex dumper.
-rw-r--r-- | libregex/bld.sub | 5 | ||||
-rw-r--r-- | libregex/compile.myr | 10 | ||||
-rw-r--r-- | libregex/interp.myr | 2 | ||||
-rw-r--r-- | libregex/redump.myr | 52 | ||||
-rw-r--r-- | libregex/types.myr | 4 |
5 files changed, 65 insertions, 8 deletions
diff --git a/libregex/bld.sub b/libregex/bld.sub index d9268a8..a56d611 100644 --- a/libregex/bld.sub +++ b/libregex/bld.sub @@ -7,6 +7,11 @@ lib regex = lib ../libstd:std ;; +bin redump {noinst} = + redump.myr + lib ../libstd:std +;; + gen ranges.myr {durable} = mkchartab -a -p_ranges UnicodeData.txt -o ranges.myr ;; diff --git a/libregex/compile.myr b/libregex/compile.myr index eac43d3..4081e9f 100644 --- a/libregex/compile.myr +++ b/libregex/compile.myr @@ -19,7 +19,7 @@ type parseresult = union /* Compiles a pattern into a regex */ const compile = {pat - -> regexcompile(std.mk([.pat = pat, .nmatch = 1])) + -> regexcompile(std.mk([.pat = pat, .nmatch = 1]), 0) } const parse = {pat @@ -43,11 +43,11 @@ const dbgcompile = {pat var re re = std.mk([.pat = pat, .nmatch = 1, .debug = true]) - -> regexcompile(re) + -> regexcompile(re, 0) } /* compiles a pattern into an allocated regex */ -const regexcompile = {re +const regexcompile = {re, id match regexparse(re) | `None: -> `std.Fail (`Incomplete) | `Fail f: -> `std.Fail f @@ -64,7 +64,7 @@ const regexcompile = {re append(re, `Ilbra 0) gen(re, t) append(re, `Irbra 0) - append(re, `Imatch) + append(re, `Imatch id) idump(re) astfree(t) -> `std.Ok re @@ -394,7 +394,7 @@ const idump = {re /* control flow */ | `Ifork (lip, rip): std.put("`Ifork (%z,%z)\n", lip, rip) | `Ijmp ip: std.put("`Ijmp %z\n", ip) - | `Imatch: std.put("`Imatch\n") + | `Imatch id: std.put("`Imatch %z\n", id) ;; ;; } diff --git a/libregex/interp.myr b/libregex/interp.myr index 7e1bb42..36fae53 100644 --- a/libregex/interp.myr +++ b/libregex/interp.myr @@ -210,7 +210,7 @@ const step = {re, thr, curip trace(re, thr, "\t%z:\tJmp %z\n", thr.ip, ip) thr.ip = ip -> false - | `Imatch: + | `Imatch id: trace(re, thr, "\t%z:\tMatch\n", thr.ip) finish(re, thr) -> true diff --git a/libregex/redump.myr b/libregex/redump.myr new file mode 100644 index 0000000..bf52057 --- /dev/null +++ b/libregex/redump.myr @@ -0,0 +1,52 @@ +use std +use bio +use regex + +const main = {args + var cmd, opts + var fd + + opts = [ + .argdesc = "regex [inputs...]", + .minargs = 1 + ] + cmd = std.optparse(args, &opts) + match regex.dbgcompile(cmd.args[0]) + | `std.Fail m: + std.fatal(1, "unable to compile regex: %s\n", regex.failmsg(m)) + | `std.Ok re: + if cmd.args.len > 1 + runall(re, cmd.args) + else + fd = bio.mkfile(0, bio.Rd) + dump(re, fd) + bio.close(fd) + ;; + ;; +} + +const runall = {re, files + + for f in files + match bio.open(f, bio.Rd) + | `std.Some fd: + dump(re, fd) + bio.close(fd) + | `std.None: + std.fatal(1, "failed to open %s\n", f) + ;; + ;; +} + +const dump = {re, fd + while true + match bio.readln(fd) + | `std.Some ln: + regex.exec(re, ln) + std.slfree(ln) + | `std.None: + break + ;; + ;; +} + diff --git a/libregex/types.myr b/libregex/types.myr index a0f8ceb..2bfa6c9 100644 --- a/libregex/types.myr +++ b/libregex/types.myr @@ -66,7 +66,7 @@ pkg regex = mend : std.size[:] /* match ends */ ;; - type reinst = union + pkglocal type reinst = union /* direct consumers */ `Ibyte byte `Irange (byte, byte) @@ -84,6 +84,6 @@ pkg regex = /* control flow */ `Ifork (std.size, std.size) `Ijmp std.size - `Imatch + `Imatch std.size ;; ;; |