-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ml
More file actions
298 lines (243 loc) · 8.2 KB
/
Copy pathparser.ml
File metadata and controls
298 lines (243 loc) · 8.2 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
open Prelude
open Endofunctors
module StringParser = struct
module Error = struct
type t = {
msg : string ;
remainder : string ;
}
end
module PResult = Result.Make (Error)
module ParserMonad = struct
type 'output t =
string -> (('output, string) result * string)
let pure x input = (PResult.ok x, input)
let bind prsr k input =
let open PResult in
let (mresult1, remainder1) = prsr input in
match mresult1 with
| Ok value -> (k value) remainder1
| Error _ as err -> (err, remainder1)
end
include Monad.ToApplicative (ParserMonad)
let alternative prsr1 prsr2 input =
match prsr1 input with
| Error _, _ -> prsr2 input
| _ -> prsr1 input
let (<|>) = alternative
let consumes_input prsr input
= snd (prsr input) <> input
module Combinators = struct
let succeed input = PResult.ok input
let fail msg remainder =
(PResult.error msg, remainder)
let choice prsrs = List.foldl (<|>) (fail "choice") prsrs
let optional prsr = prsr *> pure () <|> pure ()
let satisfy ?(msg_override = "") pred =
let open String in
let compute_override ?(got = "EOF") msg =
if msg = ""
then sprintf "satisfy: got: %s" got
else sprintf "expected: %s got: %s" msg got
in
function
| "" ->
let msg =
compute_override msg_override
in
let remainder = "" in
(PResult.error msg, remainder)
| str ->
let head = str.[0] in
let got = sub str 0 1 in
let tail = sub str 1 (length str - 1) in
let msg =
compute_override ~got:got msg_override
in
let remainder = str in
if pred head
then (PResult.ok head, tail)
else (PResult.error msg, remainder)
let eof = function
| "" -> (PResult.ok (), "")
| other ->
let msg =
sprintf "expected: EOF got: %c" other.[0]
in
let remainder = other in
(PResult.error msg , remainder)
let char c =
let msg = String.make 1 c in
satisfy ~msg_override:msg (fun x -> x = c)
let string str =
let concat_char strP chr =
let+ str = strP
and+ chr = char chr in
str ^ String.make 1 chr
in
String.foldl concat_char (pure "") str
let rec many prsr input =
match prsr input with
| Ok _, _ -> (pure cons <*> prsr <*> many prsr) input
| Error _, _ -> (pure []) input
let many1 prsr = pure cons <*> prsr <*> many prsr
let negate_parser prsr msg input =
match prsr input with
| Error _, _ -> pure () @@ input
| _ -> fail msg @@ input
let sep_by1 prsr sep =
let msg = "error: too many separators" in
let a_bunch =
let+ initials = many1 (prsr <* sep)
and+ final = prsr in
initials @ [final]
in
let just_one =
let+ final = prsr <* negate_parser sep msg in
[final]
in
a_bunch <|> just_one
let is_space chr =
String.(mem chr whitespace)
let spaces = many1 @@ satisfy @@ is_space
let spaces1 = satisfy is_space <* spaces
let rec sequence = function
| [] -> pure []
| x :: xs -> let+ p1 = x
and+ p2 = sequence xs
in cons p1 p2
let chainl op p =
let rec apply_all x = function
| [] -> x
| f :: fs -> apply_all (f x) fs
in
apply_all <$> p <*> many (flip <$> op <*> p)
let pack l prsr r = string l *> prsr <* string r
let op (func, str) = pure func <* string str
let any_op input = (choice << List.map op) input
let mk_expr atomic ranking left right =
let rec expr input = (foldr chainl factor ranking) input
and factor input = (atomic <|> pack left expr right) input
in (expr, factor)
module RunParser = struct
let compute_line size str =
let open String in
let fragment = take size str in
let reducer count chr =
if chr == '\n'
then count + 1
else count
in
foldl reducer 1 fragment
let trim_lines str =
let open String in
let non_newline c = c <> '\n' in
takewhile non_newline (rev str) |> rev
let parse_string prsr str =
match prsr str with
| Ok output, _ -> PResult.ok output
| Error e, remainder ->
let offset = String.(length str - length remainder) in
let line = compute_line offset str in
let line_msg = sprintf "Line: %i " line in
let col =
let open String in
(length @@ take offset @@ trim_lines str) + 1
in
let col_msg = sprintf "Column: %i " col in
PResult.error (line_msg ^ col_msg ^ e)
end
include RunParser
end
include Combinators
end
module TokenParser = struct
module type TOKEN = sig
include FOLDABLE
type tok
type stream = tok t
val pop : 'a t -> 'a option * 'a t
val cons : 'a -> 'a t -> 'a t
val re_append : 'a t -> 'a t -> 'a t
val rev: 'a t -> 'a t
end
(* IS short for 'input stream'
* OS short for 'output stream' *)
module Make (OS: TOKEN) (IS : TOKEN) = struct
module PResult = Result.Make (String)
module ParseResult = struct
type ('a, 'b) t = { result : ('a, 'b) PResult.t ;
consumed : IS.tok IS.t
}
end
module ParserMonad = struct
type 'output t =
IS.stream -> (('output * IS.stream), string) result
let pure x = fun stream -> PResult.ok (x, stream)
let bind prsr k = let open PResult in
fun input ->
let* (result1, remainder1) = prsr input in
(k result1) remainder1
end
include Monad.ToApplicative (ParserMonad)
let run_parser prsr input = prsr input
module Combinators = struct
let alternative prsr1 prsr2 input =
match prsr1 input with
| Error _ -> prsr2 input
| _ -> prsr1 input
let (<|>) = alternative
let succeed input = PResult.ok input
let fail _ = PResult.error "error: pfail"
let choice prsrs = List.foldl (<|>) fail prsrs
let optional prsr = prsr *> pure () <|> pure ()
let satisfy pred input =
match IS.pop input with
| None, _ -> PResult.error "end of file"
| Some x, xs -> begin
if pred x
then PResult.ok (x, xs)
else fail (IS.cons x xs)
end
let eof input =
match IS.pop input with
| None, _ -> PResult.ok ((), IS.empty)
| Some x, xs -> fail (IS.cons x xs)
let rec many prsr input =
match prsr input with
| Ok _ -> (pure OS.cons <*> prsr <*> many prsr) input
| Error _ -> (pure OS.empty) input
let many1 prsr = pure OS.cons <*> prsr <*> many prsr
let sep_by1 prsr sepPrsr =
let+ initial = many (prsr <* sepPrsr)
and+ final = prsr
in
OS.(append initial @@ cons final empty)
let munch1 pred = many1 (satisfy pred)
let token tok = satisfy (eq tok)
let tokens toks =
let concat_tok toksP tok =
let+ toks = toksP
and+ token = token tok in
OS.(append toks @@ cons token empty)
in
OS.(foldl concat_tok @@ pure empty) toks
let chainl op p =
let rec apply_all x stream =
match OS.pop stream with
| None, _ -> x
| Some f, fs -> apply_all (f x) (fs)
in
apply_all <$> p <*> many (flip <$> op <*> p)
let any_op input =
let op (func, toks) = pure func <* tokens toks in
(choice << List.map op) input
let mk_expr atomic ranking left right =
let pack l prsr r = tokens l *> prsr <* tokens r in
let rec expr input = (foldr chainl factor ranking) input
and factor input = (atomic <|> pack left expr right) input
in (expr, factor)
end
include Combinators
end
end