pub struct Token {
pub kind: TokenKind,
pub start: u32,
pub end: u32,
pub mate_plus_one: Option<NonZeroU32>,
}Fields§
§kind: TokenKindThe token’s typed class.
start: u32Inclusive start byte offset into the input.
end: u32Exclusive end byte offset into the input.
mate_plus_one: Option<NonZeroU32>The mate’s index plus one, or None. Read it through
Token::mate and write it through Token::set_mate, which hold
the encoding in one place; the field is named for what it stores so
that reading it as an index does not compile.
Implementations§
Source§impl Token
impl Token
Sourcepub fn new(kind: TokenKind, start: usize, end: usize) -> Self
pub fn new(kind: TokenKind, start: usize, end: usize) -> Self
Construct a token with no bracket mate.
§Panics
When an offset does not fit the stored width, which needs an input over four gigabytes. Truncating instead would point a token at the wrong bytes.
Sourcepub fn span(&self) -> Range<usize> ⓘ
pub fn span(&self) -> Range<usize> ⓘ
The token’s byte span, for indexing the input it was lexed from.
Sourcepub fn start(&self) -> usize
pub fn start(&self) -> usize
The start offset as a usize, for arithmetic and indexing against
everything else, which counts bytes in the machine’s width. The field
is the storage and this is the view of it; the two never disagree,
and a site that needs the wider one fails to compile rather than
silently taking the narrower.
Sourcepub fn end(&self) -> usize
pub fn end(&self) -> usize
The end offset as a usize. See Token::start.
Sourcepub fn shift(&mut self, by: usize)
pub fn shift(&mut self, by: usize)
Move the span by by bytes, for a token lexed from a chunk and
rebased onto the whole input.
§Panics
When the shifted offset does not fit the stored width.
Sourcepub fn mate(&self) -> Option<usize>
pub fn mate(&self) -> Option<usize>
The index of this token’s matching bracket, or None when it has
none.
Sourcepub fn set_mate(&mut self, index: Option<usize>)
pub fn set_mate(&mut self, index: Option<usize>)
Record this token’s matching bracket, or clear it.
§Panics
When the index does not fit the stored width. A stream that long would need more than four billion tokens, which no input this lexer can hold produces, and silently storing a wrapped index would pair the wrong brackets.
Sourcepub fn is_significant(&self) -> bool
pub fn is_significant(&self) -> bool
Whether this token participates in token-mode matching (everything except insignificant whitespace).