Skip to content

Commit fcbab74

Browse files
authored
Merge pull request #378 from BurntSushi/multiline-table
Allow newlines and trailing commas in inline tables
2 parents 69d7903 + d54aa5d commit fcbab74

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"tbl-1": {
3+
"1": {
4+
"type": "integer",
5+
"value": "2"
6+
},
7+
"arr": [
8+
{
9+
"type": "integer",
10+
"value": "1"
11+
},
12+
{
13+
"type": "integer",
14+
"value": "2"
15+
},
16+
{
17+
"type": "integer",
18+
"value": "3"
19+
}
20+
],
21+
"hello": {
22+
"type": "string",
23+
"value": "world"
24+
},
25+
"tbl": {
26+
"k": {
27+
"type": "integer",
28+
"value": "1"
29+
}
30+
}
31+
},
32+
"tbl-2": {
33+
"k": {
34+
"type": "string",
35+
"value": "\tHello\n\t"
36+
}
37+
},
38+
"trailing-comma-1": {
39+
"c": {
40+
"type": "integer",
41+
"value": "1"
42+
}
43+
},
44+
"trailing-comma-2": {
45+
"c": {
46+
"type": "integer",
47+
"value": "1"
48+
}
49+
}
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TOML 1.1 supports newlines in inline tables and trailing commas.
2+
3+
trailing-comma-1 = {
4+
c = 1,
5+
}
6+
trailing-comma-2 = { c = 1, }
7+
8+
tbl-1 = {
9+
hello = "world",
10+
1 = 2,
11+
arr = [1,
12+
2,
13+
3,
14+
],
15+
tbl = {
16+
k = 1,
17+
}
18+
}
19+
20+
tbl-2 = {
21+
k = """
22+
Hello
23+
"""
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"a": {
3+
"extend": {
4+
"key": {
5+
"type": "integer",
6+
"value": "2"
7+
},
8+
"more": {
9+
"key": {
10+
"type": "integer",
11+
"value": "3"
12+
}
13+
}
14+
},
15+
"key": {
16+
"type": "integer",
17+
"value": "1"
18+
}
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[a]
2+
key = 1
3+
4+
# a.extend is a key inside the "a" table.
5+
[a.extend]
6+
key = 2
7+
8+
[a.extend.more]
9+
key = 3

internal/toml-test/version.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var versions = map[string]versionSpec{
1313
exclude: []string{
1414
"invalid/datetime/no-secs", // Times without seconds is no longer invalid.
1515
"invalid/string/basic-byte-escapes", // \x is now valid.
16+
"invalid/inline-table/trailing-comma",
17+
"invalid/inline-table/linebreak-1",
18+
"invalid/inline-table/linebreak-2",
19+
"invalid/inline-table/linebreak-3",
20+
"invalid/inline-table/linebreak-4",
1621
},
1722
},
1823

@@ -21,6 +26,7 @@ var versions = map[string]versionSpec{
2126
"valid/string/escape-esc", // \e
2227
"valid/string/hex-escape", "invalid/string/bad-hex-esc", // \x..
2328
"valid/datetime/no-seconds", // Times without seconds
29+
"valid/inline-table/newline",
2430
},
2531
},
2632

lex.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ func lexInlineTableValue(lx *lexer) stateFn {
618618
case isWhitespace(r):
619619
return lexSkip(lx, lexInlineTableValue)
620620
case isNL(r):
621+
if tomlNext {
622+
return lexSkip(lx, lexInlineTableValue)
623+
}
621624
return lx.errorPrevLine(errLexInlineTableNL{})
622625
case r == '#':
623626
lx.push(lexInlineTableValue)
@@ -640,6 +643,9 @@ func lexInlineTableValueEnd(lx *lexer) stateFn {
640643
case isWhitespace(r):
641644
return lexSkip(lx, lexInlineTableValueEnd)
642645
case isNL(r):
646+
if tomlNext {
647+
return lexSkip(lx, lexInlineTableValueEnd)
648+
}
643649
return lx.errorPrevLine(errLexInlineTableNL{})
644650
case r == '#':
645651
lx.push(lexInlineTableValueEnd)
@@ -648,6 +654,9 @@ func lexInlineTableValueEnd(lx *lexer) stateFn {
648654
lx.ignore()
649655
lx.skip(isWhitespace)
650656
if lx.peek() == '}' {
657+
if tomlNext {
658+
return lexInlineTableValueEnd
659+
}
651660
return lx.errorf("trailing comma not allowed in inline tables")
652661
}
653662
return lexInlineTableValue

toml_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ func TestTomlNextFails(t *testing.T) {
256256
runTomlTest(t, true,
257257
"valid/string/escape-esc",
258258
"valid/datetime/no-seconds",
259-
"valid/string/hex-escape")
259+
"valid/string/hex-escape",
260+
"valid/inline-table/newline")
260261
}
261262

262263
func runTomlTest(t *testing.T, includeNext bool, wantFail ...string) {

0 commit comments

Comments
 (0)