A LeekScript parser implemented in Rust using sipha 2.0 (PEG parser with green/red syntax trees).
- Phase 1 (lexer) — Done: token stream (keywords, identifiers, numbers, strings, operators, brackets, comments). Use
parse_tokens(). - Phase 2 — Done: primary expressions (number, string, identifier, parenthesized). Use
parse_expression(). - Phase 3 — Done: program = list of statements (var/global/const/let, if/while/for/do-while, return/break/continue, blocks, expression statements). Use
parse(). - Phase 4 — Done: top-level statements include
include, function declarations, and class declarations; program root is a single node with statement children.
The leekscript binary supports format and validate (and more to come):
# Format from stdin to stdout
cargo run --bin leekscript -- format
# Format a file in place
cargo run --bin leekscript -- format --in-place script.leek
# Check if formatting would change (exit 1 if so)
cargo run --bin leekscript -- format --check script.leek
# Validate syntax and run semantic analysis (scopes, types, deprecations)
cargo run --bin leekscript -- validate script.leek
# Canonical format: normalize indentation, braces, semicolons
cargo run --bin leekscript -- format --canonical script.leekFormat: by default prints the syntax tree as-is (round-trip). Use --canonical to normalize layout (indent, brace style, semicolons). Use --preserve-comments (default) to include comments and whitespace. See leekscript format --help.
use leekscript_rs::{parse, parse_tokens, format, FormatterOptions};
// Token stream only (Phase 1)
let out = parse_tokens("var x = 42")?;
let root = out.syntax_root("var x = 42".as_bytes()).unwrap();
// Full parse
let root = parse("return 1 + 2")?.expect("root");
// Format
let options = FormatterOptions::default();
let formatted = format(&root, &options);# Parse and print syntax tree for example .leek files
cargo run -p leekscript-rs --example parse_leekscript
# Validate a program (optional: path to .leek file)
cargo run -p leekscript-rs --example validate_with_signatures
cargo run -p leekscript-rs --example validate_with_signatures script.leekcargo test -p leekscript-rsFrom the repository root (so the workspace uses local sipha):
cargo fmt --all -- --check
cargo clippy --workspace --all-features -- -D warnings
cargo test --workspace --all-featuresSee the root CONTRIBUTING.md and cursor.md for conventions and workflow.
See ARCHITECTURE.md for grammar phases (token stream → expression → program), the analysis pipeline (ScopeBuilder → Validator → TypeChecker → DeprecationChecker), and how DocumentAnalysis ties parsing, analysis, and definition map for LSP.
Grammar and token set are aligned with the LeekScript Java compiler (lexer in LexicalParser.java, token types in TokenType.java).