shulkerscript-lang/src/lib.rs

116 lines
2.8 KiB
Rust
Raw Normal View History

2024-03-27 19:27:11 +01:00
//! 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
)]
2024-03-27 21:39:56 +01:00
#![warn(missing_docs, clippy::all, clippy::pedantic)]
2024-03-27 19:27:11 +01:00
#![allow(clippy::missing_panics_doc, clippy::missing_const_for_fn)]
pub use shulkerbox;
2024-03-27 19:27:11 +01:00
pub mod base;
pub mod lexical;
2024-03-27 21:39:56 +01:00
pub mod syntax;
pub mod transpile;
2024-03-27 19:27:11 +01:00
mod public_helpers;
2024-04-01 20:42:38 +02:00
use std::{cell::Cell, fmt::Display, path::Path};
2024-03-27 19:27:11 +01:00
use base::{Handler, Result};
use syntax::syntax_tree::program::ProgramFile;
#[cfg(feature = "shulkerbox")]
use shulkerbox::{datapack::Datapack, virtual_fs::VFolder};
2024-04-03 00:45:34 +02:00
use crate::lexical::token_stream::TokenStream;
2024-03-27 19:27:11 +01:00
2024-04-01 20:42:38 +02:00
/// Converts the given source code to tokens.
///
/// # Errors
/// - If an error occurs while reading the file.
pub fn tokenize(path: &Path) -> Result<TokenStream> {
let printer = Printer::new();
public_helpers::tokenize(&printer, path)
2024-04-01 20:42:38 +02:00
}
/// 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(path: &Path) -> Result<ProgramFile> {
2024-04-01 20:42:38 +02:00
let printer = Printer::new();
public_helpers::parse(&printer, path)
2024-04-01 20:42:38 +02:00
}
2024-04-03 00:45:34 +02:00
/// 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.
///
2024-04-03 00:45:34 +02:00
/// # 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<P>(script_paths: &[(String, P)]) -> Result<Datapack>
where
P: AsRef<Path>,
{
2024-04-03 00:45:34 +02:00
let printer = Printer::new();
public_helpers::transpile(&printer, script_paths)
2024-04-03 00:45:34 +02:00
}
2024-03-27 19:27:11 +01:00
/// Compiles the given source code.
///
/// # Parameters:
/// - `script_paths`: A list of tuples containing the identifier of the program and the path to the script.
///
2024-03-27 19:27:11 +01:00
/// # Errors
/// - If an error occurs while reading the file.
2024-04-03 00:45:34 +02:00
/// - If an error occurs while parsing the source code.
/// - If an error occurs while transpiling the source code.
#[cfg(feature = "shulkerbox")]
pub fn compile<P>(script_paths: &[(String, P)]) -> Result<VFolder>
where
P: AsRef<Path>,
{
2024-03-27 19:27:11 +01:00
let printer = Printer::new();
public_helpers::compile(&printer, script_paths)
2024-03-27 19:27:11 +01:00
}
struct Printer {
printed: Cell<bool>,
}
impl Printer {
/// Creates a new [`Printer`].
fn new() -> Self {
Self {
printed: Cell::new(false),
}
}
fn has_printed(&self) -> bool {
self.printed.get()
}
}
impl<E: Display> Handler<E> for Printer {
fn receive(&self, error: E) {
eprintln!("{error}");
self.printed.set(true);
}
}