//! Implements `Parser` for Erg. `Parser` parses the source code to generate `AST`. //! The generated `AST`s are guaranteed to be identical if the source code is identical. //! However, identical `AST`s may be generated even if the source code is (a bit) different. #![allow(clippy::large_enum_variant)] extern crate erg_common; pub mod ast; pub mod build_ast; pub mod convert; pub mod desugar; pub mod error; pub mod lex; pub mod parse; pub mod token; pub mod typespec; pub mod visitor; pub use parse::{Parser, ParserRunner}; pub use visitor::ASTVisitor; #[cfg(feature = "pylib")] use pyo3::prelude::*; /// parse(code: str) -> erg_parser.Module /// -- /// /// parse an Erg code as a module at runtime #[cfg(feature = "pylib")] #[pyfunction] #[pyo3(name = "parse")] fn _parse(code: String) -> Result { parse::SimpleParser::parse(code) .map(|art| art.ast) .map_err(|iart| iart.errors) } #[cfg(feature = "pylib")] #[cfg_attr(feature = "pylib_parser", pymodule)] pub fn erg_parser(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_function(wrap_pyfunction!(_parse, m)?)?; let expr = PyModule::new_bound(py, "expr")?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; expr.add_class::()?; m.add_submodule(&expr)?; let ast = PyModule::new_bound(py, "ast")?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; ast.add_class::()?; m.add_submodule(&ast)?; py.run_bound( "\ import sys sys.modules['erg_parser.ast'] = ast sys.modules['erg_parser.expr'] = expr ", None, Some(&m.dict()), )?; Ok(()) }