Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ltx_parser

Recursive-descent parser for LaTeX. Consumes a ltx_lexer::TokenStream and produces an AST.

Responsibilities

  • syntax parsing and AST generation
  • parser-specific diagnostics (owned by this crate)

Architecture

  • parser::LtxParser wraps a TokenStream and exposes cursor methods (peek, bump, checkpoint/rewind, skip_ws).
  • parser_traits::Parse is the trait every AST node implements.
  • ast contains the node types: Document, Command, Environment, Math, Group, Text, Comment, UsePackage, DocumentClassDecl, Arg, …
  • parse_document is the top-level convenience entry point.

AST nodes

NodeDescription
DocumentTop-level root containing preamble and body.
PreambleItemPreamble items (\documentclass, \usepackage, …).
DocumentClassDecl\documentclass declaration.
UsePackage\usepackage declaration.
CommandControl sequence with its arguments.
Arg / OptionalArgRequired and optional argument variants.
Environment\begin{...}...\end{...} block.
GroupBalanced {...} group.
MathMath expression.
TextPlain text run.
CommentLaTeX comment.

Error ownership

ParserError (in src/error.rs) owns all 6 parser diagnostics (LTX::PARSER::E001E006):

CodeVariant
E001ExpectedToken
E002UnexpectedEOF
E003UnclosedEnvironment
E004MismatchedEnvironment
E005MissingClosingBrace
E006UnexpectedEOFWhileParsing

ParserErrorHandler collects these. When LtxParser::new is constructed it drains the lexer’s diagnostics into the same handler, so a single sink reports both phases. See the Parser Errors table.

Usage

#![allow(unused)]
fn main() {
use ltx_parser::{LtxParser, parse_document};

let mut parser = LtxParser::new(stream);
let doc = parse_document(&mut parser);

let handler = parser.error_handler_mut();
if handler.has_errors() {
    eprintln!("{}", handler.render_pretty());
}
}

Design notes

  • Zero token clones: AST nodes store Range<usize> token spans or zero-copy &'src str slices.
  • Diagnostics flow through ltx_diagnostics for rendering — the parser owns the errors, the diagnostics crate renders them.