Skip to main content

Indenter

Struct Indenter 

pub struct Indenter {
    pub nl_type: String,
    pub open_paren_types: Vec<String>,
    pub close_paren_types: Vec<String>,
    pub indent_type: String,
    pub dedent_type: String,
    pub tab_len: usize,
}
Expand description

Lark’s Indenter, configured by terminal names.

State machine per token: an NL at paren_level == 0 emits NL then INDENT/DEDENTs by comparing the post-newline whitespace width (count(' ') + count('\t') * tab_len, code points) against the indent stack; open/close types track paren_level; EOF flushes remaining DEDENTs.

Confirmed-and-kept deviations from indenter.py, all reviewer calls:

  • A newline-free NL token yields an empty indent (Lark IndexErrors, after already yielding the NL).
  • Paren underflow is left to the parser (Lark hard-asserts; its assert also vanishes under python -O).
  • An unknown nl/indent/dedent NAME is a PostlexError at Postlex::session; Lark runs on, emitting a token of a type the grammar never declared.
  • tab_len == 0 is a PostlexError at session time, where Lark asserts at Indenter() construction.
  • nl/indent/dedent are matched and emitted by id, so if two terminals share a name only the first is recognized. Lark compares name strings, so both are. The paren types, which are only ever matched, do follow Lark and mark every matching id (a_duplicated_name_resolves_first_but_marks_every_paren_id). Unreachable through the compiler, which interns names.

Not a deviation: Lark’s closing assert self.indent_level == [0] (indenter.py:76) has no counterpart here because it cannot fail — the stack’s bottom is invariantly 0 and the pop loop stops there.

Fields§

§nl_type: String§open_paren_types: Vec<String>§close_paren_types: Vec<String>§indent_type: String§dedent_type: String§tab_len: usize

Trait Implementations§

§

impl Clone for Indenter

§

fn clone(&self) -> Indenter

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 Indenter

§

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

Formats the value using the given formatter. Read more
§

impl Postlex for Indenter

§

fn session( &self, names: &[String], ) -> Result<Box<dyn PostlexStream>, PostlexError>

Start a session for one lex pass. names is the terminal-name table indexed by TokenId (the session resolves its configured names once). An unknown nl/indent/dedent name is a PostlexError (they are emitted or consumed, so they must exist); unknown paren names are dropped — Lark treats the paren types as matched-only string sets (Lark parity). Read more
§

fn always_accept(&self) -> Vec<String>

Terminal names accepted in every parser state (indenter: [NL_type]).

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.