Reduce size of Engine.
This commit is contained in:
@@ -12,7 +12,7 @@ use crate::{
|
||||
};
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
use std::{borrow::Borrow, ops::Deref};
|
||||
use std::{borrow::Borrow, collections::BTreeMap, ops::Deref};
|
||||
|
||||
/// Collection of special markers for custom syntax definition.
|
||||
pub mod markers {
|
||||
@@ -257,19 +257,29 @@ impl Engine {
|
||||
// Standard or reserved keyword/symbol not in first position
|
||||
_ if !segments.is_empty() && token.is_some() => {
|
||||
// Make it a custom keyword/symbol if it is disabled or reserved
|
||||
if ((!self.disabled_symbols.is_empty() && self.disabled_symbols.contains(s))
|
||||
if (self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(s))
|
||||
|| token.map_or(false, |v| v.is_reserved()))
|
||||
&& (self.custom_keywords.is_empty()
|
||||
|| !self.custom_keywords.contains_key(s))
|
||||
&& !self
|
||||
.custom_keywords
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains_key(s))
|
||||
{
|
||||
self.custom_keywords.insert(s.into(), None);
|
||||
self.custom_keywords
|
||||
.get_or_insert_with(|| BTreeMap::new().into())
|
||||
.insert(s.into(), None);
|
||||
}
|
||||
s.into()
|
||||
}
|
||||
// Standard keyword in first position but not disabled
|
||||
_ if segments.is_empty()
|
||||
&& token.as_ref().map_or(false, Token::is_standard_keyword)
|
||||
&& (self.disabled_symbols.is_empty() || !self.disabled_symbols.contains(s)) =>
|
||||
&& !self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(s)) =>
|
||||
{
|
||||
return Err(LexError::ImproperSymbol(
|
||||
s.to_string(),
|
||||
@@ -283,12 +293,19 @@ impl Engine {
|
||||
// Identifier in first position
|
||||
_ if segments.is_empty() && is_valid_identifier(s) => {
|
||||
// Make it a custom keyword/symbol if it is disabled or reserved
|
||||
if (!self.disabled_symbols.is_empty() && self.disabled_symbols.contains(s))
|
||||
|| token.map_or(false, |v| v.is_reserved())
|
||||
&& self.custom_keywords.is_empty()
|
||||
|| !self.custom_keywords.contains_key(s)
|
||||
if self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(s))
|
||||
|| (token.map_or(false, |v| v.is_reserved())
|
||||
&& !self
|
||||
.custom_keywords
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains_key(s)))
|
||||
{
|
||||
self.custom_keywords.insert(s.into(), None);
|
||||
self.custom_keywords
|
||||
.get_or_insert_with(|| BTreeMap::new().into())
|
||||
.insert(s.into(), None);
|
||||
}
|
||||
s.into()
|
||||
}
|
||||
@@ -373,14 +390,16 @@ impl Engine {
|
||||
scope_may_be_changed: bool,
|
||||
func: impl Fn(&mut EvalContext, &[Expression], &Dynamic) -> RhaiResult + SendSync + 'static,
|
||||
) -> &mut Self {
|
||||
self.custom_syntax.insert(
|
||||
key.into(),
|
||||
CustomSyntax {
|
||||
parse: Box::new(parse),
|
||||
func: Box::new(func),
|
||||
scope_may_be_changed,
|
||||
},
|
||||
);
|
||||
self.custom_syntax
|
||||
.get_or_insert_with(|| BTreeMap::new().into())
|
||||
.insert(
|
||||
key.into(),
|
||||
CustomSyntax {
|
||||
parse: Box::new(parse),
|
||||
func: Box::new(func),
|
||||
scope_may_be_changed,
|
||||
},
|
||||
);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@@ -369,6 +369,7 @@ impl Definitions<'_> {
|
||||
.engine
|
||||
.global_sub_modules
|
||||
.iter()
|
||||
.flat_map(|m| m.iter())
|
||||
.map(move |(name, module)| {
|
||||
(
|
||||
name.to_string(),
|
||||
@@ -445,13 +446,16 @@ impl Module {
|
||||
first = false;
|
||||
|
||||
if f.access != FnAccess::Private {
|
||||
#[cfg(not(feature = "no_custom_syntax"))]
|
||||
let operator = def.engine.custom_keywords.contains_key(f.name.as_str())
|
||||
|| (!f.name.contains('$') && !is_valid_function_name(f.name.as_str()));
|
||||
|
||||
#[cfg(feature = "no_custom_syntax")]
|
||||
let operator = !f.name.contains('$') && !is_valid_function_name(&f.name);
|
||||
|
||||
#[cfg(not(feature = "no_custom_syntax"))]
|
||||
let operator = operator
|
||||
|| def
|
||||
.engine
|
||||
.custom_keywords
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains_key(f.name.as_str()));
|
||||
|
||||
f.write_definition(writer, def, operator)?;
|
||||
}
|
||||
}
|
||||
|
@@ -360,7 +360,7 @@ impl Engine {
|
||||
+ SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
self.debugger = Some((Box::new(init), Box::new(callback)));
|
||||
self.debugger = Some(Box::new((Box::new(init), Box::new(callback))));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@@ -35,6 +35,7 @@ pub mod definitions;
|
||||
|
||||
use crate::{Dynamic, Engine, Identifier};
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
#[cfg(feature = "no_std")]
|
||||
use std::prelude::v1::*;
|
||||
|
||||
@@ -107,7 +108,9 @@ impl Engine {
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn disable_symbol(&mut self, symbol: impl Into<Identifier>) -> &mut Self {
|
||||
self.disabled_symbols.insert(symbol.into());
|
||||
self.disabled_symbols
|
||||
.get_or_insert_with(|| BTreeSet::new().into())
|
||||
.insert(symbol.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -163,18 +166,31 @@ impl Engine {
|
||||
// Active standard keywords cannot be made custom
|
||||
// Disabled keywords are OK
|
||||
Some(token) if token.is_standard_keyword() => {
|
||||
if !self.disabled_symbols.contains(token.literal_syntax()) {
|
||||
if !self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(token.literal_syntax()))
|
||||
{
|
||||
return Err(format!("'{keyword}' is a reserved keyword"));
|
||||
}
|
||||
}
|
||||
// Active standard symbols cannot be made custom
|
||||
Some(token) if token.is_standard_symbol() => {
|
||||
if !self.disabled_symbols.contains(token.literal_syntax()) {
|
||||
if !self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(token.literal_syntax()))
|
||||
{
|
||||
return Err(format!("'{keyword}' is a reserved operator"));
|
||||
}
|
||||
}
|
||||
// Active standard symbols cannot be made custom
|
||||
Some(token) if !self.disabled_symbols.contains(token.literal_syntax()) => {
|
||||
Some(token)
|
||||
if !self
|
||||
.disabled_symbols
|
||||
.as_ref()
|
||||
.map_or(false, |m| m.contains(token.literal_syntax())) =>
|
||||
{
|
||||
return Err(format!("'{keyword}' is a reserved symbol"))
|
||||
}
|
||||
// Disabled symbols are OK
|
||||
@@ -183,6 +199,7 @@ impl Engine {
|
||||
|
||||
// Add to custom keywords
|
||||
self.custom_keywords
|
||||
.get_or_insert_with(|| std::collections::BTreeMap::new().into())
|
||||
.insert(keyword.into(), Some(precedence));
|
||||
|
||||
Ok(self)
|
||||
|
@@ -683,8 +683,10 @@ impl Engine {
|
||||
name: impl AsRef<str>,
|
||||
module: SharedModule,
|
||||
) -> &mut Self {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn register_static_module_raw(
|
||||
root: &mut std::collections::BTreeMap<Identifier, SharedModule>,
|
||||
root: &mut BTreeMap<Identifier, SharedModule>,
|
||||
name: &str,
|
||||
module: SharedModule,
|
||||
) {
|
||||
@@ -717,7 +719,12 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
register_static_module_raw(&mut self.global_sub_modules, name.as_ref(), module);
|
||||
register_static_module_raw(
|
||||
self.global_sub_modules
|
||||
.get_or_insert_with(|| BTreeMap::new().into()),
|
||||
name.as_ref(),
|
||||
module,
|
||||
);
|
||||
self
|
||||
}
|
||||
/// _(metadata)_ Generate a list of all registered functions.
|
||||
@@ -737,7 +744,7 @@ impl Engine {
|
||||
signatures.extend(self.global_namespace().gen_fn_signatures());
|
||||
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
for (name, m) in &self.global_sub_modules {
|
||||
for (name, m) in self.global_sub_modules.iter().flat_map(|m| m.iter()) {
|
||||
signatures.extend(m.gen_fn_signatures().map(|f| format!("{name}::{f}")));
|
||||
}
|
||||
|
||||
|
@@ -205,6 +205,7 @@ impl Engine {
|
||||
return self
|
||||
.global_sub_modules
|
||||
.iter()
|
||||
.flat_map(|m| m.iter())
|
||||
.find_map(|(_, m)| m.get_custom_type(name));
|
||||
#[cfg(feature = "no_module")]
|
||||
return None;
|
||||
@@ -238,6 +239,7 @@ impl Engine {
|
||||
return self
|
||||
.global_sub_modules
|
||||
.iter()
|
||||
.flat_map(|m| m.iter())
|
||||
.find_map(|(_, m)| m.get_custom_type(name));
|
||||
#[cfg(feature = "no_module")]
|
||||
return None;
|
||||
|
Reference in New Issue
Block a user