From 72f5afb6afc31a1a023941aec365c91600b869b1 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 25 Jul 2020 21:06:26 +0900 Subject: [PATCH] refactor: remove template from detail::location --- tests/test_lex_aux.hpp | 4 +- tests/test_parse_aux.hpp | 4 +- tests/test_parse_floating.cpp | 12 +-- tests/test_parse_table.cpp | 4 +- toml/combinator.hpp | 96 ++++++----------- toml/literal.hpp | 2 +- toml/parser.hpp | 196 +++++++++++++++------------------- toml/region.hpp | 27 ++--- toml/serializer.hpp | 2 +- 9 files changed, 143 insertions(+), 204 deletions(-) diff --git a/tests/test_lex_aux.hpp b/tests/test_lex_aux.hpp index c0d2b16..77f2a9b 100644 --- a/tests/test_lex_aux.hpp +++ b/tests/test_lex_aux.hpp @@ -8,7 +8,7 @@ do { \ const std::string token (tkn); \ const std::string expected(expct); \ - toml::detail::location loc("test", token); \ + toml::detail::location loc("test", token); \ const auto result = lxr::invoke(loc); \ BOOST_TEST(result.is_ok()); \ if(result.is_ok()){ \ @@ -28,7 +28,7 @@ do { \ #define TOML11_TEST_LEX_REJECT(lxr, tkn) \ do { \ const std::string token (tkn); \ - toml::detail::location loc("test", token); \ + toml::detail::location loc("test", token); \ const auto result = lxr::invoke(loc); \ BOOST_TEST(result.is_err()); \ const bool loc_same = (loc.begin() == loc.iter()); \ diff --git a/tests/test_parse_aux.hpp b/tests/test_parse_aux.hpp index 6702181..b9fd07c 100644 --- a/tests/test_parse_aux.hpp +++ b/tests/test_parse_aux.hpp @@ -7,7 +7,7 @@ #define TOML11_TEST_PARSE_EQUAL(psr, tkn, expct) \ do { \ const std::string token(tkn); \ - toml::detail::location loc("test", token); \ + toml::detail::location loc("test", token); \ const auto result = psr(loc); \ BOOST_TEST(result.is_ok()); \ if(result.is_ok()){ \ @@ -23,7 +23,7 @@ do { \ #define TOML11_TEST_PARSE_EQUAL_VALUE(psr, tkn, expct) \ do { \ const std::string token(tkn); \ - toml::detail::location loc("test", token); \ + toml::detail::location loc("test", token); \ const auto result = psr(loc); \ BOOST_TEST(result.is_ok()); \ if(result.is_ok()){ \ diff --git a/tests/test_parse_floating.cpp b/tests/test_parse_floating.cpp index 6b0c680..8ddcb90 100644 --- a/tests/test_parse_floating.cpp +++ b/tests/test_parse_floating.cpp @@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(test_inf) { { const std::string token("inf"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isinf(r.unwrap().first)); @@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(test_inf) } { const std::string token("+inf"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isinf(r.unwrap().first)); @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(test_inf) } { const std::string token("-inf"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isinf(r.unwrap().first)); @@ -156,21 +156,21 @@ BOOST_AUTO_TEST_CASE(test_nan) { { const std::string token("nan"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isnan(r.unwrap().first)); } { const std::string token("+nan"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isnan(r.unwrap().first)); } { const std::string token("-nan"); - toml::detail::location loc("test", token); + toml::detail::location loc("test", token); const auto r = parse_floating(loc); BOOST_CHECK(r.is_ok()); BOOST_CHECK(std::isnan(r.unwrap().first)); diff --git a/tests/test_parse_table.cpp b/tests/test_parse_table.cpp index e30e257..cb5baee 100644 --- a/tests/test_parse_table.cpp +++ b/tests/test_parse_table.cpp @@ -19,7 +19,7 @@ BOOST_AUTO_TEST_CASE(test_normal_table) "key2 = 42\n" "key3 = 3.14\n" ); - location loc("test", table); + location loc("test", table); const auto result = toml::detail::parse_ml_table(loc); BOOST_TEST(result.is_ok()); @@ -36,7 +36,7 @@ BOOST_AUTO_TEST_CASE(test_nested_table) "a.b = \"value\"\n" "a.c.d = 42\n" ); - location loc("test", table); + location loc("test", table); const auto result = toml::detail::parse_ml_table(loc); BOOST_TEST(result.is_ok()); diff --git a/toml/combinator.hpp b/toml/combinator.hpp index 3d72718..4df55aa 100644 --- a/toml/combinator.hpp +++ b/toml/combinator.hpp @@ -58,13 +58,9 @@ struct character { static constexpr char target = C; - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - if(loc.iter() == loc.end()) {return none();} const auto first = loc.iter(); @@ -75,7 +71,7 @@ struct character } loc.advance(); // update location - return ok(region(loc, first, loc.iter())); + return ok(region>(loc, first, loc.iter())); } }; template @@ -91,13 +87,9 @@ struct in_range static constexpr char upper = Up; static constexpr char lower = Low; - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - if(loc.iter() == loc.end()) {return none();} const auto first = loc.iter(); @@ -108,7 +100,7 @@ struct in_range } loc.advance(); - return ok(region(loc, first, loc.iter())); + return ok(region>(loc, first, loc.iter())); } }; template constexpr char in_range::upper; @@ -119,13 +111,9 @@ template constexpr char in_range::lower; template struct exclude { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - if(loc.iter() == loc.end()) {return none();} auto first = loc.iter(); @@ -136,7 +124,7 @@ struct exclude return none(); } loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but... - return ok(region(loc, first, loc.iter())); + return ok(region>(loc, first, loc.iter())); } }; @@ -144,19 +132,15 @@ struct exclude template struct maybe { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - const auto rslt = Combinator::invoke(loc); if(rslt.is_ok()) { return rslt; } - return ok(region(loc)); + return ok(region>(loc)); } }; @@ -166,13 +150,9 @@ struct sequence; template struct sequence { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - const auto first = loc.iter(); const auto rslt = Head::invoke(loc); if(rslt.is_err()) @@ -184,9 +164,9 @@ struct sequence } // called from the above function only, recursively. - template - static result, none_t> - invoke(location& loc, region reg, Iterator first) + template + static result>, none_t> + invoke(location& loc, region> reg, Iterator first) { const auto rslt = Head::invoke(loc); if(rslt.is_err()) @@ -203,9 +183,9 @@ template struct sequence { // would be called from sequence::invoke only. - template - static result, none_t> - invoke(location& loc, region reg, Iterator first) + template + static result>, none_t> + invoke(location& loc, region> reg, Iterator first) { const auto rslt = Head::invoke(loc); if(rslt.is_err()) @@ -224,13 +204,9 @@ struct either; template struct either { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); - const auto rslt = Head::invoke(loc); if(rslt.is_ok()) {return rslt;} return either::invoke(loc); @@ -239,12 +215,9 @@ struct either template struct either { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - static_assert(std::is_same::value, - "internal error: container::value_type should be `char`."); return Head::invoke(loc); } }; @@ -259,11 +232,10 @@ struct unlimited{}; template struct repeat> { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - region retval(loc); + region> retval(loc); const auto first = loc.iter(); for(std::size_t i=0; i> template struct repeat> { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - region retval(loc); + region> retval(loc); const auto first = loc.iter(); for(std::size_t i=0; i> template struct repeat { - template - static result, none_t> - invoke(location& loc) + static result>, none_t> + invoke(location& loc) { - region retval(loc); + region> retval(loc); while(true) { auto rslt = T::invoke(loc); diff --git a/toml/literal.hpp b/toml/literal.hpp index 14b6883..65a5511 100644 --- a/toml/literal.hpp +++ b/toml/literal.hpp @@ -13,7 +13,7 @@ inline namespace toml_literals inline ::toml::value operator"" _toml(const char* str, std::size_t len) { - ::toml::detail::location> + ::toml::detail::location loc(/* filename = */ std::string("TOML literal encoded in a C++ code"), /* contents = */ std::vector(str, str + len)); diff --git a/toml/parser.hpp b/toml/parser.hpp index fbbf38c..6ab5252 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -25,9 +25,8 @@ namespace toml namespace detail { -template -result>, std::string> -parse_boolean(location& loc) +inline result>>, std::string> +parse_boolean(location& loc) { const auto first = loc.iter(); if(const auto token = lex_boolean::invoke(loc)) @@ -48,9 +47,8 @@ parse_boolean(location& loc) {{std::addressof(loc), "the next token is not a boolean"}})); } -template -result>, std::string> -parse_binary_integer(location& loc) +inline result>>, std::string> +parse_binary_integer(location& loc) { const auto first = loc.iter(); if(const auto token = lex_bin_int::invoke(loc)) @@ -78,9 +76,8 @@ parse_binary_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -template -result>, std::string> -parse_octal_integer(location& loc) +inline result>>, std::string> +parse_octal_integer(location& loc) { const auto first = loc.iter(); if(const auto token = lex_oct_int::invoke(loc)) @@ -99,9 +96,8 @@ parse_octal_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -template -result>, std::string> -parse_hexadecimal_integer(location& loc) +inline result>>, std::string> +parse_hexadecimal_integer(location& loc) { const auto first = loc.iter(); if(const auto token = lex_hex_int::invoke(loc)) @@ -120,9 +116,8 @@ parse_hexadecimal_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -template -result>, std::string> -parse_integer(location& loc) +inline result>>, std::string> +parse_integer(location& loc) { const auto first = loc.iter(); if(first != loc.end() && *first == '0') @@ -130,7 +125,7 @@ parse_integer(location& loc) const auto second = std::next(first); if(second == loc.end()) // the token is just zero. { - return ok(std::make_pair(0, region(loc, first, second))); + return ok(std::make_pair(0, region>(loc, first, second))); } if(*second == 'b') {return parse_binary_integer (loc);} // 0b1100 @@ -166,9 +161,8 @@ parse_integer(location& loc) {{std::addressof(loc), "the next token is not an integer"}})); } -template -result>, std::string> -parse_floating(location& loc) +inline result>>, std::string> +parse_floating(location& loc) { const auto first = loc.iter(); if(const auto token = lex_float::invoke(loc)) @@ -255,9 +249,9 @@ parse_floating(location& loc) {{std::addressof(loc), "the next token is not a float"}})); } -template +template std::string read_utf8_codepoint(const region& reg, - /* for err msg */ const location& loc) + /* for err msg */ const location& loc) { const auto str = reg.str().substr(1); std::uint_least32_t codepoint; @@ -314,8 +308,7 @@ std::string read_utf8_codepoint(const region& reg, return character; } -template -result parse_escape_sequence(location& loc) +inline result parse_escape_sequence(location& loc) { const auto first = loc.iter(); if(first == loc.end() || *first != '\\') @@ -370,9 +363,8 @@ result parse_escape_sequence(location& loc) return err(msg); } -template -result>, std::string> -parse_ml_basic_string(location& loc) +inline result>>, std::string> +parse_ml_basic_string(location& loc) { const auto first = loc.iter(); if(const auto token = lex_ml_basic_string::invoke(loc)) @@ -450,9 +442,8 @@ parse_ml_basic_string(location& loc) } } -template -result>, std::string> -parse_basic_string(location& loc) +inline result>>, std::string> +parse_basic_string(location& loc) { const auto first = loc.iter(); if(const auto token = lex_basic_string::invoke(loc)) @@ -503,14 +494,13 @@ parse_basic_string(location& loc) } } -template -result>, std::string> -parse_ml_literal_string(location& loc) +inline result>>, std::string> +parse_ml_literal_string(location& loc) { const auto first = loc.iter(); if(const auto token = lex_ml_literal_string::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto open = lex_ml_literal_string_open::invoke(inner_loc); if(!open) @@ -566,14 +556,13 @@ parse_ml_literal_string(location& loc) } } -template -result>, std::string> -parse_literal_string(location& loc) +inline result>>, std::string> +parse_literal_string(location& loc) { const auto first = loc.iter(); if(const auto token = lex_literal_string::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto open = lex_apostrophe::invoke(inner_loc); if(!open) @@ -607,9 +596,8 @@ parse_literal_string(location& loc) } } -template -result>, std::string> -parse_string(location& loc) +inline result>>, std::string> +parse_string(location& loc) { if(loc.iter() != loc.end() && *(loc.iter()) == '"') { @@ -639,14 +627,13 @@ parse_string(location& loc) {{std::addressof(loc), "the next token is not a string"}})); } -template -result>, std::string> -parse_local_date(location& loc) +inline result>>, std::string> +parse_local_date(location& loc) { const auto first = loc.iter(); if(const auto token = lex_local_date::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto y = lex_date_fullyear::invoke(inner_loc); if(!y || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-') @@ -689,14 +676,13 @@ parse_local_date(location& loc) } } -template -result>, std::string> -parse_local_time(location& loc) +inline result>>, std::string> +parse_local_time(location& loc) { const auto first = loc.iter(); if(const auto token = lex_local_time::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto h = lex_time_hour::invoke(inner_loc); if(!h || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':') @@ -778,14 +764,13 @@ parse_local_time(location& loc) } } -template -result>, std::string> -parse_local_datetime(location& loc) +inline result>>, std::string> +parse_local_datetime(location& loc) { const auto first = loc.iter(); if(const auto token = lex_local_date_time::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto date = parse_local_date(inner_loc); if(!date || inner_loc.iter() == inner_loc.end()) { @@ -823,14 +808,13 @@ parse_local_datetime(location& loc) } } -template -result>, std::string> -parse_offset_datetime(location& loc) +inline result>>, std::string> +parse_offset_datetime(location& loc) { const auto first = loc.iter(); if(const auto token = lex_offset_date_time::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto datetime = parse_local_datetime(inner_loc); if(!datetime || inner_loc.iter() == inner_loc.end()) { @@ -872,9 +856,8 @@ parse_offset_datetime(location& loc) } } -template -result>, std::string> -parse_simple_key(location& loc) +inline result>>, std::string> +parse_simple_key(location& loc) { if(const auto bstr = parse_basic_string(loc)) { @@ -894,16 +877,15 @@ parse_simple_key(location& loc) } // dotted key become vector of keys -template -result, region>, std::string> -parse_key(location& loc) +inline result, region>>, std::string> +parse_key(location& loc) { const auto first = loc.iter(); // dotted key -> foo.bar.baz whitespaces are allowed if(const auto token = lex_dotted_key::invoke(loc)) { const auto reg = token.unwrap(); - location> inner_loc(loc.name(), reg.str()); + location inner_loc(loc.name(), reg.str()); std::vector keys; while(inner_loc.iter() != inner_loc.end()) @@ -953,12 +935,12 @@ parse_key(location& loc) } // forward-decl to implement parse_array and parse_table -template -result parse_value(location&); +template +result parse_value(location&); -template -result>, std::string> -parse_array(location& loc) +template +result>>, std::string> +parse_array(location& loc) { using value_type = Value; using array_type = typename value_type::array_type; @@ -986,7 +968,7 @@ parse_array(location& loc) { loc.advance(); // skip ']' return ok(std::make_pair(retval, - region(loc, first, loc.iter()))); + region>(loc, first, loc.iter()))); } if(auto val = parse_value(loc)) @@ -1039,7 +1021,7 @@ parse_array(location& loc) { loc.advance(); // skip ']' return ok(std::make_pair(retval, - region(loc, first, loc.iter()))); + region>(loc, first, loc.iter()))); } else { @@ -1061,9 +1043,9 @@ parse_array(location& loc) source_location(std::addressof(loc))); } -template -result, region>, Value>, std::string> -parse_key_value_pair(location& loc) +template +result, region>>, Value>, std::string> +parse_key_value_pair(location& loc) { using value_type = Value; @@ -1151,9 +1133,8 @@ std::string format_dotted_keys(InputIterator first, const InputIterator last) } // forward decl for is_valid_forward_table_definition -template -result, region>, std::string> -parse_table_key(location& loc); +result, region>>, std::string> +parse_table_key(location& loc); // The following toml file is allowed. // ```toml @@ -1182,7 +1163,7 @@ template bool is_valid_forward_table_definition(const Value& fwd, Iterator key_first, Iterator key_curr, Iterator key_last) { - location> def("internal", detail::get_region(fwd).str()); + location def("internal", detail::get_region(fwd).str()); if(const auto tabkeys = parse_table_key(def)) { // table keys always contains all the nodes from the root. @@ -1220,11 +1201,11 @@ bool is_valid_forward_table_definition(const Value& fwd, return false; } -template +template result insert_nested_key(typename Value::table_type& root, const Value& v, InputIterator iter, const InputIterator last, - region key_reg, + region> key_reg, const bool is_array_of_table = false) { static_assert(std::is_same -result>, std::string> -parse_inline_table(location& loc) +template +result>>, std::string> +parse_inline_table(location& loc) { using value_type = Value; using table_type = typename value_type::table_type; @@ -1479,8 +1460,8 @@ parse_inline_table(location& loc) if(loc.iter() != loc.end() && *loc.iter() == '}') { loc.advance(); // skip `}` - return ok(std::make_pair( - retval, region(loc, first, loc.iter()))); + return ok(std::make_pair(retval, + region>(loc, first, loc.iter()))); } const auto kv_r = parse_key_value_pair(loc); @@ -1490,7 +1471,7 @@ parse_inline_table(location& loc) } const auto& kvpair = kv_r.unwrap(); const std::vector& keys = kvpair.first.first; - const region& key_reg = kvpair.first.second; + const auto& key_reg = kvpair.first.second; const value_type& val = kvpair.second; const auto inserted = @@ -1511,7 +1492,7 @@ parse_inline_table(location& loc) { loc.advance(); // skip `}` return ok(std::make_pair( - retval, region(loc, first, loc.iter()))); + retval, region>(loc, first, loc.iter()))); } else if(*loc.iter() == '#' || *loc.iter() == '\r' || *loc.iter() == '\n') { @@ -1536,8 +1517,7 @@ parse_inline_table(location& loc) source_location(std::addressof(loc))); } -template -result guess_number_type(const location& l) +inline result guess_number_type(const location& l) { // This function tries to find some (common) mistakes by checking characters // that follows the last character of a value. But it is often difficult @@ -1545,7 +1525,7 @@ result guess_number_type(const location& l) // spaces, tabs, commas (in an array or inline table), closing brackets // (of an array or inline table), comment-sign (#). Since this function // does not parse further, those characters are always allowed to be there. - location loc = l; + location loc = l; if(lex_offset_date_time::invoke(loc)) {return ok(value_t::offset_datetime);} loc.reset(l.iter()); @@ -1674,8 +1654,7 @@ result guess_number_type(const location& l) {{std::addressof(loc), "here"}})); } -template -result guess_value_type(const location& loc) +inline result guess_value_type(const location& loc) { switch(*loc.iter()) { @@ -1691,8 +1670,8 @@ result guess_value_type(const location& loc) } } -template -result parse_value(location& loc) +template +result parse_value(location& loc) { using value_type = Value; @@ -1730,13 +1709,12 @@ result parse_value(location& loc) } } -template -result, region>, std::string> -parse_table_key(location& loc) +inline result, region>>, std::string> +parse_table_key(location& loc) { if(auto token = lex_std_table::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto open = lex_std_table_open::invoke(inner_loc); if(!open || inner_loc.iter() == inner_loc.end()) @@ -1792,13 +1770,12 @@ parse_table_key(location& loc) } } -template -result, region>, std::string> -parse_array_table_key(location& loc) +inline result, region>>, std::string> +parse_array_table_key(location& loc) { if(auto token = lex_array_table::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); const auto open = lex_array_table_open::invoke(inner_loc); if(!open || inner_loc.iter() == inner_loc.end()) @@ -1851,9 +1828,9 @@ parse_array_table_key(location& loc) } // parse table body (key-value pairs until the iter hits the next [tablekey]) -template +template result -parse_ml_table(location& loc) +parse_ml_table(location& loc) { using value_type = Value; using table_type = typename value_type::table_type; @@ -1890,7 +1867,7 @@ parse_ml_table(location& loc) { const auto& kvpair = kv.unwrap(); const std::vector& keys = kvpair.first.first; - const region& key_reg = kvpair.first.second; + const auto& key_reg = kvpair.first.second; const value_type& val = kvpair.second; const auto inserted = insert_nested_key(tab, val, keys.begin(), keys.end(), key_reg); @@ -1936,8 +1913,8 @@ parse_ml_table(location& loc) return ok(tab); } -template -result parse_toml_file(location& loc) +template +result parse_toml_file(location& loc) { using value_type = Value; using table_type = typename value_type::table_type; @@ -1950,7 +1927,7 @@ result parse_toml_file(location& loc) // put the first line as a region of a file // Here first != loc.end(), so taking std::next is okay - const region file(loc, first, std::next(loc.iter())); + const region> file(loc, first, std::next(loc.iter())); // The first successive comments that are separated from the first value // by an empty line are for a file itself. @@ -1970,7 +1947,7 @@ result parse_toml_file(location& loc) >; if(const auto token = lex_first_comments::invoke(loc)) { - location> inner_loc(loc.name(), token.unwrap().str()); + location inner_loc(loc.name(), token.unwrap().str()); while(inner_loc.iter() != inner_loc.end()) { maybe::invoke(inner_loc); // remove ws if exists @@ -2071,8 +2048,7 @@ parse(std::istream& is, const std::string& fname = "unknown file") } assert(letters.empty() || letters.back() != '\0'); - detail::location> - loc(std::move(fname), std::move(letters)); + detail::location loc(std::move(fname), std::move(letters)); // skip BOM if exists. // XXX component of BOM (like 0xEF) exceeds the representable range of diff --git a/toml/region.hpp b/toml/region.hpp index bc5d98c..55a748d 100644 --- a/toml/region.hpp +++ b/toml/region.hpp @@ -67,25 +67,18 @@ struct region_base // // it contains pointer to the file content and iterator that points the current // location. -template struct location final : public region_base { - using const_iterator = typename Container::const_iterator; + using const_iterator = typename std::vector::const_iterator; using difference_type = typename const_iterator::difference_type; - using source_ptr = std::shared_ptr; + using source_ptr = std::shared_ptr>; - static_assert(std::is_same::value,""); - static_assert(std::is_same::iterator_category>::value, - "container should be randomly accessible"); - - location(std::string name, Container cont) - : source_(std::make_shared(std::move(cont))), line_number_(1), - source_name_(std::move(name)), iter_(source_->cbegin()) + location(std::string name, std::vector cont) + : source_(std::make_shared>(std::move(cont))), + line_number_(1), source_name_(std::move(name)), iter_(source_->cbegin()) {} - location(std::string name, const std::string& cont) - : source_(std::make_shared(cont.begin(), cont.end())), + : source_(std::make_shared>(cont.begin(), cont.end())), line_number_(1), source_name_(std::move(name)), iter_(source_->cbegin()) {} @@ -215,19 +208,19 @@ struct region final : public region_base // delete default constructor. source_ never be null. region() = delete; - region(const location& loc) + region(const location& loc) : source_(loc.source()), source_name_(loc.name()), first_(loc.iter()), last_(loc.iter()) {} - region(location&& loc) + region(location&& loc) : source_(loc.source()), source_name_(loc.name()), first_(loc.iter()), last_(loc.iter()) {} - region(const location& loc, const_iterator f, const_iterator l) + region(const location& loc, const_iterator f, const_iterator l) : source_(loc.source()), source_name_(loc.name()), first_(f), last_(l) {} - region(location&& loc, const_iterator f, const_iterator l) + region(location&& loc, const_iterator f, const_iterator l) : source_(loc.source()), source_name_(loc.name()), first_(f), last_(l) {} diff --git a/toml/serializer.hpp b/toml/serializer.hpp index 4ed929a..ed07f46 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -29,7 +29,7 @@ std::basic_string format_key(const std::basic_string& key) { // check the key can be a bare (unquoted) key - detail::location loc(key, key); + detail::location loc(key, std::vector(key.begin(), key.end())); detail::lex_unquoted_key::invoke(loc); if(loc.iter() == loc.end()) {