-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParserModule.sml
427 lines (379 loc) · 15.2 KB
/
ParserModule.sml
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
(* This is a direct implementation of Ridge combinator parsing. See
Parser.sml for more details and references to the original
papers, whence most of the following code was drawn. *)
signature ParserGeneratorModuleArguments =
sig
type semantic
type symbol
type results = (semantic * substring) list
val symbolBoolDecon : (string -> bool) -> (string -> bool) -> symbol -> bool
val symbolResultsDecon : (string -> results) -> (string -> results) -> symbol -> results
end
signature ParserGeneratorModule =
sig
type semantic
type symbol
type cache = semantic Parser.cachet
exception Semantics of string * semantic list
val grammar_to_parser :
(string -> substring Parser.parser) ->
(string -> semantic) ->
(string * semantic list -> semantic) -> cache ->
(symbol * symbol list list) list ->
symbol -> semantic Parser.parser
end
functor ParserGeneratorModule
(Arguments : ParserGeneratorModuleArguments) : ParserGeneratorModule
where type semantic = Arguments.semantic =
struct
type semantic = Arguments.semantic
type cache = (string * (int * semantic Parser.memo)) Parser.ml ref
datatype symbol = datatype Arguments.symbol
exception Semantics of string * semantic list
open Parser
open ParserMonad
open ParserTerminals
infixr **>
infix |||
infix >>
val always:'a list Parser.parser =
fn i => [([],Parser.substr i)]
val never = fn i:Parser.input => []
fun then_list ([]:'a Parser.parser list) = always
| then_list ((p::ps):'a Parser.parser list) =
((p **> (then_list ps)) >> (op ::))
fun or_list [] = never
| or_list (p::ps) = (p ||| (or_list ps))
fun then_list2 (semantics:string * semantic list -> semantic) =
fn nt => fn ps => then_list ps >> (fn xs => semantics (nt,xs))
fun grammar_to_parser
p_of_tm
(strExn:string -> semantic)
(semantics:string * semantic list -> semantic)
cache =
let val then_list2s = then_list2 semantics
fun linkmp nt =
let fun it ntno' (l as (ref Empty)) =
let val ntno'' = ntno'+1
val mp = ref Empty
val m = Link((nt,(ntno'',mp)),ref Empty)
in
l := m;
(ntno'',mp)
end
| it ntno (ref (Link((nt',(ntno',mp)),l))) =
if nt=nt' then (ntno',mp) else it ntno' l
in it 0
end
fun iter cache g sym =
fn i => Arguments.symbolResultsDecon
(fn tm => ((p_of_tm tm) >>
(fn v => (strExn (Substring.string v)))) i)
(fn nt =>
let val (ntno,mp) = linkmp nt cache
val rules = List.filter
(fn (sym,_) => Arguments.symbolBoolDecon
(fn _ => false)
(fn nt' => nt'=nt) sym) g
val alts1 =
(List.concat o (List.map (fn (_,b) => b)))
rules
val alts2 = List.map
(List.map (fn sym => iter cache g sym))
alts1
val p = or_list (List.map (then_list2s nt) alts2)
in memop mp (check_and_upd_lctxt ntno p) i
end) sym
in
iter cache
end
end
signature ParserGenerator =
sig
type semantic
type cache = semantic Parser.cachet
val generate_parser : (string -> substring Parser.parser) ->
(string -> semantic) ->
(string * semantic list -> semantic) -> cache ->
string -> string -> semantic Parser.parser
end
signature ParserGeneratorArguments =
sig
type semantic
val termCons : string -> semantic
end
functor ParserGenerator
(Arguments: ParserGeneratorArguments) : ParserGenerator =
struct
open Parser
open ParserMonad
open ParserTerminals
infixr **>
infix |||
infix >>
type cache = Arguments.semantic Parser.cachet
exception GrammarError of string
fun assq p l =
let fun iter [] = raise GrammarError "Failed to associate key"
| iter ((k,v)::xs) =
if (k=p) then v else iter xs
in iter l end
fun p_of_tm tm =
let val q = String.substring (tm,0,1)
val stripquotes = fn tm =>
String.substring (tm,1,(String.size tm) - 2)
in
if q = "\"" then parse_a (stripquotes tm)
else if q = "<" then parse_lws_a (stripquotes tm)
else if q = "'" then parse_a (stripquotes tm)
else assq tm [("?AZs?",parse_AZs),
("?azAZs?",parse_azAZs),
("?azs?",parse_azs),
("?notsquote?",parse_notsquote),
("?notdquote?",parse_notdquote),
("?notgt?",parse_notgt),
("?ws?",parse_ws)]
end
val grammar_text =
"RULES -> RULE | RULE ?ws? RULES\n\
\RULE -> SYM ?ws? '->' ?ws? SYMSLIST\n\
\SYMSLIST -> SYMS | SYMS ?ws? '|' ?ws? SYMSLIST\n\
\SYMS -> SYM | SYM ?ws? SYMS\n\
\SYM -> '<' ?notgt? '>' | '\"' ?notdquote? '\"'\n\
\ | \"'\" ?notsquote? \"'\" | ?AZs? | '?' ?azAZs? '?'"
datatype symbol = TM of string
| NT of string
fun symbolDecon f g =
fn sym =>
case sym of
(TM tm) => f tm
| (NT nt) => g nt
datatype production = strProd of string
| symProd of symbol
| symListProd of symbol list
| symListListProd of symbol list list
| ruleProd of symbol * symbol list list
| ruleListProd of (symbol * symbol list list) list
type semantic = Arguments.semantic
exception Semantics of string * production list
structure MetaGrammarParser =
ParserGeneratorModule(type semantic = production
type symbol = symbol
type results = (semantic * substring) list
val symbolBoolDecon = symbolDecon
val symbolResultsDecon = symbolDecon)
structure ObjectGrammarParser =
ParserGeneratorModule(type semantic = semantic
type symbol = symbol
type results = (semantic * substring) list
val symbolBoolDecon = symbolDecon
val symbolResultsDecon = symbolDecon)
fun printParserExn sep p =
let val _ = print sep
val _ = case p of
symProd s => ignore (Meta.printVal s)
| symListProd l => ignore (Meta.printVal l)
| symListListProd l => ignore (Meta.printVal l)
| ruleProd l => ignore (Meta.printVal l)
| ruleListProd l => ignore (Meta.printVal l)
| strProd s => print s
in () end
fun parserFailHandler s es =
let fun iter n [] = ()
| iter n (e::es) =
let val sep = if n = 0 then "" else "\n"
in
printParserExn sep e;
iter (n+1) es
end
in
print ("Exception: Semantics: "^s^"\n");
iter 0 es;
print "\n"
end
fun semantics ps =
case ps of
("RULES",[ruleProd r]) => ruleListProd[r]
| ("RULES",[ruleProd r,_,ruleListProd rs]) => ruleListProd (r::rs)
| ("RULE",[symProd s,_,strProd "->",_,symListListProd l]) => ruleProd (s,l)
| ("SYMSLIST",[symListProd sl]) => symListListProd [sl]
| ("SYMSLIST",[symListProd sl,_,strProd "|",_,symListListProd sls]) =>
symListListProd (sl::sls)
| ("SYMS",[symProd s]) => symListProd[s]
| ("SYMS",[symProd s,_,symListProd ss]) => symListProd (s::ss)
| ("SYM", [strProd "\"",strProd s,_]) => symProd (TM("\"" ^ s ^ "\""))
| ("SYM", [strProd "'",strProd s,_]) => symProd (TM("'" ^ s ^ "'"))
| ("SYM", [strProd "<",strProd s,_]) => symProd (TM("<" ^ s ^ ">"))
| ("SYM", [strProd "?",strProd s,_]) => symProd (TM("?" ^ s ^ "?"))
| ("SYM", [strProd s]) => symProd (NT s)
| (s,ps) => raise Semantics (s,ps)
fun call_parser parser =
fn s =>
let val syntax =
(List.map (fn (x,_) => x)
(List.filter
(fn (l,ss) => Substring.size ss = 0)
(parser (toinput s))))
in
syntax
end
fun extractGrammar [ruleListProd e] = e
| extractGrammar e = raise Semantics ("Bind failed",e)
fun generate_parser1 parser p_of_tm strExn semantics cache grammar top =
let val syntax = extractGrammar (call_parser parser grammar)
in
if syntax = []
then raise Fail "Error: syntax error in grammar"
else MetaGrammarParser.grammar_to_parser
p_of_tm strExn semantics cache syntax (NT top)
end
(* Delia Smith bootstrapping ... *)
val grammar1 =
[(NT "RULES",
[[NT "RULE"], [NT "RULE", TM "?ws?", NT "RULES"]]),
(NT "RULE",
[[NT "SYM", TM "?ws?", TM "'->'", TM "?ws?", NT "SYMSLIST"]]),
(NT "SYMSLIST",
[[NT "SYMS"],
[NT "SYMS", TM "?ws?", TM "'|'", TM "?ws?", NT "SYMSLIST"]]),
(NT "SYMS", [[NT "SYM"], [NT "SYM", TM "?ws?", NT "SYMS"]]),
(NT "SYM",
[[TM "'\"'", TM "?notdquote?", TM "'\"'"],
[TM "\"'\"", TM "?notsquote?", TM "\"'\""], [TM "?AZs?"],
[TM "'?'", TM "?azAZs?", TM "'?'"]])]
val parser1 =
MetaGrammarParser.grammar_to_parser
p_of_tm strProd semantics (ref Empty) grammar1 (NT "RULES")
val grammar2 =
extractGrammar (call_parser parser1 grammar_text)
val parser2 =
MetaGrammarParser.grammar_to_parser
p_of_tm strProd semantics (ref Empty) grammar2 (NT "RULES")
val generate_parser2 = generate_parser1 parser2
val parser3 = generate_parser2 p_of_tm strProd
semantics (ref Empty) grammar_text "RULES"
fun generate_parser3 parser p_of_tm strExn semantics cache grammar top =
let val syntax = extractGrammar (call_parser parser grammar)
in
if syntax = []
then raise Fail "Error: syntax error in grammar"
else ObjectGrammarParser.grammar_to_parser
p_of_tm strExn semantics cache syntax (NT top)
end
val generate_parser = generate_parser3 parser3
end
signature ParserModule =
sig
type semantic
type cache = semantic Parser.cachet
val cache : cache
val parse : string -> semantic list
val showcache : (semantic -> string) -> string -> cache -> string -> unit
end
signature ParserModuleArguments =
sig
type semantic
type cache = semantic Parser.cachet
val cache : cache
val grammar : string
val start : string
val termCons : string -> semantic
val terminals : string -> substring Parser.parser
val semantics : string * semantic list -> semantic
end
functor ParserModule(Arguments : ParserModuleArguments) : ParserModule =
struct
type semantic = Arguments.semantic
type cache = Arguments.cache
val cache = Arguments.cache
val terminals = Arguments.terminals
val termCons = Arguments.termCons
val grammar = Arguments.grammar
val start = Arguments.start
val semantics = Arguments.semantics
local open Parser
structure ParserGenerator =
ParserGenerator(type semantic = semantic
val termCons = termCons)
fun parse_grammar grammar start =
ParserGenerator.generate_parser
terminals termCons semantics cache grammar start
fun get_nts nts l =
case l of
(ref(Link((nt,(ntno,ref(Link(((ctxt,(i',n')),r),l)))),l'))) =>
let val ctxtb = ctxt
in get_nts ((ntno,nt)::nts) l'
end
| _ => rev nts
fun pad l c n s =
let fun iter r 0 = r
| iter r n = iter (if l then c^r else r^c) (n-1)
val sl = UTF8.size s
in if sl < n then iter s (n - sl) else s
end
val padl = pad true
val padr = pad false
val spadl = padl " "
val spadr = padr " "
fun ppexpsublist printer =
let fun iter [] = ()
| iter ((e,ss)::ps) =
let val s = Substring.string ss
val _ = print ("\n [")
val _ = print (""^(printer e)^"")
val _ = print (", \""^s^"\"]")
in
print (if not (null ps) then "," else "");
iter ps
end
in iter
end
fun assq p l =
let fun iter [] = raise Fail "Grammar error"
| iter ((k,v)::xs) =
if (k=p) then v else iter xs
in iter l end
fun ppcache printer nts estr =
let val ntsz = List.foldr
(fn ((_,s),m) =>
let val n = String.size s
in if n > m then n else m end) 0 nts
fun iter m (ref(Link((nt,(ntno,ref(Link(((ctxt,(i',n')),r),l)))),l'))) =
let val lr = length r
val esz = String.size estr
fun istr n = Int.toString n
fun pctxt _ [] = ()
| pctxt m ((ntno,(i,n))::ctxt) =
let val nt = assq ntno nts
in print ("\n "^
(spadl 4 (istr m))^" "^
(spadl 3 (istr ntno))^" "^(spadr ntsz nt)^
(spadl 7 ("("^(istr i)^","^(istr n)^")"))^" = "^
(spadl esz ("\""^(String.substring(estr,i,n))))^"\"");
pctxt (m+1) ctxt
end
val sep = if m > 1 then "\n" else ""
in
if lr > 0 then
(print(sep^" "^nt^" ("^(istr i')^","^(istr n')^") = \""^
(String.substring(estr,i',n'))^"\"");
ppexpsublist printer (rev r);
pctxt 1 ctxt)
else ();
iter (m+1) l'
end
| iter _ _ = ()
in
iter 1
end
in
val parser = parse_grammar grammar start
fun parse s =
(List.map (fn (x,_) => x)
(List.filter
(fn (l,ss) => Substring.size ss = 0)
(parser (Parser.toinput s))))
fun showcache (printer:semantic -> string) suffix mp e =
ppcache printer (get_nts [] mp) (e^suffix) mp
end
end