shithub: mc

Download patch

ref: 8a46d62744d2969ba9c3488672027509f00fdb4b
parent: c65c9894c643308c87de69de8c0be5c6c6e1c663
author: Ori Bernstein <[email protected]>
date: Wed Oct 23 08:51:09 EDT 2013

Add beginning/end of line matches

--- a/compile.myr
+++ b/compile.myr
@@ -24,8 +24,8 @@
 
 	/* meta */
 	`Cap	tree#
-	`Astart
-	`Aend
+	`Bol	/* beginning of line */
+	`Eol	/* end of line */
 ;;
 
 type parseresult = union
@@ -76,6 +76,12 @@
 	`Dot: 		append(re, `Idot);;
 
 	/* meta */
+	`Bol:
+		append(re, `Ibol)
+		;;
+	`Eol:
+		append(re, `Ibol)
+		;;
 	`Cap	a:
 		m = re.nmatch++
 		append(re, `Ilbra m)
@@ -164,32 +170,19 @@
 		std.put("%i:\t", i)
 		match re.prog[i]
 		/* Char matching. Consume exactly one byte from the string. */
-		`Ibyte b: 
-			std.put("`Ibyte %b (%c)\n", b, b castto(char))
-			;;
-		`Irange (start, end):
-			std.put("`Irange (%b,%b)\n", start, end)
-			;;
-		`Idot:
-			std.put("`Idot\n")
-			;;
+		`Ibyte b:		std.put("`Ibyte %b (%c)\n", b, b castto(char)) ;;
+		`Irange (start, end):	std.put("`Irange (%b,%b)\n", start, end) ;;
+		`Idot:	std.put("`Idot\n") ;;
 		/* capture groups */
-		`Ilbra m:
-			std.put("`Ilbra %z\n", m)
-			;;
-		`Irbra m:
-			std.put("`Irbra %z\n", m)
-			;;
+		`Ilbra m:		std.put("`Ilbra %z\n", m) ;;
+		`Irbra m:		std.put("`Irbra %z\n", m) ;;
+		/* anchors */
+		`Ibol:			std.put("`Ibol\n");;
+		`Ieol:			std.put("`Ieol\n");;
 		/* 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")
-			;;
+		`Ifork	(lip, rip):	std.put("`Ifork (%z,%z)\n", lip, rip) ;;
+		`Ijmp ip:		std.put("`Ijmp %z\n", ip) ;;
+		`Imatch:		std.put("`Imatch\n") ;;
 		;;
 	;;
 }
@@ -224,6 +217,12 @@
 		std.put("Quest\n")
 		dump(a, indent + 1)
 		;;
+	`Bol:
+		std.put("Bol\n")
+		;;
+	`Eol:
+		std.put("Eol\n")
+		;;
 	/* end matches */
 	`Class	sl:
 		std.put("Class [a..b]\n")
@@ -342,8 +341,8 @@
 	'?':	-> `Fail (`Badrep);;
 	'[':	ret = chrclass(re);;
 	'.':	getc(re); ret = mk(`Dot);;
-	'^':	getc(re); ret = mk(`Astart);;
-	'$':	getc(re); ret = mk(`Aend);;
+	'^':	getc(re); ret = mk(`Bol);;
+	'$':	getc(re); ret = mk(`Eol);;
 	'(':	
 		getc(re)
 		match altexpr(re)
--- a/interp.myr
+++ b/interp.myr
@@ -92,6 +92,23 @@
 	  Non-consuming. All of these recursively call step() until
 	  exactly one byte is consumed from the string.
 	 */
+	`Ibol:
+		trace(re, "\t%i:\tBol\n", thr.ip)
+		if re.strp == 0 || str[re.strp -1] == 0x10
+			thr.ip++
+			step(re, tid)
+		else
+			kill(re, tid, "not beginning of line")
+		;;
+		;;
+	`Ieol:
+		trace(re, "\t%i:\tEol\n", thr.ip)
+		if re.strp == str.len || str[re.strp] == 0x10
+			step(re, tid)
+		else
+			kill(re, tid, "not end of line")
+		;;
+		;;
 	`Ilbra	m:
 		trace(re, "\t%i:\tLbra %z\n", thr.ip, m)
 		trace(re, "\t\tmatch start = %z\n", re.strp)
--- a/main.myr
+++ b/main.myr
@@ -3,9 +3,9 @@
 
 const main = {
 	var found
-	match regex.compile("(a*b)*")
+	match regex.compile(".*^a*")
 	`std.Success re:
-		found = regex.exec(re, "a")
+		found = regex.exec(re, "b\naaa")
 		std.put("Found = %t: len = %z\n", found, re.strp)
 		-> 0
 		;;
--- a/types.myr
+++ b/types.myr
@@ -42,6 +42,10 @@
 		`Ilbra	std.size
 		`Irbra	std.size
 
+		/* anchors */
+		`Ibol
+		`Ieol
+
 		/* control flow */
 		`Ifork	[std.size, std.size]
 		`Ijmp	std.size