From cceee8693a03ffab73b9e41ad1d07f8383e6a7aa Mon Sep 17 00:00:00 2001
From: Kaz Kylheku <kaz@kylheku.com>
Date: Thu, 12 Apr 2012 22:32:33 -0700
Subject: Improve the regex Lisp syntax by allowing strings to specify
 character compounds. I.e. the syntax "foo" is equivalent to the cumbersome
 canonical form (compound #\f #\o #\o).

* regex.c (nfa_compile_regex, dv_compile_regex): Use chrp function
instead of typeof. Handle stringp case by forming a compound out of the
characters and recursing. Check for some bad objects in the regex
that would never come out of our regex parser but could occur
in a "hand crafted" syntax tree.
---
 ChangeLog | 12 ++++++++++++
 regex.c   | 16 ++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 598e046a..2aa35f91 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2012-04-12  Kaz Kylheku  <kaz@kylheku.com>
+
+	Improve the regex Lisp syntax by allowing strings to specify
+	character compounds. I.e. the syntax "foo" is equivalent to the
+	cumbersome canonical form (compound #\f #\o #\o).
+
+	* regex.c (nfa_compile_regex, dv_compile_regex): Use chrp function
+	instead of typeof. Handle stringp case by forming a compound out of the
+	characters and recursing. Check for some bad objects in the regex
+	that would never come out of our regex parser but could occur
+	in a "hand crafted" syntax tree.
+
 2012-04-11  Kaz Kylheku  <kaz@kylheku.com>
 
 	* txr.1: Fix misleading comment example.
diff --git a/regex.c b/regex.c
index eddb3b0e..20774651 100644
--- a/regex.c
+++ b/regex.c
@@ -842,15 +842,17 @@ static nfa_t nfa_compile_regex(val exp)
     nfa_state_t *acc = nfa_state_accept();
     nfa_state_t *s = nfa_state_empty(acc, 0);
     return nfa_make(s, acc);
-  } else if (typeof(exp) == chr_s) {
+  } else if (chrp(exp)) {
     nfa_state_t *acc = nfa_state_accept();
     nfa_state_t *s = nfa_state_single(acc, c_chr(exp));
     return nfa_make(s, acc);
+  } else if (stringp(exp)) {
+    return nfa_compile_regex(cons(compound_s, list_str(exp)));
   } else if (exp == wild_s) {
     nfa_state_t *acc = nfa_state_accept();
     nfa_state_t *s = nfa_state_wild(acc);
     return nfa_make(s, acc);
-  } else {
+  } else if (consp(exp)) {
     val sym = first(exp), args = rest(exp);
 
     if (sym == set_s) {
@@ -903,6 +905,8 @@ static nfa_t nfa_compile_regex(val exp)
     } else {
       internal_error("bad operator in regex");
     }
+  } else {
+    uw_throwf(error_s, lit("bad object in regex syntax: ~s"), exp, nao);
   }
 }
 
@@ -1174,9 +1178,11 @@ static val reg_nullable(val);
  */
 static val dv_compile_regex(val exp)
 {
-  if (atom(exp)) {
+  if (symbolp(exp) || chrp(exp)) {
     return exp;
-  } else {
+  } else if (stringp(exp)) {
+    return cons(compound_s, list_str(exp));
+  } else if (consp(exp)) {
     val sym = first(exp);
     val args = rest(exp);
 
@@ -1224,6 +1230,8 @@ static val dv_compile_regex(val exp)
     } else {
       internal_error("bad operator in regex");
     }
+  } else {
+    uw_throwf(error_s, lit("bad object in regex syntax: ~s"), exp, nao);
   }
 }
 
-- 
cgit v1.2.3