leo_parser/lib.rs
1// Copyright (C) 2019-2025 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//! The parser to convert Leo code text into an [`AST`] type.
18//!
19//! This module contains the [`parse_ast()`] method which calls the underlying [`parse()`]
20//! method to create a new program ast.
21
22#![forbid(unsafe_code)]
23#![allow(clippy::vec_init_then_push)]
24#![doc = include_str!("../README.md")]
25
26pub(crate) mod tokenizer;
27pub use tokenizer::KEYWORD_TOKENS;
28pub(crate) use tokenizer::*;
29
30pub mod parser;
31pub use parser::*;
32
33use leo_ast::{Ast, NodeBuilder};
34use leo_errors::{Handler, Result};
35
36use snarkvm::prelude::Network;
37
38#[cfg(test)]
39mod test;
40
41/// Creates a new AST from a given file path and source code text.
42pub fn parse_ast<N: Network>(
43 handler: Handler,
44 node_builder: &NodeBuilder,
45 source: &str,
46 start_pos: u32,
47) -> Result<Ast> {
48 Ok(Ast::new(parse::<N>(handler, node_builder, source, start_pos)?))
49}