From 9d62e763155942328b2f428886d99bfb89714410 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 3 Jul 2026 12:15:19 +0200 Subject: [PATCH] feat(lexer): preserve the /o "interpolate once" regexp flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The lexer collected only i/m/x from a regexp literal's trailing flags and silently dropped o (and the encoding flags). Downstream VMs need /o to implement Ruby's "interpolate once" semantics — compile an interpolated literal a single time and reuse that object — so preserve o in Flags at both the /…/ and %r{…} scan sites. Encoding flags (n/u/e/s) stay consumed-but- unrecorded, unchanged. Co-Authored-By: Claude Opus 4.8 --- lexer/lexer.go | 9 +++++---- percent_rxs_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lexer/lexer.go b/lexer/lexer.go index 959b5ab..fc49623 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -997,8 +997,9 @@ func (l *Lexer) lexIvar(spaceBefore bool, line, col int) token.Token { // lexRegexp lexes a /pattern/flags regexp literal. The opening '/' is at the // cursor. Escapes are preserved verbatim into the source (so \d, \. and the // like reach the engine untouched) except that an escaped delimiter \/ becomes -// a literal '/'. Trailing flag letters i, m, x are collected into Flags; any -// other trailing letters are ignored gracefully (consumed but not recorded). +// a literal '/'. Trailing flag letters i, m, x and o (the "interpolate once" +// flag) are collected into Flags; any other trailing letters (e.g. the encoding +// flags n/u/e/s) are ignored gracefully (consumed but not recorded). func (l *Lexer) lexRegexp(spaceBefore bool, line, col int) token.Token { l.advance() // opening '/' var src []byte @@ -1043,7 +1044,7 @@ func (l *Lexer) lexRegexp(spaceBefore bool, line, col int) token.Token { break } l.advance() - if c == 'i' || c == 'm' || c == 'x' { + if c == 'i' || c == 'm' || c == 'x' || c == 'o' { flags = append(flags, c) } } @@ -1535,7 +1536,7 @@ func (l *Lexer) lexPercentRXS(spaceBefore bool, line, col int) token.Token { break } l.advance() - if c == 'i' || c == 'm' || c == 'x' { + if c == 'i' || c == 'm' || c == 'x' || c == 'o' { flags = append(flags, c) } } diff --git a/percent_rxs_test.go b/percent_rxs_test.go index 0c66fff..150a19d 100644 --- a/percent_rxs_test.go +++ b/percent_rxs_test.go @@ -49,6 +49,31 @@ func TestPercentRegexpNoFlags(t *testing.T) { } } +func TestRegexpOnceFlag(t *testing.T) { + // The /o ("interpolate once") flag is preserved so the VM can compile an + // interpolated literal a single time. It sorts after i/m/x in source order. + rx := parseOne(t, "/a#{x}b/io").(*ast.RegexpLit) + if rx.Source != "a#{x}b" || rx.Flags != "io" { + t.Errorf("got %q/%q, want a#{x}b/io", rx.Source, rx.Flags) + } +} + +func TestPercentRegexpOnceFlag(t *testing.T) { + rx := parseOne(t, "%r{a#{x}b}mo").(*ast.RegexpLit) + if rx.Source != "a#{x}b" || rx.Flags != "mo" { + t.Errorf("got %q/%q, want a#{x}b/mo", rx.Source, rx.Flags) + } +} + +func TestRegexpEncodingFlagsStillDropped(t *testing.T) { + // The encoding flags n/u/e/s remain consumed-but-unrecorded (as before), so a + // bare /x/n exposes no flags — only i/m/x/o are meaningful to the VM. + rx := parseOne(t, "/x/n").(*ast.RegexpLit) + if rx.Flags != "" { + t.Errorf("Flags = %q, want empty (encoding flag n dropped)", rx.Flags) + } +} + func TestPercentXString(t *testing.T) { xs, ok := parseOne(t, "%x{ls}").(*ast.XStr) if !ok {