preserve source locations

pull/170/head
Matthew Butterick 7 years ago
parent c60a397365
commit 16bf40bc68

@ -11,7 +11,7 @@
(define is-meta-value? hash?)
(define is-doc-value? txexpr?)
(define+provide define-meta values) ;; stub so it will be picked up for docs
(define+provide define-meta (λ xs "\n")) ;; newline will merge with surroundings
(define+provide current-metas (make-parameter #f))

@ -1,5 +1,5 @@
#lang racket/base
(require (for-syntax racket/base racket/syntax "../setup.rkt" "split-metas.rkt")
(require (for-syntax racket/base racket/syntax syntax/strip-context "../setup.rkt" "split-metas.rkt")
"to-string.rkt" "../pagetree.rkt" "splice.rkt" "../setup.rkt" "../core.rkt"
(prefix-in doclang: "doclang-raw.rkt"))
(provide (except-out (all-from-out racket/base) #%module-begin)
@ -43,29 +43,28 @@
(define-syntax (pollen-module-begin stx)
(syntax-case stx ()
[(_ . EXPRS)
(let-values ([(meta-hash exprs-without-metas) (split-metas (syntax->datum #'EXPRS) (setup:define-meta-name))])
(with-syntax (;; 'parser-mode-from-reader will be #f for an inline submodule
[PARSER-MODE-FROM-READER (syntax-property stx 'parser-mode-from-reader)]
[PARSER-MODE-FROM-EXPANDER (syntax-property #'EXPRS 'parser-mode-from-expander)]
[META-HASH meta-hash]
[EXPRS-WITHOUT-METAS exprs-without-metas]
[METAS-ID (setup:meta-export)]
[META-MOD-ID (setup:meta-export)]
[ROOT-ID (setup:main-root-node)]
[DOC-ID (setup:main-export)])
#'(doclang:#%module-begin
DOC-ID ; positional arg for doclang-raw: name of export
(λ (xs)
(define parser-mode (or 'PARSER-MODE-FROM-READER PARSER-MODE-FROM-EXPANDER))
(define proc (make-parse-proc parser-mode ROOT-ID))
(define doc-elements (splice (strip-leading-newlines xs) (setup:splicing-tag)))
(proc doc-elements)) ; positional arg for doclang-raw: post-processor
(module META-MOD-ID racket/base
(provide METAS-ID)
(define METAS-ID META-HASH))
(require pollen/top pollen/core pollen/setup (submod "." META-MOD-ID))
(provide (all-defined-out) METAS-ID DOC-ID)
(define prev-metas (current-metas))
(and (current-metas METAS-ID) "\n") ; because newlines get stripped, voids don't
(begin . EXPRS-WITHOUT-METAS)
(and (current-metas prev-metas) ""))))])) ; leave behind empty string, not void
(with-syntax (;; 'parser-mode-from-reader will be #f for an inline submodule
[EXPRS (replace-context #'here #'EXPRS)]
[PARSER-MODE-FROM-READER (syntax-property stx 'parser-mode-from-reader)]
[PARSER-MODE-FROM-EXPANDER (syntax-property #'EXPRS 'parser-mode-from-expander)]
[META-HASH (split-metas #'EXPRS (setup:define-meta-name))]
[METAS-ID (setup:meta-export)]
[META-MOD-ID (setup:meta-export)]
[ROOT-ID (setup:main-root-node)]
[DOC-ID (setup:main-export)])
#'(doclang:#%module-begin
DOC-ID ; positional arg for doclang-raw: name of export
(λ (xs)
(define parser-mode (or 'PARSER-MODE-FROM-READER PARSER-MODE-FROM-EXPANDER))
(define proc (make-parse-proc parser-mode ROOT-ID))
(define doc-elements (splice (strip-leading-newlines xs) (setup:splicing-tag)))
(proc doc-elements)) ; positional arg for doclang-raw: post-processor
(module META-MOD-ID racket/base
(provide METAS-ID)
(define METAS-ID META-HASH))
(require pollen/top pollen/core pollen/setup (submod "." META-MOD-ID))
(provide (all-defined-out) METAS-ID DOC-ID)
(define prev-metas (current-metas))
(and (current-metas METAS-ID) "\n") ; because newlines get stripped, voids don't
(begin . EXPRS)
(and (current-metas prev-metas) "")))])) ; leave behind empty string, not void

@ -1,28 +1,20 @@
#lang racket/base
#lang debug racket/base
(require racket/list)
(provide (all-defined-out))
(define (split-metas tree meta-key)
(define missing-sym (gensym))
(define matches null)
(define (meta? x) ; meta has form (define-meta key value)
(and (list? x) (>= (length x) 3) (eq? (car x) meta-key)))
(define rest
(let loop ([x (if (list? tree) tree (list tree))])
(cond
[(meta? x) (set! matches (cons x matches)) missing-sym]
[(list? x) (filter (λ (x) (not (eq? x missing-sym))) (map loop x))]
[else x])))
(values (apply hasheq (apply append (reverse (map cdr matches)))) rest))
(define datums (flatten (if (syntax? tree) (syntax->datum tree) tree)))
(if (>= (length datums) 3)
(for/hasheq ([name (in-list datums)]
[k (in-list (cdr datums))]
[v (in-list (cddr datums))]
#:when (eq? name meta-key))
(values k v))
(hasheq)))
(module+ test
(require rackunit)
(let-values ([(metas rest) (split-metas '(root (div #:kw #f (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam") 'define-meta)])
(check-equal? metas '#hasheq((foo . "boing")))
(check-equal? rest '(root (div #:kw #f "hi") "zim" "zam")))
(let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (define-meta dog "Lex")) 'define-meta)])
(check-equal? metas '#hasheq((dog . "Lex"))))
(let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (div (define-meta dog "Lex"))) 'define-meta)])
(check-equal? metas '#hasheq((dog . "Lex")))))
(check-equal? (split-metas '(root) 'define-meta) (hasheq))
(check-equal? (split-metas '(root (div #:kw #f (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam") 'define-meta) '#hasheq((foo . "boing")))
(check-equal? (split-metas #'(root (define-meta dog "Roxy") (define-meta dog "Lex")) 'define-meta) '#hasheq((dog . "Lex")))
(check-equal? (split-metas #'(root (define-meta dog "Roxy") (div (define-meta dog "Lex"))) 'define-meta) '#hasheq((dog . "Lex"))))

@ -1 +1 @@
1519080530
1519508344

Loading…
Cancel
Save