From 0f66ac2b5412eb432f165b680fb495f972f33917 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 2 Jun 2021 22:47:07 -0700 Subject: matcher: better error handling for backquotes. * share/txr/stdlib/match.tl (transform-qquote): Handle hash error case with separate pattern. Use compile-error and *match form instead of error. Diagnose splicing unquote and nested quasiquote. --- share/txr/stdlib/match.tl | 17 ++++++++++++----- txr.1 | 9 ++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/share/txr/stdlib/match.tl b/share/txr/stdlib/match.tl index df143dac..dca0ebf9 100644 --- a/share/txr/stdlib/match.tl +++ b/share/txr/stdlib/match.tl @@ -989,11 +989,9 @@ (defun transform-qquote (syn) (match-case syn - ((sys:hash-lit @props . @(coll (@key @val))) - (if props - (error "~s: only equal hash tables supported" syn) - ^@(hash ,*(zip [mapcar transform-qquote key] - [mapcar transform-qquote val])))) + ((sys:hash-lit nil . @(coll (@key @val))) + ^@(hash ,*(zip [mapcar transform-qquote key] + [mapcar transform-qquote val]))) ((sys:struct-lit @type . @args) ^@(struct ,(transform-qquote type) ,*[mapcar transform-qquote args])) @@ -1003,6 +1001,15 @@ ((sys:unquote @pat) (if (symbolp pat) ^(sys:var ,pat) ^(sys:expr ,pat))) + ((sys:hash-lit @(have) . @nil) + (compile-error *match-form* + "only equal hash tables supported")) + ((@(or sys:qquote) . @nil) + (compile-error *match-form* + "pattern-matching quasiquote doesn't support nesting")) + ((sys:splice . @nil) + (compile-error *match-form* + "pattern-matching quasiquote doesn't support splicing")) ((@ca . @cd) (cons (transform-qquote ca) (transform-qquote cd))) (@else else))) diff --git a/txr.1 b/txr.1 index 7b1446dd..5aaf35e2 100644 --- a/txr.1 +++ b/txr.1 @@ -41361,11 +41361,18 @@ A JSON hash quasiquote is translated into .onom exactly like a hash. .meIP >> ( car . << cdr ) -Tree structure is translated according ot the +Tree structure is translated according to the .mono .meti <> ([ car ] . <> [ cdr ]) .onom pattern: it is recursively examined for translations. +.meIP >> ^ nested-qq-syntax +A nested quasiquote pattern is diagnosed as an error. +.meIP >> ,* expr +Splicing syntax is diagnosed as an error. +.meIP >> ~* expr +Splicing JSON syntax is diagnosed as an error inside a JSON quasiliteral. +.meIP >> ~* expr .meIP < obj Any other quasiquoted object is left untranslated. .RE -- cgit v1.2.3