From 4a6f49d88965b774a6780bffb9563a4a87da974a Mon Sep 17 00:00:00 2001 From: Glyphack Date: Tue, 10 Oct 2023 23:33:40 +0200 Subject: [PATCH] Refactor errors --- parser/src/parser/parser.rs | 17 ++-- ...parser__tests__one_liners@lists.py-11.snap | 4 +- ...parser__tests__one_liners@lists.py-12.snap | 96 +++++++++++++++++++ parser/test_data/inputs/one_liners/lists.py | 2 + typechecker/src/errors.rs | 27 ++++++ typechecker/src/lib.rs | 1 + 6 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-12.snap create mode 100644 typechecker/src/errors.rs diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index 12612587..d4e1d82b 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -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), @@ -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); @@ -249,7 +247,6 @@ impl Parser { Err(err) } - // write this like the expect function fn unexpected_token_new(&mut self, node: Node, kinds: Vec, advice: &str) -> ParsingError { let curr_kind = self.cur_kind(); self.bump_any(); @@ -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" )); } diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-11.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-11.snap index 54507533..c68bbca3 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-11.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-11.snap @@ -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( diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-12.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-12.snap new file mode 100644 index 00000000..372b7872 --- /dev/null +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__one_liners@lists.py-12.snap @@ -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, + }, + ], + }, + ), + ), + ], +} diff --git a/parser/test_data/inputs/one_liners/lists.py b/parser/test_data/inputs/one_liners/lists.py index aed575db..6f5f8ec4 100644 --- a/parser/test_data/inputs/one_liners/lists.py +++ b/parser/test_data/inputs/one_liners/lists.py @@ -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] diff --git a/typechecker/src/errors.rs b/typechecker/src/errors.rs new file mode 100644 index 00000000..b6edf50d --- /dev/null +++ b/typechecker/src/errors.rs @@ -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), +} diff --git a/typechecker/src/lib.rs b/typechecker/src/lib.rs index 78b1ffbe..20dd9008 100644 --- a/typechecker/src/lib.rs +++ b/typechecker/src/lib.rs @@ -12,3 +12,4 @@ pub mod build; pub mod project; pub mod semantic_analyzer; pub mod settings; +pub mod errors;