Skip to content

Fix comment parsing to respect word boundaries around '#' - #18

Merged
david-littlefarmer merged 1 commit into
masterfrom
fix/comment-hash-word-boundary
Aug 7, 2026
Merged

Fix comment parsing to respect word boundaries around '#'#18
david-littlefarmer merged 1 commit into
masterfrom
fix/comment-hash-word-boundary

Conversation

@david-littlefarmer

@david-littlefarmer david-littlefarmer commented Aug 7, 2026

Copy link
Copy Markdown
Member

Summary

  • parseComment used a naive strings.SplitN(s, "#", 2), treating any # in a line as a comment marker. webrpc's real ridl lexer only starts a comment when # is not touching an in-progress word token — e.g. in v0.0.1#beta, the # is part of the value, not a comment. ridlfmt was silently altering parsed values by inserting a space before such a #.
  • reduceSpaces in processor.go had the same naive-hash-index bug and fed corrupted (space-squashed) input into parseComment, so it's fixed the same way.
  • Updated the two pre-existing pathological example fixtures (_examples/e1.ridl, main_test.go) that had a # glued directly to a value with no separating space — that's genuinely ambiguous per webrpc's grammar, so a space was added to make them valid, unambiguous RIDL.
  • Added formatter/comments_test.go covering the exact cases from the issue.

Fixes #17

Examples

Case 1 — # glued to a value (version=v0.0.1#beta)

Output
Input version=v0.0.1#beta
Before (bug) version = v0.0.1 # beta — split the value, changing it from v0.0.1#beta to v0.0.1
After (fixed) version = v0.0.1#beta — value preserved intact

Case 2 — trailing # with nothing after it (name =foo-bar#)

Output
Input name =foo-bar#
Before (bug) name = foo-bar # — turned the trailing # into an (empty) comment, changing the value from foo-bar# to foo-bar
After (fixed) name = foo-bar# — value preserved intact

A real inline comment (# after whitespace) is unaffected in both versions, e.g. version = v1 # my comment stays version = v1 # my comment.

Test plan

  • go build ./...
  • go test ./... (all pass, including new formatter package tests)
  • gofmt -l . / go vet ./... clean
  • Manually verified before/after behavior above against master and this branch

ridlfmt treated any '#' as a comment marker, but webrpc's ridl lexer
only starts a comment when '#' is not touching an in-progress word
token. This caused ridlfmt to split values like v0.0.1#version into a
value and a comment, altering the parsed meaning of the schema.

Fixes #17

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@david-littlefarmer
david-littlefarmer merged commit f7f9d81 into master Aug 7, 2026
1 check passed
@david-littlefarmer
david-littlefarmer deleted the fix/comment-hash-word-boundary branch August 7, 2026 10:09
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.

Inconsistent pathological comment parsing

1 participant