Skip to main content

Lexer

Struct Lexer 

Source
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()); // EOF

Implementations§

Source§

impl<'a> Lexer<'a>

Source

pub const fn new(buf: &'a [u8]) -> Self

Lexer over buf, starting at byte 0.

Source

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).

Source

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.

Source

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).

Trait Implementations§

Source§

impl<'a> Clone for Lexer<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Lexer<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Lexer<'a>

§

impl<'a> Freeze for Lexer<'a>

§

impl<'a> Move for Lexer<'a>

§

impl<'a> RefUnwindSafe for Lexer<'a>

§

impl<'a> Send for Lexer<'a>

§

impl<'a> Sync for Lexer<'a>

§

impl<'a> Unpin for Lexer<'a>

§

impl<'a> UnsafeUnpin for Lexer<'a>

§

impl<'a> UnwindSafe for Lexer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.