Skip to main content

LineCounter

Struct LineCounter 

pub struct LineCounter {
    pub byte_pos: usize,
    pub char_pos: usize,
    pub line: usize,
    pub column: usize,
    /* private fields */
}
Expand description

Line/column/offset tracker (Lark’s LineCounter), with both cursors: byte_pos is where the scanner reads; char_pos (and every derived position) counts code points, exactly as Python len/rindex do.

Fields§

§byte_pos: usize

Scan cursor in bytes (scanner input only — never reported).

§char_pos: usize

Cursor in code points — the value every reported position derives from.

§line: usize§column: usize

Implementations§

§

impl LineCounter

pub fn new(newline_char: char) -> Self

A fresh counter bound to no text — the SAFE default: ascii stays false, so a construction site that cannot name its text only loses speed.

pub fn resume_at( text: &str, char_pos: usize, line: usize, column: usize, line_start_char_pos: usize, newline_char: char, ) -> Self

Seed a counter mid-input, at code-point offset char_pos within text.

Lark’s LexerState IS the scan cursor: next_token reads line_ctr.char_pos as the place to scan from, so calling lex() again over a state that has already advanced RESUMES rather than rewinding — which is how a custom lexer skips a token it just rejected. hyperlark’s per-lex() cursor starts at zero unless it is seeded from that state, and without this it would re-lex the same prefix forever.

line/column/line_start_char_pos are taken as given rather than recomputed: Lark reports whatever the caller left in the counter, so a hand-adjusted one must be honoured, not corrected. byte_pos IS derived from text — it is the scanner’s own index and has no meaning outside this string.

Every slice later fed to the returned counter must come from text: that is what its all-ASCII verdict is about, and feeding a foreign slice would silently mis-count positions in release (debug_asserted in feed).

pub fn line_start_char_pos(&self) -> usize

The code-point offset the current line starts at — the counterpart of Self::resume_at’s input, so a caller can round-trip a counter through a binding boundary without losing it.

pub fn feed(&mut self, slice: &str, test_newline: bool)

Consume one matched slice: advance both cursors; when test_newline, count newlines and reset the line start from the last newline — all newline arithmetic in code points. Under the ASCII flag the non-newline path makes no pass at all; otherwise it is single-pass, and the newline path makes a few (count/rfind/char-count), fine for the per-token sizes seen here.

Trait Implementations§

§

impl Clone for LineCounter

§

fn clone(&self) -> LineCounter

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
§

impl Debug for LineCounter

§

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

Formats the value using the given formatter. Read more
§

impl Eq for LineCounter

§

impl PartialEq for LineCounter

Equality is over the OBSERVABLE counter only. ascii is a derived fact about the bound text, not counter state — deriving it would make new(c) compare unequal to a resume over empty text despite identical positions.

§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

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> 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, <T as TryFrom<U>>::Error>

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.