allow omitting the colon if the value is an object

This commit is contained in:
Havoc Pennington 2011-11-12 14:36:45 -05:00
parent 55595e4c39
commit 39de5aa444
4 changed files with 64 additions and 8 deletions

View File

@ -398,14 +398,24 @@ final class Parser {
} else {
Path path = parseKey(t);
Token afterKey = nextTokenIgnoringNewline();
if (!isKeyValueSeparatorToken(afterKey)) {
throw parseError("Key may not be followed by token: "
+ afterKey);
}
consolidateValueTokens();
Token valueToken = nextTokenIgnoringNewline();
AbstractConfigValue newValue = parseValue(valueToken);
Token valueToken;
AbstractConfigValue newValue;
if (flavor == SyntaxFlavor.CONF
&& afterKey == Tokens.OPEN_CURLY) {
// can omit the ':' or '=' before an object value
valueToken = afterKey;
newValue = parseObject();
} else {
if (!isKeyValueSeparatorToken(afterKey)) {
throw parseError("Key may not be followed by token: "
+ afterKey);
}
consolidateValueTokens();
valueToken = nextTokenIgnoringNewline();
newValue = parseValue(valueToken);
}
String key = path.first();
Path remaining = path.remainder();

View File

@ -0,0 +1,44 @@
{
"ints" {
"fortyTwo" : 42,
"fortyTwoAgain" : 42
},
"floats"
{
"fortyTwoPointOne" : 42.1,
"fortyTwoPointOneAgain" : 42.1
},
"strings"
{
"abcd" : "abcd",
"abcdAgain" : "abcd",
"a" : "a",
"b" : "b",
"c" : "c",
"d" : "d",
"concatenated" : "null bar 42 baz true 3.14 hi"
},
"arrays" {
"empty" : [],
"1" : [ 1 ],
"12" : [1, 2],
"123" : [1, 2, 3],
"ofString" : [ "a", "b", "c" ]
},
"booleans" {
"true" : true,
"trueAgain" : true,
"false" : false,
"falseAgain" : false
},
"nulls" {
"null" : null,
"nullAgain" : null
}
}

View File

@ -87,6 +87,6 @@ class EquivalentsTest extends TestUtils {
// it breaks every time you add a file, so you have to update it.
assertEquals(2, dirCount)
// this is the number of files not named original.*
assertEquals(8, fileCount)
assertEquals(9, fileCount)
}
}

View File

@ -172,6 +172,8 @@ abstract trait TestUtils {
private val validConfInvalidJson = List[ParseTest](
"""{ "foo" = 42 }""", // equals rather than colon
"""{ foo { "bar" : 42 } }""", // omit the colon for object value
"""{ foo baz { "bar" : 42 } }""", // omit the colon with unquoted key with spaces
"""{ "foo" : bar }""", // no quotes on value
"""{ "foo" : null bar 42 baz true 3.14 "hi" }""", // bunch of values to concat into a string
"{ foo : \"bar\" }", // no quotes on key