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,85 +1,66 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[ (
( FunctionDef(FunctionDef {
FunctionDef( declaration: Normal(NormalDecl {
FunctionDef { decl_spec: DeclSpec {
declaration: Normal( ty: Int,
NormalDecl { attrs: "(empty)",
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: [],
}, },
), init_declarators: [
1..30, (
), 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,
),
])

View file

@ -1,42 +1,33 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[ (
( FunctionDef(FunctionDef {
FunctionDef( declaration: Normal(NormalDecl {
FunctionDef { decl_spec: DeclSpec {
declaration: Normal( ty: Int,
NormalDecl { attrs: "EXTERN | THREAD_LOCAL",
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: [],
}, },
), init_declarators: [
1..43, (
), InitDecl {
], declarator: Declarator {
) decl: WithParams {
ident: ("uwu", 35..38),
params: [],
},
pointer: false,
},
init: None,
},
35..38,
),
],
}),
body: [],
}),
1..43,
),
])

View file

@ -1,42 +1,33 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[ (
( FunctionDef(FunctionDef {
FunctionDef( declaration: Normal(NormalDecl {
FunctionDef { decl_spec: DeclSpec {
declaration: Normal( ty: Void,
NormalDecl { attrs: "(empty)",
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: [],
}, },
), init_declarators: [
1..14, (
), InitDecl {
], declarator: Declarator {
) decl: WithParams {
ident: ("uwu", 6..9),
params: [],
},
pointer: false,
},
init: None,
},
6..9,
),
],
}),
body: [],
}),
1..14,
),
])

View file

@ -1,114 +1,88 @@
--- ---
source: parser/src/parser/tests.rs source: parser/src/parser/tests.rs
expression: parsed expression: parsed_pretty
--- ---
Ok( Ok([
[ (
( Decl(
Decl( Normal(NormalDecl {
Normal( decl_spec: DeclSpec {
NormalDecl { ty: Int,
decl_spec: DeclSpec { attrs: "(empty)",
ty: Int, },
attrs: (empty), init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: Ident(("test", 5..9)),
pointer: false,
},
init: None,
}, },
init_declarators: [ 5..9,
( ),
InitDecl { ],
declarator: Declarator { }),
decl: Ident(
(
"test",
5..9,
),
),
pointer: false,
},
init: None,
},
5..9,
),
],
},
),
),
1..9,
), ),
( 1..9,
Decl( ),
Normal( (
NormalDecl { Decl(
decl_spec: DeclSpec { Normal(NormalDecl {
ty: Double, decl_spec: DeclSpec {
attrs: THREAD_LOCAL, ty: Double,
attrs: "THREAD_LOCAL",
},
init_declarators: [
(
InitDecl {
declarator: Declarator {
decl: Ident(("uwu", 32..35)),
pointer: false,
},
init: None,
}, },
init_declarators: [ 32..35,
( ),
InitDecl { (
declarator: Declarator { InitDecl {
decl: Ident( declarator: Declarator {
( decl: Ident(("owo", 37..40)),
"uwu", pointer: false,
32..35, },
), init: None,
),
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),
}, },
init_declarators: [ 37..40,
( ),
InitDecl { ],
declarator: Declarator { }),
decl: WithParams {
ident: (
"function",
68..76,
),
params: [],
},
pointer: false,
},
init: None,
},
68..76,
),
],
},
),
),
64..76,
), ),
], 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,
),
])

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(NormalDecl {
Normal( decl_spec: DeclSpec {
NormalDecl { ty: Int,
decl_spec: DeclSpec { attrs: "(empty)",
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: [ 5..6,
( ),
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,
), ),
], 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);
}; };
} }