Parser

Parsing of QVR domain-specific language syntax.

parser

Parser for the quivers DSL.

The lexer/parser pipeline is delegated to panproto via the qvr tree-sitter grammar registered in panproto-grammars-all. The public :func:parse entry point consumes .qvr source bytes and returns a :class:Module of dataclass AST nodes.

ParseError

Bases: Exception

Raised when the .qvr source fails to parse or wrap into AST nodes.

parse

parse(source: str | bytes, file_path: str = '<source>') -> Module

Parse .qvr source bytes into a :class:Module.

Source code in src/quivers/dsl/parser.py
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
def parse(source: str | bytes, file_path: str = "<source>") -> Module:
    """Parse `.qvr` source bytes into a :class:`Module`."""
    if isinstance(source, str):
        source_bytes = source.encode("utf-8")
    else:
        source_bytes = source

    schema = _registry().parse_with_protocol("qvr", source_bytes, file_path)
    tree = _Tree(schema, source_bytes)

    root_id = next(
        (v.id for v in schema.vertices if v.kind == "source_file"),
        None,
    )
    if root_id is None:
        raise ParseError(f"panproto schema has no source_file vertex for {file_path}")

    statements: list[Statement] = []
    for child in tree.positional(root_id):
        if tree.kind(child) == "line_comment":
            continue
        result = _walk_statement(tree, child)
        if isinstance(result, list):
            statements.extend(result)
        else:
            statements.append(result)
    return Module(statements=tuple(statements))

parse_file

parse_file(path: str | Path) -> Module

Parse a .qvr file at path.

Source code in src/quivers/dsl/parser.py
900
901
902
903
def parse_file(path: str | Path) -> Module:
    """Parse a `.qvr` file at `path`."""
    p = Path(path)
    return parse(p.read_bytes(), str(p))