implement SyntaxError attributes

This commit is contained in:
Jeong YunWon 2019-12-14 22:05:15 +09:00
parent aff1a6d53c
commit 23993c2833
3 changed files with 24 additions and 3 deletions

View file

@ -63,17 +63,21 @@ pub fn compile(
match mode { match mode {
Mode::Exec => { Mode::Exec => {
let ast = parser::parse_program(source)?; let ast = parser::parse_program(source)?;
compile_program(ast, source_path, optimize) compile_program(ast, source_path.clone(), optimize)
} }
Mode::Eval => { Mode::Eval => {
let statement = parser::parse_statement(source)?; let statement = parser::parse_statement(source)?;
compile_statement_eval(statement, source_path, optimize) compile_statement_eval(statement, source_path.clone(), optimize)
} }
Mode::Single => { Mode::Single => {
let ast = parser::parse_program(source)?; 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 /// A helper function for the shared code of the different compile functions
@ -258,6 +262,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::ExpectExpr, error: CompileErrorType::ExpectExpr,
location: statement.location.clone(), location: statement.location.clone(),
source_path: None,
}); });
} }
} }
@ -537,6 +542,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::InvalidBreak, error: CompileErrorType::InvalidBreak,
location: statement.location.clone(), location: statement.location.clone(),
source_path: None,
}); });
} }
self.emit(Instruction::Break); self.emit(Instruction::Break);
@ -547,6 +553,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::InvalidContinue, error: CompileErrorType::InvalidContinue,
location: statement.location.clone(), location: statement.location.clone(),
source_path: None,
}); });
} }
self.emit(Instruction::Continue); self.emit(Instruction::Continue);
@ -557,6 +564,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::InvalidReturn, error: CompileErrorType::InvalidReturn,
location: statement.location.clone(), location: statement.location.clone(),
source_path: None,
}); });
} }
match value { match value {
@ -635,6 +643,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::Delete(expression.name()), error: CompileErrorType::Delete(expression.name()),
location: self.current_source_location.clone(), location: self.current_source_location.clone(),
source_path: None,
}); });
} }
} }
@ -1339,6 +1348,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::StarArgs, error: CompileErrorType::StarArgs,
location: self.current_source_location.clone(), location: self.current_source_location.clone(),
source_path: None,
}); });
} else { } else {
seen_star = true; seen_star = true;
@ -1369,6 +1379,7 @@ impl<O: OutputStream> Compiler<O> {
statement: None, statement: None,
error: CompileErrorType::Assign(target.name()), error: CompileErrorType::Assign(target.name()),
location: self.current_source_location.clone(), location: self.current_source_location.clone(),
source_path: None,
}); });
} }
} }
@ -1654,6 +1665,7 @@ impl<O: OutputStream> Compiler<O> {
statement: Option::None, statement: Option::None,
error: CompileErrorType::InvalidYield, error: CompileErrorType::InvalidYield,
location: self.current_source_location.clone(), location: self.current_source_location.clone(),
source_path: Option::None,
}); });
} }
self.mark_generator(); self.mark_generator();
@ -1751,6 +1763,7 @@ impl<O: OutputStream> Compiler<O> {
"Invalid starred expression", "Invalid starred expression",
)), )),
location: self.current_source_location.clone(), location: self.current_source_location.clone(),
source_path: Option::None,
}); });
} }
IfExpression { test, body, orelse } => { IfExpression { test, body, orelse } => {

View file

@ -10,12 +10,18 @@ pub struct CompileError {
pub statement: Option<String>, pub statement: Option<String>,
pub error: CompileErrorType, pub error: CompileErrorType,
pub location: Location, pub location: Location,
pub source_path: Option<String>,
} }
impl CompileError { impl CompileError {
pub fn update_statement_info(&mut self, statement: String) { pub fn update_statement_info(&mut self, statement: String) {
self.statement = Some(statement); 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<ParseError> for CompileError { impl From<ParseError> for CompileError {
@ -24,6 +30,7 @@ impl From<ParseError> for CompileError {
statement: None, statement: None,
error: CompileErrorType::Parse(error.error), error: CompileErrorType::Parse(error.error),
location: error.location, location: error.location,
source_path: None,
} }
} }
} }

View file

@ -145,6 +145,7 @@ impl From<SymbolTableError> for CompileError {
statement: None, statement: None,
error: CompileErrorType::SyntaxError(error.error), error: CompileErrorType::SyntaxError(error.error),
location: error.location, location: error.location,
source_path: None,
} }
} }
} }