Fix mid-continuation comments and Fypp token-break safety#4
Merged
Conversation
Three related fixes for Cray-compiler-safe Fortran formatting:
1. reader: strip_amp_comment_continuation now handles any amount of
whitespace between & and ! (was hardcoded to a single space), so
real-world patterns like "& ! comment" are correctly converted to
plain comment lines instead of being silently included in the joined
logical line.
2. reader: mid-continuation comments (& ! comment in the middle of a
multi-line statement) now use peek-ahead to detect whether more
continuation code follows. If so, the comment is preserved in
raw_lines but omitted from joined_parts so the statement stays
intact. If the comment is terminal, the trailing & is stripped and
the continuation ends cleanly.
3. formatter: find_token_breaks now skips entire Fypp inline
expressions (${...}$ and @{...}@) so line-break candidates are
never inserted inside a Fypp expression block.
Four targeted fixes: 1. reader: Never insert a space when joining a continuation line that begins with `%` (member accessor). Per Fortran free-form §6.3.2.4, `x &\n& %y` joins as `x%y`; inserting a space produces `x %y` which is a blank within a lexical token (§6.3.2.2) — invalid in Cray CCE and confusing everywhere. 2. formatter/find_token_breaks: Filter out any break point immediately before `%`. This prevents the formatter from ever emitting `a &\n& %member` in the output, which is the symmetric output-side fix to reader fix (1). 3. formatter/post-processing rewrap pass: Skip continuation lines (lines whose trimmed content starts with `& `). Rewrapping them individually — without the full logical-line context — can move a trailing comment above the line, placing it between two `&` continuation lines and creating an illegal mid-continuation comment that Cray ftn (ftn-71) rejects. 4. formatter/join_short_comments: Only merge consecutive comment lines when the combined text fits on a single line. Previously the pass would always merge and rewrap, which caused a trailing comment moved above a code line by rewrap_line to be silently merged into an unrelated preceding standalone comment.
Long logical conditions like `if (a .or. b .or. c .or. d)` that exceeded
the line length limit had no valid break point and were emitted as-is.
Breaking before each dot-operator produces idiomatic Fortran wrapping:
if (a .or. b .or. c &
& .or. d) then
Skips .not. (unary) and .true./.false. (literals) to avoid spurious breaks.
Fix CI failure from cargo fmt check.
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.
Fixes #3
Summary
& ! commentwith any whitespace:strip_amp_comment_continuationwas matching only"& !"(exactly one space). Real MFC code uses"& !"(two or more spaces). Now usestrimmed[1..].trim_start()to handle any amount of whitespace between&and!.Mid-continuation comments: When a
& ! commentline appears in the middle of a multi-line continuation (more& codefollows), the old reader terminated the continuation there, leaving subsequent& arg3, arg4lines as orphaned invalid Fortran. Now a peek-ahead scan detects whether more continuation code follows — if so, the comment is dropped fromjoined_partsbut preserved inraw_lines, keeping the logical statement intact. If the comment is terminal, the trailing&is stripped and the continuation ends cleanly.Fypp expression safety in
find_token_breaks: Break candidates were computed byte-by-byte without skipping${...}$/@{...}@Fypp inline expressions. A wrap could be inserted inside an expression, producing invalid Fypp output. Now the scanner skips the entire block when it encounters${or@{.Test plan
cargo testpasses (all 47 tests green)tests/fixtures/trailing_comment_wrap.*updated with mid-continuation comment cases covering single-comment and multi-continuation scenarios& ! get z derivative ...(two-space variant) now stripped correctly before joining