-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
parser_header.re
103 lines (90 loc) 路 2.66 KB
/
parser_header.re
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
open Location;
open Identifier;
open Parsetree;
open Ast_helper;
open Grain_utils;
let make_line_comment = (source, loc) => {
let content = String_utils.slice(~first=2, source) |> String.trim;
Line({cmt_content: content, cmt_source: source, cmt_loc: loc});
};
let make_shebang_comment = (source, loc) => {
let content = String_utils.slice(~first=2, source) |> String.trim;
Shebang({cmt_content: content, cmt_source: source, cmt_loc: loc});
};
let make_block_comment = (source, loc) => {
let content =
String_utils.slice(~first=2, ~last=-2, source)
|> String_utils.deasterisk_each_line
|> String_utils.trim_each_line;
Block({cmt_content: content, cmt_source: source, cmt_loc: loc});
};
let make_doc_comment = (source, loc) => {
let content =
String_utils.slice(~first=3, ~last=-2, source)
|> String_utils.deasterisk_each_line
|> String_utils.trim_each_line(~style=String_utils.KeepIndent);
Doc({cmt_content: content, cmt_source: source, cmt_loc: loc});
};
let to_loc = ((loc_start, loc_end)) => {
{loc_start, loc_end, loc_ghost: false};
};
let mkid = ns => {
let help = ns => {
let rec help = (ns, (acc_ident, acc_str)) => {
let ident =
Option.fold(
~some=i => IdentExternal(i, acc_str),
~none=IdentName(acc_str),
acc_ident,
);
switch (ns) {
| [] => ident
| [n, ...tl] => help(tl, (Some(ident), n))
};
};
switch (ns) {
| [] => failwith("Should be impossible")
| [n, ...tl] => help(tl, (None, n))
};
};
mkloc @@ help(ns);
};
let mkid_expr = (loc, ns) => {
let loc = to_loc(loc);
Expression.ident(~loc, ~core_loc=loc, mkid(ns, loc));
};
let mkstr = (loc, s) => mkloc(s, to_loc(loc));
let make_include_ident = ident => {
switch (ident.txt) {
| IdentName(name) => name
| IdentExternal(_) =>
raise(
SyntaxError(
ident.loc,
"A module include name cannot contain `.` as that would reference a binding within another module.",
),
)
};
};
let make_include_alias = ident => {
switch (ident.txt) {
| IdentName(name) => name
| IdentExternal(_) =>
raise(
SyntaxError(
ident.loc,
"A module alias cannot contain `.` as that would reference a binding within another module.",
),
)
};
};
let make_program = (~loc, module_name, statements) => {
// Ensure the program loc starts at the beginning of the file even if
// there's whitespace or comments
let loc_start = {...loc.loc_start, pos_lnum: 1, pos_cnum: 0, pos_bol: 0};
let prog_loc = {...loc, loc_start};
{module_name, statements, comments: [], prog_loc};
};
let parse_program = (program, token, lexbuf) => {
program(token);
};