Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type alias parsing; harden parser tests #41

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::function_decl / #1
================================================================================
(FunctionDecl
(Decl::Function (FunctionDecl
storage: `fn` (Keyword (2:3..2:5)),
name: `main` (Function (2:6..2:10)),
params: [
Expand Down Expand Up @@ -68,7 +68,7 @@ tests::decl::function_decl / #1
],
brace_close: `}` (Brace (4:3..4:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`fn` (Keyword (2:3..2:5))
`main` (Function (2:6..2:10))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::function_decl / #2
================================================================================
(FunctionDecl
(Decl::Function (FunctionDecl
storage: `fn` (Keyword (2:3..2:5)),
name: `main` (Function (2:6..2:10)),
params: [
Expand Down Expand Up @@ -107,7 +107,7 @@ tests::decl::function_decl / #2
],
brace_close: `}` (Brace (6:3..6:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`fn` (Keyword (2:3..2:5))
`main` (Function (2:6..2:10))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::function_decl / #3
================================================================================
(FunctionDecl
(Decl::Function (FunctionDecl
storage: `fn` (Keyword (2:3..2:5)),
name: `mul` (Function (2:6..2:9)),
params: [
Expand Down Expand Up @@ -43,7 +43,7 @@ tests::decl::function_decl / #3
],
brace_close: `}` (Brace (4:3..4:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`fn` (Keyword (2:3..2:5))
`mul` (Function (2:6..2:9))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/struct_decl-001.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::struct_decl / #1
================================================================================
(StructDecl
(Decl::Struct (StructDecl
storage: `struct` (Keyword (2:3..2:9)),
name: `Data` (Struct (2:10..2:14)),
body: (StructBody
Expand Down Expand Up @@ -29,7 +29,7 @@ tests::decl::struct_decl / #1
],
close_brace: `}` (Brace (5:3..5:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`struct` (Keyword (2:3..2:9))
`Data` (Struct (2:10..2:14))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/struct_decl-002.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::struct_decl / #2
================================================================================
(StructDecl
(Decl::Struct (StructDecl
storage: `struct` (Keyword (2:3..2:9)),
name: `VertexOutput` (Struct (2:10..2:22)),
body: (StructBody
Expand Down Expand Up @@ -122,7 +122,7 @@ tests::decl::struct_decl / #2
],
close_brace: `}` (Brace (7:3..7:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`struct` (Keyword (2:3..2:9))
`VertexOutput` (Struct (2:10..2:22))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/struct_decl-003.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::struct_decl / #3
================================================================================
(StructDecl
(Decl::Struct (StructDecl
storage: `struct` (Keyword (2:3..2:9)),
name: `View` (Struct (2:10..2:14)),
body: (StructBody
Expand Down Expand Up @@ -113,7 +113,7 @@ tests::decl::struct_decl / #3
],
close_brace: `}` (Brace (12:3..12:4)),
),
)
))
--- Tokens ---------------------------------------------------------------------
`struct` (Keyword (2:3..2:9))
`View` (Struct (2:10..2:14))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::type_alias_decl / #1
================================================================================
(TypeAliasDecl
(Decl::TypeAlias (TypeAliasDecl
storage: `alias` (Keyword (1:1..1:6)),
name: `ViewProjectionMatrix` (Ident (1:7..1:27)),
eq: `=` (Operator (1:28..1:29)),
Expand All @@ -12,7 +12,7 @@ tests::decl::type_alias_decl / #1
),
),
semicolon: `;` (Punct (1:41..1:42)),
)
))
--- Tokens ---------------------------------------------------------------------
`alias` (Keyword (1:1..1:6))
`ViewProjectionMatrix` (Ident (1:7..1:27))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-001.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
================================================================================
tests::decl::var_decl / #1
================================================================================
(VarDecl
(Decl::Const (VarDecl
storage: `let` (Keyword (1:1..1:4)),
name: `foo` (Ident (1:5..1:8)),
assignment: (Expr::Primary (PrimaryExpr
expr: (Expr::Literal `1.0` (FloatLiteral (1:11..1:14))),
)),
semicolon: `;` (Punct (1:14..1:15)),
)
))
--- Tokens ---------------------------------------------------------------------
`let` (Keyword (1:1..1:4))
`foo` (Ident (1:5..1:8))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-002.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::var_decl / #2
================================================================================
(VarDecl
(Decl::Var (VarDecl
storage: `var` (Keyword (1:1..1:4)),
name: `foo` (Ident (1:5..1:8)),
ty: (TypeDecl
Expand All @@ -12,7 +12,7 @@ tests::decl::var_decl / #2
expr: (Expr::Literal `1.0` (FloatLiteral (1:16..1:19))),
)),
semicolon: `;` (Punct (1:19..1:20)),
)
))
--- Tokens ---------------------------------------------------------------------
`var` (Keyword (1:1..1:4))
`foo` (Ident (1:5..1:8))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-003.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
================================================================================
tests::decl::var_decl / #3
================================================================================
(VarDecl
(Decl::Var (VarDecl
storage: `var` (Keyword (1:1..1:4)),
name: `foo` (Ident (1:5..1:8)),
ty: (TypeDecl
annotator: `:` (Punct (1:8..1:9)),
name: (IdentExpr::Leaf `f32` (Type (1:10..1:13))),
),
semicolon: `;` (Punct (1:13..1:14)),
)
))
--- Tokens ---------------------------------------------------------------------
`var` (Keyword (1:1..1:4))
`foo` (Ident (1:5..1:8))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-004.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::var_decl / #4
================================================================================
(VarDecl
(Decl::Var (VarDecl
storage: `var` (Keyword (1:1..1:4)),
storage_qualifiers: [
`uniform` (Keyword (1:5..1:12)),
Expand All @@ -12,7 +12,7 @@ tests::decl::var_decl / #4
name: (IdentExpr::Leaf `Uniforms` (Struct (1:24..1:32))),
),
semicolon: `;` (Punct (1:32..1:33)),
)
))
--- Tokens ---------------------------------------------------------------------
`var` (Keyword (1:1..1:4))
`<` (Operator (1:4..1:5))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-005.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::var_decl / #5
================================================================================
(VarDecl
(Decl::Var (VarDecl
storage: `var` (Keyword (1:1..1:4)),
storage_qualifiers: [
`uniform` (Keyword (1:5..1:12)),
Expand All @@ -15,7 +15,7 @@ tests::decl::var_decl / #5
)),
),
semicolon: `;` (Punct (1:40..1:41)),
)
))
--- Tokens ---------------------------------------------------------------------
`var` (Keyword (1:1..1:4))
`<` (Operator (1:4..1:5))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/snapshots/tests/decl/var_decl-006.wgsl-ast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
================================================================================
tests::decl::var_decl / #6
================================================================================
(VarDecl
(Decl::Const (VarDecl
storage: `const` (Keyword (1:1..1:6)),
name: `PI` (Ident (1:7..1:9)),
ty: (TypeDecl
Expand All @@ -12,7 +12,7 @@ tests::decl::var_decl / #6
expr: (Expr::Literal `3.14159` (FloatLiteral (1:17..1:24))),
)),
semicolon: `;` (Punct (1:24..1:25)),
)
))
--- Tokens ---------------------------------------------------------------------
`const` (Keyword (1:1..1:6))
`PI` (Ident (1:7..1:9))
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ impl Parse for Decl {
}
(Keyword, "var", _) => Ok(Decl::Var(input.parse()?)),
(Keyword, "let" | "const", _) => Ok(Decl::Const(input.parse()?)),
(Keyword, "type", _) => Ok(Decl::TypeAlias(input.parse()?)),
(Keyword, "alias", _) => Ok(Decl::TypeAlias(input.parse()?)),
(Keyword, "struct", _) => Ok(Decl::Struct(input.parse()?)),
(Keyword, "fn", _) => Ok(Decl::Function(input.parse()?)),
(Keyword, "enable", _) => Ok(Decl::Extension(input.parse()?)),
(Directive, "#define_import_path", _) => Ok(Decl::ImportPath(input.parse()?)),
(Directive, "#import", _) => Ok(Decl::Import(input.parse()?)),
(Keyword, "import", _) => Ok(Decl::Module(input.parse()?)),
(_, _, span) => Err(SpannedError {
message: "Expected `var`, `let`, `const`, `type`, `struct`, `fn`, `enable`, or `import`"
message: "Expected `var`, `let`, `const`, `alias`, `struct`, `fn`, `enable`, or `import`"
.into(),
span: Some(span),
source: input.source(),
Expand Down
28 changes: 14 additions & 14 deletions packages/parser/src/tests/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ use super::token;
begin_snapshots!();

use crate::{
decl::{Decl, FunctionDecl, StructDecl, TypeAliasDecl, VarDecl},
decl::Decl,
modules::ImportedSymbol,
utils::{WithComments, WithTokens},
ParseStream,
};

#[snapshot_test]
fn var_decl() {
snapshot!(VarDecl(WithTokens) "let foo = 1.0;");
snapshot!(VarDecl(WithTokens) "var foo: f32 = 1.0;");
snapshot!(VarDecl(WithTokens) "var foo: f32;");
snapshot!(VarDecl(WithTokens) "var<uniform> uniforms: Uniforms;");
snapshot!(VarDecl(WithTokens) "var<uniform> uniforms: common::Uniforms;");
snapshot!(VarDecl(WithTokens) "const PI: f32 = 3.14159;");
snapshot!(Decl(WithTokens) "let foo = 1.0;");
snapshot!(Decl(WithTokens) "var foo: f32 = 1.0;");
snapshot!(Decl(WithTokens) "var foo: f32;");
snapshot!(Decl(WithTokens) "var<uniform> uniforms: Uniforms;");
snapshot!(Decl(WithTokens) "var<uniform> uniforms: common::Uniforms;");
snapshot!(Decl(WithTokens) "const PI: f32 = 3.14159;");
}

#[snapshot_test]
fn type_alias_decl() {
snapshot! {
TypeAliasDecl(WithTokens)
Decl(WithTokens)
"alias ViewProjectionMatrix = mat4x4<f32>;"
};
}

#[snapshot_test]
fn struct_decl() {
// With semicolon
snapshot!(StructDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
struct Data {
a: i32,
b: vec2<f32>,
};
"#);

// Without semicolon
snapshot!(StructDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
Expand All @@ -52,7 +52,7 @@ fn struct_decl() {
}
"#);

snapshot!(StructDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
Expand All @@ -69,21 +69,21 @@ fn struct_decl() {

#[snapshot_test]
fn function_decl() {
snapshot!(FunctionDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
fn main() -> @location(0) vec4<f32> {
return vec4<f32>(0.4, 0.4, 0.8, 1.0);
}
"#);

snapshot!(FunctionDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
fn main(
@builtin(position) coord_in: vec4<f32>,
) -> @location(0) vec4<f32> {
return vec4<f32>(coord_in.x, coord_in.y, 0.0, 1.0);
}
"#);

snapshot!(FunctionDecl(WithTokens) r#"
snapshot!(Decl(WithTokens) r#"
fn mul(a: f32, b: f32) -> f32 {
return a * b;
}
Expand Down