Skip to main content

leo_parser_rowan/
lib.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17//! Rowan-based lossless syntax tree and parser for Leo.
18//!
19//! This crate provides a lossless parser using the rowan library, designed for
20//! IDE-grade error recovery. It will eventually replace the LALRPOP-based
21//! parser in `leo-parser-lossless`.
22
23mod lexer;
24mod parser;
25mod syntax_kind;
26
27use leo_errors::Result;
28pub use lexer::{LexError, Token, lex};
29pub use parser::{
30    Parse,
31    ParseError,
32    Parser,
33    parse_expression_entry,
34    parse_file,
35    parse_module_entry,
36    parse_statement_entry,
37};
38pub use syntax_kind::{SyntaxKind, syntax_kind_from_raw};
39
40/// The Leo language type for rowan.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub enum LeoLanguage {}
43
44impl rowan::Language for LeoLanguage {
45    type Kind = SyntaxKind;
46
47    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
48        syntax_kind_from_raw(raw)
49    }
50
51    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
52        kind.into()
53    }
54}
55
56/// A syntax node in the Leo syntax tree.
57pub type SyntaxNode = rowan::SyntaxNode<LeoLanguage>;
58
59/// A syntax token in the Leo syntax tree.
60pub type SyntaxToken = rowan::SyntaxToken<LeoLanguage>;
61
62/// Either a syntax node or token.
63pub type SyntaxElement = rowan::SyntaxElement<LeoLanguage>;
64
65/// Parse an expression from source code.
66///
67/// # Arguments
68/// * `source` - The source code to parse.
69///
70/// # Returns
71/// A syntax node representing the parsed expression.
72pub fn parse_expression(source: &str) -> Result<SyntaxNode> {
73    let parse = parse_expression_entry(source);
74    // TODO: Convert ParseErrors to leo_errors::Result when needed
75    Ok(parse.syntax())
76}
77
78/// Parse a statement from source code.
79///
80/// # Arguments
81/// * `source` - The source code to parse.
82///
83/// # Returns
84/// A syntax node representing the parsed statement.
85pub fn parse_statement(source: &str) -> Result<SyntaxNode> {
86    let parse = parse_statement_entry(source);
87    // TODO: Convert ParseErrors to leo_errors::Result when needed
88    Ok(parse.syntax())
89}
90
91/// Parse a module from source code.
92///
93/// # Arguments
94/// * `source` - The source code to parse.
95///
96/// # Returns
97/// A syntax node representing the parsed module.
98pub fn parse_module(source: &str) -> Result<SyntaxNode> {
99    let parse = parse_module_entry(source);
100    // TODO: Convert ParseErrors to leo_errors::Result when needed
101    Ok(parse.syntax())
102}
103
104/// Parse a main program file from source code.
105///
106/// # Arguments
107/// * `source` - The source code to parse.
108///
109/// # Returns
110/// A syntax node representing the parsed program.
111pub fn parse_main(source: &str) -> Result<SyntaxNode> {
112    let parse = parse_file(source);
113    // TODO: Convert ParseErrors to leo_errors::Result when needed
114    Ok(parse.syntax())
115}