Skip to content
Open
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
60 changes: 60 additions & 0 deletions lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package crossplane

import (
"os"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -446,6 +447,65 @@ func TestLex(t *testing.T) {
}
}

func TestLex_setByLuaBlockOpeningBraceDelimiter(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
config string
expectedArg string
}{
{
name: "whitespace before opening brace",
config: `set_by_lua_block $pass { return { ok = true } }`,
expectedArg: "$pass",
},
{
name: "opening brace adjacent to output variable",
config: `set_by_lua_block $pass{ return { ok = true } }`,
expectedArg: "$pass",
},
{
name: "whitespace after braced variable",
config: `set_by_lua_block ${pass} { return { ok = true } }`,
expectedArg: "${pass}",
},
{
name: "opening brace adjacent to braced variable",
config: `set_by_lua_block ${pass}{ return { ok = true } }`,
expectedArg: "${pass}",
},
}

for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

var actual []string
options := LexOptions{
Lexers: []RegisterLexer{lua.RegisterLexer()},
}
for token := range LexWithOptions(strings.NewReader(testcase.config), options) {
if token.Error != nil {
t.Fatal(token.Error)
}
actual = append(actual, token.Value)
}

expected := []string{
setByLuaBlock,
testcase.expectedArg,
" return { ok = true } ",
";",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected tokens %#v, got %#v", expected, actual)
}
})
}
}

func TestLex_unhappy(t *testing.T) {
t.Parallel()

Expand Down
33 changes: 21 additions & 12 deletions lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func (l *Lua) Lex(s *SubScanner, matchedToken string) <-chan NgxToken {
return
}
next := s.Text()
// NGINX treats an opening brace as the end of the current argument,
// except when it starts a parameter expansion such as ${variable}.
if next == "{" && arg != "" && !strings.HasSuffix(arg, "$") {
tokenCh <- NgxToken{Value: arg, Line: s.Line(), IsQuoted: false}
tokenDepth++
break
}
if isSpace(next) {
if arg != "" {
tokenCh <- NgxToken{Value: arg, Line: s.Line(), IsQuoted: false}
Expand All @@ -90,20 +97,22 @@ func (l *Lua) Lex(s *SubScanner, matchedToken string) <-chan NgxToken {
}

// check that Lua block starts correctly
for {
if !s.Scan() {
return
}
next := s.Text()

if !isSpace(next) {
if next != "{" {
lineno := s.Line()
tokenCh <- NgxToken{Error: &ParseError{File: &lexerFile, What: `expected "{" to start lua block`, Line: &lineno}}
if tokenDepth == 0 {
for {
if !s.Scan() {
return
}
tokenDepth++
break
next := s.Text()

if !isSpace(next) {
if next != "{" {
lineno := s.Line()
tokenCh <- NgxToken{Error: &ParseError{File: &lexerFile, What: `expected "{" to start lua block`, Line: &lineno}}
return
}
tokenDepth++
break
}
}
}

Expand Down