-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathclass.js
130 lines (109 loc) · 3.05 KB
/
class.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
const {
sep1,
layout,
context,
forall,
} = require('./util.js')
module.exports = {
// ------------------------------------------------------------------------
// associated families
// ------------------------------------------------------------------------
/**
* In associated family declarations, result type aliasing without injectivity is invalid, since that syntax is taken
* by type instance declarations.
*/
_assoc_tyfam: $ => seq(
'type',
optional('family'),
$._type_head,
optional(choice(
$._kind_annotation,
seq(
$.type_family_result,
$.type_family_injectivity,
),
)),
),
_assoc_tyinst: $ => seq(
'type',
optional('instance'),
forall($),
$._cond_assoc_tyinst,
$._type_instance_common,
),
_assoc_datafam: $ => seq(
'data',
optional('family'),
$._datafam,
),
_assoc_datainst_adt: $ => seq(
'data',
optional('instance'),
$._inst_adt,
),
_assoc_datainst_newtype: $ => seq(
'newtype',
optional('instance'),
$._inst_newtype,
),
_assoc_datainst: $ => choice(
alias($._assoc_datainst_adt, $.data_type),
alias($._assoc_datainst_newtype, $.newtype),
),
// ------------------------------------------------------------------------
// class
// ------------------------------------------------------------------------
default_signature: $ => seq('default', field('signature', $.signature)),
/**
* Classes can have both type families and instances, but only data families.
*/
class_decl: $ => choice(
$._local_decl,
$.default_signature,
alias($._assoc_tyfam, $.type_family),
alias($._assoc_tyinst, $.type_instance),
alias($._assoc_datafam, $.data_family),
),
fundep: $ => seq(
field('matched', repeat1($.variable)),
$._arrow,
field('determined', repeat1($.variable)),
),
fundeps: $ => seq($._bar, sep1(',', field('fundep', $.fundep))),
class_declarations: $ => layout($, field('declaration', $.class_decl)),
class: $ => seq(
'class',
context($),
$._type_head,
field('fundeps', optional($.fundeps)),
optional(seq($._where, optional(field('declarations', $.class_declarations)))),
),
// ------------------------------------------------------------------------
// instance
// ------------------------------------------------------------------------
instance_decl: $ => choice(
$.decl,
alias($._assoc_datainst, $.data_instance),
alias($._assoc_tyinst, $.type_instance),
),
instance_declarations: $ => layout($, field('declaration', $.instance_decl)),
/**
* instances only allow single foralls and contexts
*/
_instance: $ => seq(
'instance',
forall($),
context($),
$._type_instance_head,
),
instance: $ => seq(
$._instance,
optional(seq($._where, optional(field('declarations', $.instance_declarations)))),
),
deriving_instance: $ => seq(
optional($._phantom_deriving),
'deriving',
optional(choice(field('strategy', $.deriving_strategy), field('via', $.via))),
$._instance,
),
}