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 {