Skip to content

Commit 15b04d4

Browse files
author
Markus
committed
chore: prototype an idiomatic Lark (LALR) engine for workflow expressions
Evaluation spike for Discussion github#4917; the candidate proposed there. Adds engine_lark_a2.py and wires it into engine.py: - LALR parser with keyword terminal priority; same language as today. - The ambiguous-precedence guards are a grammar property: a filter chain only applies to an operator-free head, so a pipe after a comparison or after `not` is a parse error ("ambiguous filter precedence"). - The PATH terminal is built from namespace._PLAIN_SEGMENT / _INDEXED_SEGMENT, so path syntax has one definition; the existing drift test now covers the grammar too. - _collect_leaves / _evaluator_rejects are a single static tree walk; the probe run and _leaf_sink are gone from the contract. - _syntax_error returns the parser's message with column, which the condition diagnostics use. - Filters dispatch through FILTER_TABLE. Deliberate test changes, each with its reason in the test: - [1,2,3][1] and an unclosed quote in a condition raise ValueError instead of silently yielding None / False. - Trailing tokens after a filter name, and a comparison after a filter chain, are parse errors ("invalid expression") instead of dispatcher messages; still ValueError. - Three diagnostics fixture rows expect the parser's message. - The probe-only tests are replaced (leaf-walk transparency of groups) or removed (_leaf_sink); imports of engine internals are dropped. Measured: corpus 96/96; full test suite 11427 passed, 0 failed; owned code 334 SLOC, cognitive complexity 110 vs 166 (hardest function 31 vs 70); runtime about 1.3x the hand-written engine. Assisted-by: OpenCode (model: claude-opus-5.5, autonomous)
1 parent ce25492 commit 15b04d4

6 files changed

Lines changed: 638 additions & 74 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies = [
1515
"json5>=0.13.0",
1616
"mcp>=2.2.0,<3.0.0",
1717
"pydantic>=2.13.0,<3.0.0",
18+
"lark>=1.3",
1819
]
1920

2021
[project.scripts]

‎src/specify_cli/workflows/expressions/__init__.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
This package is split by responsibility:
88
99
- ``namespace``/``filters``/``operators``/``template`` — shared leaves;
10-
- ``engine_current`` — the hand-written evaluator behind the contract;
10+
- ``engine_lark_a2`` — the Lark (LALR) evaluator behind the contract;
1111
- ``engine`` — the selection point (the one line that swaps the engine);
1212
- ``api`` — the public evaluation entry points;
1313
- ``diagnostics`` — condition validation and remediation.
@@ -36,7 +36,6 @@
3636
_collect_leaves,
3737
_evaluator_rejects,
3838
_has_incomplete_operand,
39-
_leaf_sink,
4039
)
4140
from .filters import _REGISTERED_FILTERS
4241
from .template import _strip_stray_delimiters
@@ -57,7 +56,6 @@
5756
"_has_incomplete_operand",
5857
"_has_unbalanced_bracket",
5958
"_has_unbalanced_quote",
60-
"_leaf_sink",
6159
"_strip_stray_delimiters",
6260
"_unresolvable_term",
6361
]

‎src/specify_cli/workflows/expressions/engine.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from __future__ import annotations
1111

12-
from .engine_current import (
12+
from .engine_lark_a2 import (
1313
_COMPARISON_OPERATORS,
1414
_WORD_OPERATORS,
1515
_collect_leaves,
@@ -18,7 +18,6 @@
1818
_first_unclosable_block,
1919
_has_incomplete_operand,
2020
_is_single_expression,
21-
_leaf_sink,
2221
_syntax_error,
2322
)
2423

@@ -31,6 +30,5 @@
3130
"_first_unclosable_block",
3231
"_has_incomplete_operand",
3332
"_is_single_expression",
34-
"_leaf_sink",
3533
"_syntax_error",
3634
]

0 commit comments

Comments
 (0)