Preserve protocol blocks and quoted whitespace during BNGL round-trip#88
Open
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
Open
Conversation
Two coupled fidelity fixes in the BNGL → BNGL round-trip path:
1. ``begin protocol`` / ``end protocol`` blocks are now preserved.
Protocol blocks live INSIDE ``begin model`` / ``end model`` and hold a
sequence of state-mutating action lines (``setParameter``,
``setConcentration``, ``simulate``, etc.) that BNG2.pl executes when
``parameter_scan({method=>"protocol"})`` is invoked. Previously,
``BNGFile.strip_actions`` would extract every action-shaped line from
the file — including those inside a protocol block — and BNGParser
would route them into the top-level ``ActionBlock``, so the
regenerated BNGL had no protocol block at all and BNG2.pl ran the
actions at the wrong level.
Fix:
- New ``ProtocolBlock(ActionBlock)`` in ``blocks.py``: same item-list
shape as ``ActionBlock`` but emits its content wrapped in
``begin protocol`` / ``end protocol``.
- ``bngmodel`` registers ``"protocol"`` in ``_block_order`` between
``rules`` and ``actions`` so it lands inside the model body, and
exposes ``add_protocol_block`` for the parser's dispatch by name.
- ``BNGFile.strip_actions`` walks lines tracking an ``in_protocol``
flag and routes action-shaped lines into ``parsed_protocol_actions``
instead of ``parsed_actions`` while inside a protocol block. The
``begin protocol`` / ``end protocol`` delimiter lines themselves
stay in the stripped output so BNG2.pl re-emits the wrapper.
- ``BNGParser.parse_actions`` now invokes a shared
``_parse_action_block`` helper twice — once with ``ActionBlock`` for
the top-level actions and once with ``ProtocolBlock`` for the
protocol-block actions.
2. Quoted whitespace inside action arguments is preserved.
Previously ``strip_actions`` ran ``line.replace(" ", "")`` on each
action line, and ``BNGParser.parse_actions`` re-ran ``re.sub(r"\s", "",
action)``. Both were quote-naive, so an action like
simulate({method=>"nf",param=>"-v -gml 1000000"})
came out as
simulate({method=>"nf",param=>"-v-gml1000000"})
and NFsim choked on the broken argument string.
The space-collapse is now a quote-aware pass
(``_normalize_action_text`` + ``_strip_comment_outside_quotes`` +
``_collapse_unquoted_whitespace``) that lives module-level in
``bngparser.py``. Tokens inside ``"..."`` / ``'...'`` survive intact;
unquoted whitespace and trailing ``# comments`` are still stripped as
before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two coupled fidelity fixes in the BNGL → BNGL round-trip path. The two share the same `parse_actions` refactor, so they're bundled.
1. `begin protocol` / `end protocol` blocks are now preserved
Protocol blocks live INSIDE `begin model` / `end model` and hold a sequence of state-mutating action lines (`setParameter`, `setConcentration`, `simulate`, etc.) that BNG2.pl executes when `parameter_scan({method=>"protocol"})` is invoked.
Previously, `BNGFile.strip_actions` extracted every action-shaped line from the file — including those inside a protocol block — and `BNGParser` routed them into the top-level `ActionBlock`. The regenerated BNGL had no protocol block at all, and BNG2.pl ran the actions at the wrong level.
Fix:
2. Quoted whitespace inside action arguments is preserved
Previously `strip_actions` ran `line.replace(" ", "")` on each action line, and `BNGParser.parse_actions` re-ran `re.sub(r"\s", "", action)`. Both were quote-naive, so an action like
```
simulate({method=>"nf",param=>"-v -gml 1000000"})
```
came out as
```
simulate({method=>"nf",param=>"-v-gml1000000"})
```
and NFsim choked on the broken argument string.
The space-collapse is now a quote-aware pass (`_normalize_action_text` + `_strip_comment_outside_quotes` + `_collapse_unquoted_whitespace`) that lives module-level in `bngparser.py`. Tokens inside `"..."` / `'...'` survive intact; unquoted whitespace and trailing `# comments` are still stripped as before.
Test plan