ds_common_placeholder_py_lib.grammar

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

BLANK

Characters a blank string consists of (contract §8.2). ASCII only, so every

LOOKUP

@{lookup, owner.settings.tz} is

FORMATS

the value as text

TYPES

The formats that give the value a JSON type.

NOT_READ_WHOLE

tasks is every task's output,

Expr

A placeholder's expression. A Literal is a quoted text,

MAX_DEPTH

@{a, @{b, @{c}}} is depth 2.

Segment

Item

its dot-separated parts, each a

Classes

Literal

A string argument.

Reference

<namespace>.<path>, or @{lookup, <namespace>[.<path>][, <default>][, <format>]}

Call

<name> or <name>, <arg>, …. An argument written as a

Text

Plain text. It never contains @{: that always starts a

Placeholder

One @{…} and its offsets in the scanned string.

Functions

to_type(value, fmt) → Any

value as the JSON type fmt names (int, float, bool):

escape(text) → str

Write text so that resolving it gives exactly text: every @{

parse_path(path) → tuple[str, ...]

Read a path to a value in a JSON document, written like a reference:

write(expr) → str

The placeholder text of expr: parsing it gives expr back. Every

reference_text(ref) → str

ref as written in a path: owner.settings.'a b'; a whole

namespaces(expr) → frozenset[str]

Every namespace expr reads, nested placeholders included.

calls(expr) → list[Call]

Every call in expr, outermost first, nested ones included.

parse(text) → list[Segment]

Split text into text and placeholder segments.

Module Contents

ds_common_placeholder_py_lib.grammar.BLANK = Multiline-String
Show Value
"""



"""

Characters a blank string consists of (contract §8.2). ASCII only, so every implementation agrees.

class ds_common_placeholder_py_lib.grammar.Literal[source]

A string argument.

value: str
ds_common_placeholder_py_lib.grammar.LOOKUP = 'lookup'

@{lookup, owner.settings.tz} is @{owner.settings.tz}; @{lookup, owner} reads the whole namespace.

Type:

The function that reads a reference

ds_common_placeholder_py_lib.grammar.FORMATS = ('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

ds_common_placeholder_py_lib.grammar.TYPES = ('int', 'float', 'bool')

The formats that give the value a JSON type.

ds_common_placeholder_py_lib.grammar.to_type(value: Any, fmt: str) → Any[source]

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.

ds_common_placeholder_py_lib.grammar.NOT_READ_WHOLE

tasks is every task’s output, unbounded, so a read names a task (tasks.<id>).

Type:

Namespaces lookup cannot read whole

class ds_common_placeholder_py_lib.grammar.Reference[source]

<namespace>.<path>, or @{lookup, <namespace>[.<path>][, <default>][, <format>]} (an empty path is the whole namespace). default is the text read when there is no value.

namespace: str
path: tuple[str, ...]
format: str | None = None
default: str | None = None
class ds_common_placeholder_py_lib.grammar.Call[source]

<name> or <name>, <arg>, …. An argument written as a placeholder, @{f, @{g, x}}, is the expression it holds: a Call, a Reference (@{lookup, …}), or a Literal (@{'…'}).

name: str
args: tuple[Literal | Reference | Call, ...]
ds_common_placeholder_py_lib.grammar.Expr

A placeholder’s expression. A Literal is a quoted text, @{'@{'}: the way to write @{ as text.

ds_common_placeholder_py_lib.grammar.MAX_DEPTH = 16

@{a, @{b, @{c}}} is depth 2.

Type:

How deep placeholders can be nested

class ds_common_placeholder_py_lib.grammar.Text[source]

Plain text. It never contains @{: that always starts a placeholder.

value: str
class ds_common_placeholder_py_lib.grammar.Placeholder[source]

One @{…} and its offsets in the scanned string.

expr: Expr
raw: str
start: int
end: int
ds_common_placeholder_py_lib.grammar.Segment
ds_common_placeholder_py_lib.grammar.escape(text: str) → str[source]

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.

Parameters:

text – Any string.

Returns:

text, with no @{ left that would start a placeholder.

ds_common_placeholder_py_lib.grammar.parse_path(path: str) → tuple[str, ...][source]

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').

Parameters:

path – The path.

Returns:

Its keys, in order (array indexes as strings).

Raises:
  • TypeError – If path is not a string.

  • ValueError – If path is not such a path.

ds_common_placeholder_py_lib.grammar.write(expr: Expr) → str[source]

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.

Parameters:

expr – A parsed expression.

Returns:

@{…}.

ds_common_placeholder_py_lib.grammar.reference_text(ref: Reference) → str[source]

ref as written in a path: owner.settings.'a b'; a whole namespace is its name.

ds_common_placeholder_py_lib.grammar.namespaces(expr: Expr) → frozenset[str][source]

Every namespace expr reads, nested placeholders included.

ds_common_placeholder_py_lib.grammar.calls(expr: Expr) → list[Call][source]

Every call in expr, outermost first, nested ones included.

ds_common_placeholder_py_lib.grammar.parse(text: str) → list[Segment][source]

Split text into text and placeholder segments.

Parameters:

text – The string to scan.

Returns:

The segments in order. Adjacent text is merged.

Raises:

PlaceholderError – syntax for any malformed @{…}.

ds_common_placeholder_py_lib.grammar.Item

its dot-separated parts, each a (text, quoted) pair, or a placeholder written as the whole item.

Type:

One comma-separated item