diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-04-12 22:32:33 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-04-12 22:32:33 -0700 |
commit | cceee8693a03ffab73b9e41ad1d07f8383e6a7aa (patch) | |
tree | abb48dac71acc445933ed351adb1e0a41da6d72e | |
parent | 49256d6d022226bc0a5c92e44ef0757ab025a3bc (diff) | |
download | txr-cceee8693a03ffab73b9e41ad1d07f8383e6a7aa.tar.gz txr-cceee8693a03ffab73b9e41ad1d07f8383e6a7aa.tar.bz2 txr-cceee8693a03ffab73b9e41ad1d07f8383e6a7aa.zip |
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.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | regex.c | 16 |
2 files changed, 24 insertions, 4 deletions
@@ -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. @@ -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); } } |