mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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
|
||||
{
|
||||
|
||||
+1260
-1248
File diff suppressed because it is too large
Load Diff
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user