pub struct Lexer<'a> { /* private fields */ }Expand description
A pull tokenizer over a byte buffer.
Construct with Lexer::new (from the buffer start) or
Lexer::at (from an arbitrary offset — xref-driven parsing jumps
straight to object offsets), then call Lexer::next_token
repeatedly. White-space and comments are skipped between tokens
(§7.2.2/§7.2.3: a comment separates tokens exactly like white-space);
their bytes remain reconstructable from the buffer because every
token records its span, and untouched-object re-emission works on
whole-object spans anyway (see crate::span).
§Examples
use pdfcer_model::lexer::{Lexer, TokenKind};
let mut lx = Lexer::new(b"/Type /Pages % a comment\n42");
assert!(matches!(lx.next_token().unwrap().unwrap().kind,
TokenKind::Name(ref n) if n == b"Type"));
assert!(matches!(lx.next_token().unwrap().unwrap().kind,
TokenKind::Name(ref n) if n == b"Pages"));
assert!(matches!(lx.next_token().unwrap().unwrap().kind,
TokenKind::Integer(42)));
assert!(lx.next_token().unwrap().is_none()); // EOFImplementations§
Source§impl<'a> Lexer<'a>
impl<'a> Lexer<'a>
Sourcepub const fn at(buf: &'a [u8], pos: usize) -> Self
pub const fn at(buf: &'a [u8], pos: usize) -> Self
Lexer over buf, starting at pos. If pos is past the end of
buf, the lexer is immediately at EOF (a caller passing a bogus
xref offset gets a clean “no token” rather than a panic).
Sourcepub const fn pos(&self) -> usize
pub const fn pos(&self) -> usize
Current byte offset (the next byte the lexer will examine).
After a successful Lexer::next_token this sits one past the
returned token — which is exactly where stream data begins after
a stream keyword’s EOL, so the parser uses this to take over
for §7.3.8 data extraction.
Sourcepub fn next_token(&mut self) -> Result<Option<Token>, LexError>
pub fn next_token(&mut self) -> Result<Option<Token>, LexError>
Scan and return the next token, Ok(None) at end of input.
§Errors
LexError with the offending byte offset — see
LexErrorKind for the cases. After an error the lexer’s
position is unspecified; callers treat lex errors as fatal for
the current parse unit (recovery strategies live in the parser,
not here).