shithub: mc

Download patch

ref: fdafa085a245d4f48f84c6875a6b7cb89b8b5c6c
parent: 97836cfa040efbbbda0f6da6b8df94a4553c9709
author: Ori Bernstein <ori@eigenstate.org>
date: Sun May 22 12:57:18 EDT 2016

Implement noncapturing groups, '\r', and '\n'.

--- a/lib/regex/compile.myr
+++ b/lib/regex/compile.myr
@@ -570,6 +570,7 @@
 
 const baseexpr = {re
 	var ret, m, idx
+	var nocap
 
 	if re.pat.len == re.idx
 		-> `None
@@ -589,12 +590,25 @@
 		getc(re);
 		ret = mk(re, `Ranges std.sldup([[0, std.Maxcharval]][:]), idx)
 	| '(':	
-		m = re.nmatch++
+		nocap = false
+		m = 0
 		getc(re)
+		if matchc(re, '?')
+			if !matchc(re, ':')
+				-> `Fail `Badrep '?'
+			;;
+			nocap = true
+		else
+			m = re.nmatch++
+		;;
 		match altexpr(re)
 		| `Some s:
 			if matchc(re, ')')
-				-> `Some mk(re, `Cap (m, s), idx)
+				if nocap
+					-> `Some s
+				else
+					-> `Some mk(re, `Cap (m, s), idx)
+				;;
 			else
 				-> `Fail `Unbalanced '('
 			;;
@@ -648,6 +662,10 @@
 	| '+': ret = `Some mk(re, `Chr '+', idx)
 	| '?': ret = `Some mk(re, `Chr '?', idx)
 	| '*': ret = `Some mk(re, `Chr '*', idx)
+	/* escaped nonprintable characters */
+	| 'r': ret = `Some mk(re, `Chr '\r', idx)
+	| 'n': ret = `Some mk(re, `Chr '\n', idx)
+	| 'b': ret = `Some mk(re, `Chr '\b', idx)
 	| chr: ret = `Fail `Badescape chr
 	;;
 	-> ret