Tree-sitter grammar for Dune build system files.
- Complete dune file support: Parses all major stanzas including
executable,library,rule,test,alias, and more - dune-project support: Full support for project metadata files including
lang,name,version,package, and all project configuration stanzas - dune-workspace support: Complete support for workspace configuration including
context,profile, and cross-compilation setup - Syntax highlighting: Comprehensive highlight queries for editor integration
- Field markers: Structured AST with named fields for precise code navigation
- 86+ test cases: Extensively tested against real-world Dune files
dune- Build files in each directorydune-project- Project metadata and configurationdune-workspace- Workspace-level settings and build contexts
cargo add tree-sitter-dunegit clone https://github.com/ocaml-dune/tree-sitter-dune
cd tree-sitter-dune
npm ci # installs the pinned tree-sitter CLI
npm run generate # make generate, at the pinned ABI
npx tree-sitter testThe committed src/parser.c is ABI 14, because most Emacs builds cannot load
ABI 15 yet. The CLI and ABI versions are set in the Makefile
(TREE_SITTER_VERSION and TS_ABI_VERSION).
use tree_sitter::Parser;
fn main() {
let mut parser = Parser::new();
parser.set_language(&tree_sitter_dune::LANGUAGE.into()).expect("Error loading Dune grammar");
let source_code = r#"
(library
(name my_lib)
(libraries base stdio))
"#;
let tree = parser.parse(source_code, None).unwrap();
println!("{}", tree.root_node().to_sexp());
}package main
import (
"context"
"fmt"
sitter "github.com/tree-sitter/go-tree-sitter"
dune "github.com/ocaml-dune/tree-sitter-dune/bindings/go"
)
func main() {
parser := sitter.NewParser()
parser.SetLanguage(sitter.NewLanguage(dune.Language()))
sourceCode := []byte(`
(library
(name my_lib)
(libraries base stdio))
`)
tree := parser.Parse(sourceCode, nil)
fmt.Println(tree.RootNode().ToSexp())
}With nvim-treesitter:
require'nvim-treesitter.configs'.setup {
ensure_installed = { "dune" },
highlight = {
enable = true,
},
}neocaml provides neocaml-dune-mode, a tree-sitter based major mode for dune, dune-project, and dune-workspace files. It uses Emacs's built-in treesit library (Emacs 29+).
✅ Fully Supported:
executable,executables- Build executableslibrary- OCaml librariestest,tests- Test executablesrule- Custom build rules with actionsalias- Named build targetsinstall- Installation rulestoplevel- Custom toplevelscopy_files- File copying with globsinclude- Include dune fragmentsmenhir- Menhir parser generatorocamllex,ocamlyacc- Lexer/parser generationforeign_library- C/C++ librariesplugin- Compiler pluginsmdx- Markdown executable blockscoq.theory- Coq integrationcram- Cram testssubdir- Nested directoriesenv- Environment configurationignored_subdirs- Ignore directoriesdeprecated_library_name- Library renaming- Actions:
run,bash,copy,diff,write-file,with-stdout-to,progn, and 15+ more
Coverage: 30/34 stanzas (88%), ~99% real-world usage
✅ Fully Supported (100% coverage):
lang- Language version (required)name,version,license- Project metadataauthors,maintainers,maintenance_intent- Contributorshomepage,documentation,bug_reports- URLssource- Repository information (github/git/uri)package- Package definitions with complex dependenciesgenerate_opam_files,opam_file_location- OPAM integrationusing- Extension configuration (menhir, coq, etc.)formatting- Formatter settingsdialect- Custom dialectspin- Pin dependencieswarnings- Compiler warnings configexplicit_js_mode- JavaScript modesubst- Watermarking- Boolean flags:
accept_alternative_dune_file_name,executables_implicit_empty_intf,expand_aliases_in_sandbox,implicit_transitive_deps,map_workspace_root,use_standard_c_and_cxx_flags,wrapped_executables
Coverage: 29/29 stanzas (100%)
✅ Fully Supported:
context- Build contexts(context default)- Default context(context (opam ...))- Opam switch contexts- Fields:
switch,name,profile,toolchain,host,target - Flags:
merlin,generate_merlin_rules,disable_dynamically_linked_foreign_archives - Additional:
root,lock_dir,paths,env,fdo,targets
profile- Build profile selection (dev/release/custom)
npm ci
npm run generatenpm ci also builds the Node binding, which fails on Node 26 (CI uses Node 20).
For grammar work only the CLI is needed:
npm ci --ignore-scripts && npm rebuild tree-sitter-cli.
npx tree-sitter testThis runs the corpus tests in test/corpus/ and the tag assertions in
test/tags/.
npx tree-sitter parse test/examples/dune
npx tree-sitter parse --debug test/examples/dune-projectContributions are welcome! Please:
- Add tests for any new features in
test/corpus/ - Ensure all tests pass with
npx tree-sitter test - Update this README if adding new stanza support
- Follow the existing code style
-
Define the stanza in
grammar.js:_stanza_my_new_stanza: ($) => dune_stanza( $, "my_stanza", choice( alias($._sf_my_field, $.stanza_field), $.sexp, ), ), _sf_my_field: ($) => dune_field($, "my_field", $.value_type),
Each field is a hidden
_sf_*rule aliased tostanza_field, so every field gets one wrapper node. Reuse an existing_sf_*rule when one fits. -
Add it to the
stanzachoice list -
Create tests in
test/corpus/ -
Run
npm run generate && npx tree-sitter test
MIT