Skip to content

Commit

Permalink
implement block comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsis1david committed Jan 19, 2022
1 parent a1840be commit 4315d79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/front/wgsl/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,41 @@ 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;
while let Some(c) = chars.next() {
match c {
'*' if chars.next() == Some('/') => {
depth -= 1;
if depth == 0 {
break;
}
}
'/' if chars.next() == Some('*') => {
depth += 1;
}
_ => {}
}
}

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 +401,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
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

0 comments on commit 4315d79

Please sign in to comment.