ref: 3b5a5cd9b9843d0ba824c756911456d57cf1c1be
parent: 4a2263e9daa8ce34bfb9796b36f23c15d060e306
author: Ori Bernstein <[email protected]>
date: Fri May 1 18:07:13 EDT 2015
Add regex dumper.
--- a/libregex/bld.sub
+++ b/libregex/bld.sub
@@ -7,6 +7,11 @@
lib ../libstd:std
;;
+bin redump {noinst} =
+ redump.myr
+ lib ../libstd:std
+;;
+
gen ranges.myr {durable} =
mkchartab -a -p_ranges UnicodeData.txt -o ranges.myr
;;
--- a/libregex/compile.myr
+++ b/libregex/compile.myr
@@ -19,7 +19,7 @@
/* 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 @@
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 @@
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 @@
/* 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)
;;
;;
}
--- a/libregex/interp.myr
+++ b/libregex/interp.myr
@@ -210,7 +210,7 @@
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
--- /dev/null
+++ b/libregex/redump.myr
@@ -1,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
+ ;;
+ ;;
+}
+
--- a/libregex/types.myr
+++ b/libregex/types.myr
@@ -66,7 +66,7 @@
mend : std.size[:] /* match ends */
;;
- type reinst = union
+ pkglocal type reinst = union
/* direct consumers */
`Ibyte byte
`Irange (byte, byte)
@@ -84,6 +84,6 @@
/* control flow */
`Ifork (std.size, std.size)
`Ijmp std.size
- `Imatch
+ `Imatch std.size
;;
;;