Source file parseTree.ml

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
(* -------------------------------------------------------------------- *)
open Ident
open Location

(* -------------------------------------------------------------------- *)
type lident = ident loced
[@@deriving yojson]

let pp_lident fmt i = Format.fprintf fmt "%s" (unloc i)

(* -------------------------------------------------------------------- *)
type container =
  | Collection
  | Partition
[@@deriving yojson, show {with_path = false}]

type type_r =
  | Tref of lident
  | Tasset of lident
  | Tcontainer of type_t * container
  | Ttuple of type_t list
  | Toption of type_t
[@@deriving yojson, show {with_path = false}]

and type_t = type_r loced
[@@deriving yojson, show {with_path = false}]

(* -------------------------------------------------------------------- *)
type logical_operator =
  | And
  | Or
  | Imply
  | Equiv
[@@deriving yojson, show {with_path = false}]

type comparison_operator =
  | Equal
  | Nequal
  | Gt
  | Ge
  | Lt
  | Le
[@@deriving yojson, show {with_path = false}]

type arithmetic_operator =
  | Plus
  | Minus
  | Mult
  | Div
  | Modulo
[@@deriving yojson, show {with_path = false}]

type unary_operator =
  | Uplus
  | Uminus
  | Not
[@@deriving yojson, show {with_path = false}]

type assignment_operator =
  | ValueAssign
  | PlusAssign
  | MinusAssign
  | MultAssign
  | DivAssign
  | AndAssign
  | OrAssign
[@@deriving yojson, show {with_path = false}]

type quantifier =
  | Forall
  | Exists
[@@deriving yojson, show {with_path = false}]

type operator = [
  | `Logical of logical_operator
  | `Cmp     of comparison_operator
  | `Arith   of arithmetic_operator
  | `Unary   of unary_operator
]
[@@deriving yojson, show {with_path = false}]

type qualid =
  | Qident of lident
  | Qdot of qualid * lident
[@@deriving yojson, show {with_path = false}]

type pattern_unloc =
  | Pwild
  | Pref of lident
[@@deriving yojson, show {with_path = false}]

type pattern = pattern_unloc loced
[@@deriving yojson, show {with_path = false}]

type expr_unloc =
  | Eterm         of (lident option * lident option * lident)
  | Eliteral      of literal
  | Earray        of expr list
  | Erecord       of record_item list
  | Etuple        of expr list
  | Edot          of expr * lident
  | Emulticomp    of expr * (comparison_operator loced * expr) list
  | Eapp          of function_ * expr list
  | Emethod       of expr * lident * expr list
  | Etransfer     of expr * bool * lident option
  | Erequire      of expr
  | Efailif       of expr
  | Eassign       of assignment_operator * expr * expr
  | Eif           of expr * expr * expr option
  | Ebreak
  | Efor          of lident option * lident * expr * expr
  | Eiter         of lident option * lident * expr option * expr * expr
  | Eseq          of expr * expr
  | Eletin        of lident * type_t option * expr * expr * expr option
  | Ematchwith    of expr * (pattern list * expr) list
  | Equantifier   of quantifier * lident * quantifier_kind * expr
  | Eassert       of lident
  | Ereturn       of expr
  | Eoption       of option_
  | Einvalid
[@@deriving yojson, show {with_path = false}]

and scope = [
  | `Added
  | `After
  | `Before
  | `Fixed
  | `Removed
  | `Stable
]
[@@derive yojson, show {with_path = false}]

and quantifier_kind =
  | Qcollection of expr
  | Qtype of type_t
[@@deriving yojson, show {with_path = false}]

and option_ =
  | OSome of expr
  | ONone
[@@deriving yojson, show {with_path = false}]

and function_ =
  | Fident of lident
  | Foperator of operator loced
[@@deriving yojson, show {with_path = false}]

and literal =
  | Lnumber   of Core.big_int
  | Lrational of Core.big_int * Core.big_int
  | Ltz       of Core.big_int
  | Lmtz      of Core.big_int
  | Laddress  of string
  | Lstring   of string
  | Lbool     of bool
  | Lduration of string
  | Ldate     of string
[@@deriving yojson, show {with_path = false}]

and record_item = (assignment_operator * lident) option * expr

and expr = expr_unloc loced
[@@deriving yojson, show {with_path = false}]

and lident_typ = lident * type_t * extension list option
[@@deriving yojson, show {with_path = false}]

and label_expr = (lident * expr) loced

and label_exprs = label_expr list

(* -------------------------------------------------------------------- *)
and extension_unloc =
  | Eextension of lident * expr option (** extension *)
[@@deriving yojson, show {with_path = false}]

and extension = extension_unloc loced
[@@deriving yojson, show {with_path = false}]

and exts = extension list option
[@@deriving yojson, show {with_path = false}]

(* -------------------------------------------------------------------- *)
type field_unloc =
  | Ffield of lident * type_t * expr option * exts   (** field *)
[@@deriving yojson, show {with_path = false}]

and field = field_unloc loced
[@@deriving yojson, show {with_path = false}]

type args = lident_typ list
[@@deriving yojson, show {with_path = false}]

type invariants = (lident * expr list) list
[@@deriving yojson, show {with_path = false}]

type specification_item_unloc =
  | Vpredicate of lident * args * expr
  | Vdefinition of lident * type_t * lident * expr
  | Vlemma of lident * expr
  | Vtheorem of lident * expr
  | Vvariable of lident * type_t * expr option
  | Veffect of expr
  | Vassert of (lident * expr * invariants)
  | Vpostcondition of (lident * expr * invariants)
[@@deriving yojson, show {with_path = false}]

type specification_item = specification_item_unloc loced
[@@deriving yojson, show {with_path = false}]

type specification_unloc = specification_item list * exts
[@@deriving yojson, show {with_path = false}]

type specification = specification_unloc loced
[@@deriving yojson, show {with_path = false}]

type security_arg_unloc =
  | Sident of lident
  | Sdot   of lident * lident
  | Slist of security_arg list
  | Sapp of lident * security_arg list
  | Sbut of lident * security_arg
  | Sto of lident * security_arg
[@@deriving yojson, show {with_path = false}]

and security_arg = security_arg_unloc loced
[@@deriving yojson, show {with_path = false}]

type security_item_unloc = lident * lident * security_arg list
[@@deriving yojson, show {with_path = false}]

type security_item = security_item_unloc loced
[@@deriving yojson, show {with_path = false}]

type security_unloc = security_item list * exts
[@@deriving yojson, show {with_path = false}]

and security = security_unloc loced
[@@deriving yojson, show {with_path = false}]

type s_function = {
  name  : lident;
  args  : args;
  ret_t : type_t option;
  spec : specification option;
  body  : expr;
}
[@@deriving yojson, show {with_path = false}]

type action_properties = {
  accept_transfer : bool;
  calledby        : (expr * exts) option;
  require         : (label_exprs * exts) option;
  failif          : (label_exprs * exts) option;
  spec            : specification option;
  functions       : (s_function loced) list;
}
[@@deriving yojson, show {with_path = false}]

type transition = (lident * (expr * exts) option * (expr * exts) option) list
[@@deriving yojson, show {with_path = false}]

(* -------------------------------------------------------------------- *)
type variable_kind =
  | VKvariable
  | VKconstant
[@@deriving yojson, show {with_path = false}]

type enum_kind =
  | EKenum of lident
  | EKstate
[@@deriving yojson, show {with_path = false}]

(* -------------------------------------------------------------------- *)
type declaration_unloc =
  | Darchetype     of lident * exts
  | Dvariable      of variable_decl
  | Dinstance      of instance_decl
  | Denum          of enum_kind * enum_decl
  | Dasset         of asset_decl
  | Daction        of action_decl
  | Dtransition    of transition_decl
  | Dcontract      of contract_decl
  | Dextension     of extension_decl
  | Dnamespace     of namespace_decl
  | Dfunction      of s_function
  | Dspecification of specification
  | Dsecurity      of security
  | Dinvalid
[@@deriving yojson, show {with_path = false}]

and variable_decl =
  lident
  * type_t
  * expr option
  * value_option list option
  * variable_kind
  * exts

and instance_decl = (* instance[%exts%] var of contract_type = e *)
  lident   (* var *)
  * lident (* contract_type *)
  * expr   (* e *)
  * exts   (* exts *)

and enum_decl =
  (lident * enum_option list) list * exts

and asset_decl =
  lident
  * field list
  * asset_option list
  * asset_post_option list
  * asset_operation option
  * exts

and action_decl =
  lident
  * args
  * action_properties
  * (expr * exts) option
  * exts

and transition_decl =
  lident
  * args
  * (lident * lident) option
  * expr
  * action_properties
  * transition
  * exts

and contract_decl =
  lident * signature list * exts

and extension_decl =
  lident * expr option

and namespace_decl =
  lident * declaration list

and value_option =
  | VOfrom of lident
  | VOto of lident
[@@deriving yojson, show {with_path = false}]

and asset_option =
  | AOidentifiedby of lident
  | AOsortedby of lident
[@@deriving yojson, show {with_path = false}]

and asset_post_option =
  | APOstates of lident
  | APOconstraints of label_exprs
  | APOinit of expr
[@@deriving yojson, show {with_path = false}]

and enum_option =
  | EOinitial
  | EOspecification of label_exprs
[@@deriving yojson, show {with_path = false}]

and signature =
  | Ssignature of lident * type_t list
[@@deriving yojson, show {with_path = false}]

and declaration = declaration_unloc loced
[@@deriving yojson, show {with_path = false}]

and asset_operation_enum =
  | AOadd
  | AOremove
  | AOupdate
[@@deriving yojson, show {with_path = false}]

and asset_operation =
  | AssetOperation of asset_operation_enum list * expr option
[@@deriving yojson, show {with_path = false}]

(* -------------------------------------------------------------------- *)
type archetype_unloc =
  | Marchetype of declaration list
  | Mextension of lident * declaration list * declaration list
[@@deriving yojson, show {with_path = false}]

and archetype = archetype_unloc loced
[@@deriving yojson, show {with_path = false}]

let mk_archetype ?(decls=[]) ?(loc=dummy) () =
  mkloc loc (Marchetype decls)