@willbooster/tree-sitter-haskell 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ const {
2
+ sep1,
3
+ sep,
4
+ braces,
5
+ layout,
6
+ unboxed_sum_single,
7
+ qualified,
8
+ context,
9
+ forall,
10
+ } = require('./util.js')
11
+
12
+ module.exports = {
13
+
14
+ // ------------------------------------------------------------------------
15
+ // fields
16
+ // ------------------------------------------------------------------------
17
+
18
+ field_name: $ => $.variable,
19
+ _qfield_name: $ => qualified($, $.field_name),
20
+ _field_names: $ => choice($.field_name, alias($._qfield_name, $.qualified)),
21
+
22
+ field_path: $ => seq(
23
+ field('field', $._field_names),
24
+ repeat1(seq($._tight_dot, field('subfield', $.field_name))),
25
+ ),
26
+
27
+ _field_spec: $ => choice(
28
+ $._field_names,
29
+ $.field_path,
30
+ ),
31
+
32
+ field: $ => prec('annotated', seq(
33
+ sep1(',', field('name', $.field_name)),
34
+ $._colon2,
35
+ field('type', $._parameter_type),
36
+ )),
37
+
38
+ _record_fields: $ => braces($, sep(',', field('field', $.field)), optional(',')),
39
+
40
+ // ------------------------------------------------------------------------
41
+ // deriving
42
+ // ------------------------------------------------------------------------
43
+
44
+ via: $ => seq('via', field('type', $.quantified_type)),
45
+
46
+ deriving_strategy: _ => choice('stock', 'newtype', 'anyclass'),
47
+
48
+ deriving: $ => seq(
49
+ optional($._phantom_deriving),
50
+ 'deriving',
51
+ optional(field('strategy', $.deriving_strategy)),
52
+ field('classes', $.constraint),
53
+ optional(field('via', $.via)),
54
+ ),
55
+
56
+ // ------------------------------------------------------------------------
57
+ // gadt
58
+ // ------------------------------------------------------------------------
59
+
60
+ _gadt_con_prefix: $ => field('type', $.quantified_type),
61
+
62
+ _gadt_con_record: $ => seq(
63
+ field('fields', alias($._record_fields, $.fields)),
64
+ field('arrow', $._fun_arrow),
65
+ field('type', $.quantified_type),
66
+ ),
67
+
68
+ /**
69
+ * GADT constructors only allow single foralls and contexts
70
+ */
71
+ gadt_constructor: $ => seq(
72
+ choice(
73
+ field('name', $._con),
74
+ field('names', alias($._con_binding_list, $.binding_list)),
75
+ ),
76
+ $._colon2,
77
+ forall($),
78
+ context($),
79
+ field('type', choice(
80
+ alias($._gadt_con_prefix, $.prefix),
81
+ alias($._gadt_con_record, $.record),
82
+ )),
83
+ ),
84
+
85
+ gadt_constructors: $ => layout($, field('constructor', $.gadt_constructor)),
86
+
87
+ _gadt: $ => seq(
88
+ optional($._kind_annotation),
89
+ $._where,
90
+ optional(field('constructors', $.gadt_constructors)),
91
+ ),
92
+
93
+ // ------------------------------------------------------------------------
94
+ // data type
95
+ // ------------------------------------------------------------------------
96
+
97
+ _field_type: $ => choice($.strict_field, $.lazy_field, $.type),
98
+
99
+ _datacon_prefix: $ => seq(
100
+ field('name', $._con),
101
+ repeat(prec('patterns', field('field', $._field_type))),
102
+ ),
103
+
104
+ _datacon_infix: $ => prec('infix', seq(
105
+ $._cond_data_infix,
106
+ field('left_operand', $._field_type),
107
+ field('operator', $._conop),
108
+ field('right_operand', $._field_type),
109
+ )),
110
+
111
+ _datacon_record: $ => seq(
112
+ field('name', $._constructor),
113
+ field('fields', alias($._record_fields, $.fields)),
114
+ ),
115
+
116
+ _datacon_unboxed_sum: $ => unboxed_sum_single($, $.quantified_type),
117
+
118
+ /**
119
+ * Special constructors occurring in GHC code
120
+ */
121
+ _datacon_special: $ => choice(
122
+ $.unit,
123
+ $.unboxed_unit,
124
+ alias($._plist, $.empty_list),
125
+ alias($._type_tuple, $.tuple),
126
+ alias($._type_unboxed_tuple, $.unboxed_tuple),
127
+ alias($._datacon_unboxed_sum, $.unboxed_sum),
128
+ ),
129
+
130
+ /**
131
+ * data constructors only allow single foralls and contexts
132
+ */
133
+ data_constructor: $ => seq(
134
+ forall($),
135
+ context($),
136
+ field('constructor', choice(
137
+ alias($._datacon_prefix, $.prefix),
138
+ alias($._datacon_infix, $.infix),
139
+ alias($._datacon_record, $.record),
140
+ alias($._datacon_special, $.special),
141
+ )),
142
+ ),
143
+
144
+ data_constructors: $ => sep1($._bar, field('constructor', $.data_constructor)),
145
+
146
+ _data_rhs: $ => choice(
147
+ $._kind_annotation,
148
+ seq('=', field('constructors', $.data_constructors)),
149
+ $._gadt,
150
+ ),
151
+
152
+ _data: $ => seq(
153
+ context($),
154
+ $._type_head,
155
+ optional($._data_rhs),
156
+ repeat(field('deriving', $.deriving)),
157
+ ),
158
+
159
+ data_type: $ => seq(
160
+ optional('type'),
161
+ 'data',
162
+ $._data,
163
+ ),
164
+
165
+ // ------------------------------------------------------------------------
166
+ // newtype
167
+ // ------------------------------------------------------------------------
168
+
169
+ _newtype_con_field: $ => $.type,
170
+
171
+ newtype_constructor: $ => seq(
172
+ field('name', $._con),
173
+ field('field', choice(
174
+ alias($._newtype_con_field, $.field),
175
+ alias($._record_fields, $.record),
176
+ )),
177
+ ),
178
+
179
+ _newtype: $ => seq(
180
+ choice(
181
+ seq('=', field('constructor', $.newtype_constructor)),
182
+ $._gadt,
183
+ ),
184
+ repeat(field('deriving', $.deriving)),
185
+ ),
186
+
187
+ newtype: $ => seq(
188
+ 'newtype',
189
+ context($),
190
+ $._type_head,
191
+ $._newtype,
192
+ ),
193
+
194
+ // ------------------------------------------------------------------------
195
+ // data family
196
+ // ------------------------------------------------------------------------
197
+
198
+ _datafam: $ => seq(
199
+ $._type_head,
200
+ optional($._kind_annotation),
201
+ ),
202
+
203
+ data_family: $ => seq(
204
+ 'data',
205
+ 'family',
206
+ $._datafam,
207
+ ),
208
+
209
+ _inst_adt: $ => seq(
210
+ forall($),
211
+ context($),
212
+ $._type_instance_head,
213
+ optional($._data_rhs),
214
+ repeat(field('deriving', $.deriving)),
215
+ ),
216
+
217
+ decl_inst_adt: $ => seq(
218
+ 'data',
219
+ 'instance',
220
+ $._inst_adt,
221
+ ),
222
+
223
+ _inst_newtype: $ => seq(
224
+ forall($),
225
+ context($),
226
+ $._type_instance_head,
227
+ $._newtype,
228
+ ),
229
+
230
+ decl_inst_newtype: $ => seq(
231
+ 'newtype',
232
+ 'instance',
233
+ $._inst_newtype,
234
+ ),
235
+
236
+ data_instance: $ => choice(
237
+ alias($.decl_inst_adt, $.data_type),
238
+ alias($.decl_inst_newtype, $.newtype),
239
+ ),
240
+
241
+ }
@@ -0,0 +1,184 @@
1
+ const {
2
+ sep1,
3
+ sep2,
4
+ parens,
5
+ layout,
6
+ } = require('./util.js')
7
+
8
+ module.exports = {
9
+
10
+ // ------------------------------------------------------------------------
11
+ // fixity
12
+ // ------------------------------------------------------------------------
13
+
14
+ _fun_arrow_prec: _ => seq('-', '1'),
15
+
16
+ // GHC.Types special decl
17
+ _fun_arrow_fixity: $ => seq(
18
+ field('associativity', 'infixr'),
19
+ field('precedence', alias($._fun_arrow_prec, $.integer)),
20
+ field('operator', alias('->', $.operator)),
21
+ ),
22
+
23
+ fixity: $ => choice(
24
+ $._fun_arrow_fixity,
25
+ seq(
26
+ field('associativity', choice('infixl', 'infixr', 'infix')),
27
+ field('precedence', optional($.integer)),
28
+ field('operator', sep1(',', choice($._operator_minus, $._varop, $._conop))),
29
+ ),
30
+ ),
31
+
32
+ // ------------------------------------------------------------------------
33
+ // signature
34
+ // ------------------------------------------------------------------------
35
+
36
+ _con_binding_list: $ => sep2(',', field('name', $._con)),
37
+
38
+ _var_binding_list: $ => sep2(',', field('name', $._var)),
39
+
40
+ signature: $ => seq(
41
+ choice(
42
+ field('name', $._var),
43
+ field('names', alias($._var_binding_list, $.binding_list)),
44
+ ),
45
+ $._type_annotation,
46
+ ),
47
+
48
+ // ------------------------------------------------------------------------
49
+ // function and pattern bind
50
+ // ------------------------------------------------------------------------
51
+
52
+ _simple_bind_match: $ => seq('=', field('expression', $._exp)),
53
+
54
+ _bind_match: $ => seq(
55
+ $._guards,
56
+ '=',
57
+ $._cmd_texp_end,
58
+ field('expression', $._exp),
59
+ ),
60
+
61
+ _bind_matches: $ => seq(
62
+ choice(
63
+ field('match', alias($._simple_bind_match, $.match)),
64
+ repeat1(field('match', alias($._bind_match, $.match))),
65
+ ),
66
+ optional($._where_binds),
67
+ ),
68
+
69
+ _function_name: $ => field('name', $._var),
70
+
71
+ function_head_parens: $ => parens(
72
+ $,
73
+ choice(
74
+ $._function_head,
75
+ $._function_head_patterns,
76
+ ),
77
+ ),
78
+
79
+ _function_head_patterns: $ => choice(
80
+ $._function_name,
81
+ field('parens', $.function_head_parens),
82
+ ),
83
+
84
+ /**
85
+ * The difference between a `function` with an `infix` head and a `bind` with `pat_infix` is that the former is for
86
+ * _declaring_ a `varop` and the latter uses a `conop` to pattern match on the rhs expression.
87
+ * The former may not have a type annotation, while the latter may.
88
+ *
89
+ * > a <> b = undefined
90
+ * > h : t :: [Int] = undefined
91
+ */
92
+ _function_head_infix: $ => seq(
93
+ field('left_operand', $.pattern),
94
+ optional($._cond_no_section_op),
95
+ field('operator', choice(seq($._cond_minus, $._operator_minus), $._varop)),
96
+ field('right_operand', $.pattern),
97
+ ),
98
+
99
+ _function_head: $ => choice(
100
+ seq($._function_head_patterns, field('patterns', $.patterns)),
101
+ alias($._function_head_infix, $.infix),
102
+ ),
103
+
104
+ function: $ => seq(
105
+ $._function_head,
106
+ $._bind_matches,
107
+ ),
108
+
109
+ /**
110
+ * The `implicit_variable` here is for:
111
+ * g = let ?par = Impy 5 in f
112
+ */
113
+ bind: $ => prec('bind', seq(
114
+ choice(
115
+ field('pattern', $._pat),
116
+ field('name', $._var),
117
+ field('implicit', $.implicit_variable),
118
+ ),
119
+ $._bind_matches,
120
+ )),
121
+
122
+ /**
123
+ * This is a supertype.
124
+ */
125
+ decl: $ => choice(
126
+ $.signature,
127
+ $.function,
128
+ $.bind,
129
+ ),
130
+
131
+ _local_decl: $ => choice(
132
+ $.fixity,
133
+ $.decl,
134
+ ),
135
+
136
+ local_binds: $ => layout($, field('decl', $._local_decl)),
137
+
138
+ _where_binds: $ => seq($._where, optional(field('binds', $.local_binds))),
139
+
140
+ // ------------------------------------------------------------------------
141
+ // foreign
142
+ // ------------------------------------------------------------------------
143
+
144
+ calling_convention: _ => token(choice(
145
+ 'ccall',
146
+ 'stdcall',
147
+ 'capi',
148
+ 'prim',
149
+ 'javascript',
150
+ /[A-Z_]+/, // It's common in GHC to use a cpp #define for this
151
+ )),
152
+
153
+ safety: _ => token(choice(
154
+ 'unsafe',
155
+ 'safe',
156
+ 'interruptible',
157
+ )),
158
+
159
+ entity: $ => $.string,
160
+
161
+ foreign_import: $ => seq(
162
+ 'foreign',
163
+ 'import',
164
+ field('calling_convention', $.calling_convention),
165
+ optional(field('safety', $.safety)),
166
+ optional(field('entity', $.entity)),
167
+ field('signature', $.signature),
168
+ ),
169
+
170
+ foreign_export: $ => seq(
171
+ 'foreign',
172
+ 'export',
173
+ field('calling_convention', $.calling_convention),
174
+ optional(field('entity', $.entity)),
175
+ field('signature', $.signature),
176
+ ),
177
+
178
+ // ------------------------------------------------------------------------
179
+ // default
180
+ // ------------------------------------------------------------------------
181
+
182
+ default_types: $ => seq('default', parens($, optional(sep1(',', field('type', $._ktype))))),
183
+
184
+ }