diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 20c01af..66bdd37 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -1,3 +1,4 @@ +use dbg_pls::{DebugPls, Formatter}; use peekmore::PeekMoreIterator; use crate::{ @@ -19,22 +20,28 @@ pub struct ParserError { } impl ParserError { - #[track_caller] fn new(span: Span, message: String) -> Self { Self { span, message } } - #[track_caller] fn eof() -> Self { Self::new(Span::default(), "unexpected end of file".to_string()) } - #[track_caller] fn unsupported(span: Span, token: &Tok<'_>) -> Self { Self::new(span, format!("`{token}` is not supported")) } } +impl DebugPls for ParserError { + fn fmt(&self, f: Formatter<'_>) { + f.debug_struct("ParserError") + .field("span", &self.span) + .field("message", &self.message) + .finish(); + } +} + type Result = std::result::Result; struct Parser<'src, I> diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap index 3476762..d588a33 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_function_with_params.snap @@ -1,85 +1,66 @@ --- source: parser/src/parser/tests.rs -expression: parsed +expression: parsed_pretty --- -Ok( - [ - ( - FunctionDef( - FunctionDef { - declaration: Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Int, - attrs: (empty), - }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: WithParams { - ident: ( - "uwu", - 5..8, - ), - params: [ - FunctionParamDecl { - decl_spec: ( - DeclSpec { - ty: Long, - attrs: (empty), - }, - 9..13, - ), - declarator: ( - Declarator { - decl: Ident( - ( - "owo", - 14..17, - ), - ), - pointer: false, - }, - 14..17, - ), - }, - FunctionParamDecl { - decl_spec: ( - DeclSpec { - ty: Int, - attrs: (empty), - }, - 19..22, - ), - declarator: ( - Declarator { - decl: Ident( - ( - "qwq", - 23..26, - ), - ), - pointer: false, - }, - 23..26, - ), - }, - ], - }, - pointer: false, - }, - init: None, - }, - 5..8, - ), - ], - }, - ), - body: [], +Ok([ + ( + FunctionDef(FunctionDef { + declaration: Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Int, + attrs: "(empty)", }, - ), - 1..30, - ), - ], -) + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: WithParams { + ident: ("uwu", 5..8), + params: [ + FunctionParamDecl { + decl_spec: ( + DeclSpec { + ty: Long, + attrs: "(empty)", + }, + 9..13, + ), + declarator: ( + Declarator { + decl: Ident(("owo", 14..17)), + pointer: false, + }, + 14..17, + ), + }, + FunctionParamDecl { + decl_spec: ( + DeclSpec { + ty: Int, + attrs: "(empty)", + }, + 19..22, + ), + declarator: ( + Declarator { + decl: Ident(("qwq", 23..26)), + pointer: false, + }, + 23..26, + ), + }, + ], + }, + pointer: false, + }, + init: None, + }, + 5..8, + ), + ], + }), + body: [], + }), + 1..30, + ), +]) diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap index d5da061..5de27a9 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_funky_attributes_no_params_function.snap @@ -1,42 +1,33 @@ --- source: parser/src/parser/tests.rs -expression: parsed +expression: parsed_pretty --- -Ok( - [ - ( - FunctionDef( - FunctionDef { - declaration: Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Int, - attrs: EXTERN | THREAD_LOCAL, - }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: WithParams { - ident: ( - "uwu", - 35..38, - ), - params: [], - }, - pointer: false, - }, - init: None, - }, - 35..38, - ), - ], - }, - ), - body: [], +Ok([ + ( + FunctionDef(FunctionDef { + declaration: Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Int, + attrs: "EXTERN | THREAD_LOCAL", }, - ), - 1..43, - ), - ], -) + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: WithParams { + ident: ("uwu", 35..38), + params: [], + }, + pointer: false, + }, + init: None, + }, + 35..38, + ), + ], + }), + body: [], + }), + 1..43, + ), +]) diff --git a/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap b/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap index d9c6c4a..aafb3a2 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__empty_void_function.snap @@ -1,42 +1,33 @@ --- source: parser/src/parser/tests.rs -expression: parsed +expression: parsed_pretty --- -Ok( - [ - ( - FunctionDef( - FunctionDef { - declaration: Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Void, - attrs: (empty), - }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: WithParams { - ident: ( - "uwu", - 6..9, - ), - params: [], - }, - pointer: false, - }, - init: None, - }, - 6..9, - ), - ], - }, - ), - body: [], +Ok([ + ( + FunctionDef(FunctionDef { + declaration: Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Void, + attrs: "(empty)", }, - ), - 1..14, - ), - ], -) + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: WithParams { + ident: ("uwu", 6..9), + params: [], + }, + pointer: false, + }, + init: None, + }, + 6..9, + ), + ], + }), + body: [], + }), + 1..14, + ), +]) diff --git a/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap b/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap index 9365231..0bddb39 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__global_variable_declarations.snap @@ -1,114 +1,88 @@ --- source: parser/src/parser/tests.rs -expression: parsed +expression: parsed_pretty --- -Ok( - [ - ( - Decl( - Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Int, - attrs: (empty), +Ok([ + ( + Decl( + Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Int, + attrs: "(empty)", + }, + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: Ident(("test", 5..9)), + pointer: false, + }, + init: None, }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: Ident( - ( - "test", - 5..9, - ), - ), - pointer: false, - }, - init: None, - }, - 5..9, - ), - ], - }, - ), - ), - 1..9, + 5..9, + ), + ], + }), ), - ( - Decl( - Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Double, - attrs: THREAD_LOCAL, + 1..9, + ), + ( + Decl( + Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Double, + attrs: "THREAD_LOCAL", + }, + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: Ident(("uwu", 32..35)), + pointer: false, + }, + init: None, }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: Ident( - ( - "uwu", - 32..35, - ), - ), - pointer: false, - }, - init: None, - }, - 32..35, - ), - ( - InitDecl { - declarator: Declarator { - decl: Ident( - ( - "owo", - 37..40, - ), - ), - pointer: false, - }, - init: None, - }, - 37..40, - ), - ], - }, - ), - ), - 11..40, - ), - ( - Decl( - Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Int, - attrs: (empty), + 32..35, + ), + ( + InitDecl { + declarator: Declarator { + decl: Ident(("owo", 37..40)), + pointer: false, + }, + init: None, }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: WithParams { - ident: ( - "function", - 68..76, - ), - params: [], - }, - pointer: false, - }, - init: None, - }, - 68..76, - ), - ], - }, - ), - ), - 64..76, + 37..40, + ), + ], + }), ), - ], -) + 11..40, + ), + ( + Decl( + Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Int, + attrs: "(empty)", + }, + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: WithParams { + ident: ("function", 68..76), + params: [], + }, + pointer: false, + }, + init: None, + }, + 68..76, + ), + ], + }), + ), + 64..76, + ), +]) diff --git a/parser/src/parser/snapshots/parser__parser__tests__small_expression.snap b/parser/src/parser/snapshots/parser__parser__tests__small_expression.snap index 326056d..c7c8f17 100644 --- a/parser/src/parser/snapshots/parser__parser__tests__small_expression.snap +++ b/parser/src/parser/snapshots/parser__parser__tests__small_expression.snap @@ -1,63 +1,36 @@ --- source: parser/src/parser/tests.rs -expression: parsed +expression: parsed_pretty --- -Ok( - [ - ( - Decl( - Normal( - NormalDecl { - decl_spec: DeclSpec { - ty: Int, - attrs: (empty), +Ok([ + ( + Decl( + Normal(NormalDecl { + decl_spec: DeclSpec { + ty: Int, + attrs: "(empty)", + }, + init_declarators: [ + ( + InitDecl { + declarator: Declarator { + decl: Ident(("x", 5..6)), + pointer: false, + }, + init: Some(( + Binary(ExprBinary { + lhs: (Atom(Int(1)), 9..10), + rhs: (Atom(Int(1)), 13..14), + op: Add, + }), + 9..14, + )), }, - init_declarators: [ - ( - InitDecl { - declarator: Declarator { - decl: Ident( - ( - "x", - 5..6, - ), - ), - pointer: false, - }, - init: Some( - ( - Binary( - ExprBinary { - lhs: ( - Atom( - Int( - 1, - ), - ), - 9..10, - ), - rhs: ( - Atom( - Int( - 1, - ), - ), - 13..14, - ), - op: Add, - }, - ), - 9..14, - ), - ), - }, - 5..6, - ), - ], - }, - ), - ), - 1..6, + 5..6, + ), + ], + }), ), - ], -) + 1..6, + ), +]) diff --git a/parser/src/parser/tests.rs b/parser/src/parser/tests.rs index fb6ec22..fafcff9 100644 --- a/parser/src/parser/tests.rs +++ b/parser/src/parser/tests.rs @@ -9,20 +9,16 @@ fn lex_and_pre(src: &str) -> impl Iterator, Span)> + '_ { } fn the_current_root_parse_thing<'src>(src: impl Iterator, Span)>) -> impl Debug { - use peekmore::PeekMore; - - let mut parser = Parser { - lex: src.peekmore(), - }; - - parser.external_declarations() + super::parse_declarations(src) } macro_rules! parse_test { ($src:expr) => { let lexer = lex_and_pre($src); - let parsed = the_current_root_parse_thing(lexer); - insta::assert_debug_snapshot!(parsed); + let parsed = super::parse_declarations(lexer); + let parsed_pretty = dbg_pls::pretty(&parsed); + + insta::assert_debug_snapshot!(parsed_pretty); }; }