use dbg-pls for parser snapshot tests

This commit is contained in:
nora 2022-06-26 15:20:20 +02:00
parent 31575cbdfb
commit e7597dab07
7 changed files with 246 additions and 333 deletions

View file

@ -1,3 +1,4 @@
use dbg_pls::{DebugPls, Formatter};
use peekmore::PeekMoreIterator; use peekmore::PeekMoreIterator;
use crate::{ use crate::{
@ -19,22 +20,28 @@ pub struct ParserError {
} }
impl ParserError { impl ParserError {
#[track_caller]
fn new(span: Span, message: String) -> Self { fn new(span: Span, message: String) -> Self {
Self { span, message } Self { span, message }
} }
#[track_caller]
fn eof() -> Self { fn eof() -> Self {
Self::new(Span::default(), "unexpected end of file".to_string()) Self::new(Span::default(), "unexpected end of file".to_string())
} }
#[track_caller]
fn unsupported(span: Span, token: &Tok<'_>) -> Self { fn unsupported(span: Span, token: &Tok<'_>) -> Self {
Self::new(span, format!("`{token}` is not supported")) 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<T, E = ParserError> = std::result::Result<T, E>; type Result<T, E = ParserError> = std::result::Result<T, E>;
struct Parser<'src, I> struct Parser<'src, I>

View file

@ -1,44 +1,33 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[
( (
FunctionDef( FunctionDef(FunctionDef {
FunctionDef { declaration: Normal(NormalDecl {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: (empty), attrs: "(empty)",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: WithParams { decl: WithParams {
ident: ( ident: ("uwu", 5..8),
"uwu",
5..8,
),
params: [ params: [
FunctionParamDecl { FunctionParamDecl {
decl_spec: ( decl_spec: (
DeclSpec { DeclSpec {
ty: Long, ty: Long,
attrs: (empty), attrs: "(empty)",
}, },
9..13, 9..13,
), ),
declarator: ( declarator: (
Declarator { Declarator {
decl: Ident( decl: Ident(("owo", 14..17)),
(
"owo",
14..17,
),
),
pointer: false, pointer: false,
}, },
14..17, 14..17,
@ -48,18 +37,13 @@ Ok(
decl_spec: ( decl_spec: (
DeclSpec { DeclSpec {
ty: Int, ty: Int,
attrs: (empty), attrs: "(empty)",
}, },
19..22, 19..22,
), ),
declarator: ( declarator: (
Declarator { Declarator {
decl: Ident( decl: Ident(("qwq", 23..26)),
(
"qwq",
23..26,
),
),
pointer: false, pointer: false,
}, },
23..26, 23..26,
@ -74,12 +58,9 @@ Ok(
5..8, 5..8,
), ),
], ],
}, }),
),
body: [], body: [],
}, }),
),
1..30, 1..30,
), ),
], ])
)

View file

@ -1,27 +1,21 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[
( (
FunctionDef( FunctionDef(FunctionDef {
FunctionDef { declaration: Normal(NormalDecl {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: EXTERN | THREAD_LOCAL, attrs: "EXTERN | THREAD_LOCAL",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: WithParams { decl: WithParams {
ident: ( ident: ("uwu", 35..38),
"uwu",
35..38,
),
params: [], params: [],
}, },
pointer: false, pointer: false,
@ -31,12 +25,9 @@ Ok(
35..38, 35..38,
), ),
], ],
}, }),
),
body: [], body: [],
}, }),
),
1..43, 1..43,
), ),
], ])
)

View file

@ -1,27 +1,21 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[
( (
FunctionDef( FunctionDef(FunctionDef {
FunctionDef { declaration: Normal(NormalDecl {
declaration: Normal(
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Void, ty: Void,
attrs: (empty), attrs: "(empty)",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: WithParams { decl: WithParams {
ident: ( ident: ("uwu", 6..9),
"uwu",
6..9,
),
params: [], params: [],
}, },
pointer: false, pointer: false,
@ -31,12 +25,9 @@ Ok(
6..9, 6..9,
), ),
], ],
}, }),
),
body: [], body: [],
}, }),
),
1..14, 1..14,
), ),
], ])
)

View file

@ -1,27 +1,20 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[
( (
Decl( Decl(
Normal( Normal(NormalDecl {
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: (empty), attrs: "(empty)",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: Ident( decl: Ident(("test", 5..9)),
(
"test",
5..9,
),
),
pointer: false, pointer: false,
}, },
init: None, init: None,
@ -29,29 +22,22 @@ Ok(
5..9, 5..9,
), ),
], ],
}, }),
),
), ),
1..9, 1..9,
), ),
( (
Decl( Decl(
Normal( Normal(NormalDecl {
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Double, ty: Double,
attrs: THREAD_LOCAL, attrs: "THREAD_LOCAL",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: Ident( decl: Ident(("uwu", 32..35)),
(
"uwu",
32..35,
),
),
pointer: false, pointer: false,
}, },
init: None, init: None,
@ -61,12 +47,7 @@ Ok(
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: Ident( decl: Ident(("owo", 37..40)),
(
"owo",
37..40,
),
),
pointer: false, pointer: false,
}, },
init: None, init: None,
@ -74,28 +55,23 @@ Ok(
37..40, 37..40,
), ),
], ],
}, }),
),
), ),
11..40, 11..40,
), ),
( (
Decl( Decl(
Normal( Normal(NormalDecl {
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: (empty), attrs: "(empty)",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: WithParams { decl: WithParams {
ident: ( ident: ("function", 68..76),
"function",
68..76,
),
params: [], params: [],
}, },
pointer: false, pointer: false,
@ -105,10 +81,8 @@ Ok(
68..76, 68..76,
), ),
], ],
}, }),
),
), ),
64..76, 64..76,
), ),
], ])
)

View file

@ -1,63 +1,36 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[
( (
Decl( Decl(
Normal( Normal(NormalDecl {
NormalDecl {
decl_spec: DeclSpec { decl_spec: DeclSpec {
ty: Int, ty: Int,
attrs: (empty), attrs: "(empty)",
}, },
init_declarators: [ init_declarators: [
( (
InitDecl { InitDecl {
declarator: Declarator { declarator: Declarator {
decl: Ident( decl: Ident(("x", 5..6)),
(
"x",
5..6,
),
),
pointer: false, pointer: false,
}, },
init: Some( init: Some((
( Binary(ExprBinary {
Binary( lhs: (Atom(Int(1)), 9..10),
ExprBinary { rhs: (Atom(Int(1)), 13..14),
lhs: (
Atom(
Int(
1,
),
),
9..10,
),
rhs: (
Atom(
Int(
1,
),
),
13..14,
),
op: Add, op: Add,
}, }),
),
9..14, 9..14,
), )),
),
}, },
5..6, 5..6,
), ),
], ],
}, }),
),
), ),
1..6, 1..6,
), ),
], ])
)

View file

@ -9,20 +9,16 @@ fn lex_and_pre(src: &str) -> impl Iterator<Item = (Tok<'_>, Span)> + '_ {
} }
fn the_current_root_parse_thing<'src>(src: impl Iterator<Item = (Tok<'src>, Span)>) -> impl Debug { fn the_current_root_parse_thing<'src>(src: impl Iterator<Item = (Tok<'src>, Span)>) -> impl Debug {
use peekmore::PeekMore; super::parse_declarations(src)
let mut parser = Parser {
lex: src.peekmore(),
};
parser.external_declarations()
} }
macro_rules! parse_test { macro_rules! parse_test {
($src:expr) => { ($src:expr) => {
let lexer = lex_and_pre($src); let lexer = lex_and_pre($src);
let parsed = the_current_root_parse_thing(lexer); let parsed = super::parse_declarations(lexer);
insta::assert_debug_snapshot!(parsed); let parsed_pretty = dbg_pls::pretty(&parsed);
insta::assert_debug_snapshot!(parsed_pretty);
}; };
} }