Skip to content

Attach comments after the parse, so one may sit anywhere - #35

Merged
revarbat merged 1 commit into
mainfrom
fix/comment-capture
Sep 26, 2026
Merged

revarbat merged 1 commit into
mainfrom
fix/comment-capture

Conversation

@revarbat

Copy link
Copy Markdown
Member

include_comments=True parsed with a grammar that allowed a comment only at the points it listed. A comment anywhere else was a syntax error: in a ternary, after a (, inside each if, or inside a range ([0 : // why). At some of the listed points, the comment was dropped instead: in let/for assignment lists, and between a call and its child. 19 of BOSL2's 57 files failed to parse with comments kept.

The fix. The source is now parsed with comments treated as whitespace, the same way every file already parses without them. Each comment is then placed by its position: into a statement list, onto an expression as a CommentedExpr, or into a declaration's comment fields. That code is ast/comments.py, ported from openscad_lalr_parser, whose per-comment line counting I replaced with a binary search. Along with it:

  • CommentLine.same_line is added, as in lalr.
  • CommentedExpr.__str__ ends the line after a // comment.
  • Two // comments trailing one expression print on two lines instead of merging into one.
  • Blank lines between comments in a block are kept, as before.

BOSL2, all 57 files:

before after
parse with comments 38 57
printed output reparses — 57
comments kept — 45,174 / 45,174
stable on a second print — 57
same program with comments stripped — 57

Attaching comments costs at most about 10% on BOSL2's largest files. getOpenSCADParser(include_comments=True) is left as it was for callers using its parse trees; the getAST* functions no longer use it.

1,059 tests pass, with no expectation changed. The 10 new tests cover the cases that failed.

🤖 Generated with Claude Code

include_comments=True parsed with a grammar that allowed a comment only at
the points it listed; anywhere else -- in a ternary, after a '(', inside
'each if', mid-range -- was a syntax error, and some listed points dropped
the comment. 19 of BOSL2's 57 files failed to parse with comments kept.

The source is now parsed with comments as whitespace and each comment
placed by position afterwards (ast/comments.py, ported from
openscad_lalr_parser, with its per-comment line counting made a bisect).
CommentLine gains same_line; CommentedExpr.__str__ ends the line after a
// comment; two // comments trailing one expression print on two lines.

All of BOSL2 now parses with comments, reprints to code that reparses,
keeps all 45,174 comments, is stable on a second reprint, and is the same
program with comments stripped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@revarbat
revarbat merged commit 7cfbafa into main Sep 26, 2026
6 checks passed
@revarbat
revarbat deleted the fix/comment-capture branch September 26, 2026 10:39
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