mruby-compiler: accept double-quoted strings in case/in patterns

The p_value rule only accepted bare tSTRING tokens, which the lexer
emits for single-quoted strings.  Double-quoted strings emit
tSTRING_BEG ... tSTRING (or with interpolation, tSTRING_BEG
string_rep tSTRING), so

  case "hello"
  in "hello"
    :match
  end

raised "syntax error, unexpected string literal" at the `"` after
`in`.  Use the existing `string` non-terminal instead of bare
tSTRING, which also enables alternation (`"a" | "b"`), interpolation
(`"hel#{x}"`), and concatenation by juxtaposition in patterns.

close #6830

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-05-16 22:01:52 +09:00
parent b059f3df25
commit 40264c9aad
3 changed files with 1288 additions and 1250 deletions
+2 -2
View File
@@ -3987,9 +3987,9 @@ p_value : p_var
{
$$ = new_pat_value(p, $1);
}
| tSTRING
| string
{
$$ = new_pat_value(p, new_str(p, list1($1)));
$$ = new_pat_value(p, new_str(p, $1));
}
| keyword_nil
{
File diff suppressed because it is too large Load Diff
+26
View File
@@ -981,6 +981,32 @@ assert('pattern matching - basic case/in') do
assert_equal :other, result
end
assert('pattern matching - string literal patterns') do
result = case "hello"
in "hello" then :match
end
assert_equal :match, result
# double-quoted vs single-quoted should both work
result = case 'world'
in "world" then :double
end
assert_equal :double, result
# alternation with strings
result = case "ab"
in "ab" | "cd" then :alt
end
assert_equal :alt, result
# string interpolation in pattern
expected = "lo"
result = case "hello"
in "hel#{expected}" then :interp
end
assert_equal :interp, result
end
assert('pattern matching - array patterns') do
# simple array pattern
case [1, 2, 3]