Skip to content

Print codepoints in generated guards as character literals - #158

Merged
josevalim merged 2 commits into
dashbitco:masterfrom
dkuku:dk/guard-char-literals
Aug 13, 2026
Merged

Print codepoints in generated guards as character literals#158
josevalim merged 2 commits into
dashbitco:masterfrom
dkuku:dk/guard-char-literals

Conversation

@dkuku

@dkuku dkuku commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

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

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>
Comment thread lib/nimble_parsec/compiler.ex Outdated
?\t -> "?\\t"
?\r -> "?\\r"
codepoint when codepoint in ?!..?~ -> "?" <> <<codepoint>>
codepoint -> "0x" <> String.pad_leading(Integer.to_string(codepoint, 16), 2, "0")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just fallback to a regular number in this case?

@josevalim
josevalim merged commit 6f00961 into dashbitco:master Aug 13, 2026
3 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

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