This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathres_driver_reason_binary.ml
101 lines (97 loc) · 3.4 KB
/
res_driver_reason_binary.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
module IO = Res_io
let isReasonDocComment (comment: Res_comment.t) =
let content = Res_comment.txt comment in
let len = String.length content in
if len = 0 then true
else if len >= 2 && (String.unsafe_get content 0 = '*' && String.unsafe_get content 1 = '*') then false
else if len >= 1 && (String.unsafe_get content 0 = '*') then true
else false
let extractConcreteSyntax filename =
let commentData = ref [] in
let stringData = ref [] in
let src =
if String.length filename > 0 then IO.readFile ~filename
else IO.readStdin ()
in
let scanner = Res_scanner.make (Bytes.of_string src) ~filename in
let rec next prevEndPos scanner =
let (startPos, endPos, token) = Res_scanner.scan scanner in
match token with
| Eof -> ()
| Comment c ->
Res_comment.setPrevTokEndPos c prevEndPos;
commentData := c::(!commentData);
next endPos scanner
| String _ ->
let loc = {Location.loc_start = startPos; loc_end = endPos; loc_ghost = false} in
let len = endPos.pos_cnum - startPos.pos_cnum in
let txt = (String.sub [@doesNotRaise]) src startPos.pos_cnum len in
stringData := (txt, loc)::(!stringData);
next endPos scanner
| _ ->
next endPos scanner
in
next Lexing.dummy_pos scanner;
let comments =
!commentData
|> List.filter (fun c -> not (isReasonDocComment c))
|> List.rev
in
(comments, !stringData)
let parsingEngine = {
Res_driver.parseImplementation = begin fun ~forPrinter:_ ~filename ->
let (chan, close) = if (String.length filename) == 0 then
(stdin, fun _ -> ())
else
let file_chan = open_in_bin filename in
let () = seek_in file_chan 0 in
file_chan, close_in_noerr
in
let magic = Config.ast_impl_magic_number in
ignore ((really_input_string [@doesNotRaise]) chan (String.length magic));
let filename = input_value chan in
let (comments, stringData) = if filename <> "" then extractConcreteSyntax filename else ([], []) in
let ast = input_value chan in
close chan;
let structure = ast
|> Res_ast_conversion.replaceStringLiteralStructure stringData
|> Res_ast_conversion.normalizeReasonArityStructure ~forPrinter:true
|> Res_ast_conversion.structure
in {
Res_driver.filename = filename;
source = "";
parsetree = structure;
diagnostics = ();
invalid = false;
comments = comments;
}
end;
parseInterface = begin fun ~forPrinter:_ ~filename ->
let (chan, close) = if String.length filename == 0 then
(stdin, fun _ -> ())
else
let file_chan = open_in_bin filename in
let () = seek_in file_chan 0 in
file_chan, close_in_noerr
in
let magic = Config.ast_intf_magic_number in
ignore ((really_input_string [@doesNotRaise]) chan (String.length magic));
let filename = input_value chan in
let (comments, stringData) = if filename <> "" then extractConcreteSyntax filename else ([], []) in
let ast = input_value chan in
close chan;
let signature = ast
|> Res_ast_conversion.replaceStringLiteralSignature stringData
|> Res_ast_conversion.normalizeReasonAritySignature ~forPrinter:true
|> Res_ast_conversion.signature
in {
Res_driver.filename;
source = "";
parsetree = signature;
diagnostics = ();
invalid = false;
comments = comments;
}
end;
stringOfDiagnostics = begin fun ~source:_ ~filename:_ _diagnostics -> () end;
}