Skip to content

Commit 7e40b7f

Browse files
Compile error message cleanup
1 parent e2ee933 commit 7e40b7f

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

parser/src/ast.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,50 @@ pub enum Expression {
219219
None,
220220
}
221221

222+
impl Expression {
223+
/// Returns a short name for the node suitable for use in error messages.
224+
pub fn name(&self) -> &'static str {
225+
use self::Expression::*;
226+
use self::StringGroup::*;
227+
228+
match self {
229+
BoolOp { .. } | Binop { .. } | Unop { .. } => "operator",
230+
Subscript { .. } => "subscript",
231+
Yield { .. } | YieldFrom { .. } => "yield expression",
232+
Compare { .. } => "comparison",
233+
Attribute { .. } => "attribute",
234+
Call { .. } => "function call",
235+
Number { .. }
236+
| String {
237+
value: Constant { .. },
238+
}
239+
| Bytes { .. } => "literal",
240+
List { .. } => "list",
241+
Tuple { .. } => "tuple",
242+
Dict { .. } => "dict display",
243+
Set { .. } => "set display",
244+
Comprehension { kind, .. } => match **kind {
245+
ComprehensionKind::List { .. } => "list comprehension",
246+
ComprehensionKind::Dict { .. } => "dict comprehension",
247+
ComprehensionKind::Set { .. } => "set comprehension",
248+
ComprehensionKind::GeneratorExpression { .. } => "generator expression",
249+
},
250+
Starred { .. } => "starred",
251+
Slice { .. } => "slice",
252+
String {
253+
value: Joined { .. },
254+
}
255+
| String {
256+
value: FormattedValue { .. },
257+
} => "f-string expression",
258+
Identifier { .. } => "named expression",
259+
Lambda { .. } => "lambda",
260+
IfExpression { .. } => "conditional expression",
261+
True | False | None => "keyword",
262+
}
263+
}
264+
}
265+
222266
/*
223267
* In cpython this is called arguments, but we choose parameters to
224268
* distinguish between function parameters and actual call arguments.

vm/src/compile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,13 @@ impl Compiler {
573573
}
574574
ast::Statement::Break => {
575575
if !self.in_loop {
576-
return Err(CompileError::SyntaxErr(String::from("break")));
576+
return Err(CompileError::InvalidBreak);
577577
}
578578
self.emit(Instruction::Break);
579579
}
580580
ast::Statement::Continue => {
581581
if !self.in_loop {
582-
return Err(CompileError::SyntaxErr(String::from("continue")));
582+
return Err(CompileError::InvalidContinue);
583583
}
584584
self.emit(Instruction::Continue);
585585
}
@@ -646,7 +646,7 @@ impl Compiler {
646646
self.emit(Instruction::DeleteSubscript);
647647
}
648648
_ => {
649-
return Err(CompileError::Delete);
649+
return Err(CompileError::Delete(target.name()));
650650
}
651651
}
652652
}
@@ -766,7 +766,7 @@ impl Compiler {
766766
}
767767
}
768768
_ => {
769-
return Err(CompileError::Assign(format!("{:?}", target)));
769+
return Err(CompileError::Assign(target.name()));
770770
}
771771
}
772772

vm/src/error.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@ use std::fmt;
55

66
#[derive(Debug)]
77
pub enum CompileError {
8-
// Invalid assignment, cannot store value in target.
9-
Assign(String),
10-
// Invalid delete
11-
Delete,
12-
// Expected an expression got a statement
8+
/// Invalid assignment, cannot store value in target.
9+
Assign(&'static str),
10+
/// Invalid delete
11+
Delete(&'static str),
12+
/// Expected an expression got a statement
1313
ExpectExpr,
14-
// Parser error
14+
/// Parser error
1515
Parse(ParseError),
16-
// Multiple `*` detected
16+
/// Multiple `*` detected
1717
StarArgs,
18-
// Catches errors undetectable by the parser
19-
SyntaxErr(String),
18+
/// Break statement outside of loop.
19+
InvalidBreak,
20+
/// Continue statement outside of loop.
21+
InvalidContinue,
2022
}
2123

2224
impl fmt::Display for CompileError {
2325
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2426
match self {
25-
CompileError::Assign(expr) => write!(f, "Invalid assignment: {}", expr),
26-
CompileError::Delete => write!(f, "Invalid delete statement"),
27+
CompileError::Assign(target) => write!(f, "can't assign to {}", target),
28+
CompileError::Delete(target) => write!(f, "can't delete {}", target),
2729
CompileError::ExpectExpr => write!(f, "Expecting expression, got statement"),
28-
CompileError::Parse(err) => err.fmt(f),
30+
CompileError::Parse(err) => write!(f, "{}", err),
2931
CompileError::StarArgs => write!(f, "Two starred expressions in assignment"),
30-
CompileError::SyntaxErr(expr) => write!(f, "Syntax Error: '{}' ouside loop", expr),
32+
CompileError::InvalidBreak => write!(f, "break outside loop"),
33+
CompileError::InvalidContinue => write!(f, "continue outside loop"),
3134
}
3235
}
3336
}

0 commit comments

Comments
 (0)