diff --git a/src/lib.rs b/src/lib.rs index 6c739e6..4f93630 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,29 +19,26 @@ pub mod lexical; pub mod syntax; pub mod transpile; +mod public_helpers; + use std::{cell::Cell, fmt::Display, path::Path}; -use base::{source_file::SourceFile, Handler, Result}; +use base::{Handler, Result}; use syntax::syntax_tree::program::ProgramFile; #[cfg(feature = "shulkerbox")] -use transpile::transpiler::Transpiler; +use shulkerbox::{datapack::Datapack, virtual_fs::VFolder}; -#[cfg(feature = "shulkerbox")] -use shulkerbox::{datapack::Datapack, util::compile::CompileOptions, virtual_fs::VFolder}; - -use crate::{base::Error, lexical::token_stream::TokenStream, syntax::parser::Parser}; +use crate::lexical::token_stream::TokenStream; /// Converts the given source code to tokens. /// /// # Errors /// - If an error occurs while reading the file. pub fn tokenize(path: &Path) -> Result { - let source_file = SourceFile::load(path)?; - let printer = Printer::new(); - Ok(TokenStream::tokenize(&source_file, &printer)) + public_helpers::tokenize(&printer, path) } /// Parses the given source code. @@ -50,34 +47,16 @@ pub fn tokenize(path: &Path) -> Result { /// - If an error occurs while reading the file. /// - If an error occurs while parsing the source code. pub fn parse(path: &Path) -> Result { - let source_file = SourceFile::load(path)?; - let printer = Printer::new(); - let tokens = TokenStream::tokenize(&source_file, &printer); - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while tokenizing the source code.", - )); - } - - let mut parser = Parser::new(&tokens); - let program = parser.parse_program(&printer).ok_or(Error::Other( - "An error occured while parsing the source code.", - ))?; - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while parsing the source code.", - )); - } - - Ok(program) + public_helpers::parse(&printer, path) } /// Transpiles the given source code into a shulkerbox [`Datapack`]. /// +/// # Parameters: +/// - `script_paths`: A list of tuples containing the identifier of the program and the path to the script. +/// /// # Errors /// - If an error occurs while reading the file. /// - If an error occurs while parsing the source code. @@ -89,61 +68,14 @@ where { let printer = Printer::new(); - let programs = script_paths - .iter() - .map(|(program_identifier, path)| { - let source_file = SourceFile::load(path.as_ref())?; - - let tokens = TokenStream::tokenize(&source_file, &printer); - - // println!("tokens: {tokens:#?}"); - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while tokenizing the source code.", - )); - } - - let mut parser = Parser::new(&tokens); - let program = parser.parse_program(&printer).ok_or(Error::Other( - "An error occured while parsing the source code.", - ))?; - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while parsing the source code.", - )); - } - - Ok((program_identifier, program)) - }) - .collect::>(); - - if programs.iter().any(Result::is_err) { - return Err(programs.into_iter().find_map(Result::err).unwrap()); - } - let programs = programs - .into_iter() - .filter_map(Result::ok) - .collect::>(); - - let mut transpiler = Transpiler::new(27); - transpiler.transpile(&programs, &printer)?; - let datapack = transpiler.into_datapack(); - - // println!("datapack: {datapack:#?}"); - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while transpiling the source code.", - )); - } - - Ok(datapack) + public_helpers::transpile(&printer, script_paths) } /// Compiles the given source code. /// +/// # Parameters: +/// - `script_paths`: A list of tuples containing the identifier of the program and the path to the script. +/// /// # Errors /// - If an error occurs while reading the file. /// - If an error occurs while parsing the source code. @@ -155,57 +87,7 @@ where { let printer = Printer::new(); - let programs = script_paths - .iter() - .map(|(program_identifier, path)| { - let source_file = SourceFile::load(path.as_ref())?; - - let tokens = TokenStream::tokenize(&source_file, &printer); - - // println!("tokens: {tokens:#?}"); - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while tokenizing the source code.", - )); - } - - let mut parser = Parser::new(&tokens); - let program = parser.parse_program(&printer).ok_or(Error::Other( - "An error occured while parsing the source code.", - ))?; - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while parsing the source code.", - )); - } - - Ok((program_identifier, program)) - }) - .collect::>(); - - if programs.iter().any(Result::is_err) { - return Err(programs.into_iter().find_map(Result::err).unwrap()); - } - let programs = programs - .into_iter() - .filter_map(Result::ok) - .collect::>(); - - let mut transpiler = Transpiler::new(27); - transpiler.transpile(&programs, &printer)?; - let datapack = transpiler.into_datapack(); - - // println!("datapack: {datapack:#?}"); - - if printer.has_printed() { - return Err(Error::Other( - "An error occurred while transpiling the source code.", - )); - } - - Ok(datapack.compile(&CompileOptions::default())) + public_helpers::compile(&printer, script_paths) } struct Printer { diff --git a/src/public_helpers.rs b/src/public_helpers.rs new file mode 100644 index 0000000..7899d35 --- /dev/null +++ b/src/public_helpers.rs @@ -0,0 +1,92 @@ +use std::path::Path; + +use crate::{ + base::{source_file::SourceFile, Error, Result}, + lexical::token_stream::TokenStream, + syntax::{parser::Parser, syntax_tree::program::ProgramFile}, + Printer, +}; + +#[cfg(feature = "shulkerbox")] +use crate::transpile::transpiler::Transpiler; + +#[cfg(feature = "shulkerbox")] +use shulkerbox::{datapack::Datapack, util::compile::CompileOptions, virtual_fs::VFolder}; + +/// Tokenizes the source code at the given path. +pub fn tokenize(printer: &Printer, path: &Path) -> Result { + let source_file = SourceFile::load(path)?; + + Ok(TokenStream::tokenize(&source_file, printer)) +} + +/// Parses the source code at the given path. +pub fn parse(printer: &Printer, path: &Path) -> Result { + let tokens = tokenize(printer, path)?; + + if printer.has_printed() { + return Err(Error::Other( + "An error occurred while tokenizing the source code.", + )); + } + + let mut parser = Parser::new(&tokens); + let program = parser.parse_program(printer).ok_or(Error::Other( + "An error occured while parsing the source code.", + ))?; + + if printer.has_printed() { + return Err(Error::Other( + "An error occurred while parsing the source code.", + )); + } + + Ok(program) +} + +/// Transpiles the source code at the given paths into a shulkerbox [`Datapack`]. +#[cfg(feature = "shulkerbox")] +pub fn transpile

(printer: &Printer, script_paths: &[(String, P)]) -> Result +where + P: AsRef, +{ + let programs = script_paths + .iter() + .map(|(program_identifier, path)| { + let program = parse(printer, path.as_ref())?; + + Ok((program_identifier, program)) + }) + .collect::>(); + + if programs.iter().any(Result::is_err) { + return Err(programs.into_iter().find_map(Result::err).unwrap()); + } + let programs = programs + .into_iter() + .filter_map(Result::ok) + .collect::>(); + + let mut transpiler = Transpiler::new(27); + transpiler.transpile(&programs, printer)?; + let datapack = transpiler.into_datapack(); + + if printer.has_printed() { + return Err(Error::Other( + "An error occurred while transpiling the source code.", + )); + } + + Ok(datapack) +} + +/// Compiles the source code at the given paths. +#[cfg(feature = "shulkerbox")] +pub fn compile

(printer: &Printer, script_paths: &[(String, P)]) -> Result +where + P: AsRef, +{ + let datapack = transpile(printer, script_paths)?; + + Ok(datapack.compile(&CompileOptions::default())) +}