Skip to content

Commit 8b49116

Browse files
nobuk0kubun
authored andcommitted
[Bug #21186] multibyte char literal should be a single letter word
[Backport #21186]
1 parent 6ca43e9 commit 8b49116

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

parse.y

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10047,6 +10047,7 @@ parse_qmark(struct parser_params *p, int space_seen)
1004710047
rb_encoding *enc;
1004810048
register int c;
1004910049
rb_parser_string_t *lit;
10050+
const char *start = p->lex.pcur;
1005010051

1005110052
if (IS_END()) {
1005210053
SET_LEX_STATE(EXPR_VALUE);
@@ -10071,13 +10072,11 @@ parse_qmark(struct parser_params *p, int space_seen)
1007110072
}
1007210073
newtok(p);
1007310074
enc = p->enc;
10074-
if (!parser_isascii(p)) {
10075-
if (tokadd_mbchar(p, c) == -1) return 0;
10076-
}
10077-
else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
10078-
!lex_eol_p(p) && is_identchar(p, p->lex.pcur, p->lex.pend, p->enc)) {
10075+
int w = parser_precise_mbclen(p, start);
10076+
if (is_identchar(p, start, p->lex.pend, p->enc) &&
10077+
!(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
1007910078
if (space_seen) {
10080-
const char *start = p->lex.pcur - 1, *ptr = start;
10079+
const char *ptr = start;
1008110080
do {
1008210081
int n = parser_precise_mbclen(p, ptr);
1008310082
if (n < 0) return -1;
@@ -10105,7 +10104,7 @@ parse_qmark(struct parser_params *p, int space_seen)
1010510104
}
1010610105
}
1010710106
else {
10108-
tokadd(p, c);
10107+
if (tokadd_mbchar(p, c) == -1) return 0;
1010910108
}
1011010109
tokfix(p);
1011110110
lit = STR_NEW3(tok(p), toklen(p), enc, 0);

test/ruby/test_parse.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ def test_question
657657
assert_equal("\u{1234}", eval('?\u{1234}'))
658658
assert_equal("\u{1234}", eval('?\u1234'))
659659
assert_syntax_error('?\u{41 42}', 'Multiple codepoints at single character literal')
660+
assert_syntax_error("?and", /unexpected '\?'/)
661+
assert_syntax_error("?\u1234and", /unexpected '\?'/)
660662
e = assert_syntax_error('"#{?\u123}"', 'invalid Unicode escape')
661663
assert_not_match(/end-of-input/, e.message)
662664

0 commit comments

Comments
 (0)