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, NetworkName, NodeBuilder};
34use leo_errors::{Handler, Result};
35use leo_span::source_map::SourceFile;
36
37#[cfg(test)]
38mod test;
39
40/// Creates a new AST from a given file path and source code text.
41pub fn parse_ast(
42 handler: Handler,
43 node_builder: &NodeBuilder,
44 source: &SourceFile,
45 modules: &[std::rc::Rc<SourceFile>],
46 network: NetworkName,
47) -> Result<Ast> {
48 Ok(Ast::new(parse(handler, node_builder, source, modules, network)?))
49}