shithub: mc

Download patch

ref: 31dc0048af3102df08524099df2f005f547a2421
parent: d7d95f9f1db8434f40d5ac3c81ba486223e3893b
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Oct 1 17:27:04 EDT 2015

Fix up error formatting in libregex.

    Also add support for '\*' escape sequence.

--- a/lib/regex/compile.myr
+++ b/lib/regex/compile.myr
@@ -8,7 +8,6 @@
 	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 @@
 	/* 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 @@
 			if matchc(re, ')')
 				-> `Some mk(`Cap (m, s))
 			else
-				-> `Fail `Unbalanced
+				-> `Fail `Unbalanced '('
 			;;
 		| `None:	-> `Fail `Emptyparen
 		| `Fail st:	-> `Fail st
@@ -613,7 +612,8 @@
 	| '.': 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 @@
 	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 @@
 	;;
 	if !matchc(re, ']')
 		std.slfree(rl)
-		-> `Fail `Unbalanced
+		-> `Fail `Unbalanced '['
 	;;
 
 	std.sort(rl, {a, b;
@@ -825,16 +825,21 @@
 	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, [][:])
 }
 
--- a/lib/regex/redump.myr
+++ b/lib/regex/redump.myr
@@ -29,7 +29,7 @@
 	;;
 	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)
--- a/lib/regex/types.myr
+++ b/lib/regex/types.myr
@@ -4,11 +4,11 @@
 	type status = union
 		`Noimpl
 		`Incomplete
-		`Unbalanced
+		`Unbalanced char
 		`Emptyparen
-		`Badrep
-		`Badrange
-		`Badescape
+		`Badrep char
+		`Badrange byte[:]
+		`Badescape char
 	;;
 
 	type ast = union