ml_switcheroo.core.tikz.parser¶

TikZ Parser (Lexer & Logical Reconstruction).

This module provides the TikzParser which consumes raw LaTeX/TikZ source code and reconstructs the LogicalGraph representation. It effectively reverses the operation of the TikzEmitter.

Capabilities:

  1. Tokenization: Regex-based lexer for LaTeX commands, groups, and options.

  2. Structural Parsing: Identifies nodes, edges, and environments via recursive descent.

  3. Metadata Extraction: Parses HTML-like tabular environments embedded in node labels to recover layer hyperparameters (e.g., kernel size, stride).

  4. Graph Reconstruction: Builds a LogicalGraph object compatible with the rest of the transpiler pipeline.

Classes¶

TokenKind

Enumeration of LaTeX/TikZ token types.

Token

A lexical unit with position info.

TikzLexer

Regex-based tokenizer for TikZ source code.

TikzParser

Parses tokenized TikZ code into a LogicalGraph.

Module Contents¶

class ml_switcheroo.core.tikz.parser.TokenKind(*args, **kwds)[source]¶

Bases: enum.Enum

Enumeration of LaTeX/TikZ token types.

COMMAND¶
LBRACE¶
RBRACE¶
LBRACKET¶
RBRACKET¶
LPAREN¶
RPAREN¶
SEMICOLON¶
ARROW¶
WORD¶
NUMBER¶
COMMENT¶
WHITESPACE¶
EOF¶
class ml_switcheroo.core.tikz.parser.Token[source]¶

A lexical unit with position info.

kind: TokenKind¶
text: str¶
line: int¶
col: int¶
class ml_switcheroo.core.tikz.parser.TikzLexer(text: str)[source]¶

Regex-based tokenizer for TikZ source code.

Splits raw LaTeX strings into a stream of typed Tokens, handling symbols, commands, strings, and whitespace.

PATTERNS¶
text¶
pos = 0¶
line = 1¶
col = 1¶
tokenize() → List[Token][source]¶

Converts the full string into a list of Tokens.

Returns:

Sequence of tokens, excluding comments and whitespace.

Ends with an EOF token.

Return type:

List[Token]

class ml_switcheroo.core.tikz.parser.TikzParser(text: str)[source]¶

Parses tokenized TikZ code into a LogicalGraph.

This parser implements a recursive descent strategy tailored to the specific TikZ subset produced by the TikzEmitter. It is not a general-purpose TeX parser.

lexer¶
tokens = []¶
pos = 0¶
graph¶
parse() → ml_switcheroo.core.tikz.analyser.LogicalGraph[source]¶

Main entry point. Iterates top-level commands.

Returns:

The reconstructed graph extracted from the visual definition.

Return type:

LogicalGraph