capybara/template.rkt

50 lines
1.8 KiB
Racket
Raw Normal View History

2021-06-07 02:38:03 +00:00
#lang racket/base
(require racket/base racket/format racket/list racket/match racket/runtime-path racket/string
racket/splicing
2021-06-08 07:14:44 +00:00
"defs.rkt" "util.rkt"
2021-06-07 07:56:53 +00:00
syntax/parse syntax/parse/define racket/stxparam
2021-06-07 02:38:03 +00:00
(for-syntax racket/base racket/match racket/set))
(provide (rename-out [x:#%module-begin #%module-begin]
[x:#%top #%top]
[x:include include])
2021-06-07 09:09:20 +00:00
(except-out (all-from-out racket/base racket/format racket/list racket/match racket/string
2021-06-08 07:14:44 +00:00
"defs.rkt" "util.rkt")
2021-06-07 02:38:03 +00:00
#%module-begin #%top))
2021-06-07 07:56:53 +00:00
(define-syntax-parameter template-id #f)
(define-syntax-parameter template-vars #f)
2021-06-07 02:38:03 +00:00
(define-syntax-parse-rule (x:#%top . id:id)
2021-06-07 07:56:53 +00:00
#:with tem:id (syntax-parameter-value #'template-id)
#:do [(set-add! (syntax-parameter-value #'template-vars)
(syntax->datum #'id))]
(hash-ref tem 'id))
2021-06-07 02:38:03 +00:00
(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
2021-06-07 02:38:03 +00:00
(define-syntax-parse-rule (x:#%module-begin body)
(#%module-begin
2021-06-07 07:56:53 +00:00
(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)