| 1 |
use super::{number::consume_number, Error, ExpectedToken, Result}; |
| 2 |
use crate::front::wgsl::error::NumberError; |
| 3 |
use crate::front::wgsl::parse::directive::enable_extension::EnableExtensions; |
| 4 |
use crate::front::wgsl::parse::{conv, Number}; |
| 5 |
use crate::front::wgsl::Scalar; |
| 6 |
use crate::Span; |
| 7 |
|
| 8 |
use alloc::boxed::Box; |
| 9 |
|
| 10 |
type TokenSpan<'a> = (Token<'a>, Span); |
| 11 |
|
| 12 |
#[derive(Copy, Clone, Debug, PartialEq)] |
| 13 |
pub enum Token<'a> { |
| 14 |
Separator(char), |
| 15 |
Paren(char), |
| 16 |
Attribute, |
| 17 |
Number(core::result::Result<Number, NumberError>), |
| 18 |
Word(&'a str), |
| 19 |
Operation(char), |
| 20 |
LogicalOperation(char), |
| 21 |
ShiftOperation(char), |
| 22 |
AssignmentOperation(char), |
| 23 |
IncrementOperation, |
| 24 |
DecrementOperation, |
| 25 |
Arrow, |
| 26 |
Unknown(char), |
| 27 |
Trivia, |
| 28 |
End, |
| 29 |
} |
| 30 |
|
| 31 |
fn consume_any(input: &str, what: impl Fn(char) -> bool) -> (&str, &str) { |
| 32 |
let pos = input.find(|c| !what(c)).unwrap_or(input.len()); |
| 33 |
input.split_at(pos) |
| 34 |
} |
| 35 |
|
| 36 |
/// Return the token at the start of `input`. |
| 37 |
/// |
| 38 |
/// If `generic` is `false`, then the bit shift operators `>>` or `<<` |
| 39 |
/// are valid lookahead tokens for the current parser state (see [§3.1 |
| 40 |
/// Parsing] in the WGSL specification). In other words: |
| 41 |
/// |
| 42 |
/// - If `generic` is `true`, then we are expecting an angle bracket |
| 43 |
/// around a generic type parameter, like the `<` and `>` in |
| 44 |
/// `vec3<f32>`, so interpret `<` and `>` as `Token::Paren` tokens, |
| 45 |
/// even if they're part of `<<` or `>>` sequences. |
| 46 |
/// |
| 47 |
/// - Otherwise, interpret `<<` and `>>` as shift operators: |
| 48 |
/// `Token::LogicalOperation` tokens. |
| 49 |
/// |
| 50 |
/// [§3.1 Parsing]: https://gpuweb.github.io/gpuweb/wgsl/#parsing |
| 51 |
fn consume_token(input: &str, generic: bool) -> (Token<'_>, &str) { |
| 52 |
let mut chars = input.chars(); |
| 53 |
let cur = match chars.next() { |
| 54 |
Some(c) => c, |
| 55 |
None => return (Token::End, ""), |
| 56 |
}; |
| 57 |
match cur { |
| 58 |
':' | ';' | ',' => (Token::Separator(cur), chars.as_str()), |
| 59 |
'.' => { |
| 60 |
let og_chars = chars.as_str(); |
| 61 |
match chars.next() { |
| 62 |
Some('0'..='9') => consume_number(input), |
| 63 |
_ => (Token::Separator(cur), og_chars), |
| 64 |
} |
| 65 |
} |
| 66 |
'@' => (Token::Attribute, chars.as_str()), |
| 67 |
'(' | ')' | '{' | '}' | '[' | ']' => (Token::Paren(cur), chars.as_str()), |
| 68 |
'<' | '>' => { |
| 69 |
let og_chars = chars.as_str(); |
| 70 |
match chars.next() { |
| 71 |
Some('=') if !generic => (Token::LogicalOperation(cur), chars.as_str()), |
| 72 |
Some(c) if c == cur && !generic => { |
| 73 |
let og_chars = chars.as_str(); |
| 74 |
match chars.next() { |
| 75 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 76 |
_ => (Token::ShiftOperation(cur), og_chars), |
| 77 |
} |
| 78 |
} |
| 79 |
_ => (Token::Paren(cur), og_chars), |
| 80 |
} |
| 81 |
} |
| 82 |
'0'..='9' => consume_number(input), |
| 83 |
'/' => { |
| 84 |
let og_chars = chars.as_str(); |
| 85 |
match chars.next() { |
| 86 |
Some('/') => { |
| 87 |
let _ = chars.position(is_comment_end); |
| 88 |
(Token::Trivia, chars.as_str()) |
| 89 |
} |
| 90 |
Some('*') => { |
| 91 |
let mut depth = 1; |
| 92 |
let mut prev = None; |
| 93 |
|
| 94 |
for c in &mut chars { |
| 95 |
match (prev, c) { |
| 96 |
(Some('*'), '/') => { |
| 97 |
prev = None; |
| 98 |
depth -= 1; |
| 99 |
if depth == 0 { |
| 100 |
return (Token::Trivia, chars.as_str()); |
| 101 |
} |
| 102 |
} |
| 103 |
(Some('/'), '*') => { |
| 104 |
prev = None; |
| 105 |
depth += 1; |
| 106 |
} |
| 107 |
_ => { |
| 108 |
prev = Some(c); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
(Token::End, "") |
| 114 |
} |
| 115 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 116 |
_ => (Token::Operation(cur), og_chars), |
| 117 |
} |
| 118 |
} |
| 119 |
'-' => { |
| 120 |
let og_chars = chars.as_str(); |
| 121 |
match chars.next() { |
| 122 |
Some('>') => (Token::Arrow, chars.as_str()), |
| 123 |
Some('-') => (Token::DecrementOperation, chars.as_str()), |
| 124 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 125 |
_ => (Token::Operation(cur), og_chars), |
| 126 |
} |
| 127 |
} |
| 128 |
'+' => { |
| 129 |
let og_chars = chars.as_str(); |
| 130 |
match chars.next() { |
| 131 |
Some('+') => (Token::IncrementOperation, chars.as_str()), |
| 132 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 133 |
_ => (Token::Operation(cur), og_chars), |
| 134 |
} |
| 135 |
} |
| 136 |
'*' | '%' | '^' => { |
| 137 |
let og_chars = chars.as_str(); |
| 138 |
match chars.next() { |
| 139 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 140 |
_ => (Token::Operation(cur), og_chars), |
| 141 |
} |
| 142 |
} |
| 143 |
'~' => (Token::Operation(cur), chars.as_str()), |
| 144 |
'=' | '!' => { |
| 145 |
let og_chars = chars.as_str(); |
| 146 |
match chars.next() { |
| 147 |
Some('=') => (Token::LogicalOperation(cur), chars.as_str()), |
| 148 |
_ => (Token::Operation(cur), og_chars), |
| 149 |
} |
| 150 |
} |
| 151 |
'&' | '|' => { |
| 152 |
let og_chars = chars.as_str(); |
| 153 |
match chars.next() { |
| 154 |
Some(c) if c == cur => (Token::LogicalOperation(cur), chars.as_str()), |
| 155 |
Some('=') => (Token::AssignmentOperation(cur), chars.as_str()), |
| 156 |
_ => (Token::Operation(cur), og_chars), |
| 157 |
} |
| 158 |
} |
| 159 |
_ if is_blankspace(cur) => { |
| 160 |
let (_, rest) = consume_any(input, is_blankspace); |
| 161 |
(Token::Trivia, rest) |
| 162 |
} |
| 163 |
_ if is_word_start(cur) => { |
| 164 |
let (word, rest) = consume_any(input, is_word_part); |
| 165 |
(Token::Word(word), rest) |
| 166 |
} |
| 167 |
_ => (Token::Unknown(cur), chars.as_str()), |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/// Returns whether or not a char is a comment end |
| 172 |
/// (Unicode Pattern_White_Space excluding U+0020, U+0009, U+200E and U+200F) |
| 173 |
const fn is_comment_end(c: char) -> bool { |
| 174 |
match c { |
| 175 |
'\u{000a}'..='\u{000d}' | '\u{0085}' | '\u{2028}' | '\u{2029}' => true, |
| 176 |
_ => false, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/// Returns whether or not a char is a blankspace (Unicode Pattern_White_Space) |
| 181 |
const fn is_blankspace(c: char) -> bool { |
| 182 |
match c { |
| 183 |
'\u{0020}' |
| 184 |
| '\u{0009}'..='\u{000d}' |
| 185 |
| '\u{0085}' |
| 186 |
| '\u{200e}' |
| 187 |
| '\u{200f}' |
| 188 |
| '\u{2028}' |
| 189 |
| '\u{2029}' => true, |
| 190 |
_ => false, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/// Returns whether or not a char is a word start (Unicode XID_Start + '_') |
| 195 |
fn is_word_start(c: char) -> bool { |
| 196 |
c == '_' || unicode_ident::is_xid_start(c) |
| 197 |
} |
| 198 |
|
| 199 |
/// Returns whether or not a char is a word part (Unicode XID_Continue) |
| 200 |
fn is_word_part(c: char) -> bool { |
| 201 |
unicode_ident::is_xid_continue(c) |
| 202 |
} |
| 203 |
|
| 204 |
#[derive(Clone)] |
| 205 |
pub(in crate::front::wgsl) struct Lexer<'a> { |
| 206 |
/// The remaining unconsumed input. |
| 207 |
input: &'a str, |
| 208 |
|
| 209 |
/// The full original source code. |
| 210 |
/// |
| 211 |
/// We compare `input` against this to compute the lexer's current offset in |
| 212 |
/// the source. |
| 213 |
pub(in crate::front::wgsl) source: &'a str, |
| 214 |
|
| 215 |
/// The byte offset of the end of the most recently returned non-trivia |
| 216 |
/// token. |
| 217 |
/// |
| 218 |
/// This is consulted by the `span_from` function, for finding the |
| 219 |
/// end of the span for larger structures like expressions or |
| 220 |
/// statements. |
| 221 |
last_end_offset: usize, |
| 222 |
|
| 223 |
pub(in crate::front::wgsl) enable_extensions: EnableExtensions, |
| 224 |
} |
| 225 |
|
| 226 |
impl<'a> Lexer<'a> { |
| 227 |
pub(in crate::front::wgsl) const fn new(input: &'a str) -> Self { |
| 228 |
Lexer { |
| 229 |
input, |
| 230 |
source: input, |
| 231 |
last_end_offset: 0, |
| 232 |
enable_extensions: EnableExtensions::empty(), |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/// Calls the function with a lexer and returns the result of the function as well as the span for everything the function parsed |
| 237 |
/// |
| 238 |
/// # Examples |
| 239 |
/// ```ignore |
| 240 |
/// let lexer = Lexer::new("5"); |
| 241 |
/// let (value, span) = lexer.capture_span(Lexer::next_uint_literal); |
| 242 |
/// assert_eq!(value, 5); |
| 243 |
/// ``` |
| 244 |
#[inline] |
| 245 |
pub fn capture_span<T, E>( |
| 246 |
&mut self, |
| 247 |
inner: impl FnOnce(&mut Self) -> core::result::Result<T, E>, |
| 248 |
) -> core::result::Result<(T, Span), E> { |
| 249 |
let start = self.current_byte_offset(); |
| 250 |
let res = inner(self)?; |
| 251 |
let end = self.current_byte_offset(); |
| 252 |
Ok((res, Span::from(start..end))) |
| 253 |
} |
| 254 |
|
| 255 |
pub(in crate::front::wgsl) fn start_byte_offset(&mut self) -> usize { |
| 256 |
loop { |
| 257 |
// Eat all trivia because `next` doesn't eat trailing trivia. |
| 258 |
let (token, rest) = consume_token(self.input, false); |
| 259 |
if let Token::Trivia = token { |
| 260 |
self.input = rest; |
| 261 |
} else { |
| 262 |
return self.current_byte_offset(); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
fn peek_token_and_rest(&mut self) -> (TokenSpan<'a>, &'a str) { |
| 268 |
let mut cloned = self.clone(); |
| 269 |
let token = cloned.next(); |
| 270 |
let rest = cloned.input; |
| 271 |
(token, rest) |
| 272 |
} |
| 273 |
|
| 274 |
const fn current_byte_offset(&self) -> usize { |
| 275 |
self.source.len() - self.input.len() |
| 276 |
} |
| 277 |
|
| 278 |
pub(in crate::front::wgsl) fn span_from(&self, offset: usize) -> Span { |
| 279 |
Span::from(offset..self.last_end_offset) |
| 280 |
} |
| 281 |
|
| 282 |
/// Return the next non-whitespace token from `self`. |
| 283 |
/// |
| 284 |
/// Assume we are a parse state where bit shift operators may |
| 285 |
/// occur, but not angle brackets. |
| 286 |
#[must_use] |
| 287 |
pub(in crate::front::wgsl) fn next(&mut self) -> TokenSpan<'a> { |
| 288 |
self.next_impl(false) |
| 289 |
} |
| 290 |
|
| 291 |
/// Return the next non-whitespace token from `self`. |
| 292 |
/// |
| 293 |
/// Assume we are in a parse state where angle brackets may occur, |
| 294 |
/// but not bit shift operators. |
| 295 |
#[must_use] |
| 296 |
pub(in crate::front::wgsl) fn next_generic(&mut self) -> TokenSpan<'a> { |
| 297 |
self.next_impl(true) |
| 298 |
} |
| 299 |
|
| 300 |
/// Return the next non-whitespace token from `self`, with a span. |
| 301 |
/// |
| 302 |
/// See [`consume_token`] for the meaning of `generic`. |
| 303 |
fn next_impl(&mut self, generic: bool) -> TokenSpan<'a> { |
| 304 |
let mut start_byte_offset = self.current_byte_offset(); |
| 305 |
loop { |
| 306 |
let (token, rest) = consume_token(self.input, generic); |
| 307 |
self.input = rest; |
| 308 |
match token { |
| 309 |
Token::Trivia => start_byte_offset = self.current_byte_offset(), |
| 310 |
_ => { |
| 311 |
self.last_end_offset = self.current_byte_offset(); |
| 312 |
return (token, self.span_from(start_byte_offset)); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#[must_use] |
| 319 |
pub(in crate::front::wgsl) fn peek(&mut self) -> TokenSpan<'a> { |
| 320 |
let (token, _) = self.peek_token_and_rest(); |
| 321 |
token |
| 322 |
} |
| 323 |
|
| 324 |
pub(in crate::front::wgsl) fn expect_span(&mut self, expected: Token<'a>) -> Result<'a, Span> { |
| 325 |
let next = self.next(); |
| 326 |
if next.0 == expected { |
| 327 |
Ok(next.1) |
| 328 |
} else { |
| 329 |
Err(Box::new(Error::Unexpected( |
| 330 |
next.1, |
| 331 |
ExpectedToken::Token(expected), |
| 332 |
))) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
pub(in crate::front::wgsl) fn expect(&mut self, expected: Token<'a>) -> Result<'a, ()> { |
| 337 |
self.expect_span(expected)?; |
| 338 |
Ok(()) |
| 339 |
} |
| 340 |
|
| 341 |
pub(in crate::front::wgsl) fn expect_generic_paren( |
| 342 |
&mut self, |
| 343 |
expected: char, |
| 344 |
) -> Result<'a, ()> { |
| 345 |
let next = self.next_generic(); |
| 346 |
if next.0 == Token::Paren(expected) { |
| 347 |
Ok(()) |
| 348 |
} else { |
| 349 |
Err(Box::new(Error::Unexpected( |
| 350 |
next.1, |
| 351 |
ExpectedToken::Token(Token::Paren(expected)), |
| 352 |
))) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
pub(in crate::front::wgsl) fn end_of_generic_arguments(&mut self) -> bool { |
| 357 |
self.skip(Token::Separator(',')) && self.peek().0 != Token::Paren('>') |
| 358 |
} |
| 359 |
|
| 360 |
/// If the next token matches it is skipped and true is returned |
| 361 |
pub(in crate::front::wgsl) fn skip(&mut self, what: Token<'_>) -> bool { |
| 362 |
let (peeked_token, rest) = self.peek_token_and_rest(); |
| 363 |
if peeked_token.0 == what { |
| 364 |
self.input = rest; |
| 365 |
true |
| 366 |
} else { |
| 367 |
false |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
pub(in crate::front::wgsl) fn next_ident_with_span(&mut self) -> Result<'a, (&'a str, Span)> { |
| 372 |
match self.next() { |
| 373 |
(Token::Word(word), span) => Self::word_as_ident_with_span(word, span), |
| 374 |
other => Err(Box::new(Error::Unexpected( |
| 375 |
other.1, |
| 376 |
ExpectedToken::Identifier, |
| 377 |
))), |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
pub(in crate::front::wgsl) fn peek_ident_with_span(&mut self) -> Result<'a, (&'a str, Span)> { |
| 382 |
match self.peek() { |
| 383 |
(Token::Word(word), span) => Self::word_as_ident_with_span(word, span), |
| 384 |
other => Err(Box::new(Error::Unexpected( |
| 385 |
other.1, |
| 386 |
ExpectedToken::Identifier, |
| 387 |
))), |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
fn word_as_ident_with_span(word: &'a str, span: Span) -> Result<'a, (&'a str, Span)> { |
| 392 |
match word { |
| 393 |
"_" => Err(Box::new(Error::InvalidIdentifierUnderscore(span))), |
| 394 |
word if word.starts_with("__") => Err(Box::new(Error::ReservedIdentifierPrefix(span))), |
| 395 |
word => Ok((word, span)), |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
pub(in crate::front::wgsl) fn next_ident(&mut self) -> Result<'a, super::ast::Ident<'a>> { |
| 400 |
self.next_ident_with_span() |
| 401 |
.and_then(|(word, span)| Self::word_as_ident(word, span)) |
| 402 |
.map(|(name, span)| super::ast::Ident { name, span }) |
| 403 |
} |
| 404 |
|
| 405 |
fn word_as_ident(word: &'a str, span: Span) -> Result<'a, (&'a str, Span)> { |
| 406 |
if crate::keywords::wgsl::RESERVED.contains(&word) { |
| 407 |
Err(Box::new(Error::ReservedKeyword(span))) |
| 408 |
} else { |
| 409 |
Ok((word, span)) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
/// Parses a generic scalar type, for example `<f32>`. |
| 414 |
pub(in crate::front::wgsl) fn next_scalar_generic(&mut self) -> Result<'a, Scalar> { |
| 415 |
self.expect_generic_paren('<')?; |
| 416 |
let (scalar, _span) = match self.next() { |
| 417 |
(Token::Word(word), span) => { |
| 418 |
conv::get_scalar_type(&self.enable_extensions, span, word)? |
| 419 |
.map(|scalar| (scalar, span)) |
| 420 |
.ok_or(Error::UnknownScalarType(span))? |
| 421 |
} |
| 422 |
(_, span) => return Err(Box::new(Error::UnknownScalarType(span))), |
| 423 |
}; |
| 424 |
|
| 425 |
self.expect_generic_paren('>')?; |
| 426 |
Ok(scalar) |
| 427 |
} |
| 428 |
|
| 429 |
/// Parses a generic scalar type, for example `<f32>`. |
| 430 |
/// |
| 431 |
/// Returns the span covering the inner type, excluding the brackets. |
| 432 |
pub(in crate::front::wgsl) fn next_scalar_generic_with_span( |
| 433 |
&mut self, |
| 434 |
) -> Result<'a, (Scalar, Span)> { |
| 435 |
self.expect_generic_paren('<')?; |
| 436 |
|
| 437 |
let (scalar, span) = match self.next() { |
| 438 |
(Token::Word(word), span) => { |
| 439 |
conv::get_scalar_type(&self.enable_extensions, span, word)? |
| 440 |
.map(|scalar| (scalar, span)) |
| 441 |
.ok_or(Error::UnknownScalarType(span))? |
| 442 |
} |
| 443 |
(_, span) => return Err(Box::new(Error::UnknownScalarType(span))), |
| 444 |
}; |
| 445 |
|
| 446 |
self.expect_generic_paren('>')?; |
| 447 |
Ok((scalar, span)) |
| 448 |
} |
| 449 |
|
| 450 |
pub(in crate::front::wgsl) fn next_storage_access( |
| 451 |
&mut self, |
| 452 |
) -> Result<'a, crate::StorageAccess> { |
| 453 |
let (ident, span) = self.next_ident_with_span()?; |
| 454 |
match ident { |
| 455 |
"read" => Ok(crate::StorageAccess::LOAD), |
| 456 |
"write" => Ok(crate::StorageAccess::STORE), |
| 457 |
"read_write" => Ok(crate::StorageAccess::LOAD | crate::StorageAccess::STORE), |
| 458 |
"atomic" => Ok(crate::StorageAccess::ATOMIC |
| 459 |
| crate::StorageAccess::LOAD |
| 460 |
| crate::StorageAccess::STORE), |
| 461 |
_ => Err(Box::new(Error::UnknownAccess(span))), |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
pub(in crate::front::wgsl) fn next_format_generic( |
| 466 |
&mut self, |
| 467 |
) -> Result<'a, (crate::StorageFormat, crate::StorageAccess)> { |
| 468 |
self.expect(Token::Paren('<'))?; |
| 469 |
let (ident, ident_span) = self.next_ident_with_span()?; |
| 470 |
let format = conv::map_storage_format(ident, ident_span)?; |
| 471 |
self.expect(Token::Separator(','))?; |
| 472 |
let access = self.next_storage_access()?; |
| 473 |
self.expect(Token::Paren('>'))?; |
| 474 |
Ok((format, access)) |
| 475 |
} |
| 476 |
|
| 477 |
pub(in crate::front::wgsl) fn next_acceleration_structure_flags(&mut self) -> Result<'a, bool> { |
| 478 |
Ok(if self.skip(Token::Paren('<')) { |
| 479 |
if !self.skip(Token::Paren('>')) { |
| 480 |
let (name, span) = self.next_ident_with_span()?; |
| 481 |
let ret = if name == "vertex_return" { |
| 482 |
true |
| 483 |
} else { |
| 484 |
return Err(Box::new(Error::UnknownAttribute(span))); |
| 485 |
}; |
| 486 |
self.skip(Token::Separator(',')); |
| 487 |
self.expect(Token::Paren('>'))?; |
| 488 |
ret |
| 489 |
} else { |
| 490 |
false |
| 491 |
} |
| 492 |
} else { |
| 493 |
false |
| 494 |
}) |
| 495 |
} |
| 496 |
|
| 497 |
pub(in crate::front::wgsl) fn open_arguments(&mut self) -> Result<'a, ()> { |
| 498 |
self.expect(Token::Paren('(')) |
| 499 |
} |
| 500 |
|
| 501 |
pub(in crate::front::wgsl) fn close_arguments(&mut self) -> Result<'a, ()> { |
| 502 |
let _ = self.skip(Token::Separator(',')); |
| 503 |
self.expect(Token::Paren(')')) |
| 504 |
} |
| 505 |
|
| 506 |
pub(in crate::front::wgsl) fn next_argument(&mut self) -> Result<'a, bool> { |
| 507 |
let paren = Token::Paren(')'); |
| 508 |
if self.skip(Token::Separator(',')) { |
| 509 |
Ok(!self.skip(paren)) |
| 510 |
} else { |
| 511 |
self.expect(paren).map(|()| false) |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
#[cfg(test)] |
| 517 |
#[track_caller] |
| 518 |
fn sub_test(source: &str, expected_tokens: &[Token]) { |
| 519 |
let mut lex = Lexer::new(source); |
| 520 |
for &token in expected_tokens { |
| 521 |
assert_eq!(lex.next().0, token); |
| 522 |
} |
| 523 |
assert_eq!(lex.next().0, Token::End); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn test_numbers() { |
| 528 |
use half::f16; |
| 529 |
// WGSL spec examples // |
| 530 |
|
| 531 |
// decimal integer |
| 532 |
sub_test( |
| 533 |
"0x123 0X123u 1u 123 0 0i 0x3f", |
| 534 |
&[ |
| 535 |
Token::Number(Ok(Number::AbstractInt(291))), |
| 536 |
Token::Number(Ok(Number::U32(291))), |
| 537 |
Token::Number(Ok(Number::U32(1))), |
| 538 |
Token::Number(Ok(Number::AbstractInt(123))), |
| 539 |
Token::Number(Ok(Number::AbstractInt(0))), |
| 540 |
Token::Number(Ok(Number::I32(0))), |
| 541 |
Token::Number(Ok(Number::AbstractInt(63))), |
| 542 |
], |
| 543 |
); |
| 544 |
// decimal floating point |
| 545 |
sub_test( |
| 546 |
"0.e+4f 01. .01 12.34 .0f 0h 1e-3 0xa.fp+2 0x1P+4f 0X.3 0x3p+2h 0X1.fp-4 0x3.2p+2h", |
| 547 |
&[ |
| 548 |
Token::Number(Ok(Number::F32(0.))), |
| 549 |
Token::Number(Ok(Number::AbstractFloat(1.))), |
| 550 |
Token::Number(Ok(Number::AbstractFloat(0.01))), |
| 551 |
Token::Number(Ok(Number::AbstractFloat(12.34))), |
| 552 |
Token::Number(Ok(Number::F32(0.))), |
| 553 |
Token::Number(Ok(Number::F16(f16::from_f32(0.)))), |
| 554 |
Token::Number(Ok(Number::AbstractFloat(0.001))), |
| 555 |
Token::Number(Ok(Number::AbstractFloat(43.75))), |
| 556 |
Token::Number(Ok(Number::F32(16.))), |
| 557 |
Token::Number(Ok(Number::AbstractFloat(0.1875))), |
| 558 |
// https://github.com/gfx-rs/wgpu/issues/7046 |
| 559 |
Token::Number(Err(NumberError::NotRepresentable)), // Should be 0.75 |
| 560 |
Token::Number(Ok(Number::AbstractFloat(0.12109375))), |
| 561 |
// https://github.com/gfx-rs/wgpu/issues/7046 |
| 562 |
Token::Number(Err(NumberError::NotRepresentable)), // Should be 12.5 |
| 563 |
], |
| 564 |
); |
| 565 |
|
| 566 |
// MIN / MAX // |
| 567 |
|
| 568 |
// min / max decimal integer |
| 569 |
sub_test( |
| 570 |
"0i 2147483647i 2147483648i", |
| 571 |
&[ |
| 572 |
Token::Number(Ok(Number::I32(0))), |
| 573 |
Token::Number(Ok(Number::I32(i32::MAX))), |
| 574 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 575 |
], |
| 576 |
); |
| 577 |
// min / max decimal unsigned integer |
| 578 |
sub_test( |
| 579 |
"0u 4294967295u 4294967296u", |
| 580 |
&[ |
| 581 |
Token::Number(Ok(Number::U32(u32::MIN))), |
| 582 |
Token::Number(Ok(Number::U32(u32::MAX))), |
| 583 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 584 |
], |
| 585 |
); |
| 586 |
|
| 587 |
// min / max hexadecimal signed integer |
| 588 |
sub_test( |
| 589 |
"0x0i 0x7FFFFFFFi 0x80000000i", |
| 590 |
&[ |
| 591 |
Token::Number(Ok(Number::I32(0))), |
| 592 |
Token::Number(Ok(Number::I32(i32::MAX))), |
| 593 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 594 |
], |
| 595 |
); |
| 596 |
// min / max hexadecimal unsigned integer |
| 597 |
sub_test( |
| 598 |
"0x0u 0xFFFFFFFFu 0x100000000u", |
| 599 |
&[ |
| 600 |
Token::Number(Ok(Number::U32(u32::MIN))), |
| 601 |
Token::Number(Ok(Number::U32(u32::MAX))), |
| 602 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 603 |
], |
| 604 |
); |
| 605 |
|
| 606 |
// min/max decimal abstract int |
| 607 |
sub_test( |
| 608 |
"0 9223372036854775807 9223372036854775808", |
| 609 |
&[ |
| 610 |
Token::Number(Ok(Number::AbstractInt(0))), |
| 611 |
Token::Number(Ok(Number::AbstractInt(i64::MAX))), |
| 612 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 613 |
], |
| 614 |
); |
| 615 |
|
| 616 |
// min/max hexadecimal abstract int |
| 617 |
sub_test( |
| 618 |
"0 0x7fffffffffffffff 0x8000000000000000", |
| 619 |
&[ |
| 620 |
Token::Number(Ok(Number::AbstractInt(0))), |
| 621 |
Token::Number(Ok(Number::AbstractInt(i64::MAX))), |
| 622 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 623 |
], |
| 624 |
); |
| 625 |
|
| 626 |
/// ≈ 2^-126 * 2^−23 (= 2^−149) |
| 627 |
const SMALLEST_POSITIVE_SUBNORMAL_F32: f32 = 1e-45; |
| 628 |
/// ≈ 2^-126 * (1 − 2^−23) |
| 629 |
const LARGEST_SUBNORMAL_F32: f32 = 1.1754942e-38; |
| 630 |
/// ≈ 2^-126 |
| 631 |
const SMALLEST_POSITIVE_NORMAL_F32: f32 = f32::MIN_POSITIVE; |
| 632 |
/// ≈ 1 − 2^−24 |
| 633 |
const LARGEST_F32_LESS_THAN_ONE: f32 = 0.99999994; |
| 634 |
/// ≈ 1 + 2^−23 |
| 635 |
const SMALLEST_F32_LARGER_THAN_ONE: f32 = 1.0000001; |
| 636 |
/// ≈ 2^127 * (2 − 2^−23) |
| 637 |
const LARGEST_NORMAL_F32: f32 = f32::MAX; |
| 638 |
|
| 639 |
// decimal floating point |
| 640 |
sub_test( |
| 641 |
"1e-45f 1.1754942e-38f 1.17549435e-38f 0.99999994f 1.0000001f 3.40282347e+38f", |
| 642 |
&[ |
| 643 |
Token::Number(Ok(Number::F32(SMALLEST_POSITIVE_SUBNORMAL_F32))), |
| 644 |
Token::Number(Ok(Number::F32(LARGEST_SUBNORMAL_F32))), |
| 645 |
Token::Number(Ok(Number::F32(SMALLEST_POSITIVE_NORMAL_F32))), |
| 646 |
Token::Number(Ok(Number::F32(LARGEST_F32_LESS_THAN_ONE))), |
| 647 |
Token::Number(Ok(Number::F32(SMALLEST_F32_LARGER_THAN_ONE))), |
| 648 |
Token::Number(Ok(Number::F32(LARGEST_NORMAL_F32))), |
| 649 |
], |
| 650 |
); |
| 651 |
sub_test( |
| 652 |
"3.40282367e+38f", |
| 653 |
&[ |
| 654 |
Token::Number(Err(NumberError::NotRepresentable)), // ≈ 2^128 |
| 655 |
], |
| 656 |
); |
| 657 |
|
| 658 |
// hexadecimal floating point |
| 659 |
sub_test( |
| 660 |
"0x1p-149f 0x7FFFFFp-149f 0x1p-126f 0xFFFFFFp-24f 0x800001p-23f 0xFFFFFFp+104f", |
| 661 |
&[ |
| 662 |
Token::Number(Ok(Number::F32(SMALLEST_POSITIVE_SUBNORMAL_F32))), |
| 663 |
Token::Number(Ok(Number::F32(LARGEST_SUBNORMAL_F32))), |
| 664 |
Token::Number(Ok(Number::F32(SMALLEST_POSITIVE_NORMAL_F32))), |
| 665 |
Token::Number(Ok(Number::F32(LARGEST_F32_LESS_THAN_ONE))), |
| 666 |
Token::Number(Ok(Number::F32(SMALLEST_F32_LARGER_THAN_ONE))), |
| 667 |
Token::Number(Ok(Number::F32(LARGEST_NORMAL_F32))), |
| 668 |
], |
| 669 |
); |
| 670 |
sub_test( |
| 671 |
"0x1p128f 0x1.000001p0f", |
| 672 |
&[ |
| 673 |
Token::Number(Err(NumberError::NotRepresentable)), // = 2^128 |
| 674 |
Token::Number(Err(NumberError::NotRepresentable)), |
| 675 |
], |
| 676 |
); |
| 677 |
} |
| 678 |
|
| 679 |
#[test] |
| 680 |
fn double_floats() { |
| 681 |
sub_test( |
| 682 |
"0x1.2p4lf 0x1p8lf 0.0625lf 625e-4lf 10lf 10l", |
| 683 |
&[ |
| 684 |
Token::Number(Ok(Number::F64(18.0))), |
| 685 |
Token::Number(Ok(Number::F64(256.0))), |
| 686 |
Token::Number(Ok(Number::F64(0.0625))), |
| 687 |
Token::Number(Ok(Number::F64(0.0625))), |
| 688 |
Token::Number(Ok(Number::F64(10.0))), |
| 689 |
Token::Number(Ok(Number::AbstractInt(10))), |
| 690 |
Token::Word("l"), |
| 691 |
], |
| 692 |
) |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
fn test_tokens() { |
| 697 |
sub_test("id123_OK", &[Token::Word("id123_OK")]); |
| 698 |
sub_test( |
| 699 |
"92No", |
| 700 |
&[ |
| 701 |
Token::Number(Ok(Number::AbstractInt(92))), |
| 702 |
Token::Word("No"), |
| 703 |
], |
| 704 |
); |
| 705 |
sub_test( |
| 706 |
"2u3o", |
| 707 |
&[ |
| 708 |
Token::Number(Ok(Number::U32(2))), |
| 709 |
Token::Number(Ok(Number::AbstractInt(3))), |
| 710 |
Token::Word("o"), |
| 711 |
], |
| 712 |
); |
| 713 |
sub_test( |
| 714 |
"2.4f44po", |
| 715 |
&[ |
| 716 |
Token::Number(Ok(Number::F32(2.4))), |
| 717 |
Token::Number(Ok(Number::AbstractInt(44))), |
| 718 |
Token::Word("po"), |
| 719 |
], |
| 720 |
); |
| 721 |
sub_test( |
| 722 |
"Δέλτα réflexion Кызыл 𐰓𐰏𐰇 朝焼け سلام 검정 שָׁלוֹם गुलाबी փիրուզ", |
| 723 |
&[ |
| 724 |
Token::Word("Δέλτα"), |
| 725 |
Token::Word("réflexion"), |
| 726 |
Token::Word("Кызыл"), |
| 727 |
Token::Word("𐰓𐰏𐰇"), |
| 728 |
Token::Word("朝焼け"), |
| 729 |
Token::Word("سلام"), |
| 730 |
Token::Word("검정"), |
| 731 |
Token::Word("שָׁלוֹם"), |
| 732 |
Token::Word("गुलाबी"), |
| 733 |
Token::Word("փիրուզ"), |
| 734 |
], |
| 735 |
); |
| 736 |
sub_test("æNoø", &[Token::Word("æNoø")]); |
| 737 |
sub_test("No¾", &[Token::Word("No"), Token::Unknown('¾')]); |
| 738 |
sub_test("No好", &[Token::Word("No好")]); |
| 739 |
sub_test("_No", &[Token::Word("_No")]); |
| 740 |
sub_test( |
| 741 |
"*/*/***/*//=/*****//", |
| 742 |
&[ |
| 743 |
Token::Operation('*'), |
| 744 |
Token::AssignmentOperation('/'), |
| 745 |
Token::Operation('/'), |
| 746 |
], |
| 747 |
); |
| 748 |
|
| 749 |
// Type suffixes are only allowed on hex float literals |
| 750 |
// if you provided an exponent. |
| 751 |
sub_test( |
| 752 |
"0x1.2f 0x1.2f 0x1.2h 0x1.2H 0x1.2lf", |
| 753 |
&[ |
| 754 |
// The 'f' suffixes are taken as a hex digit: |
| 755 |
// the fractional part is 0x2f / 256. |
| 756 |
Token::Number(Ok(Number::AbstractFloat(1.0 + 0x2f as f64 / 256.0))), |
| 757 |
Token::Number(Ok(Number::AbstractFloat(1.0 + 0x2f as f64 / 256.0))), |
| 758 |
Token::Number(Ok(Number::AbstractFloat(1.125))), |
| 759 |
Token::Word("h"), |
| 760 |
Token::Number(Ok(Number::AbstractFloat(1.125))), |
| 761 |
Token::Word("H"), |
| 762 |
Token::Number(Ok(Number::AbstractFloat(1.125))), |
| 763 |
Token::Word("lf"), |
| 764 |
], |
| 765 |
) |
| 766 |
} |
| 767 |
|
| 768 |
#[test] |
| 769 |
fn test_variable_decl() { |
| 770 |
sub_test( |
| 771 |
"@group(0 ) var< uniform> texture: texture_multisampled_2d <f32 >;", |
| 772 |
&[ |
| 773 |
Token::Attribute, |
| 774 |
Token::Word("group"), |
| 775 |
Token::Paren('('), |
| 776 |
Token::Number(Ok(Number::AbstractInt(0))), |
| 777 |
Token::Paren(')'), |
| 778 |
Token::Word("var"), |
| 779 |
Token::Paren('<'), |
| 780 |
Token::Word("uniform"), |
| 781 |
Token::Paren('>'), |
| 782 |
Token::Word("texture"), |
| 783 |
Token::Separator(':'), |
| 784 |
Token::Word("texture_multisampled_2d"), |
| 785 |
Token::Paren('<'), |
| 786 |
Token::Word("f32"), |
| 787 |
Token::Paren('>'), |
| 788 |
Token::Separator(';'), |
| 789 |
], |
| 790 |
); |
| 791 |
sub_test( |
| 792 |
"var<storage,read_write> buffer: array<u32>;", |
| 793 |
&[ |
| 794 |
Token::Word("var"), |
| 795 |
Token::Paren('<'), |
| 796 |
Token::Word("storage"), |
| 797 |
Token::Separator(','), |
| 798 |
Token::Word("read_write"), |
| 799 |
Token::Paren('>'), |
| 800 |
Token::Word("buffer"), |
| 801 |
Token::Separator(':'), |
| 802 |
Token::Word("array"), |
| 803 |
Token::Paren('<'), |
| 804 |
Token::Word("u32"), |
| 805 |
Token::Paren('>'), |
| 806 |
Token::Separator(';'), |
| 807 |
], |
| 808 |
); |
| 809 |
} |