Skip to content

Commit 4a8d1c0

Browse files
committed
small cleanup and handle escaped quotes in WGSL string lexing
1 parent 8ee2c0e commit 4a8d1c0

13 files changed

Lines changed: 56 additions & 299 deletions

File tree

docs/api-specs/debug_printf.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ This is a debugging extension and is not part of core WebGPU.
1212
- Vulkan with `VK_KHR_shader_non_semantic_info` support.
1313

1414
On Vulkan, `debugPrintf` output is produced through the validation layer debug-printf path. It is not enabled when GPU-assisted validation is enabled, because the two validation features are mutually exclusive.
15+
To receive Vulkan `debugPrintf` output:
16+
17+
- Install the Vulkan SDK.
18+
- Request `InstanceFlags::VALIDATION`.
19+
- Do not request `InstanceFlags::GPU_ASSISTED_VALIDATION`.
20+
- Listen for log messages at the `Info` level.
1521

1622
## WGSL Syntax
1723

naga/src/front/wgsl/parse/lexer.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ fn consume_any(input: &str, what: impl Fn(char) -> bool) -> (&str, &str) {
106106
input.split_at(pos)
107107
}
108108

109+
fn find_string_literal_end(input: &str) -> Option<usize> {
110+
let mut escaped = false;
111+
for (index, c) in input.char_indices() {
112+
if escaped {
113+
escaped = false;
114+
} else if c == '\\' {
115+
escaped = true;
116+
} else if c == '"' {
117+
return Some(index);
118+
}
119+
}
120+
None
121+
}
122+
109123
struct UnclosedCandidate {
110124
index: usize,
111125
depth: usize,
@@ -273,17 +287,14 @@ fn consume_token(
273287
None => return (Token::End, ""),
274288
};
275289
match cur {
276-
'"' => {
277-
// Find the next quote in the remaining string
278-
match chars.as_str().find('"') {
279-
Some(len) => {
280-
let content = &chars.as_str()[..len];
281-
let rest = &chars.as_str()[len + 1..];
282-
(Token::String(content), rest)
283-
}
284-
None => (Token::Unknown('"'), chars.as_str()),
290+
'"' => match find_string_literal_end(chars.as_str()) {
291+
Some(len) => {
292+
let content = &chars.as_str()[..len];
293+
let rest = &chars.as_str()[len + 1..];
294+
(Token::String(content), rest)
285295
}
286-
}
296+
None => (Token::Unknown('"'), chars.as_str()),
297+
},
287298
':' | ';' | ',' => (Token::Separator(cur), chars.as_str()),
288299
'.' => {
289300
let og_chars = chars.as_str();
@@ -978,6 +989,22 @@ fn test_tokens() {
978989
sub_test("No¾", &[Token::Word("No"), Token::Unknown('¾')]);
979990
sub_test("No好", &[Token::Word("No好")]);
980991
sub_test("_No", &[Token::Word("_No")]);
992+
sub_test(
993+
r#""debug \"value\": %d", next"#,
994+
&[
995+
Token::String(r#"debug \"value\": %d"#),
996+
Token::Separator(','),
997+
Token::Word("next"),
998+
],
999+
);
1000+
sub_test(
1001+
r#""debug\\", next"#,
1002+
&[
1003+
Token::String(r#"debug\\"#),
1004+
Token::Separator(','),
1005+
Token::Word("next"),
1006+
],
1007+
);
9811008

9821009
sub_test_with_and_without_doc_comments(
9831010
"*/*/***/*//=/*****//",

naga/src/valid/function.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ pub enum FunctionError {
236236
MismatchedPayloadType(Handle<crate::Type>, Handle<crate::Type>),
237237
#[error("The payload passed to `traceRay` must be a pointer directly to a global variable")]
238238
PayloadPointerNotGlobal,
239-
#[error("Expression {0:?} is used before it is defined/emitted")]
240-
InvalidExpression(Handle<crate::Expression>),
241239
#[error("Argument {0:?} for `debugPrintf` must be a supported scalar type")]
242240
InvalidDebugPrintfArgument(Handle<crate::Expression>),
243241
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#version 460
2+
#extension GL_EXT_debug_printf : require
3+
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
4+
5+
void main()
6+
{
7+
debugPrintfEXT("debug id: %u %u %u", gl_GlobalInvocationID.x, gl_GlobalInvocationID.y, gl_GlobalInvocationID.z);
8+
}
9+

repro.air

-4.03 KB
Binary file not shown.

repro.ll

Lines changed: 0 additions & 113 deletions
This file was deleted.

test.asm

Lines changed: 0 additions & 66 deletions
This file was deleted.

test.metal

Lines changed: 0 additions & 63 deletions
This file was deleted.

test.spv

-2.22 KB
Binary file not shown.

test.wgsl

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)