From a791cd6527f20c531898390e14f3c7c21c1d9571 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 25 Dec 2016 09:59:28 -0800 Subject: 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). --- regex.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/regex.c b/regex.c index 1f645ead..00611d2a 100644 --- a/regex.c +++ b/regex.c @@ -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; } } -- cgit v1.2.3