Source code for simvx.core.scene_io.source._source_ast
"""The stdlib backend for the source layer: `ast` for structure, `tokenize` for trivia.
Two foundation modules sit under everything else here. :mod:`spans` is the only
module in the engine allowed to know that CPython reports column offsets in utf-8
bytes; it converts every position the parser hands out into a `str` index and
resolves the spans an editor needs to splice. :mod:`trivia` makes one `tokenize`
pass and says what lies between the statements: the comments, the blank lines,
the indentation and the semicolons.
Everything above them deals in spans and text. No module here mutates a tree,
because there is no tree to mutate: the document's only state is the author's
text, and an edit is a splice followed by a re-parse.
"""
from __future__ import annotations
from .. import SourceBackend, SourceDocument
from .document import AstDocument, parse_document, parse_one_expression
__all__ = ["BACKEND", "AstBackend", "AstDocument"]
[docs]
class AstBackend(SourceBackend):
"""Reads and edits through CPython's own parser, and splices the text itself."""
__slots__ = ()
[docs]
@property
def name(self) -> str:
return "ast"
[docs]
def parse(self, text: str) -> SourceDocument:
return parse_document(text)
[docs]
def parse_expression(self, text: str) -> None:
parse_one_expression(text)
#: The instance :func:`simvx.core.scene_io.source.get_backend` resolves.
BACKEND = AstBackend()