shithub: mc

ref: 94acc736ec2d8d80533439a24742d7c796aaab4f
dir: /libregex/redump.myr/

View raw version
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("unable to compile regex: {}\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("failed to open {}\n", f)
		;;
	;;
}

const dump = {re, fd 
	while true
		match bio.readln(fd)
		| `std.Some ln:
			show(re, ln, regex.exec(re, ln))
			std.slfree(ln)
		| `std.None:
			break
		;;
	;;
}

const show = {re, ln, mg
	var i

	match mg
	| `std.Some rl:
		std.put("Matched: {}\n", rl[0])
		for i = 1; i < rl.len; i++
			std.put("group {}: {}\n", i, rl[i])
		;;
	| `std.None:
		std.put("Match failed\n")
		std.put("{}\n", ln)
		for i = 0; i < re.strp - 1; i++
			std.put(" ")
		;;
		std.put("^\n")
	;;
}