-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rkt
37 lines (31 loc) · 1.22 KB
/
main.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
;; https://news.ycombinator.com/item?id=14242481#14244305
#lang racket
(require
(rename-in racket (#%datum core-datum))
(for-syntax syntax/parse))
(define-syntax (#%datum stx)
(syntax-parse stx
[(_ . x:str) (interpolate #'x)]
[(_ . x) #'(core-datum . x)]))
(define-for-syntax (interpolate stx)
(define re #rx"@[^\\]?{[^}]+}")
(define (trim match) (substring match 2 (- (string-length match) 1)))
(define (to-stx val) (datum->syntax stx val))
(define (datum val) (to-stx (cons #'core-datum val)))
(define (source text) (to-stx (read (open-input-string text))))
(define (unescape text) (regexp-replace* #rx"@\\\\{" text "@{"))
(let* ([text (syntax->datum stx)]
[matches (regexp-match* re text)]
[template (datum (unescape (regexp-replace* re text "~a")))]
[values (map (compose source trim) matches)])
(if (null? matches)
template
(to-stx `(,#'format ,template ,@values)))))
(provide #%datum)
(module+ test
(require rackunit)
(define answer 42)
(define two 2)
(check-equal? "What's the answer? 42" "What's the answer? @{answer}")
(check-equal? "4 + 2 = 6" "4 + 2 = @{(+ 4 2)}")
(check-equal? "4 + 2 = 6" "4 + @{two} = @{(+ 4 2)}"))