Skip to content

Commit

Permalink
Refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 10, 2023
1 parent 419b1ec commit 4a6f49d
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 12 deletions.
17 changes: 7 additions & 10 deletions parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,14 @@ impl Parser {
} else {
self.parse_simple_statement()
};
if stmt.is_ok() {
body.push(stmt.unwrap());
} else {
self.errors.push(stmt.err().unwrap());
self.bump_any();
match stmt {
Ok(stmt) => body.push(stmt),
Err(err) => {
self.errors.push(err);
}
}
}

for err in &self.errors {
println!("{:#?}", err);
}

Module {
node: self.finish_node(node),
Expand Down Expand Up @@ -234,6 +231,7 @@ impl Parser {
Ok(())
}

// deprecated
fn unepxted_token(&mut self, node: Node, kind: Kind) -> Result<(), ParsingError> {
self.bump_any();
let range = self.finish_node(node);
Expand All @@ -249,7 +247,6 @@ impl Parser {
Err(err)
}

// write this like the expect function
fn unexpected_token_new(&mut self, node: Node, kinds: Vec<Kind>, advice: &str) -> ParsingError {
let curr_kind = self.cur_kind();
self.bump_any();
Expand Down Expand Up @@ -1543,7 +1540,7 @@ impl Parser {
} else {
return Err(self.unexpected_token_new(
import_node,
vec![Kind::Identifier, Kind::Mul],
vec![Kind::Identifier, Kind::Mul, Kind::LeftParen],
"Use * for importing everthing or use () to specify names to import or specify the name you want to import"
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
source: parser/src/parser/parser.rs
description: "[a for a in b for c in d]\n"
description: "[a for a in b for c in d]"
input_file: parser/test_data/inputs/one_liners/lists.py
---
Module {
node: Node {
start: 0,
end: 26,
end: 25,
},
body: [
ExpressionStatement(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
source: parser/src/parser/parser.rs
description: "[a for a in b if c for d in e]\n"
input_file: parser/test_data/inputs/one_liners/lists.py
---
Module {
node: Node {
start: 0,
end: 31,
},
body: [
ExpressionStatement(
ListComp(
ListComp {
node: Node {
start: 0,
end: 30,
},
element: Name(
Name {
node: Node {
start: 1,
end: 2,
},
id: "a",
},
),
generators: [
Comprehension {
node: Node {
start: 3,
end: 18,
},
target: Name(
Name {
node: Node {
start: 7,
end: 8,
},
id: "a",
},
),
iter: Name(
Name {
node: Node {
start: 12,
end: 13,
},
id: "b",
},
),
ifs: [
Name(
Name {
node: Node {
start: 17,
end: 18,
},
id: "c",
},
),
],
is_async: false,
},
Comprehension {
node: Node {
start: 19,
end: 29,
},
target: Name(
Name {
node: Node {
start: 23,
end: 24,
},
id: "d",
},
),
iter: Name(
Name {
node: Node {
start: 28,
end: 29,
},
id: "e",
},
),
ifs: [],
is_async: false,
},
],
},
),
),
],
}
2 changes: 2 additions & 0 deletions parser/test_data/inputs/one_liners/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
[a for a in b if c if d]

[a for a in b for c in d]

[a for a in b if c for d in e]
27 changes: 27 additions & 0 deletions typechecker/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use enderpy_python_parser::ParsingError;
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;

#[derive(Error, Diagnostic, Debug)]
pub enum BuildError {
#[error(transparent)]
#[diagnostic(code(builder::io_error))]
IoError(#[from] std::io::Error),

#[error("Invalid syntax")]
#[diagnostic(code(builder::invalid_syntax))]
TypeError {
path: String,
msg: String,
line: u32,
#[help]
advice: String,
#[label("span")]
span: SourceSpan,
},

#[error(transparent)]
// Use `#[diagnostic(transparent)]` to wrap another [`Diagnostic`]. You won't see labels otherwise
#[diagnostic(transparent)]
ParsingError(#[from] ParsingError),
}
1 change: 1 addition & 0 deletions typechecker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod build;
pub mod project;
pub mod semantic_analyzer;
pub mod settings;
pub mod errors;

0 comments on commit 4a6f49d

Please sign in to comment.