forked from tree-sitter/tree-sitter-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
248 lines (228 loc) · 5.53 KB
/
grammar.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
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
const basic = require('./grammar/basic.js')
const id = require('./grammar/id.js')
const type = require('./grammar/type.js')
const exp = require('./grammar/exp.js')
const pat = require('./grammar/pat.js')
const import_ = require('./grammar/import.js')
const module_ = require('./grammar/module.js')
const data = require('./grammar/data.js')
const class_ = require('./grammar/class.js')
const decl = require('./grammar/decl.js')
const pattern = require('./grammar/pattern.js')
const misc = require('./grammar/misc.js')
module.exports = grammar({
name: 'haskell',
/**
* These rules may occur anywhere in the grammar and don't have to be specified.
*/
extras: $ => [
/\p{Zs}/,
/\n/,
$.cpp,
$.comment,
$.pragma,
],
/**
* These rules are handled manually by the scanner. Whenever their identifiers are used in the rule tree, the parser
* executes the scanner.
* Since the newline character is present both here and in `extras`, the scanner will be called before every token.
* This makes indentation/layout tracking simpler.
*/
externals: $ => [
$._layout_semicolon,
$._layout_start,
$._layout_end,
$._dot,
$.where,
$._splice_dollar,
$._varsym,
$._consym,
$._tyconsym,
$.comment,
$.cpp,
$.comma,
$.quasiquote_start,
$.quasiquote_bar,
$.quasiquote_body,
$._strict,
$._unboxed_tuple_close,
'|',
'in',
/\n/,
$.empty_file,
],
inline: $ => [
$._number,
$._stringly,
$._qvarid,
$._operator_minus,
$._qvarsym,
$._qvarsym_nominus,
$._var,
$._qvar,
$._tyvar,
$._qconid,
$._qconsym,
$._con,
$._conop,
$._qconop,
$._op,
$._qop_nominus,
$._gcon_literal,
$._gcon,
$._tyconid,
$._qtyconid,
$._qtyconsym,
$._qtycon,
$._gtycon,
$._simple_tycon,
$._simple_tyconop,
$._quantifiers,
$._tyfam_pat_prefix,
$._tyfam_pat_infix,
],
precedences: _ => [
[
'context-empty',
'con_unit',
],
[
'infix-type',
'btype',
],
[
'function-type',
'type',
],
],
conflicts: $ => [
/**
* This could be done with the second named precedence further up, but it somehow overrides symbolic infix
* constructors.
* Needs more investigation.
*/
[$._type_infix, $.type_infix],
/**
* Optional context for a data/newtype decl with infix types:
*
* data a ~ b => A a b
* data a + b
*/
[$.type_name, $._simpletype_infix],
/**
* Same as above, but with regular types:
*
* data A a b
* data C a b => A a b
* data C Int a => A a
* data B Int ~ B a => A a
*/
[$.type_name, $._simpletype],
[$._atype, $.constraint],
/**
* Constraints and parenthesized types.
*
* data (A a) => A
* data (A a) %% A => A
*
* After the `a`, the closing parens is ambiguous.
*/
[$._type_infix, $.constraint],
/**
* Top-level expression splices fundamentally conflict with decls, and since decls start with either `var` or `pat`,
* they cannot be disambiguated.
*
* function_variable:
* func (A a) = a
*
* function_pattern:
* Just 1 = Just 1
* a : as = [1, 2, 3]
*
* splice:
* makeLenses ''A
*
* The disambiguation can clearly be made from the `=`, but my impression is that the conflict check only considers
* immediate lookahead.
*/
[$._fun_name, $.exp_name],
[$._fun_name, $.pat_name],
[$._fun_name, $.pat_name, $.exp_name],
[$.signature, $.pat_name],
[$.exp_name, $._pat_constructor],
[$.exp_name, $.pat_name],
[$._aexp, $._apat],
[$.pat_negation, $._literal],
/**
* Ambiguity between symbolic and regular constructors:
*
* data A = Maybe Int :+ Int
* data A = Name Int
*
* both start with two tycons.
*/
[$.type_name, $.data_constructor],
/**
* Ambiguity between symbolic and regular type family equations.
*/
[$.type_name, $.tyfam_pat],
/**
* For getting a node for function application, and no extra node if the expression only consists of one term.
*/
[$._exp_apply, $._fexp],
[$._exp_apply],
/**
* Same as `exp_apply`, but for patterns.
*/
[$.pat_apply, $._apat],
[$.pat_apply],
/**
* Same as `exp_apply`, but for types.
*/
[$.type_apply, $._btype],
[$.type_apply],
/**
* Implicit parameters have slightly weird restrictions.
*/
[$._type_or_implicit, $._context_constraints],
],
word: $ => $._varid,
rules: {
haskell: $ => choice(
$.empty_file,
$._module,
terminated($, $._topdecl),
),
_topdecl: $ => choice(
alias($.decl_type, $.type_alias),
alias($.decl_tyfam, $.type_family),
alias($.decl_tyinst, $.type_instance),
alias($.decl_role, $.role_annotation),
alias($.decl_adt, $.adt),
alias($.decl_newtype, $.newtype),
alias($.decl_datafam, $.data_family),
alias($.decl_datainst, $.data_instance),
alias($.decl_import, $.import),
alias($.decl_class, $.class),
alias($.decl_instance, $.instance),
alias($.decl_default, $.default_declaration),
$._decl_foreign,
alias($.decl_deriving, $.deriving_declaration),
$._decl,
alias($.decl_pattern, $.pattern_synonym),
$.top_splice,
),
...basic,
...id,
...type,
...exp,
...pat,
...import_,
...module_,
...data,
...class_,
...decl,
...pattern,
...misc,
}
})