summaryrefslogtreecommitdiff
path: root/lib/regex
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-10-01 21:27:04 -0700
committerOri Bernstein <ori@eigenstate.org>2015-10-01 22:35:32 -0700
commit31dc0048af3102df08524099df2f005f547a2421 (patch)
tree14f2cc78b750cdcf19ba7c9f8435011a7b8c91bd /lib/regex
parentd7d95f9f1db8434f40d5ac3c81ba486223e3893b (diff)
downloadmc-31dc0048af3102df08524099df2f005f547a2421.tar.gz
Fix up error formatting in libregex.
Also add support for '\*' escape sequence.
Diffstat (limited to 'lib/regex')
-rw-r--r--lib/regex/compile.myr39
-rw-r--r--lib/regex/redump.myr2
-rw-r--r--lib/regex/types.myr8
3 files changed, 27 insertions, 22 deletions
diff --git a/lib/regex/compile.myr b/lib/regex/compile.myr
index f51f7d1..5162cb7 100644
--- a/lib/regex/compile.myr
+++ b/lib/regex/compile.myr
@@ -8,7 +8,6 @@ pkg regex =
const compile : (re : byte[:] -> std.result(regex#, status))
const dbgcompile : (re : byte[:] -> std.result(regex#, status))
const free : (re : regex# -> void)
- const failmsg : (st : status -> byte[:])
;;
type parseresult = union
@@ -548,9 +547,9 @@ const baseexpr = {re
/* lower prec operators */
| '|': -> `None
| ')': -> `None
- | '*': -> `Fail `Badrep
- | '+': -> `Fail `Badrep
- | '?': -> `Fail `Badrep
+ | '*': -> `Fail `Badrep '*'
+ | '+': -> `Fail `Badrep '+'
+ | '?': -> `Fail `Badrep '?'
| '[': -> chrclass(re)
| '.': getc(re); ret = mk(`Ranges std.slpush([][:], [0, std.Maxcharval]))
| '^': getc(re); ret = mk(`Bol)
@@ -563,7 +562,7 @@ const baseexpr = {re
if matchc(re, ')')
-> `Some mk(`Cap (m, s))
else
- -> `Fail `Unbalanced
+ -> `Fail `Unbalanced '('
;;
| `None: -> `Fail `Emptyparen
| `Fail st: -> `Fail st
@@ -613,7 +612,8 @@ const escaped = {re
| '.': ret = `Some mk(`Chr '.')
| '+': ret = `Some mk(`Chr '+')
| '?': ret = `Some mk(`Chr '?')
- | chr: ret = `Fail `Badescape
+ | '*': ret = `Some mk(`Chr '*')
+ | chr: ret = `Fail `Badescape chr
;;
-> ret
}
@@ -661,7 +661,7 @@ const unicodeclass = {re, neg
elif std.sleq(s, "Zs") || std.sleq(s, "Space_Separator")
tab = _ranges.tabblank[:]
else
- -> `Fail (`Badrange)
+ -> `Fail (`Badrange s)
;;
if !neg
t = mk(`Ranges std.sldup(tab))
@@ -688,7 +688,7 @@ const chrclass = {re
;;
if !matchc(re, ']')
std.slfree(rl)
- -> `Fail `Unbalanced
+ -> `Fail `Unbalanced '['
;;
std.sort(rl, {a, b;
@@ -825,16 +825,21 @@ const astfree = {t
std.free(t)
}
-const failmsg = {st
- match st
- | `Noimpl: -> "no implementation"
- | `Incomplete: -> "regex ended before input fully parsed"
- | `Unbalanced: -> "unbalanced bracket"
- | `Emptyparen: -> "empty parentheses"
- | `Badrep: -> "invalid repetition"
- | `Badrange: -> "invalid range"
- | `Badescape: -> "invalid escape code"
+const fmtfail = {sb, ap, opt
+ match std.vanext(ap)
+ | `Noimpl: std.sbfmt(sb, "no implementation")
+ | `Incomplete: std.sbfmt(sb, "regex ended before input fully parsed")
+ | `Unbalanced c: std.sbfmt(sb, "unbalanced {}", c)
+ | `Emptyparen: std.sbfmt(sb, "empty parentheses")
+ | `Badrep c: std.sbfmt(sb, "invalid repetition {}", c)
+ | `Badrange s: std.sbfmt(sb, "invalid range name {}", s)
+ | `Badescape c: std.sbfmt(sb, "invalid escape code {}", c)
;;
}
+const __init__ = {
+ var e : status
+ std.fmtinstall(std.typeof(e), fmtfail, [][:])
+}
+
diff --git a/lib/regex/redump.myr b/lib/regex/redump.myr
index 7233864..920b257 100644
--- a/lib/regex/redump.myr
+++ b/lib/regex/redump.myr
@@ -29,7 +29,7 @@ const main = {args
;;
match comp
| `std.Fail m:
- std.fatal("unable to compile regex: {}\n", regex.failmsg(m))
+ std.fatal("unable to compile regex: {}\n", m)
| `std.Ok re:
if cmd.args.len > 1
runall(re, cmd.args)
diff --git a/lib/regex/types.myr b/lib/regex/types.myr
index b283d2a..8de55ca 100644
--- a/lib/regex/types.myr
+++ b/lib/regex/types.myr
@@ -4,11 +4,11 @@ pkg regex =
type status = union
`Noimpl
`Incomplete
- `Unbalanced
+ `Unbalanced char
`Emptyparen
- `Badrep
- `Badrange
- `Badescape
+ `Badrep char
+ `Badrange byte[:]
+ `Badescape char
;;
type ast = union