Attach comments after the parse, so one may sit anywhere - #35
Merged
Merged
Conversation
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>
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.
include_comments=Trueparsed 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(, insideeach if, or inside a range ([0 : // why). At some of the listed points, the comment was dropped instead: inlet/forassignment 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 isast/comments.py, ported from openscad_lalr_parser, whose per-comment line counting I replaced with a binary search. Along with it:CommentLine.same_lineis added, as in lalr.CommentedExpr.__str__ends the line after a//comment.//comments trailing one expression print on two lines instead of merging into one.BOSL2, all 57 files:
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; thegetAST*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