diff --git a/src/compile.rs b/src/compile.rs index e4bf208..39f550d 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -63,17 +63,21 @@ pub fn compile( match mode { Mode::Exec => { let ast = parser::parse_program(source)?; - compile_program(ast, source_path, optimize) + compile_program(ast, source_path.clone(), optimize) } Mode::Eval => { let statement = parser::parse_statement(source)?; - compile_statement_eval(statement, source_path, optimize) + compile_statement_eval(statement, source_path.clone(), optimize) } Mode::Single => { let ast = parser::parse_program(source)?; - compile_program_single(ast, source_path, optimize) + compile_program_single(ast, source_path.clone(), optimize) } } + .map_err(|mut err| { + err.update_source_path(&source_path); + err + }) } /// A helper function for the shared code of the different compile functions @@ -258,6 +262,7 @@ impl Compiler { statement: None, error: CompileErrorType::ExpectExpr, location: statement.location.clone(), + source_path: None, }); } } @@ -537,6 +542,7 @@ impl Compiler { statement: None, error: CompileErrorType::InvalidBreak, location: statement.location.clone(), + source_path: None, }); } self.emit(Instruction::Break); @@ -547,6 +553,7 @@ impl Compiler { statement: None, error: CompileErrorType::InvalidContinue, location: statement.location.clone(), + source_path: None, }); } self.emit(Instruction::Continue); @@ -557,6 +564,7 @@ impl Compiler { statement: None, error: CompileErrorType::InvalidReturn, location: statement.location.clone(), + source_path: None, }); } match value { @@ -635,6 +643,7 @@ impl Compiler { statement: None, error: CompileErrorType::Delete(expression.name()), location: self.current_source_location.clone(), + source_path: None, }); } } @@ -1339,6 +1348,7 @@ impl Compiler { statement: None, error: CompileErrorType::StarArgs, location: self.current_source_location.clone(), + source_path: None, }); } else { seen_star = true; @@ -1369,6 +1379,7 @@ impl Compiler { statement: None, error: CompileErrorType::Assign(target.name()), location: self.current_source_location.clone(), + source_path: None, }); } } @@ -1654,6 +1665,7 @@ impl Compiler { statement: Option::None, error: CompileErrorType::InvalidYield, location: self.current_source_location.clone(), + source_path: Option::None, }); } self.mark_generator(); @@ -1751,6 +1763,7 @@ impl Compiler { "Invalid starred expression", )), location: self.current_source_location.clone(), + source_path: Option::None, }); } IfExpression { test, body, orelse } => { diff --git a/src/error.rs b/src/error.rs index f426937..ae53a00 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,12 +10,18 @@ pub struct CompileError { pub statement: Option, pub error: CompileErrorType, pub location: Location, + pub source_path: Option, } impl CompileError { pub fn update_statement_info(&mut self, statement: String) { self.statement = Some(statement); } + + pub fn update_source_path(&mut self, source_path: &str) { + debug_assert!(self.source_path.is_none()); + self.source_path = Some(source_path.to_owned()); + } } impl From for CompileError { @@ -24,6 +30,7 @@ impl From for CompileError { statement: None, error: CompileErrorType::Parse(error.error), location: error.location, + source_path: None, } } } diff --git a/src/symboltable.rs b/src/symboltable.rs index 217265b..2813854 100644 --- a/src/symboltable.rs +++ b/src/symboltable.rs @@ -145,6 +145,7 @@ impl From for CompileError { statement: None, error: CompileErrorType::SyntaxError(error.error), location: error.location, + source_path: None, } } }