From 4f225cc740c8fdd3ab597e8e95bbe0887e3dfe08 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 18 Jun 2018 21:48:30 -0700 Subject: [PATCH] reorder hide & splice (fixes #8) --- brag/codegen/runtime.rkt | 21 ++++++++++++--------- brag/examples/hide-and-splice.rkt | 4 ++++ brag/test/test-all.rkt | 1 + brag/test/test-hide-and-splice.rkt | 9 +++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 brag/examples/hide-and-splice.rkt create mode 100755 brag/test/test-hide-and-splice.rkt diff --git a/brag/codegen/runtime.rkt b/brag/codegen/runtime.rkt index 7267de1..0182a61 100755 --- a/brag/codegen/runtime.rkt +++ b/brag/codegen/runtime.rkt @@ -171,7 +171,7 @@ This would be the place to check a syntax property for hiding. (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property)) -(define (remove-rule-name component-stx [splice #f]) +(define (remove-rule-name component-stx #:splice? [splice #f]) ;; when removing a rule name, we apply it as a syntax property to the remaining elements ;; for possible later usage (aka, why throw away information) (with-syntax ([(name . subcomponents) component-stx]) @@ -191,14 +191,17 @@ This would be the place to check a syntax property for hiding. ;; inside `component-stx` is a name followed by subcomponents (for*/list ([component-list (in-list component-lists)] [component-stx (in-list component-list)]) ; this has the effect of omitting any empty `component-list` - (list - (cond - [(eq? (syntax-property component-stx 'hide-or-splice) 'hide) - (list (remove-rule-name component-stx))] ; hidden version still wrapped in a sublist - [(or (eq? (syntax-property component-stx 'hide-or-splice) 'splice) - (syntax-property component-stx 'splice-rh-id)) - (remove-rule-name component-stx #t)] ; spliced version is lifted out of the sublist - [else (list component-stx)]))))) + (list + (cond + ;; test splice first in case both hiding and splicing are set, for instance: + ;; /rule : thing @rule + ;; otherwise the hide prevents the splice from being expressed + [(or (eq? (syntax-property component-stx 'hide-or-splice) 'splice) + (syntax-property component-stx 'splice-rh-id)) + (remove-rule-name component-stx #:splice? #t)] ; spliced version is lifted out of the sublist + [(eq? (syntax-property component-stx 'hide-or-splice) 'hide) + (list (remove-rule-name component-stx))] ; hidden version still wrapped in a sublist + [else (list component-stx)]))))) ;; rule-components->syntax: (U symbol false) (listof stx) ... #:srcloc (U #f (list src line column offset span)) -> stx diff --git a/brag/examples/hide-and-splice.rkt b/brag/examples/hide-and-splice.rkt new file mode 100644 index 0000000..e1e422d --- /dev/null +++ b/brag/examples/hide-and-splice.rkt @@ -0,0 +1,4 @@ +#lang brag +top : x | y +/x : Ø | "x" @x +@y : Ø | "y" y \ No newline at end of file diff --git a/brag/test/test-all.rkt b/brag/test/test-all.rkt index 578eefb..58e326d 100755 --- a/brag/test/test-all.rkt +++ b/brag/test/test-all.rkt @@ -10,6 +10,7 @@ "test-empty-symbol.rkt" "test-errors.rkt" "test-flatten.rkt" + "test-hide-and-splice.rkt" "test-lexer.rkt" "test-old-token.rkt" "test-parser.rkt" diff --git a/brag/test/test-hide-and-splice.rkt b/brag/test/test-hide-and-splice.rkt new file mode 100755 index 0000000..17d19e4 --- /dev/null +++ b/brag/test/test-hide-and-splice.rkt @@ -0,0 +1,9 @@ +#lang racket/base +(require brag/examples/hide-and-splice + brag/support + rackunit) + +;; check that an id with both a splice and hide is handled correctly + +(check-equal? (parse-to-datum "xxx") '(top ("x" "x" "x"))) +(check-equal? (parse-to-datum "yyy") '(top "y" "y" "y")) \ No newline at end of file