-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc.ml
More file actions
336 lines (274 loc) · 9.63 KB
/
pc.ml
File metadata and controls
336 lines (274 loc) · 9.63 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
(* pc.ml
* The new implementation of the parsing-combinators package for ocaml
*
* Programmer: Mayer Goldberg, 2024
*)
(* general list-processing procedures *)
let list_of_string string =
let rec run i s =
if i < 0 then s
else run (i - 1) (string.[i] :: s) in
run (String.length string - 1) [];;
let string_of_list s =
List.fold_left
(fun str ch -> str ^ (String.make 1 ch))
""
s;;
let string_of_file input_file =
let in_channel = open_in input_file in
let rec run () =
try
let ch = input_char in_channel in ch :: (run ())
with End_of_file ->
( close_in in_channel;
[] )
in string_of_list (run ());;
let rec ormap f s =
match s with
| [] -> false
| car :: cdr -> (f car) || (ormap f cdr);;
let rec andmap f s =
match s with
| [] -> true
| car :: cdr -> (f car) && (andmap f cdr);;
module PC = struct
type 'a parsing_result = {
index_from : int;
index_to : int;
found : 'a
};;
type 'a parser = string -> int -> 'a parsing_result;;
(* the parsing combinators defined here *)
exception X_not_yet_implemented of string;;
exception X_no_match;;
let const pred =
((fun str index ->
if (index < String.length str) && (pred str.[index])
then {
index_from = index;
index_to = index + 1;
found = str.[index]
}
else raise X_no_match) : 'a parser);;
let caten (nt_1 : 'a parser) (nt_2 : 'b parser) =
((fun str index ->
let {index_from = index_from_1;
index_to = index_to_1;
found = e_1} = (nt_1 str index) in
let {index_from = index_from_2;
index_to = index_to_2;
found = e_2} = (nt_2 str index_to_1) in
{index_from = index_from_1;
index_to = index_to_2;
found = (e_1, e_2)}) : (('a * 'b) parser));;
let pack (nt : 'a parser) (f : 'a -> 'b) =
((fun str index ->
let {index_from; index_to; found} = (nt str index) in
{index_from; index_to; found = (f found)})
: 'b parser);;
let nt_epsilon =
((fun str index ->
{index_from = index;
index_to = index;
found = []}) : 'a parser);;
let caten_list nts =
List.fold_right
(fun nt1 nt2 ->
pack (caten nt1 nt2)
(fun (e, es) -> (e :: es)))
nts
nt_epsilon;;
let disj (nt1 : 'a parser) (nt2 : 'a parser) =
((fun str index ->
try (nt1 str index)
with X_no_match -> (nt2 str index)) : 'a parser);;
let nt_none = ((fun _str _index -> raise X_no_match) : 'a parser);;
let nt_fail = nt_none;;
let disj_list nts = List.fold_right disj nts nt_none;;
let delayed (thunk : unit -> 'a parser) =
((fun str index -> thunk() str index) : 'a parser);;
let nt_end_of_input str index =
if (index < String.length str)
then raise X_no_match
else {index_from = index; index_to = index; found = []};;
let rec star (nt : 'a parser) =
((fun str index ->
try let {index_from = index_from_1;
index_to = index_to_1;
found = e} = (nt str index) in
let {index_from = index_from_rest;
index_to = index_to_rest;
found = es} = (star nt str index_to_1) in
{index_from = index_from_1;
index_to = index_to_rest;
found = (e :: es)}
with X_no_match -> {index_from = index; index_to = index; found = []})
: 'a list parser);;
let plus nt =
pack (caten nt (star nt))
(fun (e, es) -> (e :: es));;
let rec power nt = function
| 0 -> nt_epsilon
| n -> pack(caten nt (power nt (n - 1)))
(fun (e, es) -> e :: es);;
let at_least nt n =
pack (caten (power nt n) (star nt))
(fun (es_1, es_2) -> es_1 @ es_2);;
let only_if (nt : 'a parser) pred =
((fun str index ->
let ({index_from; index_to; found} as result) = (nt str index) in
if (pred found) then result
else raise X_no_match) : 'a parser);;
let maybe (nt : 'a parser) =
((fun str index ->
try let {index_from; index_to; found} = (nt str index) in
{index_from; index_to; found = Some(found)}
with X_no_match ->
{index_from = index; index_to = index; found = None})
: 'a option parser);;
let diff nt1 nt2 =
((fun str index ->
match (maybe nt1 str index) with
| {index_from; index_to; found = None} -> raise X_no_match
| {index_from; index_to; found = Some(e)} ->
match (maybe nt2 str index) with
| {index_from = _; index_to = _; found = None} ->
{index_from; index_to; found = e}
| _ -> raise X_no_match) : 'a parser);;
let followed_by (nt1 : 'a parser) (nt2 : 'b parser) =
((fun str index ->
let ({index_from; index_to; found} as result) = (nt1 str index) in
let _ = (nt2 str index_to) in
result) : 'a parser);;
let not_followed_by (nt1 : 'a parser) (nt2 : 'b parser) =
((fun str index ->
match (let ({index_from; index_to; found} as result) = (nt1 str index) in
try let _ = (nt2 str index_to) in
None
with X_no_match -> (Some(result))) with
| None -> raise X_no_match
| Some(result) -> result) : 'a parser);;
(* useful general parsers for working with text *)
let make_char equal ch1 = const (fun ch2 -> equal ch1 ch2);;
let char = make_char (fun ch1 ch2 -> ch1 = ch2);;
let char_ci =
make_char (fun ch1 ch2 ->
(Char.lowercase_ascii ch1) =
(Char.lowercase_ascii ch2));;
let make_word char str =
List.fold_right
(fun nt1 nt2 -> pack (caten nt1 nt2) (fun (a, b) -> a :: b))
(List.map char (list_of_string str))
nt_epsilon;;
let word = make_word char;;
let word_ci = make_word char_ci;;
let make_one_of char str =
List.fold_right
disj
(List.map char (list_of_string str))
nt_none;;
let one_of = make_one_of char;;
let one_of_ci = make_one_of char_ci;;
let nt_whitespace = const (fun ch -> ch <= ' ');;
let make_range leq ch1 ch2 =
const (fun ch -> (leq ch1 ch) && (leq ch ch2));;
let range = make_range (fun ch1 ch2 -> ch1 <= ch2);;
let range_ci =
make_range (fun ch1 ch2 ->
(Char.lowercase_ascii ch1) <=
(Char.lowercase_ascii ch2));;
let nt_any = ((fun str index -> const (fun ch -> true) str index) : 'a parser);;
let trace_pc desc (nt : 'a parser) =
((fun str index ->
try let ({index_from; index_to; found} as value) = (nt str index)
in
(Printf.printf ";;; %s matched from char %d to char %d, leaving %d chars unread\n"
desc
index_from index_to
((String.length str) - index_to) ;
value)
with X_no_match ->
(Printf.printf ";;; %s failed\n"
desc ;
raise X_no_match)) : 'a parser);;
let unitify nt = pack nt (fun _ -> ());;
let make_separated_by_power nt_sep n nt =
let nt1 = pack (caten nt_sep nt) (fun (_, e) -> e) in
let nt1 = caten nt (power nt1 n) in
let nt1 = pack nt1 (fun (e, es) -> e :: es) in
nt1;;
let make_separated_by_plus nt_sep nt =
let nt1 = pack (caten nt_sep nt) (fun (_, e) -> e) in
let nt1 = caten nt (star nt1) in
let nt1 = pack nt1 (fun (e, es) -> e :: es) in
nt1;;
let make_separated_by_star nt_sep nt =
let nt1 = make_separated_by_plus nt_sep nt in
let nt1 = pack (maybe nt1)
(function
| None -> []
| Some es -> es) in
nt1;;
let maybeify nt none_value =
pack (maybe nt)
(function
| None -> none_value
| Some e -> e);;
let make_wrapped nt_left nt_right nt =
let nt1 = caten nt_left (caten nt nt_right) in
let nt1 = pack nt1 (fun (_, (e, _)) -> e) in
nt1;;
(* testing the parsers *)
let test_string (nt : 'a parser) str index =
nt str index;;
let search_forward (nt : 'a parser) str =
let limit = String.length str in
let rec run i =
if (i < limit)
then (match (maybe nt str i) with
| {index_from; index_to; found = None} -> run (i + 1)
| {index_from; index_to; found = Some(e)} ->
{index_from; index_to; found = e})
else raise X_no_match in
run 0;;
let search_forward_all_with_overlap (nt : 'a parser) str =
let limit = String.length str in
let rec run i =
if (i < limit)
then (match (maybe nt str i) with
| {index_from; index_to; found = None} -> run (i + 1)
| {index_from; index_to; found = Some(e)} ->
{index_from; index_to; found = e} :: (run (i + 1)))
else [] in
run 0;;
let search_forward_all_without_overlap (nt : 'a parser) str =
let limit = String.length str in
let rec run i =
if (i < limit)
then (match (maybe nt str i) with
| {index_from; index_to; found = None} -> run (i + 1)
| {index_from; index_to; found = Some(e)} ->
{index_from; index_to; found = e} :: (run index_to))
else [] in
run 0;;
let search_backward (nt : 'a parser) str =
let rec run i =
if (-1 < i)
then (match (maybe nt str i) with
| {index_from; index_to; found = None} -> run (i - 1)
| {index_from; index_to; found = Some(e)} ->
{index_from; index_to; found = e})
else raise X_no_match in
run (String.length str - 1);;
let search_backward_all_with_overlap (nt : 'a parser) str =
let limit = String.length str in
let rec run i =
if (-1 < i)
then (match (maybe nt str i) with
| {index_from; index_to; found = None} -> run (i - 1)
| {index_from; index_to; found = Some(e)} ->
{index_from; index_to; found = e} :: (run (i - 1)))
else [] in
run (limit - 1);;
end;; (* end of struct PC *)
(* end-of-input *)