Esempio n. 1
0
def parse_document(
    txt: str, version: Optional[str] = None, uri: str = "", imported: bool = False
) -> D.Document:
    if version is None:
        # for now assume the version is 1.0 if the first line is "version <number>"
        # otherwise draft-2
        version = "draft-2"
        for line in txt.split("\n"):
            line = line.strip()
            if line and line[0] != "#":
                if line.startswith("version ") and line[8].isdigit():
                    version = "1.0"
                break
    if not txt.strip():
        return D.Document(
            SourcePosition(filename=uri, line=0, column=0, end_line=0, end_column=0),
            [],
            [],
            None,
            imported,
        )
    try:
        return _DocTransformer(uri, imported).transform(parse(txt, "document", version))
    except lark.exceptions.UnexpectedCharacters as exn:
        raise Err.ParserError(uri if uri != "" else "(in buffer)") from exn
    except lark.exceptions.UnexpectedToken as exn:
        raise Err.ParserError(uri if uri != "" else "(in buffer)") from exn
Esempio n. 2
0
def sp(filename, meta) -> SourcePosition:
    return SourcePosition(
        filename=filename,
        line=meta.line,
        column=meta.column,
        end_line=meta.end_line,
        end_column=meta.end_column,
    )
Esempio n. 3
0
def parse_document(txt: str,
                   version: Optional[str] = None,
                   uri: str = "") -> D.Document:
    if version is None:
        # for now assume the version is 1.0 if the first line is "version <number>"
        # otherwise draft-2
        version = "draft-2"
        for line in txt.split("\n"):
            line = line.strip()
            if line and line[0] != "#":
                if line.startswith("version ") and line[8].isdigit():
                    version = "1.0"
                break
    if not txt.strip():
        return D.Document(
            SourcePosition(filename=uri,
                           line=0,
                           column=0,
                           end_line=0,
                           end_column=0),
            [],
            {},
            [],
            None,
        )
    try:
        return _DocTransformer(uri).transform(parse(txt, "document", version))
    except lark.exceptions.UnexpectedInput as exn:
        pos = SourcePosition(
            filename=(uri if uri != "" else "(buffer)"),
            line=getattr(exn, "line", "?"),
            column=getattr(exn, "column", "?"),
            end_line=getattr(exn, "line", "?"),
            end_column=getattr(exn, "column", "?"),
        )
        raise Err.SyntaxError(pos, str(exn)) from None
    except lark.exceptions.VisitError as exn:
        raise exn.__context__
Esempio n. 4
0
def parse_expr(txt: str, version: Optional[str] = None) -> E.Base:
    try:
        return _ExprTransformer(txt).transform(parse(txt, "expr", version))
    except lark.exceptions.UnexpectedInput as exn:
        pos = SourcePosition(
            filename="(buffer)",
            line=getattr(exn, "line", "?"),
            column=getattr(exn, "column", "?"),
            end_line=getattr(exn, "line", "?"),
            end_column=getattr(exn, "column", "?"),
        )
        raise Err.SyntaxError(pos, str(exn)) from None
    except lark.exceptions.VisitError as exn:
        raise exn.__context__