Module: braid.parser

Mermaid diagram parser for Guided Reasoning Diagrams (GRD).

class braid.parser.NodeType(value)[source]

Bases: Enum

Types of nodes in a Mermaid diagram.

RECTANGLE = 'rectangle'
ROUNDED = 'rounded'
STADIUM = 'stadium'
SUBROUTINE = 'subroutine'
CYLINDRICAL = 'cylindrical'
CIRCLE = 'circle'
DIAMOND = 'diamond'
HEXAGON = 'hexagon'
PARALLELOGRAM = 'parallelogram'
TRAPEZOID = 'trapezoid'
UNKNOWN = 'unknown'
class braid.parser.GRDNode(id: str, label: str, node_type: ~braid.parser.NodeType = NodeType.RECTANGLE, metadata: ~typing.Dict[str, ~typing.Any] = <factory>)[source]

Bases: object

Represents a node in a Guided Reasoning Diagram.

id: str
label: str
node_type: NodeType = 'rectangle'
metadata: Dict[str, Any]
__init__(id: str, label: str, node_type: ~braid.parser.NodeType = NodeType.RECTANGLE, metadata: ~typing.Dict[str, ~typing.Any] = <factory>) None
class braid.parser.GRDEdge(from_node: str, to_node: str, label: str | None = None, style: str | None = None, condition: str | None = None)[source]

Bases: object

Represents an edge in a Guided Reasoning Diagram.

from_node: str
to_node: str
label: str | None = None
style: str | None = None
condition: str | None = None
__init__(from_node: str, to_node: str, label: str | None = None, style: str | None = None, condition: str | None = None) None
class braid.parser.GRDStructure(nodes: ~typing.List[~braid.parser.GRDNode] = <factory>, edges: ~typing.List[~braid.parser.GRDEdge] = <factory>, start_nodes: ~typing.List[str] = <factory>, end_nodes: ~typing.List[str] = <factory>, metadata: ~typing.Dict[str, ~typing.Any] = <factory>)[source]

Bases: object

Complete structure of a Guided Reasoning Diagram.

nodes: List[GRDNode]
edges: List[GRDEdge]
start_nodes: List[str]
end_nodes: List[str]
metadata: Dict[str, Any]
get_node_by_id(node_id: str) GRDNode | None[source]

Get a node by its ID.

get_outgoing_edges(node_id: str) List[GRDEdge][source]

Get all edges outgoing from a node.

get_incoming_edges(node_id: str) List[GRDEdge][source]

Get all edges incoming to a node.

get_execution_order() List[str][source]

Get the execution order of nodes using topological sort. Returns a list of node IDs in execution order.

__init__(nodes: ~typing.List[~braid.parser.GRDNode] = <factory>, edges: ~typing.List[~braid.parser.GRDEdge] = <factory>, start_nodes: ~typing.List[str] = <factory>, end_nodes: ~typing.List[str] = <factory>, metadata: ~typing.Dict[str, ~typing.Any] = <factory>) None
class braid.parser.MermaidParser[source]

Bases: object

Parser for Mermaid flowchart diagrams.

NODE_PATTERNS = [('(\\w+)\\[\\[(.*?)\\]\\]', NodeType.SUBROUTINE), ('(\\w+)\\[\\((.*?)\\)\\]', NodeType.STADIUM), ('(\\w+)\\(\\((.*?)\\)\\)', NodeType.CIRCLE), ('(\\w+)\\{(.*?)\\}', NodeType.DIAMOND), ('(\\w+)\\{\\{(.*?)\\}\\}', NodeType.HEXAGON), ('(\\w+)\\((.*?)\\)', NodeType.ROUNDED), ('(\\w+)\\[(.*?)\\]', NodeType.RECTANGLE), ('(\\w+)\\[/?(.*?)[/\\\\]\\]', NodeType.PARALLELOGRAM)]
EDGE_PATTERN = '(\\w+)\\s*(--[>]|==[>])\\s*(\\w+)|(\\w+)\\s*--[->]\\s*\\|\\s*(.*?)\\s*\\|\\s*(\\w+)'
__init__()[source]

Initialize the parser.

parse(mermaid_code: str) GRDStructure[source]

Parse Mermaid flowchart code into a GRD structure.

Parameters:

mermaid_code – Mermaid diagram code (flowchart format)

Returns:

GRDStructure object containing parsed nodes and edges

Raises:

ValueError – If the Mermaid code is invalid or cannot be parsed

validate(mermaid_code: str) Tuple[bool, str | None][source]

Validate Mermaid code syntax.

Parameters:

mermaid_code – Mermaid diagram code to validate

Returns:

Tuple of (is_valid, error_message)

extract_execution_steps(grd: GRDStructure) List[Dict[str, Any]][source]

Extract execution steps from a GRD structure.

Parameters:

grd – Parsed GRD structure

Returns:

List of execution steps with node information