Skip to content

Lexer: keep last line of a C code file lacking a final newline - #3089

Merged
willend merged 1 commit into
mainfrom
lexer-unterminated-last-line
Nov 11, 2026
Merged

willend merged 1 commit into
mainfrom
lexer-unterminated-last-line

Conversation

@willend

@willend willend commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Free-form text area

TL;DR: A %included library (or other C code file) whose last line has no trailing newline lost that line in the generated C. McStas echoed it to stdout instead, typically dropping a closing #endif or } and breaking compilation. This adds one lexer rule so the line is kept.

Found with the ESS BIFROST model (niess / mccode-antlr), where rfc-lib.h and rfc-lib.c end without a newline:

INFO: Regenerating c-file: bifrost.c
#endif // RFC_LIB_H}
...
./bifrost.c:14238:62: error: function definition is not allowed here

Change: one new <ccode> rule in mccode/src/instrument.l. It can only be the longest match on an unterminated last line; it pushes the line back with a newline appended, so the existing rules (%}, %include, plain code line) handle it. Details in docs/GRAMMAR/ADR-records/ADR_20261111_UNTERMINATED_LAST_LINE.md.

Test output (code generation only, patched vs. unpatched mcstas built from this branch / main):

  • Unmodified BIFROST model: the brace/#endif errors are gone. (Its remaining errors are unrelated and handled upstream.)
  • Generated C compared for the 369 McStas example instruments: 365 identical; 2 fail to generate both before and after (ESS_butterfly_MCPL_test, Test_Dispersion_relation_TAS, not investigated).
  • 2 differ by one restored line (Supermirror_thin_substrate, Test_lib_supermirror_lib): mcstas-comps/share/supermirror-lib.h also lacks a final newline, and its closing comment was silently dropped until now. Comment only, no behaviour change.
  • Two synthetic headers ending in an unterminated %include "read_table-lib" generate and pass a compiler syntax check.

Not tested: McXtrace (same lexer file, not built), Linux, Windows, and no compile-and-run of the example instruments.

Related upstream fix that adds the newlines: mcdotstar/mcstas-radial-filter-collimator#11


Declaration of use of AI-tools

  • Please add a checkmark here if you used AI-tools during the work for this contribution
  • Furter, please describe how / where and for what the tools were used: Claude Code was used to diagnose the failure, write the lexer rule and the ADR, and run the before/after comparison.

Development OS / boundary conditions

macOS arm64 (clang), flex/bison from conda-forge. No new dependencies.


PR Checklist for contributing to McStas/McXtrace

  • My work touches the code-generator in mccode/src

    • I have added reasoning and documentation for the change through an ADR record in our GRAMMAR section
    • I am attaching test output in the comments

🤖 Generated with Claude Code

Such a line matched no <ccode> rule and was echoed to stdout instead of
reaching the generated C, e.g. losing a closing #endif or }.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@willend
willend force-pushed the lexer-unterminated-last-line branch from 5037dc5 to c0c84d4 Compare November 11, 2026 09:05
@willend

willend commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

mccode-antlr parity: no collateral effect expected. mccode-antlr (0.36.0) already keeps a last line that lacks a newline, so this PR brings the C code generator in line with it.

Test: %include "nonl-lib" in DECLARE, where nonl-lib.h ends in #endif // NONL_H_END and nonl-lib.c in } // NONL_C_END, both without a final newline.

code generator both last lines in generated C
mcstas from main no (echoed to stdout as #endif // NONL_H_END} // NONL_C_END)
mcstas from this PR yes
mcstas-antlr 0.36.0 yes

Tested on macOS arm64 only.

🤖 Generated with Claude Code

@willend
willend merged commit db19118 into main Nov 11, 2026
27 checks passed
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