HCL: allow if and in as attribute and object key names#7542
HCL: allow if and in as attribute and object key names#7542scuba10steve wants to merge 1 commit intomainfrom
if and in as attribute and object key names#7542Conversation
The HCL lexer reserves `IF` and `IN` as dedicated tokens for use in for-expressions, but the spec doesn't reserve these as keywords at the body level. Terraform itself accepts `if` and `in` as attribute names and object keys, so the parser should too. Extend the `attribute` and `qualifiedIdentifier` rules to accept `IF` and `IN` alongside `Identifier`. The visitor walks `qualifiedIdentifier` children in source order to keep dotted-name positions correct when the alternatives are interleaved. Fixes #6446
|
Thanks! We'd had a parallel effort open as well with a slightly different approach: For both I'm wondering whether we should have these keywords called out in the grammar and generated parser, or whether we can better scope when to interpret and |
ha! If you already had something going let's just take yours. I haven't really tested this at all so it was just something I threw together. |
Summary
The HCL lexer reserves
IFandINas dedicated tokens for use in for-expressions, but the HCL spec does not reserve these as keywords at the body level. Terraform itself acceptsifandinas attribute names and object keys.Extend
attributeandqualifiedIdentifierrules inHCLParser.g4to acceptIFandINalongsideIdentifier. The visitor walksqualifiedIdentifierchildren in source order so dotted-name positions stay correct when alternatives are interleaved.For-expression parsing (
for x in xs : x if cond) is unaffected — those tokens are still consumed at the for-rule level. Verified by the existingHclForTestsuite.Fixes HclParser might fail if you use
ifas property name #6446Test plan
rewrite-hcl:test— 9 new + existing tests pass:usingIfAsAttributeName/usingInAsAttributeName— issue's reproducerkeywordsAsTopLevelAttributes— body-level form, no enclosing blockkeywordAttributeAlongsideForExpression— coexistence withfor ... in ... if ...keywordAttributeWithCommentAndUnusualSpacing— formatting variationskeywordsAsObjectKeys/keywordObjectKeyMixedWithRegularKeys— object literal keysrewrite-hcl:check— license, lint, all green./gradlew build—BUILD SUCCESSFUL in 10m 13s, 442 tasks, no regressions in any downstream consumerHclForTest— for-expression regression coverage still passes