capybara/template.rkt

50 lines
1.8 KiB
Racket

#lang racket/base
(require racket/base racket/format racket/list racket/match racket/runtime-path racket/string
racket/splicing
"defs.rkt" "util.rkt"
syntax/parse syntax/parse/define racket/stxparam
(for-syntax racket/base racket/match racket/set))
(provide (rename-out [x:#%module-begin #%module-begin]
[x:#%top #%top]
[x:include include])
(except-out (all-from-out racket/base racket/format racket/list racket/match racket/string
"defs.rkt" "util.rkt")
#%module-begin #%top))
(define-syntax-parameter template-id #f)
(define-syntax-parameter template-vars #f)
(define-syntax-parse-rule (x:#%top . id:id)
#:with tem:id (syntax-parameter-value #'template-id)
#:do [(set-add! (syntax-parameter-value #'template-vars)
(syntax->datum #'id))]
(hash-ref tem 'id))
(define-syntax-parse-rule (x:include what:string)
#:with tem:id (syntax-parameter-value #'template-id)
#:with execute-temp:id (car (generate-temporaries '(included-execute)))
#:with execute-fn
(syntax-local-lift-require #`(rename what execute-temp execute) #'execute-temp)
(execute-fn tem))
;; TODO : maybe use make-wrapping-module-begin
(define-syntax-parse-rule (x:#%module-begin body)
(#%module-begin
(provide execute required-keys)
(define-values [execute required-keys]
(syntax-parameterize ([template-vars (mutable-seteq)])
(values (λ (tem)
(syntax-parameterize ([template-id #'tem])
body))
(x:required-ids-list))))))
(define-syntax-parse-rule (x:required-ids-list)
#:with (items ...)
(map (λ (stx) #`(quote #,stx)) (set->list (syntax-parameter-value #'template-vars)))
(list items ...))
(module reader syntax/module-reader
capybara/template)