Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
hclsyntax: emit Newline after a CHeredoc
Browse files Browse the repository at this point in the history
Previously, due to how heredoc scanning was implemented, the closing
marker for a heredoc would consume the newline that terminated it. This
was problematic in any context that is newline-sensitive, because it
would cause us to skip the TokenNewline that might terminate e.g. an
attribute definition:

    foo = <<EOT
    hello
    EOT
    bar = "hello"

Previously the "foo" attribute would fail to parse properly due to trying
to consume the "bar" definition as part of its expression.

Now we synthetically split the marker token into two parts: the marker
itself and the newline that follows it. This means that using a heredoc
in any context where newlines are sensitive will involuntarily introduce
a newline, but that seems consistent with user expectation based on how
heredocs seem to be used "in the wild".
  • Loading branch information
apparentlymart committed Mar 8, 2018
1 parent be66a72 commit 7d6ed4d
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 86 deletions.
83 changes: 83 additions & 0 deletions hcl/hclsyntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,89 @@ block "valid" {}
},
},
},
{
"a = <<EOT\nHello\nEOT\nb = \"Hi\"",
0,
&Body{
Attributes: Attributes{
"a": {
Name: "a",
Expr: &TemplateExpr{
Parts: []Expression{
&LiteralValueExpr{
Val: cty.StringVal("Hello\n"),

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 6, Byte: 5},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
},
},

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 5, Byte: 4},
End: hcl.Pos{Line: 3, Column: 4, Byte: 19},
},
},

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 3, Column: 4, Byte: 19},
},
NameRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 2, Byte: 1},
},
EqualsRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 3, Byte: 2},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
},
"b": {
Name: "b",
Expr: &TemplateExpr{
Parts: []Expression{
&LiteralValueExpr{
Val: cty.StringVal("Hi"),

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 24, Byte: 5},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
},
},
},

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 5, Byte: 24},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
},
},

SrcRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 1, Byte: 20},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
},
NameRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 1, Byte: 20},
End: hcl.Pos{Line: 4, Column: 2, Byte: 21},
},
EqualsRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 3, Byte: 22},
End: hcl.Pos{Line: 4, Column: 4, Byte: 23},
},
},
},
Blocks: Blocks{},
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 4, Column: 9, Byte: 28},
End: hcl.Pos{Line: 4, Column: 9, Byte: 28},
},
},
},
{
"a = foo.bar\n",
0,
Expand Down
Loading

0 comments on commit 7d6ed4d

Please sign in to comment.