tree-sitter-haskell 0.21.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
+ 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.
@@ -0,0 +1,402 @@
1
+ Metadata-Version: 2.1
2
+ Name: tree-sitter-haskell
3
+ Version: 0.21.0
4
+ Summary: Haskell grammar for tree-sitter
5
+ Author: Rick Winfrey
6
+ Maintainer: Torsten Schmits
7
+ License: MIT
8
+ Project-URL: Homepage, https://github.com/tree-sitter/tree-sitter-haskell
9
+ Keywords: incremental,parsing,tree-sitter,haskell
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Topic :: Software Development :: Compilers
13
+ Classifier: Topic :: Text Processing :: Linguistic
14
+ Classifier: Typing :: Typed
15
+ Requires-Python: >=3.8
16
+ Description-Content-Type: text/markdown
17
+ Provides-Extra: core
18
+ License-File: LICENSE
19
+
20
+ # tree-sitter-haskell
21
+
22
+ [![CI](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-haskell/actions/workflows/ci.yml)
23
+
24
+ Haskell grammar for [tree-sitter].
25
+
26
+ # References
27
+
28
+ - [Haskell 2010 Language Report – Syntax References][ref]
29
+ - [GHC Language Extensions][ext]
30
+
31
+ # Supported Language Extensions
32
+
33
+ These extensions are supported ✅, unsupported ❌ or not applicable because they don't involve parsing ➖️:
34
+
35
+ - AllowAmbiguousTypes ➖️
36
+ - ApplicativeDo ➖️
37
+ - Arrows ❌
38
+ - BangPatterns ✅
39
+ - BinaryLiterals ✅
40
+ - BlockArguments ✅
41
+ - CApiFFI ✅
42
+ - ConstrainedClassMethods ✅
43
+ - ConstraintKinds ✅
44
+ - CPP ✅
45
+ - CUSKs ✅
46
+ - DataKinds ✅
47
+ - DatatypeContexts ✅
48
+ - DefaultSignatures ✅
49
+ - DeriveAnyClass ➖️
50
+ - DeriveDataTypeable ➖️
51
+ - DeriveFoldable ➖️
52
+ - DeriveFunctor ➖️
53
+ - DeriveGeneric ➖️
54
+ - DeriveLift ➖️
55
+ - DeriveTraversable ➖️
56
+ - DerivingStrategies ✅
57
+ - DerivingVia ✅
58
+ - DisambiguateRecordFields ➖️
59
+ - DuplicateRecordFields ➖️
60
+ - EmptyCase ✅
61
+ - EmptyDataDecls ✅
62
+ - EmptyDataDeriving ✅
63
+ - ExistentialQuantification ✅
64
+ - ExplicitForAll ✅
65
+ - ExplicitNamespaces ✅
66
+ - ExtendedDefaultRules ➖️
67
+ - FlexibleContexts ✅
68
+ - FlexibleInstances ✅
69
+ - ForeignFunctionInterface ✅
70
+ - FunctionalDependencies ✅
71
+ - GADTs ✅
72
+ - GADTSyntax ✅
73
+ - GeneralisedNewtypeDeriving ➖️
74
+ - GHCForeignImportPrim ✅
75
+ - Haskell2010 ➖️
76
+ - Haskell98 ➖️
77
+ - HexFloatLiterals ✅
78
+ - ImplicitParams ✅
79
+ - ImplicitPrelude ➖️
80
+ - ImportQualifiedPost ✅
81
+ - ImpredicativeTypes ➖️
82
+ - IncoherentInstances ➖️
83
+ - InstanceSigs ✅
84
+ - InterruptibleFFI ✅
85
+ - KindSignatures ✅
86
+ - LambdaCase ✅
87
+ - LexicalNegation ❌
88
+ - LiberalTypeSynonyms ✅
89
+ - LinearTypes ✅
90
+ - ListTuplePuns ✅
91
+ - MagicHash ✅
92
+ - Modifiers ❌
93
+ - MonadComprehensions ➖️
94
+ - MonadFailDesugaring ➖️
95
+ - MonoLocalBinds ➖️
96
+ - MonomorphismRestriction ➖️
97
+ - MultiParamTypeClasses ✅
98
+ - MultiWayIf ✅
99
+ - NamedFieldPuns ✅
100
+ - NamedWildCards ✅
101
+ - NegativeLiterals ➖️
102
+ - NondecreasingIndentation ✅
103
+ - NPlusKPatterns ➖️
104
+ - NullaryTypeClasses ✅
105
+ - NumDecimals ➖️
106
+ - NumericUnderscores ✅
107
+ - OverlappingInstances ➖️
108
+ - OverloadedLabels ✅
109
+ - OverloadedLists ➖️
110
+ - OverloadedRecordDot ✅
111
+ - OverloadedRecordUpdate ✅
112
+ - OverloadedStrings ➖️
113
+ - PackageImports ✅
114
+ - ParallelListComp ✅
115
+ - PartialTypeSignatures ✅
116
+ - PatternGuards ✅
117
+ - PatternSynonyms ✅
118
+ - PolyKinds ➖️
119
+ - PostfixOperators ➖️
120
+ - QualifiedDo ✅
121
+ - QuantifiedConstraints ✅
122
+ - QuasiQuotes ✅
123
+ - Rank2Types ✅
124
+ - RankNTypes ✅
125
+ - RebindableSyntax ➖️
126
+ - RecordWildCards ➖️
127
+ - RecursiveDo ✅
128
+ - RequiredTypeArguments ✅
129
+ - RoleAnnotations ✅
130
+ - Safe ➖️
131
+ - ScopedTypeVariables ✅
132
+ - StandaloneDeriving ✅
133
+ - StandaloneKindSignatures ✅
134
+ - StarIsType ✅
135
+ - StaticPointers ❌
136
+ - Strict ➖️
137
+ - StrictData ✅
138
+ - TemplateHaskell ✅
139
+ - TemplateHaskellQuotes ✅
140
+ - TraditionalRecordSyntax ➖️
141
+ - TransformListComp ✅
142
+ - Trustworthy ➖️
143
+ - TupleSections ✅
144
+ - TypeAbstractions ✅
145
+ - TypeApplications ✅
146
+ - TypeData ✅
147
+ - TypeFamilies ✅
148
+ - TypeFamilyDependencies ✅
149
+ - TypeInType ✅
150
+ - TypeOperators ✅
151
+ - TypeSynonymInstances ➖️
152
+ - UnboxedSums ✅
153
+ - UnboxedTuples ✅
154
+ - UndecidableInstances ➖️
155
+ - UndecidableSuperClasses ➖️
156
+ - UnicodeSyntax ✅
157
+ - UnliftedFFITypes ➖️
158
+ - UnliftedNewtypes ✅
159
+ - Unsafe ➖️
160
+ - ViewPatterns ✅
161
+
162
+ # Bugs
163
+
164
+ ## CPP
165
+
166
+ Preprocessor `#elif` and `#else` directives cannot be handled correctly, since the parser state would have to be
167
+ manually reset to what it was at the `#if`.
168
+ As a workaround, the code blocks in the alternative branches are parsed as part of the directives.
169
+
170
+ # Querying
171
+
172
+ The grammar contains several [supertypes](https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types),
173
+ which group multiple other node types under a single name.
174
+
175
+ Supertype names do not occur as extra nodes in parse trees, but they can be used in queries in special ways:
176
+
177
+ - As an alias, matching any of their subtypes
178
+ - As prefix for one of their subtypes, matching its symbol only when it occurs as a production of the supertype
179
+
180
+ For example, the query `(expression)` matches the nodes `infix`, `record`, `projection`, `constructor`, and the second
181
+ and third `variable` in this tree for `cats <> Cat {mood = moods.sleepy}`:
182
+
183
+ ```
184
+ (infix
185
+ (variable)
186
+ (operator)
187
+ (record
188
+ (constructor)
189
+ (field_update
190
+ (field_name (variable))
191
+ (projection (variable) (field_name (variable)))))))))
192
+ ```
193
+
194
+ The two occurrences of `variable` in `field_name` (`mood` and `sleepy`) are not expressions, but record field names part
195
+ of a composite `record` expression.
196
+
197
+ Matching `variable` nodes specifically that are expressions is possible with the second special form.
198
+ A query for `(expression/variable)` will match only the other two, `cats` and `moods`.
199
+
200
+ The grammar's supertypes consist of the following sets:
201
+
202
+ - [`expression`](./grammar/exp.js)
203
+
204
+ Rules that are valid in any expression position, excluding type applications, explicit types and expression
205
+ signatures.
206
+
207
+ - [`pattern`](./grammar/pat.js)
208
+
209
+ Rules that are valid in any pattern position, excluding type binders, explicit types and pattern signatures.
210
+
211
+ - [`type`](./grammar/type.js)
212
+
213
+ Types that are either atomic (have no ambiguous associativity, like bracketed constructs, variables and type
214
+ constructors), applied types or infix types.
215
+
216
+ - [`quantified_type`](./grammar/type.js)
217
+
218
+ Types prefixed with a `forall`, context or function parameter.
219
+
220
+ - [`constraint`](./grammar/constraint.js)
221
+
222
+ Almost the same rules as `type`, but mirrored for use in contexts.
223
+
224
+ - [`constraints`](./grammar/constraints.js)
225
+
226
+ Analog of `quantified_type`, for constraints with `forall` or context.
227
+
228
+ - [`type_param`](./grammar/type.js)
229
+
230
+ Atomic nodes in type and class heads, like the three nodes following `A` in `data A @k a (b :: k)`.
231
+
232
+ - [`declaration`](./grammar/module.js)
233
+
234
+ All top-level declarations, like functions and data types.
235
+
236
+ - [`decl`](./grammar/decl.js)
237
+
238
+ Shorthand for declarations that are also valid in local bindings (`let` and `where`) and in class and instance bodies,
239
+ except for fixity declarations.
240
+ Consists of `signature`, `function` and `bind`.
241
+
242
+ - [`class_decl` and `instance_decl`](./grammar/class.js)
243
+
244
+ All declarations that are valid in classes and instances, which includes associated type and data families.
245
+
246
+ - [`statement`](./grammar/exp.js)
247
+
248
+ Different forms of `do`-notation statements.
249
+
250
+ - [`qualifier`](./grammar/exp.js)
251
+
252
+ Different forms of list comprehension qualifiers.
253
+
254
+ - [`guard`](./grammar/exp.js)
255
+
256
+ Different forms of guards in function equations and case alternatives.
257
+
258
+ # Development
259
+
260
+ The main driver for generating and testing the parser for this grammar is the [tree-sitter CLI][cli].
261
+ Other components of the project require additional tools, described below.
262
+
263
+ Some are made available through `npm` – for example, `npx tree-sitter` runs the CLI.
264
+ If you don't have `tree-sitter` available otherwise, prefix all the commands in the following sections with `npx`.
265
+
266
+ ## Output path
267
+
268
+ The CLI writes the shared library containing the parser to the directory denoted by `$TREE_SITTER_LIBDIR`.
269
+ If that variable is unset, it defaults to `$HOME/.cache/tree-sitter/lib`.
270
+
271
+ In order to avoid clobbering this global directory with development versions, you can set the env var to a local path:
272
+
273
+ ```
274
+ export TREE_SITTER_LIBDIR=$PWD/.lib
275
+ ```
276
+
277
+ ## The grammar
278
+
279
+ The javascript file `grammar.js` contains the entry point into the grammar's production rules.
280
+ Please consult the [tree-sitter documentation][grammar-docs] for a comprehensive introduction to the syntax and
281
+ semantics.
282
+
283
+ Parsing starts with the first item in the `rules` field:
284
+
285
+ ```javascript
286
+ {
287
+ rules: {
288
+ haskell: $ => seq(
289
+ optional($.header),
290
+ optional($._body),
291
+ ),
292
+ }
293
+ }
294
+ ```
295
+
296
+ ## Generating the parser
297
+
298
+ The first step in the development workflow converts the javascript rule definitions to C code in `src/parser.c`:
299
+
300
+ ```
301
+ $ tree-sitter generate
302
+ ```
303
+
304
+ Two byproducts of this process are written to `src/grammar.json` and `src/node-types.json`.
305
+
306
+ ## Compiling the parser
307
+
308
+ The C code is automatically compiled by most of the test tools mentioned below, but you can instruct tree-sitter to do
309
+ it in one go:
310
+
311
+ ```
312
+ $ tree-sitter generate --build
313
+ ```
314
+
315
+ If you've set `$TREE_SITTER_LIBDIR` as mentioned above, the shared object will be written to `$PWD/.lib/haskell.so`.
316
+
317
+ Aside from the generated `src/parser.c`, tree-sitter will also compile and link `src/scanner.c` into this object.
318
+ This file contains the _external scanner_, which is a custom extension of the built-in lexer whose purpose is to handle
319
+ language constructs that cannot be expressed (efficiently) in the javascript grammar, like Haskell layouts.
320
+
321
+ ### WebAssembly
322
+
323
+ The parser can be compiled to WebAssembly as well, which requires `emscripten`:
324
+
325
+ ```
326
+ $ tree-sitter build --wasm
327
+ ```
328
+
329
+ The resulting binary is written to `$PWD/tree-sitter-haskell.wasm`.
330
+
331
+ ## Testing the parser
332
+
333
+ The most fundamental test infrastructure for tree-sitter grammars consists of a set of code snippets with associated
334
+ reference ASTs stored in `./test/corpus/*.txt`.
335
+
336
+ ```
337
+ $ tree-sitter test
338
+ ```
339
+
340
+ Individual tests can be run by specifying (a substring of) their description with `-f`:
341
+
342
+ ```
343
+ $ tree-sitter test -f 'module: exports empty'
344
+ ```
345
+
346
+ The project contains several other types of tests:
347
+
348
+ - `test/parse/run.bash [update] [test names ...]` parses the files in `test/parse/*.hs` and compares the output with
349
+ `test/parse/*.target`.
350
+ If `update` is specified as the first argument, it will update the `.target` file for the first failing test.
351
+
352
+ - `test/query/run.bash [update] [test names ...]` parses the files in `test/query/*.hs`, applies the queries in
353
+ `test/query/*.query` and compares the output with `test/query/*.target`, similar to `test/parse`.
354
+
355
+ - `test/rust/parse-test.rs` contains a few tests that use tree-sitter's Rust API to extract the test ranges for
356
+ terminals in a slightly more convenient way.
357
+ This requires `cargo` to be installed, and can be executed with `cargo test` (which also runs the tests in
358
+ `bindings/rust`).
359
+
360
+ - `test/parse-libs [wasm]` clones a set of Haskell libraries to `test/libs` and parses the entire codebase.
361
+ When invoked as `test/parse-libs wasm`, it will use the WebAssembly parser.
362
+ This requires `bc` to be installed.
363
+
364
+ - `test/parse-lib name [wasm]` parses only the library `name` in that directory (without cloning the repository).
365
+
366
+ ### Debugging
367
+
368
+ The shared library built by `tree-sitter test` includes debug symbols, so if the scanner segfaults you can just run
369
+ `coredumpctl debug` to inspect the backtrace and memory:
370
+
371
+ ```
372
+ newline_lookahead () at src/scanner.c:2583
373
+ 2583 ((Newline *) 0)->indent = 5;
374
+ (gdb) bt
375
+ #0 newline_lookahead () at src/scanner.c:2583
376
+ #1 0x00007ffff7a0740e in newline_start () at src/scanner.c:2604
377
+ #2 scan () at src/scanner.c:2646
378
+ #3 eval () at src/scanner.c:2684
379
+ #4 tree_sitter_haskell_external_scanner_scan (payload=<optimized out>, lexer=<optimized out>,
380
+ valid_symbols=<optimized out>) at src/scanner.c:2724
381
+ #5 0x0000555555772488 in ts_parser.lex ()
382
+ ```
383
+
384
+ For more control, launch `gdb tree-sitter` and start the process with `run test -f 'some test'`, and set a breakpoint
385
+ with `break tree_sitter_haskell_external_scanner_scan`.
386
+
387
+ To disable optimizations, run `tree-sitter test --debug-build`.
388
+
389
+ #### Tracing
390
+
391
+ The `test` and `parse` commands offer two modes for obtaining detailed information about the parsing process.
392
+
393
+ With `tree-sitter test --debug`, every lexer step and shift/reduce action is printed to stderr.
394
+
395
+ With `tree-sitter test --debug-graph`, the CLI will generate an HTML file showing a graph representation of every step.
396
+ This requires `graphviz` to be installed.
397
+
398
+ [tree-sitter]: https://github.com/tree-sitter/tree-sitter
399
+ [ref]: https://www.haskell.org/onlinereport/haskell2010/haskellch10.html
400
+ [ext]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/table.html
401
+ [cli]: https://github.com/tree-sitter/tree-sitter/tree/master/cli
402
+ [grammar-docs]: https://tree-sitter.github.io/tree-sitter/creating-parsers#writing-the-grammar