Tree-sitter grammar for the Kraken programming language. Provides fast, incremental parsing for syntax highlighting, code navigation, and editor integration.
Version: v0.9.2 — This repo tracks the Kraken language version. Tags match compiler tags.
This grammar covers the full syntax of Kraken v0.9.2, including:
- Function declarations with generics,
pub,async,unsafe,const fn, where clauses - Struct, enum, union, class, and interface declarations
- Trait declarations and implementations with associated types
- Closure expressions with move semantics
- Advanced pattern matching (tuples, structs, enums, enum variants, ranges, guards, or-patterns)
- Type system (primitives, generics, tuples, arrays, function types, pointers, references, trait objects, containers, slices)
- Control flow (if, while, for, for-in, match, defer, break, continue)
- Operators (arithmetic, logical, bitwise, comparison, compound assignment)
- Module system (import, module declarations)
- Unsafe blocks and raw pointers
- Range expressions and try operator
- Await and spawn expressions
- Enum variant paths (
Enum::Variant) - Turbofish syntax for explicit type arguments
- Attributes (
#[repr(C)], etc.) - Hex, binary, and octal integer literals
- Doc comments (
///,//!)
- File extension:
.kr - Language name:
kraken - Scope:
source.kraken - Tree-sitter language ID:
kraken
All editor integrations should use these identifiers for consistency.
npm install tree-sitter-kraken # (publishing planned)Add to your Cargo.toml:
[dependencies]
tree-sitter-kraken = "0.9.2" # (publishing to crates.io planned)const Parser = require('tree-sitter');
const Kraken = require('tree-sitter-kraken');
const parser = new Parser();
parser.setLanguage(Kraken);
const sourceCode = `
fn main() -> int {
let x = 42;
return x;
}
`;
const tree = parser.parse(sourceCode);
console.log(tree.rootNode.toString());use tree_sitter::Parser;
fn main() {
let mut parser = Parser::new();
parser.set_language(tree_sitter_kraken::language()).unwrap();
let source_code = r#"
fn main() -> int {
let x = 42;
return x;
}
"#;
let tree = parser.parse(source_code, None).unwrap();
println!("{}", tree.root_node().to_sexp());
}Using nvim-treesitter:
require('nvim-treesitter.configs').setup {
ensure_installed = { "kraken" },
highlight = { enable = true },
}Add to languages.toml:
[[language]]
name = "kraken"
scope = "source.kraken"
file-types = ["kr"]
roots = ["Cargo.toml"]
comment-token = "//"
indent = { tab-width = 4, unit = " " }
[[grammar]]
name = "kraken"
source = { git = "https://github.com/kraken-lang/tree-sitter-kraken", rev = "main" }Generate the parser from grammar:
npm install
npm run buildRun the test suite:
npm testParse a specific file:
npm run parse examples/hello.krThe grammar is defined in grammar.js and covers:
- Declarations: Functions, structs, enums, traits, impl blocks
- Statements: Let bindings, control flow, expressions
- Expressions: Literals, operators, calls, closures
- Patterns: Comprehensive pattern matching support
- Types: Full type system including generics and trait objects
Syntax highlighting queries are provided in queries/highlights.scm. The grammar supports semantic highlighting for:
- Keywords and operators
- Function and type names
- Variables and parameters
- String literals and escape sequences
- Comments
- Attributes and macros
Tree-sitter provides incremental parsing, making it efficient for real-time editor use. The parser can handle large Kraken files with minimal latency.
This grammar tracks the Kraken language version. Version 0.9.2 covers the full syntax of Kraken v0.9.2. Gaps or edge cases are tracked in Issues.
Note: Tree-sitter grammars focus on syntax structure, not full semantic validation. For complete language correctness, use the Kraken compiler.
Licensed under the Apache License, Version 2.0. See LICENSE for details.
