Skip to content

Commit

Permalink
Merge pull request hashicorp#467 from hashicorp/alisdair/fix-parse-qu…
Browse files Browse the repository at this point in the history
…oted-string-literal-end-range

Fix parseQuotedStringLiteral zero end range
  • Loading branch information
alisdair authored May 5, 2021
2 parents f601991 + ad6a17c commit cbf95f6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
8 changes: 5 additions & 3 deletions hclsyntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1661,15 +1661,15 @@ func (p *parser) parseQuotedStringLiteral() (string, hcl.Range, hcl.Diagnostics)

var diags hcl.Diagnostics
ret := &bytes.Buffer{}
var cQuote Token
var endRange hcl.Range

Token:
for {
tok := p.Read()
switch tok.Type {

case TokenCQuote:
cQuote = tok
endRange = tok.Range
break Token

case TokenQuotedLit:
Expand Down Expand Up @@ -1712,6 +1712,7 @@ Token:
Subject: &tok.Range,
Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(),
})
endRange = tok.Range
break Token

default:
Expand All @@ -1724,13 +1725,14 @@ Token:
Context: hcl.RangeBetween(oQuote.Range, tok.Range).Ptr(),
})
p.recover(TokenCQuote)
endRange = tok.Range
break Token

}

}

return ret.String(), hcl.RangeBetween(oQuote.Range, cQuote.Range), diags
return ret.String(), hcl.RangeBetween(oQuote.Range, endRange), diags
}

// ParseStringLiteralToken processes the given token, which must be either a
Expand Down
58 changes: 58 additions & 0 deletions hclsyntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2484,6 +2484,64 @@ block "valid" {}
},
},
},
{
`block "unterminated_string "name" {}`,
2, // "Invalid string literal" and "Invalid block definition"
&Body{
Attributes: Attributes{},
Blocks: Blocks{
&Block{
Type: "block",
Labels: []string{"unterminated_string ", "name", " {}"},
Body: &Body{
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
},

TypeRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
LabelRanges: []hcl.Range{
{
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
End: hcl.Pos{Line: 1, Column: 29, Byte: 28},
},
{
Start: hcl.Pos{Line: 1, Column: 29, Byte: 28},
End: hcl.Pos{Line: 1, Column: 33, Byte: 32},
},
{
Start: hcl.Pos{Line: 1, Column: 33, Byte: 32},
End: hcl.Pos{Line: 1, Column: 37, Byte: 36},
},
},
OpenBraceRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
CloseBraceRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
},
},
SrcRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 37, Byte: 36},
},
EndRange: hcl.Range{
Start: hcl.Pos{Line: 1, Column: 37, Byte: 36},
End: hcl.Pos{Line: 1, Column: 37, Byte: 36},
},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit cbf95f6

Please sign in to comment.