summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2017-07-19 06:46:22 -0700
committerKaz Kylheku <kaz@kylheku.com>2017-07-19 17:17:40 -0700
commit5d224bc42497072c259eb0696db5497312ac0b6c (patch)
tree80740728d60ebfb8201983d53c0d08b6110fc0e6
parent33c0a11858294c7888de4f37038bbdc691e8d2dc (diff)
downloadtxr-5d224bc42497072c259eb0696db5497312ac0b6c.tar.gz
txr-5d224bc42497072c259eb0696db5497312ac0b6c.tar.bz2
txr-5d224bc42497072c259eb0696db5497312ac0b6c.zip
Named filters now stored in new *filters* hash.
* filter.c (filters): Global variable removed. (filter_s): New symbol variable. (filter_init): Remove gc-protection from removed variable. Intern the *filters* symbol. Use local variable for filters hash, create the *filters* special variable and store the hash into that. * filter.h (filters): Variable declaraton removed. (filters): New macro: expands to an expression designating the current value of *filters* in the dynamic environment. * txr.1: Documented *filters*, adding a forward reference to it from the description of filtering.
-rw-r--r--filter.c42
-rw-r--r--filter.h4
-rw-r--r--txr.139
3 files changed, 64 insertions, 21 deletions
diff --git a/filter.c b/filter.c
index 5838057b..92a51592 100644
--- a/filter.c
+++ b/filter.c
@@ -43,7 +43,7 @@
#include "eval.h"
#include "stream.h"
-val filters;
+val filters_s;
val filter_k, lfilt_k, rfilt_k, tohtml_k, fromhtml_k;
val tohtml_star_k;
val upcase_k, downcase_k;
@@ -879,9 +879,9 @@ static val html_decode(val str)
void filter_init(void)
{
- protect(&filters, convert(val *, 0));
+ val fh = make_hash(nil, nil, nil);
- filters = make_hash(nil, nil, nil);
+ filters_s = intern(lit("*filters*"), user_package);
filter_k = intern(lit("filter"), keyword_package);
lfilt_k = intern(lit("lfilt"), keyword_package);
rfilt_k = intern(lit("rfilt"), keyword_package);
@@ -901,30 +901,32 @@ void filter_init(void)
tofloat_k = intern(lit("tofloat"), keyword_package);
hextoint_k = intern(lit("hextoint"), keyword_package);
- sethash(filters, tohtml_k, build_filter(tohtml_table, t));
- sethash(filters, tohtml_star_k, build_filter(tohtml_star_table, t));
+ reg_var(filters_s, fh);
+
+ sethash(fh, tohtml_k, build_filter(tohtml_table, t));
+ sethash(fh, tohtml_star_k, build_filter(tohtml_star_table, t));
{
val trie = build_filter(fromhtml_table, nil);
trie_add(trie, lit("&#"), func_n1(html_numeric_handler));
trie_compress(mkcloc(trie));
- sethash(filters, fromhtml_k, trie);
+ sethash(fh, fromhtml_k, trie);
}
- sethash(filters, intern(lit("to_html"), keyword_package),
+ sethash(fh, intern(lit("to_html"), keyword_package),
get_filter(tohtml_k));
- sethash(filters, intern(lit("from_html"), keyword_package),
+ sethash(fh, intern(lit("from_html"), keyword_package),
get_filter(fromhtml_k));
- sethash(filters, upcase_k, func_n1(upcase_str));
- sethash(filters, downcase_k, func_n1(downcase_str));
- sethash(filters, topercent_k, curry_12_1(func_n2(url_encode), nil));
- sethash(filters, frompercent_k, curry_12_1(func_n2(url_decode), nil));
- sethash(filters, tourl_k, curry_12_1(func_n2(url_encode), t));
- sethash(filters, fromurl_k, curry_12_1(func_n2(url_decode), t));
- sethash(filters, tobase64_k, curry_12_1(func_n2(base64_encode), 0));
- sethash(filters, frombase64_k, func_n1(base64_decode));
- sethash(filters, tonumber_k, func_n1(num_str));
- sethash(filters, toint_k, curry_12_1(func_n2(int_str), nil));
- sethash(filters, tofloat_k, func_n1(flo_str));
- sethash(filters, hextoint_k, curry_12_1(func_n2(int_str), num_fast(16)));
+ sethash(fh, upcase_k, func_n1(upcase_str));
+ sethash(fh, downcase_k, func_n1(downcase_str));
+ sethash(fh, topercent_k, curry_12_1(func_n2(url_encode), nil));
+ sethash(fh, frompercent_k, curry_12_1(func_n2(url_decode), nil));
+ sethash(fh, tourl_k, curry_12_1(func_n2(url_encode), t));
+ sethash(fh, fromurl_k, curry_12_1(func_n2(url_decode), t));
+ sethash(fh, tobase64_k, curry_12_1(func_n2(base64_encode), 0));
+ sethash(fh, frombase64_k, func_n1(base64_decode));
+ sethash(fh, tonumber_k, func_n1(num_str));
+ sethash(fh, toint_k, curry_12_1(func_n2(int_str), nil));
+ sethash(fh, tofloat_k, func_n1(flo_str));
+ sethash(fh, hextoint_k, curry_12_1(func_n2(int_str), num_fast(16)));
reg_fun(intern(lit("make-trie"), user_package), func_n0(make_trie));
reg_fun(intern(lit("trie-add"), user_package), func_n3(trie_add));
diff --git a/filter.h b/filter.h
index 6b576eff..a8507b9c 100644
--- a/filter.h
+++ b/filter.h
@@ -25,7 +25,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-extern val filters;
+#define filters (deref(lookup_var_l(nil, filters_s)))
+
+extern val filters_s;
extern val filter_k, lfilt_k, rfilt_k, to_html_k, from_html_k;
extern val upcase_k, downcase_k;
extern val topercent_k, frompercent_k, tourl_k, fromurl_k;
diff --git a/txr.1 b/txr.1
index 724b42d2..e979e061 100644
--- a/txr.1
+++ b/txr.1
@@ -3473,6 +3473,9 @@ have different special characters or other syntax, requiring escaping
or similar treatment. Note that it is also possible to use a function
as a filter. See Function Filters below.
+Named filters are stored in the hash table held in the Lisp special variable
+.codn *filters* .
+
.coIP @(filter)
The
.code filter
@@ -52160,6 +52163,42 @@ results in more compact syntax.
Note: this function is useful for creating a compact, prefix-compressed
regular expression which matches a list of strings.
+.coNP Special variable @ *filters*
+.desc
+The
+.code *filters*
+special variable holds a hash table which associates symbols with
+filters. This hash table defines the named filters used in the
+\*(TX pattern language. The names are the hash table keys, and filter
+objects are the values. Filter objects are one of three representations.
+The value
+.code nil
+represents a null filter, which performs no filtering, passing the input
+string through. A filter object may be a raw or compressed trie.
+It may also be a Lisp function, which must be callable with one argument
+of string type, and must return a string.
+
+The application may define new filters by associating symbolic keys in
+.code *filters*
+with values which conform to the above representation of filters.
+
+The behavior is unspecified if any of the predefined filters
+are removed or redefined, and are subsequently used, or if the
+.code *filters*
+variable is replaced or rebound with a hash table value which omits
+those keys, or associates them with different values.
+
+Note that functions
+.codn html-encode ,
+.code html-encode*
+and
+.code html-decode
+use, respectively, the HTML-related
+.codn :tohtml ,
+.code :tohtml*
+and
+.codn :fromhtml .
+
.SS* Access To TXR Pattern Language From Lisp
It is useful to be able to invoke the abilities of the \*(TX pattern Language