Encapsulate scope into EvalContext;
Fix bug with custom syntax delta.
This commit is contained in:
27
src/api.rs
27
src/api.rs
@@ -3,12 +3,12 @@
|
||||
use crate::any::{Dynamic, Variant};
|
||||
use crate::engine::{Engine, EvalContext, Imports, State};
|
||||
use crate::error::ParseError;
|
||||
use crate::fn_native::{NativeCallContext, SendSync};
|
||||
use crate::fn_native::{FnCallArgs, NativeCallContext, SendSync};
|
||||
use crate::optimize::OptimizationLevel;
|
||||
use crate::parser::AST;
|
||||
use crate::result::EvalAltResult;
|
||||
use crate::scope::Scope;
|
||||
use crate::token::{lex, Position};
|
||||
use crate::token::Position;
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
use crate::{
|
||||
@@ -68,7 +68,7 @@ impl Engine {
|
||||
&mut self,
|
||||
name: &str,
|
||||
arg_types: &[TypeId],
|
||||
func: impl Fn(NativeCallContext, &mut [&mut Dynamic]) -> Result<T, Box<EvalAltResult>>
|
||||
func: impl Fn(NativeCallContext, &mut FnCallArgs) -> Result<T, Box<EvalAltResult>>
|
||||
+ SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
@@ -913,7 +913,7 @@ impl Engine {
|
||||
scripts: &[&str],
|
||||
optimization_level: OptimizationLevel,
|
||||
) -> Result<AST, ParseError> {
|
||||
let stream = lex(scripts, None, self);
|
||||
let stream = self.lex(scripts, None);
|
||||
self.parse(&mut stream.peekable(), scope, optimization_level)
|
||||
}
|
||||
|
||||
@@ -1067,7 +1067,7 @@ impl Engine {
|
||||
.into());
|
||||
};
|
||||
|
||||
let stream = lex(
|
||||
let stream = self.lex(
|
||||
&scripts,
|
||||
if has_null {
|
||||
Some(Box::new(|token| match token {
|
||||
@@ -1078,7 +1078,6 @@ impl Engine {
|
||||
} else {
|
||||
None
|
||||
},
|
||||
self,
|
||||
);
|
||||
let ast =
|
||||
self.parse_global_expr(&mut stream.peekable(), &scope, OptimizationLevel::None)?;
|
||||
@@ -1162,7 +1161,7 @@ impl Engine {
|
||||
script: &str,
|
||||
) -> Result<AST, ParseError> {
|
||||
let scripts = [script];
|
||||
let stream = lex(&scripts, None, self);
|
||||
let stream = self.lex(&scripts, None);
|
||||
{
|
||||
let mut peekable = stream.peekable();
|
||||
self.parse_global_expr(&mut peekable, scope, self.optimization_level)
|
||||
@@ -1323,7 +1322,7 @@ impl Engine {
|
||||
script: &str,
|
||||
) -> Result<T, Box<EvalAltResult>> {
|
||||
let scripts = [script];
|
||||
let stream = lex(&scripts, None, self);
|
||||
let stream = self.lex(&scripts, None);
|
||||
|
||||
// No need to optimize a lone expression
|
||||
let ast = self.parse_global_expr(&mut stream.peekable(), scope, OptimizationLevel::None)?;
|
||||
@@ -1421,6 +1420,7 @@ impl Engine {
|
||||
})
|
||||
.or_else(|err| match *err {
|
||||
EvalAltResult::Return(out, _) => Ok(out),
|
||||
EvalAltResult::LoopBreak(_, _) => unreachable!(),
|
||||
_ => Err(err),
|
||||
})
|
||||
.map(|v| (v, state.operations))
|
||||
@@ -1464,7 +1464,7 @@ impl Engine {
|
||||
script: &str,
|
||||
) -> Result<(), Box<EvalAltResult>> {
|
||||
let scripts = [script];
|
||||
let stream = lex(&scripts, None, self);
|
||||
let stream = self.lex(&scripts, None);
|
||||
let ast = self.parse(&mut stream.peekable(), scope, self.optimization_level)?;
|
||||
self.consume_ast_with_scope(scope, &ast)
|
||||
}
|
||||
@@ -1495,7 +1495,8 @@ impl Engine {
|
||||
.map_or_else(
|
||||
|err| match *err {
|
||||
EvalAltResult::Return(_, _) => Ok(()),
|
||||
err => Err(Box::new(err)),
|
||||
EvalAltResult::LoopBreak(_, _) => unreachable!(),
|
||||
_ => Err(err),
|
||||
},
|
||||
|_| Ok(()),
|
||||
)
|
||||
@@ -1643,7 +1644,7 @@ impl Engine {
|
||||
lib: &Module,
|
||||
name: &str,
|
||||
this_ptr: &mut Option<&mut Dynamic>,
|
||||
args: &mut [&mut Dynamic],
|
||||
args: &mut FnCallArgs,
|
||||
) -> Result<Dynamic, Box<EvalAltResult>> {
|
||||
let fn_def = lib
|
||||
.get_script_fn(name, args.len(), true)
|
||||
@@ -1713,7 +1714,7 @@ impl Engine {
|
||||
/// let mut engine = Engine::new();
|
||||
///
|
||||
/// // Register a variable resolver.
|
||||
/// engine.on_var(|name, _, _, _| {
|
||||
/// engine.on_var(|name, _, _| {
|
||||
/// match name {
|
||||
/// "MYSTIC_NUMBER" => Ok(Some(42_i64.into())),
|
||||
/// _ => Ok(None)
|
||||
@@ -1728,7 +1729,7 @@ impl Engine {
|
||||
#[inline(always)]
|
||||
pub fn on_var(
|
||||
&mut self,
|
||||
callback: impl Fn(&str, usize, &Scope, &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>
|
||||
callback: impl Fn(&str, usize, &EvalContext) -> Result<Option<Dynamic>, Box<EvalAltResult>>
|
||||
+ SendSync
|
||||
+ 'static,
|
||||
) -> &mut Self {
|
||||
|
Reference in New Issue
Block a user