diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-12-25 09:59:28 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-12-25 09:59:28 -0800 |
commit | a791cd6527f20c531898390e14f3c7c21c1d9571 (patch) | |
tree | d94c60a05abc8cc274bf5b5976c6917c88176b70 /regex.c | |
parent | d45338dd1be5dae9e0c3d979d846398677ee5699 (diff) | |
download | txr-a791cd6527f20c531898390e14f3c7c21c1d9571.tar.gz txr-a791cd6527f20c531898390e14f3c7c21c1d9571.tar.bz2 txr-a791cd6527f20c531898390e14f3c7c21c1d9571.zip |
Fix inconsistency in regex-source.
If we compile the regex expression (compound "str*"), calling
regex-source on the compiled regex object yields "str*".
That, of course, is treated as regex character syntax if
fed back to regex-compile, and the * becomes an operator.
We want the source to be (compound "str*").
This happens because the AST optimizer reduces
(compound X) -> X.
* regex.c (regex_compile): If the optimized expression is just
a character string atom S, then for the purposes of
maintaining the source code, convert it to (compound S).
Diffstat (limited to 'regex.c')
-rw-r--r-- | regex.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -2101,6 +2101,8 @@ static val regex_requires_dv(val exp) val regex_compile(val regex_sexp, val error_stream) { + val regex_source; + if (stringp(regex_sexp)) { regex_sexp = regex_parse(regex_sexp, default_bool_arg(error_stream)); return if2(regex_sexp, regex_compile(regex_sexp, error_stream)); @@ -2108,6 +2110,10 @@ val regex_compile(val regex_sexp, val error_stream) regex_sexp = reg_optimize(reg_expand_nongreedy(reg_nary_to_bin(regex_sexp))); + regex_source = if3(stringp(regex_sexp), + cons(compound_s, cons(regex_sexp, nil)), + regex_sexp); + if (opt_derivative_regex || regex_requires_dv(regex_sexp)) { regex_t *regex = coerce(regex_t *, chk_malloc(sizeof *regex)); val ret; @@ -2117,7 +2123,7 @@ val regex_compile(val regex_sexp, val error_stream) regex->source = nil; ret = cobj(coerce(mem_t *, regex), regex_s, ®ex_obj_ops); regex->r.dv = dv; - regex->source = regex_sexp; + regex->source = regex_source; return ret; } else { regex_t *regex = coerce(regex_t *, chk_malloc(sizeof *regex)); @@ -2127,7 +2133,7 @@ val regex_compile(val regex_sexp, val error_stream) ret = cobj(coerce(mem_t *, regex), regex_s, ®ex_obj_ops); regex->r.nfa = nfa_compile_regex(regex_sexp); regex->nstates = nfa_count_states(regex->r.nfa.start); - regex->source = regex_sexp; + regex->source = regex_source; return ret; } } |