Skip to content

Commit

Permalink
Pipeline-overridable constants
Browse files Browse the repository at this point in the history
  • Loading branch information
dannymcgee committed May 24, 2024
1 parent 26b0a05 commit 58fb892
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/grammar/src/lib/repository/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const keyword: TMGrammarScope = {
name: "keyword.control.$1.wgsl",
},
{
match: /\b(struct|fn|const|var|let|alias)\b/,
match: /\b(struct|fn|const|var|let|override|alias)\b/,
name: "storage.type.$1.wgsl",
},
{
Expand Down
46 changes: 46 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-001.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
================================================================================
tests::decl::override_decl / #1
================================================================================
(Decl::Const (VarDecl
attributes: (AttributeList
attributes: [
(Attribute
at_sign: `@` (Punct (1:1..1:2)),
name: `id` (Attribute (1:2..1:4)),
params: (ArgumentList
brace_open: `(` (Brace (1:4..1:5)),
arguments: [
(Expr::Primary (PrimaryExpr
expr: (Expr::Literal `0` (IntLiteral (1:5..1:6))),
)),
],
brace_close: `)` (Brace (1:6..1:7)),
),
),
],
),
storage: `override` (Keyword (1:8..1:16)),
name: `has_point_light` (Ident (1:17..1:32)),
ty: (TypeDecl
annotator: `:` (Punct (1:32..1:33)),
name: (IdentExpr::Leaf `bool` (Type (1:34..1:38))),
),
assignment: (Expr::Primary (PrimaryExpr
expr: (Expr::Literal `true` (Keyword (1:41..1:45))),
)),
semicolon: `;` (Punct (1:45..1:46)),
))
--- Tokens ---------------------------------------------------------------------
`@` (Punct (1:1..1:2))
`id` (Attribute (1:2..1:4))
`(` (Brace (1:4..1:5))
`0` (IntLiteral (1:5..1:6))
`)` (Brace (1:6..1:7))
`override` (Keyword (1:8..1:16))
`has_point_light` (Ident (1:17..1:32))
`:` (Punct (1:32..1:33))
`bool` (Type (1:34..1:38))
`=` (Operator (1:39..1:40))
`true` (Keyword (1:41..1:45))
`;` (Punct (1:45..1:46))
================================================================================
46 changes: 46 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-002.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
================================================================================
tests::decl::override_decl / #2
================================================================================
(Decl::Const (VarDecl
attributes: (AttributeList
attributes: [
(Attribute
at_sign: `@` (Punct (1:1..1:2)),
name: `id` (Attribute (1:2..1:4)),
params: (ArgumentList
brace_open: `(` (Brace (1:4..1:5)),
arguments: [
(Expr::Primary (PrimaryExpr
expr: (Expr::Literal `1200` (IntLiteral (1:5..1:9))),
)),
],
brace_close: `)` (Brace (1:9..1:10)),
),
),
],
),
storage: `override` (Keyword (1:11..1:19)),
name: `specular_param` (Ident (1:20..1:34)),
ty: (TypeDecl
annotator: `:` (Punct (1:34..1:35)),
name: (IdentExpr::Leaf `f32` (Type (1:36..1:39))),
),
assignment: (Expr::Primary (PrimaryExpr
expr: (Expr::Literal `2.3` (FloatLiteral (1:42..1:45))),
)),
semicolon: `;` (Punct (1:45..1:46)),
))
--- Tokens ---------------------------------------------------------------------
`@` (Punct (1:1..1:2))
`id` (Attribute (1:2..1:4))
`(` (Brace (1:4..1:5))
`1200` (IntLiteral (1:5..1:9))
`)` (Brace (1:9..1:10))
`override` (Keyword (1:11..1:19))
`specular_param` (Ident (1:20..1:34))
`:` (Punct (1:34..1:35))
`f32` (Type (1:36..1:39))
`=` (Operator (1:40..1:41))
`2.3` (FloatLiteral (1:42..1:45))
`;` (Punct (1:45..1:46))
================================================================================
41 changes: 41 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-003.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
================================================================================
tests::decl::override_decl / #3
================================================================================
(Decl::Const (VarDecl
attributes: (AttributeList
attributes: [
(Attribute
at_sign: `@` (Punct (1:1..1:2)),
name: `id` (Attribute (1:2..1:4)),
params: (ArgumentList
brace_open: `(` (Brace (1:4..1:5)),
arguments: [
(Expr::Primary (PrimaryExpr
expr: (Expr::Literal `1300` (IntLiteral (1:5..1:9))),
)),
],
brace_close: `)` (Brace (1:9..1:10)),
),
),
],
),
storage: `override` (Keyword (1:11..1:19)),
name: `gain` (Ident (1:20..1:24)),
ty: (TypeDecl
annotator: `:` (Punct (1:24..1:25)),
name: (IdentExpr::Leaf `f32` (Type (1:26..1:29))),
),
semicolon: `;` (Punct (1:29..1:30)),
))
--- Tokens ---------------------------------------------------------------------
`@` (Punct (1:1..1:2))
`id` (Attribute (1:2..1:4))
`(` (Brace (1:4..1:5))
`1300` (IntLiteral (1:5..1:9))
`)` (Brace (1:9..1:10))
`override` (Keyword (1:11..1:19))
`gain` (Ident (1:20..1:24))
`:` (Punct (1:24..1:25))
`f32` (Type (1:26..1:29))
`;` (Punct (1:29..1:30))
================================================================================
24 changes: 24 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-004.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
================================================================================
tests::decl::override_decl / #4
================================================================================
(Decl::Const (VarDecl
storage: `override` (Keyword (1:1..1:9)),
name: `width` (Ident (1:10..1:15)),
ty: (TypeDecl
annotator: `:` (Punct (1:15..1:16)),
name: (IdentExpr::Leaf `f32` (Type (1:17..1:20))),
),
assignment: (Expr::Primary (PrimaryExpr
expr: (Expr::Literal `0.0` (FloatLiteral (1:23..1:26))),
)),
semicolon: `;` (Punct (1:26..1:27)),
))
--- Tokens ---------------------------------------------------------------------
`override` (Keyword (1:1..1:9))
`width` (Ident (1:10..1:15))
`:` (Punct (1:15..1:16))
`f32` (Type (1:17..1:20))
`=` (Operator (1:21..1:22))
`0.0` (FloatLiteral (1:23..1:26))
`;` (Punct (1:26..1:27))
================================================================================
19 changes: 19 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-005.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
================================================================================
tests::decl::override_decl / #5
================================================================================
(Decl::Const (VarDecl
storage: `override` (Keyword (1:1..1:9)),
name: `depth` (Ident (1:10..1:15)),
ty: (TypeDecl
annotator: `:` (Punct (1:15..1:16)),
name: (IdentExpr::Leaf `f32` (Type (1:17..1:20))),
),
semicolon: `;` (Punct (1:20..1:21)),
))
--- Tokens ---------------------------------------------------------------------
`override` (Keyword (1:1..1:9))
`depth` (Ident (1:10..1:15))
`:` (Punct (1:15..1:16))
`f32` (Type (1:17..1:20))
`;` (Punct (1:20..1:21))
================================================================================
26 changes: 26 additions & 0 deletions packages/parser/snapshots/tests/decl/override_decl-006.wgsl-ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
================================================================================
tests::decl::override_decl / #6
================================================================================
(Decl::Const (VarDecl
storage: `override` (Keyword (1:1..1:9)),
name: `height` (Ident (1:10..1:16)),
assignment: (Expr::Binary (BinaryExpr
lhs: (Expr::Primary (PrimaryExpr
expr: (Expr::Literal `2` (IntLiteral (1:19..1:20))),
)),
op: `*` (Operator (1:21..1:22)),
rhs: (Expr::Primary (PrimaryExpr
expr: (Expr::Ident (IdentExpr::Leaf `depth` (Ident (1:23..1:28)))),
)),
)),
semicolon: `;` (Punct (1:28..1:29)),
))
--- Tokens ---------------------------------------------------------------------
`override` (Keyword (1:1..1:9))
`height` (Ident (1:10..1:16))
`=` (Operator (1:17..1:18))
`2` (IntLiteral (1:19..1:20))
`*` (Operator (1:21..1:22))
`depth` (Ident (1:23..1:28))
`;` (Punct (1:28..1:29))
================================================================================
5 changes: 3 additions & 2 deletions packages/parser/src/decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Parse for Decl {
}
}
(Keyword, "var", _) => Ok(Decl::Var(input.parse()?)),
(Keyword, "let" | "const", _) => Ok(Decl::Const(input.parse()?)),
(Keyword, "let" | "const" | "override", _) => Ok(Decl::Const(input.parse()?)),
(Keyword, "alias", _) => Ok(Decl::TypeAlias(input.parse()?)),
(Keyword, "struct", _) => Ok(Decl::Struct(input.parse()?)),
(Keyword, "fn", _) => Ok(Decl::Function(input.parse()?)),
Expand All @@ -123,7 +123,8 @@ impl Parse for Decl {
(Directive, "#import", _) => Ok(Decl::Import(input.parse()?)),
(Keyword, "import", _) => Ok(Decl::Module(input.parse()?)),
(_, _, span) => Err(SpannedError {
message: "Expected `var`, `let`, `const`, `alias`, `struct`, `fn`, `enable`, or `import`"
message: "Expected `var`, `let`, `const`, `override`, `alias`, \
`struct`, `fn`, `enable`, or `import`"
.into(),
span: Some(span),
source: input.source(),
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/decl/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Parse for VarDecl {
let storage = input.consume_kind(TokenKind::Keyword)?;
assert!(matches!(
storage.as_matchable(),
(TokenKind::Keyword, "let" | "const" | "var", _)
(TokenKind::Keyword, "let" | "const" | "override" | "var", _)
));

let storage_qualifiers = if input.check(operator![<]) {
Expand Down
10 changes: 10 additions & 0 deletions packages/parser/src/tests/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ fn var_decl() {
snapshot!(Decl(WithTokens) "const PI: f32 = 3.14159;");
}

#[snapshot_test]
fn override_decl() {
snapshot!(Decl(WithTokens) "@id(0) override has_point_light: bool = true;");
snapshot!(Decl(WithTokens) "@id(1200) override specular_param: f32 = 2.3;");
snapshot!(Decl(WithTokens) "@id(1300) override gain: f32;");
snapshot!(Decl(WithTokens) "override width: f32 = 0.0;");
snapshot!(Decl(WithTokens) "override depth: f32;");
snapshot!(Decl(WithTokens) "override height = 2 * depth;");
}

#[snapshot_test]
fn type_alias_decl() {
snapshot! {
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum Token {
PreprocessorInsertion(Substr, Span),

#[subset_of(Ident)]
#[pattern = r"(const|fn|let|struct|alias|var|export)"]
#[pattern = r"(const|fn|let|struct|alias|var|override|export)"]
#[pattern = r"(function|private|read(_write)?|storage|uniform|workgroup|write)"]
#[pattern = r"(break|case|continu(e|ing)|default|else|fallthrough|for|if|loop|return|switch|from)"]
#[pattern = r"(true|false)"]
Expand Down

0 comments on commit 58fb892

Please sign in to comment.