tree-sitter-hcl
An incremental Tree-sitter grammar for HCL, the configuration language behind Terraform.
Repository Health
Technical Analysis
tree-sitter-hcl is a Tree-sitter grammar for HashiCorp Configuration Language (HCL), the syntax used by Terraform, Nomad, Consul, and other HashiCorp tools. It parses HCL source into a fast, incrementally-updated concrete syntax tree that editors and tooling can walk for highlighting, navigation, and analysis.
The grammar is validated against a large real-world corpus (thousands of HCL files from CoreOS, HashiCorp, Oracle, and community modules) with a 100% parse success rate, and ships bindings for Rust, C, Node, and other Tree-sitter hosts from a single definition.
What You Get
- A complete Tree-sitter grammar for HCL, compiled to a portable C parser.
- A Rust binding exposing the parser as a
tree_sitter_language::LanguageFnfor the Tree-sitter runtime. - Highlight and injection query files ready for editor integration.
- A grammar validated against thousands of real-world HCL files with a 100% parse success rate.
Common Use Cases
- Syntax highlighting for Terraform (.tf) and other HCL files in editors like Neovim.
- Structural navigation, folding, and text objects over HCL configuration.
- Static analysis and linting tools that need a reliable HCL parse tree.
Under The Hood
Architecture - The grammar is authored in grammar.js using Tree-sitter’s DSL and compiled to a portable src/parser.c plus src/node-types.json; language bindings under bindings/ each wrap that generated parser, and queries/ holds the highlight and injection query files editors consume.
Tech Stack - The Rust binding (built via build.rs with the cc crate) depends on tree-sitter-language at runtime and tree-sitter for testing; the repo also carries npm, Go, and Swift manifests so one grammar serves multiple ecosystems, with a Nix shell provided for development.
Code Quality - Correctness is anchored by a Tree-sitter test corpus plus a compliance suite over example/real_world_stuff that parses ~1,892 real HCL files at 100% success, and a separate fuzzing repository stresses the parser against malformed input.
API Design - The public surface follows Tree-sitter conventions: the crate exports a single LANGUAGE constant (LanguageFn) and query strings, so anyone familiar with the ecosystem can wire it into a parser in a few lines.