@willbooster/tree-sitter-haskell 0.0.0-placeholder.0 → 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Max Brunsfeld
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.
package/README.md CHANGED
@@ -1,3 +1,156 @@
1
1
  # @willbooster/tree-sitter-haskell
2
2
 
3
- This version is a placeholder that reserves the package name so that releases can be published from GitHub Actions with trusted publishing. Use 1.0.0 or later.
3
+ [![npm version](https://img.shields.io/npm/v/@willbooster/tree-sitter-haskell.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-haskell)
4
+ [![license](https://img.shields.io/npm/l/@willbooster/tree-sitter-haskell.svg)](https://www.npmjs.com/package/@willbooster/tree-sitter-haskell)
5
+ [![Test](https://github.com/WillBooster/tree-sitter-haskell/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-haskell/actions/workflows/test.yml)
6
+ [![Test rust](https://github.com/WillBooster/tree-sitter-haskell/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-haskell/actions/workflows/test-rust.yml)
7
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8
+ [![wbfy](https://img.shields.io/badge/wbfy-20.36.11-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
9
+ [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-haskell.svg)](https://crates.io/crates/willbooster-tree-sitter-haskell)
10
+
11
+ Haskell grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
12
+ [tree-sitter/tree-sitter-haskell](https://github.com/tree-sitter/tree-sitter-haskell). We are grateful
13
+ to its authors and contributors. This is not an official release of that project.
14
+
15
+ This fork keeps the grammar of upstream's version 0.23.1 and generates the parser with the
16
+ [WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parser carries that fork's own ABI number, so it loads only with that fork's runtime, version 2.0.0
17
+ or later; upstream's runtime rejects it.
18
+ [Upstream's README at that version](https://github.com/tree-sitter/tree-sitter-haskell/blob/v0.23.1/README.md) lists
19
+ the supported language extensions and, under "Querying", explains how to query the grammar's supertypes; it is the
20
+ readme that the comments in `grammar.js` refer to.
21
+
22
+ ## Differences from upstream
23
+
24
+ `grammar.js`, the files in `grammar/`, the external scanner (`src/scanner.c`, `src/unicode.h`), `queries/`, and
25
+ upstream's corpus are those of upstream's 0.23.1, except for:
26
+
27
+ - **The newline token** (`grammar.js`, `grammar/externals.js`): the entry `/\n/` of `extras` and of `externals` is
28
+ written `token(prec(0, /\n/))` in both places, with a comment line above the one in `grammar.js` that points here. The WillBooster/tree-sitter generator turns an unnamed pattern in
29
+ `extras` into a lexer separator, which leaves the external token of the same pattern without a symbol, and the
30
+ generator then fails. Precedence 0 is the default, so the token lexes as before, and the generated parser has the
31
+ same symbols as upstream's. A later upstream version of these files needs the same two lines until the generator
32
+ keeps such a pattern as a token ([WillBooster/tree-sitter#129](https://github.com/WillBooster/tree-sitter/issues/129)).
33
+ - **`queries/highlights.scm`**: three patterns that match fields of the supertype `decl` are written through its
34
+ subtypes (`decl/function`, `decl/bind`, `decl/signature`), as upstream's repository has them since
35
+ `tree-sitter/tree-sitter-haskell@0b2ebf7`. A parser of ABI 15 or later carries the supertypes, and the runtime then
36
+ rejects the 0.23.1 patterns as impossible; upstream's own CLI 0.27.0 does so as well.
37
+ - **`test/corpus/null-characters.txt`** is added, because the fuzz job checks that every NUL character of the corpus
38
+ reaches the fuzz seeds and upstream's corpus has none.
39
+
40
+ A NUL character inside a string or character literal parses as part of the literal, where upstream's prebuilt parser
41
+ reports an error: the lexers of the WillBooster/tree-sitter generator let character classes match a NUL character.
42
+
43
+ ## Usage
44
+
45
+ The npm package ships `tree-sitter-haskell.wasm` for
46
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 2.0.0 or later, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from its path:
47
+
48
+ ```js
49
+ import { fileURLToPath } from 'node:url';
50
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
51
+
52
+ await Parser.init();
53
+ const parser = new Parser();
54
+ const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-haskell/tree-sitter-haskell.wasm'));
55
+ parser.setLanguage(await Language.load(wasmPath));
56
+ const tree = parser.parse('main = putStrLn "Hello World!"\n');
57
+ ```
58
+
59
+ In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
60
+
61
+ ```js
62
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
63
+ import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
64
+ import haskellUrl from '@willbooster/tree-sitter-haskell/tree-sitter-haskell.wasm?url';
65
+
66
+ await Parser.init({ locateFile: () => runtimeUrl });
67
+ const parser = new Parser();
68
+ parser.setLanguage(await Language.load(haskellUrl));
69
+ ```
70
+
71
+ Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
72
+ Node.js compatibility:
73
+
74
+ ```js
75
+ import { Language, Parser } from '@willbooster/web-tree-sitter';
76
+ import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
77
+ import haskell from '@willbooster/tree-sitter-haskell/tree-sitter-haskell.wasm';
78
+
79
+ await Parser.init({ wasmModule: runtime });
80
+ const parser = new Parser();
81
+ parser.setLanguage(await Language.load(haskell));
82
+ ```
83
+
84
+ The package also ships `grammar.js` with the files in `grammar/` it loads, the queries in `queries/`, and the node types in `src/node-types.json`.
85
+
86
+ In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-haskell) and on
87
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
88
+ fuzzed with:
89
+
90
+ ```toml
91
+ [dependencies]
92
+ tree-sitter = { package = "willbooster-tree-sitter", version = "2.0.0" }
93
+ tree-sitter-haskell = { package = "willbooster-tree-sitter-haskell", version = "1" }
94
+ ```
95
+
96
+ ```rust
97
+ let mut parser = tree_sitter::Parser::new();
98
+ parser.set_language(&tree_sitter_haskell::LANGUAGE.into())?;
99
+ ```
100
+
101
+ ## Development
102
+
103
+ ```sh
104
+ mise install
105
+ bun install --frozen-lockfile
106
+ bun run test/ci-setup # installs Chromium for the browser test
107
+ bun run build/ci
108
+ bun run test
109
+ script/parse-examples
110
+ cargo test
111
+ ```
112
+
113
+ The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
114
+ WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
115
+ not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
116
+ with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary that runs here. Run other CLI commands through it as
117
+ well (e.g. `script/tree-sitter parse file.hs`).
118
+
119
+ `bun run generate` records a fresh generation profile from the applicable `test/corpus` cases and Git-tracked
120
+ files in `examples/`, then generates compact parser tables. The parser also embeds metadata from `tree-sitter.json`. After changing a grammar file,
121
+ `tree-sitter.json`, a corpus case, or a tracked example,
122
+ regenerate and commit `src/`. Stage added or removed examples with `git add -A examples` before generation so the profile uses the intended file list.
123
+ Profiles in `.tmp/generation-profiles/` are temporary and must not be committed. `bun run build-wasm`, `bun run build/ci`,
124
+ and the release build regenerate the parsers before compiling them.
125
+
126
+ `bun run test` runs:
127
+
128
+ - the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
129
+ - an incremental-parsing check (`test/unit/incremental.test.ts`): `script/fuzz-corpus` runs `tree-sitter fuzz`, which
130
+ edits each corpus case at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
131
+ `TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits. It skips the eleven cases that
132
+ upstream's own parser also reparses differently from a fresh parse (see `script/fuzz-corpus`);
133
+ - a check that the real-world Haskell files in `examples/`, the checked-in ones and those of the cloned repositories, fail to
134
+ parse exactly as listed in `script/known-failures.txt`; upstream's prebuilt parser fails on the same files. The first run clones the repositories. The example repositories are pinned to commits in
135
+ `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples` rewrites it;
136
+ review its diff before committing;
137
+ - a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear
138
+ time, since consumers parse files while they are being edited. It loads the Wasm build through
139
+ @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
140
+ - a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
141
+ `willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
142
+ tests, the fuzzer, and the CLI on the latter;
143
+ - tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
144
+ Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
145
+
146
+ The tests and `script/parse-examples` compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared
147
+ by every checkout; `script/fuzz-corpus` builds a parser of its own in `.tmp/fuzz` for each run and deletes it
148
+ afterwards.
149
+
150
+ CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner natively, and fuzzes the parser
151
+ with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
152
+
153
+ ### References
154
+
155
+ - [Haskell 2010 Language Report: Syntax Reference](https://www.haskell.org/onlinereport/haskell2010/haskellch10.html)
156
+ - [GHC Language Extensions](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/table.html)
@@ -0,0 +1,130 @@
1
+ const {
2
+ sep1,
3
+ layout,
4
+ context,
5
+ forall,
6
+ } = require('./util.js')
7
+
8
+ module.exports = {
9
+
10
+ // ------------------------------------------------------------------------
11
+ // associated families
12
+ // ------------------------------------------------------------------------
13
+
14
+ /**
15
+ * In associated family declarations, result type aliasing without injectivity is invalid, since that syntax is taken
16
+ * by type instance declarations.
17
+ */
18
+ _assoc_tyfam: $ => seq(
19
+ 'type',
20
+ optional('family'),
21
+ $._type_head,
22
+ optional(choice(
23
+ $._kind_annotation,
24
+ seq(
25
+ $.type_family_result,
26
+ $.type_family_injectivity,
27
+ ),
28
+ )),
29
+ ),
30
+
31
+ _assoc_tyinst: $ => seq(
32
+ 'type',
33
+ optional('instance'),
34
+ forall($),
35
+ $._cond_assoc_tyinst,
36
+ $._type_instance_common,
37
+ ),
38
+
39
+ _assoc_datafam: $ => seq(
40
+ 'data',
41
+ optional('family'),
42
+ $._datafam,
43
+ ),
44
+
45
+ _assoc_datainst_adt: $ => seq(
46
+ 'data',
47
+ optional('instance'),
48
+ $._inst_adt,
49
+ ),
50
+
51
+ _assoc_datainst_newtype: $ => seq(
52
+ 'newtype',
53
+ optional('instance'),
54
+ $._inst_newtype,
55
+ ),
56
+
57
+ _assoc_datainst: $ => choice(
58
+ alias($._assoc_datainst_adt, $.data_type),
59
+ alias($._assoc_datainst_newtype, $.newtype),
60
+ ),
61
+
62
+ // ------------------------------------------------------------------------
63
+ // class
64
+ // ------------------------------------------------------------------------
65
+
66
+ default_signature: $ => seq('default', field('signature', $.signature)),
67
+
68
+ /**
69
+ * Classes can have both type families and instances, but only data families.
70
+ */
71
+ class_decl: $ => choice(
72
+ $._local_decl,
73
+ $.default_signature,
74
+ alias($._assoc_tyfam, $.type_family),
75
+ alias($._assoc_tyinst, $.type_instance),
76
+ alias($._assoc_datafam, $.data_family),
77
+ ),
78
+
79
+ fundep: $ => seq(
80
+ field('matched', repeat1($.variable)),
81
+ $._arrow,
82
+ field('determined', repeat1($.variable)),
83
+ ),
84
+
85
+ fundeps: $ => seq($._bar, sep1(',', field('fundep', $.fundep))),
86
+
87
+ class_declarations: $ => layout($, field('declaration', $.class_decl)),
88
+
89
+ class: $ => seq(
90
+ 'class',
91
+ context($),
92
+ $._type_head,
93
+ field('fundeps', optional($.fundeps)),
94
+ optional(seq($._where, optional(field('declarations', $.class_declarations)))),
95
+ ),
96
+
97
+ // ------------------------------------------------------------------------
98
+ // instance
99
+ // ------------------------------------------------------------------------
100
+
101
+ instance_decl: $ => choice(
102
+ $.decl,
103
+ alias($._assoc_datainst, $.data_instance),
104
+ alias($._assoc_tyinst, $.type_instance),
105
+ ),
106
+
107
+ instance_declarations: $ => layout($, field('declaration', $.instance_decl)),
108
+
109
+ /**
110
+ * instances only allow single foralls and contexts
111
+ */
112
+ _instance: $ => seq(
113
+ 'instance',
114
+ forall($),
115
+ context($),
116
+ $._type_instance_head,
117
+ ),
118
+
119
+ instance: $ => seq(
120
+ $._instance,
121
+ optional(seq($._where, optional(field('declarations', $.instance_declarations)))),
122
+ ),
123
+
124
+ deriving_instance: $ => seq(
125
+ optional($._phantom_deriving),
126
+ 'deriving',
127
+ optional(choice(field('strategy', $.deriving_strategy), field('via', $.via))),
128
+ $._instance,
129
+ ),
130
+ }
@@ -0,0 +1,116 @@
1
+ module.exports = {
2
+
3
+ conflicts: $ => [
4
+
5
+ /**
6
+ * For reference in GHC:
7
+ * - Note [Ambiguous syntactic categories]
8
+ * - Note [PatBuilder]
9
+ * - Note [Declaration/signature overlap]
10
+ * - These correspond to `DisambECP`
11
+ *
12
+ * (fun x) y = undefined
13
+ * (fun x -> y) = undefined
14
+ * (fun) <> x = undefined
15
+ *
16
+ * The first one is a regular function with some redundant parens, where `fun` is the declared name.
17
+ * The second one is a pattern binder with a view pattern, where `fun` is a free variable.
18
+ * The third one is an infix pattern binder, where `fun` is a simple varid pattern with redundant parens.
19
+ *
20
+ * These conflicts are also relevant for top-level expression splices, which fundamentally conflict with decls, and
21
+ * since decls start with either `var` or `pat`, they cannot be disambiguated.
22
+ *
23
+ * GHC parses functions and binds as expressions and sorts them into the right LHS in a post-processing step.
24
+ * Since this is not possible in tree-sitter, these conflicts are more function-centric than in GHC.
25
+ *
26
+ * function:
27
+ * func (A a) = a
28
+ *
29
+ * bind variable:
30
+ * a : as = [1, 2, 3]
31
+ *
32
+ * pattern bind infix:
33
+ * a : as = [1, 2, 3]
34
+ *
35
+ * pattern bind prefix:
36
+ * Just 1 = Just 1
37
+ *
38
+ * splice:
39
+ * makeLenses ''A
40
+ *
41
+ * Signature and bind:
42
+ *
43
+ * fun :: Int
44
+ * fun :: Int = 5
45
+ */
46
+
47
+ // Function vs bind
48
+ [$._function_name, $.pattern],
49
+
50
+ // Function vs bind vs splice
51
+ [$._function_name, $.pattern, $.expression],
52
+
53
+ // Bind vs splice
54
+ [$.pattern, $.expression],
55
+
56
+ // Signature vs bind
57
+ [$.signature, $.pattern],
58
+
59
+ /**
60
+ * Unboxed syntax
61
+ *
62
+ * The hash in the opening parenthesis of unboxed tuples can be an operator.
63
+ */
64
+ [$._operator_hash_head, $._unboxed_open],
65
+
66
+ /**
67
+ * Types conflicting with structures that look like types
68
+ *
69
+ * Note: These conflicts have been circumvented by a lookahead mechanism in the scanner.
70
+ * This comment is preserved for reference.
71
+ *
72
+ * `name` and `constructor` use the same terminal symbol, but we cannot reduce `constructor` in prefix data
73
+ * constructor declarations.
74
+ *
75
+ * In GHC, this corresponds to `DisambTD`.
76
+ *
77
+ * > data A = Name Int
78
+ * > data A = Maybe Int :+ Int
79
+ * > data A = Monoid a => A a
80
+ *
81
+ * All of these start with a `name` node.
82
+ * In the first example, the `name` is a data constructor, which will not be reduced to a `type`.
83
+ *
84
+ * In the second example, the `name` is a type constructor applied to another type in the left operand of an infix
85
+ * data constructor, so it must be reduced to `type`, and then to `apply` with the `Int`.
86
+ *
87
+ * In the third example, the `name` is a type constructor applied to a variable resulting in a constraint.
88
+ * It will be reduced the same way as the second example, but using the class tree, which is mostly identical to the
89
+ * type tree, but conflicts since we want to distinguish classes from types granularly.
90
+ *
91
+ * In GHC, these correspond to `mkHsAppTyHeadPV` and `mkHsAppTyPV`.
92
+ *
93
+ * > data A a b = a `C` b => a `A` b
94
+ * > data A a b = a `A` b
95
+ *
96
+ * > data a *** b
97
+ * > data a +++ b => a *** b
98
+ *
99
+ * In GHC, this corresponds to `mkHsOpTyPV`.
100
+ *
101
+ * > class A where type a + b = r | r -> a b
102
+ * > class A where type a + b = (a, b)
103
+ *
104
+ * The first one is a type family declaration, the second one an instance.
105
+ *
106
+ * These were the conflicts that have been turned into scanner lookahead:
107
+ *
108
+ * [$._type_con, $.data_constructor],
109
+ * [$._type_con, $._type_head_name],
110
+ * [$._type_variable, $._tyvar],
111
+ * [$._constructor_ticked, $._tycon_ticked],
112
+ */
113
+
114
+ ],
115
+
116
+ }
@@ -0,0 +1,69 @@
1
+ const {
2
+ parens,
3
+ sep2,
4
+ } = require('./util.js')
5
+
6
+ module.exports = {
7
+
8
+ // ------------------------------------------------------------------------
9
+ // context
10
+ // ------------------------------------------------------------------------
11
+
12
+ _class_apply: $ => prec.left('apply', seq(
13
+ field('constructor', $.constraint),
14
+ field('argument', $._type_apply_arg),
15
+ )),
16
+
17
+ _class_infix: $ => prec.right('infix', seq(
18
+ $._cond_infix,
19
+ field('left_operand', $.type),
20
+ field('operator', $._tyops),
21
+ field('right_operand', $.type),
22
+ )),
23
+
24
+ _ctr_parens: $ => parens($, $.constraints),
25
+
26
+ _ctr_tuple: $ => parens($, sep2(',', $.constraints)),
27
+
28
+ /**
29
+ * Implicit parameters have an annotation with `::` but bind tighter than `_type_signature`, with the same precedence
30
+ * as foralls, contexts and arrows.
31
+ *
32
+ * > A => ?a :: A | associates as | A => (?a :: A)
33
+ * > ?a :: A -> A | associates as | ?a :: (A -> A)
34
+ */
35
+ implicit_parameter: $ => prec.left(seq(field('name', $.implicit_variable), $._type_annotation)),
36
+
37
+ constraint: $ => choice(
38
+ $._type_name,
39
+ alias($._class_infix, $.infix),
40
+ alias($._class_apply, $.apply),
41
+ alias($._ctr_parens, $.parens),
42
+ alias($._ctr_tuple, $.tuple),
43
+ alias($._type_wildcard, $.wildcard),
44
+ $._universal,
45
+ ),
46
+
47
+ _ctr_forall: $ => prec('fun', seq($._forall_body, '.', field('constraint', $.constraints))),
48
+
49
+ _ctr_context: $ => prec('fun', seq($._context_inline, field('constraint', $.constraints))),
50
+
51
+ _ctr_signature: $ => prec('annotated', seq(field('constraint', $.constraints), $._kind_annotation)),
52
+
53
+ constraints: $ => choice(
54
+ $.constraint,
55
+ alias($._ctr_context, $.context),
56
+ alias($._ctr_forall, $.forall),
57
+ $.implicit_parameter,
58
+ alias($._ctr_signature, $.signature),
59
+ ),
60
+
61
+ _context_inline: $ => seq(
62
+ $._cond_context,
63
+ field('context', $.constraint),
64
+ field('arrow', $._carrow),
65
+ ),
66
+
67
+ context: $ => prec('qtype-single', $._context_inline),
68
+
69
+ }