summaryrefslogtreecommitdiff
path: root/libregex/redump.myr
blob: 7f7ed40c3221a28b0f77cc4fccc78457fcde6770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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(regex.exec(re, ln))
			std.slfree(ln)
		| `std.None:
			break
		;;
	;;
}

const show = {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")
	;;
}