mpylab.tools.dotparser module

Dependency-free parser for the DOT subset used by mpylab.

The production parser used by mpylab.tools.mgraph.MGraph is pydot. This module is maintained as an independent parser and validation aid; it is not selected automatically by MGraph.

class mpylab.tools.dotparser.DotParser(text: str)

Bases: object

Recursive descent parser for the supported DOT subset.

Return value is (nodes, graph) where:

  • nodes maps each node name to a dictionary of node attributes.

  • graph maps each left node to its right-side neighbors and edge attribute dictionaries.

parse() tuple[dict, dict]

Parse DOT text and return (nodes, graph) dictionaries.

exception mpylab.tools.dotparser.DotSyntaxError(message: str, pos: int | None = None)

Bases: ValueError

Syntax error raised for invalid DOT input.

class mpylab.tools.dotparser.DotTokenizer(text: str)

Bases: object

Tokenizer for the supported DOT subset.

accept(token_type: str) Token | None

Consume and return token when type matches, else return None.

expect(token_type: str) Token

Consume and return token, raising on type mismatch.

peek() Token

Return the current token without consuming it.

peek_type() str

Return the type of the current token.

class mpylab.tools.dotparser.Token(type: str, value: str, pos: int)

Bases: object

Token produced by the DOT tokenizer.

pos: int
type: str
value: str
mpylab.tools.dotparser.parse(rule: str, text: str)

Compatibility wrapper for old parse(rule, text) usage.

Currently only ‘graph’ is supported.

mpylab.tools.dotparser.parse_dot(text: str) tuple[dict, dict]

Parse DOT text with the handwritten parser.

mpylab.tools.dotparser.test_typical_graph()

Run a smoke test on a representative DOT graph definition.