make it possible to use pollen on inline submodule

pull/102/head
Matthew Butterick 9 years ago
parent d23f4f133a
commit fd1164d02e

@ -1,8 +1,26 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base syntax/strip-context racket/syntax pollen/world) pollen/decode pollen/pagetree racket/list pollen/world markdown) (require (for-syntax racket/base syntax/strip-context racket/syntax pollen/world racket/list) pollen/decode pollen/pagetree racket/list pollen/world markdown)
(provide (all-defined-out) (all-from-out pollen/world)) (provide (all-defined-out) (all-from-out pollen/world))
(define-for-syntax (split-metas tree)
(define (meta-matcher x) ; meta has form (define-meta key value)
(and (list? x) (>= (length x) 3) (eq? (first x) (world:current-define-meta-name))))
(define matches empty)
(define rest
(let loop ([x tree])
(cond
[(meta-matcher x)
(set! matches (cons x matches))
(loop empty)]
[(list? x)
(define-values (new-matches rest) (partition meta-matcher x))
(set! matches (append new-matches matches))
(map loop rest)]
[else x])))
(let ([meta-key second][meta-value third])
(values (map meta-key matches) (map meta-value matches) rest)))
(define-syntax-rule (define+provide-module-begin-in-mode PARSER-MODE-ARG) (define-syntax-rule (define+provide-module-begin-in-mode PARSER-MODE-ARG)
(begin (begin
(provide (except-out (all-from-out racket/base) #%module-begin) (provide (except-out (all-from-out racket/base) #%module-begin)
@ -10,14 +28,25 @@
(define-syntax (pollen-module-begin stx) (define-syntax (pollen-module-begin stx)
(syntax-case stx () (syntax-case stx ()
[(_ EXPR (... ...)) [(_ EXPR (... ...))
(with-syntax ([DOC (format-id #'(EXPR (... ...)) "~a" (world:current-main-export))]) (let-values ([(meta-keys meta-values expr-without-metas) (split-metas (syntax->datum #'(EXPR (... ...))))])
(with-syntax ([(EXPR-WITHOUT-METAS (... ...)) (datum->syntax #'(EXPR (... ...)) expr-without-metas)]
[(KEY (... ...)) (datum->syntax #'(EXPR (... ...)) meta-keys)]
[(VALUE (... ...)) (datum->syntax #'(EXPR (... ...)) meta-values)]
[METAS (format-id #'(EXPR (... ...)) "~a" (world:current-meta-export))]
[META-MOD (format-symbol "~a" (world:current-meta-export))]
[DOC (format-id #'(EXPR (... ...)) "~a" (world:current-main-export))])
(replace-context #'(EXPR (... ...)) (replace-context #'(EXPR (... ...))
#'(#%module-begin #'(#%module-begin
(module META-MOD racket/base
(provide (all-defined-out))
(define METAS (apply hash (append (list 'KEY VALUE) (... ...)))))
(module inner pollen/doclang-raw ; exports result as doc-raw (module inner pollen/doclang-raw ; exports result as doc-raw
(require pollen/top pollen/world) (require pollen/top pollen/world)
(provide #%top (all-defined-out) (all-from-out pollen/world)) (provide #%top (all-defined-out) (all-from-out pollen/world))
EXPR (... ...)) EXPR-WITHOUT-METAS (... ...))
(require 'inner)
(require 'inner 'META-MOD)
(define DOC (define DOC
(let* ([parser-mode-undefined? (procedure? inner:parser-mode)] ; if undefined, #%top makes it a procedure (let* ([parser-mode-undefined? (procedure? inner:parser-mode)] ; if undefined, #%top makes it a procedure
[parser-mode (if parser-mode-undefined? PARSER-MODE-ARG inner:parser-mode)] [parser-mode (if parser-mode-undefined? PARSER-MODE-ARG inner:parser-mode)]
@ -31,4 +60,4 @@
;; drop leading newlines, as they're often the result of `defines` and `requires` ;; drop leading newlines, as they're often the result of `defines` and `requires`
[doc-elements (dropf doc-raw (λ(ln) (equal? ln "\n")))]) [doc-elements (dropf doc-raw (λ(ln) (equal? ln "\n")))])
(apply proc doc-elements))) (apply proc doc-elements)))
(provide DOC (except-out (all-from-out 'inner) doc-raw #%top)))))])))) ; hide internal exports (provide DOC METAS (except-out (all-from-out 'inner) doc-raw #%top))))))])))) ; hide internal exports

@ -8,25 +8,6 @@
(λ(p) (syntax->datum (custom-read-syntax-proc (object-name p) p)))) (λ(p) (syntax->datum (custom-read-syntax-proc (object-name p) p))))
(define (split-metas tree)
(define (meta-matcher x) ; meta has form (define-meta key value)
(and (list? x) (>= (length x) 3) (eq? (first x) (world:current-define-meta-name))))
(define matches empty)
(define rest
(let loop ([x tree])
(cond
[(meta-matcher x)
(set! matches (cons x matches))
(loop empty)]
[(list? x)
(define-values (new-matches rest) (partition meta-matcher x))
(set! matches (append new-matches matches))
(map loop rest)]
[else x])))
(let ([meta-key second][meta-value third])
(values (map meta-key matches) (map meta-value matches) rest)))
(define (make-custom-read-syntax reader-mode) (define (make-custom-read-syntax reader-mode)
(λ (path-string p) (λ (path-string p)
(define read-inner (make-at-reader (define read-inner (make-at-reader
@ -38,7 +19,6 @@
#:syntax? #t #:syntax? #t
#:inside? #t)) #:inside? #t))
(define source-stx (read-inner path-string p)) (define source-stx (read-inner path-string p))
(define-values (meta-keys meta-values meta-free-file-data) (split-metas (syntax->datum source-stx)))
(define reader-here-path (cond (define reader-here-path (cond
[(symbol? path-string) (symbol->string path-string)] [(symbol? path-string) (symbol->string path-string)]
[(equal? path-string "unsaved editor") path-string] [(equal? path-string "unsaved editor") path-string]
@ -53,34 +33,24 @@
[else world:mode-preproc])]) [else world:mode-preproc])])
auto-computed-mode) auto-computed-mode)
reader-mode)) reader-mode))
(define meta-keys-plus-here (cons (world:current-here-path-key) meta-keys)) ; here-path at front so it can be overridden
(define meta-values-plus-here (cons reader-here-path meta-values))
(define post-parser-syntax (define post-parser-syntax
(with-syntax ([(KEY ...) (datum->syntax source-stx meta-keys-plus-here)] (with-syntax ([HERE-KEY (format-id source-stx "~a" (world:current-here-path-key))]
[(VALUE ...) (datum->syntax source-stx meta-values-plus-here)] [HERE-PATH (datum->syntax source-stx reader-here-path)]
[METAS (format-id source-stx "~a" (world:current-meta-export))]
[META-MOD (format-symbol "~a" (world:current-meta-export))]
[POLLEN-MOD (format-symbol "~a" 'pollen-lang-module)] [POLLEN-MOD (format-symbol "~a" 'pollen-lang-module)]
[DOC (format-id source-stx "~a" (world:current-main-export))]
[PARSER-MODE-VALUE (format-symbol "~a" parser-mode)] [PARSER-MODE-VALUE (format-symbol "~a" parser-mode)]
[DIRECTORY-REQUIRES (datum->syntax source-stx (require+provide-directory-require-files path-string))] [DIRECTORY-REQUIRES (datum->syntax source-stx (require+provide-directory-require-files path-string))]
[(SOURCE-LINE ...) (datum->syntax source-stx meta-free-file-data)]) [(SOURCE-LINE ...) source-stx]
[DOC (format-id source-stx "~a" (world:current-main-export))])
(replace-context (replace-context
source-stx source-stx
#'(module runtime-wrapper racket/base #'(module runtime-wrapper racket/base
(module META-MOD racket/base
(provide (all-defined-out))
(define METAS (apply hash (append (list 'KEY VALUE) ...))))
(module POLLEN-MOD pollen (module POLLEN-MOD pollen
(define-meta HERE-KEY HERE-PATH)
(define parser-mode 'PARSER-MODE-VALUE) (define parser-mode 'PARSER-MODE-VALUE)
(provide (except-out (all-defined-out) parser-mode) (provide (except-out (all-defined-out) parser-mode)
(prefix-out inner: parser-mode)) ; avoids conflicts with importing modules (prefix-out inner: parser-mode)) ; avoids conflicts with importing modules
DIRECTORY-REQUIRES DIRECTORY-REQUIRES
(require (submod ".." ".." META-MOD)) ; get metas from adjacent submodule
(provide (all-from-out (submod ".." ".." META-MOD)))
SOURCE-LINE ...) SOURCE-LINE ...)
(require (submod pollen/runtime-config show) 'POLLEN-MOD) (require (submod pollen/runtime-config show) 'POLLEN-MOD)
(provide (all-from-out 'POLLEN-MOD)) (provide (all-from-out 'POLLEN-MOD))
(show DOC inner:parser-mode))))) (show DOC inner:parser-mode)))))

@ -0,0 +1,23 @@
#lang racket/base
(module inline pollen
(define-meta foo 42)
"Zut Alors")
(module test-main racket/base
(require rackunit)
(require (submod ".." inline))
(check-equal? doc "Zut Alors")
(check-equal? metas (hash 'foo 42)))
(module test-metas-submod racket/base
(require rackunit)
(require (submod ".." inline metas))
(let ()
(define-syntax-rule (#%top . xs)
'unbound-identifier)
(check-equal? doc 'unbound-identifier))
(check-equal? metas (hash 'foo 42)))
(require 'test-main)
(require 'test-metas-submod)
Loading…
Cancel
Save