-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathast.ml
205 lines (198 loc) · 4.56 KB
/
ast.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
(* AST *)
open Conduit;;
type info = {
i_start : int;
i_end : int;
i_extra : bool
};;
let version = 8;;
type program = source_element list
and source_element =
| St of int * int * st
| FunDecl of int * int * func
and block = st list
and st =
| Position of int * int * st
| Expr of expr
| If of expr * st * st option
| Do of st * expr
| While of expr * st
| For of st option * st option * st option * st
| Continue of label option
| Break of label option
| Return of expr option
| With of expr * st
| Labeled of label * st
| Switch of expr * (case_clause list * st) list
| Throw of expr
| Try of st * (arg * st) option * st option
| Variable of variable_declaration list
| Block of st list
| ForIn of lhs_or_var * expr * st
| Nop
and lhs_or_var =
| LHS of expr
| Vars of variable_declaration list
and variable_declaration = name * expr option
and case_clause = Default | Case of expr
and func = name option * name list * source_element list
and extra = int * int * extra_tag
and extra_tag =
| DanglingComma
and expr =
| Assign of expr * assignment_operator * expr
| Sq of expr list
| Function of int * int * func
| L of litteral
| U of unop * expr
| B of binop * expr * expr
| V of name
| Object of (property_name * expr) list
| Array of array_litteral
| Apply of expr * expr list
| Conditional of expr * expr * expr
| This
| Extra of extra
and property_name =
| PN_String of string
| PN_Float of float
| PN_Int of int32
| PN_Empty
and binop =
| B_mul
| B_div
| B_mod
| B_add
| B_sub
| B_le
| B_ge
| B_lt
| B_gt
| B_instanceof
| B_in
| B_equal
| B_lsr
| B_asr
| B_lsl
| B_notequal
| B_physequal
| B_physnotequal
| B_bitand
| B_bitor
| B_bitxor
| B_and
| B_or
| B_bracket
and unop =
| U_bitnot
| U_delete
| U_void
| U_typeof
| U_pre_increment
| U_pre_decrement
| U_post_increment
| U_post_decrement
| U_plus
| U_minus
| U_not
| U_new
and assignment_operator =
| A_eq
| A_mul
| A_div
| A_mod
| A_add
| A_sub
| A_lsl
| A_lsr
| A_asr
| A_and
| A_xor
| A_or
and litteral =
| Float of float
| Int of int32
| String of string
| Regexp of string * string
| Bool of bool
| Null
| Undefined
and array_litteral = expr list
and name = string
and arg = string
and label = string
let info0 = { i_start = 0; i_end = max_int; i_extra = true };;
let rec iter_over_expr_in_program info f p = List.iter (iter_over_expr_in_source_element info f) p
and iter_over_expr_in_source_element info f = function
| St(start_pos, end_pos, s) -> iter_over_expr_in_st { info with i_start = start_pos; i_end = end_pos } f s
| FunDecl(_,_,(_,_,sl)) -> iter_over_expr_in_program info f sl
and iter_over_expr_in_sto info f = function
| None -> ()
| Some s -> iter_over_expr_in_st info f s
and iter_over_expr_in_variable_declaration_list info f vl =
List.iter (fun (_, xo) ->
match xo with
| None -> ()
| Some x -> f info x) vl
and iter_over_expr_in_st info f = function
| Position(start_pos, end_pos, s) -> iter_over_expr_in_st { info with i_start = start_pos; i_end = end_pos } f s
| Expr x -> f info x
| If(x, s1, so) -> f info x; iter_over_expr_in_st info f s1;
begin
match so with
| None -> ()
| Some s -> iter_over_expr_in_st info f s
end
| Do(s, x)|While(x,s)|With(x,s) -> iter_over_expr_in_st info f s; f info x
| For(so1, so2, so3, s) ->
iter_over_expr_in_sto info f so1;
iter_over_expr_in_sto info f so2;
iter_over_expr_in_sto info f so3;
iter_over_expr_in_st info f s
| Return(Some x)|Throw x -> f info x
| Continue _|Break _|Return None -> ()
| Labeled(_, s) -> iter_over_expr_in_st info f s
| Switch(x, cls) ->
f info x;
List.iter
begin fun (cl, s) ->
iter_over_expr_in_st info f s;
List.iter
begin function
| Default -> ()
| Case x -> f info x
end
cl
end
cls
| Try(s, aso, so) ->
iter_over_expr_in_st info f s;
begin
match aso with
| None -> ()
| Some(_, s) -> iter_over_expr_in_st info f s
end;
begin
match so with
| None -> ()
| Some s -> iter_over_expr_in_st info f s
end
| Variable vl -> iter_over_expr_in_variable_declaration_list info f vl
| Block sl -> List.iter (iter_over_expr_in_st info f) sl
| ForIn(l, x, s) ->
f info x;
iter_over_expr_in_lhs info f l;
iter_over_expr_in_st info f s
| Nop -> ()
and iter_over_expr_in_lhs info f = function
| LHS x -> f info x
| Vars vl -> iter_over_expr_in_variable_declaration_list info f vl
;;
(*** scribe_property_name *)
let scribe_property_name cd oc = function
| PN_String u -> cd.cd_print oc "%S" u
| PN_Float f -> cd.cd_print oc "%f" f
| PN_Int x -> cd.cd_print oc "%ld" x
| PN_Empty -> cd.cd_print oc "*empty*"
;;
(* ***)