Skip to main content

oak_rbq/
lib.rs

1#![doc = include_str!("readme.md")]
2#![feature(new_range_api)]
3#![warn(missing_docs)]
4#![doc(html_logo_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
5#![doc(html_favicon_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
6
7/// AST module containing node definitions for the RBQ language.
8pub mod ast;
9/// Builder module for constructing RBQ trees.
10pub mod builder;
11
12/// Language configuration and syntax kind definitions.
13pub mod language;
14/// Lexer implementation for RBQ.
15pub mod lexer;
16/// LSP-related functionality (hover, completion, highlighting).
17#[cfg(feature = "lsp")]
18pub mod lsp;
19/// MCP (Model Context Protocol) integration for RBQ.
20#[cfg(feature = "mcp")]
21pub mod mcp;
22
23/// Parser implementation for RBQ.
24pub mod parser;
25
26pub use crate::{
27    ast::RbqRoot,
28    builder::RbqBuilder,
29    language::RbqLanguage,
30    lexer::{RbqLexer, token_type::RbqTokenType},
31    parser::{RbqParser, element_type::RbqElementType},
32};
33
34/// Alias for RbqTokenType to support tests and common usage
35pub type RbqSyntaxKind = RbqTokenType;
36
37/// Highlighter implementation.
38#[cfg(feature = "oak-highlight")]
39pub use crate::lsp::highlighter::RbqHighlighter;
40
41/// Formatter implementation.
42#[cfg(feature = "lsp")]
43pub use crate::lsp::formatter::RbqFormatter;
44
45/// LSP implementation.
46#[cfg(feature = "lsp")]
47pub use crate::lsp::RbqLanguageService;
48
49/// MCP implementation.
50#[cfg(feature = "mcp")]
51pub use crate::mcp::serve_rbq_mcp;
52
53/// Parses a string into an RBQ AST.
54pub fn parse(input: &str) -> Result<RbqRoot, oak_core::OakError> {
55    use oak_core::{ParseSession, Parser, tree::RedTree};
56
57    // Create language configuration
58    let language = RbqLanguage::new();
59
60    // Create parser
61    let parser = RbqParser::new(&language);
62
63    // Create parse session
64    let mut session = ParseSession::new(16);
65
66    // Parse the input
67    let output = parser.parse(input, &[], &mut session);
68
69    // Check for errors
70    if let Err(err) = output.result {
71        return Err(err);
72    }
73
74    // Get the parse tree
75    let tree = output.result.unwrap();
76
77    // Convert the green tree to red tree
78    let red_tree = RedTree::new(&tree);
79
80    // Get the red node from the red tree
81    let red_node = match red_tree.as_node() {
82        Some(node) => node,
83        None => return Err(oak_core::OakError::custom_error("Root node not found")),
84    };
85
86    // Convert the red tree to AST
87    let ast = RbqRoot::lower(red_node, input);
88
89    Ok(ast)
90}