MCPcopy
hub / github.com/hashicorp/hcl / checkInvalidTokens

Function checkInvalidTokens

hclsyntax/token.go:186–323  ·  view source on GitHub ↗

checkInvalidTokens does a simple pass across the given tokens and generates diagnostics for tokens that should _never_ appear in HCL source. This is intended to avoid the need for the parser to have special support for them all over. Returns a diagnostics with no errors if everything seems acceptab

(tokens Tokens)

Source from the content-addressed store, hash-verified

184// Otherwise, returns zero or more error diagnostics, though tries to limit
185// repetition of the same information.
186func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
187 var diags hcl.Diagnostics
188
189 toldBitwise := 0
190 toldExponent := 0
191 toldBacktick := 0
192 toldApostrophe := 0
193 toldSemicolon := 0
194 toldTabs := 0
195 toldBadUTF8 := 0
196
197 for _, tok := range tokens {
198 tokRange := func() *hcl.Range {
199 r := tok.Range
200 return &r
201 }
202
203 switch tok.Type {
204 case TokenBitwiseAnd, TokenBitwiseOr, TokenBitwiseXor, TokenBitwiseNot:
205 if toldBitwise < 4 {
206 var suggestion string
207 switch tok.Type {
208 case TokenBitwiseAnd:
209 suggestion = " Did you mean boolean AND (\"&&\")?"
210 case TokenBitwiseOr:
211 suggestion = " Did you mean boolean OR (\"||\")?"
212 case TokenBitwiseNot:
213 suggestion = " Did you mean boolean NOT (\"!\")?"
214 }
215
216 diags = append(diags, &hcl.Diagnostic{
217 Severity: hcl.DiagError,
218 Summary: "Unsupported operator",
219 Detail: fmt.Sprintf("Bitwise operators are not supported.%s", suggestion),
220 Subject: tokRange(),
221 })
222 toldBitwise++
223 }
224 case TokenStarStar:
225 if toldExponent < 1 {
226 diags = append(diags, &hcl.Diagnostic{
227 Severity: hcl.DiagError,
228 Summary: "Unsupported operator",
229 Detail: "\"**\" is not a supported operator. Exponentiation is not supported as an operator.",
230 Subject: tokRange(),
231 })
232
233 toldExponent++
234 }
235 case TokenBacktick:
236 // Only report for alternating (even) backticks, so we won't report both start and ends of the same
237 // backtick-quoted string.
238 if (toldBacktick % 2) == 0 {
239 diags = append(diags, &hcl.Diagnostic{
240 Severity: hcl.DiagError,
241 Summary: "Invalid character",
242 Detail: "The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<<EOT\".",
243 Subject: tokRange(),

Callers 3

LexConfigFunction · 0.85
LexExpressionFunction · 0.85
LexTemplateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected