Merge branch 'master' into plugins
This commit is contained in:
54
src/api.rs
54
src/api.rs
@@ -7,7 +7,7 @@ use crate::fn_call::FuncArgs;
|
||||
use crate::fn_native::{IteratorFn, SendSync};
|
||||
use crate::fn_register::RegisterFn;
|
||||
use crate::optimize::{optimize_into_ast, OptimizationLevel};
|
||||
use crate::parser::{parse, parse_global_expr, AST};
|
||||
use crate::parser::AST;
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::scope::Scope;
|
||||
use crate::token::{lex, Position};
|
||||
@@ -449,14 +449,7 @@ impl Engine {
|
||||
optimization_level: OptimizationLevel,
|
||||
) -> Result<AST, ParseError> {
|
||||
let stream = lex(scripts);
|
||||
|
||||
parse(
|
||||
&mut stream.peekable(),
|
||||
self,
|
||||
scope,
|
||||
optimization_level,
|
||||
(self.max_expr_depth, self.max_function_expr_depth),
|
||||
)
|
||||
self.parse(&mut stream.peekable(), scope, optimization_level)
|
||||
}
|
||||
|
||||
/// Read the contents of a file into a string.
|
||||
@@ -578,13 +571,8 @@ impl Engine {
|
||||
// Trims the JSON string and add a '#' in front
|
||||
let scripts = ["#", json.trim()];
|
||||
let stream = lex(&scripts);
|
||||
let ast = parse_global_expr(
|
||||
&mut stream.peekable(),
|
||||
self,
|
||||
&scope,
|
||||
OptimizationLevel::None,
|
||||
self.max_expr_depth,
|
||||
)?;
|
||||
let ast =
|
||||
self.parse_global_expr(&mut stream.peekable(), &scope, OptimizationLevel::None)?;
|
||||
|
||||
// Handle null - map to ()
|
||||
if has_null {
|
||||
@@ -667,13 +655,7 @@ impl Engine {
|
||||
|
||||
{
|
||||
let mut peekable = stream.peekable();
|
||||
parse_global_expr(
|
||||
&mut peekable,
|
||||
self,
|
||||
scope,
|
||||
self.optimization_level,
|
||||
self.max_expr_depth,
|
||||
)
|
||||
self.parse_global_expr(&mut peekable, scope, self.optimization_level)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -825,12 +807,10 @@ impl Engine {
|
||||
let scripts = [script];
|
||||
let stream = lex(&scripts);
|
||||
|
||||
let ast = parse_global_expr(
|
||||
let ast = self.parse_global_expr(
|
||||
&mut stream.peekable(),
|
||||
self,
|
||||
scope,
|
||||
OptimizationLevel::None, // No need to optimize a lone expression
|
||||
self.max_expr_depth,
|
||||
)?;
|
||||
|
||||
self.eval_ast_with_scope(scope, &ast)
|
||||
@@ -957,13 +937,7 @@ impl Engine {
|
||||
let scripts = [script];
|
||||
let stream = lex(&scripts);
|
||||
|
||||
let ast = parse(
|
||||
&mut stream.peekable(),
|
||||
self,
|
||||
scope,
|
||||
self.optimization_level,
|
||||
(self.max_expr_depth, self.max_function_expr_depth),
|
||||
)?;
|
||||
let ast = self.parse(&mut stream.peekable(), scope, self.optimization_level)?;
|
||||
self.consume_ast_with_scope(scope, &ast)
|
||||
}
|
||||
|
||||
@@ -1104,16 +1078,20 @@ impl Engine {
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let mut args: StaticVec<_> = arg_values.iter_mut().collect();
|
||||
let lib = ast.lib();
|
||||
let pos = Position::none();
|
||||
|
||||
let fn_def = lib
|
||||
.get_function_by_signature(name, args.len(), true)
|
||||
.ok_or_else(|| Box::new(EvalAltResult::ErrorFunctionNotFound(name.into(), pos)))?;
|
||||
.ok_or_else(|| {
|
||||
Box::new(EvalAltResult::ErrorFunctionNotFound(
|
||||
name.into(),
|
||||
Position::none(),
|
||||
))
|
||||
})?;
|
||||
|
||||
let mut state = State::new();
|
||||
let args = args.as_mut();
|
||||
|
||||
self.call_script_fn(scope, &mut state, &lib, name, fn_def, args, pos, 0)
|
||||
self.call_script_fn(scope, &mut state, &lib, name, fn_def, args, 0)
|
||||
}
|
||||
|
||||
/// Optimize the `AST` with constants defined in an external Scope.
|
||||
@@ -1159,7 +1137,7 @@ impl Engine {
|
||||
///
|
||||
/// let mut engine = Engine::new();
|
||||
///
|
||||
/// engine.on_progress(move |ops| {
|
||||
/// engine.on_progress(move |&ops| {
|
||||
/// if ops > 10000 {
|
||||
/// false
|
||||
/// } else if ops % 800 == 0 {
|
||||
@@ -1178,7 +1156,7 @@ impl Engine {
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn on_progress(&mut self, callback: impl Fn(u64) -> bool + SendSync + 'static) {
|
||||
pub fn on_progress(&mut self, callback: impl Fn(&u64) -> bool + SendSync + 'static) {
|
||||
self.progress = Some(Box::new(callback));
|
||||
}
|
||||
|
||||
|
272
src/engine.rs
272
src/engine.rs
@@ -3,7 +3,7 @@
|
||||
use crate::any::{Dynamic, Union};
|
||||
use crate::calc_fn_hash;
|
||||
use crate::error::ParseErrorType;
|
||||
use crate::fn_native::{CallableFunction, FnCallArgs, Shared};
|
||||
use crate::fn_native::{CallableFunction, Callback, FnCallArgs, Shared};
|
||||
use crate::module::{resolvers, Module, ModuleResolver};
|
||||
use crate::optimize::OptimizationLevel;
|
||||
use crate::packages::{CorePackage, Package, PackageLibrary, PackagesCollection, StandardPackage};
|
||||
@@ -116,17 +116,20 @@ impl Target<'_> {
|
||||
}
|
||||
|
||||
/// Update the value of the `Target`.
|
||||
pub fn set_value(&mut self, new_val: Dynamic, pos: Position) -> Result<(), Box<EvalAltResult>> {
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
pub fn set_value(&mut self, new_val: Dynamic) -> Result<(), Box<EvalAltResult>> {
|
||||
match self {
|
||||
Target::Ref(r) => **r = new_val,
|
||||
Target::Value(_) => {
|
||||
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(pos)))
|
||||
return Err(Box::new(EvalAltResult::ErrorAssignmentToUnknownLHS(
|
||||
Position::none(),
|
||||
)))
|
||||
}
|
||||
Target::StringChar(Dynamic(Union::Str(ref mut s)), index, _) => {
|
||||
// Replace the character at the specified index position
|
||||
let new_ch = new_val
|
||||
.as_char()
|
||||
.map_err(|_| EvalAltResult::ErrorCharMismatch(pos))?;
|
||||
.map_err(|_| EvalAltResult::ErrorCharMismatch(Position::none()))?;
|
||||
|
||||
let mut chars: StaticVec<char> = s.chars().collect();
|
||||
let ch = chars[*index];
|
||||
@@ -299,26 +302,12 @@ pub struct Engine {
|
||||
/// A hashmap mapping type names to pretty-print names.
|
||||
pub(crate) type_names: HashMap<String, String>,
|
||||
|
||||
/// Closure for implementing the `print` command.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub(crate) print: Box<dyn Fn(&str) + 'static>,
|
||||
/// Closure for implementing the `print` command.
|
||||
#[cfg(feature = "sync")]
|
||||
pub(crate) print: Box<dyn Fn(&str) + Send + Sync + 'static>,
|
||||
|
||||
/// Closure for implementing the `debug` command.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub(crate) debug: Box<dyn Fn(&str) + 'static>,
|
||||
/// Closure for implementing the `debug` command.
|
||||
#[cfg(feature = "sync")]
|
||||
pub(crate) debug: Box<dyn Fn(&str) + Send + Sync + 'static>,
|
||||
|
||||
/// Closure for progress reporting.
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub(crate) progress: Option<Box<dyn Fn(u64) -> bool + 'static>>,
|
||||
/// Closure for progress reporting.
|
||||
#[cfg(feature = "sync")]
|
||||
pub(crate) progress: Option<Box<dyn Fn(u64) -> bool + Send + Sync + 'static>>,
|
||||
/// Callback closure for implementing the `print` command.
|
||||
pub(crate) print: Callback<str, ()>,
|
||||
/// Callback closure for implementing the `debug` command.
|
||||
pub(crate) debug: Callback<str, ()>,
|
||||
/// Callback closure for progress reporting.
|
||||
pub(crate) progress: Option<Callback<u64, bool>>,
|
||||
|
||||
/// Optimize the AST after compilation.
|
||||
pub(crate) optimization_level: OptimizationLevel,
|
||||
@@ -575,6 +564,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Universal method for calling functions either registered with the `Engine` or written in Rhai.
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
///
|
||||
/// ## WARNING
|
||||
///
|
||||
@@ -591,10 +581,9 @@ impl Engine {
|
||||
args: &mut FnCallArgs,
|
||||
is_ref: bool,
|
||||
def_val: Option<&Dynamic>,
|
||||
pos: Position,
|
||||
level: usize,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
self.inc_operations(state, pos)?;
|
||||
self.inc_operations(state)?;
|
||||
|
||||
let native_only = hashes.1 == 0;
|
||||
|
||||
@@ -603,7 +592,9 @@ impl Engine {
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
{
|
||||
if level > self.max_call_stack_depth {
|
||||
return Err(Box::new(EvalAltResult::ErrorStackOverflow(pos)));
|
||||
return Err(Box::new(
|
||||
EvalAltResult::ErrorStackOverflow(Position::none()),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -650,7 +641,7 @@ impl Engine {
|
||||
|
||||
// Run scripted function
|
||||
let result =
|
||||
self.call_script_fn(scope, state, lib, fn_name, fn_def, args, pos, level)?;
|
||||
self.call_script_fn(scope, state, lib, fn_name, fn_def, args, level)?;
|
||||
|
||||
// Restore the original reference
|
||||
restore_first_arg(old_this_ptr, args);
|
||||
@@ -674,20 +665,18 @@ impl Engine {
|
||||
);
|
||||
|
||||
// Run external function
|
||||
let result = func.get_native_fn()(args);
|
||||
let result = func.get_native_fn()(args)?;
|
||||
|
||||
// Restore the original reference
|
||||
restore_first_arg(old_this_ptr, args);
|
||||
|
||||
let result = result.map_err(|err| err.new_position(pos))?;
|
||||
|
||||
// See if the function match print/debug (which requires special processing)
|
||||
return Ok(match fn_name {
|
||||
KEYWORD_PRINT => (
|
||||
(self.print)(result.as_str().map_err(|type_name| {
|
||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||
type_name.into(),
|
||||
pos,
|
||||
Position::none(),
|
||||
))
|
||||
})?)
|
||||
.into(),
|
||||
@@ -697,7 +686,7 @@ impl Engine {
|
||||
(self.debug)(result.as_str().map_err(|type_name| {
|
||||
Box::new(EvalAltResult::ErrorMismatchOutputType(
|
||||
type_name.into(),
|
||||
pos,
|
||||
Position::none(),
|
||||
))
|
||||
})?)
|
||||
.into(),
|
||||
@@ -724,7 +713,7 @@ impl Engine {
|
||||
if let Some(prop) = extract_prop_from_getter(fn_name) {
|
||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||
format!("- property '{}' unknown or write-only", prop),
|
||||
pos,
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -732,7 +721,7 @@ impl Engine {
|
||||
if let Some(prop) = extract_prop_from_setter(fn_name) {
|
||||
return Err(Box::new(EvalAltResult::ErrorDotExpr(
|
||||
format!("- property '{}' unknown or read-only", prop),
|
||||
pos,
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -745,18 +734,19 @@ impl Engine {
|
||||
if fn_name == FUNC_INDEXER {
|
||||
return Err(Box::new(EvalAltResult::ErrorFunctionNotFound(
|
||||
format!("[]({})", types_list.join(", ")),
|
||||
pos,
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
|
||||
// Raise error
|
||||
Err(Box::new(EvalAltResult::ErrorFunctionNotFound(
|
||||
format!("{} ({})", fn_name, types_list.join(", ")),
|
||||
pos,
|
||||
Position::none(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// Call a script-defined function.
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
///
|
||||
/// ## WARNING
|
||||
///
|
||||
@@ -771,7 +761,6 @@ impl Engine {
|
||||
fn_name: &str,
|
||||
fn_def: &FnDef,
|
||||
args: &mut FnCallArgs,
|
||||
pos: Position,
|
||||
level: usize,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let orig_scope_level = state.scope_level;
|
||||
@@ -798,13 +787,17 @@ impl Engine {
|
||||
.or_else(|err| match *err {
|
||||
// Convert return statement to return value
|
||||
EvalAltResult::Return(x, _) => Ok(x),
|
||||
EvalAltResult::ErrorInFunctionCall(name, err, _) => Err(Box::new(
|
||||
EvalAltResult::ErrorInFunctionCall(format!("{} > {}", fn_name, name), err, pos),
|
||||
)),
|
||||
EvalAltResult::ErrorInFunctionCall(name, err, _) => {
|
||||
Err(Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
format!("{} > {}", fn_name, name),
|
||||
err,
|
||||
Position::none(),
|
||||
)))
|
||||
}
|
||||
_ => Err(Box::new(EvalAltResult::ErrorInFunctionCall(
|
||||
fn_name.to_string(),
|
||||
err,
|
||||
pos,
|
||||
Position::none(),
|
||||
))),
|
||||
});
|
||||
|
||||
@@ -825,7 +818,8 @@ impl Engine {
|
||||
|| lib.contains_key(&hashes.1)
|
||||
}
|
||||
|
||||
// Perform an actual function call, taking care of special functions
|
||||
/// Perform an actual function call, taking care of special functions
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
///
|
||||
/// ## WARNING
|
||||
///
|
||||
@@ -842,7 +836,6 @@ impl Engine {
|
||||
args: &mut FnCallArgs,
|
||||
is_ref: bool,
|
||||
def_val: Option<&Dynamic>,
|
||||
pos: Position,
|
||||
level: usize,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
|
||||
@@ -865,7 +858,7 @@ impl Engine {
|
||||
KEYWORD_EVAL if args.len() == 1 && !self.has_override(lib, hashes) => {
|
||||
Err(Box::new(EvalAltResult::ErrorRuntime(
|
||||
"'eval' should not be called in method style. Try eval(...);".into(),
|
||||
pos,
|
||||
Position::none(),
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -873,24 +866,24 @@ impl Engine {
|
||||
_ => {
|
||||
let mut scope = Scope::new();
|
||||
self.call_fn_raw(
|
||||
&mut scope, state, lib, fn_name, hashes, args, is_ref, def_val, pos, level,
|
||||
&mut scope, state, lib, fn_name, hashes, args, is_ref, def_val, level,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate a text string as a script - used primarily for 'eval'.
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
fn eval_script_expr(
|
||||
&self,
|
||||
scope: &mut Scope,
|
||||
state: &mut State,
|
||||
lib: &FunctionsLib,
|
||||
script: &Dynamic,
|
||||
pos: Position,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let script = script
|
||||
.as_str()
|
||||
.map_err(|type_name| EvalAltResult::ErrorMismatchOutputType(type_name.into(), pos))?;
|
||||
let script = script.as_str().map_err(|type_name| {
|
||||
EvalAltResult::ErrorMismatchOutputType(type_name.into(), Position::none())
|
||||
})?;
|
||||
|
||||
// Compile the script text
|
||||
// No optimizations because we only run it once
|
||||
@@ -903,7 +896,7 @@ impl Engine {
|
||||
// If new functions are defined within the eval string, it is an error
|
||||
if !ast.lib().is_empty() {
|
||||
return Err(Box::new(EvalAltResult::ErrorParsing(
|
||||
ParseErrorType::WrongFnDefinition.into_err(pos),
|
||||
ParseErrorType::WrongFnDefinition.into_err(Position::none()),
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -911,17 +904,16 @@ impl Engine {
|
||||
let ast = AST::new(statements, lib.clone());
|
||||
|
||||
// Evaluate the AST
|
||||
let (result, operations) = self
|
||||
.eval_ast_with_scope_raw(scope, &ast)
|
||||
.map_err(|err| err.new_position(pos))?;
|
||||
let (result, operations) = self.eval_ast_with_scope_raw(scope, &ast)?;
|
||||
|
||||
state.operations += operations;
|
||||
self.inc_operations(state, pos)?;
|
||||
self.inc_operations(state)?;
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// Chain-evaluate a dot/index chain.
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
fn eval_dot_index_chain_helper(
|
||||
&self,
|
||||
state: &mut State,
|
||||
@@ -930,7 +922,6 @@ impl Engine {
|
||||
rhs: &Expr,
|
||||
idx_values: &mut StaticVec<Dynamic>,
|
||||
is_index: bool,
|
||||
op_pos: Position,
|
||||
level: usize,
|
||||
mut new_val: Option<Dynamic>,
|
||||
) -> Result<(Dynamic, bool), Box<EvalAltResult>> {
|
||||
@@ -951,25 +942,27 @@ impl Engine {
|
||||
let (idx, expr, pos) = x.as_ref();
|
||||
let is_idx = matches!(rhs, Expr::Index(_));
|
||||
let idx_pos = idx.position();
|
||||
let this_ptr = &mut self.get_indexed_mut(
|
||||
state, lib, obj, is_ref, idx_val, idx_pos, op_pos, false,
|
||||
)?;
|
||||
let this_ptr = &mut self
|
||||
.get_indexed_mut(state, lib, obj, is_ref, idx_val, idx_pos, false)?;
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
state, lib, this_ptr, expr, idx_values, is_idx, *pos, level, new_val,
|
||||
state, lib, this_ptr, expr, idx_values, is_idx, level, new_val,
|
||||
)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
// xxx[rhs] = new_val
|
||||
_ if new_val.is_some() => {
|
||||
let this_ptr = &mut self
|
||||
.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, true)?;
|
||||
let this_ptr =
|
||||
&mut self.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, true)?;
|
||||
|
||||
this_ptr.set_value(new_val.unwrap(), rhs.position())?;
|
||||
this_ptr
|
||||
.set_value(new_val.unwrap())
|
||||
.map_err(|err| EvalAltResult::new_position(err, rhs.position()))?;
|
||||
Ok((Default::default(), true))
|
||||
}
|
||||
// xxx[rhs]
|
||||
_ => self
|
||||
.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, op_pos, false)
|
||||
.get_indexed_mut(state, lib, obj, is_ref, idx_val, pos, false)
|
||||
.map(|v| (v.clone_into_dynamic(), false)),
|
||||
}
|
||||
} else {
|
||||
@@ -989,9 +982,8 @@ impl Engine {
|
||||
.collect();
|
||||
let args = arg_values.as_mut();
|
||||
|
||||
self.exec_fn_call(
|
||||
state, lib, name, *native, *hash, args, is_ref, def_val, *pos, 0,
|
||||
)
|
||||
self.exec_fn_call(state, lib, name, *native, *hash, args, is_ref, def_val, 0)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
// xxx.module::fn_name(...) - syntax error
|
||||
Expr::FnCall(_) => unreachable!(),
|
||||
@@ -1001,9 +993,10 @@ impl Engine {
|
||||
let ((prop, _, _), pos) = x.as_ref();
|
||||
let index = prop.clone().into();
|
||||
let mut val =
|
||||
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, true)?;
|
||||
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, true)?;
|
||||
|
||||
val.set_value(new_val.unwrap(), rhs.position())?;
|
||||
val.set_value(new_val.unwrap())
|
||||
.map_err(|err| EvalAltResult::new_position(err, rhs.position()))?;
|
||||
Ok((Default::default(), true))
|
||||
}
|
||||
// {xxx:map}.id
|
||||
@@ -1011,8 +1004,7 @@ impl Engine {
|
||||
Expr::Property(x) if obj.is::<Map>() => {
|
||||
let ((prop, _, _), pos) = x.as_ref();
|
||||
let index = prop.clone().into();
|
||||
let val =
|
||||
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, false)?;
|
||||
let val = self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, false)?;
|
||||
|
||||
Ok((val.clone_into_dynamic(), false))
|
||||
}
|
||||
@@ -1020,19 +1012,17 @@ impl Engine {
|
||||
Expr::Property(x) if new_val.is_some() => {
|
||||
let ((_, _, setter), pos) = x.as_ref();
|
||||
let mut args = [obj, new_val.as_mut().unwrap()];
|
||||
self.exec_fn_call(
|
||||
state, lib, setter, true, 0, &mut args, is_ref, None, *pos, 0,
|
||||
)
|
||||
.map(|(v, _)| (v, true))
|
||||
self.exec_fn_call(state, lib, setter, true, 0, &mut args, is_ref, None, 0)
|
||||
.map(|(v, _)| (v, true))
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
// xxx.id
|
||||
Expr::Property(x) => {
|
||||
let ((_, getter, _), pos) = x.as_ref();
|
||||
let mut args = [obj];
|
||||
self.exec_fn_call(
|
||||
state, lib, getter, true, 0, &mut args, is_ref, None, *pos, 0,
|
||||
)
|
||||
.map(|(v, _)| (v, false))
|
||||
self.exec_fn_call(state, lib, getter, true, 0, &mut args, is_ref, None, 0)
|
||||
.map(|(v, _)| (v, false))
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
#[cfg(not(feature = "no_object"))]
|
||||
// {xxx:map}.prop[expr] | {xxx:map}.prop.expr
|
||||
@@ -1043,14 +1033,15 @@ impl Engine {
|
||||
let mut val = if let Expr::Property(p) = prop {
|
||||
let ((prop, _, _), _) = p.as_ref();
|
||||
let index = prop.clone().into();
|
||||
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, op_pos, false)?
|
||||
self.get_indexed_mut(state, lib, obj, is_ref, index, *pos, false)?
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
|
||||
self.eval_dot_index_chain_helper(
|
||||
state, lib, &mut val, expr, idx_values, is_idx, *pos, level, new_val,
|
||||
state, lib, &mut val, expr, idx_values, is_idx, level, new_val,
|
||||
)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
// xxx.prop[expr] | xxx.prop.expr
|
||||
Expr::Index(x) | Expr::Dot(x) => {
|
||||
@@ -1061,16 +1052,19 @@ impl Engine {
|
||||
let (mut val, updated) = if let Expr::Property(p) = prop {
|
||||
let ((_, getter, _), _) = p.as_ref();
|
||||
let args = &mut args[..1];
|
||||
self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, *pos, 0)?
|
||||
self.exec_fn_call(state, lib, getter, true, 0, args, is_ref, None, 0)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))?
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
let val = &mut val;
|
||||
let target = &mut val.into();
|
||||
|
||||
let (result, may_be_changed) = self.eval_dot_index_chain_helper(
|
||||
state, lib, target, expr, idx_values, is_idx, *pos, level, new_val,
|
||||
)?;
|
||||
let (result, may_be_changed) = self
|
||||
.eval_dot_index_chain_helper(
|
||||
state, lib, target, expr, idx_values, is_idx, level, new_val,
|
||||
)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))?;
|
||||
|
||||
// Feed the value back via a setter just in case it has been updated
|
||||
if updated || may_be_changed {
|
||||
@@ -1078,14 +1072,12 @@ impl Engine {
|
||||
let ((_, _, setter), _) = p.as_ref();
|
||||
// Re-use args because the first &mut parameter will not be consumed
|
||||
args[1] = val;
|
||||
self.exec_fn_call(
|
||||
state, lib, setter, true, 0, args, is_ref, None, *pos, 0,
|
||||
)
|
||||
.or_else(|err| match *err {
|
||||
// If there is no setter, no need to feed it back because the property is read-only
|
||||
EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()),
|
||||
err => Err(Box::new(err)),
|
||||
})?;
|
||||
self.exec_fn_call(state, lib, setter, true, 0, args, is_ref, None, 0)
|
||||
.or_else(|err| match *err {
|
||||
// If there is no setter, no need to feed it back because the property is read-only
|
||||
EvalAltResult::ErrorDotExpr(_, _) => Ok(Default::default()),
|
||||
err => Err(EvalAltResult::new_position(Box::new(err), *pos)),
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1124,7 +1116,8 @@ impl Engine {
|
||||
// id.??? or id[???]
|
||||
Expr::Variable(_) => {
|
||||
let (target, name, typ, pos) = search_scope(scope, state, dot_lhs)?;
|
||||
self.inc_operations(state, pos)?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, pos))?;
|
||||
|
||||
// Constants cannot be modified
|
||||
match typ {
|
||||
@@ -1140,9 +1133,10 @@ impl Engine {
|
||||
|
||||
let this_ptr = &mut target.into();
|
||||
self.eval_dot_index_chain_helper(
|
||||
state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val,
|
||||
state, lib, this_ptr, dot_rhs, idx_values, is_index, level, new_val,
|
||||
)
|
||||
.map(|(v, _)| v)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *op_pos))
|
||||
}
|
||||
// {expr}.??? = ??? or {expr}[???] = ???
|
||||
expr if new_val.is_some() => {
|
||||
@@ -1155,9 +1149,10 @@ impl Engine {
|
||||
let val = self.eval_expr(scope, state, lib, expr, level)?;
|
||||
let this_ptr = &mut val.into();
|
||||
self.eval_dot_index_chain_helper(
|
||||
state, lib, this_ptr, dot_rhs, idx_values, is_index, *op_pos, level, new_val,
|
||||
state, lib, this_ptr, dot_rhs, idx_values, is_index, level, new_val,
|
||||
)
|
||||
.map(|(v, _)| v)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *op_pos))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1177,7 +1172,8 @@ impl Engine {
|
||||
size: usize,
|
||||
level: usize,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
self.inc_operations(state, expr.position())?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, expr.position()))?;
|
||||
|
||||
match expr {
|
||||
Expr::FnCall(x) if x.1.is_none() => {
|
||||
@@ -1211,6 +1207,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Get the value at the indexed position of a base type
|
||||
/// Position in `EvalAltResult` may be None and should be set afterwards.
|
||||
fn get_indexed_mut<'a>(
|
||||
&self,
|
||||
state: &mut State,
|
||||
@@ -1219,10 +1216,9 @@ impl Engine {
|
||||
is_ref: bool,
|
||||
mut idx: Dynamic,
|
||||
idx_pos: Position,
|
||||
op_pos: Position,
|
||||
create: bool,
|
||||
) -> Result<Target<'a>, Box<EvalAltResult>> {
|
||||
self.inc_operations(state, op_pos)?;
|
||||
self.inc_operations(state)?;
|
||||
|
||||
match val {
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
@@ -1292,10 +1288,13 @@ impl Engine {
|
||||
let fn_name = FUNC_INDEXER;
|
||||
let type_name = self.map_type_name(val.type_name());
|
||||
let args = &mut [val, &mut idx];
|
||||
self.exec_fn_call(state, lib, fn_name, true, 0, args, is_ref, None, op_pos, 0)
|
||||
self.exec_fn_call(state, lib, fn_name, true, 0, args, is_ref, None, 0)
|
||||
.map(|(v, _)| v.into())
|
||||
.map_err(|_| {
|
||||
Box::new(EvalAltResult::ErrorIndexingType(type_name.into(), op_pos))
|
||||
Box::new(EvalAltResult::ErrorIndexingType(
|
||||
type_name.into(),
|
||||
Position::none(),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1311,7 +1310,8 @@ impl Engine {
|
||||
rhs: &Expr,
|
||||
level: usize,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
self.inc_operations(state, rhs.position())?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, rhs.position()))?;
|
||||
|
||||
let lhs_value = self.eval_expr(scope, state, lib, lhs, level)?;
|
||||
let rhs_value = self.eval_expr(scope, state, lib, rhs, level)?;
|
||||
@@ -1327,7 +1327,6 @@ impl Engine {
|
||||
for value in rhs_value.iter_mut() {
|
||||
let args = &mut [&mut lhs_value.clone(), value];
|
||||
let def_value = Some(&def_value);
|
||||
let pos = rhs.position();
|
||||
|
||||
let hashes = (
|
||||
// Qualifiers (none) + function name + number of arguments + argument `TypeId`'s.
|
||||
@@ -1335,9 +1334,11 @@ impl Engine {
|
||||
0,
|
||||
);
|
||||
|
||||
let (r, _) = self.call_fn_raw(
|
||||
&mut scope, state, lib, op, hashes, args, false, def_value, pos, level,
|
||||
)?;
|
||||
let (r, _) = self
|
||||
.call_fn_raw(
|
||||
&mut scope, state, lib, op, hashes, args, false, def_value, level,
|
||||
)
|
||||
.map_err(|err| EvalAltResult::new_position(err, rhs.position()))?;
|
||||
if r.as_bool().unwrap_or(false) {
|
||||
return Ok(true.into());
|
||||
}
|
||||
@@ -1371,7 +1372,8 @@ impl Engine {
|
||||
expr: &Expr,
|
||||
level: usize,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
self.inc_operations(state, expr.position())?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, expr.position()))?;
|
||||
|
||||
match expr {
|
||||
Expr::Expr(x) => self.eval_expr(scope, state, lib, x.as_ref(), level),
|
||||
@@ -1395,7 +1397,8 @@ impl Engine {
|
||||
let (lhs_expr, op, rhs_expr, op_pos) = x.as_ref();
|
||||
let mut rhs_val = self.eval_expr(scope, state, lib, rhs_expr, level)?;
|
||||
let (lhs_ptr, name, typ, pos) = search_scope(scope, state, lhs_expr)?;
|
||||
self.inc_operations(state, pos)?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, pos))?;
|
||||
|
||||
match typ {
|
||||
// Assignment to constant variable
|
||||
@@ -1432,10 +1435,9 @@ impl Engine {
|
||||
|
||||
// Set variable value
|
||||
*lhs_ptr = self
|
||||
.exec_fn_call(
|
||||
state, lib, op, true, hash, args, false, None, *op_pos, level,
|
||||
)
|
||||
.map(|(v, _)| v)?;
|
||||
.exec_fn_call(state, lib, op, true, hash, args, false, None, level)
|
||||
.map(|(v, _)| v)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *op_pos))?;
|
||||
}
|
||||
Ok(Default::default())
|
||||
}
|
||||
@@ -1460,10 +1462,9 @@ impl Engine {
|
||||
&mut self.eval_expr(scope, state, lib, lhs_expr, level)?,
|
||||
&mut rhs_val,
|
||||
];
|
||||
self.exec_fn_call(
|
||||
state, lib, op, true, hash, args, false, None, *op_pos, level,
|
||||
)
|
||||
.map(|(v, _)| v)?
|
||||
self.exec_fn_call(state, lib, op, true, hash, args, false, None, level)
|
||||
.map(|(v, _)| v)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *op_pos))?
|
||||
});
|
||||
|
||||
match lhs_expr {
|
||||
@@ -1531,11 +1532,11 @@ impl Engine {
|
||||
if !self.has_override(lib, (hash_fn, *hash)) {
|
||||
// eval - only in function call style
|
||||
let prev_len = scope.len();
|
||||
let pos = args_expr.get(0).position();
|
||||
|
||||
// Evaluate the text string as a script
|
||||
let script = self.eval_expr(scope, state, lib, args_expr.get(0), level)?;
|
||||
let result = self.eval_script_expr(scope, state, lib, &script, pos);
|
||||
let expr = args_expr.get(0);
|
||||
let script = self.eval_expr(scope, state, lib, expr, level)?;
|
||||
let result = self
|
||||
.eval_script_expr(scope, state, lib, &script)
|
||||
.map_err(|err| EvalAltResult::new_position(err, expr.position()));
|
||||
|
||||
if scope.len() != prev_len {
|
||||
// IMPORTANT! If the eval defines new variables in the current scope,
|
||||
@@ -1568,7 +1569,8 @@ impl Engine {
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
let (target, _, typ, pos) = search_scope(scope, state, lhs)?;
|
||||
self.inc_operations(state, pos)?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, pos))?;
|
||||
|
||||
match typ {
|
||||
ScopeEntryType::Module => unreachable!(),
|
||||
@@ -1593,9 +1595,10 @@ impl Engine {
|
||||
|
||||
let args = args.as_mut();
|
||||
self.exec_fn_call(
|
||||
state, lib, name, *native, *hash, args, is_ref, def_val, *pos, level,
|
||||
state, lib, name, *native, *hash, args, is_ref, def_val, level,
|
||||
)
|
||||
.map(|(v, _)| v)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
|
||||
// Module-qualified function call
|
||||
@@ -1628,7 +1631,8 @@ impl Engine {
|
||||
let func = match module.get_qualified_fn(name, *hash_fn_def) {
|
||||
Err(err) if matches!(*err, EvalAltResult::ErrorFunctionNotFound(_, _)) => {
|
||||
// Then search in Rust functions
|
||||
self.inc_operations(state, *pos)?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))?;
|
||||
|
||||
// Rust functions are indexed in two steps:
|
||||
// 1) Calculate a hash in a similar manner to script-defined functions,
|
||||
@@ -1650,7 +1654,8 @@ impl Engine {
|
||||
let args = args.as_mut();
|
||||
let fn_def = x.get_fn_def();
|
||||
let mut scope = Scope::new();
|
||||
self.call_script_fn(&mut scope, state, lib, name, fn_def, args, *pos, level)
|
||||
self.call_script_fn(&mut scope, state, lib, name, fn_def, args, level)
|
||||
.map_err(|err| EvalAltResult::new_position(err, *pos))
|
||||
}
|
||||
Ok(x) if x.is_plugin_fn() => x.get_plugin_fn().call(args.as_mut(), *pos),
|
||||
Ok(x) => x.get_native_fn()(args.as_mut()).map_err(|err| err.new_position(*pos)),
|
||||
@@ -1719,7 +1724,8 @@ impl Engine {
|
||||
stmt: &Stmt,
|
||||
level: usize,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
self.inc_operations(state, stmt.position())?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, stmt.position()))?;
|
||||
|
||||
match stmt {
|
||||
// No-op
|
||||
@@ -1824,7 +1830,8 @@ impl Engine {
|
||||
|
||||
for loop_var in func(iter_type) {
|
||||
*scope.get_mut(index).0 = loop_var;
|
||||
self.inc_operations(state, stmt.position())?;
|
||||
self.inc_operations(state)
|
||||
.map_err(|err| EvalAltResult::new_position(err, stmt.position()))?;
|
||||
|
||||
match self.eval_stmt(scope, state, lib, stmt, level) {
|
||||
Ok(_) => (),
|
||||
@@ -1976,22 +1983,25 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Check if the number of operations stay within limit.
|
||||
fn inc_operations(&self, state: &mut State, pos: Position) -> Result<(), Box<EvalAltResult>> {
|
||||
/// Position in `EvalAltResult` is None and must be set afterwards.
|
||||
fn inc_operations(&self, state: &mut State) -> Result<(), Box<EvalAltResult>> {
|
||||
state.operations += 1;
|
||||
|
||||
#[cfg(not(feature = "unchecked"))]
|
||||
{
|
||||
// Guard against too many operations
|
||||
if state.operations > self.max_operations {
|
||||
return Err(Box::new(EvalAltResult::ErrorTooManyOperations(pos)));
|
||||
return Err(Box::new(EvalAltResult::ErrorTooManyOperations(
|
||||
Position::none(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// Report progress - only in steps
|
||||
if let Some(progress) = &self.progress {
|
||||
if !progress(state.operations) {
|
||||
if !progress(&state.operations) {
|
||||
// Terminate script if progress returns false
|
||||
return Err(Box::new(EvalAltResult::ErrorTerminated(pos)));
|
||||
return Err(Box::new(EvalAltResult::ErrorTerminated(Position::none())));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use crate::any::Dynamic;
|
||||
use crate::parser::FnDef;
|
||||
use crate::plugin::PluginFunction;
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::token::Position;
|
||||
|
||||
use crate::stdlib::{boxed::Box, rc::Rc, sync::Arc};
|
||||
|
||||
@@ -63,7 +63,10 @@ pub type SharedPluginFunction = Arc<dyn PluginFunction + Send + Sync>;
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type SharedPluginFunction = Rc<dyn PluginFunction>;
|
||||
|
||||
use crate::plugin::PluginFunction;
|
||||
#[cfg(not(feature = "sync"))]
|
||||
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + 'static>;
|
||||
#[cfg(feature = "sync")]
|
||||
pub type Callback<T, R> = Box<dyn Fn(&T) -> R + Send + Sync + 'static>;
|
||||
|
||||
/// A type encapsulating a function callable by Rhai.
|
||||
#[derive(Clone)]
|
||||
|
@@ -5,9 +5,7 @@ use crate::engine::{
|
||||
KEYWORD_TYPE_OF,
|
||||
};
|
||||
use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST};
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
|
||||
use crate::token::Position;
|
||||
use crate::utils::StaticVec;
|
||||
|
||||
use crate::stdlib::{
|
||||
@@ -113,8 +111,7 @@ fn call_fn_with_constant_arguments(
|
||||
state: &State,
|
||||
fn_name: &str,
|
||||
arg_values: &mut [Dynamic],
|
||||
pos: Position,
|
||||
) -> Result<Option<Dynamic>, Box<EvalAltResult>> {
|
||||
) -> Option<Dynamic> {
|
||||
// Search built-in's and external functions
|
||||
let hash_fn = calc_fn_hash(
|
||||
empty(),
|
||||
@@ -134,11 +131,10 @@ fn call_fn_with_constant_arguments(
|
||||
arg_values.iter_mut().collect::<StaticVec<_>>().as_mut(),
|
||||
false,
|
||||
None,
|
||||
pos,
|
||||
0,
|
||||
)
|
||||
.map(|(v, _)| Some(v))
|
||||
.or_else(|_| Ok(None))
|
||||
.unwrap_or_else(|_| None)
|
||||
}
|
||||
|
||||
/// Optimize a statement.
|
||||
@@ -574,22 +570,22 @@ fn optimize_expr(expr: Expr, state: &mut State) -> Expr {
|
||||
""
|
||||
};
|
||||
|
||||
call_fn_with_constant_arguments(&state, name, arg_values.as_mut(), *pos).ok()
|
||||
.and_then(|result|
|
||||
result.or_else(|| {
|
||||
if !arg_for_type_of.is_empty() {
|
||||
// Handle `type_of()`
|
||||
Some(arg_for_type_of.to_string().into())
|
||||
} else {
|
||||
// Otherwise use the default value, if any
|
||||
def_value.clone()
|
||||
}
|
||||
}).and_then(|result| map_dynamic_to_expr(result, *pos))
|
||||
.map(|expr| {
|
||||
state.set_dirty();
|
||||
expr
|
||||
})
|
||||
).unwrap_or_else(|| {
|
||||
call_fn_with_constant_arguments(&state, name, arg_values.as_mut())
|
||||
.or_else(|| {
|
||||
if !arg_for_type_of.is_empty() {
|
||||
// Handle `type_of()`
|
||||
Some(arg_for_type_of.to_string().into())
|
||||
} else {
|
||||
// Otherwise use the default value, if any
|
||||
def_value.clone()
|
||||
}
|
||||
})
|
||||
.and_then(|result| map_dynamic_to_expr(result, *pos))
|
||||
.map(|expr| {
|
||||
state.set_dirty();
|
||||
expr
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
// Optimize function call arguments
|
||||
x.3 = x.3.into_iter().map(|a| optimize_expr(a, state)).collect();
|
||||
Expr::FnCall(x)
|
||||
|
@@ -2478,44 +2478,15 @@ fn parse_fn<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_global_expr<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
engine: &Engine,
|
||||
scope: &Scope,
|
||||
optimization_level: OptimizationLevel,
|
||||
max_expr_depth: usize,
|
||||
) -> Result<AST, ParseError> {
|
||||
let mut state = ParseState::new(max_expr_depth);
|
||||
let expr = parse_expr(input, &mut state, 0, false, false)?;
|
||||
|
||||
match input.peek().unwrap() {
|
||||
(Token::EOF, _) => (),
|
||||
// Return error if the expression doesn't end
|
||||
(token, pos) => {
|
||||
return Err(PERR::BadInput(format!("Unexpected '{}'", token.syntax())).into_err(*pos))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
// Optimize AST
|
||||
optimize_into_ast(
|
||||
engine,
|
||||
scope,
|
||||
vec![Stmt::Expr(Box::new(expr))],
|
||||
vec![],
|
||||
optimization_level,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Parse the global level statements.
|
||||
fn parse_global_level<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
max_expr_depth: (usize, usize),
|
||||
max_expr_depth: usize,
|
||||
max_function_expr_depth: usize,
|
||||
) -> Result<(Vec<Stmt>, Vec<FnDef>), ParseError> {
|
||||
let mut statements = Vec::<Stmt>::new();
|
||||
let mut functions = HashMap::<u64, FnDef, _>::with_hasher(StraightHasherBuilder);
|
||||
let mut state = ParseState::new(max_expr_depth.0);
|
||||
let mut state = ParseState::new(max_expr_depth);
|
||||
|
||||
while !input.peek().unwrap().0.is_eof() {
|
||||
// Collect all the function definitions
|
||||
@@ -2530,7 +2501,7 @@ fn parse_global_level<'a>(
|
||||
match input.peek().unwrap() {
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
(Token::Fn, _) => {
|
||||
let mut state = ParseState::new(max_expr_depth.1);
|
||||
let mut state = ParseState::new(max_function_expr_depth);
|
||||
let func = parse_fn(input, &mut state, access, 0, true, true)?;
|
||||
|
||||
// Qualifiers (none) + function name + number of arguments.
|
||||
@@ -2586,20 +2557,49 @@ fn parse_global_level<'a>(
|
||||
Ok((statements, functions.into_iter().map(|(_, v)| v).collect()))
|
||||
}
|
||||
|
||||
/// Run the parser on an input stream, returning an AST.
|
||||
pub fn parse<'a>(
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
engine: &Engine,
|
||||
scope: &Scope,
|
||||
optimization_level: OptimizationLevel,
|
||||
max_expr_depth: (usize, usize),
|
||||
) -> Result<AST, ParseError> {
|
||||
let (statements, lib) = parse_global_level(input, max_expr_depth)?;
|
||||
impl Engine {
|
||||
pub(crate) fn parse_global_expr<'a>(
|
||||
&self,
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
scope: &Scope,
|
||||
optimization_level: OptimizationLevel,
|
||||
) -> Result<AST, ParseError> {
|
||||
let mut state = ParseState::new(self.max_expr_depth);
|
||||
let expr = parse_expr(input, &mut state, 0, false, false)?;
|
||||
|
||||
Ok(
|
||||
// Optimize AST
|
||||
optimize_into_ast(engine, scope, statements, lib, optimization_level),
|
||||
)
|
||||
match input.peek().unwrap() {
|
||||
(Token::EOF, _) => (),
|
||||
// Return error if the expression doesn't end
|
||||
(token, pos) => {
|
||||
return Err(
|
||||
PERR::BadInput(format!("Unexpected '{}'", token.syntax())).into_err(*pos)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let expr = vec![Stmt::Expr(Box::new(expr))];
|
||||
|
||||
Ok(
|
||||
// Optimize AST
|
||||
optimize_into_ast(self, scope, expr, Default::default(), optimization_level),
|
||||
)
|
||||
}
|
||||
|
||||
/// Run the parser on an input stream, returning an AST.
|
||||
pub(crate) fn parse<'a>(
|
||||
&self,
|
||||
input: &mut Peekable<TokenIterator<'a>>,
|
||||
scope: &Scope,
|
||||
optimization_level: OptimizationLevel,
|
||||
) -> Result<AST, ParseError> {
|
||||
let (statements, lib) =
|
||||
parse_global_level(input, self.max_expr_depth, self.max_function_expr_depth)?;
|
||||
|
||||
Ok(
|
||||
// Optimize AST
|
||||
optimize_into_ast(self, scope, statements, lib, optimization_level),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a `Dynamic` value to an expression.
|
||||
|
@@ -331,10 +331,12 @@ impl EvalAltResult {
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume the current `EvalAltResult` and return a new one
|
||||
/// with the specified `Position`.
|
||||
/// Consume the current `EvalAltResult` and return a new one with the specified `Position`
|
||||
/// if the current position is `Position::None`.
|
||||
pub(crate) fn new_position(mut self: Box<Self>, new_position: Position) -> Box<Self> {
|
||||
self.set_position(new_position);
|
||||
if self.position().is_none() {
|
||||
self.set_position(new_position);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@@ -175,7 +175,7 @@ impl<'a> Scope<'a> {
|
||||
///
|
||||
/// Modules are used for accessing member variables, functions and plugins under a namespace.
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
pub fn push_module<K: Into<Cow<'a, str>>>(&mut self, name: K, mut value: Module) {
|
||||
pub fn push_module<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Module) {
|
||||
self.push_module_internal(name, value);
|
||||
}
|
||||
|
||||
|
@@ -198,6 +198,7 @@ pub enum Token {
|
||||
XOrAssign,
|
||||
ModuloAssign,
|
||||
PowerOfAssign,
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Private,
|
||||
Import,
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@@ -284,6 +285,7 @@ impl Token {
|
||||
ModuloAssign => "%=",
|
||||
PowerOf => "~",
|
||||
PowerOfAssign => "~=",
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
Private => "private",
|
||||
Import => "import",
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
@@ -757,6 +759,7 @@ impl<'a> TokenIterator<'a> {
|
||||
"throw" => Token::Throw,
|
||||
"for" => Token::For,
|
||||
"in" => Token::In,
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
"private" => Token::Private,
|
||||
"import" => Token::Import,
|
||||
#[cfg(not(feature = "no_module"))]
|
||||
|
Reference in New Issue
Block a user