-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.rkt
348 lines (332 loc) · 14.5 KB
/
syntax.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
#lang racket/base
(require racket/string)
(require racket/contract)
(require parser-tools/lex)
(require parser-tools/yacc)
(require (prefix-in : parser-tools/lex-sre))
(define-tokens variable-tokens
(SYMBOL PROPERTY SINGLE-STR DOUBLE-STR POP+MOVE))
(define-empty-tokens constant-tokens
(PUSH MOVE EQ LEFT-CURLY RIGHT-CURLY EOF))
(define-lex-abbrev symbol
(:+ (:or (char-range #\0 #\9)
(char-range #\a #\z)
(char-range #\A #\Z)
#\_)))
(define lwg-lexer
(lexer-src-pos
[(eof) (token-EOF)]
[#\} (token-RIGHT-CURLY)]
[#\{ (token-LEFT-CURLY)]
[#\= (token-EQ)]
[#\- (token-MOVE)]
[#\^ (token-PUSH)]
[whitespace (return-without-pos (lwg-lexer input-port))]
[(:+ #\>) (token-POP+MOVE (- (string-length lexeme) 1))]
[(:: #\" (:* (char-complement #\")) #\")
(token-DOUBLE-STR (substring lexeme 1 (- (string-length lexeme) 1)))]
[(:: #\' (:* (char-complement #\')) #\')
(token-SINGLE-STR (substring lexeme 1 (- (string-length lexeme) 1)))]
[(:: (:or symbol #\*) (:+ (:: #\. (:or symbol #\*))))
(token-PROPERTY (string-split lexeme "."))]
[symbol (token-SYMBOL lexeme)]))
(struct AST (node-type type value start-pos end-pos) #:transparent)
(provide
(contract-out (struct AST
((node-type (one-of/c 'leaf 'branch))
(type (one-of/c 'program
'statement
'graph-statement
'mono-graph-statement
'poly-graph-statement
'assign-statement
'node
'edge-operator
'left-value
'right-value
'assign-statement+))
(value (or/c (list/c symbol? any/c)
(non-empty-listof AST?)))
(start-pos position?)
(end-pos position?)))))
(define (terminal type token-type value start-pos end-pos)
(AST 'leaf type (list token-type value) start-pos end-pos))
(define (non-terminal type subtrees start-pos end-pos)
(define subtrees/list
(if (list? subtrees) subtrees (list subtrees)))
(AST 'branch type subtrees/list start-pos end-pos))
(define lwg-parser
(parser
(src-pos)
(error void)
(tokens variable-tokens constant-tokens)
(grammar
(program ((statement program) (non-terminal 'program
(list $1 $2)
$1-start-pos $2-end-pos))
((statement) (non-terminal 'program $1 $1-start-pos $1-end-pos)))
(statement ((graph-statement) (non-terminal 'statement
$1 $1-start-pos $1-end-pos))
((assign-statement) (non-terminal 'statement
$1 $1-start-pos $1-end-pos)))
(graph-statement ((mono-graph-statement)
(non-terminal 'graph-statement $1
$1-start-pos $1-end-pos))
((poly-graph-statement)
(non-terminal 'graph-statement $1
$1-start-pos $1-end-pos)))
(mono-graph-statement ((PUSH SYMBOL)
(terminal 'mono-graph-statement 'PUSH+SYMBOL
$2 $1-start-pos $2-end-pos)))
(poly-graph-statement ((node edge-operator poly-graph-statement)
(non-terminal 'poly-graph-statement
(list $1 $2 $3)
$1-start-pos $3-end-pos))
((node edge-operator node)
(non-terminal 'poly-graph-statement
(list $1 $2 $3)
$1-start-pos $3-end-pos)))
(node ((SYMBOL) (terminal 'node 'SYMBOL $1 $1-start-pos $1-end-pos))
((PUSH SYMBOL) (terminal 'node 'PUSH+SYMBOL
$2 $1-start-pos $2-end-pos)))
(edge-operator ((MOVE) (terminal 'edge-operator 'MOVE null
$1-start-pos $1-end-pos))
((POP+MOVE) (terminal 'edge-operator 'POP+MOVE $1
$1-start-pos $1-end-pos)))
(assign-statement ((left-value EQ right-value)
(non-terminal 'assign-statement (list $1 $3)
$1-start-pos $3-end-pos))
((left-value)
(non-terminal 'assign-statement $1
$1-start-pos $1-end-pos)))
(left-value ((SYMBOL)
(terminal 'left-value 'SYMBOL $1 $1-start-pos $1-end-pos))
((PROPERTY)
(terminal 'left-value 'PROPERTY $1 $1-start-pos $1-end-pos)))
(right-value ((SYMBOL)
(terminal 'right-value 'SYMBOL $1 $1-start-pos $1-end-pos))
((PROPERTY)
(terminal 'right-value 'PROPERTY $1 $1-start-pos $1-end-pos))
((SINGLE-STR)
(terminal 'right-value 'SINGLE-STR
$1 $1-start-pos $1-end-pos))
((DOUBLE-STR)
(terminal 'right-value 'DOUBLE-STR
$1 $1-start-pos $1-end-pos))
((LEFT-CURLY assign-statement+ RIGHT-CURLY)
(non-terminal 'right-value $2 $1-start-pos $3-end-pos)))
(assign-statement+ ((assign-statement)
(non-terminal 'assign-statement+
$1 $1-start-pos $1-end-pos))
((assign-statement assign-statement+)
(non-terminal 'assign-statement+
(list $1 $2)
$1-start-pos $2-end-pos))))
(start program)
(end EOF)))
(define (read-AST port)
(lwg-parser (lambda () (lwg-lexer port))))
(define (string->AST source)
(read-AST (open-input-string source)))
(provide
(contract-out (read-AST (-> input-port? AST?))
(string->AST (-> string? AST?))))
(module+ test
(require rackunit)
(require racket/match)
(define (token->list token)
(if (symbol? token)
token
(list (token-name token)
(token-value token))))
(define (string->tokens source)
(let ([port (open-input-string source)])
(let collect-next ([tokens null])
(match (lwg-lexer port)
[(position-token 'EOF _ _) (reverse tokens)]
[(position-token token _ _)
(collect-next (cons (token->list token) tokens))]))))
(test-case "Simple graph tokenization test"
(define source "^x-y>>>>z \n^x -^t")
(check-equal? (string->tokens source)
'(PUSH (SYMBOL "x") MOVE (SYMBOL "y")
(POP+MOVE 3) (SYMBOL "z")
PUSH (SYMBOL "x") MOVE PUSH
(SYMBOL "t"))))
(test-case "Simple assignment tokenization test"
(define source "x=10 x.y=x x={y=10 z=20}")
(check-equal? (string->tokens source)
'((SYMBOL "x") EQ (SYMBOL "10") (PROPERTY ("x" "y"))
EQ (SYMBOL "x") (SYMBOL "x")
EQ LEFT-CURLY (SYMBOL "y")
EQ (SYMBOL "10") (SYMBOL "z")
EQ (SYMBOL "20") RIGHT-CURLY)))
(test-case "Single-quoted string tokenization test"
(define source "'hello world!\\n'")
(check-equal? (string->tokens source)
'((SINGLE-STR "hello world!\\n"))))
(test-case "Double-quoted string tokenization test"
(define source "\"hello \\{world\\}\"")
(check-equal? (string->tokens source)
'((DOUBLE-STR "hello \\{world\\}"))))
(test-case "Properties' tokenization test"
(define source "a.b *.a.b *.*")
(check-equal? (string->tokens source)
'((PROPERTY ("a" "b"))
(PROPERTY ("*" "a" "b"))
(PROPERTY ("*" "*")))))
(define (AST->sexp ast)
(match ast
[(AST 'leaf type (list token-type token-value) _ _)
(list type (list token-type token-value))]
[(AST 'branch type subtrees _ _)
(list* type (map AST->sexp subtrees))]))
(define (string->sexp source)
(AST->sexp (string->AST source)))
(test-case "Single node test"
(check-equal? (string->sexp "^x")
'(program
(statement
(graph-statement
(mono-graph-statement (PUSH+SYMBOL "x")))))))
(test-case "Single assignment test"
(check-equal? (string->sexp "x")
'(program
(statement
(assign-statement
(left-value (SYMBOL "x")))))))
(test-case "Simple graph-statement test"
(check-equal? (string->sexp "^a - b")
'(program
(statement
(graph-statement
(poly-graph-statement
(node (PUSH+SYMBOL "a"))
(edge-operator (MOVE ()))
(node (SYMBOL "b")))))))
(check-equal? (string->sexp "a - b >> c")
'(program
(statement
(graph-statement
(poly-graph-statement
(node (SYMBOL "a"))
(edge-operator (MOVE ()))
(poly-graph-statement
(node (SYMBOL "b"))
(edge-operator (POP+MOVE 1))
(node (SYMBOL "c")))))))))
(test-case "Simple assign-statement test"
(define source "node.10.name = 'ten'")
(check-equal? (string->sexp source)
'(program
(statement
(assign-statement
(left-value (PROPERTY ("node" "10" "name")))
(right-value (SINGLE-STR "ten")))))))
(test-case "Compound graph-statements test"
(define source "a > b a > c ^b - c > d")
(check-equal? (string->sexp source)
'(program
(statement
(graph-statement
(poly-graph-statement
(node (SYMBOL "a"))
(edge-operator (POP+MOVE 0))
(node (SYMBOL "b")))))
(program
(statement
(graph-statement
(poly-graph-statement
(node (SYMBOL "a"))
(edge-operator (POP+MOVE 0))
(node (SYMBOL "c")))))
(program
(statement
(graph-statement
(poly-graph-statement
(node (PUSH+SYMBOL "b"))
(edge-operator (MOVE ()))
(poly-graph-statement
(node (SYMBOL "c"))
(edge-operator (POP+MOVE 0))
(node (SYMBOL "d")))))))))))
(test-case "Compound assign-statements test"
(define source "a = b a.b = 'a'")
(check-equal? (string->sexp source)
'(program
(statement
(assign-statement
(left-value (SYMBOL "a"))
(right-value (SYMBOL "b"))))
(program
(statement
(assign-statement
(left-value (PROPERTY ("a" "b")))
(right-value (SINGLE-STR "a"))))))))
(test-case "Mixed statements test"
(define source "a - ^b >> c a = 0 b - c c.a = 'b'")
(check-equal? (string->sexp source)
'(program
(statement
(graph-statement
(poly-graph-statement
(node (SYMBOL "a"))
(edge-operator (MOVE ()))
(poly-graph-statement
(node (PUSH+SYMBOL "b"))
(edge-operator (POP+MOVE 1))
(node (SYMBOL "c"))))))
(program
(statement
(assign-statement
(left-value (SYMBOL "a"))
(right-value (SYMBOL "0"))))
(program
(statement
(graph-statement
(poly-graph-statement
(node (SYMBOL "b"))
(edge-operator (MOVE ()))
(node (SYMBOL "c")))))
(program
(statement
(assign-statement
(left-value (PROPERTY ("c" "a")))
(right-value (SINGLE-STR "b"))))))))))
(test-case "Nested assign-statement test"
(define source "*.x = { a = 10 y = { z = x }}")
(check-equal? (string->sexp source)
'(program
(statement
(assign-statement
(left-value (PROPERTY ("*" "x")))
(right-value
(assign-statement+
(assign-statement
(left-value (SYMBOL "a"))
(right-value (SYMBOL "10")))
(assign-statement+
(assign-statement
(left-value (SYMBOL "y"))
(right-value
(assign-statement+
(assign-statement
(left-value (SYMBOL "z"))
(right-value (SYMBOL "x"))))))))))))))
(test-case "Switch-like assign-statement test"
(define source "^automaton automaton automaton.accepts.state_0")
(check-equal? (string->sexp source)
'(program
(statement
(graph-statement
(mono-graph-statement
(PUSH+SYMBOL "automaton"))))
(program
(statement
(assign-statement
(left-value (SYMBOL "automaton"))))
(program
(statement
(assign-statement
(left-value
(PROPERTY ("automaton" "accepts" "state_0")))))))))))