From fd16a0c79e5d13e2a08a31493e4c100913c720c3 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Wed, 11 Mar 2026 22:32:06 -0700 Subject: [PATCH] Allow trailing comments in lists --- include/flexi_cfg/config/grammar.h | 2 +- tests/config_grammar_test.cpp | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/flexi_cfg/config/grammar.h b/include/flexi_cfg/config/grammar.h index 7230462..665c54a 100644 --- a/include/flexi_cfg/config/grammar.h +++ b/include/flexi_cfg/config/grammar.h @@ -101,7 +101,7 @@ struct STRING : peg::seq, peg::plus>, peg::one<' struct EXPRESSION : peg::seq {}; template -struct LIST_CONTENT_ : peg::list, peg::space> { using element = Element; }; +struct LIST_CONTENT_ : peg::list_tail, peg::space> { using element = Element; }; template // TODO: Figure out how to support 'peg::if_must<>' here instead of 'peg::seq<>' so that we can get diff --git a/tests/config_grammar_test.cpp b/tests/config_grammar_test.cpp index 4ecf79b..dc5b982 100644 --- a/tests/config_grammar_test.cpp +++ b/tests/config_grammar_test.cpp @@ -408,6 +408,17 @@ const std::vector list_test_cases = { "[$(ref.var2), $(ref.var1), 3.456]", // Verify that a mix of variables, expressions and numbers is supported R"([$(ref.var2), {{ 2^14 - 1}}, 0.32])", + + // Verify that a list with a trailing comma is supported + "[0x123, 0Xabc, 0xA1B2F9,]", + // Verify that a list of lists with a trailing comma is supported + "[[1, 2, 3], [4, 5, 6,], [7, 8, 9],]", + // Verify that a list with a trailing comma and a trailing comment is supported + R"([ + 0x123, # Another comment here with a comma, + 0Xabc, 0xA1B2F9, # a trailing comma and a trailingcomment + # comment + ])", }; TEST(ConfigGrammar, LIST) { @@ -437,13 +448,6 @@ TEST(ConfigGrammar, LIST) { EXPECT_THROW(ret.emplace(runTest>(content)), flexi_cfg::config::InvalidTypeException); } - { - // Fails due to the trailing comma - const std::string content = "[0x123, 0Xabc, 0xA1B2F9,]"; - std::optional ret; - EXPECT_THROW(ret.emplace(runTest>(content)), - tao::pegtl::parse_error); - } { // Fails due to containing a VAR const std::string content = "[0x123, $VAR, 0xA1B2F9,]";