Skip to content

Stop a backslash-newline in a string leaking to stdout - #4

Merged
revarbat merged 1 commit into
mainfrom
fix-string-line-continuation
Aug 10, 2026
Merged

Stop a backslash-newline in a string leaking to stdout#4
revarbat merged 1 commit into
mainfrom
fix-string-line-continuation

Conversation

@revarbat

Copy link
Copy Markdown
Member

Flex's . never matches a newline, so <STR>\\. could not match a backslash escaping the end of a line. With no rule for it, the backslash fell through to flex's default ECHO rule and was printed straight to stdout — a stray \ on every parse of such a file, in the middle of whatever the program was writing. The newline was then picked up by the content-run rule and became part of the string.

s = "a \
b";

Both characters are now matched and kept verbatim in the literal.

Why kept, not dropped

StringLiteral::toString() wraps the stored text in quotes to reproduce the source. Cooking escapes here would make the pretty-printer emit real newlines and quotes and corrupt the file it was formatting. Resolving escapes belongs where the literal is evaluated; this side's job is to describe the source faithfully.

Testing

Two tests: the literal keeps \+LF and \+CRLF verbatim, and the continued line still advances the line number (YY_USER_ACTION counts newlines per rule, so this comes for free — but it would be silent if it broke, and every position after a multi-line string would be one line short).

632 tests pass. Negative-controlled: removing the rule fails the verbatim test, and the stray \ reappears in the test runner's own output.

Flex's `.` never matches a newline, so `<STR>\\.` could not match a
backslash escaping the end of a line. With no rule for it the backslash
fell through to flex's DEFAULT ECHO rule and was printed straight to
stdout -- a stray `\` on every parse of such a file, in the middle of
whatever the program was writing. The newline was then picked up by the
content-run rule and became part of the string.

Both characters are now matched and kept verbatim in the literal.

Kept rather than dropped on purpose. StringLiteral::toString() wraps the
stored text in quotes to reproduce the source, so cooking escapes here
would make the pretty-printer emit real newlines and quotes and corrupt
the file it was formatting. Resolving escapes belongs where the literal
is evaluated; this side's job is to describe the source faithfully.
@revarbat
revarbat merged commit b621014 into main Aug 10, 2026
3 checks passed
@revarbat
revarbat deleted the fix-string-line-continuation branch August 10, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant