Expand description
§leekscript-rs
A LeekScript parser and analysis library implemented with sipha.
§Architecture
The crate is layered as follows (dependencies flow downward):
- Core:
syntax,types— token/node kinds, type system. - Parse:
grammar,parser,preprocess— grammar, parsing, include handling. - Analysis:
analysis— scope, validation, type checking. - Orchestration:
document,doc_comment— document-level API and doc comments. - Tooling:
formatter,visitor,tree_display, [transform] — formatting, visiting, display. - LSP: [
lsp] (and [utf16] when enabled) — language server and UTF-16 utilities.
See ARCHITECTURE.md in the repository root for layer details and where to add new
features (new syntax, LSP features, etc.).
§Utilities from sipha
The syntax tree (sipha::red::SyntaxNode) supports node_at_offset,
token_at_offset, first_token,
last_token, and find_ancestor.
Use parse_to_doc for a single handle (source, root, line index, and those helpers).
§UTF-16 (optional feature)
Enable the utf16 feature for LSP and editor integration: ParsedDoc then provides
offset_to_line_col_utf16,
offset_to_line_col_utf16_1based,
and span_to_utf16_range.
Modules§
- analysis
- doc_
comment - document
- formatter
- grammar
- parser
- preprocess
- signatures
- Loading of signature files (.sig) for stdlib and API definitions.
- syntax
- tree_
display - types
- visitor
Structs§
- Analysis
Result - Result of running scope building and validation.
- Analyze
Options - Options for running analysis (single entry point for program or document).
- DocComment
- Structured documentation parsed from a Doxygen-style comment.
- Document
Analysis - Result of document-level analysis: source, AST, diagnostics, scope, types, definition map, doc maps, class hierarchy.
- Document
Analysis Options - Options for building a
DocumentAnalysis. - Format
Driver - Driver that writes the syntax tree to a string by visiting tokens.
You can use [
format] directly, or build a custom flow withwalk(root, &mut driver, &opts). - Formatter
Options - Options for printing the syntax tree.
- Include
Tree - One file in the include tree: path, source, parsed root, and parsed included files in order.
- Line
Index - Maps byte offsets to line and column.
- Parsed
Doc - A successfully parsed document: source buffer, line index, and syntax root.
- Recover
Multi Result - Result of parsing in multi-error recovery mode: partial output and all collected errors.
- ScopeId
- Identifies a scope in the scope store (used for parent chain).
- Scope
Store - Store for all scopes; root is at index 0.
- Semantic
Diagnostic - A single diagnostic from semantic analysis or validation (e.g. undefined variable, type error).
- Text
Edit - A text edit: replace
old_source[start..end]withnew_text. - Tree
Display Options - Options for formatting the syntax tree.
- Walk
Options - Options that control how the tree is traversed.
Enums§
- Analysis
Error - Error codes for semantic validation (mirroring leekscript-java Error enum).
- Cast
Type - Cast compatibility between types (matches Java
CastType). - Include
Error - Error from the include preprocessor.
- Resolved
Symbol - Root
Symbol Kind - Kind of root-level symbol for definition map (matches scope seeding order).
- Severity
- Severity for analysis/validation diagnostics (errors, warnings, etc.).
- Type
- Primitive and built-in types matching leekscript-java’s Type constants.
Constants§
- KEYWORDS
LeekScriptkeywords (for completion and tooling). Sorted for display.
Traits§
- Visitor
- Trait for visiting a sipha syntax tree.
Functions§
- all_
files - Flatten the tree into (path, source) for all files (root first, then includes depth-first).
- analyze
- Build scope from the tree and run validation. Returns diagnostics and the scope store.
- analyze_
with_ include_ tree - Analyze the main file of an include tree: seed scope from included files and signatures, then analyze the main AST.
- analyze_
with_ options - Run analysis with the given options. Dispatches to
analyze,analyze_with_signatures, oranalyze_with_include_treeas appropriate. - analyze_
with_ signatures - Analyze the program with the root scope pre-seeded from signature files (e.g. stdlib constants and functions).
This allows references to global constants and built-in functions to resolve without errors.
Also seeds built-in type/class names (e.g.
Class,bool) so that commonLeekScriptcode validates. - build_
class_ super - Build map
class_name->superclass_namefrom the AST (for visibility: subclass can see protected). - build_
definition_ map - Build (name, kind) -> (path, start, end) for root-level symbols (includes first, then main).
- build_
doc_ map - Build a map from declaration (
start_byte,end_byte) to parsed documentation. - build_
grammar - Build the
LeekScriptgrammar (Phase 1: token stream). - build_
include_ tree - Build the include tree: parse
sourceas the main file, resolve eachinclude("path"), load and parse those files (with circular include detection), and return the tree. - build_
scope_ extents - Build list of (
ScopeId, extent) where extent is (start_byte,end_byte). Root scope (ScopeId(0)) is always first with extent (0,source_len). Remaining entries are in walk order, matchingscope_id_sequence. - build_
signature_ grammar - Build the signature file grammar.
Start rule:
start→sig_wssig_filesig_wseof. - decl_
span_ for_ name_ span - Find the declaration node span (for
doc_maplookup) that contains the given name span. - format
- Print the syntax tree to a string.
When
canonical_format,parenthesize_expressions, orannotate_typesis set, runs a custom walk; otherwise uses sipha’s emit API for round-trip. - format_
syntax_ tree - Format a syntax tree starting at
rootas a multi-line string. - is_
valid_ identifier - Returns true if
nameis a validLeekScriptidentifier (non-empty, starts with letter or underscore, rest alphanumeric or underscore). Used e.g. for rename validation in LSP. - parse
- Parse source as a program (Phase 3/4: list of statements).
- parse_
doc_ comment - Parse one or more raw comment strings (e.g. from multiple preceding trivia tokens).
If the combined text contains a block comment (
/** ... */), only that block is used so that a preceding line comment (e.g.// Comment) does not corrupt the doc block. - parse_
error_ to_ diagnostics - Convert a parse error into semantic diagnostics for LSP or other tooling.
- parse_
error_ to_ miette - Convert a parse error into a
miette::Reportwith source snippet and resolved literals. - parse_
expression - Parse source as a single expression (Phase 2).
- parse_
recovering - Parse in recovering mode: on failure, returns the partial output and the error.
- parse_
recovering_ multi - Parse in multi-error recovery mode: on statement failures, skip to the next sync point
(e.g.
;,}, or statement-start keyword) and continue, collecting up tomax_errorserrors. - parse_
signatures - Parse source as a signature file (function/class/global declarations only).
- parse_
to_ doc - Parse source and return a
ParsedDoc: source bytes, line index, and syntax root. - parse_
tokens - Parse source as a token stream (Phase 1 lexer).
- print_
syntax_ tree - Print the syntax tree to stdout.
- program_
literals - Literal table for the program grammar (used for parsing full programs).
- reparse
- Reparse after a text edit, reusing unchanged parts of the tree.
- reparse_
or_ parse - Apply a text edit and reparse incrementally, or fall back to full parse.
- scope_
at_ offset - Find the innermost scope containing the given byte offset. When extents is empty (e.g. partial state), returns root scope ID so the LSP never panics.
- seed_
scope_ from_ signatures - Seed the root scope from parsed signature file(s). Each element of
signature_rootsshould be the root node returned byparse_signatures()(may be a wrapper orNodeSigFile). - walk
- Walks the tree starting at
rootwith the given visitor and options.
Type Aliases§
- Walk
Result - Result of a visitor callback: continue walking or stop.