Skip to content

Fix mid-continuation comments and Fypp token-break safety#4

Merged
sbryngelson merged 5 commits into
masterfrom
fix/mid-continuation-comments-and-fypp
May 12, 2026
Merged

Fix mid-continuation comments and Fypp token-break safety#4
sbryngelson merged 5 commits into
masterfrom
fix/mid-continuation-comments-and-fypp

Conversation

@sbryngelson
Copy link
Copy Markdown
Owner

Fixes #3

Summary

  • & ! comment with any whitespace: strip_amp_comment_continuation was matching only "& !" (exactly one space). Real MFC code uses "& !" (two or more spaces). Now uses trimmed[1..].trim_start() to handle any amount of whitespace between & and !.

  • Mid-continuation comments: When a & ! comment line appears in the middle of a multi-line continuation (more & code follows), the old reader terminated the continuation there, leaving subsequent & arg3, arg4 lines as orphaned invalid Fortran. Now a peek-ahead scan detects whether more continuation code follows — if so, the comment is dropped from joined_parts but preserved in raw_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 test passes (all 47 tests green)
  • tests/fixtures/trailing_comment_wrap.* updated with mid-continuation comment cases covering single-comment and multi-continuation scenarios
  • Real MFC pattern & ! get z derivative ... (two-space variant) now stripped correctly before joining

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.
@sbryngelson sbryngelson merged commit 7e0af15 into master May 12, 2026
9 checks passed
@sbryngelson sbryngelson deleted the fix/mid-continuation-comments-and-fypp branch May 14, 2026 13:00
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.

Broken code on some compilers after formatting

1 participant