fix: accept HCL keywords as block labels and object keys (#148) - #356
Closed
jmkacz wants to merge 1 commit into
Closed
fix: accept HCL keywords as block labels and object keys (#148)#356jmkacz wants to merge 1 commit into
jmkacz wants to merge 1 commit into
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) <noreply@anthropic.com>
Author
|
Closed automatically by a branch rename ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 position in the object decide whether the file parsed.{ in = "h", name = "n" } # parsed { name = "n", in = "h" } # failed <- the shape in #148 { for = 1 } # failed in every positionThat second line is why the
jsonencodeOpenAPI body from #148 still raised onmain— verified against a clean checkout, independent of this branch.Fix
hcl2/hcl2.larkblocklabels route through a new_block_label : identifier | keyword | literal_value, mirroring_attribute_namehcl2/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