Skip to main content

MacroExpander

Struct MacroExpander 

Source
pub struct MacroExpander<'a> {
    pub lexer: Lexer<'a>,
    pub mode: Mode,
    /* private fields */
}
Expand description

The MacroExpander (or “gullet”) manages macro expansion.

It sits between the Lexer (mouth) and the Parser (stomach). Tokens are read from the lexer, pushed onto an internal stack, and macros are expanded until only non-expandable tokens remain.

Modeled after KaTeX’s MacroExpander.ts.

Fields§

§lexer: Lexer<'a>§mode: Mode

Implementations§

Source§

impl<'a> MacroExpander<'a>

Source

pub fn new(input: &'a str, mode: Mode) -> Self

Source

pub fn set_macro(&mut self, name: String, def: MacroDefinition)

Source

pub fn set_macro_global(&mut self, name: String, def: MacroDefinition)

Source

pub fn set_text_macro(&mut self, name: &str, text: &str)

Source

pub fn get_macro(&self, name: &str) -> Option<&MacroDefinition>

Source

pub fn expand_tokens(&mut self, tokens: Vec<Token>) -> ParseResult<Vec<Token>>

Expand a list of tokens fully (for \edef/\xdef).

Source

pub fn switch_mode(&mut self, new_mode: Mode)

Source

pub fn begin_group(&mut self)

Source

pub fn end_group(&mut self)

Source

pub fn end_groups(&mut self)

Source

pub fn future(&mut self) -> &Token

Returns the topmost token on the stack, without expanding it.

Source

pub fn pop_token(&mut self) -> Token

Remove and return the next unexpanded token.

Source

pub fn set_top_text(&mut self, text: String)

Modify the top token’s text on the stack (for \global prefix handling).

Source

pub fn push_token(&mut self, token: Token)

Push a token onto the stack.

Source

pub fn push_tokens(&mut self, tokens: Vec<Token>)

Push multiple tokens onto the stack.

Source

pub fn consume_spaces(&mut self)

Consume all following space tokens, without expansion.

Source

pub fn expand_next_token(&mut self) -> ParseResult<Token>

Recursively expand the next token until a non-expandable token is found.

Source

pub fn consume_arg( &mut self, delims: Option<&[&str]>, ) -> ParseResult<ConsumedArg>

Consume a single argument from the token stream.

Source

pub fn scan_argument(&mut self, is_optional: bool) -> ParseResult<Option<Token>>

Scan a function argument (optional or mandatory). Pushes an EOF token to mark the end, then pushes the argument tokens.

Source

pub fn is_defined(&self, name: &str) -> bool

Check if a command name is currently defined.

Source

pub fn is_expandable(&self, name: &str) -> bool

Check if a command is expandable.

Auto Trait Implementations§

§

impl<'a> Freeze for MacroExpander<'a>

§

impl<'a> RefUnwindSafe for MacroExpander<'a>

§

impl<'a> Send for MacroExpander<'a>

§

impl<'a> Sync for MacroExpander<'a>

§

impl<'a> Unpin for MacroExpander<'a>

§

impl<'a> UnsafeUnpin for MacroExpander<'a>

§

impl<'a> UnwindSafe for MacroExpander<'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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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.