@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.
package/grammar/id.js ADDED
@@ -0,0 +1,132 @@
1
+ const {
2
+ parens,
3
+ ticked,
4
+ promoted,
5
+ qualified,
6
+ } = require('./util.js')
7
+
8
+ module.exports = {
9
+ // ------------------------------------------------------------------------
10
+ // var
11
+ // ------------------------------------------------------------------------
12
+
13
+ _qualified_variable: $ => qualified($, $.variable),
14
+ _qvarid: $ => alias($._qualified_variable, $.qualified),
15
+ _varids: $ => choice($._qvarid, $.variable),
16
+
17
+ _var: $ => choice($.variable, $._pvarsym),
18
+ _qvar: $ => choice($._qvarid, $._pqvarsym),
19
+ _vars: $ => choice($._var, $._qvar),
20
+
21
+ _variable_ticked: $ => ticked($.variable),
22
+
23
+ _varop: $ => choice($.operator, alias($._variable_ticked, $.infix_id)),
24
+
25
+ _qvariable_ticked: $ => ticked($._qvarid),
26
+
27
+ _varids_ticked: $ => alias(
28
+ choice(
29
+ $._variable_ticked,
30
+ $._qvariable_ticked,
31
+ ),
32
+ $.infix_id,
33
+ ),
34
+
35
+ // ------------------------------------------------------------------------
36
+ // con
37
+ // ------------------------------------------------------------------------
38
+
39
+ _constructor: $ => alias($.name, $.constructor),
40
+ _qualified_constructor: $ => qualified($, $._constructor),
41
+ _qconid: $ => alias($._qualified_constructor, $.qualified),
42
+ _conids: $ => choice($._qconid, $._constructor),
43
+
44
+ _con: $ => choice($._constructor, $._pconsym),
45
+ _qcon: $ => choice($._qconid, $._pqconsym),
46
+ _cons: $ => choice(prec('con', $._con), $._qcon),
47
+
48
+ _constructor_ticked: $ => ticked($._constructor),
49
+ _conop: $ => choice($._constructor_operator_alias, alias($._constructor_ticked, $.infix_id)),
50
+
51
+ _qconstructor_ticked: $ => ticked($._qconid),
52
+
53
+ _conids_ticked: $ => alias(
54
+ choice(
55
+ $._constructor_ticked,
56
+ $._qconstructor_ticked,
57
+ ),
58
+ $.infix_id,
59
+ ),
60
+
61
+ // ------------------------------------------------------------------------
62
+ // tycon
63
+ // ------------------------------------------------------------------------
64
+
65
+ _tyconid: $ => $.name,
66
+ _qualified_type: $ => qualified($, $._tyconid),
67
+ _qtyconid: $ => alias($._qualified_type, $.qualified),
68
+ _tyconids: $ => choice($._qtyconid, $._tyconid),
69
+
70
+ _tycon_arrow: $ => parens($, alias($._arrow, $.operator)),
71
+ _qualified_arrow: $ => qualified($, alias($._arrow, $.operator)),
72
+ _qtycon_arrow: $ => parens($, alias($._qualified_arrow, $.qualified)),
73
+
74
+ _tycon: $ => choice($._tyconid, $._pvarsym, alias($._tycon_arrow, $.prefix_id), $._pconsym),
75
+ _qtycon: $ => choice($._qtyconid, alias($._qtycon_arrow, $.prefix_id), $._pqsym),
76
+ _tycons: $ => choice($._tycon, $._qtycon),
77
+
78
+ _promoted_tycons_alias: $ => seq('\'', $._cons),
79
+
80
+ _promoted_tycons: $ => alias($._promoted_tycons_alias, $.promoted),
81
+
82
+ _tycon_ticked: $ => ticked($._tyconid),
83
+ _qtycon_ticked: $ => ticked($._qtyconid),
84
+
85
+ _tyconids_ticked: $ => alias(
86
+ choice(
87
+ $._tycon_ticked,
88
+ $._qtycon_ticked,
89
+ ),
90
+ $.infix_id,
91
+ ),
92
+
93
+ _tyconops: $ => choice(
94
+ $._sym,
95
+ $._qsym,
96
+ $._operator_minus,
97
+ $._tyconids_ticked,
98
+ ),
99
+
100
+ /**
101
+ * Lenient parsing: `varsym` is not legal (like `'++`).
102
+ */
103
+ _promoted_tyconops_alias: $ => promoted($._tyconops),
104
+
105
+ _promoted_tyconops: $ => alias($._promoted_tyconops_alias, $.promoted),
106
+
107
+ _tyops: $ => choice(
108
+ $._tyconops,
109
+ $._promoted_tyconops,
110
+ ),
111
+
112
+ // ------------------------------------------------------------------------
113
+ // op
114
+ // ------------------------------------------------------------------------
115
+
116
+ _op_ticked: $ => choice(
117
+ $._varids_ticked,
118
+ $._conids_ticked,
119
+ ),
120
+
121
+ _ops: $ => choice(
122
+ $.operator,
123
+ $._qvarsym,
124
+ $.constructor_operator,
125
+ $._qconsym,
126
+ $._op_ticked,
127
+ ),
128
+
129
+ _name: $ => choice($._var, $._con),
130
+ _qname: $ => choice($._vars, $._cons),
131
+
132
+ }
@@ -0,0 +1,99 @@
1
+ module.exports = {
2
+
3
+ inline: $ => [
4
+
5
+ // ------------------------------------------------
6
+ // variable
7
+ // ------------------------------------------------
8
+
9
+ $._var,
10
+ $._vars,
11
+ $._varids,
12
+ $._varids_ticked,
13
+ $._varop,
14
+
15
+ // ------------------------------------------------
16
+ // constructor
17
+ // ------------------------------------------------
18
+
19
+ $._constructor,
20
+ $._con,
21
+ $._qcon,
22
+ $._cons,
23
+ $._conids,
24
+ $._conids_ticked,
25
+ $._conop,
26
+ $._op_ticked,
27
+ $._modid,
28
+
29
+ // ------------------------------------------------
30
+ // operator
31
+ // ------------------------------------------------
32
+
33
+ $._qvarsym,
34
+ $._qconsym,
35
+ $._sym,
36
+ $._qsym,
37
+ $._pqsym,
38
+ $._any_prefix_dot,
39
+ $._any_tight_dot,
40
+ $._unboxed_bar,
41
+
42
+ // ------------------------------------------------
43
+ // expression
44
+ // ------------------------------------------------
45
+
46
+ $._exp_name,
47
+ $._exp_greedy,
48
+ $._let,
49
+
50
+ // ------------------------------------------------
51
+ // pattern
52
+ // ------------------------------------------------
53
+
54
+ $._pat_apply_arg,
55
+ $._pat_name,
56
+ $._pat_texp,
57
+
58
+ // ------------------------------------------------
59
+ // type
60
+ // ------------------------------------------------
61
+
62
+ $._tyconid,
63
+ $._tyconids,
64
+ $._tycon,
65
+ $._qtycon,
66
+ $._tycons,
67
+ $._tyconops,
68
+ $._tyops,
69
+
70
+ $._type_name,
71
+ $._forall,
72
+ $._type_apply_arg,
73
+ $._parameter_type,
74
+ $._field_type,
75
+ $._type_head,
76
+ $._type_instance_head,
77
+ $._type_annotation,
78
+ $._kind_annotation,
79
+
80
+ // ------------------------------------------------
81
+ // literal
82
+ // ------------------------------------------------
83
+
84
+ $._number,
85
+ $._stringly,
86
+ $._unit_cons,
87
+ $._tuple_cons,
88
+ $._universal,
89
+
90
+ // ------------------------------------------------
91
+ // decl
92
+ // ------------------------------------------------
93
+
94
+ $._function_head_patterns,
95
+ $._function_head,
96
+
97
+ ],
98
+
99
+ }
@@ -0,0 +1,59 @@
1
+ const id_char = /[\pL\p{Mn}\pN_']*/
2
+
3
+ const varid_start_char = /[_\p{Ll}\p{Lo}]/
4
+
5
+ const conid_start_char = /[\p{Lu}\p{Lt}]/
6
+
7
+ module.exports = {
8
+
9
+ variable: _ => token(seq(varid_start_char, id_char, /#*/)),
10
+
11
+ implicit_variable: _ => token(seq('?', varid_start_char, id_char)),
12
+
13
+ name: _ => token(seq(conid_start_char, id_char, /#*/)),
14
+
15
+ label: _ => token(seq('#', varid_start_char, id_char)),
16
+
17
+ _carrow: _ => choice('=>', '⇒'),
18
+ _arrow: _ => choice('->', '→'),
19
+ _linear_arrow: _ => choice('->.', '⊸'),
20
+ _larrow: _ => choice('<-', '←'),
21
+ _colon2: _ => choice('::', '∷'),
22
+ _promote: _ => '\'',
23
+
24
+ _qual_dot: $ => seq($._cond_qual_dot, '.'),
25
+ _tight_dot: $ => seq($._cond_tight_dot, '.'),
26
+ _any_tight_dot: $ => choice($._qual_dot, $._tight_dot),
27
+ _prefix_dot: $ => seq($._cond_prefix_dot, '.'),
28
+ _any_prefix_dot: $ => choice($._qual_dot, $._prefix_dot),
29
+
30
+ _tight_at: $ => seq($._cond_tight_at, '@'),
31
+ _prefix_at: $ => seq($._cond_prefix_at, '@'),
32
+
33
+ _prefix_bang: $ => seq($._cond_prefix_bang, '!'),
34
+ _tight_bang: $ => seq($._cond_tight_bang, '!'),
35
+ _any_prefix_bang: $ => choice($._prefix_bang, $._tight_bang),
36
+
37
+ _prefix_tilde: $ => seq($._cond_prefix_tilde, '~'),
38
+ _tight_tilde: $ => seq($._cond_tight_tilde, '~'),
39
+ _any_prefix_tilde: $ => choice($._prefix_tilde, $._tight_tilde),
40
+
41
+ _prefix_percent: $ => seq($._cond_prefix_percent, '%'),
42
+
43
+ _dotdot: $ => seq($._cond_dotdot, '..'),
44
+
45
+ _paren_open: $ => seq(alias(/\(/, '('), $._cmd_texp_start),
46
+ _paren_close: $ => seq(alias(/\)/, ')'), $._cmd_texp_end),
47
+ _bracket_open: $ => seq('[', $._cmd_texp_start),
48
+ _bracket_close: $ => seq(']', $._cmd_texp_end),
49
+
50
+ // Sadly, this does not have the effect of creating a single terminal for the bracket :'(
51
+ _unboxed_open: $ => alias(seq($._paren_open, token.immediate('#')), '(#'),
52
+ _unboxed_close: $ => seq('#)', $._cmd_texp_end),
53
+ _unboxed_bar: _ => choice('|', token.immediate('|')),
54
+
55
+ _where: $ => seq(optional($._phantom_where), 'where'),
56
+
57
+ _bar: $ => seq(optional($._phantom_bar), '|'),
58
+
59
+ }
@@ -0,0 +1,102 @@
1
+ const {
2
+ parens,
3
+ brackets,
4
+ unboxed,
5
+ } = require('./util.js')
6
+
7
+ const decimal = /[0-9][0-9_]*/
8
+ const exponent = /[eE][+-]?[0-9_]+/
9
+ const hex_exponent = /[pP][+-]?[0-9a-fA-F_]+/
10
+ const magic_hash = rule => token(seq(rule, optional(token.immediate(/##?/))))
11
+
12
+ module.exports = {
13
+
14
+ // ------------------------------------------------------------------------
15
+ // literals
16
+ // ------------------------------------------------------------------------
17
+
18
+ // the `choice` here is necessary to avoid integers being parsed as floats
19
+ float: _ => magic_hash(
20
+ seq(
21
+ decimal,
22
+ choice(
23
+ seq(/\.[0-9_]+/, optional(exponent)),
24
+ exponent,
25
+ ),
26
+ ),
27
+ ),
28
+
29
+ char: _ => magic_hash(
30
+ choice(
31
+ /'[^']'/,
32
+ /'\\[^ ]*'/,
33
+ ),
34
+ ),
35
+
36
+ string: _ => magic_hash(
37
+ seq(
38
+ '"',
39
+ repeat(choice(
40
+ /[^\\"\n]/,
41
+ /\\(\^)?./,
42
+ /\\\n\s*\\/,
43
+ )),
44
+ '"',
45
+ ),
46
+ ),
47
+
48
+ _integer_literal: _ => magic_hash(decimal),
49
+ _binary_literal: _ => magic_hash(/0[bB][01_]+/),
50
+ _octal_literal: _ => magic_hash(/0[oO][0-7]+/),
51
+
52
+ _hex_literal: _ => magic_hash(
53
+ seq(
54
+ /0[xX][0-9a-fA-F_]+/,
55
+ optional(/\.[0-9a-fA-F_]+/),
56
+ optional(hex_exponent),
57
+ )
58
+ ),
59
+
60
+ integer: $ => choice(
61
+ $._binary_literal,
62
+ $._integer_literal,
63
+ $._octal_literal,
64
+ $._hex_literal,
65
+ ),
66
+
67
+ _stringly: $ => choice(
68
+ $.string,
69
+ $.char,
70
+ ),
71
+
72
+ _number: $ => choice(
73
+ $.integer,
74
+ $.float,
75
+ ),
76
+
77
+ _plist: $ => brackets($),
78
+
79
+ unit: $ => parens($),
80
+ unboxed_unit: $ => unboxed($),
81
+
82
+ prefix_tuple: $ => parens($, repeat1(',')),
83
+ prefix_unboxed_tuple: $ => unboxed($, repeat1(',')),
84
+ prefix_unboxed_sum: $ => unboxed($, repeat1($._unboxed_bar)),
85
+
86
+ literal: $ => choice(
87
+ $._stringly,
88
+ $._number,
89
+ ),
90
+
91
+ _unit_cons: $ => choice(
92
+ $.unit,
93
+ $.unboxed_unit,
94
+ ),
95
+
96
+ _tuple_cons: $ => choice(
97
+ $.prefix_tuple,
98
+ $.prefix_unboxed_tuple,
99
+ $.prefix_unboxed_sum,
100
+ ),
101
+
102
+ }
@@ -0,0 +1,150 @@
1
+ const {
2
+ sep1,
3
+ sep,
4
+ parens,
5
+ semi,
6
+ semi_opt,
7
+ semis,
8
+ } = require('./util.js')
9
+
10
+ module.exports = {
11
+
12
+ // ------------------------------------------------------------------------
13
+ // module names
14
+ // ------------------------------------------------------------------------
15
+
16
+ _modid: $ => alias($.name, $.module_id),
17
+
18
+ _modid_prefix: $ => prec('qualifying-module', seq($._modid, $._any_tight_dot)),
19
+
20
+ _qualifying_module: $ => repeat1($._modid_prefix),
21
+
22
+ module: $ => seq(repeat($._modid_prefix), $._modid),
23
+
24
+ // ------------------------------------------------------------------------
25
+ // import/export
26
+ // ------------------------------------------------------------------------
27
+
28
+ namespace: _ => choice('pattern', 'type'),
29
+
30
+ _child_type: $ => seq(field('namespace', 'type'), field('type', $._tyconids)),
31
+
32
+ _child: $ => choice(
33
+ alias($._child_type, $.associated_type),
34
+ $._qname,
35
+ ),
36
+
37
+ children: $ => parens($, sep(',', field('element', choice(alias('..', $.all_names), $._child)))),
38
+
39
+ _ie_entity: $ => seq(
40
+ optional(field('namespace', $.namespace)),
41
+ choice(
42
+ field('variable', $._varids),
43
+ field('type', $._tyconids),
44
+ field('operator', choice($._sym_prefix, $._pqsym)),
45
+ ),
46
+ optional(field('children', $.children)),
47
+ ),
48
+
49
+ // ------------------------------------------------------------------------
50
+ // import
51
+ // ------------------------------------------------------------------------
52
+
53
+ import_list: $ => parens(
54
+ $,
55
+ sep(',', field('name', alias($._ie_entity, $.import_name))),
56
+ optional(','),
57
+ ),
58
+
59
+ import: $ => seq(
60
+ 'import',
61
+ optional('qualified'),
62
+ optional(field('package', alias($.string, $.import_package))),
63
+ field('module', $.module),
64
+ optional('qualified'),
65
+ optional(seq('as', field('alias', $.module))),
66
+ optional(seq(
67
+ optional('hiding'),
68
+ field('names', $.import_list),
69
+ )),
70
+ ),
71
+
72
+ // ------------------------------------------------------------------------
73
+ // export
74
+ // ------------------------------------------------------------------------
75
+
76
+ module_export: $ => seq('module', field('module', $.module)),
77
+
78
+ exports: $ => parens(
79
+ $,
80
+ optional(sep1(',', choice(field('export', alias($._ie_entity, $.export)), $.module_export))),
81
+ optional(','),
82
+ ),
83
+
84
+ // ------------------------------------------------------------------------
85
+ // module body / sections
86
+ // ------------------------------------------------------------------------
87
+
88
+ header: $ => seq(
89
+ 'module',
90
+ field('module', $.module),
91
+ field('exports', optional($.exports)),
92
+ $._where,
93
+ ),
94
+
95
+ imports: $ => seq(semis($, field('import', $.import)), semi($)),
96
+
97
+ /**
98
+ * Using `semi` at the end instead of `semi_opt` increases parser size by a full megabyte!!
99
+ *
100
+ * This allows imports after the first declaration to prevent the tree from jittering while typing an import:
101
+ *
102
+ * > import A
103
+ * > imp
104
+ * > import B
105
+ *
106
+ * The partially typed `imp` will be parsed as a `top_splice`, which forces `imports` to reduce after `import A`.
107
+ * The rest of the file will then be part of `declarations` and all following imports will be broken until the keyword
108
+ * has been completed.
109
+ */
110
+ declarations: $ => seq($.declaration, repeat(seq(semi($), choice($.declaration, $.import))), semi_opt($)),
111
+
112
+ _body: $ => seq(
113
+ choice($._cmd_layout_start, alias($._cmd_layout_start_explicit, '{')),
114
+ semi_opt($),
115
+ field('imports', optional($.imports)),
116
+ field('declarations', optional($.declarations)),
117
+ $._layout_end,
118
+ ),
119
+
120
+ _layout_end: $ => choice(
121
+ $._cond_layout_end,
122
+ alias($._cond_layout_end_explicit, '}'),
123
+ ),
124
+
125
+ /**
126
+ * This is a supertype.
127
+ */
128
+ declaration: $ => choice(
129
+ $.decl,
130
+ $.type_synomym,
131
+ $.kind_signature,
132
+ $.type_family,
133
+ $.type_instance,
134
+ $.role_annotation,
135
+ $.data_type,
136
+ $.newtype,
137
+ $.data_family,
138
+ $.data_instance,
139
+ $.class,
140
+ $.instance,
141
+ $.default_types,
142
+ $.deriving_instance,
143
+ $.pattern_synonym,
144
+ $.foreign_import,
145
+ $.foreign_export,
146
+ $.fixity,
147
+ $.top_splice,
148
+ ),
149
+
150
+ }
@@ -0,0 +1,75 @@
1
+ const {
2
+ parens,
3
+ qualified,
4
+ } = require('./util.js')
5
+
6
+ module.exports = {
7
+
8
+ // ------------------------------------------------------------------------
9
+ // var
10
+ // ------------------------------------------------------------------------
11
+
12
+ _operator_qual_dot_head: $ => seq($._cond_qual_dot, $._varsym),
13
+
14
+ _operator_hash_head: _ => seq(
15
+ choice('#', token.immediate('#')),
16
+ optional(choice(token.immediate('#'), token.immediate('|'))),
17
+ ),
18
+
19
+ operator: $ => choice(
20
+ seq(optional($._cond_prefix_dot), $._varsym),
21
+ $._operator_hash_head,
22
+ '*',
23
+ ),
24
+
25
+ _operator_alias: $ => $.operator,
26
+
27
+ _operator_minus: $ => alias('-', $.operator),
28
+
29
+ _varsym_prefix: $ => parens(
30
+ $,
31
+ choice(
32
+ $.operator,
33
+ $._operator_minus,
34
+ alias($._operator_qual_dot_head, $.operator),
35
+ ),
36
+ ),
37
+
38
+ _pvarsym: $ => alias($._varsym_prefix, $.prefix_id),
39
+
40
+ _qualified_varsym: $ => qualified($, choice($.operator, $._operator_minus)),
41
+ _qvarsym: $ => alias($._qualified_varsym, $.qualified),
42
+
43
+ _qvarsym_prefix: $ => parens($, $._qvarsym),
44
+ _pqvarsym: $ => alias($._qvarsym_prefix, $.prefix_id),
45
+
46
+ // ------------------------------------------------------------------------
47
+ // con
48
+ // ------------------------------------------------------------------------
49
+
50
+ constructor_operator: $ => $._consym,
51
+
52
+ _constructor_operator_alias: $ => $.constructor_operator,
53
+
54
+ _consym_prefix: $ => parens($, $.constructor_operator),
55
+ _pconsym: $ => alias($._consym_prefix, $.prefix_id),
56
+
57
+ _qualified_consym: $ => qualified($, $.constructor_operator),
58
+ _qconsym: $ => alias($._qualified_consym, $.qualified),
59
+
60
+ _qconsym_prefix: $ => parens($, $._qconsym),
61
+ _pqconsym: $ => alias($._qconsym_prefix, $.prefix_id),
62
+
63
+ // ------------------------------------------------------------------------
64
+ // op
65
+ // ------------------------------------------------------------------------
66
+
67
+ _sym: $ => choice($._operator_alias, $._constructor_operator_alias),
68
+
69
+ _sym_prefix: $ => choice($._pvarsym, $._pconsym),
70
+
71
+ _qsym: $ => choice($._qvarsym, $._qconsym),
72
+
73
+ _pqsym: $ => choice($._pqvarsym, $._pqconsym),
74
+
75
+ }