Commit 9a40b101 authored by Drew Fisher's avatar Drew Fisher

Allow trailing commas in parenthesized and bracketed lists

And add a test to verify this behavior.
parent 9473dff0
......@@ -146,6 +146,19 @@ TEST(Lexer, Tokens) {
"])",
doLex<LexedTokens>("[foo bar, baz qux, corge grault]").cStr());
// Trailing commas should not create an empty final list item, but be stripped by the lexer.
EXPECT_STREQ(
"(tokens = ["
"(bracketedList = ["
"["
"(identifier = 'foo', startByte = 1, endByte = 4)"
"], ["
"(identifier = 'bar', startByte = 6, endByte = 9)"
"]"
"], startByte = 0, endByte = 11)"
"])",
doLex<LexedTokens>("[foo, bar,]").cStr());
EXPECT_STREQ(
"(tokens = ["
"(bracketedList = ["
......
......@@ -158,9 +158,16 @@ Lexer::Lexer(Orphanage orphanageParam, ErrorReporter& errorReporter)
// Completely empty list.
return nullptr;
} else {
auto result = kj::heapArrayBuilder<kj::Array<Orphan<Token>>>(rest.size() + 1);
uint restSize = rest.size();
if (rest.size() > 0 && rest[restSize - 1] == nullptr) {
// Allow for trailing commas by shortening the list by one item if the final token is
// nullptr
restSize--;
}
auto result = kj::heapArrayBuilder<kj::Array<Orphan<Token>>>(1 + restSize); // first + rest
result.add(kj::mv(first));
for (auto& item: rest) {
for (uint i = 0; i < restSize ; i++) {
auto& item = rest[i];
result.add(kj::mv(item));
}
return result.finish();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment