summaryrefslogtreecommitdiff
path: root/hclwrite
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2020-12-02 11:39:03 -0800
committerMartin Atkins <mart@degeneration.co.uk>2020-12-02 12:03:00 -0800
commitf1f39852306ab7dc0a64c4f7d300999c5832ca17 (patch)
tree48f178b1e1c2c1520040b614e66d25ec73b7f71c /hclwrite
parentd510cb032660aff9bb82505b7766eb54587cd2d0 (diff)
hclsyntax: Explicit AST node for parentheses
So far the expression parentheses syntax has been handled entirely in the parser and has been totally invisible in the AST. That's fine for typical expression evaluation, but over the years it's led to a few quirky behaviors in less common situations where we've assumed that all expressions are covered by the AST itself or by the source ranges that the AST captures. In particular, hclwrite assumes that all expressions will have source ranges that cover their tokens, and it generates an incorrect physical syntax tree when the AST doesn't uphold that. After resisting through a few other similar bugs, this commit finally introduces an explicit AST node for parentheses, which makes the parentheses explicit in the AST and captures the larger source range that includes the TokenOParen and the TokenCParen. This means that parentheses will now be visible as a distinct node when walking the AST, as reflected in the updated tests here. That may cause downstream applications that traverse the tree to exhibit different behaviors but we're not considering that as a "breaking change" because the Walk function doesn't make any guarantees about the specific AST shape.
Diffstat (limited to 'hclwrite')
-rw-r--r--hclwrite/parser_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/hclwrite/parser_test.go b/hclwrite/parser_test.go
index ed17303..6edebc7 100644
--- a/hclwrite/parser_test.go
+++ b/hclwrite/parser_test.go
@@ -190,6 +190,70 @@ func TestParse(t *testing.T) {
},
},
{
+ "a = (\n 1 + 2\n)\nb = 3\n",
+ TestTreeNode{
+ Type: "Body",
+ Children: []TestTreeNode{
+ {
+ Type: "Attribute",
+ Children: []TestTreeNode{
+ {Type: "comments"},
+ {
+ Type: "identifier",
+ Val: "a",
+ },
+ {
+ Type: "Tokens",
+ Val: " =",
+ },
+ {
+ Type: "Expression",
+ Children: []TestTreeNode{
+ {
+ Type: "Tokens",
+ Val: " (\n 1 + 2\n)",
+ },
+ },
+ },
+ {Type: "comments"},
+ {
+ Type: "Tokens",
+ Val: "\n",
+ },
+ },
+ },
+ {
+ Type: "Attribute",
+ Children: []TestTreeNode{
+ {Type: "comments"},
+ {
+ Type: "identifier",
+ Val: "b",
+ },
+ {
+ Type: "Tokens",
+ Val: " =",
+ },
+ {
+ Type: "Expression",
+ Children: []TestTreeNode{
+ {
+ Type: "Tokens",
+ Val: " 3",
+ },
+ },
+ },
+ {Type: "comments"},
+ {
+ Type: "Tokens",
+ Val: "\n",
+ },
+ },
+ },
+ },
+ },
+ },
+ {
"b {}\n",
TestTreeNode{
Type: "Body",