{"id":21517556,"url":"https://github.com/tek/tree-sitter-haskell","last_synced_at":"2025-09-13T16:44:37.953Z","repository":{"id":238304096,"uuid":"772767593","full_name":"tek/tree-sitter-haskell","owner":"tek","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-05T12:46:04.000Z","size":528,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-23T23:34:35.238Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-15T21:23:44.000Z","updated_at":"2025-03-05T12:46:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"646719e9-e47e-4088-9d20-792a441e616e","html_url":"https://github.com/tek/tree-sitter-haskell","commit_stats":null,"previous_names":["tek/tree-sitter-haskell"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Ftree-sitter-haskell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Ftree-sitter-haskell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Ftree-sitter-haskell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Ftree-sitter-haskell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tek","download_url":"https://codeload.github.com/tek/tree-sitter-haskell/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399826,"owners_count":20932875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T00:42:29.449Z","updated_at":"2025-04-09T21:42:22.571Z","avatar_url":"https://github.com/tek.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tree-sitter-haskell\n\nHaskell grammar for [tree-sitter].\n\n# References\n\n* [Haskell 2010 Language Report – Syntax References][ref]\n* [GHC Language Extensions][ext]\n\n# Building with nvim-treesitter\n\nWhen installing the grammar from source, be sure to include `src/scanner.c` in the source files:\n\n```vim\nlua \u003c\u003cEOF\nlocal parser_config = require \"nvim-treesitter.parsers\".get_parser_configs()\nparser_config.haskell = {\n  install_info = {\n    url = \"~/path/to/tree-sitter-haskell\",\n    files = {\"src/parser.c\", \"src/scanner.c\"}\n  }\n}\nEOF\n```\n\n# Supported Language Extensions\n\nThese extensions are supported ✅, unsupported ❌ or not applicable because they don't involve parsing ➖️:\n\n* AllowAmbiguousTypes ➖️\n* ApplicativeDo ➖️\n* Arrows ❌\n* BangPatterns ✅\n* BinaryLiterals ✅\n* BlockArguments ✅\n* CApiFFI ✅\n* ConstrainedClassMethods ✅\n* ConstraintKinds ✅\n* CPP ✅\n* CUSKs ✅\n* DataKinds ✅\n* DatatypeContexts ✅\n* DefaultSignatures ✅\n* DeriveAnyClass ➖️\n* DeriveDataTypeable ➖️\n* DeriveFoldable ➖️\n* DeriveFunctor ➖️\n* DeriveGeneric ➖️\n* DeriveLift ➖️\n* DeriveTraversable ➖️\n* DerivingStrategies ✅\n* DerivingVia ✅\n* DisambiguateRecordFields ➖️\n* DuplicateRecordFields ➖️\n* EmptyCase ✅\n* EmptyDataDecls ✅\n* EmptyDataDeriving ✅\n* ExistentialQuantification ✅\n* ExplicitForAll ✅\n* ExplicitNamespaces ✅\n* ExtendedDefaultRules ➖️\n* FlexibleContexts ✅\n* FlexibleInstances ✅\n* ForeignFunctionInterface ✅\n* FunctionalDependencies ✅\n* GADTs ✅\n* GADTSyntax ✅\n* GeneralisedNewtypeDeriving ➖️\n* GHCForeignImportPrim ✅\n* Haskell2010 ➖️\n* Haskell98 ➖️\n* HexFloatLiterals ✅\n* ImplicitParams ✅\n* ImplicitPrelude ➖️\n* ImportQualifiedPost ✅\n* ImpredicativeTypes ➖️\n* IncoherentInstances ➖️\n* InstanceSigs ✅\n* InterruptibleFFI ✅\n* KindSignatures ✅\n* LambdaCase ✅\n* LexicalNegation ❌\n* LiberalTypeSynonyms ✅\n* LinearTypes ✅\n* ListTuplePuns ✅\n* MagicHash ✅\n* Modifiers ❌\n* MonadComprehensions ➖️\n* MonadFailDesugaring ➖️\n* MonoLocalBinds ➖️\n* MonomorphismRestriction ➖️\n* MultilineStrings ✅\n* MultiParamTypeClasses ✅\n* MultiWayIf ✅\n* NamedFieldPuns ✅\n* NamedWildCards ✅\n* NegativeLiterals ➖️\n* NondecreasingIndentation ✅\n* NPlusKPatterns ➖️\n* NullaryTypeClasses ✅\n* NumDecimals ➖️\n* NumericUnderscores ✅\n* OverlappingInstances ➖️\n* OverloadedLabels ✅\n* OverloadedLists ➖️\n* OverloadedRecordDot ✅\n* OverloadedRecordUpdate ✅\n* OverloadedStrings ➖️\n* PackageImports ✅\n* ParallelListComp ✅\n* PartialTypeSignatures ✅\n* PatternGuards ✅\n* PatternSynonyms ✅\n* PolyKinds ➖️\n* PostfixOperators ➖️\n* QualifiedDo ✅\n* QuantifiedConstraints ✅\n* QuasiQuotes ✅\n* Rank2Types ✅\n* RankNTypes ✅\n* RebindableSyntax ➖️\n* RecordWildCards ➖️\n* RecursiveDo ✅\n* RequiredTypeArguments ✅\n* RoleAnnotations ✅\n* Safe ➖️\n* ScopedTypeVariables ✅\n* StandaloneDeriving ✅\n* StandaloneKindSignatures ✅\n* StarIsType ✅\n* StaticPointers ❌\n* Strict ➖️\n* StrictData ✅\n* TemplateHaskell ✅\n* TemplateHaskellQuotes ✅\n* TraditionalRecordSyntax ➖️\n* TransformListComp ✅\n* Trustworthy ➖️\n* TupleSections ✅\n* TypeAbstractions ✅\n* TypeApplications ✅\n* TypeData ✅\n* TypeFamilies ✅\n* TypeFamilyDependencies ✅\n* TypeInType ✅\n* TypeOperators ✅\n* TypeSynonymInstances ➖️\n* UnboxedSums ✅\n* UnboxedTuples ✅\n* UndecidableInstances ➖️\n* UndecidableSuperClasses ➖️\n* UnicodeSyntax ✅\n* UnliftedFFITypes ➖️\n* UnliftedNewtypes ✅\n* Unsafe ➖️\n* ViewPatterns ✅\n\n# Bugs\n\n## CPP\n\nPreprocessor `#elif` and `#else` directives cannot be handled correctly, since the parser state would have to be\nmanually reset to what it was at the `#if`.\nAs a workaround, the code blocks in the alternative branches are parsed as part of the directives.\n\n# Querying\n\nThe grammar contains several [supertypes](https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types),\nwhich group multiple other node types under a single name.\n\nSupertype names do not occur as extra nodes in parse trees, but they can be used in queries in special ways:\n\n* As an alias, matching any of their subtypes\n* As prefix for one of their subtypes, matching its symbol only when it occurs as a production of the supertype\n\nFor example, the query `(expression)` matches the nodes `infix`, `record`, `projection`, `constructor`, and the second\nand third `variable` in this tree for `cats \u003c\u003e Cat {mood = moods.sleepy}`:\n\n```\n(infix\n  (variable)\n  (operator)\n  (record\n    (constructor)\n    (field_update\n      (field_name (variable))\n      (projection (variable) (field_name (variable)))))))))\n```\n\nThe two occurrences of `variable` in `field_name` (`mood` and `sleepy`) are not expressions, but record field names part\nof a composite `record` expression.\n\nMatching `variable` nodes specifically that are expressions is possible with the second special form.\nA query for `(expression/variable)` will match only the other two, `cats` and `moods`.\n\nThe grammar's supertypes consist of the following sets:\n\n* [`expression`](./grammar/exp.js)\n\n  Rules that are valid in any expression position, excluding type applications, explicit types and expression\n  signatures.\n\n* [`pattern`](./grammar/pat.js)\n\n  Rules that are valid in any pattern position, excluding type binders, explicit types and pattern signatures.\n\n* [`type`](./grammar/type.js)\n\n  Types that are either atomic (have no ambiguous associativity, like bracketed constructs, variables and type\n  constructors), applied types or infix types.\n\n* [`quantified_type`](./grammar/type.js)\n\n  Types prefixed with a `forall`, context or function parameter.\n\n* [`constraint`](./grammar/constraint.js)\n\n  Almost the same rules as `type`, but mirrored for use in contexts.\n\n* [`constraints`](./grammar/constraints.js)\n\n  Analog of `quantified_type`, for constraints with `forall` or context.\n\n* [`type_param`](./grammar/type.js)\n\n  Atomic nodes in type and class heads, like the three nodes following `A` in `data A @k a (b :: k)`.\n\n* [`declaration`](./grammar/module.js)\n\n  All top-level declarations, like functions and data types.\n\n* [`decl`](./grammar/decl.js)\n\n  Shorthand for declarations that are also valid in local bindings (`let` and `where`) and in class and instance bodies,\n  except for fixity declarations.\n  Consists of `signature`, `function` and `bind`.\n\n* [`class_decl` and `instance_decl`](./grammar/class.js)\n\n  All declarations that are valid in classes and instances, which includes associated type and data families.\n\n* [`statement`](./grammar/exp.js)\n\n  Different forms of `do`-notation statements.\n\n* [`qualifier`](./grammar/exp.js)\n\n  Different forms of list comprehension qualifiers.\n\n* [`guard`](./grammar/exp.js)\n\n  Different forms of guards in function equations and case alternatives.\n\n# Development\n\nThe main driver for generating and testing the parser for this grammar is the [tree-sitter CLI][cli].\nOther components of the project require additional tools, described below.\n\nSome are made available through `npm` – for example, `npx tree-sitter` runs the CLI.\nIf you don't have `tree-sitter` available otherwise, prefix all the commands in the following sections with `npx`.\n\nFor [Nix] users, the project's [flake](./flake.nix) provides the full toolkit for maximum convenience, as well as\na bunch of test apps and packages.\nRun `nix develop` to start a shell with all tools in `$PATH`.\n\n## Output path\n\nThe CLI writes the shared library containing the parser to the directory denoted by `$TREE_SITTER_LIBDIR`.\nIf that variable is unset, it defaults to `$HOME/.cache/tree-sitter/lib`.\n\nIn order to avoid clobbering this global directory with development versions, you can set the env var to a local path:\n\n```\nexport TREE_SITTER_LIBDIR=$PWD/.lib\n```\n\nAll CLI commands that use the parser will load it from that path.\nThe Nix shell sets this variable automatically.\n\n## The grammar\n\nThe javascript file `grammar.js` contains the entry point into the grammar's production rules.\nPlease consult the [tree-sitter documentation][grammar-docs] for a comprehensive introduction to the syntax and\nsemantics.\n\nParsing starts with the first item in the `rules` field:\n\n```javascript\n{\n  rules: {\n    haskell: $ =\u003e seq(\n      optional($.header),\n      optional($._body),\n    ),\n  }\n}\n```\n\n## Generating the parser\n\nThe first step in the development workflow converts the javascript rule definitions to C code in `src/parser.c`:\n\n```\n$ tree-sitter generate\n```\n\nTwo byproducts of this process are written to `src/grammar.json` and `src/node-types.json`.\n\nNix derivation: `nix build .#parser-gen`\n\n## Compiling the parser\n\nThe C code is automatically compiled by most of the test tools mentioned below, but you can instruct tree-sitter to do\nit in one go:\n\n```\n$ tree-sitter generate --build\n```\n\nIf you've set `$TREE_SITTER_LIBDIR` as mentioned above, the shared object will be written to `$PWD/.lib/haskell.so`.\n\nAside from the generated `src/parser.c`, tree-sitter will also compile and link `src/scanner.c` into this object.\nThis file contains the _external scanner_, which is a custom extension of the built-in lexer whose purpose is to handle\nlanguage constructs that cannot be expressed (efficiently) in the javascript grammar, like Haskell layouts.\n\nNix derivation: `nix build .#parser-lib`\n\n### WebAssembly\n\nThe parser can be compiled to WebAssembly as well, which requires `emscripten`:\n\n```\n$ tree-sitter build --wasm\n```\n\nThe resulting binary is written to `$PWD/tree-sitter-haskell.wasm`.\n\nNix derivation: `nix build .#parser-wasm`\n\n## Testing the parser\n\nThe most fundamental test infrastructure for tree-sitter grammars consists of a set of code snippets with associated\nreference ASTs stored in `./test/corpus/*.txt`.\n\n```\n$ tree-sitter test\n```\n\nIndividual tests can be run by specifying (a substring of) their description with `-f`:\n\n```\n$ tree-sitter test -f 'module: exports empty'\n```\n\nThe project contains several other types of tests:\n\n* `test/parse/run.bash [update] [test names ...]` parses the files in `test/parse/*.hs` and compares the output with\n  `test/parse/*.target`.\n  If `update` is specified as the first argument, it will update the `.target` file for the first failing test.\n\n* `test/query/run.bash [update] [test names ...]` parses the files in `test/query/*.hs`, applies the queries in\n  `test/query/*.query` and compares the output with `test/query/*.target`, similar to `test/parse`.\n\n* `test/rust/parse-test.rs` contains a few tests that use tree-sitter's Rust API to extract the test ranges for\n  terminals in a slightly more convenient way.\n  This requires `cargo` to be installed, and can be executed with `cargo test` (which also runs the tests in\n  `bindings/rust`).\n\n* `test/parse-libs [wasm]` clones a set of Haskell libraries to `test/libs` and parses the entire codebase.\n  When invoked as `test/parse-libs wasm`, it will use the WebAssembly parser.\n  This requires `bc` to be installed.\n\n* `test/parse-lib name [wasm]` parses only the library `name` in that directory (without cloning the repository).\n\n### Debugging\n\nThe shared library built by `tree-sitter test` includes debug symbols, so if the scanner segfaults you can just run\n`coredumpctl debug` to inspect the backtrace and memory:\n\n```\nnewline_lookahead () at src/scanner.c:2583\n2583                ((Newline *) 0)-\u003eindent = 5;\n(gdb) bt\n#0  newline_lookahead () at src/scanner.c:2583\n#1  0x00007ffff7a0740e in newline_start () at src/scanner.c:2604\n#2  scan () at src/scanner.c:2646\n#3  eval () at src/scanner.c:2684\n#4  tree_sitter_haskell_external_scanner_scan (payload=\u003coptimized out\u003e, lexer=\u003coptimized out\u003e,\n    valid_symbols=\u003coptimized out\u003e) at src/scanner.c:2724\n#5  0x0000555555772488 in ts_parser.lex ()\n```\n\nFor more control, launch `gdb tree-sitter` and start the process with `run test -f 'some test'`, and set a breakpoint\nwith `break tree_sitter_haskell_external_scanner_scan`.\n\nTo disable optimizations, run `tree-sitter test --debug-build`.\n\n#### Tracing\n\nThe `test` and `parse` commands offer two modes for obtaining detailed information about the parsing process.\n\nWith `tree-sitter test --debug`, every lexer step and shift/reduce action is printed to stderr.\n\nWith `tree-sitter test --debug-graph`, the CLI will generate an HTML file showing a graph representation of every step.\nThis requires `graphviz` to be installed.\n\n[tree-sitter]: https://github.com/tree-sitter/tree-sitter\n[ref]: https://www.haskell.org/onlinereport/haskell2010/haskellch10.html\n[ext]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/exts/table.html\n[cli]: https://github.com/tree-sitter/tree-sitter/tree/master/cli\n[Nix]: https://nixos.org/manual/nix/stable/introduction\n[grammar-docs]: https://tree-sitter.github.io/tree-sitter/creating-parsers#writing-the-grammar\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftek%2Ftree-sitter-haskell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftek%2Ftree-sitter-haskell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftek%2Ftree-sitter-haskell/lists"}