-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre.rkt
65 lines (53 loc) · 1.87 KB
/
pre.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#lang racket/base
(require "module-lang-utils.rkt"
"to-string.rkt"
racket/contract
(for-syntax racket/base))
(provide (rename-out [md-module-begin #%module-begin]
[tag-top #%top])
(except-out (all-from-out racket/base)
#%module-begin
#%top))
; create reader module
(module reader racket/base
(require syntax/strip-context
scribble/reader)
(provide (rename-out [md-read read]
[md-read-syntax read-syntax]))
; create an @ reader to parse @ expressions
(define my-at-reader (make-at-reader #:inside? #t
#:syntax? #t))
; read just uses read-syntax defined below
(define (md-read in)
(syntax->datum
(md-read-syntax #f in)))
; use the @ reader to seperate out actual expressions from text
; for example, the program:
;
; @string-append["hi " "there"]asdf123(+ 1 1)
;
; would become the syntax object:
;
; #'((string-append "hi "
; "there")
; "asdf123(+ 1 1)")
;
; then we give the parsed expression to whatever `module-name` is
; which takes care of whether it should be treated as markdown
; or plain text or whatever
(define (md-read-syntax src in)
(let ([stx (my-at-reader src in)])
(strip-context
#`(module anything frollen/pre
(define-meta path #,src)
#,@stx)))))
; end of reader module
; convert values to strings, conglomerate into 1 string,
; and ignore given root-proc
(define/contract (accumulate-to-string list-of-values root-proc)
(-> list? procedure? string?)
(apply string-append (map to-string list-of-values)))
; create a string accumulating module begin using
; `accumulate-to-string` to parse the accumulated string
(define-syntax md-module-begin
(make-accumulating-module-begin #'accumulate-to-string))