Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions scripts/grammar_obligations/_helpers/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
WITHDRAWN_FILE,
)
from scripts.grammar_obligations.exceptions import ObligationError
from scripts.grammar_obligations.models import Draft, Node, ReceiptView, Rendered
from scripts.grammar_obligations.models import DialectConfig, Draft, Node, ReceiptView, Rendered
from scripts.grammar_obligations.types import Row


Expand All @@ -65,6 +65,7 @@ def canonical(value: object) -> str:

def family_cases( # noqa: PLR0913
*,
config: DialectConfig,
root: Path,
evidence: Path,
family: str,
Expand All @@ -74,7 +75,9 @@ def family_cases( # noqa: PLR0913
unpacked: frozenset[str] = frozenset(),
) -> tuple[list[Row], dict[str, set[str]], list[Row], set[str], set[str], set[str]]:
"""Cases, outcomes, arity cases, extremes, documented SQL, disputed positive obligations."""
numbered, trees, _, _, arity = family_obligations(root=root, family=family, unpacked=unpacked)
numbered, trees, _, _, arity = family_obligations(
config=config, root=root, family=family, unpacked=unpacked
)
by_sql: dict[str, tuple[str, Rendered, list[str]]] = {}
expected: dict[str, set[str]] = {}
documented: set[str] = set()
Expand All @@ -83,7 +86,7 @@ def family_cases( # noqa: PLR0913
undisputed: set[str] = set()
conflicts: list[str] = []
for identifier, draft in numbered:
found: list[Rendered] = _rendered(draft=draft, trees=trees, family=family)
found: list[Rendered] = _rendered(config=config, draft=draft, trees=trees, family=family)
if not found:
raise ObligationError(f"{identifier}: no case could be rendered")
for rendered in found:
Expand Down Expand Up @@ -127,6 +130,7 @@ def family_cases( # noqa: PLR0913
)
if to_derive_one_option_per_case:
return family_cases(
config=config,
root=root,
evidence=evidence,
family=family,
Expand Down Expand Up @@ -316,13 +320,18 @@ def _arity_cases(


def _rendered(
*, draft: Draft, trees: dict[str, list[tuple[Node, ...]]], family: str
*,
config: DialectConfig,
draft: Draft,
trees: dict[str, list[tuple[Node, ...]]],
family: str,
) -> list[Rendered]:
if draft.spec is None:
return []
return submissions(
config=config,
trees=trees[draft.form],
style=style_of(family=family, form=draft.form),
style=style_of(config=config, family=family, form=draft.form),
spec=draft.spec,
depths=draft.depths,
)
Expand Down Expand Up @@ -527,6 +536,7 @@ def _observed_outcome(

def settled_cases( # noqa: PLR0913
*,
config: DialectConfig,
root: Path,
evidence: Path,
family: str,
Expand All @@ -536,6 +546,7 @@ def settled_cases( # noqa: PLR0913
) -> tuple[list[Row], list[Row], list[Row]]:
"""The merged cases with their receipts' stages, unscored arity cases, case marks."""
generated, expected, arity, extremes, documented, disputed_positives = family_cases(
config=config,
root=root,
evidence=evidence,
family=family,
Expand Down Expand Up @@ -623,6 +634,7 @@ def packet_text(cases: list[Row]) -> str:

def family_outputs( # noqa: PLR0913
*,
config: DialectConfig,
root: Path,
evidence: Path,
family: str,
Expand All @@ -632,6 +644,7 @@ def family_outputs( # noqa: PLR0913
) -> tuple[list[Row], list[Row], list[tuple[Path, str | None]]]:
"""One family's cases and arity cases, and each of its files' text (None: no file)."""
cases, arity, marked = settled_cases(
config=config,
root=root,
evidence=evidence,
family=family,
Expand Down
35 changes: 26 additions & 9 deletions scripts/grammar_obligations/_helpers/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
OPEN_PAREN,
TOKEN_KEYWORD,
)
from scripts.grammar_obligations.models import Node
from scripts.grammar_obligations.models import DialectConfig, Node
from scripts.grammar_obligations.types import NodePath as Path


def definition_branches(
*, name: str, definitions: dict[str, str]
*, name: str, definitions: dict[str, str], config: DialectConfig
) -> list[tuple[tuple[str, str], str, str]]:
"""(definition and branch, label, operand taking it) per keyword branch `name` reaches."""
body: tuple[Node, ...] = parse(definitions[name])
body: tuple[Node, ...] = parse(production=definitions[name], config=config)
expanded: dict[int, tuple[Node, ...]] = _expansions(
nodes=body, definitions=definitions, stack=(name,)
nodes=body, definitions=definitions, stack=(name,), config=config
)
found: list[tuple[tuple[str, str], str, str]] = []
for path, owner, choice in _keyword_choices(
Expand All @@ -46,24 +46,36 @@ def definition_branches(
expanded=expanded,
definitions=definitions,
stack=(name,),
config=config,
)
found.append(((owner, render(branch)), label, text))
return found


def _expansions(
*, nodes: tuple[Node, ...], definitions: dict[str, str], stack: tuple[str, ...]
*,
nodes: tuple[Node, ...],
definitions: dict[str, str],
stack: tuple[str, ...],
config: DialectConfig,
) -> dict[int, tuple[Node, ...]]:
"""The parsed definition of each placeholder under `nodes` that names one not on `stack`."""
found: dict[int, tuple[Node, ...]] = {}
for node in nodes:
for branch in node.branches:
found.update(_expansions(nodes=branch, definitions=definitions, stack=stack))
found.update(
_expansions(nodes=branch, definitions=definitions, stack=stack, config=config)
)
name: str = placeholder_name(node)
if node.kind == NODE_TOKEN and name in definitions and name not in stack:
found[id(node)] = parse(definitions[name])
found[id(node)] = parse(production=definitions[name], config=config)
found.update(
_expansions(nodes=found[id(node)], definitions=definitions, stack=(*stack, name))
_expansions(
nodes=found[id(node)],
definitions=definitions,
stack=(*stack, name),
config=config,
)
)
return found

Expand Down Expand Up @@ -109,6 +121,7 @@ def _written( # noqa: PLR0913
expanded: dict[int, tuple[Node, ...]],
definitions: dict[str, str],
stack: tuple[str, ...],
config: DialectConfig,
) -> str:
"""`nodes` as grammar text: `choice` as branch `index`, `path` opened, definitions inlined."""
pieces: list[str] = []
Expand All @@ -124,6 +137,7 @@ def _written( # noqa: PLR0913
expanded=expanded,
definitions=definitions,
stack=stack,
config=config,
)
)
elif path and node is path[0]:
Expand All @@ -144,6 +158,7 @@ def _written( # noqa: PLR0913
expanded=expanded,
definitions=definitions,
stack=(*stack, name) if name else stack,
config=config,
)
)
elif node.kind in (NODE_CHOICE, NODE_OPTIONAL):
Expand All @@ -156,6 +171,7 @@ def _written( # noqa: PLR0913
expanded=expanded,
definitions=definitions,
stack=stack,
config=config,
)
for branch in node.branches
]
Expand All @@ -164,13 +180,14 @@ def _written( # noqa: PLR0913
elif name in definitions and name not in stack:
pieces.append(
_written(
nodes=parse(definitions[name]),
nodes=parse(production=definitions[name], config=config),
path=(),
choice=choice,
index=index,
expanded=expanded,
definitions=definitions,
stack=(*stack, name),
config=config,
)
)
else:
Expand Down
17 changes: 10 additions & 7 deletions scripts/grammar_obligations/_helpers/derive.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
QUOTED_WITHOUT_BLANK,
QUOTED_WITHOUT_BLANK_NAMES,
)
from scripts.grammar_obligations.models import Draft, Node, Spec
from scripts.grammar_obligations.models import DialectConfig, Draft, Node, Spec


def derive(*, form: str, grammar: list[str]) -> list[Draft]:
def derive(*, config: DialectConfig, form: str, grammar: list[str]) -> list[Draft]:
"""Drafts in a fixed order: positive, then per production its parts, then form-wide cases."""
return derive_trees(form=form, grammar=grammar)[0]
return config.derive_trees(config=config, form=form, grammar=grammar)[0]


def derive_trees(
*,
config: DialectConfig,
form: str,
grammar: list[str],
unpacked: bool = False,
Expand All @@ -44,13 +45,13 @@ def derive_trees(
"""Drafts, parsed productions, skipped argument classes; unpacked gives one case per option."""
if not grammar:
return [], [], Counter()
walk: Walk = Walk(form, unpacked=unpacked, definitions=definitions)
walk: Walk = config.walk(form, config=config, unpacked=unpacked, definitions=definitions)
note: str = "the documented form with every required part and no optional part"
walk.add(kind=KIND_POSITIVE, target="", note=note, spec=walk.spec(action=ACTION_MIN_OPTIONS))
trees: list[tuple[Node, ...]] = []
for index, production in enumerate(grammar):
walk.production = index
trees.append(parse(production))
trees.append(parse(production=production, config=config))
walk.sequence(nodes=trees[-1], where="", top=True)
if index and not any(d.spec and d.spec.production == index for d in walk.drafts):
walk.add(
Expand Down Expand Up @@ -78,7 +79,9 @@ def derive_trees(
form=form,
kind=KIND_IDENTIFIER,
target=placeholder,
note=_class_note(label=label, description=description, placeholder=placeholder),
note=config.class_note(
label=label, description=description, placeholder=placeholder
),
anchor=IDENTIFIER_ANCHOR,
spec=reserved
if label == IDENTIFIER_RESERVED
Expand All @@ -99,7 +102,7 @@ def derive_trees(
return walk.drafts, trees, walk.argument_classes


def _class_note(*, label: str, description: str, placeholder: str) -> str:
def class_note(*, label: str, description: str, placeholder: str) -> str:
"""The identifier class note, blank-free for names where a blank breaks Snowflake."""
if label == IDENTIFIER_QUOTED and placeholder.strip("<>") in QUOTED_WITHOUT_BLANK_NAMES:
return f"{label}: {QUOTED_WITHOUT_BLANK}"
Expand Down
Loading
Loading