-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mld
More file actions
191 lines (183 loc) · 6.61 KB
/
Copy pathindex.mld
File metadata and controls
191 lines (183 loc) · 6.61 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
{0 Synopsis}
Ostap is a parser-combinator library, armed by Camlp5 syntax extension to write parsers.
{1 Basic Usage}
This is an expression parser being run in OCaml toplevel.
{@ocaml[
# #use "topfind.camlp5";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()
Additional Camlp5 directives:
#camlp5o;; to load camlp5 (standard syntax)
#camlp5r;; to load camlp5 (revised syntax)
- : unit = ()
# #require "compiler-libs.common";;
# #require "camlp-streams";;
# #require "camlp5";;
# #load "camlp5o.cma";;
# #camlp5o;;
# type expr = Mul of expr * expr | Add of expr * expr | Var of string;;
type expr = Mul of expr * expr | Add of expr * expr | Var of string
# let rec expr_to_string = function
| Var s -> s
| Mul (x, y) -> "(" ^ expr_to_string x ^ " * " ^ expr_to_string y ^ ")"
| Add (x, y) -> "(" ^ expr_to_string x ^ " + " ^ expr_to_string y ^ ")";;
val expr_to_string : expr -> string = <fun>
# #require "ostap";;
# #require "ostap.syntax";;
# open Ostap;;
# ostap (
expr : addi;
addi : x:mulli "+" y:addi { Add (x, y) } | mulli;
mulli : x:primary "*" y:mulli { Mul (x, y) } | primary;
primary: x:IDENT {Var x}
| -"(" expr -")"
)
val expr :
(< equal : 'self -> bool;
getIDENT : 'b.
(string ->
'self ->
('self, 'b, < add : 'a -> 'a; .. > as 'a)
Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
look : 'b.
string ->
('alook -> 'self -> ('self, 'b, 'a) Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
.. >
as 'self) ->
(expr, 'self, 'c, 'a) Types.k -> ('c * 'self, 'a) Types.tag = <fun>
val addi :
(< equal : 'self -> bool;
getIDENT : 'b.
(string ->
'self ->
('self, 'b, < add : 'a -> 'a; .. > as 'a)
Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
look : 'b.
string ->
('alook -> 'self -> ('self, 'b, 'a) Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
.. >
as 'self) ->
(expr, 'self, 'c, 'a) Types.k -> ('c * 'self, 'a) Types.tag = <fun>
val mulli :
(< equal : 'self -> bool;
getIDENT : 'b.
(string ->
'self ->
('self, 'b, < add : 'a -> 'a; .. > as 'a)
Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
look : 'b.
string ->
('alook -> 'self -> ('self, 'b, 'a) Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
.. >
as 'self) ->
(expr, 'self, 'c, 'a) Types.k -> ('self, 'c, 'a) Ostap.Types.result = <fun>
val primary :
(< equal : 'self -> bool;
getIDENT : 'b.
(string ->
'self ->
('self, 'b, < add : 'a -> 'a; .. > as 'a)
Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
look : 'b.
string ->
('alook -> 'self -> ('self, 'b, 'a) Ostap.Types.result) ->
('self, 'b, 'a) Ostap.Types.result;
.. >
as 'self) ->
(expr, 'self, 'c, 'a) Types.k -> ('self, 'c, 'a) Ostap.Types.result = <fun>
# let parse p pr s =
match Util.parse
(object
(* Makes some default stream with minimal entries *)
inherit Matcher.t s
inherit Util.Lexers.decimal s
inherit Util.Lexers.ident [] s
inherit! Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n";
Matcher.Skip.lineComment "--";
Matcher.Skip.nestedComment "(*" "*)"
] s
end)
(ostap (p -EOF))
with
| `Ok p -> Printf.printf "Parsed : %s\n" @@ pr p
| `Fail er -> Printf.printf "Syntax error: %s\n" er;;
val parse :
((< chrs : char list; col : int; coord : Msg.MC.key; equal : 'a -> bool;
get : 'b.
string ->
Re.Str.regexp ->
(Matcher.Token.t ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
getDECIMAL : 'b.
(int ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
getEOF : 'b.
(Matcher.Token.t ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
getIDENT : 'b.
(string ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
line : int; loc : Msg.Locator.t;
look : 'b.
string ->
(Matcher.Token.t ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
pos : int; prefix : int -> string;
regexp : 'b.
string ->
string ->
(Matcher.Token.t ->
'a -> ('a, 'b, Ostap.Reason.t) Ostap.Types.result) ->
('a, 'b, Ostap.Reason.t) Ostap.Types.result;
skip : int ->
Msg.MC.key ->
[ `Failed of Msg.t | `Skipped of int * Msg.MC.key ];
str : string >
as 'a) ->
('d ->
(< equal : 'self -> bool;
getEOF : 'b.
('aEOF -> 'self -> ('self, 'b, 'c) Ostap.Types.result) ->
('self, 'b, 'c) Ostap.Types.result;
.. >
as 'self) ->
('d * 'self, 'c) Types.tag) ->
('e * 'f,
< retrieve : [> `First of int ] ->
[> `Desc ] ->
(Msg.Locator.t *
[< `Comment of string * 'g | `Msg of Msg.t ] list)
list;
.. >)
Types.tag) ->
('e -> string) -> string -> unit = <fun>
# let _ = List.iter (parse expr expr_to_string) ["a"; "a+b"; "a+b+c"; "a+b*c"; "a+b*c+d"; "(a+b)*c"];;
Parsed : a
Parsed : (a + b)
Parsed : (a + (b + c))
Parsed : (a + (b * c))
Parsed : (a + ((b * c) + d))
Parsed : ((a + b) * c)
- : unit = ()
]}