pub struct PtxTokenStream<'a> { /* private fields */ }Expand description
Token stream wrapper for parsing PTX tokens.
This struct provides methods for consuming and inspecting tokens during parsing.
Implementations§
Source§impl<'a> PtxTokenStream<'a>
impl<'a> PtxTokenStream<'a>
pub fn new(tokens: &'a [(PtxToken, Range<usize>)]) -> Self
Sourcepub fn peek(&self) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
pub fn peek(&self) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
Peek at the next token without consuming it.
Sourcepub fn consume(&mut self) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
pub fn consume(&mut self) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
Consume and return the next token.
Sourcepub fn expect(
&mut self,
expected: &PtxToken,
) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
pub fn expect( &mut self, expected: &PtxToken, ) -> Result<&'a (PtxToken, Range<usize>), PtxParseError>
Check if the next token is the expected type, and if so, consume it. Otherwise, return an error and do NOT consume the token.
Sourcepub fn expect_identifier(
&mut self,
) -> Result<(String, Range<usize>), PtxParseError>
pub fn expect_identifier( &mut self, ) -> Result<(String, Range<usize>), PtxParseError>
Check if the next token is an identifier, and if so, consume it and return the String.
Sourcepub fn expect_register(
&mut self,
) -> Result<(String, Range<usize>), PtxParseError>
pub fn expect_register( &mut self, ) -> Result<(String, Range<usize>), PtxParseError>
Check if the next token is a register, and if so, consume it and return the String.
Sourcepub fn expect_directive(
&mut self,
) -> Result<(String, Range<usize>), PtxParseError>
pub fn expect_directive( &mut self, ) -> Result<(String, Range<usize>), PtxParseError>
Check if the next token is a directive (Dot + Identifier), and if so, consume them and return the String.
Sourcepub fn expect_modifier(
&mut self,
) -> Result<(String, Range<usize>), PtxParseError>
pub fn expect_modifier( &mut self, ) -> Result<(String, Range<usize>), PtxParseError>
Check if the next token is a directive that represents a modifier (type, state space, etc.). This is an alias for expect_directive for semantic clarity when parsing modifiers.
Sourcepub fn expect_double_colon(&mut self) -> Result<(), PtxParseError>
pub fn expect_double_colon(&mut self) -> Result<(), PtxParseError>
Expect and consume a double colon (::) token sequence.
Sourcepub fn expect_strings(
&mut self,
candidates: &[&str],
) -> Result<usize, PtxParseError>
pub fn expect_strings( &mut self, candidates: &[&str], ) -> Result<usize, PtxParseError>
Try to match and consume a sequence of tokens that matches one of the candidate strings. Returns the index of the matched candidate.
This is used for parsing modifiers that may contain :: sequences like “.to::cluster” The candidates should include the leading dot (e.g., [“.to::cluster”, “.to::cta”])
pub fn expect_string(&mut self, expected: &str) -> Result<(), PtxParseError>
Sourcepub fn try_match_string(&mut self, pattern: &str) -> bool
pub fn try_match_string(&mut self, pattern: &str) -> bool
Try to match a string pattern by consuming characters from the stream.
§Behavior
Matches the pattern character-by-character against the token stream. Tokens are converted to their string representation and matched from char_offset. If all characters match, the stream is advanced and returns true. If any character fails to match, the stream is reset and returns false.
§Returns
trueif the entire pattern was successfully matched (chars consumed)falseif matching failed at any point (stream position restored)
Sourcepub fn check<F>(&self, predicate: F) -> bool
pub fn check<F>(&self, predicate: F) -> bool
Check if the next token matches a specific pattern.
Sourcepub fn expect_complete(&self) -> Result<(), PtxParseError>
pub fn expect_complete(&self) -> Result<(), PtxParseError>
Expect that we’ve consumed a complete token (not stopped in the middle). This should be called at the end of each struct parser to verify that character-level parsing has consumed all characters from the current token.
§Returns
Ok(())ifchar_offset == 0(no partial token consumption)Err(PtxParseError)ifchar_offset > 0(stopped in middle of token)
Sourcepub fn consume_if<F>(
&mut self,
predicate: F,
) -> Option<&'a (PtxToken, Range<usize>)>
pub fn consume_if<F>( &mut self, predicate: F, ) -> Option<&'a (PtxToken, Range<usize>)>
Consume the next token if it matches the predicate.
Sourcepub fn position(&self) -> StreamPosition
pub fn position(&self) -> StreamPosition
Get the current position in the stream, for backtracking.
Sourcepub fn set_position(&mut self, pos: StreamPosition)
pub fn set_position(&mut self, pos: StreamPosition)
Reset the stream to an old position, for backtracking.
Sourcepub fn peek_char_in_token(&self) -> Option<char>
pub fn peek_char_in_token(&self) -> Option<char>
Peek at the character at the current char_offset within the current token’s string. Returns None if we’re at the end of the current token’s string or if the token has no string content.
Sourcepub fn consume_char_in_token(&mut self) -> Option<char>
pub fn consume_char_in_token(&mut self) -> Option<char>
Consume one character from the current token by advancing char_offset. If we reach the end of the token’s string, advance to the next token and reset char_offset. Returns the consumed character.
Sourcepub fn expect_char_in_token(
&mut self,
expected: char,
) -> Result<char, PtxParseError>
pub fn expect_char_in_token( &mut self, expected: char, ) -> Result<char, PtxParseError>
Match a specific character at the current position within the token. Consumes the character if it matches.