Recursive self-contained AST.

This commit is contained in:
Stephen Chung
2021-01-09 16:52:22 +08:00
parent 637f47d259
commit ad250fc973
7 changed files with 190 additions and 74 deletions

View File

@@ -227,4 +227,30 @@ impl ModuleResolver for FileModuleResolver {
Ok(m)
}
fn resolve_ast(
&self,
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Option<crate::AST>, Box<EvalAltResult>> {
// Construct the script file path
let mut file_path = self.base_path.clone();
file_path.push(path);
file_path.set_extension(&self.extension); // Force extension
// Load the script file and compile it
let mut ast = engine
.compile_file(file_path.clone())
.map_err(|err| match *err {
EvalAltResult::ErrorSystem(_, err) if err.is::<IoError>() => {
Box::new(EvalAltResult::ErrorModuleNotFound(path.to_string(), pos))
}
_ => Box::new(EvalAltResult::ErrorInModule(path.to_string(), err, pos)),
})?;
ast.set_source(path);
Ok(Some(ast))
}
}

View File

@@ -1,6 +1,6 @@
use crate::fn_native::SendSync;
use crate::stdlib::boxed::Box;
use crate::{Engine, EvalAltResult, Module, Position, Shared};
use crate::{Engine, EvalAltResult, Module, Position, Shared, AST};
mod dummy;
pub use dummy::DummyModuleResolver;
@@ -28,4 +28,23 @@ pub trait ModuleResolver: SendSync {
path: &str,
pos: Position,
) -> Result<Shared<Module>, Box<EvalAltResult>>;
/// Resolve a module into an `AST` based on a path string.
///
/// Returns [`None`] (default) if such resolution is not supported
/// (e.g. if the module is Rust-based).
///
/// ## Low-Level API
///
/// Override the default implementation of this method if the module resolver
/// serves modules based on compiled Rhai scripts.
#[allow(unused_variables)]
fn resolve_ast(
&self,
engine: &Engine,
path: &str,
pos: Position,
) -> Result<Option<AST>, Box<EvalAltResult>> {
Ok(None)
}
}