-
Notifications
You must be signed in to change notification settings - Fork 3
/
constants.rkt
41 lines (35 loc) · 1.4 KB
/
constants.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
#lang racket
(provide define-constant-format/parse
define-constant-format
define-constant-parse)
(require syntax/parse/define)
(begin-for-syntax
(define-syntax-class clause
#:attributes (constant string)
[pattern [constant:id string:string]]))
(define-syntax-parser define-constant-format/parse
[(_ formatter:id parser:id (c:clause ...))
(syntax/loc this-syntax
(begin
(define-constant-format formatter (c ...))
(define-constant-parse parser (c ...))))])
(define-syntax-parser define-constant-format
[(_ formatter:id (c:clause ...))
(syntax/loc this-syntax
(define formatter
(let ([table (hash {~@ c.constant c.string} ...)])
(λ (x)
(hash-ref table x (λ ()
(raise-arguments-error 'formatter
"value not eligible for formatting"
"value" x)))))))])
(define-syntax-parser define-constant-parse
[(_ parser:id (c:clause ...))
(syntax/loc this-syntax
(define parser
(let ([table (hash {~@ c.string c.constant} ...)])
(λ (x)
(hash-ref table x (λ ()
(raise-arguments-error 'parser
"string not eligible for parsing"
"string" x)))))))])