/
bslJsParse.ml
393 lines (349 loc) · 11.8 KB
/
bslJsParse.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
391
392
393
(*
Copyright © 2012 MLstate
This file is part of Opa.
Opa is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
Opa 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 GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with Opa. If not, see <http://www.gnu.org/licenses/>.
*)
(**
@author Arthur Azevedo de Amorim
*)
module Format = BaseFormat
module List = BaseList
module String = BaseString
module BD = BslDirectives.Js
module BT = BslTypes
module BRS = BslRegisterParserState
module J = JsAst
type tag = string
type message = string
type pos = FilePos.pos
type parsed_file = {
directives: (FilePos.pos * BslTags.t * BslDirectives.Js.t) list;
code: JsAst.code;
}
let whitespace = Str.regexp "[ \t]*"
(** When trying to interpret a comment as a bsl directive, we do the
following:
- One group of tags, such as "@opaName" or "@raise", is used to
build a BslTags.t value, that should be associated with a
directive later.
- Other tags, such as "@externType" and "@register", denote the
actual bsl directives. They are mutually exclusive: there can be
no comment with two or more of them.
- Tags that are recognized but badly formatted (e.g. "@register I
have no type") trigger an error. Other tags (such as "@param") are
silently ignored.
- Comments that have no directives are ignored as well.
*)
(* Example:
/**
* @register {int -> int} my_bypass
* @cpsBypass
*
*/
function my_bypass(val) {
return 4;
}
*)
(** Builds a set of bsl tags based on comment annotations *)
let collect_bsl_tags tags =
(* Reads tags with an associated set of strings *)
let string_set tag update bsl_tags =
let aux (_, tag', args) =
if tag = tag' then
let attributes = Str.split whitespace args in
Some (StringSet.from_list attributes)
else
None
in
match List.find_map aux tags with
| Some attributes -> Some (update bsl_tags attributes)
| None -> Some bsl_tags
in
(* Sets a flag to true if it finds the corresponding tags *)
let bool tag update bsl_tags =
let rec aux tags =
match tags with
| [] -> Some bsl_tags
| (_, tag', args) :: rest ->
if tag <> tag' then
aux rest
else if Str.string_match whitespace args 0 then
Some (update bsl_tags true)
else
None
in aux tags
in
(* List of tags, their formats and how they update a BslTags.t *)
let updates = [
string_set "noProjection" (fun t v ->
{t with BslTags.no_projection = Some v}
);
bool "opaName" (fun t v -> {t with BslTags.opaname = v});
bool "raise" (fun t v -> {t with BslTags.raise_ = v});
bool "cpsBypass" (fun t v -> {t with BslTags.cps_bypass = v});
bool "opacapi" (fun t v -> {t with BslTags.opacapi = v});
] in
let rec try_updates bsl_tags updates =
match updates with
| [] -> Some bsl_tags
| update :: rest -> (
match update bsl_tags with
| Some bsl_tags' -> try_updates bsl_tags' rest
| None -> None
)
in
try_updates BslTags.default updates
(** The next set of tags corresponds to directive tags. We try to
extract a directive from each comment line. In the end, we check
if a unique directive was defined or not. *)
type global_read_result = [ `no_occurrences
| `wrong_format of pos * message
| `multiple_occurrences of tag
| `found of pos * tag * BD.t ]
type local_read_result = [ `wrong_format of pos * message
| `found of BD.t ]
(** Extracts all occurrences of tag [keyword] *)
let try_read_args tag
(arg_reader : pos -> string -> local_read_result)
tags : global_read_result =
let rec aux acc tags =
match tags with
| [] ->
Option.default_map `no_occurrences
(fun (pos, dir) -> `found (pos, tag, dir))
acc
| (pos, tag', args) :: rest ->
if tag <> tag' then
aux acc rest
else if Option.is_some acc then
`multiple_occurrences tag
else
match arg_reader pos args with
| `wrong_format _ as s -> s
| `found directive -> aux (Some (pos, directive)) rest
in
aux None tags
let identifier_regexp =
Str.regexp "^[ \t]*\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*$"
let get_identifier string =
if Str.string_match identifier_regexp string 0 then
Some (Str.matched_group 1 string)
else
None
(** Reads something of the form "@tag I.am.a_type('var1, 'var2)" *)
let extract_type_declaration =
let type_regexp =
Str.regexp (
"^[ \t]*" ^ (* whitespace *)
"\\([^( \t]*\\)" ^ (* type name *)
"\\((\\([^)]*\\))\\)?" ^ (* optional type vars *)
"[ \t]*$" (* ending white space *)
)
in
let split_vars_regexp = Str.regexp "[ \t]*,[ \t]*" in
let var_regexp = Str.regexp "'[a-z]*" in
fun tag constructor ->
try_read_args tag (fun pos args ->
if Str.string_match type_regexp args 0 then
let name = Str.matched_group 1 args in
try
let vars = Str.matched_group 3 args in
let vars = Str.split split_vars_regexp vars in
if List.for_all (fun s -> Str.string_match var_regexp s 0) vars then
let vars = List.map (fun var ->
let var = Str.string_after var 1 in
BRS.TypeVar.var var
) vars in
`found (constructor name vars)
else
`wrong_format (pos,
Printf.sprintf
"Couldn't read type in @%s directive"
tag
)
with
Not_found ->
`found (constructor name [])
else
`wrong_format (pos,
Printf.sprintf
"Directive @%s requires a type"
tag
)
)
(** The tag readers, one for each recognized tag *)
let extract_extern_type_def =
extract_type_declaration "externType" (fun ty args ->
BD.ExternalTypeDef (ty, args)
)
let extract_opa_type_def =
extract_type_declaration "opaType" (fun ty args ->
BD.OpaTypeDef (ty, args)
)
let extract_module =
try_read_args "module" (fun pos args ->
match get_identifier args with
| Some i -> `found (BD.Module i)
| None -> `wrong_format (pos, "Expected identifier in module declaration")
)
let extract_end_module =
try_read_args "endModule" (fun pos args ->
if args = "" then
`found BD.EndModule
else
`wrong_format (pos, "@endModule takes no arguments")
)
let extract_register implementation =
let re = Str.regexp (
"^{\\([^}]*\\)}" ^ (* Type between brackets *)
"\\([ \t]+[a-zA-Z][a-zA-Z0-9_]*\\)?[ \t]*" ^ (* Optional bypass name *)
"\\([^ \t]+\\)?[ \t]*$" (* Optional source code *)
) in
try_read_args "register" (fun pos args ->
if Str.string_match re args 0 then
let ty = Str.matched_group 1 args in
let (_, ty) = BslRegisterParser.parse_bslregisterparser_bslty ty in
let name =
try
`success (JsCons.Ident.native
(String.trim (Str.matched_group 2 args)))
with
Not_found ->
match implementation with
| `func (ident, args1) -> (
(* Check if arities match *)
match ty with
| BT.Fun (_, args2, _) ->
let l1 = List.length args1 in
let l2 = List.length args2 in
if l1 <> l2 then
`error (Printf.sprintf (
"Function definition takes %d arguments, "^^
"but its registred type expects %d"
) l1 l2)
else
`success ident
| _ -> `success ident
)
| `var ident -> `success ident
| `none -> `error "Missing bypass name in @register declaration"
in
let definition =
try
`success (BD.Inline (Str.matched_group 3 args))
with
Not_found ->
match implementation with
| `func (ident, _)
| `var ident -> `success (BD.Regular ident)
| `none -> `error "Missing definition in @register declaration"
in
match name, definition with
| `success name, `success definition ->
`found (BD.Register (JsIdent.to_string name, definition, ty))
| `success _, `error message
| `error message, `success _ -> `wrong_format (pos, message)
| `error m1, `error m2 -> `wrong_format (pos, Printf.sprintf "%s, %s" m1 m2)
else
`wrong_format
(pos, "Format of @register is \"@register {type} key [optional source]\"")
)
let readers implementation = [
extract_extern_type_def;
extract_opa_type_def;
extract_module;
extract_end_module;
extract_register implementation;
]
type extract_result =
| NoOccurrences
| Error of string
| Found of pos * BslTags.t * BD.t
(** Try to extract a bsl directive from a list of tags *)
let maybe_extract_directive implementation tags : extract_result =
let extracted_directives = List.map (fun extract ->
extract tags
) (readers implementation) in
let rec aux acc extracted_directives =
match extracted_directives with
| [] -> (
match acc with
| None -> NoOccurrences
| Some (pos, _, d) -> (
match collect_bsl_tags tags with
| Some bsl_tags -> Found (pos, bsl_tags, d)
| None -> Error "Badly formatted BSL tags"
)
)
| extracted :: rest -> (
match extracted with
| `no_occurrences -> aux acc rest
| `found (pos, name, directive) -> (
match acc with
| None -> aux (Some (pos, name, directive)) rest
| Some (_pos', name', _directive') ->
Error (
Printf.sprintf
"Multiple directives have been found: @%s and @%s"
name name'
)
)
| `wrong_format (pos, message) ->
Error (Format.sprintf "%a%s" FilePos.pp_citation pos message)
| `multiple_occurrences name ->
Error (
Printf.sprintf "Multiple occurrences of tag @%s" name
)
)
in
aux None extracted_directives
let filter_lines lines = List.filter_map (fun line ->
match line with
| JsLex.CommentLine _ -> None
| JsLex.CommentTag (pos, tag, args) -> Some (pos, tag, args)
) lines
let rec analyze_comments directives code =
match code with
| J.Js_comment (_, J.Jc_doc (_, lines)) :: rest -> (
let tags = filter_lines lines in
let implementation, rest = match rest with
| J.Js_function (_, ident, args, _) :: rest
| J.Js_var (_, ident, Some (J.Je_function (_, _, args, _))) :: rest ->
`func (ident, args), rest
| J.Js_var (_, ident, _) :: rest ->
`var ident, rest
| _ -> `none, rest in
match maybe_extract_directive implementation tags with
| NoOccurrences -> analyze_comments directives rest
| Error e -> `error e
| Found (pos, bsl_tags, d) ->
analyze_comments ((pos, bsl_tags, d) :: directives) rest
)
| _ :: rest -> analyze_comments directives rest
| [] -> `success (List.rev directives)
let process code =
match analyze_comments [] code with
| `error e -> `error e
| `success directives -> `success {directives; code}
let parse_file filename =
try
let code = JsParse.File.code ~throw_exn:true filename in
process code
with
JsParse.Exception e ->
`error (Format.to_string JsParse.pp e)
let parse_string ?filename content =
try
let code = JsParse.String.code ?filename ~throw_exn:true content in
process code
with
JsParse.Exception e ->
`error (Format.to_string JsParse.pp e)