ds_common_placeholder_py_lib.grammar ==================================== .. py:module:: ds_common_placeholder_py_lib.grammar .. autoapi-nested-parse:: **File:** ``grammar.py`` **Region:** ``ds-common-placeholder-py-lib`` Description ----------- Single-pass scanner for the ``standard`` grammar (contract §3). Splits a string into text and placeholders and parses each placeholder into an AST. Attributes ---------- .. autoapisummary:: ds_common_placeholder_py_lib.grammar.BLANK ds_common_placeholder_py_lib.grammar.LOOKUP ds_common_placeholder_py_lib.grammar.FORMATS ds_common_placeholder_py_lib.grammar.TYPES ds_common_placeholder_py_lib.grammar.NOT_READ_WHOLE ds_common_placeholder_py_lib.grammar.Expr ds_common_placeholder_py_lib.grammar.MAX_DEPTH ds_common_placeholder_py_lib.grammar.Segment ds_common_placeholder_py_lib.grammar.Item Classes ------- .. autoapisummary:: ds_common_placeholder_py_lib.grammar.Literal ds_common_placeholder_py_lib.grammar.Reference ds_common_placeholder_py_lib.grammar.Call ds_common_placeholder_py_lib.grammar.Text ds_common_placeholder_py_lib.grammar.Placeholder Functions --------- .. autoapisummary:: ds_common_placeholder_py_lib.grammar.to_type ds_common_placeholder_py_lib.grammar.escape ds_common_placeholder_py_lib.grammar.parse_path ds_common_placeholder_py_lib.grammar.write ds_common_placeholder_py_lib.grammar.reference_text ds_common_placeholder_py_lib.grammar.namespaces ds_common_placeholder_py_lib.grammar.calls ds_common_placeholder_py_lib.grammar.parse Module Contents --------------- .. py:data:: BLANK :value: Multiline-String .. raw:: html
Show Value .. code-block:: python """ """ .. raw:: html
Characters a blank string consists of (contract §8.2). ASCII only, so every implementation agrees. .. py:class:: Literal A string argument. .. py:attribute:: value :type: str .. py:data:: LOOKUP :value: 'lookup' ``@{lookup, owner.settings.tz}`` is ``@{owner.settings.tz}``; ``@{lookup, owner}`` reads the whole namespace. :type: The function that reads a reference .. py:data:: FORMATS :value: ('str', 'json', 'int', 'float', 'bool') the value as text (``str``), as compact JSON text (``json``), or as a whole number, a number or a boolean (``int``, ``float``, ``bool``). A default cannot be one of these words. Without a format a default is text. :type: ``lookup``'s optional format, its last argument .. py:data:: TYPES :value: ('int', 'float', 'bool') The formats that give the value a JSON type. .. py:function:: to_type(value: Any, fmt: str) -> Any *value* as the JSON type *fmt* names (``int``, ``float``, ``bool``): already that type, or text that spells it (``"3"``, ``"2.5"``, ``"true"``). Nothing is guessed: ``"yes"`` is not a boolean. :raises ValueError: *value* is not, or does not spell, that type. .. py:data:: NOT_READ_WHOLE ``tasks`` is every task's output, unbounded, so a read names a task (``tasks.``). :type: Namespaces ``lookup`` cannot read whole .. py:class:: Reference ``.``, or ``@{lookup, [.][, ][, ]}`` (an empty *path* is the whole namespace). *default* is the text read when there is no value. .. py:attribute:: namespace :type: str .. py:attribute:: path :type: tuple[str, ...] .. py:attribute:: format :type: str | None :value: None .. py:attribute:: default :type: str | None :value: None .. py:class:: Call ```` or ``, , …``. An argument written as a placeholder, ``@{f, @{g, x}}``, is the expression it holds: a :class:`Call`, a :class:`Reference` (``@{lookup, …}``), or a :class:`Literal` (``@{'…'}``). .. py:attribute:: name :type: str .. py:attribute:: args :type: tuple[Literal | Reference | Call, ...] .. py:data:: Expr A placeholder's expression. A :class:`Literal` is a quoted text, ``@{'@{'}``: the way to write ``@{`` as text. .. py:data:: MAX_DEPTH :value: 16 ``@{a, @{b, @{c}}}`` is depth 2. :type: How deep placeholders can be nested .. py:class:: Text Plain text. It never contains ``@{``: that always starts a placeholder. .. py:attribute:: value :type: str .. py:class:: Placeholder One ``@{…}`` and its offsets in the scanned string. .. py:attribute:: expr :type: Expr .. py:attribute:: raw :type: str .. py:attribute:: start :type: int .. py:attribute:: end :type: int .. py:data:: Segment .. py:function:: escape(text: str) -> str Write *text* so that resolving it gives exactly *text*: every ``@{`` becomes the placeholder ``@{'@{'}``, which resolves to ``@{``. Use it for a value from outside that must be taken literally, such as a caller's override of a declared variable. :param text: Any string. :returns: *text*, with no ``@{`` left that would start a placeholder. .. py:function:: parse_path(path: str) -> tuple[str, ...] Read a path to a value in a JSON document, written like a reference: keys separated by ``.``, an array item by its index, and a key with other characters in quotes (``tasks.0.settings.'a.b'``). :param path: The path. :returns: Its keys, in order (array indexes as strings). :raises TypeError: If *path* is not a string. :raises ValueError: If *path* is not such a path. .. py:function:: write(expr: Expr) -> str The placeholder text of *expr*: parsing it gives *expr* back. Every literal is written quoted, so a value with ``,``, ``}`` or ``@{`` in it stays one literal. :param expr: A parsed expression. :returns: ``@{…}``. .. py:function:: reference_text(ref: Reference) -> str *ref* as written in a path: ``owner.settings.'a b'``; a whole namespace is its name. .. py:function:: namespaces(expr: Expr) -> frozenset[str] Every namespace *expr* reads, nested placeholders included. .. py:function:: calls(expr: Expr) -> list[Call] Every call in *expr*, outermost first, nested ones included. .. py:function:: parse(text: str) -> list[Segment] Split *text* into text and placeholder segments. :param text: The string to scan. :returns: The segments in order. Adjacent text is merged. :raises PlaceholderError: ``syntax`` for any malformed ``@{…}``. .. py:data:: Item its dot-separated parts, each a ``(text, quoted)`` pair, or a placeholder written as the whole item. :type: One comma-separated item