Skip to content

Proposal - use guards in expanded code - #159

Closed
dkuku wants to merge 4 commits into
dashbitco:masterfrom
dkuku:dk/char-guards
Closed

Proposal - use guards in expanded code#159
dkuku wants to merge 4 commits into
dashbitco:masterfrom
dkuku:dk/char-guards

Conversation

@dkuku

@dkuku dkuku commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

I think the readme update explains it best.
It generates guards for the function heads to avoid repetition.
I tested it on cldr parser and there is almost 500 lines less on a 9k lines file - the code also is more readable.

dkuku and others added 4 commits August 13, 2026 16:09
While reading the generated files I had trouble parsing the ASCII codes in
my head. `?a` and `97` are the same AST node, though, so the literal
spelling has to be carried as `:token` metadata for `Macro.to_string/1` to
print it.

This makes `debug: true` and `mix nimble_parsec.compile` output readable at
a glance. For codepoints with no printable spelling I use hex instead -
`0xB80`, similar to `U+0B80`.

Assisted-by: Claude Opus 5
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A range set expanding to three or more comparisons, or matching a
well-known class, is emitted once per module as a defguardp and shared by
every parser using it. Guards are named after the class they match, or
after a hash of the ranges, so the same set always resolves to the same
guard regardless of compilation order.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@josevalim

Copy link
Copy Markdown
Member

Thanks but I’d rather not go down this path. The code is not meant to be readable, the guards are going to be macro expanded anyway (so it won’t be faster), and it adds more code to the compiler, so the benefits are not there. The $ token changes would not hurt though. :)

@dkuku

dkuku commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

ok, closing this one.

@dkuku dkuku closed this Aug 13, 2026
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.

2 participants