Repository navigation
Expand file tree
/
Copy pathgrammar.toc
More file actions
358 lines (271 loc) · 7.06 KB
/
Copy pathgrammar.toc
File metadata and controls
358 lines (271 loc) · 7.06 KB
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
(deftype parser-apply [pf args]
Stringable
(string-list [_]
(list "(parser-apply " (str pf)
"\n[" (to-str (interpose args "\n")) "])"))
Container
(map [r f]
(let [new-pf (f pf)
new-args (map args f)]
(parser-apply new-pf new-args)))
(map [r f embed]
(for [new-pf (f pf)
new-args (map args f embed)]
(parser-apply new-pf new-args))))
(deftype parser-always [value]
Stringable
(string-list [_]
(list "(parser-always " (str value) ")"))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(defn always [v]
(parser-always v))
(deftype error-value [msg]
Stringable
(string-list [_]
(list (str "(error-value " msg ")")))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(defn error [& msgs]
(-> msgs
(interpose " ")
to-str
error-value))
(deftype parser-get-text []
Stringable
(string-list [_]
(list (str "(parser-get-text)")))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(def get-text (parser-get-text))
(deftype error-catcher [handler rule]
Stringable
(string-list [_]
(list (str "(error-catcher " handler " " rule ")")))
Container
(map [x f]
(.rule x (f x)))
(map [x f embed]
(map (f x)
(partial .rule x))))
(defn catch-error [f rule]
(error-catcher f rule))
(deftype get-state-value [key]
Stringable
(string-list [_]
(list (str "(get-state-value '" key ")")))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(defn get-value [key]
(get-state-value key))
(deftype update-state-value [key f]
Stringable
(string-list [_]
(list "(update-state-value " (str key) " " (str f) ")"))
Container
(map [x _]
x)
(map [x _ embed]
(embed x)))
(defn update-value [key f]
(update-state-value key f))
(deftype parser-not-char [test-c]
Stringable
(string-list [_]
(list "(parser-not-char " (str "\"" (escape-chars test-c) "\"") ")"))
Container
(map [x _]
x)
(map [x _ embed]
(embed x)))
(defn not-char [test-c]
(assert (instance? String test-c))
(parser-not-char test-c))
(deftype parser-char-range [lower higher]
Stringable
(string-list [_]
(list "(parser-char-range " (str "\"" lower "\"") " " (str "\"" higher "\"") ")"))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(defn char-range [lower higher]
(assert (instance? String lower))
(assert (instance? String higher))
(parser-char-range lower higher))
(deftype parser-term-fn [f]
Stringable
(string-list [_]
(list "(parser-term-fn " (str f) ")"))
Container
(map [x _]
x)
(map [x _ embed]
(embed x)))
(defn term-fn [f]
(parser-term-fn f))
(deftype parser-terminal [term-str]
(assert (instance? String term-str))
Stringable
(string-list [_] (list "(parser-terminal "
(str "\"" (escape-chars term-str) "\"") ")"))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
;; too much of a perf hit to write in terms of 'term-fn'
;; but maybe not now
(defn term [term-str]
(parser-terminal term-str))
(defprotocol StringTerminal
(string-terminal [rule]
rule))
(extend-type String
StringTerminal
(string-terminal [s]
(term s)))
(deftype repeat-rule [rule]
Stringable
(string-list [_]
(list "(repeat-rule " (str rule) ")"))
Container
(map [_ f]
(repeat-rule (f rule)))
(map [_ f embed]
(map (f rule)
repeat-rule)))
(defn one-or-more [rule]
(repeat-rule rule))
(deftype none-or-more-rule [rule]
Stringable
(string-list [_]
(list "(none-or-more " (str rule) ")"))
Container
(map [_ f]
(none-or-more-rule (f rule)))
(map [_ f embed]
(map (f rule)
none-or-more-rule)))
(defn none-or-more [rule]
(none-or-more-rule rule))
(deftype ignore-rule [rule]
Stringable
(string-list [_]
(list "(ignore-rule " (str rule) ")"))
Container
(map [_ f]
(ignore-rule (f rule)))
(map [_ f embed]
(map (f rule) ignore-rule)))
(defn ignore [rule]
(ignore-rule rule))
(deftype Any-rule [rules]
(assert (instance? Vector rules))
;; min-count rules 1
Stringable
(string-list [_] (comp (list "(Any-rule [")
(flat-map (interpose rules "\n") string-list)
(list "])")))
Container
(map [_ f]
(Any-rule (map rules f)))
(map [_ f embed]
(map (map rules f embed)
Any-rule)))
(defn any [& rules]
(Any-rule (vec rules)))
(deftype All-rule [rules]
(assert (instance? Vector rules))
;; min-count rules 1
Stringable
(string-list [_] (comp (list "(All-rule [")
(flat-map (interpose rules "\n") string-list)
(list "])")))
Container
(map [_ f]
(All-rule (map rules f)))
(map [_ f embed]
(map (map rules f embed)
All-rule)))
(defn all [& rules]
(All-rule (vec rules)))
(defn optional
([rule] (optional (parser-apply (parser-always maybe)
[rule])
nothing))
([rule default-value]
(any rule (parser-always default-value))))
(deftype recursion-rule [rule-name]
Stringable
(string-list [_]
(list "(recursion-rule " rule-name ")"))
Container
(map [x f]
x)
(map [x f embed]
(embed x)))
(defn recurse [rule-name]
(recursion-rule rule-name))
(deftype parser-rule [name rule]
Stringable
(string-list [_] (comp (list "(parser-rule\n" (str "\"" name "\"") "\n")
(string-list rule)
(list ")")))
Container
(map [_ f]
(parser-rule name (f rule)))
(map [_ f embed]
(map (f rule)
(partial parser-rule name))))
(defn rule [rule-name grammar]
(parser-rule rule-name grammar))
(def lower-alpha (rule "lower-alpha" (char-range "a" "z")))
(def upper-alpha (rule "upper-alpha" (char-range "A" "Z")))
(def alpha (rule "alpha" (any lower-alpha upper-alpha)))
(def digit (rule "digit" (char-range "0" "9")))
(def alphanum (rule "alphanum" (any alpha digit)))
(def hex (rule "hex" (any digit
(char-range "a" "f")
(char-range "A" "F"))))
(defn one-of [coll]
(apply any (vec coll)))
(defn apply-fn [f & rules]
(apply f (map rules string-terminal)))
(def ParserElement (any-of parser-always
parser-apply
error-value
parser-rule
parser-get-text
error-catcher
get-state-value
update-state-value
parser-not-char
parser-char-range
parser-term-fn
parser-terminal
none-or-more-rule
All-rule
Any-rule
ignore-rule
repeat-rule
recursion-rule))
(extend-type ParserElement
Container
(wrap [_ x]
(parser-always x))
(apply [xf xs]
(parser-apply xf xs)))