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

[wgsl-in] implement block comments #1675

Merged
merged 1 commit into from
Jan 19, 2022
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
54 changes: 50 additions & 4 deletions src/front/wgsl/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,47 @@ fn consume_token(mut input: &str, generic: bool) -> (Token<'_>, &str) {
(Token::UnterminatedString, quote_content)
}
}
'/' if chars.as_str().starts_with('/') => {
let _ = chars.position(|c| c == '\n' || c == '\r');
(Token::Trivia, chars.as_str())
'/' => {
input = chars.as_str();
match chars.next() {
Some('/') => {
let _ = chars.position(|c| c == '\n' || c == '\r');
(Token::Trivia, chars.as_str())
}
Some('*') => {
input = chars.as_str();

let mut depth = 1;
let mut prev = '\0';

for c in &mut chars {
match (prev, c) {
('*', '/') => {
prev = '\0';
depth -= 1;
if depth == 0 {
break;
}
}
('/', '*') => {
prev = '\0';
depth += 1;
}
_ => {
prev = c;
}
}
}

if depth > 0 {
(Token::UnterminatedBlockComment, input)
} else {
(Token::Trivia, chars.as_str())
}
}
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()),
_ => (Token::Operation(cur), input),
}
}
'-' => {
let sub_input = chars.as_str();
Expand All @@ -369,7 +407,7 @@ fn consume_token(mut input: &str, generic: bool) -> (Token<'_>, &str) {
_ => (Token::Operation(cur), sub_input),
}
}
'+' | '*' | '/' | '%' | '^' => {
'+' | '*' | '%' | '^' => {
input = chars.as_str();
if chars.next() == Some('=') {
(Token::AssignmentOperation(cur), chars.as_str())
Expand Down Expand Up @@ -673,6 +711,14 @@ fn test_tokens() {
sub_test("No好", &[Token::Word("No"), Token::Unknown('好')]);
sub_test("_No", &[Token::Word("_No")]);
sub_test("\"\u{2}ПЀ\u{0}\"", &[Token::String("\u{2}ПЀ\u{0}")]); // https://github.com/gfx-rs/naga/issues/90
sub_test(
"*/*/***/*//=/*****//",
&[
Token::Operation('*'),
Token::AssignmentOperation('/'),
Token::Operation('/'),
],
);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub enum Token<'a> {
Arrow,
Unknown(char),
UnterminatedString,
UnterminatedBlockComment,
Trivia,
End,
}
Expand Down Expand Up @@ -204,6 +205,7 @@ impl<'a> Error<'a> {
Token::Arrow => "->".to_string(),
Token::Unknown(c) => format!("unknown ('{}')", c),
Token::UnterminatedString => "unterminated string".to_string(),
Token::UnterminatedBlockComment => "unterminated block comment".to_string(),
Token::Trivia => "trivia".to_string(),
Token::End => "end".to_string(),
}
Expand Down