Skip to content

Commit a72064f

Browse files
WIP: refactor(wgsl-in): add peeking variants of Lexer::next_ident*
1. Break out `word_as_ident*` helpers to keep validation of identifiers DRY. 2. Add `peek_*` variants. TODO: Pending validation from @jimb at <https://matrix.to/#/!fjjkgHFcwtkREywzfk:matrix.org/$feC_32iRis6rk0hiqm-gysIzljXNSfVmbNTBJAjlhKY?via=matrix.org&via=mozilla.org&via=ralith.com>
1 parent a6ae1a1 commit a72064f

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,50 @@ impl<'a> Lexer<'a> {
350350
&mut self,
351351
) -> Result<(&'a str, Span), Error<'a>> {
352352
match self.next() {
353-
(Token::Word("_"), span) => Err(Error::InvalidIdentifierUnderscore(span)),
354-
(Token::Word(word), span) if word.starts_with("__") => {
355-
Err(Error::ReservedIdentifierPrefix(span))
356-
}
357-
(Token::Word(word), span) => Ok((word, span)),
353+
(Token::Word(word), span) => Self::word_as_ident_with_span(word, span),
354+
other => Err(Error::Unexpected(other.1, ExpectedToken::Identifier)),
355+
}
356+
}
357+
358+
pub(in crate::front::wgsl) fn peek_ident_with_span(
359+
&mut self,
360+
) -> Result<(&'a str, Span), Error<'a>> {
361+
match self.peek() {
362+
(Token::Word(word), span) => Self::word_as_ident_with_span(word, span),
358363
other => Err(Error::Unexpected(other.1, ExpectedToken::Identifier)),
359364
}
360365
}
361366

367+
fn word_as_ident_with_span(word: &'a str, span: Span) -> Result<(&'a str, Span), Error<'a>> {
368+
match word {
369+
"_" => Err(Error::InvalidIdentifierUnderscore(span)),
370+
word if word.starts_with("__") => Err(Error::ReservedIdentifierPrefix(span)),
371+
word => Ok((word, span)),
372+
}
373+
}
374+
362375
pub(in crate::front::wgsl) fn next_ident(
363376
&mut self,
364377
) -> Result<super::ast::Ident<'a>, Error<'a>> {
365-
let ident = self
366-
.next_ident_with_span()
367-
.map(|(name, span)| super::ast::Ident { name, span })?;
378+
self.next_ident_with_span()
379+
.and_then(|(word, span)| Self::word_as_ident(word, span))
380+
.map(|(name, span)| super::ast::Ident { name, span })
381+
}
368382

369-
if crate::keywords::wgsl::RESERVED.contains(&ident.name) {
370-
return Err(Error::ReservedKeyword(ident.span));
383+
fn word_as_ident(word: &'a str, span: Span) -> Result<(&'a str, Span), Error<'a>> {
384+
if crate::keywords::wgsl::RESERVED.contains(&word) {
385+
Err(Error::ReservedKeyword(span))
386+
} else {
387+
Ok((word, span))
371388
}
389+
}
372390

373-
Ok(ident)
391+
pub(in crate::front::wgsl) fn peek_ident(
392+
&mut self,
393+
) -> Result<super::ast::Ident<'a>, Error<'a>> {
394+
self.peek_ident_with_span()
395+
.and_then(|(word, span)| Self::word_as_ident(word, span))
396+
.map(|(name, span)| super::ast::Ident { name, span })
374397
}
375398

376399
/// Parses a generic scalar type, for example `<f32>`.

0 commit comments

Comments
 (0)