Repository navigation
fix: accept HCL keywords as block labels and object keys (#148) - #357
Conversation
HCL does not reserve its keywords, so `in`, `for`, `true`, etc. are legal
identifiers. Two positions rejected them.
Block labels. `block : identifier ...` where `identifier : NAME`, but `in`
lexes as the dedicated `IN` terminal and never as `NAME`. In a body position
the contextual lexer therefore only accepted `IN` as the start of an
*attribute*, so the Snowflake provider's nested `in` block failed with
"Expected one of: * EQ" right after `Token('IN', 'in')`:
data "snowflake_schemas" "in" {
in {
database = "database"
}
}
Object keys. `object_elem_key : expression` cannot reach `keyword` either,
which regressed #148 (fixed once by #164, then lost when 8.x moved keywords
out of `identifier` into their own rule and wired it only into
`_attribute_name`). #164's regression test covered a block-body attribute,
a different grammar path from the object element the issue actually reports.
The object path then worked only by accident: Lark's contextual lexer falls
back to `NAME` only in states that do not accept the keyword terminal, so a
key's *position* in the object decided whether the file parsed.
{ in = "h", name = "n" } # parsed
{ name = "n", in = "h" } # failed <- the shape in #148
{ for = 1 } # failed in every position
Both now route through new `_block_label` / `keyword` alternatives, and the
transformer normalizes the resulting KeywordRule and LiteralValueRule nodes
to IdentifierRule, mirroring what `attribute()` already did. That
normalization is load-bearing beyond tidiness: `_label_to_str` in
hcl2/query/blocks.py falls through to `str(label.serialize())`, and
LiteralValueRule serializes `true` to Python `True`, so a `true`-named block
would otherwise query as "True".
Fixes #148
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ample Two review findings from comparing this PR against #355. _block_label duplicated _attribute_name's three alternatives exactly, so it was a second rule to keep in sync for no gain. Reuse _attribute_name in the block type and label positions instead; behaviour is unchanged across the block, object-key, and for-expression/template sweeps. The object-key narrative named the wrong example. Both spellings it contrasted parse on clean main -- the discriminator is the separator, not the position: the comma-separated form always worked, the newline-separated form with a non-first keyword key is what failed. Correct that in the CHANGELOG and the object_elem_key comment, and table-drive test_in_key_in_any_position over both separators so a regression cannot hide behind whichever spelling happens to work. Also add the missing PR links to both CHANGELOG bullets and credit #355 for the independent block-side diagnosis. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Reviewed this alongside #355 and pushed 62772ab with two fixes. 1534 tests pass, pre-commit clean. The object-key narrative names the wrong example. The description, commit message, and CHANGELOG all claim:
Both of those parse on clean a = { name = "n", in = "h" } # parses on main
a = {
name = "n"
in = "h" # fails on main <-- the real bug
}Which is why the #148 I also table-drove
Added the missing PR links to both CHANGELOG bullets, and credited #355 for landing the block-side diagnosis independently. On #355: this PR is a strict superset — same block-side behavior, plus the object-key half tied to #148, plus the five keywords ( |
Move the two keyword-parsing fixes from #357 out of [Unreleased] into a dated 8.1.4 section and reset the placeholder. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Move the two keyword-parsing fixes from #357 out of [Unreleased] into a dated 8.1.4 section and reset the placeholder. Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Problem
HCL does not reserve its keywords, so
in,for,true, etc. are legal identifiers. Two positions in the grammar rejected them.1. Block labels
block : identifier (identifier | string)* ...whereidentifier : NAME, butinlexes as the dedicatedINterminal and never asNAME. In a body position the contextual lexer therefore only acceptedINas the start of an attribute, so this documented Snowflake provider data source failed to parse:2. Object keys — a regression of #148
object_elem_key : expressioncannot reachkeywordeither. #148 was fixed once by #164, then lost when 8.x moved keywords out ofidentifierinto their ownkeywordrule and wired it only into_attribute_name.#164's regression fixture covered a block-body attribute, which is a different grammar path (
_attribute_name) from the object element (object_elem_key) that the issue actually reports. So the fixture kept passing while the reported case broke.The object path then worked only by accident: Lark's contextual lexer falls back to
NAMEonly in states that do not accept the keyword terminal, which made a key's separator and position decide whether the file parsed.{ in = "h", name = "n" } # parsed { name = "n", in = "h" } # parsed -- the comma form always worked { # failed <- the shape in #148 name = "n" in = "h" } { for = 1 } # failed in every positionThe newline-separated form is why the
jsonencodeOpenAPI body from #148 still raised onmain: the report writes its object elements one per line, withinfollowingtypeandname. Verified against a clean checkout, independent of this branch.Fix
hcl2/hcl2.larkblocktypes and labels reuse the existing_attribute_name, so there is no second rule to keep in synchcl2/hcl2.larkobject_elem_key : expression | keywordhcl2/transformer.pyblock()andobject_elem_key()normalizeKeywordRule/LiteralValueRuletoIdentifierRuleLALR builds with no new conflicts: after reducing
keyword,EQstill means attribute andNAME/"/{means block.The normalization is load-bearing beyond tidiness.
_label_to_str(hcl2/query/blocks.py:14) falls through tostr(label.serialize()), andLiteralValueRuleserializestrueto PythonTrue— so atrue-named block would otherwise query as"True".Testing
unittest: 1534 passing (from 1518), on a freshly built grammar cache. ruff check, ruff format, and mypy pass.Unit —
test/unit/test_api.py:TestKeywordBlocks(8) — the Snowflake snippet, all ten keywords as block types, quoted and unquoted keyword labels, both reverse pipelines, and theIdentifierRulenormalization viaquery().TestKeywordAttributeNames(9) — Can not parse file that has "in" attribute #148's snippet with space and tab indentation,inin both object positions, all ten keywords as object keys, colon separator, fix: Add support for attributes named "in". #164's fixture, both reverse pipelines.Both classes carry a guard that
instill works as the for-expression keyword. Separately swept 14 for-expression and template-directive forms by hand — no regressions.Integration — two new round-trip suites,
resource_keyword_blockandobject_keyword_keys, each with all four golden files. Direct reconstruction (HCL → IR → Lark → HCL) is byte-identical to source for both.Notes for the reviewer
hq 'data.snowflake_schemas.in.in.database'resolves correctly against the new fixture.[for i in x : i]→[ for i in x : i]). That reproduces on cleanmainand is unrelated to keywords, so I left it alone rather than bake it into a golden file. Happy to fix it separately if you'd like.🤖 Generated with Claude Code
Supersedes #356, which GitHub auto-closed when the head branch was renamed to fix a typo (
bugifx→bugfix). Same commit, no content changes.Corrected in 62772ab after review. The object-key example pair above originally read
{ in = "header", name = "n" }parsed /{ name = "n", in = "header" }failed; both parse on cleanmain, so the discriminator is the separator rather than the position. The fix and the conclusion were unaffected, but the wrong pair had also reached the CHANGELOG. The same commit drops_block_labelin favour of reusing_attribute_name(the approach in #355) and widenstest_in_key_in_any_positionto cover both separators.