Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'a> {
    pub mode: Mode,
    pub gullet: MacroExpander<'a>,
    pub leftright_depth: i32,
    /* private fields */
}
Expand description

The LaTeX parser. Converts a token stream into a ParseNode AST.

Follows KaTeX’s Parser.ts closely:

  • parse() → parse full expression
  • parseExpression() → parse a list of atoms
  • parseAtom() → parse one atom with optional super/subscripts
  • parseGroup() → parse a group (braced or single token)
  • parseFunction() → parse a function call with arguments
  • parseSymbol() → parse a single symbol

Fields§

§mode: Mode§gullet: MacroExpander<'a>§leftright_depth: i32

Implementations§

Source§

impl<'a> Parser<'a>

Source

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

Source

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

Return the current lookahead token (fetching from gullet if needed).

Source

pub fn consume(&mut self)

Discard the current lookahead token.

Source

pub fn expect(&mut self, text: &str, do_consume: bool) -> ParseResult<()>

Expect the next token to have the given text, consuming it.

Source

pub fn consume_spaces(&mut self) -> ParseResult<()>

Consume spaces in math mode.

Source

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

Switch between “math” and “text” modes.

Source

pub fn parse(&mut self) -> ParseResult<Vec<ParseNode>>

Parse the entire input and return the AST.

Source

pub fn parse_expression( &mut self, break_on_infix: bool, break_on_token_text: Option<&str>, ) -> ParseResult<Vec<ParseNode>>

Parse an expression: a list of atoms.

Source

pub fn parse_atom( &mut self, break_on_token_text: Option<&str>, ) -> ParseResult<Option<ParseNode>>

Parse a single atom with optional super/subscripts.

Source

pub fn parse_group( &mut self, name: &str, break_on_token_text: Option<&str>, ) -> ParseResult<Option<ParseNode>>

Parse a group: braced expression, function call, or single symbol.

Source

pub fn parse_function( &mut self, break_on_token_text: Option<&str>, name: Option<&str>, ) -> ParseResult<Option<ParseNode>>

Try to parse a function call. Returns None if not a function.

Source

pub fn call_function( &mut self, name: &str, args: Vec<ParseNode>, opt_args: Vec<Option<ParseNode>>, token: Option<Token>, break_on_token_text: Option<&str>, ) -> ParseResult<ParseNode>

Call a function handler.

Source

pub fn parse_arguments( &mut self, func: &str, func_data: &FunctionSpec, ) -> ParseResult<(Vec<ParseNode>, Vec<Option<ParseNode>>)>

Parse the arguments for a function.

Source

pub fn parse_size_group( &mut self, optional: bool, ) -> ParseResult<Option<ParseNode>>

Parse a size group (e.g., “3pt”, “1em”).

Source

pub fn parse_argument_group( &mut self, optional: bool, mode: Option<Mode>, ) -> ParseResult<Option<ParseNode>>

Parse an argument group (with optional mode switch).

Source

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

Parse a sub-expression from the given tokens.

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnsafeUnpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'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.