-
Notifications
You must be signed in to change notification settings - Fork 4
/
enumeration.ss
233 lines (205 loc) · 8.61 KB
/
enumeration.ss
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#lang scheme/base
(require (for-syntax scheme/base
srfi/26
"debug.ss"
"enumeration-info.ss"
"for.ss"
"syntax.ss")
scheme/provide-syntax
scheme/string
"base.ss"
"enum-internal.ss"
"exn.ss"
"match.ss")
; Syntax -----------------------------------------
; (_ id (value-clause ...) keyword-arg ...)
(define-syntax (define-enum complete-stx)
(define id-stx #f) ; (U syntax #f)
(define value-id-stxs null) ; (listof syntax)
(define value-expr-stxs null) ; (listof syntax)
(define pretty-stxs null) ; (listof syntax)
(define equality-test-stx #f) ; syntax
; syntax syntax -> void
(define (parse-values stx other-stx)
(syntax-case stx ()
[() (parse-keywords other-stx)]
[([id temp-val str] other ...)
(identifier? #'id)
; Treat value of _ specially:
(with-syntax ([val (if (eq? (syntax->datum #'temp-val) '_) #''id #'temp-val)])
(set! value-id-stxs (cons #'id value-id-stxs))
(set! value-expr-stxs (cons #'val value-expr-stxs))
(set! pretty-stxs (cons #'str pretty-stxs))
(parse-values #'(other ...) other-stx))]
[([id val] other ...)
(identifier? #'id)
(with-syntax ([str (format "~a" (syntax->datum #'id))])
(parse-values #'([id val str] other ...) other-stx))]
[([id] other ...)
(identifier? #'id)
(with-syntax ([str (format "~a" (syntax->datum #'id))])
(parse-values #'([id 'id str] other ...) other-stx))]
[(id other ...)
(identifier? #'id)
(parse-values #'([id] other ...) other-stx)]
[(bad-clause other ...)
(raise-syntax-error #f "bad enum clause" complete-stx #'bad-clause)]))
(define (parse-keywords stx)
(syntax-case stx ()
[() (parse-finish)]
[(#:equality-test proc other ...)
(if equality-test-stx
(raise-syntax-error #f "#:equality-test specified multiple times" complete-stx #'(#:equality-test proc))
(begin (set! equality-test-stx #'proc)
(parse-keywords #'(other ...))))]
[(kw other ...)
(keyword? (syntax->datum #'kw))
(raise-syntax-error #f "unrecognised define-enum keyword" complete-stx #'kw)]))
(define (parse-finish)
(with-syntax ([id id-stx]
[private-id (make-id #f id-stx)]
[(value-id ...) (reverse value-id-stxs)]
[(value-private-id ...) (generate-temporaries (reverse value-id-stxs))]
[(value ...) (reverse value-expr-stxs)]
[(pretty ...) (reverse pretty-stxs)]
[equality-test (or equality-test-stx #'eq?)])
(syntax/loc complete-stx
(begin
(define value-private-id value) ...
(define private-id
(make-enum
'id
(list value-private-id ...)
(list (let ([temp pretty])
(if (string? temp)
temp
(raise-type-error 'define-enum "pretty value must be a string" pretty)))
...)
equality-test))
(define-syntaxes (id)
(let ([certify (syntax-local-certifier #t)])
(enum-info-add!
(make-enum-info
(certify #'id)
(certify #'private-id)
(list (certify #'value-id) ...)
(list (certify #'value-private-id) ...)))))))))
(syntax-case complete-stx ()
[(_ id (value ...) kw ...)
(identifier? #'id)
(begin (set! id-stx #'id)
(parse-values #'(value ...) #'(kw ...)))]))
(define-syntax (enum-list stx)
(syntax-case stx ()
[(_ enum-id val-id ...)
(andmap identifier? (syntax->list #'(enum-id val-id ...)))
(syntax/loc stx
(list (enum-id val-id) ...))]))
(define-syntax (enum-complement stx)
(syntax-case stx ()
[(_ enum-id val-id ...)
(andmap identifier? (syntax->list #'(enum-id val-id ...)))
(let ([val-ids (map syntax->datum (syntax->list #'(val-id ...)))]
[all-ids (enum-info-value-ids (enum-info-ref #'enum-id))])
(quasisyntax/loc stx
(list #,@(for/filter ([id (in-list all-ids)])
(and (not (memq (syntax->datum id) val-ids))
#`(enum-id #,id))))))]))
(define-syntax (enum-case complete-stx)
; enum-info syntax -> void
(define (check-cases info cases-stx)
(for ([case-stx (in-list (syntax->list cases-stx))])
(check-case info case-stx)))
; enum-info syntax -> void
(define (check-case info case-stx)
(let ([all-ids (map syntax->datum (enum-info-value-ids info))])
(unless (andmap identifier? (syntax->list case-stx))
(raise-syntax-error #f "bad clause syntax: expected (listof value-id)"
complete-stx
case-stx))
(for ([id-stx (in-list (syntax->list case-stx))])
(unless (memq (syntax->datum id-stx) all-ids)
(raise-syntax-error #f "identifier not found in enum" complete-stx id-stx)))))
; enum-info (listof syntax) -> void
(define (check-completeness info cases-stx)
(let* ([case-stxs (syntax->list cases-stx)]
[all-ids (map syntax->datum (enum-info-value-ids info))]
[used-ids (map syntax->datum (apply append (map syntax->list case-stxs)))])
(for ([all-id (in-list all-ids)])
(unless (memq all-id used-ids)
(raise-syntax-error #f (format "case not covered by clauses: ~a" all-id)
complete-stx)))))
; enum-info (listof syntax) -> void
(define (find-unused-ids info cases-stx)
(let* ([case-stxs (syntax->list cases-stx)]
[all-ids (map syntax->datum (enum-info-value-ids info))]
[used-ids (map syntax->datum (apply append (map syntax->list case-stxs)))])
(for/fold ([accum null])
([all-id (in-list all-ids)])
(if (memq all-id used-ids)
accum
(cons all-id accum)))))
(syntax-case* complete-stx (else) symbolic-identifier=?
; Expression has an 'else' clause:
[(_ enum-id id [(value-id ...) expr ...] ... [else else-expr ...])
(identifier? #'enum-id)
(let* ([info (enum-info-ref #'enum-id)])
(check-cases info #'((value-id ...) ...))
(with-syntax ([(unused-id ...) (find-unused-ids info #'((value-id ...) ...))])
(syntax/loc complete-stx
(match id
[(or (? (cut enum-same? enum-id (enum-id value-id) <>)) ...) expr ...] ...
[(or (? (cut enum-same? enum-id (enum-id unused-id) <>)) ...) else-expr ...]
[other else-expr ...]))))]
; Expression has no 'else' clause:
[(_ enum-id id [(value-id ...) expr ...] ...)
(identifier? #'enum-id)
(let* ([info (enum-info-ref #'enum-id)])
(check-cases info #'((value-id ...) ...))
(check-completeness info #'((value-id ...) ...))
(syntax/loc complete-stx
(match id
[(or (? (cut enum-same? enum-id (enum-id value-id) <>)) ...) expr ...] ...
[other (error (format "enum-case ~a: value not in enumeration" 'enum-id)
other)])))]))
(define-syntax (enum-lambda stx)
(syntax-case stx ()
[(_ enum clause ...)
(syntax/loc stx
(lambda (val)
(enum-case enum val clause ...)))]))
; (_ enum)
; (_ enum id ...)
(define-syntax (in-enum stx)
(syntax-case stx ()
[(_ enum)
(identifier? #'enum)
#'(in-list (enum-values enum))]
[(_ enum val ...)
(andmap identifier? (syntax->list #'(enum val ...)))
#'(in-list (enum-list enum val ...))]))
; (_ enum)
; (_ enum id ...)
(define-syntax (in-enum/pretty stx)
(syntax-case stx ()
[(_ enum)
(identifier? #'enum)
#'(in-list (map (cut enum-prettify enum <>) (enum-values enum)))]
[(_ enum val ...)
(andmap identifier? (syntax->list #'(enum val ...)))
#'(in-list (map (cut enum-prettify enum <>) (enum-list enum val ...)))]))
; Helpers ----------------------------------------
; (U boolean symbol integer) -> string
(define (enum-value->string val)
(cond [(boolean? val) (if val "yes" "no")]
[(symbol? val) (symbol->string val)]
[(integer? val) (number->string val)]))
; Provide statements -----------------------------
(provide (all-from-out "enum-internal.ss")
define-enum
enum-list
enum-complement
enum-case
enum-lambda
in-enum
in-enum/pretty)