Skip to content

math_spec.where_parser

pyparsing-based parser for where strings — grammar and the unresolved AST.

Parses strings like "p_max > 0 AND NOT is_must_run" into an AST. The resolved node vocabulary lives in :mod:math_spec.program beside the rest of what a consumer dispatches on; what stays here is the grammar and the Unresolved* nodes it emits, which resolution rewrites away.

UnresolvedWhereNode = UnresolvedNameNode | UnresolvedComparisonNode | UnresolvedPositionNode module-attribute #

UnresolvedComparisonNode(name, op, value, quoted=False) dataclass #

A comparison against an unresolved name. resolution.py types it.

name instance-attribute #

op instance-attribute #

quoted = False class-attribute instance-attribute #

value instance-attribute #

UnresolvedNameNode(name) dataclass #

A bare name — unresolved. resolution.py types it.

name instance-attribute #

UnresolvedPositionNode(dimension, op, position, by=None) dataclass #

position(dim) <op> i before the name is checked.

Kept apart from :class:UnresolvedComparisonNode because its left-hand side is not a name but an application to one, which no bare name can carry. resolution.py types it into :class:~math_spec.program.DimensionPositionNode.

by = None class-attribute instance-attribute #

dimension instance-attribute #

op instance-attribute #

position instance-attribute #

parse_where(text) #

Parse a where string into an AST.

The connectives and literals are the resolved vocabulary's own, but the leaves naming declarations come back as Unresolved* nodes — the annotation is the type the tree has once resolution types every leaf.

RAISES DESCRIPTION
SchemaError

If text is not a where string of the language.

Source code in src/math_spec/where_parser.py
def parse_where(text: str) -> WhereNode:
    """Parse a where string into an AST.

    The connectives and literals are the resolved vocabulary's own, but the
    leaves naming declarations come back as ``Unresolved*`` nodes — the
    annotation is the type the tree has once resolution types every leaf.

    Raises:
        SchemaError: If *text* is not a where string of the language.
    """
    try:
        result = _WHERE_GRAMMAR.parse_string(text, parse_all=True)
    except pp.ParseException as e:
        msg = f'Failed to parse where string: {text!r}\n{e}'
        if _INDEX_CALL.search(text):
            msg += _INDEX_REWRITE
        raise SchemaError(msg) from e
    return cast('WhereNode', result[0])