Commit 176f895
committed
Allow non-ASCII characters in identifiers
Ruby accepts identifiers with non-ASCII code points: `Ú` and `Ω` are
valid constants, `únicos` and `日本語` are valid local identifiers. The
RBS lexer only recognised ASCII, so the same names could not be written
in a signature file. See rubocop/rubocop-on-rbs#151.
`rbs_next_char` already folded every non-ASCII character into a single
sentinel code point. It now reports one of two, chosen by the encoding's
`isupper_char` — the test Ruby's `rb_sym_constant_char_p` uses to tell a
constant from a local — and the grammar matches those two classes
directly. Widening `word` also lets every existing rule accept non-ASCII
continuation characters.
Deciding this in `rbs_next_char` rather than in a lexer action covers
encodings where a non-ASCII character is a single byte: in ISO-8859-1,
`À` is 0xC0 and `à` is 0xE0, and the old `byte_len == 1` branch passed
both through as raw bytes that matched no identifier rule. It costs one
`isupper_char` call per non-ASCII character rather than one per
identifier; over the bundled core and stdlib that is below the noise
floor, and even a corpus that is 46% Japanese prose is only ~12% slower.
Builds on #2983: the two sentinels are distinct from the invalid-byte
sentinel, so an invalid byte inside an identifier still surfaces as an
ErrorToken rather than being absorbed.
ASCII-8BIT needs an exception. It carries bytes rather than characters
and its `char_width` reports 1 for every byte, so folding "every valid
non-ASCII character" would turn arbitrary binary into identifiers.
`TypeName#kind` answers the same question for names that never pass
through the lexer, so it has to agree. Ask Prism — already a hard
dependency — instead of restating Ruby's rule with regexps, which covers
titlecase letters and non-UTF-8 names that a regexp got wrong. Only the
leading `_` case stays, since that is RBS's own interface convention.
`rust/ruby-rbs` has no Prism to defer to and keeps an explicit test.
src/lexer.c is regenerated with re2c 4.5.1.1 parent 432d955 commit 176f895
9 files changed
Lines changed: 2279 additions & 1939 deletions
File tree
- include/rbs
- lib/rbs
- rust/ruby-rbs/src
- src
- test/rbs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
63 | 81 | | |
64 | 82 | | |
65 | 83 | | |
| |||
306 | 324 | | |
307 | 325 | | |
308 | 326 | | |
309 | | - | |
310 | | - | |
311 | | - | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
312 | 330 | | |
313 | | - | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
314 | 342 | | |
315 | 343 | | |
316 | 344 | | |
| |||
610 | 638 | | |
611 | 639 | | |
612 | 640 | | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
613 | 667 | | |
614 | 668 | | |
615 | 669 | | |
| |||
0 commit comments