tree-sitter-clean 1.0.0__tar.gz

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ishaq Muhammad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,12 @@
1
+ include LICENSE
2
+ include README.md
3
+ include grammar.js
4
+ include binding.gyp
5
+ include queries/*.scm
6
+ include src/grammar.json
7
+ include src/node-types.json
8
+ include src/parser.c
9
+ include src/scanner.c
10
+ include src/tree_sitter/*.h
11
+ include bindings/python/tree_sitter_clean/*.py
12
+ include bindings/python/tree_sitter_clean/*.c
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.1
2
+ Name: tree-sitter-clean
3
+ Version: 1.0.0
4
+ Summary: Clean grammar for tree-sitter
5
+ Home-page: https://github.com/ishaq2321/tree-sitter-clean
6
+ Author: Ishaq Muhammad
7
+ Author-email: Ishaq Muhammad <ishaq2321@users.noreply.github.com>
8
+ License: MIT
9
+ Project-URL: repository, https://github.com/ishaq2321/tree-sitter-clean
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: C
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Compilers
19
+ Classifier: Topic :: Text Processing :: Linguistic
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+
24
+ # tree-sitter-clean
25
+
26
+ [![CI][ci]](https://github.com/ishaq2321/tree-sitter-clean/actions/workflows/ci.yml)
27
+ [![discord][discord]](https://discord.gg/w7nTvsVJhm)
28
+ [![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
29
+ [![crates][crates]](https://crates.io/crates/tree-sitter-clean)
30
+ [![npm][npm]](https://www.npmjs.com/package/tree-sitter-clean)
31
+ [![pypi][pypi]](https://pypi.org/project/tree-sitter-clean)
32
+
33
+ Clean grammar for [tree-sitter][].
34
+
35
+ [tree-sitter]: https://github.com/tree-sitter/tree-sitter
36
+
37
+ ## Description
38
+
39
+ [Clean](https://clean-lang.org/) is a general-purpose, purely functional programming language with uniqueness typing. This grammar supports:
40
+
41
+ - Module system (`module`, `import`, `from...import`)
42
+ - Type system (`::`, algebraic data types, records, type classes)
43
+ - Functions (named, guards, `where`/`with` clauses)
44
+ - Patterns (variables, constructors, wildcards, lists, tuples)
45
+ - Expressions (application, infix operators, let, case, lambda)
46
+ - Comprehensions (list comprehensions with generators, guards, let qualifiers)
47
+ - Records (construction, update, field access)
48
+
49
+ ### Node Types
50
+
51
+ The grammar produces **69 named node types** for precise AST analysis, including:
52
+ - `application` — function/constructor call detection
53
+ - `field_access` — record field access
54
+ - `list_comprehension` — with `generator`, `guard`, `let_qualifier`
55
+ - `record_expression` / `record_update` — record manipulation
56
+ - `wildcard` — `_` pattern
57
+
58
+ ## Usage
59
+
60
+ ### Rust
61
+ ```rust
62
+ let mut parser = tree_sitter::Parser::new();
63
+ parser.set_language(&tree_sitter_clean::LANGUAGE.into())?;
64
+ ```
65
+
66
+ ### JavaScript
67
+ ```js
68
+ const Parser = require("tree-sitter");
69
+ const Clean = require("tree-sitter-clean");
70
+ const parser = new Parser();
71
+ parser.setLanguage(Clean);
72
+ ```
73
+
74
+ ### Python
75
+ ```python
76
+ import tree_sitter_clean
77
+ ```
78
+
79
+ ## References
80
+
81
+ - [Clean Language Report](https://clean-lang.org/)
82
+ - [Clean Book](https://clean.cs.ru.nl/Clean)
83
+
84
+ [ci]: https://img.shields.io/github/actions/workflow/status/ishaq2321/tree-sitter-clean/ci.yml?logo=github&label=CI
85
+ [discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
86
+ [matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
87
+ [npm]: https://img.shields.io/npm/v/tree-sitter-clean?logo=npm
88
+ [crates]: https://img.shields.io/crates/v/tree-sitter-clean?logo=rust
89
+ [pypi]: https://img.shields.io/pypi/v/tree-sitter-clean?logo=pypi&logoColor=ffd242
@@ -0,0 +1,66 @@
1
+ # tree-sitter-clean
2
+
3
+ [![CI][ci]](https://github.com/ishaq2321/tree-sitter-clean/actions/workflows/ci.yml)
4
+ [![discord][discord]](https://discord.gg/w7nTvsVJhm)
5
+ [![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
6
+ [![crates][crates]](https://crates.io/crates/tree-sitter-clean)
7
+ [![npm][npm]](https://www.npmjs.com/package/tree-sitter-clean)
8
+ [![pypi][pypi]](https://pypi.org/project/tree-sitter-clean)
9
+
10
+ Clean grammar for [tree-sitter][].
11
+
12
+ [tree-sitter]: https://github.com/tree-sitter/tree-sitter
13
+
14
+ ## Description
15
+
16
+ [Clean](https://clean-lang.org/) is a general-purpose, purely functional programming language with uniqueness typing. This grammar supports:
17
+
18
+ - Module system (`module`, `import`, `from...import`)
19
+ - Type system (`::`, algebraic data types, records, type classes)
20
+ - Functions (named, guards, `where`/`with` clauses)
21
+ - Patterns (variables, constructors, wildcards, lists, tuples)
22
+ - Expressions (application, infix operators, let, case, lambda)
23
+ - Comprehensions (list comprehensions with generators, guards, let qualifiers)
24
+ - Records (construction, update, field access)
25
+
26
+ ### Node Types
27
+
28
+ The grammar produces **69 named node types** for precise AST analysis, including:
29
+ - `application` — function/constructor call detection
30
+ - `field_access` — record field access
31
+ - `list_comprehension` — with `generator`, `guard`, `let_qualifier`
32
+ - `record_expression` / `record_update` — record manipulation
33
+ - `wildcard` — `_` pattern
34
+
35
+ ## Usage
36
+
37
+ ### Rust
38
+ ```rust
39
+ let mut parser = tree_sitter::Parser::new();
40
+ parser.set_language(&tree_sitter_clean::LANGUAGE.into())?;
41
+ ```
42
+
43
+ ### JavaScript
44
+ ```js
45
+ const Parser = require("tree-sitter");
46
+ const Clean = require("tree-sitter-clean");
47
+ const parser = new Parser();
48
+ parser.setLanguage(Clean);
49
+ ```
50
+
51
+ ### Python
52
+ ```python
53
+ import tree_sitter_clean
54
+ ```
55
+
56
+ ## References
57
+
58
+ - [Clean Language Report](https://clean-lang.org/)
59
+ - [Clean Book](https://clean.cs.ru.nl/Clean)
60
+
61
+ [ci]: https://img.shields.io/github/actions/workflow/status/ishaq2321/tree-sitter-clean/ci.yml?logo=github&label=CI
62
+ [discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
63
+ [matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
64
+ [npm]: https://img.shields.io/npm/v/tree-sitter-clean?logo=npm
65
+ [crates]: https://img.shields.io/crates/v/tree-sitter-clean?logo=rust
66
+ [pypi]: https://img.shields.io/pypi/v/tree-sitter-clean?logo=pypi&logoColor=ffd242
@@ -0,0 +1,35 @@
1
+ {
2
+ "targets": [
3
+ {
4
+ "target_name": "tree_sitter_clean_binding",
5
+ "dependencies": [
6
+ "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
7
+ ],
8
+ "include_dirs": [
9
+ "src",
10
+ ],
11
+ "sources": [
12
+ "bindings/node/binding.cc",
13
+ "src/parser.c",
14
+ ],
15
+ "variables": {
16
+ "has_scanner": "<!(node -p \"fs.existsSync('src/scanner.c')\")"
17
+ },
18
+ "conditions": [
19
+ ["has_scanner=='true'", {
20
+ "sources+": ["src/scanner.c"],
21
+ }],
22
+ ["OS!='win'", {
23
+ "cflags_c": [
24
+ "-std=c11",
25
+ ],
26
+ }, {
27
+ "cflags_c": [
28
+ "/std:c11",
29
+ "/utf-8",
30
+ ],
31
+ }],
32
+ ],
33
+ }
34
+ ]
35
+ }
@@ -0,0 +1,26 @@
1
+ """Clean grammar for tree-sitter"""
2
+
3
+ from importlib.resources import files as _files
4
+
5
+ from ._binding import language
6
+
7
+
8
+ def _get_query(name, file):
9
+ query = _files(f"{__package__}.queries") / file
10
+ globals()[name] = query.read_text()
11
+ return globals()[name]
12
+
13
+
14
+ def __getattr__(name):
15
+ if name == "HIGHLIGHTS_QUERY":
16
+ return _get_query("HIGHLIGHTS_QUERY", "highlights.scm")
17
+ if name == "TAGS_QUERY":
18
+ return _get_query("TAGS_QUERY", "tags.scm")
19
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
20
+
21
+
22
+ __all__ = [
23
+ "language",
24
+ "HIGHLIGHTS_QUERY",
25
+ "TAGS_QUERY",
26
+ ]
@@ -0,0 +1,35 @@
1
+ #include <Python.h>
2
+
3
+ typedef struct TSLanguage TSLanguage;
4
+
5
+ TSLanguage *tree_sitter_clean(void);
6
+
7
+ static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
8
+ return PyCapsule_New(tree_sitter_clean(), "tree_sitter.Language", NULL);
9
+ }
10
+
11
+ static struct PyModuleDef_Slot slots[] = {
12
+ #ifdef Py_GIL_DISABLED
13
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
14
+ #endif
15
+ {0, NULL}
16
+ };
17
+
18
+ static PyMethodDef methods[] = {
19
+ {"language", _binding_language, METH_NOARGS,
20
+ "Get the tree-sitter language for this grammar."},
21
+ {NULL, NULL, 0, NULL}
22
+ };
23
+
24
+ static struct PyModuleDef module = {
25
+ .m_base = PyModuleDef_HEAD_INIT,
26
+ .m_name = "_binding",
27
+ .m_doc = NULL,
28
+ .m_size = 0,
29
+ .m_methods = methods,
30
+ .m_slots = slots,
31
+ };
32
+
33
+ PyMODINIT_FUNC PyInit__binding(void) {
34
+ return PyModuleDef_Init(&module);
35
+ }
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.1
2
+ Name: tree-sitter-clean
3
+ Version: 1.0.0
4
+ Summary: Clean grammar for tree-sitter
5
+ Home-page: https://github.com/ishaq2321/tree-sitter-clean
6
+ Author: Ishaq Muhammad
7
+ Author-email: Ishaq Muhammad <ishaq2321@users.noreply.github.com>
8
+ License: MIT
9
+ Project-URL: repository, https://github.com/ishaq2321/tree-sitter-clean
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: C
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Compilers
19
+ Classifier: Topic :: Text Processing :: Linguistic
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+
24
+ # tree-sitter-clean
25
+
26
+ [![CI][ci]](https://github.com/ishaq2321/tree-sitter-clean/actions/workflows/ci.yml)
27
+ [![discord][discord]](https://discord.gg/w7nTvsVJhm)
28
+ [![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
29
+ [![crates][crates]](https://crates.io/crates/tree-sitter-clean)
30
+ [![npm][npm]](https://www.npmjs.com/package/tree-sitter-clean)
31
+ [![pypi][pypi]](https://pypi.org/project/tree-sitter-clean)
32
+
33
+ Clean grammar for [tree-sitter][].
34
+
35
+ [tree-sitter]: https://github.com/tree-sitter/tree-sitter
36
+
37
+ ## Description
38
+
39
+ [Clean](https://clean-lang.org/) is a general-purpose, purely functional programming language with uniqueness typing. This grammar supports:
40
+
41
+ - Module system (`module`, `import`, `from...import`)
42
+ - Type system (`::`, algebraic data types, records, type classes)
43
+ - Functions (named, guards, `where`/`with` clauses)
44
+ - Patterns (variables, constructors, wildcards, lists, tuples)
45
+ - Expressions (application, infix operators, let, case, lambda)
46
+ - Comprehensions (list comprehensions with generators, guards, let qualifiers)
47
+ - Records (construction, update, field access)
48
+
49
+ ### Node Types
50
+
51
+ The grammar produces **69 named node types** for precise AST analysis, including:
52
+ - `application` — function/constructor call detection
53
+ - `field_access` — record field access
54
+ - `list_comprehension` — with `generator`, `guard`, `let_qualifier`
55
+ - `record_expression` / `record_update` — record manipulation
56
+ - `wildcard` — `_` pattern
57
+
58
+ ## Usage
59
+
60
+ ### Rust
61
+ ```rust
62
+ let mut parser = tree_sitter::Parser::new();
63
+ parser.set_language(&tree_sitter_clean::LANGUAGE.into())?;
64
+ ```
65
+
66
+ ### JavaScript
67
+ ```js
68
+ const Parser = require("tree-sitter");
69
+ const Clean = require("tree-sitter-clean");
70
+ const parser = new Parser();
71
+ parser.setLanguage(Clean);
72
+ ```
73
+
74
+ ### Python
75
+ ```python
76
+ import tree_sitter_clean
77
+ ```
78
+
79
+ ## References
80
+
81
+ - [Clean Language Report](https://clean-lang.org/)
82
+ - [Clean Book](https://clean.cs.ru.nl/Clean)
83
+
84
+ [ci]: https://img.shields.io/github/actions/workflow/status/ishaq2321/tree-sitter-clean/ci.yml?logo=github&label=CI
85
+ [discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
86
+ [matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
87
+ [npm]: https://img.shields.io/npm/v/tree-sitter-clean?logo=npm
88
+ [crates]: https://img.shields.io/crates/v/tree-sitter-clean?logo=rust
89
+ [pypi]: https://img.shields.io/pypi/v/tree-sitter-clean?logo=pypi&logoColor=ffd242
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ binding.gyp
5
+ grammar.js
6
+ pyproject.toml
7
+ setup.py
8
+ bindings/python/tree_sitter_clean/__init__.py
9
+ bindings/python/tree_sitter_clean/binding.c
10
+ bindings/python/tree_sitter_clean.egg-info/PKG-INFO
11
+ bindings/python/tree_sitter_clean.egg-info/SOURCES.txt
12
+ bindings/python/tree_sitter_clean.egg-info/dependency_links.txt
13
+ bindings/python/tree_sitter_clean.egg-info/not-zip-safe
14
+ bindings/python/tree_sitter_clean.egg-info/top_level.txt
15
+ queries/highlights.scm
16
+ queries/tags.scm
17
+ src/grammar.json
18
+ src/node-types.json
19
+ src/parser.c
20
+ src/scanner.c
21
+ src/tree_sitter/alloc.h
22
+ src/tree_sitter/array.h
23
+ src/tree_sitter/parser.h
@@ -0,0 +1,2 @@
1
+ _binding
2
+ tree_sitter_clean
@@ -0,0 +1,269 @@
1
+ module.exports = grammar({
2
+ name: "clean",
3
+
4
+ extras: ($) => [/\s/, $.line_comment, $.block_comment],
5
+
6
+ word: ($) => $.identifier,
7
+
8
+ conflicts: ($) => [
9
+ [$._expression],
10
+ [$._expression, $._pattern],
11
+ [$.class_declaration],
12
+ [$.instance_declaration],
13
+ [$.data_constructor, $._type_atom],
14
+ [$._expression, $.let_expression],
15
+ [$._expression, $.let_before_expression],
16
+ ],
17
+
18
+ externals: ($) => [$._layout_semicolon, $._layout_start, $._layout_end],
19
+
20
+ rules: {
21
+ source_file: ($) =>
22
+ seq(
23
+ optional($._layout_start),
24
+ repeat(seq($._declaration, optional($._layout_semicolon))),
25
+ optional($._layout_end),
26
+ ),
27
+
28
+ _declaration: ($) =>
29
+ choice(
30
+ $.module_declaration,
31
+ $.import_declaration,
32
+ $.type_signature,
33
+ $.type_definition,
34
+ $.function_declaration,
35
+ $.class_declaration,
36
+ $.instance_declaration,
37
+ ),
38
+
39
+ module_declaration: ($) => seq(optional(choice("implementation", "definition", "system")), "module", $.identifier),
40
+
41
+ import_declaration: ($) =>
42
+ prec.left(
43
+ choice(
44
+ seq("import", $.identifier, repeat(seq(optional(","), $.identifier))),
45
+ seq("from", $.identifier, "import", $.identifier, repeat(seq(optional(","), $.identifier))),
46
+ ),
47
+ ),
48
+
49
+ // Type signature: name :: type
50
+ // e.g. factorial :: Int -> Int
51
+ // e.g. compose :: (a -> b) (b -> c) -> a -> c
52
+ type_signature: ($) => seq(field("name", $.identifier), "::", $._type_expression),
53
+
54
+ type_definition: ($) =>
55
+ prec.left(
56
+ seq(
57
+ "::",
58
+ optional(choice("*", "!")),
59
+ field("name", $.identifier),
60
+ repeat($.identifier),
61
+ optional(seq(choice("=", ":=="), $._type_rhs)),
62
+ ),
63
+ ),
64
+
65
+ _type_rhs: ($) => prec.left(choice($.data_constructors, $.record_definition, $._type_expression)),
66
+
67
+ data_constructors: ($) => prec.left(seq($.data_constructor, repeat(seq("|", $.data_constructor)))),
68
+
69
+ data_constructor: ($) => prec.left(seq($.identifier, repeat($._type_atom))),
70
+
71
+ record_definition: ($) => seq("{", repeat(seq($.record_field, optional(","))), "}"),
72
+
73
+ record_field: ($) => seq($.identifier, "::", $._type_expression),
74
+
75
+ class_declaration: ($) =>
76
+ seq(
77
+ "class",
78
+ field("name", $.identifier),
79
+ repeat1($.identifier),
80
+ optional(seq("where", repeat($.type_signature))),
81
+ ),
82
+
83
+ instance_declaration: ($) =>
84
+ seq(
85
+ "instance",
86
+ field("name", $.identifier),
87
+ repeat1($._type_atom),
88
+ optional(seq("where", repeat($.function_declaration))),
89
+ ),
90
+
91
+ function_declaration: ($) =>
92
+ seq(
93
+ field("name", $.identifier),
94
+ repeat($._pattern),
95
+ choice(seq("=", $._expression), repeat1($.guard_equation)),
96
+ optional(choice($.where_block, $.with_block)),
97
+ ),
98
+
99
+ guard_equation: ($) => seq("|", $._expression, "=", $._expression),
100
+
101
+ // where block: local definitions after a function body
102
+ // e.g. f x = g x where g y = y + 1
103
+ where_block: ($) => seq("where", $._layout_start, repeat1(seq(choice($.type_signature, $.function_declaration), optional($._layout_semicolon))), $._layout_end),
104
+
105
+ // with block: similar to where but with different semantics in Clean
106
+ with_block: ($) => seq("with", $._layout_start, repeat1(seq(choice($.type_signature, $.function_declaration), optional($._layout_semicolon))), $._layout_end),
107
+
108
+ _pattern: ($) =>
109
+ choice(
110
+ $.wildcard,
111
+ $.identifier,
112
+ $.lazy_pattern,
113
+ seq("(", repeat1(choice($._pattern, ",", $.operator)), ")"),
114
+ seq("[", repeat(seq($._pattern, optional(","))), "]"),
115
+ ),
116
+
117
+ // `_` wildcard — matches anything, binds nothing
118
+ wildcard: ($) => "_",
119
+
120
+ // Lazy pattern: ~pattern — defers evaluation until the value is needed
121
+ lazy_pattern: ($) => seq("~", $._pattern),
122
+
123
+ // ---- Type expressions ----
124
+ // Clean type expressions: right-associative -> for functions,
125
+ // left-associative juxtaposition for type application.
126
+ //
127
+ // Examples:
128
+ // Int → _type_atom
129
+ // a -> b → type_fun(a, ->, b)
130
+ // a -> b -> c → type_fun(a, ->, type_fun(b, ->, c))
131
+ // Maybe a → type_application(Maybe, a)
132
+ // Maybe a -> b → type_fun(type_application(Maybe, a), ->, b)
133
+ // (a, b) -> c → type_fun((a, b), ->, c)
134
+ // [Int] → _type_atom([Int])
135
+ // *World → _type_atom(*World)
136
+
137
+ _type_expression: ($) =>
138
+ choice(
139
+ $.type_fun,
140
+ $._type_application,
141
+ ),
142
+
143
+ type_fun: ($) => prec.right(1, seq($._type_expression, "->", $._type_expression)),
144
+
145
+ _type_application: ($) =>
146
+ prec.left(2, seq($._type_atom, repeat($._type_atom))),
147
+
148
+ _type_atom: ($) =>
149
+ choice(
150
+ $.identifier,
151
+ "*",
152
+ "!",
153
+ seq("(", $._type_expression, repeat(seq(",", $._type_expression)), ")"),
154
+ seq("[", $._type_expression, "]"),
155
+ ),
156
+
157
+ // ---- Term expressions ----
158
+
159
+ _expression: ($) =>
160
+ prec.left(
161
+ choice(
162
+ seq($._expression, $.operator, $._expression),
163
+ $.application,
164
+ $.lambda_expression,
165
+ $.field_access,
166
+ $.list_comprehension,
167
+ $.record_expression,
168
+ $.record_update,
169
+ $.let_expression,
170
+ $.let_before_expression,
171
+ $.case_expression,
172
+ $.identifier,
173
+ $.number,
174
+ $.string,
175
+ seq("(", repeat(choice($._expression, $.operator, ",")), ")"),
176
+ seq("[", repeat(seq($._expression, optional(","))), "]"),
177
+ ),
178
+ ),
179
+
180
+ application: ($) => prec.left(10, seq($._expression, $._expression)),
181
+
182
+ // Lambda: \pattern1 pattern2 ... -> expr or \pattern = expr
183
+ lambda_expression: ($) =>
184
+ prec.left(seq("\\", repeat1($._pattern), choice("->", "="), $._expression)),
185
+
186
+ // Record field access: `record.field` or `record!field` (strict)
187
+ field_access: ($) => prec.left(13, seq($._expression, choice(".", "!"), $.identifier)),
188
+
189
+ // Record construction: `{ name = "Ishaq", age = 25 }`
190
+ record_expression: ($) =>
191
+ seq(
192
+ "{",
193
+ repeat(seq(
194
+ field("name", $.identifier),
195
+ "=",
196
+ field("value", $._expression),
197
+ optional(","),
198
+ )),
199
+ "}",
200
+ ),
201
+
202
+ // Record update: `{ person & age = 26 }`
203
+ record_update: ($) =>
204
+ seq(
205
+ "{",
206
+ field("record", $._expression),
207
+ "&",
208
+ repeat(seq(
209
+ optional(","),
210
+ field("name", $.identifier),
211
+ "=",
212
+ field("value", $._expression),
213
+ )),
214
+ "}",
215
+ ),
216
+
217
+ // List comprehension: `[x * 2 \\ x <- [1..10] | isEven x]`
218
+ list_comprehension: ($) =>
219
+ seq(
220
+ "[",
221
+ field("body", $._expression),
222
+ "\\\\",
223
+ repeat1(seq($.comprehension_qualifier, optional(","))),
224
+ "]",
225
+ ),
226
+
227
+ comprehension_qualifier: ($) =>
228
+ choice(
229
+ $.generator,
230
+ $.guard,
231
+ $.let_qualifier,
232
+ ),
233
+
234
+ generator: ($) =>
235
+ seq(
236
+ field("pattern", $._pattern),
237
+ "<-",
238
+ field("expression", $._expression),
239
+ ),
240
+
241
+ guard: ($) => field("condition", $._expression),
242
+
243
+ let_qualifier: ($) =>
244
+ seq(
245
+ "let",
246
+ field("name", $.identifier),
247
+ "=",
248
+ field("value", $._expression),
249
+ ),
250
+
251
+ let_expression: ($) => seq("let", repeat1(choice($.function_declaration, $.type_signature)), "in", $._expression),
252
+
253
+ let_before_expression: ($) =>
254
+ seq(choice("#", "#!"), $._pattern, "=", $._expression, optional($.let_before_expression)),
255
+
256
+ case_expression: ($) => seq("case", $._expression, "of", $._layout_start, repeat1(seq($.case_alternative, optional($._layout_semicolon))), $._layout_end),
257
+
258
+ case_alternative: ($) =>
259
+ seq($._pattern, optional(seq("|", $._expression)), choice("->", "="), $._expression),
260
+
261
+ identifier: ($) => /[a-zA-Z_][a-zA-Z0-9_]*/,
262
+ operator: ($) => /[!@$%^&*+\-=\\|<>\/?~.]+/,
263
+ number: ($) => /\d+(\.\d+)?/,
264
+ string: ($) => /"([^"\\]|\\.)*"/,
265
+
266
+ line_comment: ($) => token(seq("//", /.*/)),
267
+ block_comment: ($) => token(seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, "/")),
268
+ },
269
+ })