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: usizeScan cursor in bytes (scanner input only — never reported).
char_pos: usizeCursor in code points — the value every reported position derives from.
line: usize§column: usizeImplementations§
§impl LineCounter
impl LineCounter
pub fn new(newline_char: char) -> Self
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
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
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)
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
impl Clone for LineCounter
§impl Debug for LineCounter
impl Debug for LineCounter
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.
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.