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: usizeCurrent position in the input stream
Implementations§
Source§impl Lexer
impl Lexer
Sourcepub fn parse(&mut self) -> Result<Vec<Token>, LexerError>
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.
Sourcepub fn parse_with_context(
&mut self,
ctx: ParseContext,
) -> Result<Vec<Token>, LexerError>
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.
Sourcefn parse_nested_content<F>(
&mut self,
is_delimiter: F,
ctx: ParseContext,
) -> Result<Vec<Token>, LexerError>
fn parse_nested_content<F>( &mut self, is_delimiter: F, ctx: ParseContext, ) -> Result<Vec<Token>, LexerError>
Helper function to parse nested content until a delimiter is encountered. Used for parsing content within emphasis, headings, and list items.
Sourcefn next_token(&mut self, ctx: ParseContext) -> Result<Option<Token>, LexerError>
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.
Sourcefn parse_heading(&mut self) -> Result<Token, LexerError>
fn parse_heading(&mut self) -> Result<Token, LexerError>
Parses a heading token, counting ‘#’ characters for level and collecting nested content
Sourcefn parse_emphasis(&mut self) -> Result<Token, LexerError>
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.
Sourcefn parse_code(&mut self) -> Result<Token, LexerError>
fn parse_code(&mut self) -> Result<Token, LexerError>
Parses code blocks, handling both inline code and fenced code blocks
Sourcefn count_backticks(&mut self) -> usize
fn count_backticks(&mut self) -> usize
Helper method to count consecutive backticks
Sourcefn parse_math(&mut self) -> Result<Token, LexerError>
fn parse_math(&mut self) -> Result<Token, LexerError>
Parses LaTeX math blocks ($…$ for inline, $$…$$ for display)
Sourcefn parse_blockquote(&mut self) -> Result<Token, LexerError>
fn parse_blockquote(&mut self) -> Result<Token, LexerError>
Parses a blockquote, collecting text until newline
Sourcefn parse_link(&mut self) -> Result<Token, LexerError>
fn parse_link(&mut self) -> Result<Token, LexerError>
Parses a link token, extracting display text and URL
Sourcefn parse_image(&mut self) -> Result<Token, LexerError>
fn parse_image(&mut self) -> Result<Token, LexerError>
Parses an image token, extracting alt text and URL
Sourcefn parse_newline(&mut self) -> Result<Token, LexerError>
fn parse_newline(&mut self) -> Result<Token, LexerError>
Parses a newline token
Sourcefn parse_text(&mut self, ctx: ParseContext) -> Result<Token, LexerError>
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.
Sourcefn parse_html_comment(&mut self) -> Result<Token, LexerError>
fn parse_html_comment(&mut self) -> Result<Token, LexerError>
Parses an HTML comment, extracting the comment content
Sourcefn is_at_line_start(&self) -> bool
fn is_at_line_start(&self) -> bool
Checks if current position is at the start of a line
Sourcefn skip_whitespace(&mut self)
fn skip_whitespace(&mut self)
Skips whitespace characters except newlines
Sourcefn current_char(&self) -> char
fn current_char(&self) -> char
Returns the current character or ‘\0’ if at end of input
Sourcefn read_until_newline(&mut self) -> String
fn read_until_newline(&mut self) -> String
Reads characters until a newline is encountered
Sourcefn read_until_char(&mut self, delimiter: char) -> String
fn read_until_char(&mut self, delimiter: char) -> String
Reads characters until a specific delimiter is encountered
Sourcefn is_html_comment_start(&self) -> bool
fn is_html_comment_start(&self) -> bool
Checks if current position starts an HTML comment
Sourcefn is_start_of_special_token(&self, ctx: ParseContext) -> bool
fn is_start_of_special_token(&self, ctx: ParseContext) -> bool
Checks if current position could start a special token given a context
Sourcefn is_after_special_token(&self) -> bool
fn is_after_special_token(&self) -> bool
Checks if we’re immediately after a special token that should preserve following spaces
Sourcefn check_horizontal_rule(&mut self) -> Result<bool, LexerError>
fn check_horizontal_rule(&mut self) -> Result<bool, LexerError>
Checks if the current position contains a horizontal rule (—)
Sourcefn check_ordered_list_marker(&mut self) -> Option<usize>
fn check_ordered_list_marker(&mut self) -> Option<usize>
Checks if current position starts an ordered list marker (e.g., “1.”)
Sourcefn parse_list_item(
&mut self,
ordered: bool,
indent_level: usize,
parent_ctx: ParseContext,
) -> Result<Token, LexerError>
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
Sourcefn is_table_start(&self) -> bool
fn is_table_start(&self) -> bool
Checks if the current posisiton is the start of a table
Sourcefn parse_table(&mut self) -> Result<Token, LexerError>
fn parse_table(&mut self) -> Result<Token, LexerError>
Parses a table, handling column alignment
Sourcefn get_current_indent(&self) -> usize
fn get_current_indent(&self) -> usize
Gets the current line’s indentation level
Sourcefn is_list_marker(&self, marker: char) -> bool
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.