Change resolve_ast return type.

This commit is contained in:
Stephen Chung 2021-01-09 23:26:50 +08:00
parent 0b6657d1b1
commit 14fe4f9f1b
4 changed files with 57 additions and 30 deletions

View File

@ -936,11 +936,15 @@ impl Engine {
while let Some(path) = imports.iter().next() { while let Some(path) = imports.iter().next() {
let path = path.clone(); let path = path.clone();
if let Some(module_ast) = match self
self.module_resolver .module_resolver
.resolve_ast(self, &path, Position::NONE)? .resolve_ast(self, &path, Position::NONE)
{ {
collect_imports(&module_ast, &mut resolver, &mut imports); Some(Ok(module_ast)) => {
collect_imports(&module_ast, &mut resolver, &mut imports)
}
Some(err @ Err(_)) => return err,
None => (),
} }
let module = shared_take_or_clone(self.module_resolver.resolve( let module = shared_take_or_clone(self.module_resolver.resolve(

View File

@ -168,27 +168,48 @@ impl fmt::Debug for Module {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
"Module({}\n modules: {}\n vars: {}\n functions: {}\n)", "Module({}\n{}{}{})",
if let Some(ref id) = self.id { if let Some(ref id) = self.id {
format!("id: {:?}", id) format!("id: {:?},", id)
} else { } else {
"".to_string() "".to_string()
}, },
if !self.modules.is_empty() {
format!(
" modules: {}\n",
self.modules self.modules
.keys() .keys()
.map(|m| m.as_str()) .map(|m| m.as_str())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "), .join(", ")
)
} else {
"".to_string()
},
if !self.variables.is_empty() {
format!(
" vars: {}\n",
self.variables self.variables
.iter() .iter()
.map(|(k, v)| format!("{}={:?}", k, v)) .map(|(k, v)| format!("{}={:?}", k, v))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "), .join(", ")
)
} else {
"".to_string()
},
if !self.functions.is_empty() {
format!(
" functions: {}\n",
self.functions self.functions
.values() .values()
.map(|FuncInfo { func, .. }| func.to_string()) .map(|FuncInfo { func, .. }| func.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "), .join(", ")
)
} else {
"".to_string()
}
) )
} }
} }

View File

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

View File

@ -44,7 +44,7 @@ pub trait ModuleResolver: SendSync {
engine: &Engine, engine: &Engine,
path: &str, path: &str,
pos: Position, pos: Position,
) -> Result<Option<AST>, Box<EvalAltResult>> { ) -> Option<Result<AST, Box<EvalAltResult>>> {
Ok(None) None
} }
} }