Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
25 changes: 25 additions & 0 deletions percent_rxs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading