Skip to main content

leekscript_rs/
lib.rs

1//! # leekscript-rs
2//!
3//! A [LeekScript] parser and analysis library implemented with [sipha](https://docs.rs/sipha).
4//!
5//! ## Architecture
6//!
7//! The crate is layered as follows (dependencies flow downward):
8//!
9//! - **Core:** [`syntax`], [`types`] — token/node kinds, type system.
10//! - **Parse:** [`grammar`], [`parser`], [`preprocess`] — grammar, parsing, include handling.
11//! - **Analysis:** [`analysis`] — scope, validation, type checking.
12//! - **Orchestration:** [`document`], [`doc_comment`] — document-level API and doc comments.
13//! - **Tooling:** [`formatter`], [`visitor`], [`tree_display`], [`transform`] — formatting, visiting, display.
14//! - **LSP:** [`lsp`] (and [`utf16`] when enabled) — language server and UTF-16 utilities.
15//!
16//! See `ARCHITECTURE.md` in the repository root for layer details and where to add new
17//! features (new syntax, LSP features, etc.).
18//!
19//! [LeekScript]: https://leekwars.com/
20//!
21//! ## Utilities from sipha
22//!
23//! The syntax tree ([`sipha::red::SyntaxNode`]) supports [`node_at_offset`](sipha::red::SyntaxNode::node_at_offset),
24//! [`token_at_offset`](sipha::red::SyntaxNode::token_at_offset), [`first_token`](sipha::red::SyntaxNode::first_token),
25//! [`last_token`](sipha::red::SyntaxNode::last_token), and [`find_ancestor`](sipha::red::SyntaxNode::find_ancestor).
26//! Use [`parse_to_doc`] for a single handle (source, root, line index, and those helpers).
27//!
28//! ## UTF-16 (optional feature)
29//!
30//! Enable the `utf16` feature for LSP and editor integration: [`ParsedDoc`] then provides
31//! [`offset_to_line_col_utf16`](sipha::parsed_doc::ParsedDoc::offset_to_line_col_utf16),
32//! [`offset_to_line_col_utf16_1based`](sipha::parsed_doc::ParsedDoc::offset_to_line_col_utf16_1based),
33//! and [`span_to_utf16_range`](sipha::parsed_doc::ParsedDoc::span_to_utf16_range).
34
35// Re-export internal crates so the public API is unchanged.
36pub mod analysis {
37    pub use leekscript_analysis::*;
38}
39pub mod document {
40    pub use leekscript_document::*;
41}
42pub mod doc_comment {
43    pub use leekscript_core::doc_comment::*;
44}
45pub mod formatter {
46    pub use leekscript_tooling::formatter::*;
47}
48pub mod grammar {
49    pub use leekscript_core::grammar::*;
50}
51pub mod parser {
52    pub use leekscript_core::parser::*;
53}
54pub mod preprocess {
55    pub use leekscript_core::preprocess::*;
56}
57pub mod syntax {
58    pub use leekscript_core::syntax::*;
59}
60pub mod tree_display {
61    pub use leekscript_tooling::tree_display::*;
62}
63pub mod types {
64    pub use leekscript_core::types::*;
65}
66pub mod visitor {
67    pub use leekscript_tooling::visitor::*;
68}
69
70pub mod signatures;
71
72#[cfg(feature = "transform")]
73pub mod transform {
74    pub use leekscript_tooling::transform::*;
75}
76
77// Parsing and include preprocessing
78pub use leekscript_core::{
79    all_files, build_grammar, build_include_tree, build_signature_grammar, parse,
80    parse_error_to_diagnostics, parse_error_to_miette, parse_expression, parse_recovering,
81    parse_recovering_multi, parse_signatures, parse_to_doc, parse_tokens, program_literals,
82    reparse, reparse_or_parse, IncludeError, IncludeTree, TextEdit,
83};
84pub use sipha::engine::RecoverMultiResult;
85pub use sipha::parsed_doc::ParsedDoc;
86
87// Formatting
88pub use leekscript_tooling::formatter::{format, FormatDriver, FormatterOptions};
89
90// Tree display
91pub use leekscript_tooling::tree_display::{
92    format_syntax_tree, print_syntax_tree, TreeDisplayOptions,
93};
94
95// Transform (optional)
96#[cfg(feature = "transform")]
97pub use leekscript_tooling::transform::{transform, ExpandAssignAdd, TransformResult, Transformer};
98
99// Analysis (scope + validation)
100pub use leekscript_analysis::{
101    analyze, analyze_with_include_tree, analyze_with_options, analyze_with_signatures,
102    build_scope_extents, scope_at_offset, seed_scope_from_signatures, AnalysisError,
103    AnalysisResult, AnalyzeOptions, ResolvedSymbol, ScopeId, ScopeStore,
104};
105
106// Document-level analysis (single entry point for LSP)
107pub use leekscript_document::{
108    build_class_super, build_definition_map, decl_span_for_name_span, DocumentAnalysis,
109    DocumentAnalysisOptions, RootSymbolKind,
110};
111
112// Doc comments (Doxygen-style)
113pub use leekscript_core::doc_comment::{build_doc_map, parse_doc_comment};
114pub use leekscript_core::DocComment;
115
116// Types and visitor
117pub use leekscript_core::{CastType, Type};
118pub use leekscript_tooling::visitor::{walk, Visitor, WalkOptions, WalkResult};
119
120// Re-export for formatting semantic diagnostics (e.g. in CLI).
121pub use sipha::error::{SemanticDiagnostic, Severity};
122pub use sipha::line_index::LineIndex;
123
124// Syntax keywords and identifier validation for completion, rename, and tooling.
125pub use leekscript_core::syntax::{is_valid_identifier, KEYWORDS};
126
127#[cfg(feature = "lsp")]
128pub mod lsp;
129#[cfg(feature = "lsp")]
130pub use lsp::{
131    apply_content_changes, compute_code_actions, compute_completion, compute_definition,
132    compute_document_links, compute_document_symbols, compute_hover, compute_inlay_hints,
133    compute_rename, compute_semantic_tokens, compute_semantic_tokens_fallback,
134    compute_semantic_tokens_range, compute_workspace_symbols, find_references, prepare_rename,
135    resolve_completion_item, semantic_tokens_legend, semantic_tokens_provider, to_lsp_diagnostic,
136    DocumentAnalysisLspExt, InlayHintOptions, RenameError,
137};
138
139#[cfg(feature = "utf16")]
140pub mod utf16;
141
142#[cfg(feature = "utf16")]
143pub use sipha::utf16::{byte_offset_to_utf16, span_to_utf16_range, utf16_len};
144
145#[cfg(feature = "utf16")]
146pub use utf16::{byte_offset_to_line_col_utf16, line_col_utf16_to_byte, line_prefix_utf16};