toon_lsp/lib.rs
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024-2025 w0wl0lxd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published
6// by the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! # toon-lsp
17//!
18//! A Language Server Protocol (LSP) implementation for TOON (Token-Oriented Object Notation).
19//!
20//! TOON is a compact, human-readable encoding of the JSON data model designed for LLM prompts.
21//! This crate provides:
22//!
23//! - **AST with source positions** - Full abstract syntax tree with span information
24//! - **Parser** - TOON parser that produces positioned AST nodes
25//! - **LSP Server** - Complete language server with diagnostics, symbols, and more
26//!
27//! ## Architecture
28//!
29//! ```text
30//! ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
31//! │ Scanner │ ──▶ │ Parser │ ──▶ │ AST │
32//! │ (Lexer) │ │ │ │ (with Spans)│
33//! └─────────────┘ └─────────────┘ └─────────────┘
34//! │
35//! ▼
36//! ┌─────────────┐
37//! │ LSP Server │
38//! │ (tower-lsp) │
39//! └─────────────┘
40//! ```
41//!
42//! ## Usage
43//!
44//! ```rust
45//! use toon_lsp::{parse, AstNode};
46//!
47//! let source = "user:\n name: Alice\n age: 30";
48//! let ast = parse(source).expect("valid TOON");
49//!
50//! // Every AST node carries source positions (Span)
51//! let AstNode::Document { children, span } = &ast else { return };
52//! assert_eq!(span.start.line, 0); // 0-indexed
53//!
54//! // Walk objects via entries
55//! for node in children {
56//! if let AstNode::Object { entries, .. } = node {
57//! assert_eq!(entries[0].key, "user");
58//! }
59//! }
60//! ```
61//!
62//! **Error recovery for IDEs**: parse succeeds even with syntax errors:
63//!
64//! ```rust
65//! use toon_lsp::parse_with_errors;
66//!
67//! let source = "config:\n debug: true\n port: 8080";
68//! let (ast, errors) = parse_with_errors(source);
69//!
70//! // Partial AST available for IDE features even with errors
71//! assert!(ast.is_some());
72//!
73//! // Errors carry spans for diagnostic rendering
74//! for err in &errors {
75//! let _ = (err.span.start.line, err.kind.clone());
76//! }
77//! ```
78
79pub mod ast;
80pub mod cli;
81pub mod lsp;
82pub mod parser;
83pub mod resolve;
84pub mod toon;
85
86pub use ast::{AstNode, NumberValue, ObjectEntry, Position, Span};
87pub use parser::{ParseError, ParseErrorKind, parse, parse_with_errors};
88pub use resolve::{ResolveError, ResolvedRef};