summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2013-12-05 23:47:58 -0800
committerKaz Kylheku <kaz@kylheku.com>2013-12-05 23:47:58 -0800
commit43fa874fd295b2c765cddd14939a60a47e190148 (patch)
tree19e9205efe84ec5b66e1b0b47f8bf732edde9909
parentf128b3e3f1ab9fb724d09486b59ae2a2b4cab29c (diff)
downloadtxr-43fa874fd295b2c765cddd14939a60a47e190148.tar.gz
txr-43fa874fd295b2c765cddd14939a60a47e190148.tar.bz2
txr-43fa874fd295b2c765cddd14939a60a47e190148.zip
* regex.c (regex_compile): Handle string input.
* regex.h (regex_compile): Don't call argument regex_sexp, since it can be a string. * txr.1: Updated.
-rw-r--r--ChangeLog9
-rw-r--r--regex.c6
-rw-r--r--regex.h2
-rw-r--r--txr.115
4 files changed, 26 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 2b240a42..1e46b46c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2013-12-05 Kaz Kylheku <kaz@kylheku.com>
+ * regex.c (regex_compile): Handle string input.
+
+ * regex.h (regex_compile): Don't call argument
+ regex_sexp, since it can be a string.
+
+ * txr.1: Updated.
+
+2013-12-05 Kaz Kylheku <kaz@kylheku.com>
+
* eval.c (eval_init): Registered regex_parse as new
intrinsic function and std_null as new variable.
diff --git a/regex.c b/regex.c
index 52a42b14..c589b0ca 100644
--- a/regex.c
+++ b/regex.c
@@ -35,6 +35,7 @@
#include <limits.h>
#include "config.h"
#include "lib.h"
+#include "parser.h"
#include "unwind.h"
#include "regex.h"
#include "txr.h"
@@ -1614,7 +1615,10 @@ static val regex_requires_dv(val exp)
val regex_compile(val regex_sexp)
{
- if (opt_derivative_regex || regex_requires_dv(regex_sexp)) {
+ if (stringp(regex_sexp)) {
+ regex_sexp = regex_parse(regex_sexp, nil);
+ return if2(regex_sexp, regex_compile(regex_sexp));
+ } else if (opt_derivative_regex || regex_requires_dv(regex_sexp)) {
return cons(compiled_regex_s, cons(dv_compile_regex(regex_sexp), nil));
} else {
nfa_t *pnfa = (nfa_t *) chk_malloc(sizeof *pnfa);
diff --git a/regex.h b/regex.h
index 962a2846..c75b44f1 100644
--- a/regex.h
+++ b/regex.h
@@ -27,7 +27,7 @@
extern val space_k, digit_k, word_char_k;
extern val cspace_k, cdigit_k, cword_char_k;
-val regex_compile(val regex_sexp);
+val regex_compile(val regex);
val regexp(val);
val search_regex(val haystack, val needle_regex, val start_num, val from_end);
val match_regex(val str, val regex, val pos);
diff --git a/txr.1 b/txr.1
index 2906a0ab..ad83f669 100644
--- a/txr.1
+++ b/txr.1
@@ -9348,16 +9348,20 @@ object. For any other object type, it returns nil.
.TP
Syntax:
- (regex-compile <form>)
+ (regex-compile <form-or-string>)
.TP
Description:
The regex compile function takes the source code of a regular expression,
-expressed as a Lisp data structure, and compiles it to a regular expression
-object.
+expressed as a Lisp data structure representing an abstract syntax tree, or
+else a regular expression specified as a character string, and compiles it to a
+regular expression object.
-Suitable source code is produced by regex-parse.
+If <form-or-string> is a character string, it is parsed to an
+abstract syntax tree first, if by a call to (regex-parse <form-or-string>).
+If the parse is successful (the result is not nil) then
+it is compiled by a recursive call to regex-compile.
.TP
Examples:
@@ -9372,6 +9376,9 @@ Examples:
;; #/a|b|c/
(regex-compile '(or (or #\ea #\eb) #\ec))
+ ;; string
+ (regex-compile "a|b|c")
+
.SS Function regex-parse
.TP