Skip to content

type_bridge.query.parser

parser

Parser for converting TypeQL strings back to AST nodes.

Uses the Rust type_bridge_core.parse_typeql_query function, then converts the serde-tagged dicts into the Python AST dataclasses defined in type_bridge.query.ast.

parse_typeql_query

parse_typeql_query(input)

Parse a TypeQL query string into a list of Clause AST nodes.

Uses the Rust type_bridge_core parser for performance and correctness.

Parameters:

Name Type Description Default
input str

A TypeQL data-manipulation query string.

required

Returns:

Type Description
list[Clause]

List of Clause AST nodes (MatchClause, InsertClause, etc.)

Raises:

Type Description
ValueError

If the query cannot be parsed.

NotImplementedError

If the Rust extension is not available.

Source code in type_bridge/query/parser.py
def parse_typeql_query(input: str) -> list[Clause]:
    """Parse a TypeQL query string into a list of Clause AST nodes.

    Uses the Rust ``type_bridge_core`` parser for performance and correctness.

    Args:
        input: A TypeQL data-manipulation query string.

    Returns:
        List of Clause AST nodes (MatchClause, InsertClause, etc.)

    Raises:
        ValueError: If the query cannot be parsed.
        NotImplementedError: If the Rust extension is not available.
    """
    if not RUST_AVAILABLE or _rust_parse_typeql_query is None:
        raise NotImplementedError(
            "parse_typeql_query requires the type_bridge_core Rust extension. "
            "Install it with: maturin develop --manifest-path type-bridge-core/Cargo.toml"
        )

    # Rust returns list[dict] in serde-tagged-enum format
    clause_dicts: list[dict[str, Any]] = _rust_parse_typeql_query(input)
    return [_dict_to_clause(d) for d in clause_dicts]