Skip to content

Commit

Permalink
Fix whitespace/comment parsing for arrays
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
fjl committed Apr 1, 2017
1 parent 2e01764 commit 605f287
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 418 deletions.
27 changes: 12 additions & 15 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,18 @@ func TestUnmarshal_WithArray(t *testing.T) {
err: lineErrorField(1, "toml.arrays.Ints", errArrayMultiType),
expect: &arrays{},
},
{
data: `key = [
1, 2, 3
]`,
expect: &struct{ Key []int }{
[]int{1, 2, 3},
}},
{
data: `key = [
1,
2, # this is ok
]`,
expect: &struct{ Key []int }{
[]int{1, 2},
}},
// whitespace + comments
{string(loadTestData("unmarshal-array-1.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-2.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-3.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-4.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-5.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
{string(loadTestData("unmarshal-array-6.toml")), nil, &arrays{Ints: []int{1, 2, 3}}},
// parse errors
{`ints = [ , ]`, lineError(1, errParse), &arrays{}},
{`ints = [ , 1 ]`, lineError(1, errParse), &arrays{}},
{`ints = [ 1 2 ]`, lineError(1, errParse), &arrays{}},
{`ints = [ 1 , , 2 ]`, lineError(1, errParse), &arrays{}},
})
}

Expand Down
15 changes: 11 additions & 4 deletions parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,20 @@ digitQuad <- digitDual digitDual

array <- (
'[' { p.StartArray() }
wsnl arrayValues wsnl
wsnl arrayValues? wsnl
']'
)

arrayValues <- (
val { p.AddArrayVal() }
arraySep? (comment? newline)?
)*
(
wsnl comment?
wsnl arraySep
wsnl comment?
wsnl val { p.AddArrayVal() }
)*
wsnl arraySep?
wsnl comment?
)

arraySep <- ws ',' wsnl
arraySep <- ','
Loading

0 comments on commit 605f287

Please sign in to comment.