Skip to content

Add literal_value grammar rule for true, false, and null#294

Merged
kkozik-amplify merged 1 commit intomainfrom
issue/293
Apr 10, 2026
Merged

Add literal_value grammar rule for true, false, and null#294
kkozik-amplify merged 1 commit intomainfrom
issue/293

Conversation

@kkozik-amplify
Copy link
Copy Markdown
Collaborator

@kkozik-amplify kkozik-amplify commented Apr 10, 2026

Caused by #293

Summary

HCL2 keywords true, false, and null were parsed as generic identifiers, causing them to serialize as strings ("true", "false", "null") in JSON output instead of native JSON types (true, false, null). This adds a dedicated literal_value grammar rule so these keywords produce the correct Python/JSON types.

Changes

  • Add TRUE, FALSE, NULL lexer tokens and literal_value grammar rule to the Lark grammar
  • Add LiteralValueRule class that serializes to native True/False/None (falls back to string form inside interpolation contexts)
  • Update transformer to handle literal_value in expressions, attribute names, object keys, and get_attr access
  • Update deserializer to produce LiteralValueRule for bool and None values instead of IdentifierRule
  • Update reconstructor to insert spaces between adjacent literal_value and identifier/string tokens
  • Update all affected golden files (nulls.json, smoke.json, builder_basic_*.json, comments.json)

Manual testing checklist

  • echo 'x = true' | hcl2tojson → produces {"x": true} (not "true")
  • echo 'x = null' | hcl2tojson → produces {"x": null} (not "null")
  • echo '{"x": true}' | jsontohcl2 → produces x = true
  • echo '{"x": null}' | jsontohcl2 → produces x = null
  • echo 'x = "${true}"' | hcl2tojson → interpolated form stays as string
  • Round-trip: hcl2tojson file.tf | jsontohcl2 preserves true/false/null correctly
  • Attributes named true/false/null still parse (e.g. null = 1)

🤖 Co-Authored-By: Claude Opus 4.6 (1M context) Claude Code

Introduces a dedicated `literal_value` grammar rule and `LiteralValueRule` class
so that `true`, `false`, and `null` serialize to native Python/JSON types
(True, False, None) instead of strings. Updates grammar, transformer,
deserializer, reconstructor, and all affected golden files.

Also fixes InterpolationRule to set inside_dollar_string context so that
literal values inside interpolations serialize as strings (${null} not ${None}).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kkozik-amplify kkozik-amplify merged commit 919e4b2 into main Apr 10, 2026
8 of 9 checks passed
@kkozik-amplify kkozik-amplify deleted the issue/293 branch April 10, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants