Add Engine::compact_script.

This commit is contained in:
Stephen Chung
2022-12-21 13:54:54 +08:00
parent d94f362b51
commit 9bf3a9d78f
6 changed files with 123 additions and 10 deletions

View File

@@ -1,6 +1,9 @@
//! Module that provide formatting services to the [`Engine`].
use crate::packages::iter_basic::{BitRange, CharsStream, StepRange};
use crate::parser::{ParseResult, ParseState};
use crate::{
Engine, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, Position, RhaiError, ERR,
Engine, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, OptimizationLevel, Position,
RhaiError, Scope, SmartString, StringsInterner, ERR,
};
use std::any::type_name;
#[cfg(feature = "no_std")]
@@ -263,4 +266,33 @@ impl Engine {
let t = self.map_type_name(type_name::<T>()).into();
ERR::ErrorMismatchDataType(t, typ.into(), pos).into()
}
/// Compact a script to eliminate insignificant whitespaces and comments.
///
/// This is useful to prepare a script for further compressing.
///
/// The output script is semantically identical to the input script, except smaller in size.
///
/// Unlike other uglifiers and minifiers, this method does not rename variables nor perform any
/// optimization on the input script.
#[inline]
pub fn compact_script(&self, script: impl AsRef<str>) -> ParseResult<String> {
let scripts = [script];
let (mut stream, tc) = self.lex_raw(&scripts, self.token_mapper.as_deref());
tc.borrow_mut().compressed = Some(String::new());
stream.state.last_token = Some(SmartString::new_const());
let scope = Scope::new();
let mut interner = StringsInterner::new();
let mut state = ParseState::new(&scope, &mut interner, tc);
let mut _ast = self.parse(
stream.peekable(),
&mut state,
#[cfg(not(feature = "no_optimize"))]
OptimizationLevel::None,
#[cfg(feature = "no_optimize")]
(),
)?;
let tc = state.tokenizer_control.borrow();
Ok(tc.compressed.as_ref().unwrap().into())
}
}

View File

@@ -23,7 +23,7 @@ pub mod limits_unchecked;
pub mod events;
pub mod type_names;
pub mod formatting;
pub mod custom_syntax;