Skip to content

Commit 4a6f49d

Browse files
committed
Refactor errors
1 parent 419b1ec commit 4a6f49d

File tree

6 files changed

+135
-12
lines changed

6 files changed

+135
-12
lines changed

parser/src/parser/parser.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,14 @@ impl Parser {
7171
} else {
7272
self.parse_simple_statement()
7373
};
74-
if stmt.is_ok() {
75-
body.push(stmt.unwrap());
76-
} else {
77-
self.errors.push(stmt.err().unwrap());
78-
self.bump_any();
74+
match stmt {
75+
Ok(stmt) => body.push(stmt),
76+
Err(err) => {
77+
self.errors.push(err);
78+
}
7979
}
8080
}
8181

82-
for err in &self.errors {
83-
println!("{:#?}", err);
84-
}
8582

8683
Module {
8784
node: self.finish_node(node),
@@ -234,6 +231,7 @@ impl Parser {
234231
Ok(())
235232
}
236233

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

252-
// write this like the expect function
253250
fn unexpected_token_new(&mut self, node: Node, kinds: Vec<Kind>, advice: &str) -> ParsingError {
254251
let curr_kind = self.cur_kind();
255252
self.bump_any();
@@ -1543,7 +1540,7 @@ impl Parser {
15431540
} else {
15441541
return Err(self.unexpected_token_new(
15451542
import_node,
1546-
vec![Kind::Identifier, Kind::Mul],
1543+
vec![Kind::Identifier, Kind::Mul, Kind::LeftParen],
15471544
"Use * for importing everthing or use () to specify names to import or specify the name you want to import"
15481545
));
15491546
}

parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-11.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
source: parser/src/parser/parser.rs
3-
description: "[a for a in b for c in d]\n"
3+
description: "[a for a in b for c in d]"
44
input_file: parser/test_data/inputs/one_liners/lists.py
55
---
66
Module {
77
node: Node {
88
start: 0,
9-
end: 26,
9+
end: 25,
1010
},
1111
body: [
1212
ExpressionStatement(
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
source: parser/src/parser/parser.rs
3+
description: "[a for a in b if c for d in e]\n"
4+
input_file: parser/test_data/inputs/one_liners/lists.py
5+
---
6+
Module {
7+
node: Node {
8+
start: 0,
9+
end: 31,
10+
},
11+
body: [
12+
ExpressionStatement(
13+
ListComp(
14+
ListComp {
15+
node: Node {
16+
start: 0,
17+
end: 30,
18+
},
19+
element: Name(
20+
Name {
21+
node: Node {
22+
start: 1,
23+
end: 2,
24+
},
25+
id: "a",
26+
},
27+
),
28+
generators: [
29+
Comprehension {
30+
node: Node {
31+
start: 3,
32+
end: 18,
33+
},
34+
target: Name(
35+
Name {
36+
node: Node {
37+
start: 7,
38+
end: 8,
39+
},
40+
id: "a",
41+
},
42+
),
43+
iter: Name(
44+
Name {
45+
node: Node {
46+
start: 12,
47+
end: 13,
48+
},
49+
id: "b",
50+
},
51+
),
52+
ifs: [
53+
Name(
54+
Name {
55+
node: Node {
56+
start: 17,
57+
end: 18,
58+
},
59+
id: "c",
60+
},
61+
),
62+
],
63+
is_async: false,
64+
},
65+
Comprehension {
66+
node: Node {
67+
start: 19,
68+
end: 29,
69+
},
70+
target: Name(
71+
Name {
72+
node: Node {
73+
start: 23,
74+
end: 24,
75+
},
76+
id: "d",
77+
},
78+
),
79+
iter: Name(
80+
Name {
81+
node: Node {
82+
start: 28,
83+
end: 29,
84+
},
85+
id: "e",
86+
},
87+
),
88+
ifs: [],
89+
is_async: false,
90+
},
91+
],
92+
},
93+
),
94+
),
95+
],
96+
}

parser/test_data/inputs/one_liners/lists.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@
2828
[a for a in b if c if d]
2929

3030
[a for a in b for c in d]
31+
32+
[a for a in b if c for d in e]

typechecker/src/errors.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use enderpy_python_parser::ParsingError;
2+
use miette::{Diagnostic, SourceSpan};
3+
use thiserror::Error;
4+
5+
#[derive(Error, Diagnostic, Debug)]
6+
pub enum BuildError {
7+
#[error(transparent)]
8+
#[diagnostic(code(builder::io_error))]
9+
IoError(#[from] std::io::Error),
10+
11+
#[error("Invalid syntax")]
12+
#[diagnostic(code(builder::invalid_syntax))]
13+
TypeError {
14+
path: String,
15+
msg: String,
16+
line: u32,
17+
#[help]
18+
advice: String,
19+
#[label("span")]
20+
span: SourceSpan,
21+
},
22+
23+
#[error(transparent)]
24+
// Use `#[diagnostic(transparent)]` to wrap another [`Diagnostic`]. You won't see labels otherwise
25+
#[diagnostic(transparent)]
26+
ParsingError(#[from] ParsingError),
27+
}

typechecker/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ pub mod build;
1212
pub mod project;
1313
pub mod semantic_analyzer;
1414
pub mod settings;
15+
pub mod errors;

0 commit comments

Comments
 (0)