diff --git a/config/expr_lex.go b/config/expr_lex.go index 9c9d45e16..f94e947ba 100644 --- a/config/expr_lex.go +++ b/config/expr_lex.go @@ -14,9 +14,9 @@ const lexEOF = 0 // The parser uses the type Lex as a lexer. It must provide // the methods Lex(*SymType) int and Error(string). type exprLex struct { - Err error + Err error + Input string - input string pos int width int } @@ -105,12 +105,12 @@ func (x *exprLex) lexString(yylval *exprSymType) int { // Return the next rune for the lexer. func (x *exprLex) next() rune { - if int(x.pos) >= len(x.input) { + if int(x.pos) >= len(x.Input) { x.width = 0 return lexEOF } - r, w := utf8.DecodeRuneInString(x.input[x.pos:]) + r, w := utf8.DecodeRuneInString(x.Input[x.pos:]) x.width = w x.pos += x.width return r diff --git a/config/expr_lex_test.go b/config/expr_lex_test.go new file mode 100644 index 000000000..8e94d86f3 --- /dev/null +++ b/config/expr_lex_test.go @@ -0,0 +1,51 @@ +package config + +import ( + "io/ioutil" + "path/filepath" + "reflect" + "testing" +) + +func TestLex(t *testing.T) { + cases := []struct { + Input string + Output []int + }{ + { + "concat.hcl", + []int{IDENTIFIER, LEFTPAREN, + STRING, COMMA, STRING, COMMA, STRING, + RIGHTPAREN, lexEOF}, + }, + } + + for _, tc := range cases { + d, err := ioutil.ReadFile(filepath.Join( + fixtureDir, "interpolations", tc.Input)) + if err != nil { + t.Fatalf("err: %s", err) + } + + l := &exprLex{Input: string(d)} + var actual []int + for { + token := l.Lex(new(exprSymType)) + actual = append(actual, token) + + if token == lexEOF { + break + } + + if len(actual) > 500 { + t.Fatalf("Input:%s\n\nExausted.", tc.Input) + } + } + + if !reflect.DeepEqual(actual, tc.Output) { + t.Fatalf( + "Input: %s\n\nBad: %#v\n\nExpected: %#v", + tc.Input, actual, tc.Output) + } + } +} diff --git a/config/expr_parse.go b/config/expr_parse.go index bfb81c6fb..c6fb39f87 100644 --- a/config/expr_parse.go +++ b/config/expr_parse.go @@ -21,7 +21,7 @@ func ExprParse(v string) (Interpolation, error) { exprResult = nil // Parse - lex := &exprLex{input: v} + lex := &exprLex{Input: v} exprParse(lex) // Build up the errors diff --git a/config/test-fixtures/interpolations/concat.hcl b/config/test-fixtures/interpolations/concat.hcl new file mode 100644 index 000000000..aab6e50aa --- /dev/null +++ b/config/test-fixtures/interpolations/concat.hcl @@ -0,0 +1 @@ +concat("foo","-","0.0/16")