pub struct Token {
pub name: String,
pub value: String,
pub position: usize,
}Expand description
Represents a lexical token with a name/type and its raw value from the source code.
Used to represent the output of the Tokenizer struct.
§Examples
let num = Token { name: "int".into(), value: "23".into(), position: 3 };
let kw = Token::from(("keyword", "if", 0));
assert_eq!(num, Token::from(("int", "23", 3)));Fields§
§name: StringThe type or category of the token (e.g., “int”, “identifier”, “operator”).
value: StringThe actual text/value from the source code that this token represents.
position: usizeThe position of the token in the source code.
- For [
Tokenizer::tokenize], this is the byte offset from the start of the entire source. - For [
Tokenizer::tokenize_lines], this is the byte offset from the start of each line.
For instance, tokenizing "aa\naa" for the token a will yield positions of [0, 1, 3, 4]
and [[0, 1], [0, 1]], respectively.
Trait Implementations§
impl Eq for Token
impl StructuralPartialEq for Token
Auto Trait Implementations§
impl Freeze for Token
impl RefUnwindSafe for Token
impl Send for Token
impl Sync for Token
impl Unpin for Token
impl UnwindSafe for Token
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more