Repository navigation
Fix pretty() of constants so the output recompiles to the same model - #430
Open
binggao1230 wants to merge 1 commit into
Open
binggao1230 wants to merge 1 commit into
binggao1230 wants to merge 1 commit into
Conversation
Constant._pretty() wrote every literal bare between single backticks.
Values holding a backtick or a line break produced grammars that fail
to recompile, and values that read back through the 'literal' rule were
silently reinterpreted ('42' -> 42, 'True' -> True, ' x' -> 'x').
Alert inherits the rendering.
Pick a source form that reads back to the same value: bare when safe,
repr() when a single quote kind suffices, and an escaped '''...'''
string when the value holds both kinds (multiline strings are trimmed
and stripped when read back, so line breaks, tabs, and end whitespace
are escaped as well).
Contributor
Congrats! CodSpeed is installed 🎉
You will start to see performance impacts in the reports once the benchmarks are run from your default branch.
|
This branch has not been deployed
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.
Constant._pretty()writes every literal bare between single backticks, which breaks the pretty round trip (compile(pretty(g))==compile(g)) on current main (8f9d741) in three ways:and silently, because the
'' =literal ''alternative of theconstantrule is tried before the raw/(.*?)/path:Same for
'True'/'None'/'true'/'4.5', and' x'loses its leading space to thewordrule.Alert._pretty()delegates toConstant, so alerts inherit all of this.The fix picks a source form, from the constant syntax the grammar already accepts, that reads back to the same value:
repr()of the value when one quote kind suffices — escaped quotes are not an option because the lazy string patterns stop at the first quote character, so repr's delimiter choice is what makes this parse;'''...'''string, with quotes, backslashes, line breaks, tabs, and end whitespace escaped, when the value holds both quote kinds —multiline_stringsemantics applytrim(value.strip()).rstrip()beforeeval_escapes, so anything those touch has to be escaped.Tests: a
compile -> pretty -> compileharness asserting model equality (asjson) plus a pretty fixpoint, parametrized over 39 grammars covering the shapes above, and a check that already-stable literals keep their compact bare form. I ran the same round trip across the other node types (tokens, patterns, closures, joins, gathers, lookaheads, overrides, named, directives, comments): clean, except two pre-existing Pattern cases unrelated to this change (multiline patterns containing/fall back to?"...", which cannot hold a raw newline, and multiline pattern text drifts to thetrim()indentation fixpoint — the "weird indentation" note in grammar/tatsu.ebnf). Happy to file those separately.uv run pytest tests: 641 passed, 6 skipped.ruff check --previewandty checkclean.