make ids hideable

pull/2/head
Matthew Butterick 9 years ago
parent f072c9f808
commit 2026c603de

@ -216,29 +216,29 @@
(for/list ([translated-pattern (in-list translated-patterns)] (for/list ([translated-pattern (in-list translated-patterns)]
[primitive-pattern (syntax->list a-clause)] [primitive-pattern (syntax->list a-clause)]
[pos (in-naturals 1)]) [pos (in-naturals 1)])
(with-syntax ([$X (if (syntax-property primitive-pattern 'hide)
(format-id translated-pattern "$~a" pos)] #'null
[$X-start-pos (with-syntax ([$X
(format-id translated-pattern "$~a-start-pos" pos)] (format-id translated-pattern "$~a" pos)]
[$X-end-pos [$X-start-pos
(format-id translated-pattern "$~a-end-pos" pos)]) (format-id translated-pattern "$~a-start-pos" pos)]
(syntax-case primitive-pattern (id lit token inferred-id) [$X-end-pos
(format-id translated-pattern "$~a-end-pos" pos)])
;; When a rule usage is inferred, the value of $X is a syntax object (syntax-case primitive-pattern (id lit token inferred-id)
;; whose head is the name of the inferred rule . We strip that out,
;; leaving the residue to be absorbed. ;; When a rule usage is inferred, the value of $X is a syntax object
[(inferred-id val reason) ;; whose head is the name of the inferred rule . We strip that out,
#'(syntax-case $X () ;; leaving the residue to be absorbed.
[(inferred-rule-name . rest) [(inferred-id val reason)
(syntax->list #'rest)])] #'(syntax-case $X ()
[(id val) [(inferred-rule-name . rest)
#'(list $X)] (syntax->list #'rest)])]
;; move the 'hide syntax property into the translated-action [(id val)
;; because syntax gets datum-ized #'(list $X)]
[(lit val) [(lit val)
#`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))] #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))]
[(token val) [(token val)
#`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))])))) #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))])))))
(define whole-rule-loc (define whole-rule-loc
(if (empty? translated-patterns) (if (empty? translated-patterns)

@ -150,15 +150,12 @@ This would be the place to check a syntax property for hiding.
(define stx-with-original?-property (define stx-with-original?-property
(read-syntax #f (open-input-string "meaningless-string"))) (read-syntax #f (open-input-string "meaningless-string")))
(define elided (gensym))
;; atomic-datum->syntax: datum position position ;; atomic-datum->syntax: datum position position
;; Helper that does the ugly work in wrapping a datum into a syntax ;; Helper that does the ugly work in wrapping a datum into a syntax
;; with source location. ;; with source location.
(define (atomic-datum->syntax d start-pos end-pos [hide? #f]) (define (atomic-datum->syntax d start-pos end-pos)
(if hide? (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property))
elided
(datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property)))
@ -166,10 +163,9 @@ This would be the place to check a syntax property for hiding.
;; Creates an stx out of the rule name and its components. ;; Creates an stx out of the rule name and its components.
;; The location information of the rule spans that of its components. ;; The location information of the rule spans that of its components.
(define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] . components) (define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] . components)
(define flattened-elided-components (filter-not (λ(c) (eq? c elided)) (apply append components)))
(datum->syntax #f (datum->syntax #f
(cons (cons
(datum->syntax #f rule-name/false srcloc stx-with-original?-property) (datum->syntax #f rule-name/false srcloc stx-with-original?-property)
flattened-elided-components) (apply append components))
srcloc srcloc
stx-with-original?-property)) stx-with-original?-property))

@ -1,4 +1,4 @@
#lang brag #lang brag
;; Simple baby example of JSON structure thing : foo
json: ID <":"> ID foo : <"bar">

@ -14,4 +14,4 @@ array: "[" [json ("," json)*] "]"
object: <"{"> [kvpair ("," kvpair)*] <"}"> object: <"{"> [kvpair ("," kvpair)*] <"}">
kvpair: <ID> <":"> json kvpair: <ID> ":" <json>

@ -0,0 +1,9 @@
#lang br
(require "json-elider-toy.rkt"
brag/support
rackunit)
(check-equal?
(syntax->datum
(parse (list "bar")))
'(thing))

@ -10,10 +10,10 @@
":" ":"
(token 'STRING "'hello world'") (token 'STRING "'hello world'")
"}"))) "}")))
'(json (object (kvpair (json (string "'hello world'")))))) '(json (object (kvpair "message" (json (string "'hello world'"))))))
(check-equal? #;(check-equal?
(syntax->datum (syntax->datum
(parse "[[[{}]],[],[[{}]]]")) (parse "[[[{}]],[],[[{}]]]"))
'(json (array #\[ (json (array #\[ (json (array #\[ (json (object)) #\])) #\])) #\, (json (array #\[ #\])) #\, (json (array #\[ (json (array #\[ (json (object )) #\])) #\])) #\]))) '(json (array #\[ (json (array #\[ (json (array #\[ (json (object)) #\])) #\])) #\, (json (array #\[ #\])) #\, (json (array #\[ (json (array #\[ (json (object )) #\])) #\])) #\])))

Loading…
Cancel
Save