Lexer

Struct Lexer 

Source
pub struct Lexer {
    input: Vec<char>,
    position: usize,
}
Expand description

A lexical analyzer that converts Markdown text into a sequence of tokens. Handles nested structures and special Markdown syntax elements while maintaining proper context and state during parsing.

Fields§

§input: Vec<char>

Input text as character vector for efficient parsing

§position: usize

Current position in the input stream

Implementations§

Source§

impl Lexer

Source

pub fn new(input: String) -> Self

Creates a new lexer instance from input string

Source

pub fn parse(&mut self) -> Result<Vec<Token>, LexerError>

Parses the entire input string into a sequence of tokens. Returns a Result containing either a Vec of parsed tokens or a LexerError.

Source

pub fn parse_with_context( &mut self, ctx: ParseContext, ) -> Result<Vec<Token>, LexerError>

Parses the entire input string into a sequence of tokens for a given context. Returns a Result containing either a Vec of parsed tokens or a LexerError. Takes in a ParseContext that determines which tokens are valid in the current location.

Source

fn parse_nested_content<F>( &mut self, is_delimiter: F, ctx: ParseContext, ) -> Result<Vec<Token>, LexerError>
where F: Fn(char) -> bool,

Helper function to parse nested content until a delimiter is encountered. Used for parsing content within emphasis, headings, and list items.

Source

fn next_token(&mut self, ctx: ParseContext) -> Result<Option<Token>, LexerError>

Determines the next token in the input stream based on the current character and context. Handles special cases like line starts differently.

Source

fn parse_heading(&mut self) -> Result<Token, LexerError>

Parses a heading token, counting ‘#’ characters for level and collecting nested content

Source

fn parse_emphasis(&mut self) -> Result<Token, LexerError>

Parses emphasis tokens (* or _) with support for multiple levels (1-3). Ensures proper matching of opening and closing delimiters. Follows CommonMark rules: underscore emphasis requires flanking whitespace/punctuation.

Source

fn parse_code(&mut self) -> Result<Token, LexerError>

Parses code blocks, handling both inline code and fenced code blocks

Source

fn count_backticks(&mut self) -> usize

Helper method to count consecutive backticks

Source

fn parse_math(&mut self) -> Result<Token, LexerError>

Parses LaTeX math blocks ($…$ for inline, $$…$$ for display)

Source

fn parse_blockquote(&mut self) -> Result<Token, LexerError>

Parses a blockquote, collecting text until newline

Parses a link token, extracting display text and URL

Source

fn parse_image(&mut self) -> Result<Token, LexerError>

Parses an image token, extracting alt text and URL

Source

fn parse_newline(&mut self) -> Result<Token, LexerError>

Parses a newline token

Source

fn parse_text(&mut self, ctx: ParseContext) -> Result<Token, LexerError>

Parses regular text until a special token start or newline is encountered. Returns an error if no text could be parsed.

Source

fn parse_html_comment(&mut self) -> Result<Token, LexerError>

Parses an HTML comment, extracting the comment content

Source

fn is_at_line_start(&self) -> bool

Checks if current position is at the start of a line

Source

fn skip_whitespace(&mut self)

Skips whitespace characters except newlines

Source

fn advance(&mut self)

Advances the position counter by one

Source

fn current_char(&self) -> char

Returns the current character or ‘\0’ if at end of input

Source

fn read_until_newline(&mut self) -> String

Reads characters until a newline is encountered

Source

fn read_until_char(&mut self, delimiter: char) -> String

Reads characters until a specific delimiter is encountered

Source

fn is_html_comment_start(&self) -> bool

Checks if current position starts an HTML comment

Source

fn is_start_of_special_token(&self, ctx: ParseContext) -> bool

Checks if current position could start a special token given a context

Source

fn is_after_special_token(&self) -> bool

Checks if we’re immediately after a special token that should preserve following spaces

Source

fn check_horizontal_rule(&mut self) -> Result<bool, LexerError>

Checks if the current position contains a horizontal rule (—)

Source

fn check_ordered_list_marker(&mut self) -> Option<usize>

Checks if current position starts an ordered list marker (e.g., “1.”)

Source

fn parse_list_item( &mut self, ordered: bool, indent_level: usize, parent_ctx: ParseContext, ) -> Result<Token, LexerError>

Parses a list item, handling both ordered and unordered types

Source

fn is_table_start(&self) -> bool

Checks if the current posisiton is the start of a table

Source

fn parse_table(&mut self) -> Result<Token, LexerError>

Parses a table, handling column alignment

Source

fn get_current_indent(&self) -> usize

Gets the current line’s indentation level

Source

fn is_list_marker(&self, marker: char) -> bool

Checks if the given character at the current position is a list marker A list marker is followed by whitespace (space or tab)

Auto Trait Implementations§

§

impl Freeze for Lexer

§

impl RefUnwindSafe for Lexer

§

impl Send for Lexer

§

impl Sync for Lexer

§

impl Unpin for Lexer

§

impl UnwindSafe for Lexer

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
§

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more