From c1025cd036938254cf00d0ce0da83de761525eb4 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 22 May 2018 20:03:13 -0700 Subject: txr: support variable in postive match. In the @{var mod} syntax in the pattern language, allow mod to be a variable which contains a regex or integer, not just an integer or regex literal. * match.c (h_var): Check for modifier being a variable, and resolve it. * parser.y (modifiers): Allow a SYMTOK phrase. * txr.1: Documented. --- match.c | 5 +++++ parser.y | 1 + txr.1 | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/match.c b/match.c index 3a1024e9..a7a65098 100644 --- a/match.c +++ b/match.c @@ -596,6 +596,11 @@ static val h_var(match_line_ctx *c) sym, nao); } + if (bindable(modifier)) { + val mpair = tx_lookup_var_ubc(modifier, c->bindings, elem); + modifier = cdr(mpair); + } + if (pair) { /* If the variable already has a binding, we replace it with its value, and treat it as a string match. diff --git a/parser.y b/parser.y index abc12d1c..a64d489b 100644 --- a/parser.y +++ b/parser.y @@ -788,6 +788,7 @@ modifiers : NUMBER { $$ = cons($1, nil); } rlc($$, $1); } | compound { $$ = rlc(cons(expand_meta($1, nil), nil), $1); } + | SYMTOK { $$ = cons(symhlpr($1, nil), nil); } ; o_var : SYMTOK { val expr = symhlpr($1, nil); diff --git a/txr.1 b/txr.1 index 7d6e00bb..206cdd16 100644 --- a/txr.1 +++ b/txr.1 @@ -2179,6 +2179,7 @@ enclosed with the variable in braces: .mets >> @{ bident <> / regex /} .mets >> @{ bident >> ( fun >> [args ...])} .mets >> @{ bident << number } +.mets >> @{ bident << bident } .cble These specify a variable binding that is driven by a positive match derived @@ -2227,6 +2228,26 @@ This syntax is processed without consideration of what other syntax follows. A positive match may be directly followed by an unbound variable. +The +.cblk +.mets >> @{ bident << bident } +.cble +syntax allows the +.meta number +or +.meta regex +modifier to come from a variable. The variable must be bound and contain +a non-negative integer or regular expression. +For example, +.code "@{x y}" +behaves like +.code "@{x 3}" +if +.code y +is bound to the integer 3. It is an error if +.code y +is unbound. + .coSS Special Symbols @ nil and @ t Just like in the Common Lisp language, the names -- cgit v1.2.3