support expressions in meta values

meta-expr
Matthew Butterick 6 years ago
parent 014807df58
commit 91a6a95698

@ -36,7 +36,7 @@
(syntax-case stx () (syntax-case stx ()
[(_ PARSER-MODE . EXPRS) [(_ PARSER-MODE . EXPRS)
(with-syntax ([EXPRS (replace-context #'here #'EXPRS)] (with-syntax ([EXPRS (replace-context #'here #'EXPRS)]
[META-HASH (split-metas #'EXPRS (setup:define-meta-name))] [((META-KEY . META-VAL) ...) (split-metas #'EXPRS (setup:define-meta-name))]
[METAS-ID (setup:meta-export)] [METAS-ID (setup:meta-export)]
[META-MOD-ID (setup:meta-export)] [META-MOD-ID (setup:meta-export)]
[ROOT-ID (setup:main-root-node)] [ROOT-ID (setup:main-root-node)]
@ -51,7 +51,9 @@
(proc doc-elements))) ; positional arg for doclang-raw: post-processor (proc doc-elements))) ; positional arg for doclang-raw: post-processor
(module META-MOD-ID racket/base (module META-MOD-ID racket/base
(provide METAS-ID) (provide METAS-ID)
(define METAS-ID META-HASH)) (define METAS-ID (for/hasheq ([k (in-list (list 'META-KEY ...))]
[v (in-list (list META-VAL ...))])
(values k v))))
(require pollen/top pollen/core pollen/setup (submod "." META-MOD-ID)) (require pollen/top pollen/core pollen/setup (submod "." META-MOD-ID))
(provide (all-defined-out) METAS-ID DOC-ID) (provide (all-defined-out) METAS-ID DOC-ID)
(define prev-metas (current-metas)) (define prev-metas (current-metas))

@ -4,22 +4,23 @@
(provide (all-defined-out)) (provide (all-defined-out))
(define (split-metas x [meta-key 'define-meta]) (define (split-metas x [meta-key 'define-meta])
(apply hasheq
(let loop ([x ((if (syntax? x) syntax->datum values) x)]) (let loop ([x ((if (syntax? x) syntax->datum values) x)])
(match x (match x
[(list (== meta-key eq?) key)
(raise-argument-error meta-key "meta value missing" key)]
[(list (== meta-key eq?) key val) [(list (== meta-key eq?) key val)
(unless (symbol? key) (unless (symbol? key)
(raise-argument-error meta-key "valid meta key" key)) (raise-argument-error meta-key "valid meta key" key))
(list key val)] (list (cons key val))]
[(? list? xs) (append-map loop xs)] [(? list? xs) (append-map loop xs)]
[_ null])))) [_ null])))
(module+ test (module+ test
(require rackunit) (require rackunit)
(check-equal? (split-metas 'root) (hasheq)) (check-equal? (split-metas 'root) null)
(check-equal? (split-metas '(root)) (hasheq)) (check-equal? (split-metas '(root)) null)
(check-exn exn:fail:contract? (λ () (split-metas '(root (define-meta 42 "bar"))))) (check-exn exn:fail:contract? (λ () (split-metas '(root (define-meta 42 "bar")))))
(check-equal? (split-metas '(root (div #:kw #f (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam")) '#hasheq((foo . "boing"))) (check-equal? (split-metas '(root (div #:kw #f (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam")) '((foo . "bar") (foo . "boing")))
(check-equal? (split-metas '(root (div #:kw #f (define-meta foo 'bar) "hi") "zim" (define-meta foo 'boing) "zam")) '#hasheq((foo . 'boing))) (check-equal? (split-metas '(root (div #:kw #f (define-meta foo 'bar) "hi") "zim" (define-meta foo 'boing) "zam")) '((foo . 'bar) (foo . 'boing)))
(check-equal? (split-metas #'(root (define-meta dog "Roxy") (define-meta dog "Lex"))) '#hasheq((dog . "Lex"))) (check-equal? (split-metas #'(root (define-meta dog "Roxy") (define-meta dog "Lex"))) '((dog . "Roxy") (dog . "Lex")))
(check-equal? (split-metas #'(root (define-meta dog "Roxy") (div (define-meta dog "Lex")))) '#hasheq((dog . "Lex")))) (check-equal? (split-metas #'(root (define-meta dog "Roxy") (div (define-meta dog "Lex")))) '((dog . "Roxy") (dog . "Lex"))))
Loading…
Cancel
Save