-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar.js
299 lines (273 loc) · 7.66 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const basic = require('./grammar/basic.js')
const id = require('./grammar/id.js')
const rows = require('./grammar/rows_and_records.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 derive = require('./grammar/derive.js')
const pattern = require('./grammar/pattern.js')
module.exports = grammar({
name: 'purescript',
/**
* These rules may occur anywhere in the grammar and don't have to be specified.
*/
extras: $ => [
/\p{Zs}/,
/\n/,
/\r/,
$.cpp,
$.comment,
],
/**
* 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,
$._arith_dotdot,
$.where,
// TODO: Splices were removed from the JS grammar but not from the scanner yet.
$._splice_dollar,
$._varsym,
$._consym,
$._tyconsym,
$.comment,
$.cpp,
$.comma,
// TODO: Quasiquotes were removed from the JS grammar but not from the scanner yet.
$.quasiquote_start,
$.quasiquote_bar,
$.quasiquote_body,
$._strict,
$._lazy,
// TODO: Unboxed types were removed from the JS grammar but not from the scanner yet.
$._unboxed_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,
$._simple_qtyconop,
$._quantifiers,
$._qualifying_module,
],
precedences: _ => [
[
'infix-type',
'btype',
],
[
'function-type',
'type',
],
],
conflicts: $ => [
/**
* Rows and records conflict with parenthesized types.
* Seems to be related to visible type application specifically.
*/
[$.row_type, $.type_name],
// [$.row_type, $._tyvar_no_annotation],
[$._field_name_ty, $._tyvar_no_annotation],
// [$._row_variable, $._tyvar_no_annotation],
[$.record_type_literal, $.type_name],
[$._field_name, $.pat_field],
/**
* Record updates `f { x = x }` conflict with function application `f { x: x }`.
* In PureScript record updates in fact do have higher precedence than function
* application, such that `identity { a: 1 } { a = 2 }` is a valid expression,
* but this doesn't work for parsing them correctly.
*/
[$._record_update_lhs, $._aexp_projection],
[$._record_update_lhs, $.exp_name],
/**
* Newkind's and data's signatures/declarations are in obvious conflict:
*
* data A :: Type -> Type
* data A a
*
* vs
*
* data B :: forall k. k -> Type
* data B a = B
*
* TODO: replace [almost] all distinct kinds of kind/type signatures
* with a single `type_signature` node.
*/
[$._data_type_signature, $._newkind_type_signature],
/**
* This could be done with the second named precedence further up, but it somehow overrides symbolic infix
* constructors.
* Needs more investigation.
*/
[$.type_infix, $._type],
/**
* Same as above, but for expressions.
*/
[$._exp_infix, $.exp_infix],
/*
* Wildcards in expression sections and pattern wildcards.
* They should be easily disambiguable but currently the grammar isn't capable of this.
*/
[$.exp_section_left, $.pat_wildcard],
/**
* The definition of an infix expression is rather simple and as such
* it allows things which wouldn't be possible in reality:
*
* a ``b`` c
* (note the double '`' ticks)
*/
[$.exp_ticked],
/**
* 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],
/**
* 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, $.pat_name],
[$.signature, $.pat_name],
[$.exp_name, $._pat_constructor],
[$.exp_name, $.pat_name],
[$._aexp_projection, $._apat],
[$.pat_name, $._q_op],
[$.exp_array, $.pat_array],
/**
* 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],
/**
* A weird conflict involving fundeps and type variables in class heads,
* despite the fact that fundeps are delimited by `|`.
*/
[$.type_name, $.class_head],
/**
* Type names and class names both alias `$.constructor`.
*/
[$.type_name, $.class_name],
/**
* Same as above, but for operators.
*/
[$.operator, $.type_operator],
/**
* What a `forall` binds to is ambiguous from the parser's POV:
*
* `t :: forall a. Unit` ← binds to the single type name
* `t :: forall a. Unit → Unit` ← binds to the whole expression
*
* This is solvable in theory but likely not under the current
* implementation of `type.js`. Although, the costs of a more naive
* implementation are small; it'd work fine unless someone decided
* to write `t :: forall a. forall b. ...`, in which case it wouldn't
* parse the second `forall` correctly.
*/
[$._type,],
[$._btype,],
],
word: $ => $._varid,
rules: {
purescript: $ => choice(
$.empty_file,
$._decl_module,
terminated($, $._topdecl),
),
_topdecl: $ => choice(
alias($.decl_type, $.type_alias),
$.type_role_declaration,
alias($.decl_data, $.data),
alias($.decl_newtype, $.newtype),
// TODO: Imports cannot come in random places,
// the structure of a module is always `module M [exports] where [imports] …`
// should group these together to remove extra parser overhead and simplify it for all other symbols
alias($.decl_import, $.import),
$.class_declaration,
$.class_instance,
$._decl_foreign,
alias($.decl_derive, $.derive_declaration),
$._decl,
$.kind_declaration,
$.kind_value_declaration,
alias($.decl_pattern, $.pattern_synonym),
),
...basic,
...id,
...rows,
...type,
...exp,
...pat,
...import_,
...module_,
...data,
...class_,
...decl,
...derive,
...pattern,
}
})