Code cleanup and refactor.

This commit is contained in:
Stephen Chung
2022-11-28 23:24:22 +08:00
parent 29a397b216
commit 0c85f0c796
26 changed files with 762 additions and 704 deletions

View File

@@ -4,7 +4,7 @@
use crate::ast::Expr;
use crate::func::SendSync;
use crate::parser::ParseResult;
use crate::tokenizer::{is_valid_identifier, Token};
use crate::tokenizer::{is_valid_identifier, Token, NO_TOKEN};
use crate::types::dynamic::Variant;
use crate::{
reify, Dynamic, Engine, EvalContext, Identifier, ImmutableString, LexError, Position,
@@ -166,6 +166,7 @@ impl Deref for Expression<'_> {
type Target = Expr;
#[inline(always)]
#[must_use]
fn deref(&self) -> &Self::Target {
self.0
}
@@ -230,11 +231,11 @@ impl Engine {
continue;
}
let token = Token::lookup_symbol_from_syntax(s).or_else(|| {
let token = Token::lookup_symbol_from_syntax(s).unwrap_or_else(|| {
if Token::is_reserved_keyword(s) {
Some(Token::Reserved(Box::new(s.into())))
Token::Reserved(Box::new(s.into()))
} else {
None
NO_TOKEN
}
});
@@ -255,16 +256,16 @@ impl Engine {
#[cfg(not(feature = "no_float"))]
CUSTOM_SYNTAX_MARKER_FLOAT if !segments.is_empty() => s.into(),
// Standard or reserved keyword/symbol not in first position
_ if !segments.is_empty() && token.is_some() => {
_ if !segments.is_empty() && token != NO_TOKEN => {
// Make it a custom keyword/symbol if it is disabled or reserved
if (self
.disabled_symbols
.as_deref()
.map_or(false, |m| m.contains(s))
|| token.map_or(false, |v| v.is_reserved()))
|| token.is_reserved())
&& !self
.custom_keywords
.as_ref()
.as_deref()
.map_or(false, |m| m.contains_key(s))
{
self.custom_keywords
@@ -275,10 +276,10 @@ impl Engine {
}
// Standard keyword in first position but not disabled
_ if segments.is_empty()
&& token.as_ref().map_or(false, Token::is_standard_keyword)
&& token.is_standard_keyword()
&& !self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(s)) =>
{
return Err(LexError::ImproperSymbol(
@@ -295,12 +296,12 @@ impl Engine {
// Make it a custom keyword/symbol if it is disabled or reserved
if self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(s))
|| (token.map_or(false, |v| v.is_reserved())
|| (token.is_reserved()
&& !self
.custom_keywords
.as_ref()
.as_deref()
.map_or(false, |m| m.contains_key(s)))
{
self.custom_keywords

View File

@@ -461,7 +461,7 @@ impl Module {
|| def
.engine
.custom_keywords
.as_ref()
.as_deref()
.map_or(false, |m| m.contains_key(f.metadata.name.as_str()));
f.write_definition(writer, def, operator)?;

View File

@@ -64,8 +64,8 @@ impl Engine {
let (stream, tokenizer_control) = self.lex_raw(
&scripts,
if has_null {
Some(&|token, _, _| {
Some(if has_null {
&|token, _, _| {
match token {
// `null` => `()`
Token::Reserved(s) if &*s == "null" => Token::Unit,
@@ -86,9 +86,9 @@ impl Engine {
// All others
_ => token,
}
})
}
} else {
Some(&|token, _, _| {
&|token, _, _| {
match token {
Token::Reserved(s) if &*s == "null" => Token::LexError(
LexError::ImproperSymbol("null".to_string(), String::new()).into(),
@@ -97,24 +97,21 @@ impl Engine {
Token::LeftBrace => Token::MapStart,
// Disallowed syntax
t @ (Token::Unit | Token::MapStart) => Token::LexError(
LexError::ImproperSymbol(
t.literal_syntax().to_string(),
"Invalid JSON syntax".to_string(),
)
.into(),
LexError::ImproperSymbol(t.literal_syntax().to_string(), String::new())
.into(),
),
Token::InterpolatedString(..) => Token::LexError(
LexError::ImproperSymbol(
"interpolated string".to_string(),
"Invalid JSON syntax".to_string(),
String::new(),
)
.into(),
),
// All others
_ => token,
}
})
},
}
}),
);
let ast = {

View File

@@ -0,0 +1,70 @@
#![cfg(feature = "unchecked")]
use crate::Engine;
impl Engine {
/// The maximum levels of function calls allowed for a script.
///
/// Always returns [`usize::MAX`] under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_call_levels(&self) -> usize {
usize::MAX
}
/// The maximum number of operations allowed for a script to run (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_operations(&self) -> u64 {
0
}
/// The maximum number of imported [modules][crate::Module] allowed for a script.
///
/// Always returns [`usize::MAX`] under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_modules(&self) -> usize {
usize::MAX
}
/// The depth limit for expressions (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_expr_depth(&self) -> usize {
0
}
/// The depth limit for expressions in functions (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_function_expr_depth(&self) -> usize {
0
}
/// The maximum length of [strings][crate::ImmutableString] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_string_size(&self) -> usize {
0
}
/// The maximum length of [arrays][crate::Array] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_array_size(&self) -> usize {
0
}
/// The maximum size of [object maps][crate::Map] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_map_size(&self) -> usize {
0
}
}

View File

@@ -19,9 +19,21 @@ pub mod options;
pub mod optimize;
pub mod limits;
pub mod limits_unchecked;
pub mod events;
pub mod type_names;
pub mod custom_syntax;
pub mod build_type;
#[cfg(feature = "metadata")]
pub mod definitions;
pub mod deprecated;
use crate::{Dynamic, Engine, Identifier};
#[cfg(feature = "no_std")]
@@ -156,7 +168,7 @@ impl Engine {
Some(token) if token.is_standard_keyword() => {
if !self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(token.literal_syntax()))
{
return Err(format!("'{keyword}' is a reserved keyword"));
@@ -166,7 +178,7 @@ impl Engine {
Some(token) if token.is_standard_symbol() => {
if !self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(token.literal_syntax()))
{
return Err(format!("'{keyword}' is a reserved operator"));
@@ -176,7 +188,7 @@ impl Engine {
Some(token)
if !self
.disabled_symbols
.as_ref()
.as_deref()
.map_or(false, |m| m.contains(token.literal_syntax())) =>
{
return Err(format!("'{keyword}' is a reserved symbol"))
@@ -212,82 +224,3 @@ impl Engine {
self
}
}
#[cfg(feature = "unchecked")]
impl Engine {
/// The maximum levels of function calls allowed for a script.
///
/// Always returns [`usize::MAX`] under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_call_levels(&self) -> usize {
usize::MAX
}
/// The maximum number of operations allowed for a script to run (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_operations(&self) -> u64 {
0
}
/// The maximum number of imported [modules][crate::Module] allowed for a script.
///
/// Always returns [`usize::MAX`] under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_modules(&self) -> usize {
usize::MAX
}
/// The depth limit for expressions (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_expr_depth(&self) -> usize {
0
}
/// The depth limit for expressions in functions (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_function_expr_depth(&self) -> usize {
0
}
/// The maximum length of [strings][crate::ImmutableString] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_string_size(&self) -> usize {
0
}
/// The maximum length of [arrays][crate::Array] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_array_size(&self) -> usize {
0
}
/// The maximum size of [object maps][crate::Map] (0 for unlimited).
///
/// Always returns zero under `unchecked`.
#[inline(always)]
#[must_use]
pub const fn max_map_size(&self) -> usize {
0
}
}
pub mod type_names;
pub mod custom_syntax;
pub mod build_type;
#[cfg(feature = "metadata")]
pub mod definitions;
pub mod deprecated;

View File

@@ -57,7 +57,7 @@ impl Engine {
#[cfg(not(feature = "no_function"))]
ast.shared_lib()
.iter_fn()
.map(|f| f.func.get_script_fn_def().expect("`ScriptFnDef").clone())
.map(|f| f.func.get_script_fn_def().cloned().expect("`ScriptFnDef"))
.collect(),
optimization_level,
);