1signature PEGParse =
2sig
3
4  datatype ('tok,'nt,'value)pegsym =
5           empty of 'value
6         | any of 'tok -> 'value
7         | tok of ('tok -> bool) * ('tok -> 'value)
8         | nt of 'nt * ('value -> 'value)
9         | seq of ('tok,'nt,'value)pegsym * ('tok,'nt,'value)pegsym *
10                  ('value -> 'value -> 'value)
11         | choice of ('tok,'nt,'value)pegsym * ('tok,'nt,'value)pegsym *
12                     ('value -> 'value) * ('value -> 'value)
13         | rpt of ('tok,'nt,'value)pegsym * ('value list -> 'value)
14         | not of ('tok,'nt,'value)pegsym * 'value
15
16  datatype ('tok,'nt,'value) grammar =
17           PEG of { start : ('tok,'nt,'value)pegsym,
18                    rules : 'nt -> ('tok,'nt,'value)pegsym option }
19
20  datatype ('source,'tok,'nt,'value)kont =
21           ksym of ('tok,'nt,'value)pegsym * ('source,'tok,'nt,'value)kont *
22                   ('source,'tok,'nt,'value)kont
23         | appf1 of ('value -> 'value) * ('source,'tok,'nt,'value)kont
24         | appf2 of ('value -> 'value -> 'value) * ('source,'tok,'nt,'value)kont
25         | returnTo of 'source * 'value option list *
26                       ('source,'tok,'nt,'value)kont
27         | poplist of ('value list -> 'value) * ('source,'tok,'nt,'value)kont
28         | listsym of ('tok,'nt,'value)pegsym * ('value list -> 'value) *
29                      ('source,'tok,'nt,'value)kont
30         | kdone
31         | kfailed
32
33
34  val pegexec : ('nt -> ('tok,'nt,'value) pegsym) ->
35                ('source -> ('source * 'tok) option) ->
36                ('tok,'nt,'value)pegsym ->
37                'source ->
38                'value option list ->
39                ('source,'tok,'nt,'value)kont ->
40                ('source,'tok,'nt,'value)kont ->
41                ('source * 'value) option
42
43end
44
45(* [pegexec ntmap gettok sym input stk success failure]
46
47   The input/gettok pairing must be functional. In particular, PEGs
48   support backtracking so sensible behaviour can only be guaranteed
49   if repeated calls to the same input ('source) arguments always give
50   the same result.
51
52   The standard initial call to pegexec should be
53
54     pegexec ntmap gettok symbol input [] kdone kfailed
55*)
56