-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparsatron.clj
316 lines (274 loc) · 8.93 KB
/
parsatron.clj
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
(ns the.parsatron
(:refer-clojure :exclude [char])
(:require [clojure.string :as str]))
(defrecord InputState [input pos])
(defrecord SourcePos [line column])
(defrecord Continue [fn])
(defrecord Ok [item])
(defrecord Err [errmsg])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; position
(defn inc-sourcepos
"Increment the source position by a single character, c. On newline,
increments the SourcePos's line number and resets the column, on
all other characters, increments the column"
[{:keys [line column]} c]
(if (= c \newline)
(SourcePos. (inc line) 1)
(SourcePos. line (inc column))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; errors
(defprotocol ShowableError
(show-error [this]))
(defrecord ParseError [pos msgs]
ShowableError
(show-error [_] (str (str/join ", " msgs)
" at"
" line: " (:line pos)
" column: " (:column pos))))
(defn unknown-error [{:keys [pos] :as state}]
(ParseError. pos ["Error"]))
(defn unexpect-error [msg pos]
(ParseError. pos [(str "Unexpected " msg)]))
(defn expect-error [msg pos]
(ParseError. pos [(str "Expected " msg)]))
(defn merge-errors [{:keys [pos] :as err} other-err]
(ParseError. pos (flatten (concat (:msgs err) (:msgs other-err)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; trampoline
(defn parsatron-poline
"A trampoline for executing potentially stack-blowing recursive
functions without running out of stack space. This particular
trampoline differs from clojure.core/trampoline by requiring
continuations to be wrapped in a Continue record. Will loop until
the value is no longer a Continue record, returning that."
[f & args]
(loop [value (apply f args)]
(condp instance? value
Continue (recur ((:fn value)))
value)))
(defn sequentially [f value]
(condp instance? value
Continue (Continue. #(sequentially f ((:fn value))))
(f value)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; host environment
(defn fail [message]
(RuntimeException. message))
(defn digit?
"Tests if a character is a digit: [0-9]"
[c]
(Character/isDigit ^Character c))
(defn letter?
"Tests if a character is a letter: [a-zA-Z]"
[c]
(Character/isLetter ^Character c))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; m
(defn always
"A parser that always succeeds with the value given and consumes no
input"
[x]
(fn [state cok cerr eok eerr]
(eok x state)))
(defn bind
"Parse p, and then q. The function f must be of one argument, it
will be given the value of p and must return the q to follow p"
[p f]
(fn [state cok cerr eok eerr]
(letfn [(pcok [item state]
(sequentially
(fn [q] (Continue. #(q state cok cerr cok cerr)))
(f item)))
(peok [item state]
(sequentially
(fn [q] (Continue. #(q state cok cerr eok eerr)))
(f item)))]
(Continue. #(p state pcok cerr peok eerr)))))
(defn nxt
"Parse p and then q, returning q's value and discarding p's"
[p q]
(bind p (fn [_] q)))
(defmacro defparser
"Defines a new parser. Parsers are simply functions that accept the
5 arguments state, cok, cerr, eok, eerr but this macro takes care
of writing that ceremony for you and wraps the body in a >>"
[name args & body]
`(defn ~name ~args
(fn [state# cok# cerr# eok# eerr#]
(let [p# (>> ~@body)]
(Continue. #(p# state# cok# cerr# eok# eerr#))))))
(defmacro >>
"Expands into nested nxt forms"
([m] m)
([m n] `(nxt ~m ~n))
([m n & ms] `(nxt ~m (>> ~n ~@ms))))
(defmacro let->>
"Expands into nested bind forms"
[[& bindings] & body]
(let [[bind-form p] (take 2 bindings)]
(if (= 2 (count bindings))
`(bind ~p (fn [~bind-form] ~@body))
`(bind ~p (fn [~bind-form] (let->> ~(drop 2 bindings) ~@body))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; m+
(defn never
"A parser that always fails, consuming no input"
[]
(fn [state cok cerr eok eerr]
(eerr (unknown-error state))))
(defn either
"A parser that tries p, upon success, returning its value, and upon
failure (if no input was consumed) tries to parse q"
[p q]
(fn [state cok cerr eok eerr]
(letfn [(peerr [err-from-p]
(letfn [(qeerr [err-from-q]
(eerr (merge-errors err-from-p err-from-q)))]
(Continue. #(q state cok cerr eok qeerr))))]
(Continue. #(p state cok cerr eok peerr)))))
(defn attempt
"A parser that will attempt to parse p, and upon failure never
consume any input"
[p]
(fn [state cok cerr eok eerr]
(Continue. #(p state cok eerr eok eerr))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; interacting with the parser's state
(defn extract
"Extract information from the Parser's current state. f should be a
fn of one argument, the parser's current state, and any value that
it deems worthy of returning will be returned by the entire parser.
No input is consumed by this parser, and the state itself is not
altered."
[f]
(fn [state _ _ eok _]
(eok (f state) state)))
(defn examine
"Return the Parser's current state"
[]
(extract identity))
(defn lineno
"A parser that returns the current line number. It consumes no input"
[]
(extract (comp :line :pos)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; token
(defn token
"Consume a single item from the head of the input if (consume? item)
is not nil. This parser will fail to consume if either the consume?
test returns nil or if the input is empty"
[consume?]
(fn [{:keys [input pos] :as state} cok cerr eok eerr]
(if-not (empty? input)
(let [tok (first input)]
(if (consume? tok)
(cok tok (InputState. (rest input) (inc-sourcepos pos tok)))
(eerr (unexpect-error (str "token '" tok "'") pos))))
(eerr (unexpect-error "end of input" pos)))))
(defn many
"Consume zero or more p. A RuntimeException will be thrown if this
combinator is applied to a parser that accepts the empty string, as
that would cause the parser to loop forever"
[p]
(letfn [(many-err [_ _]
(fail "Combinator '*' is applied to a parser that accepts an empty string"))
(safe-p [state cok cerr eok eerr]
(Continue. #(p state cok cerr many-err eerr)))]
(either
(let->> [x safe-p
xs (many safe-p)]
(always (cons x xs)))
(always []))))
(defn times
"Consume exactly n number of p"
[n p]
(if (= n 0)
(always [])
(let->> [x p
xs (times (dec n) p)]
(always (cons x xs)))))
(defn lookahead
"A parser that upon success consumes no input, but returns what was
parsed"
[p]
(fn [state cok cerr eok eerr]
(letfn [(ok [item _]
(eok item state))]
(Continue. #(p state ok cerr eok eerr)))))
(defn choice
"A varargs version of either that tries each given parser in turn,
returning the value of the first one that succeeds"
[& parsers]
(if (empty? parsers)
(never)
(let [p (first parsers)]
(either p (apply choice (rest parsers))))))
(defn eof
"A parser to detect the end of input. If there is nothing more to
consume from the underlying input, this parser suceeds with a nil
value, otherwise it fails"
[]
(fn [{:keys [input pos] :as state} cok cerr eok eerr]
(if (empty? input)
(eok nil state)
(eerr (expect-error "end of input" pos)))))
(defn char
"Consume the given character"
[c]
(token #(= c %)))
(defn any-char
"Consume any character"
[]
(token char?))
(defn digit
"Consume a digit [0-9] character"
[]
(token digit?))
(defn letter
"Consume a letter [a-zA-Z] character"
[]
(token letter?))
(defn string
"Consume the given string"
[s]
(reduce nxt (concat (map char s)
(list (always s)))))
(defn between
"Parse p after parsing open and before parsing close, returning the
value of p and discarding the values of open and close"
[open close p]
(let->> [_ open
x p
_ close]
(always x)))
(defn many1
"Consume 1 or more p"
[p]
(let->> [x p
xs (many p)]
(always (cons x xs))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; run parsers
(defn run-parser
"Execute a parser p, given some state, Returns Ok or Err"
[p state]
(parsatron-poline p state
(fn cok [item _]
(Ok. item))
(fn cerr [err]
(Err. (show-error err)))
(fn eok [item _]
(Ok. item))
(fn eerr [err]
(Err. (show-error err)))))
(defn run
"Run a parser p over some input. The input can be a string or a seq
of tokens, if the parser produces an error, its message is wrapped
in a RuntimeException and thrown, and if the parser succeeds, its
value is returned"
[p input]
(let [result (run-parser p (InputState. input (SourcePos. 1 1)))]
(condp instance? result
Ok (:item result)
Err (throw (fail ^String (:errmsg result))))))