diff --git a/hclsyntax/public_test.go b/hclsyntax/public_test.go index 62809985..596b1a26 100644 --- a/hclsyntax/public_test.go +++ b/hclsyntax/public_test.go @@ -2,6 +2,8 @@ package hclsyntax import ( "testing" + + "github.com/hashicorp/hcl/v2" ) func TestValidIdentifier(t *testing.T) { @@ -44,3 +46,19 @@ func TestValidIdentifier(t *testing.T) { }) } } + +var T Tokens + +func BenchmarkLexConfig(b *testing.B) { + src := []byte("module \"once\" {\n source = \"../modules/foo\"\n}\n\nmodule \"twice\" {\n source = \"../modules/foo\"\n}\n") + filename := "testdata/dave/main.tf" + start := hcl.Pos{Line: 1, Column: 1, Byte: 0} + + var tokens Tokens + + for i := 0; i < b.N; i++ { + tokens, _ = LexConfig(src, filename, start) + } + + T = tokens +} diff --git a/hclsyntax/token.go b/hclsyntax/token.go index f4c6c934..5ef093f9 100644 --- a/hclsyntax/token.go +++ b/hclsyntax/token.go @@ -191,8 +191,10 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { toldBadUTF8 := 0 for _, tok := range tokens { - // copy token so it's safe to point to it - tok := tok + tokRange := func() *hcl.Range { + r := tok.Range + return &r + } switch tok.Type { case TokenBitwiseAnd, TokenBitwiseOr, TokenBitwiseXor, TokenBitwiseNot: @@ -211,7 +213,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { Severity: hcl.DiagError, Summary: "Unsupported operator", Detail: fmt.Sprintf("Bitwise operators are not supported.%s", suggestion), - Subject: &tok.Range, + Subject: tokRange(), }) toldBitwise++ } @@ -221,7 +223,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { Severity: hcl.DiagError, Summary: "Unsupported operator", Detail: "\"**\" is not a supported operator. Exponentiation is not supported as an operator.", - Subject: &tok.Range, + Subject: tokRange(), }) toldExponent++ @@ -234,7 +236,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { Severity: hcl.DiagError, Summary: "Invalid character", Detail: "The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<