zclsyntax: require block definitions to end with a newline

Previously we ostensibly allowed block {}block {}, but now we'll reject
that.
This commit is contained in:
Martin Atkins 2017-06-02 08:19:11 -07:00
parent 4d6dbdbb37
commit c15eb86349
2 changed files with 65 additions and 1 deletions

View File

@ -169,7 +169,7 @@ func (p *parser) finishParsingBodyAttribute(ident Token) (Node, zcl.Diagnostics)
if !p.recovery {
diags = append(diags, &zcl.Diagnostic{
Severity: zcl.DiagError,
Summary: "Missing newline in attribute definition",
Summary: "Missing newline after attribute definition",
Detail: "An attribute definition must end with a newline.",
Subject: &end.Range,
Context: zcl.RangeBetween(ident.Range, end.Range).Ptr(),
@ -281,6 +281,22 @@ Token:
diags = append(diags, bodyDiags...)
cBraceRange := p.PrevRange()
eol := p.Peek()
if eol.Type == TokenNewline || eol.Type == TokenEOF {
p.Read() // eat newline
} else {
if !p.recovery {
diags = append(diags, &zcl.Diagnostic{
Severity: zcl.DiagError,
Summary: "Missing newline after block definition",
Detail: "A block definition must end with a newline.",
Subject: &eol.Range,
Context: zcl.RangeBetween(ident.Range, eol.Range).Ptr(),
})
}
p.recoverAfterBodyItem()
}
return &Block{
Type: blockType,
Labels: labels,

View File

@ -80,6 +80,54 @@ func TestParseConfig(t *testing.T) {
},
},
},
{
`block {}block {}`,
1, // missing newline after block definition
&Body{
Attributes: Attributes{},
Blocks: Blocks{
&Block{
Type: "block",
Labels: nil,
Body: &Body{
Attributes: Attributes{},
Blocks: Blocks{},
SrcRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 7, Byte: 6},
End: zcl.Pos{Line: 1, Column: 9, Byte: 8},
},
EndRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 9, Byte: 8},
End: zcl.Pos{Line: 1, Column: 9, Byte: 8},
},
},
TypeRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 6, Byte: 5},
},
LabelRanges: nil,
OpenBraceRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 7, Byte: 6},
End: zcl.Pos{Line: 1, Column: 8, Byte: 7},
},
CloseBraceRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 8, Byte: 7},
End: zcl.Pos{Line: 1, Column: 9, Byte: 8},
},
},
},
SrcRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 17, Byte: 16},
},
EndRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 17, Byte: 16},
End: zcl.Pos{Line: 1, Column: 17, Byte: 16},
},
},
},
{
`block "foo" {}`,
0,