pub struct Token {
pub kind: TokenKind,
pub text: Arc<str>,
pub start: usize,
pub end: usize,
}Expand description
Token types and token stream for lexer output. Token produced by the lexer and consumed by the parser.
Stores the token kind, original source text, and byte span. The text is kept
in an Arc<str> so buffering and lookahead can clone tokens cheaply.
Fields§
§kind: TokenKindToken classification for parser decision making
text: Arc<str>Original source text for precise reconstruction
start: usizeStarting byte position for error reporting and location tracking
end: usizeEnding byte position for span calculation and navigation
Implementations§
Source§impl Token
impl Token
Sourcepub fn new(
kind: TokenKind,
text: impl Into<Arc<str>>,
start: usize,
end: usize,
) -> Token
pub fn new( kind: TokenKind, text: impl Into<Arc<str>>, start: usize, end: usize, ) -> Token
Create a new token with the given kind, source text, and byte span.
§Examples
use perl_token::{Token, TokenKind};
let tok = Token::new(TokenKind::Sub, "sub", 0, 3);
assert_eq!(tok.kind, TokenKind::Sub);
assert_eq!(&*tok.text, "sub");Sourcepub fn try_new(
kind: TokenKind,
text: impl Into<Arc<str>>,
start: usize,
end: usize,
) -> Result<Token, TokenSpanError>
pub fn try_new( kind: TokenKind, text: impl Into<Arc<str>>, start: usize, end: usize, ) -> Result<Token, TokenSpanError>
Create a token with checked span ordering.
Unlike Token::new, this rejects spans where end < start.
Sourcepub fn new_checked(
kind: TokenKind,
text: impl Into<Arc<str>>,
start: usize,
end: usize,
) -> Result<Token, TokenSpanError>
pub fn new_checked( kind: TokenKind, text: impl Into<Arc<str>>, start: usize, end: usize, ) -> Result<Token, TokenSpanError>
Create a token while enforcing span invariants.
Rules:
start <= end- zero-length spans are accepted for EOF and explicit synthetic unknown tokens
Sourcepub fn unknown_at(text: impl Into<Arc<str>>, start: usize, end: usize) -> Token
pub fn unknown_at(text: impl Into<Arc<str>>, start: usize, end: usize) -> Token
Create an unknown (synthetic) token at start..end.
Sourcepub fn with_span(
&self,
start: usize,
end: usize,
) -> Result<Token, TokenSpanError>
pub fn with_span( &self, start: usize, end: usize, ) -> Result<Token, TokenSpanError>
Clone this token with a new checked span.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the token span length in bytes.
This uses saturating subtraction so malformed spans (where end < start)
are treated as zero-length instead of underflowing.
§Examples
use perl_token::{Token, TokenKind};
let tok = Token::new(TokenKind::Identifier, "foo", 10, 13);
assert_eq!(tok.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Return whether the token span is empty.
§Examples
use perl_token::{Token, TokenKind};
let tok = Token::new(TokenKind::Eof, "", 8, 8);
assert!(tok.is_empty());Sourcepub fn display_name(&self) -> &'static str
pub fn display_name(&self) -> &'static str
Return a human-readable display name for this token.
Sourcepub fn as_ref_token(&self) -> TokenRef<'_>
pub fn as_ref_token(&self) -> TokenRef<'_>
Return a borrowed token view over this token.