-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.rkt
358 lines (333 loc) · 14.7 KB
/
runtime.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#lang racket/base
(require "exn.rkt")
(require "trie.rkt")
(require "normalize.rkt")
(require racket/contract)
(require racket/match)
(require parser-tools/lex)
(struct handler (name operation) #:constructor-name make-handler)
(struct runtime (trie [handlers #:mutable] data))
(provide [struct-out handler]
[struct-out runtime])
(define (empty-runtime)
(runtime (empty-trie) null (make-hasheq)))
(define (add-handler! runtime handler)
(let ([handlers (runtime-handlers runtime)])
(cond [(findf (lambda (h) (eq? (handler-name h) (handler-name handler)))
handlers)
#f]
[else (set-runtime-handlers! runtime
(append handlers (list handler)))
#t])))
(define (get-runtime-variable runtime path)
(trie-ref (runtime-trie runtime) path))
(define (set-runtime-variable! runtime path new-value)
(trie-set! (runtime-trie runtime) new-value path))
(define (get-runtime-data runtime handler-name)
(hash-ref (runtime-data runtime) handler-name #f))
(define (set-runtime-data! runtime handler-name new-value)
(hash-set! (runtime-data runtime) handler-name new-value))
(define (handle runtime assignment #:collect-result [collect? #t])
(let ([result-table (and collect? (make-immutable-hasheq))])
(for ([handler (runtime-handlers runtime)])
(let* ([name (handler-name handler)]
[result ((handler-operation handler) runtime assignment)])
(and result-table (set! result-table
(hash-set result-table name result)))))
(or result-table (void))))
(provide
(contract-out [empty-runtime (-> runtime?)]
[add-handler! (-> runtime? handler? any/c)]
[get-runtime-variable (-> runtime? (listof string?) string?)]
[set-runtime-variable!
(-> runtime? (listof string?) string? any/c)]
[get-runtime-data (-> runtime? symbol? any/c)]
[set-runtime-data! (-> runtime? symbol? any/c any/c)]
[handle (->* (runtime? assignment?)
(#:collect-result boolean?)
(or/c hash? void?))]))
(define runtime-service-table (make-hasheq))
(define (find-runtime-service kwd handler-name runtime assignment)
(let ([service (hash-ref runtime-service-table kwd #f)])
(if service
(service handler-name runtime assignment)
(raise (exn:fail:lwg-runtime-error
(format "Unknown runtime service: ~A" kwd)
(current-continuation-marks))))))
(define-syntax-rule (define-runtime-service kwd
(handler-name runtime assignment) body0 body ...)
(hash-set! runtime-service-table 'kwd
(lambda (handler-name runtime assignment) body0 body ...)))
(define-runtime-service #:data (handler-name runtime assignment)
(get-runtime-data runtime handler-name))
(define-runtime-service #:set-data! (handler-name runtime assignment)
(lambda (new-value)
(set-runtime-data! runtime handler-name new-value)))
(define-runtime-service #:var (handler-name runtime assignment)
(lambda path (get-runtime-variable runtime path)))
(define-runtime-service #:set-var! (handler-name runtime assignment)
(lambda (value . path) (set-runtime-variable! runtime path value)))
(define-runtime-service #:start-pos (handler-name runtime assignment)
(assignment-start-pos assignment))
(define-runtime-service #:end-pos (handler-name runtime assignment)
(assignment-end-pos assignment))
(define-syntax-rule (define-handler id clauses ...)
(define-handler-helper id (#:ini () #:fin () #:cas () #:use ()) clauses ...))
(define-syntax define-handler-helper
(syntax-rules (=)
[(_ id (#:ini (ini ...) #:fin fin #:cas cas #:use use)
#:initially expr body ...)
(define-handler-helper id
(#:ini (ini ... expr) #:fin fin #:cas cas #:use use)
body ...)]
[(_ id (#:ini ini #:fin (fin ...) #:cas cas #:use use)
#:finally expr body ...)
(define-handler-helper id
(#:ini ini #:fin (fin ... expr) #:cas cas #:use use)
body ...)]
[(_ id (#:ini ini #:fin fin #:cas (cas ...) #:use use)
#:case [(path-pat path-pats ... = value-pat) expr exprs ...]
body ...)
(define-handler-helper id
(#:ini ini #:fin fin #:cas (cas ... [(list (list path-pat path-pats ...)
value-pat)
expr exprs ...])
#:use use)
body ...)]
[(_ id collectors
#:case [(path-pat path-pats ...) expr exprs ...]
body ...)
(define-handler-helper id collectors
#:case [(path-pat path-pats ... = _) expr exprs ...]
body ...)]
[(_ id (#:ini ini #:fin fin #:cas cas #:use (use ...))
#:use [kwd var] body ...)
(define-handler-helper id
(#:ini ini #:fin fin #:cas cas
#:use (use ... [var kwd]))
body ...)]
[(_ id collectors #:use [] body ...)
(define-handler-helper id collectors body ...)]
[(_ id collectors #:use [kwd var rest ...] body ...)
(define-handler-helper id collectors
#:use [kwd var] #:use [rest ...] body ...)]
[(_ id (#:ini (ini ...) #:fin (fin ...) #:cas (cas ...)
#:use ([var kwd] ...)))
(define id
(make-handler 'id
(lambda (runtime assignment)
(define var
(find-runtime-service 'kwd 'id runtime assignment)) ...
ini ...
(begin0 (match (list (assignment-path assignment)
(assignment-value assignment))
cas ...
[_ #f])
fin ...))))]))
(provide define-runtime-service
define-handler)
(module+ test
(require rackunit)
(require racket/string)
(require racket/list)
(test-case "Duplicate handler test"
(define h (make-handler 'h (lambda _ 0)))
(define runtime (empty-runtime))
(check-true (add-handler! runtime h))
(check-false (add-handler! runtime h))
(check-equal? (length (runtime-handlers runtime))
1))
(test-case "Primitive handler test"
(define runtime (empty-runtime))
(define output (open-output-string))
(add-handler! runtime
(make-handler 'path-handler
(lambda (_ a)
(write 1 output)
(assignment-path a))))
(add-handler! runtime
(make-handler 'value-handler
(lambda (_ a)
(write 2 output)
(assignment-value a))))
(add-handler! runtime
(make-handler 'both-handler
(lambda (_ a)
(write 3 output)
(list (assignment-path a)
(assignment-value a)))))
(define sample (make-assignment '("a" "b") "c" #:from null #:to null))
(check-pred void? (handle runtime sample #:collect-result #f))
(check-equal? (handle runtime sample)
#hasheq((both-handler . (("a" "b") "c"))
(path-handler . ("a" "b"))
(value-handler . "c")))
(check-equal? (get-output-string output)
"123123"))
(test-case "define-handler: initial clause and final clause test"
(define output (open-output-string))
(define-handler ordered-handler
#:initially (display 1 output)
#:finally (display 2 output))
(define-handler chaos-handler
#:finally (display 4 output)
#:initially (display 1 output)
#:initially (display 2 output)
#:finally (display 5 output)
#:initially (display 3 output))
(define runtime (empty-runtime))
(add-handler! runtime ordered-handler)
(add-handler! runtime chaos-handler)
(handle runtime (make-assignment '("a") "b" #:from #f #:to #f))
(check-equal? (get-output-string output) "1212345"))
(test-case "define-handler: use-data clause test"
(define output (open-output-string))
(define-handler path-handler
#:initially (fprintf output "~A;" prefix)
#:finally (fprintf output "~A" prefix)
#:case [(x ...) (cons prefix x)]
#:use [#:data prefix])
(define runtime (empty-runtime))
(set-runtime-data! runtime 'path-handler "file://")
(add-handler! runtime path-handler)
(define result-table
(handle runtime (make-assignment '("/opt") #:from #f #:to #f)))
(check-equal? result-table
#hasheq((path-handler . ("file://" "/opt"))))
(check-equal? (get-output-string output)
"file://;file://"))
(test-case "Multiple assignments test"
(define-handler graph-handler
#:use [#:data graph-box]
#:case [("graph" "node" name)
(set-box! graph-box
(append (unbox graph-box)
(list (format "[N] ~A" name))))]
#:case [("graph" "edge" from to)
(set-box! graph-box
(append (unbox graph-box)
(list (format "[E] ~A -> ~A" from to))))]
#:case [("graph" "name" = (and (regexp #px"\\w+\\s+Graph") name))
(set-box! graph-box
(append (unbox graph-box)
(list (format "<<~A>>" name))))])
(define-handler debug-handler
#:use [#:data output]
#:case [(path ... = value)
(fprintf output "~A = ~A;" (string-join path ".") value)])
(define runtime (empty-runtime))
(define debug-output (open-output-string))
(set-runtime-data! runtime 'graph-handler (box null))
(set-runtime-data! runtime 'debug-handler debug-output)
(add-handler! runtime graph-handler)
(add-handler! runtime debug-handler)
(define assignment-list
(list (make-assignment '("graph" "name") "???? Graph")
(make-assignment '("graph" "name") "My Graph")
(make-assignment '("graph" "node" "a") "_")
(make-assignment '("graph" "edge" "a" "b") "_")
(make-assignment '("graph" "node" "b") "_")
(make-assignment '("graph" "edge" "a" "c") "_")
(make-assignment '("graph" "node" "c") "_")
(make-assignment '("graph" "edge" "c" "d") "_")
(make-assignment '("graph" "node" "d") "_")))
(for ([assignment assignment-list])
(handle runtime assignment #:collect-result #f))
(check-equal? (unbox (get-runtime-data runtime 'graph-handler))
'("<<My Graph>>"
"[N] a"
"[E] a -> b"
"[N] b"
"[E] a -> c"
"[N] c"
"[E] c -> d"
"[N] d"))
(define debug (get-output-string (get-runtime-data runtime 'debug-handler)))
(check-equal? (string-split debug ";")
'("graph.name = ???? Graph"
"graph.name = My Graph"
"graph.node.a = _"
"graph.edge.a.b = _"
"graph.node.b = _"
"graph.edge.a.c = _"
"graph.node.c = _"
"graph.edge.c.d = _"
"graph.node.d = _")))
(test-case "Service test -- data/set-data!"
(define output (open-output-string))
(define-handler state-handler
#:use [#:data state #:set-data! set-state!]
#:case
[("state" "set" = value) (set-state! value)]
#:case
[("state" "show") (display state output)])
(define runtime (empty-runtime))
(add-handler! runtime state-handler)
(set-runtime-data! runtime 'state-handler "1")
(define assignment-list
(list (make-assignment '("state" "show"))
(make-assignment '("state" "set") "0")
(make-assignment '("state" "show"))
(make-assignment '("state" "show"))
(make-assignment '("state" "set") "^")
(make-assignment '("state" "show"))
(make-assignment '("state" "set") "0")
(make-assignment '("state" "show"))
(make-assignment '("state" "set") "1")
(make-assignment '("state" "show"))
(make-assignment '("state" "show"))))
(for ([assignment assignment-list])
(handle runtime assignment))
(check-equal? (get-output-string output) "100^011"))
(test-case "Service test -- var/set-var!"
(define output (open-output-string))
(define-handler variable-handler
#:use [#:var var #:set-var! set-var!]
#:initially (set-var! "0" "always" "zero")
#:case
[("show" path ...) (fprintf output "/~A/;" (apply var path))]
#:case
[(path ... = value) (apply set-var! value path)])
(define runtime (empty-runtime))
(add-handler! runtime variable-handler)
(define assignment-list
(list (make-assignment '("show" "always" "zero"))
(make-assignment '("show" "do" "not" "exist"))
(make-assignment '("node" "text") "ROOT")
(make-assignment '("show" "node" "text"))
(make-assignment '("show" "node" "text" "__SELF__"))
(make-assignment '("show" "node"))
(make-assignment '("node" "__SELF__") "Marcus")
(make-assignment '("show" "node"))
(make-assignment '("show" "node" "__SELF__"))
(make-assignment '("show" "node" "text"))))
(for ([assignment assignment-list])
(handle runtime assignment))
(define result (get-output-string output))
(check-equal? (string-split result ";")
'("/0/"
"//"
"/ROOT/"
"/ROOT/"
"//"
"/Marcus/"
"/Marcus/"
"/ROOT/")))
(test-case "Service test -- start-pos/end-pos"
(define-handler debug-handler
#:use [#:start-pos start-pos #:end-pos end-pos]
#:use [#:data pos-list #:set-data! set-pos-list!]
#:initially (set-pos-list! (append pos-list
(list (list start-pos end-pos)))))
(define runtime (empty-runtime))
(add-handler! runtime debug-handler)
(set-runtime-data! runtime 'debug-handler null)
(define position-list
(list (list (make-position 0 0 0) (make-position 0 0 1))
(list (make-position 0 1 0) (make-position 0 1 1))
(list (make-position 1 0 0) (make-position 1 0 1))))
(for ([pos (in-list position-list)])
(handle runtime (make-assignment '("a") "b"
#:from (first pos) #:to (second pos))))
(check-equal? (get-runtime-data runtime 'debug-handler)
position-list)))