-
Notifications
You must be signed in to change notification settings - Fork 37
/
module.js
150 lines (124 loc) · 4 KB
/
module.js
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
const {
sep1,
sep,
parens,
semi,
semi_opt,
semis,
} = require('./util.js')
module.exports = {
// ------------------------------------------------------------------------
// module names
// ------------------------------------------------------------------------
_modid: $ => alias($.name, $.module_id),
_modid_prefix: $ => prec('qualifying-module', seq($._modid, $._any_tight_dot)),
_qualifying_module: $ => repeat1($._modid_prefix),
module: $ => seq(repeat($._modid_prefix), $._modid),
// ------------------------------------------------------------------------
// import/export
// ------------------------------------------------------------------------
namespace: _ => choice('pattern', 'type'),
_child_type: $ => seq(field('namespace', 'type'), field('type', $._tyconids)),
_child: $ => choice(
alias($._child_type, $.associated_type),
$._qname,
),
children: $ => parens($, sep(',', field('element', choice(alias('..', $.all_names), $._child)))),
_ie_entity: $ => seq(
optional(field('namespace', $.namespace)),
choice(
field('variable', $._varids),
field('type', $._tyconids),
field('operator', choice($._sym_prefix, $._pqsym)),
),
optional(field('children', $.children)),
),
// ------------------------------------------------------------------------
// import
// ------------------------------------------------------------------------
import_list: $ => parens(
$,
sep(',', field('name', alias($._ie_entity, $.import_name))),
optional(','),
),
import: $ => seq(
'import',
optional('qualified'),
optional(field('package', alias($.string, $.import_package))),
field('module', $.module),
optional('qualified'),
optional(seq('as', field('alias', $.module))),
optional(seq(
optional('hiding'),
field('names', $.import_list),
)),
),
// ------------------------------------------------------------------------
// export
// ------------------------------------------------------------------------
module_export: $ => seq('module', field('module', $.module)),
exports: $ => parens(
$,
optional(sep1(',', choice(field('export', alias($._ie_entity, $.export)), $.module_export))),
optional(','),
),
// ------------------------------------------------------------------------
// module body / sections
// ------------------------------------------------------------------------
header: $ => seq(
'module',
field('module', $.module),
field('exports', optional($.exports)),
$._where,
),
imports: $ => seq(semis($, field('import', $.import)), semi($)),
/**
* Using `semi` at the end instead of `semi_opt` increases parser size by a full megabyte!!
*
* This allows imports after the first declaration to prevent the tree from jittering while typing an import:
*
* > import A
* > imp
* > import B
*
* The partially typed `imp` will be parsed as a `top_splice`, which forces `imports` to reduce after `import A`.
* The rest of the file will then be part of `declarations` and all following imports will be broken until the keyword
* has been completed.
*/
declarations: $ => seq($.declaration, repeat(seq(semi($), choice($.declaration, $.import))), semi_opt($)),
_body: $ => seq(
choice($._cmd_layout_start, alias($._cmd_layout_start_explicit, '{')),
semi_opt($),
field('imports', optional($.imports)),
field('declarations', optional($.declarations)),
$._layout_end,
),
_layout_end: $ => choice(
$._cond_layout_end,
alias($._cond_layout_end_explicit, '}'),
),
/**
* This is a supertype.
*/
declaration: $ => choice(
$.decl,
$.type_synomym,
$.kind_signature,
$.type_family,
$.type_instance,
$.role_annotation,
$.data_type,
$.newtype,
$.data_family,
$.data_instance,
$.class,
$.instance,
$.default_types,
$.deriving_instance,
$.pattern_synonym,
$.foreign_import,
$.foreign_export,
$.fixity,
$.top_splice,
),
}