diff --git a/hcl/hclsyntax/parser_test.go b/hcl/hclsyntax/parser_test.go index bf94b60..99ffc5e 100644 --- a/hcl/hclsyntax/parser_test.go +++ b/hcl/hclsyntax/parser_test.go @@ -2109,6 +2109,44 @@ block "valid" {} }, }, }, + { + `a = 'str'`, + 2, // Invalid character and expression + &Body{ + Attributes: Attributes{ + "a": { + Name: "a", + Expr: &LiteralValueExpr{ + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 5, Byte: 4}, + End: hcl.Pos{Line: 1, Column: 6, Byte: 5}, + }, + }, + NameRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, + }, + EqualsRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 3, Byte: 2}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 4, Byte: 3}, + }, + }, + }, + Blocks: Blocks{}, + SrcRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, + End: hcl.Pos{Line: 1, Column: 10, Byte: 9}, + }, + EndRange: hcl.Range{ + Start: hcl.Pos{Line: 1, Column: 10, Byte: 9}, + End: hcl.Pos{Line: 1, Column: 10, Byte: 9}, + }, + }, + }, { "a = sort(data.first.ref.attr)[count.index]\n", 0, diff --git a/hcl/hclsyntax/token.go b/hcl/hclsyntax/token.go index 39c7853..c9d7442 100644 --- a/hcl/hclsyntax/token.go +++ b/hcl/hclsyntax/token.go @@ -184,6 +184,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { toldBitwise := 0 toldExponent := 0 toldBacktick := 0 + toldApostrophe := 0 toldSemicolon := 0 toldTabs := 0 toldBadUTF8 := 0 @@ -238,6 +239,19 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { if toldBacktick <= 2 { toldBacktick++ } + case TokenApostrophe: + if (toldApostrophe % 2) == 0 { + newDiag := &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid character", + Detail: "The ' character is not valid. Use double quotes (\") to enclose strings.", + Subject: &tok.Range, + } + diags = append(diags, newDiag) + } + if toldApostrophe <= 2 { + toldApostrophe++ + } case TokenSemicolon: if toldSemicolon < 1 { diags = append(diags, &hcl.Diagnostic{