//! The `ShulkerScript` language. //! //! `ShulkerScript` is a simple, imperative scripting language for creating Minecraft data packs. #![deny( missing_debug_implementations, missing_copy_implementations, clippy::nursery, rustdoc::broken_intra_doc_links, clippy::missing_errors_doc )] #![warn(missing_docs, clippy::all, clippy::pedantic)] #![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)] pub use shulkerbox; pub mod base; pub mod lexical; pub mod syntax; pub mod transpile; mod public_helpers; use std::{cell::Cell, fmt::Display, path::Path}; use base::{FileProvider, Handler, Result}; use syntax::syntax_tree::program::ProgramFile; #[cfg(feature = "shulkerbox")] use shulkerbox::{datapack::Datapack, virtual_fs::VFolder}; 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(file_provider: &F, path: &Path) -> Result where F: FileProvider, { let printer = Printer::new(); public_helpers::tokenize(&printer, file_provider, path) } /// Parses the given source code. /// /// # Errors /// - If an error occurs while reading the file. /// - If an error occurs while parsing the source code. pub fn parse(file_provider: &F, path: &Path) -> Result where F: FileProvider, { let printer = Printer::new(); public_helpers::parse(&printer, file_provider, 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. /// - If an error occurs while transpiling the source code. #[cfg(feature = "shulkerbox")] pub fn transpile( file_provider: &F, pack_format: u8, script_paths: &[(String, P)], ) -> Result where F: FileProvider, P: AsRef, { let printer = Printer::new(); public_helpers::transpile(&printer, file_provider, pack_format, 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. /// - If an error occurs while transpiling the source code. #[cfg(feature = "shulkerbox")] pub fn compile( file_provider: &F, pack_format: u8, script_paths: &[(String, P)], ) -> Result where F: FileProvider, P: AsRef, { let printer = Printer::new(); public_helpers::compile(&printer, file_provider, pack_format, script_paths) } struct Printer { printed: Cell, } impl Printer { /// Creates a new [`Printer`]. fn new() -> Self { Self { printed: Cell::new(false), } } fn has_printed(&self) -> bool { self.printed.get() } } impl Handler for Printer { fn receive>(&self, error: E) { eprintln!("{}", error.into()); self.printed.set(true); } fn has_received(&self) -> bool { self.printed.get() } }