package pfff

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ast_ml.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
(* Yoann Padioleau
 *
 * Copyright (C) 2019 r2c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation, with the
 * special exception on linking described in file license.txt.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
 * license.txt for more details.
 *)

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* An Abstract Syntax Tree for OCaml.
 *
 * See cst_ml.ml for a Concrete Syntax Tree for OCaml (better for program
 * transformation purpose).
 *)

(*****************************************************************************)
(* The AST related types *)
(*****************************************************************************)

(* ------------------------------------------------------------------------- *)
(* Token/info *)
(* ------------------------------------------------------------------------- *)
type tok = Parse_info.t
 (* with tarzan *)

(* a shortcut to annotate some information with token/position information *)
type 'a wrap = 'a * tok
 (* with tarzan *)

(* ------------------------------------------------------------------------- *)
(* Names  *)
(* ------------------------------------------------------------------------- *)

type ident = string wrap
 (* with tarzan *)

type name = qualifier * ident
 and qualifier = ident list (* TODO: functor? *)
 (* with tarzan *)

(* ------------------------------------------------------------------------- *)
(* Types *)
(* ------------------------------------------------------------------------- *)

type type_ = 
  | TyName of name (* include builtins *)
  | TyVar of ident

  | TyFunction of type_ * type_
  | TyApp of type_ list * name (* less: could be merged with TyName *)

  | TyTuple of type_ list (* at least 2 *)

 (* with tarzan *)

(* ------------------------------------------------------------------------- *)
(* Expressions *)
(* ------------------------------------------------------------------------- *)

(* start of big recursive type *)
type expr =
  | L of literal
  | Name of name

  | Constructor of name * expr option
  (* special case of Constr *)
  | Tuple of expr list
  | List  of expr list

  (* can be empty *) 
  | Sequence of expr list

  | Prefix of string wrap * expr
  | Infix of expr * string wrap * expr

  | Call of expr * argument list

  (* could be factorized with Prefix but it's not a usual prefix operator! *)
  | RefAccess of tok (* ! *) * expr
  | RefAssign of expr * tok (* := *) * expr

  (* special case of RefAccess and RefAssign *)
  | FieldAccess of expr * name
  | FieldAssign of expr * name * (* <- *) expr

  | Record of expr option (* with *) * (name * expr) list

  | New of tok * name
  | ObjAccess of expr * ident
  

  (* > 1 elt for mutually recursive let (let x and y and z) *)
  | LetIn of let_binding list * expr * rec_opt
  | Fun of parameter list (* at least one *) * expr

  (* statement-like expressions *)
  | Nop (* for empty else *)

  | If of expr * expr * expr
  | Match of expr * match_case list

  | Try of expr * match_case list 

  | While of expr * expr
  | For of ident * expr * for_direction * expr *   expr

 and literal =
   | Int    of string wrap
   | Float  of string wrap
   | Char   of string wrap
   | String of string wrap

 and argument = 
   | Arg of expr
   | ArgKwd of ident * expr
   | ArgQuestion of ident  * expr

 and match_case =
  pattern * match_action

  and match_action = expr * expr option (* when *)

 and for_direction =
  | To of tok
  | Downto of tok

 and rec_opt = tok option

(* ------------------------------------------------------------------------- *)
(* Patterns *)
(* ------------------------------------------------------------------------- *)
and pattern = 
  | PatVar of ident
  | PatLiteral of literal (* can be signed *)
  | PatConstructor of name * pattern option
 
  (* special cases of PatConstructor *)
  | PatConsInfix of pattern * tok (* :: *) * pattern
  | PatTuple of pattern list
  | PatList of pattern list

  | PatUnderscore of tok
  | PatRecord of (name * pattern) list

  | PatAs of pattern * ident
  (* ocaml disjunction patterns extension *)
  | PatDisj of pattern * pattern
  | PatTyped of pattern * type_

(* ------------------------------------------------------------------------- *)
(* Let binding (global/local/function definition) *)
(* ------------------------------------------------------------------------- *)

and let_binding =
  | LetClassic of let_def
  | LetPattern of pattern * expr

 (* was called fun_binding in the grammar *)
 and let_def = {
   lname: ident;
   lparams: parameter list; (* can be empty *)
   lbody: expr;
 }

 and parameter = pattern

 (* with tarzan *)

(* ------------------------------------------------------------------------- *)
(* Type declaration *)
(* ------------------------------------------------------------------------- *)

type type_declaration = {
  tname: ident;
  tparams: type_parameter list;
  tbody: type_def_kind;
}

 and type_parameter = ident (* a TyVar, e.g., 'a *)

 and type_def_kind =
   | AbstractType
   | CoreType of type_
   (* or type *)
   | AlgebricType of (ident * type_ list) list
   (* and type *)
   | RecordType   of (ident * type_ * tok option (* mutable *)) list

 (* with tarzan *)

(* ------------------------------------------------------------------------- *)
(* Class *)
(* ------------------------------------------------------------------------- *)

(* ------------------------------------------------------------------------- *)
(* Module *)
(* ------------------------------------------------------------------------- *)
type module_declaration = {
  mname: ident;
  mbody: module_expr;
}

  (* mutually recursive with item *)
  and module_expr =
  | ModuleName of name (* alias *)
  | ModuleStruct of item list

(* ------------------------------------------------------------------------- *)
(* Signature/Structure items *)
(* ------------------------------------------------------------------------- *)

(* could split in sig_item and struct_item but many constructions are
 * valid in both contexts.
 *)
and item = 
  | Type of type_declaration list (* mutually recursive *)

  | Exception of ident * type_ list
  | External  of ident * type_ * string wrap list (* primitive declarations *)
      
  | Open of name
      
  (* only in sig_item *)
  | Val of ident * type_
      
  (* only in struct_item *)
  | Let of rec_opt * let_binding list

  | Module of module_declaration

 (* with tarzan *)
      
type program = item list

 (* with tarzan *)

(*****************************************************************************)
(* Any *)
(*****************************************************************************)

type any =
  | T of type_
  | E of expr
  | P of pattern

  | I of item
  | Pr of program

  (* with tarzan *)

(*****************************************************************************)
(* Wrappers *)
(*****************************************************************************)

let str_of_ident (s,_) = s
let info_of_ident (_,info) = info

let ident_of_name (_, ident) = ident
let qualifier_of_name (qu, _) = 
  qu |> List.map str_of_ident |> Common.join "."
OCaml

Innovation. Community. Security.