annotate dep/toml11/toml/parser.hpp @ 338:f63dfa309380

dep/animone: separate *BSD into separate files they are wholly different operating systems with very different kernels on the inside
author Paper <paper@paper.us.eu.org>
date Wed, 19 Jun 2024 13:06:10 -0400
parents 3b355fa948c7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 // Copyright Toru Niina 2017.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 // Distributed under the MIT License.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3 #ifndef TOML11_PARSER_HPP
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 #define TOML11_PARSER_HPP
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5 #include <cstring>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <fstream>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <sstream>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9 #include "combinator.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 #include "lexer.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11 #include "macros.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 #include "region.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 #include "result.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 #include "types.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 #include "value.hpp"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 #ifndef TOML11_DISABLE_STD_FILESYSTEM
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18 #ifdef __cpp_lib_filesystem
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 #if __has_include(<filesystem>)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 #define TOML11_HAS_STD_FILESYSTEM
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 #include <filesystem>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 #endif // has_include(<string_view>)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23 #endif // __cpp_lib_filesystem
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 #endif // TOML11_DISABLE_STD_FILESYSTEM
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 // the previous commit works with 500+ recursions. so it may be too small.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
27 // but in most cases, i think we don't need such a deep recursion of
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
28 // arrays or inline-tables.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
29 #define TOML11_VALUE_RECURSION_LIMIT 64
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
30
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
31 namespace toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
32 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
33 namespace detail
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
34 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
35
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
36 inline result<std::pair<boolean, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
37 parse_boolean(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
38 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
39 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
40 if(const auto token = lex_boolean::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
41 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
42 const auto reg = token.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
43 if (reg.str() == "true") {return ok(std::make_pair(true, reg));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
44 else if(reg.str() == "false") {return ok(std::make_pair(false, reg));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
45 else // internal error.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
46 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
47 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
48 "toml::parse_boolean: internal error",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
49 {{source_location(reg), "invalid token"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
50 source_location(reg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
51 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
52 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
53 loc.reset(first); //rollback
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
54 return err(format_underline("toml::parse_boolean: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
55 {{source_location(loc), "the next token is not a boolean"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
56 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
57
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
58 inline result<std::pair<integer, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
59 parse_binary_integer(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
60 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
61 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
62 if(const auto token = lex_bin_int::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
63 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
64 auto str = token.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
65 assert(str.size() > 2); // minimum -> 0b1
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
66 assert(str.at(0) == '0' && str.at(1) == 'b');
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
67
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
68 // skip all the zeros and `_` locating at the MSB
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
69 str.erase(str.begin(), std::find_if(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
70 str.begin() + 2, // to skip prefix `0b`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
71 str.end(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
72 [](const char c) { return c == '1'; })
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
73 );
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
74 assert(str.empty() || str.front() == '1');
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
75
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
76 // since toml11 uses int64_t, 64bit (unsigned) input cannot be read.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
77 const auto max_length = 63 + std::count(str.begin(), str.end(), '_');
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
78 if(static_cast<std::string::size_type>(max_length) < str.size())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
79 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
80 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
81 return err(format_underline("toml::parse_binary_integer: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
82 "only signed 64bit integer is available",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
83 {{source_location(loc), "too large input (> int64_t)"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
84 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
85
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
86 integer retval(0), base(1);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
87 for(auto i(str.rbegin()), e(str.rend()); i!=e; ++i)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
88 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
89 assert(base != 0); // means overflow, checked in the above code
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
90 if(*i == '1')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
91 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
92 retval += base;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
93 if( (std::numeric_limits<integer>::max)() / 2 < base )
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
94 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
95 base = 0;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
96 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
97 base *= 2;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
98 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
99 else if(*i == '0')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
100 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
101 if( (std::numeric_limits<integer>::max)() / 2 < base )
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
102 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
103 base = 0;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
104 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
105 base *= 2;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
106 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
107 else if(*i == '_')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
108 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
109 // do nothing.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
110 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
111 else // should be detected by lex_bin_int. [[unlikely]]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
112 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
113 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
114 "toml::parse_binary_integer: internal error",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
115 {{source_location(token.unwrap()), "invalid token"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
116 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
117 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
118 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
119 return ok(std::make_pair(retval, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
120 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
121 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
122 return err(format_underline("toml::parse_binary_integer:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
123 {{source_location(loc), "the next token is not an integer"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
124 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
125
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
126 inline result<std::pair<integer, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
127 parse_octal_integer(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
128 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
129 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
130 if(const auto token = lex_oct_int::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
131 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
132 auto str = token.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
133 str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
134 str.erase(str.begin()); str.erase(str.begin()); // remove `0o` prefix
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
135
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
136 std::istringstream iss(str);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
137 integer retval(0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
138 iss >> std::oct >> retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
139 if(iss.fail())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
140 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
141 // `istream` sets `failbit` if internally-called `std::num_get::get`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
142 // fails.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
143 // `std::num_get::get` calls `std::strtoll` if the argument type is
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
144 // signed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
145 // `std::strtoll` fails if
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
146 // - the value is out_of_range or
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
147 // - no conversion is possible.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
148 // since we already checked that the string is valid octal integer,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
149 // so the error reason is out_of_range.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
150 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
151 return err(format_underline("toml::parse_octal_integer:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
152 {{source_location(loc), "out of range"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
153 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
154 return ok(std::make_pair(retval, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
155 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
156 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
157 return err(format_underline("toml::parse_octal_integer:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
158 {{source_location(loc), "the next token is not an integer"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
159 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
160
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
161 inline result<std::pair<integer, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
162 parse_hexadecimal_integer(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
163 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
164 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
165 if(const auto token = lex_hex_int::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
166 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
167 auto str = token.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
168 str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
169 str.erase(str.begin()); str.erase(str.begin()); // remove `0x` prefix
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
170
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
171 std::istringstream iss(str);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
172 integer retval(0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
173 iss >> std::hex >> retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
174 if(iss.fail())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
175 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
176 // see parse_octal_integer for detail of this error message.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
177 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
178 return err(format_underline("toml::parse_hexadecimal_integer:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
179 {{source_location(loc), "out of range"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
180 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
181 return ok(std::make_pair(retval, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
182 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
183 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
184 return err(format_underline("toml::parse_hexadecimal_integer",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
185 {{source_location(loc), "the next token is not an integer"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
186 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
187
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
188 inline result<std::pair<integer, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
189 parse_integer(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
190 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
191 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
192 if(first != loc.end() && *first == '0')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
193 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
194 const auto second = std::next(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
195 if(second == loc.end()) // the token is just zero.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
196 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
197 loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
198 return ok(std::make_pair(0, region(loc, first, second)));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
199 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
200
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
201 if(*second == 'b') {return parse_binary_integer (loc);} // 0b1100
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
202 if(*second == 'o') {return parse_octal_integer (loc);} // 0o775
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
203 if(*second == 'x') {return parse_hexadecimal_integer(loc);} // 0xC0FFEE
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
204
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
205 if(std::isdigit(*second))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
206 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
207 return err(format_underline("toml::parse_integer: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
208 "leading zero in an Integer is not allowed.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
209 {{source_location(loc), "leading zero"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
210 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
211 else if(std::isalpha(*second))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
212 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
213 return err(format_underline("toml::parse_integer: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
214 "unknown integer prefix appeared.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
215 {{source_location(loc), "none of 0x, 0o, 0b"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
216 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
217 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
218
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
219 if(const auto token = lex_dec_int::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
220 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
221 auto str = token.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
222 str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
223
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
224 std::istringstream iss(str);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
225 integer retval(0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
226 iss >> retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
227 if(iss.fail())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
228 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
229 // see parse_octal_integer for detail of this error message.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
230 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
231 return err(format_underline("toml::parse_integer:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
232 {{source_location(loc), "out of range"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
233 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
234 return ok(std::make_pair(retval, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
235 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
236 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
237 return err(format_underline("toml::parse_integer: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
238 {{source_location(loc), "the next token is not an integer"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
239 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
240
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
241 inline result<std::pair<floating, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
242 parse_floating(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
243 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
244 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
245 if(const auto token = lex_float::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
246 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
247 auto str = token.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
248 if(str == "inf" || str == "+inf")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
249 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
250 if(std::numeric_limits<floating>::has_infinity)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
251 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
252 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
253 std::numeric_limits<floating>::infinity(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
254 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
255 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
256 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
257 throw std::domain_error("toml::parse_floating: inf value found"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
258 " but the current environment does not support inf. Please"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
259 " make sure that the floating-point implementation conforms"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
260 " IEEE 754/ISO 60559 international standard.");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
261 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
262 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
263 else if(str == "-inf")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
264 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
265 if(std::numeric_limits<floating>::has_infinity)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
266 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
267 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
268 -std::numeric_limits<floating>::infinity(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
269 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
270 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
271 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
272 throw std::domain_error("toml::parse_floating: inf value found"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
273 " but the current environment does not support inf. Please"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
274 " make sure that the floating-point implementation conforms"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
275 " IEEE 754/ISO 60559 international standard.");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
276 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
277 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
278 else if(str == "nan" || str == "+nan")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
279 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
280 if(std::numeric_limits<floating>::has_quiet_NaN)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
281 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
282 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
283 std::numeric_limits<floating>::quiet_NaN(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
284 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
285 else if(std::numeric_limits<floating>::has_signaling_NaN)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
286 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
287 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
288 std::numeric_limits<floating>::signaling_NaN(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
289 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
290 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
291 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
292 throw std::domain_error("toml::parse_floating: NaN value found"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
293 " but the current environment does not support NaN. Please"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
294 " make sure that the floating-point implementation conforms"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
295 " IEEE 754/ISO 60559 international standard.");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
296 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
297 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
298 else if(str == "-nan")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
299 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
300 if(std::numeric_limits<floating>::has_quiet_NaN)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
301 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
302 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
303 -std::numeric_limits<floating>::quiet_NaN(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
304 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
305 else if(std::numeric_limits<floating>::has_signaling_NaN)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
306 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
307 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
308 -std::numeric_limits<floating>::signaling_NaN(), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
309 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
310 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
311 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
312 throw std::domain_error("toml::parse_floating: NaN value found"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
313 " but the current environment does not support NaN. Please"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
314 " make sure that the floating-point implementation conforms"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
315 " IEEE 754/ISO 60559 international standard.");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
316 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
317 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
318 str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
319 std::istringstream iss(str);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
320 floating v(0.0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
321 iss >> v;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
322 if(iss.fail())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
323 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
324 // see parse_octal_integer for detail of this error message.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
325 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
326 return err(format_underline("toml::parse_floating:",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
327 {{source_location(loc), "out of range"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
328 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
329 return ok(std::make_pair(v, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
330 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
331 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
332 return err(format_underline("toml::parse_floating: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
333 {{source_location(loc), "the next token is not a float"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
334 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
335
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
336 inline std::string read_utf8_codepoint(const region& reg, const location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
337 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
338 const auto str = reg.str().substr(1);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
339 std::uint_least32_t codepoint;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
340 std::istringstream iss(str);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
341 iss >> std::hex >> codepoint;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
342
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
343 const auto to_char = [](const std::uint_least32_t i) noexcept -> char {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
344 const auto uc = static_cast<unsigned char>(i);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
345 return *reinterpret_cast<const char*>(std::addressof(uc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
346 };
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
347
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
348 std::string character;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
349 if(codepoint < 0x80) // U+0000 ... U+0079 ; just an ASCII.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
350 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
351 character += static_cast<char>(codepoint);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
352 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
353 else if(codepoint < 0x800) //U+0080 ... U+07FF
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
354 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
355 // 110yyyyx 10xxxxxx; 0x3f == 0b0011'1111
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
356 character += to_char(0xC0| codepoint >> 6);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
357 character += to_char(0x80|(codepoint & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
358 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
359 else if(codepoint < 0x10000) // U+0800...U+FFFF
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
360 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
361 if(0xD800 <= codepoint && codepoint <= 0xDFFF)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
362 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
363 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
364 "toml::read_utf8_codepoint: codepoints in the range "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
365 "[0xD800, 0xDFFF] are not valid UTF-8.", {{
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
366 source_location(loc), "not a valid UTF-8 codepoint"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
367 }}), source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
368 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
369 assert(codepoint < 0xD800 || 0xDFFF < codepoint);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
370 // 1110yyyy 10yxxxxx 10xxxxxx
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
371 character += to_char(0xE0| codepoint >> 12);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
372 character += to_char(0x80|(codepoint >> 6 & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
373 character += to_char(0x80|(codepoint & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
374 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
375 else if(codepoint < 0x110000) // U+010000 ... U+10FFFF
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
376 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
377 // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
378 character += to_char(0xF0| codepoint >> 18);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
379 character += to_char(0x80|(codepoint >> 12 & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
380 character += to_char(0x80|(codepoint >> 6 & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
381 character += to_char(0x80|(codepoint & 0x3F));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
382 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
383 else // out of UTF-8 region
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
384 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
385 throw syntax_error(format_underline("toml::read_utf8_codepoint:"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
386 " input codepoint is too large.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
387 {{source_location(loc), "should be in [0x00..0x10FFFF]"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
388 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
389 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
390 return character;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
391 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
392
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
393 inline result<std::string, std::string> parse_escape_sequence(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
394 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
395 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
396 if(first == loc.end() || *first != '\\')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
397 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
398 return err(format_underline("toml::parse_escape_sequence: ", {{
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
399 source_location(loc), "the next token is not a backslash \"\\\""}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
400 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
401 loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
402 switch(*loc.iter())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
403 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
404 case '\\':{loc.advance(); return ok(std::string("\\"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
405 case '"' :{loc.advance(); return ok(std::string("\""));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
406 case 'b' :{loc.advance(); return ok(std::string("\b"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
407 case 't' :{loc.advance(); return ok(std::string("\t"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
408 case 'n' :{loc.advance(); return ok(std::string("\n"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
409 case 'f' :{loc.advance(); return ok(std::string("\f"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
410 case 'r' :{loc.advance(); return ok(std::string("\r"));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
411 #ifdef TOML11_USE_UNRELEASED_TOML_FEATURES
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
412 case 'e' :{loc.advance(); return ok(std::string("\x1b"));} // ESC
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
413 #endif
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
414 case 'u' :
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
415 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
416 if(const auto token = lex_escape_unicode_short::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
417 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
418 return ok(read_utf8_codepoint(token.unwrap(), loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
419 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
420 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
421 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
422 return err(format_underline("parse_escape_sequence: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
423 "invalid token found in UTF-8 codepoint uXXXX.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
424 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
425 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
426 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
427 case 'U':
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
428 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
429 if(const auto token = lex_escape_unicode_long::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
430 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
431 return ok(read_utf8_codepoint(token.unwrap(), loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
432 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
433 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
434 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
435 return err(format_underline("parse_escape_sequence: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
436 "invalid token found in UTF-8 codepoint Uxxxxxxxx",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
437 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
438 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
439 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
440 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
441
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
442 const auto msg = format_underline("parse_escape_sequence: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
443 "unknown escape sequence appeared.", {{source_location(loc),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
444 "escape sequence is one of \\, \", b, t, n, f, r, uxxxx, Uxxxxxxxx"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
445 /* Hints = */{"if you want to write backslash as just one backslash, "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
446 "use literal string like: regex = '<\\i\\c*\\s*>'"});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
447 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
448 return err(msg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
449 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
450
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
451 inline std::ptrdiff_t check_utf8_validity(const std::string& reg)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
452 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
453 location loc("tmp", reg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
454 const auto u8 = repeat<lex_utf8_code, unlimited>::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
455 if(!u8 || loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
456 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
457 const auto error_location = std::distance(loc.begin(), loc.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
458 assert(0 <= error_location);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
459 return error_location;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
460 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
461 return -1;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
462 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
463
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
464 inline result<std::pair<toml::string, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
465 parse_ml_basic_string(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
466 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
467 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
468 if(const auto token = lex_ml_basic_string::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
469 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
470 auto inner_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
471 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
472
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
473 std::string retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
474 retval.reserve(token.unwrap().size());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
475
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
476 auto delim = lex_ml_basic_string_open::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
477 if(!delim)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
478 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
479 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
480 "parse_ml_basic_string: invalid token",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
481 {{source_location(inner_loc), "should be \"\"\""}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
482 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
483 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
484 // immediate newline is ignored (if exists)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
485 /* discard return value */ lex_newline::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
486
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
487 delim = none();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
488 while(!delim)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
489 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
490 using lex_unescaped_seq = repeat<
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
491 either<lex_ml_basic_unescaped, lex_newline>, unlimited>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
492 if(auto unescaped = lex_unescaped_seq::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
493 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
494 retval += unescaped.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
495 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
496 if(auto escaped = parse_escape_sequence(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
497 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
498 retval += escaped.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
499 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
500 if(auto esc_nl = lex_ml_basic_escaped_newline::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
501 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
502 // ignore newline after escape until next non-ws char
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
503 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
504 if(inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
505 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
506 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
507 "parse_ml_basic_string: unexpected end of region",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
508 {{source_location(inner_loc), "not sufficient token"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
509 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
510 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
511 delim = lex_ml_basic_string_close::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
512 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
513 // `lex_ml_basic_string_close` allows 3 to 5 `"`s to allow 1 or 2 `"`s
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
514 // at just before the delimiter. Here, we need to attach `"`s at the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
515 // end of the string body, if it exists.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
516 // For detail, see the definition of `lex_ml_basic_string_close`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
517 assert(std::all_of(delim.unwrap().first(), delim.unwrap().last(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
518 [](const char c) noexcept {return c == '\"';}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
519 switch(delim.unwrap().size())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
520 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
521 case 3: {break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
522 case 4: {retval += "\""; break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
523 case 5: {retval += "\"\""; break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
524 default:
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
525 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
526 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
527 "parse_ml_basic_string: closing delimiter has invalid length",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
528 {{source_location(inner_loc), "end of this"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
529 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
530 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
531 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
532
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
533 const auto err_loc = check_utf8_validity(token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
534 if(err_loc == -1)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
535 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
536 return ok(std::make_pair(toml::string(retval), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
537 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
538 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
539 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
540 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
541 inner_loc.advance(err_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
542 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
543 "parse_ml_basic_string: invalid utf8 sequence found",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
544 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
545 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
546 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
547 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
548 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
549 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
550 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
551 return err(format_underline("toml::parse_ml_basic_string: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
552 "the next token is not a valid multiline string",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
553 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
554 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
555 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
556
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
557 inline result<std::pair<toml::string, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
558 parse_basic_string(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
559 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
560 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
561 if(const auto token = lex_basic_string::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
562 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
563 auto inner_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
564 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
565
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
566 auto quot = lex_quotation_mark::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
567 if(!quot)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
568 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
569 throw internal_error(format_underline("parse_basic_string: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
570 "invalid token", {{source_location(inner_loc), "should be \""}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
571 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
572 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
573
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
574 std::string retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
575 retval.reserve(token.unwrap().size());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
576
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
577 quot = none();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
578 while(!quot)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
579 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
580 using lex_unescaped_seq = repeat<lex_basic_unescaped, unlimited>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
581 if(auto unescaped = lex_unescaped_seq::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
582 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
583 retval += unescaped.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
584 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
585 if(auto escaped = parse_escape_sequence(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
586 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
587 retval += escaped.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
588 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
589 if(inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
590 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
591 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
592 "parse_basic_string: unexpected end of region",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
593 {{source_location(inner_loc), "not sufficient token"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
594 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
595 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
596 quot = lex_quotation_mark::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
597 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
598
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
599 const auto err_loc = check_utf8_validity(token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
600 if(err_loc == -1)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
601 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
602 return ok(std::make_pair(toml::string(retval), token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
603 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
604 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
605 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
606 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
607 inner_loc.advance(err_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
608 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
609 "parse_basic_string: invalid utf8 sequence found",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
610 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
611 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
612 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
613 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
614 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
615 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
616 loc.reset(first); // rollback
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
617 return err(format_underline("toml::parse_basic_string: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
618 "the next token is not a valid string",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
619 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
620 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
621 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
622
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
623 inline result<std::pair<toml::string, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
624 parse_ml_literal_string(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
625 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
626 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
627 if(const auto token = lex_ml_literal_string::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
628 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
629 auto inner_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
630 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
631
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
632 const auto open = lex_ml_literal_string_open::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
633 if(!open)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
634 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
635 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
636 "parse_ml_literal_string: invalid token",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
637 {{source_location(inner_loc), "should be '''"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
638 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
639 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
640 // immediate newline is ignored (if exists)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
641 /* discard return value */ lex_newline::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
642
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
643 const auto body = lex_ml_literal_body::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
644
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
645 const auto close = lex_ml_literal_string_close::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
646 if(!close)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
647 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
648 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
649 "parse_ml_literal_string: invalid token",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
650 {{source_location(inner_loc), "should be '''"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
651 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
652 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
653 // `lex_ml_literal_string_close` allows 3 to 5 `'`s to allow 1 or 2 `'`s
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
654 // at just before the delimiter. Here, we need to attach `'`s at the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
655 // end of the string body, if it exists.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
656 // For detail, see the definition of `lex_ml_basic_string_close`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
657
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
658 std::string retval = body.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
659 assert(std::all_of(close.unwrap().first(), close.unwrap().last(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
660 [](const char c) noexcept {return c == '\'';}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
661 switch(close.unwrap().size())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
662 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
663 case 3: {break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
664 case 4: {retval += "'"; break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
665 case 5: {retval += "''"; break;}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
666 default:
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
667 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
668 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
669 "parse_ml_literal_string: closing delimiter has invalid length",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
670 {{source_location(inner_loc), "end of this"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
671 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
672 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
673 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
674
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
675 const auto err_loc = check_utf8_validity(token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
676 if(err_loc == -1)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
677 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
678 return ok(std::make_pair(toml::string(retval, toml::string_t::literal),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
679 token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
680 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
681 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
682 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
683 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
684 inner_loc.advance(err_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
685 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
686 "parse_ml_literal_string: invalid utf8 sequence found",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
687 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
688 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
689 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
690 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
691 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
692 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
693 loc.reset(first); // rollback
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
694 return err(format_underline("toml::parse_ml_literal_string: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
695 "the next token is not a valid multiline literal string",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
696 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
697 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
698 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
699
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
700 inline result<std::pair<toml::string, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
701 parse_literal_string(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
702 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
703 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
704 if(const auto token = lex_literal_string::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
705 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
706 auto inner_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
707 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
708
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
709 const auto open = lex_apostrophe::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
710 if(!open)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
711 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
712 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
713 "parse_literal_string: invalid token",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
714 {{source_location(inner_loc), "should be '"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
715 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
716 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
717
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
718 const auto body = repeat<lex_literal_char, unlimited>::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
719
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
720 const auto close = lex_apostrophe::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
721 if(!close)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
722 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
723 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
724 "parse_literal_string: invalid token",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
725 {{source_location(inner_loc), "should be '"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
726 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
727 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
728
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
729 const auto err_loc = check_utf8_validity(token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
730 if(err_loc == -1)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
731 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
732 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
733 toml::string(body.unwrap().str(), toml::string_t::literal),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
734 token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
735 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
736 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
737 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
738 inner_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
739 inner_loc.advance(err_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
740 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
741 "parse_literal_string: invalid utf8 sequence found",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
742 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
743 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
744 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
745 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
746 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
747 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
748 loc.reset(first); // rollback
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
749 return err(format_underline("toml::parse_literal_string: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
750 "the next token is not a valid literal string",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
751 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
752 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
753 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
754
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
755 inline result<std::pair<toml::string, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
756 parse_string(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
757 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
758 if(loc.iter() != loc.end() && *(loc.iter()) == '"')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
759 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
760 if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '"' &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
761 loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '"')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
762 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
763 return parse_ml_basic_string(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
764 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
765 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
766 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
767 return parse_basic_string(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
768 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
769 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
770 else if(loc.iter() != loc.end() && *(loc.iter()) == '\'')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
771 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
772 if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '\'' &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
773 loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '\'')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
774 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
775 return parse_ml_literal_string(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
776 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
777 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
778 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
779 return parse_literal_string(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
780 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
781 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
782 return err(format_underline("toml::parse_string: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
783 {{source_location(loc), "the next token is not a string"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
784 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
785
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
786 inline result<std::pair<local_date, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
787 parse_local_date(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
788 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
789 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
790 if(const auto token = lex_local_date::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
791 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
792 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
793
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
794 const auto y = lex_date_fullyear::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
795 if(!y || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
796 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
797 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
798 "toml::parse_local_date: invalid year format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
799 {{source_location(inner_loc), "should be `-`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
800 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
801 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
802 inner_loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
803 const auto m = lex_date_month::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
804 if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
805 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
806 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
807 "toml::parse_local_date: invalid month format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
808 {{source_location(inner_loc), "should be `-`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
809 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
810 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
811 inner_loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
812 const auto d = lex_date_mday::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
813 if(!d)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
814 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
815 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
816 "toml::parse_local_date: invalid day format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
817 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
818 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
819 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
820
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
821 const auto year = static_cast<std::int16_t>(from_string<int>(y.unwrap().str(), 0));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
822 const auto month = static_cast<std::int8_t >(from_string<int>(m.unwrap().str(), 0));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
823 const auto day = static_cast<std::int8_t >(from_string<int>(d.unwrap().str(), 0));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
824
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
825 // We briefly check whether the input date is valid or not. But here, we
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
826 // only check if the RFC3339 compliance.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
827 // Actually there are several special date that does not exist,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
828 // because of historical reasons, such as 1582/10/5-1582/10/14 (only in
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
829 // several countries). But here, we do not care about such a complicated
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
830 // rule. It makes the code complicated and there is only low probability
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
831 // that such a specific date is needed in practice. If someone need to
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
832 // validate date accurately, that means that the one need a specialized
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
833 // library for their purpose in a different layer.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
834 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
835 const bool is_leap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
836 const auto max_day = (month == 2) ? (is_leap ? 29 : 28) :
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
837 ((month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
838
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
839 if((month < 1 || 12 < month) || (day < 1 || max_day < day))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
840 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
841 throw syntax_error(format_underline("toml::parse_date: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
842 "invalid date: it does not conform RFC3339.", {{
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
843 source_location(loc), "month should be 01-12, day should be"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
844 " 01-28,29,30,31, depending on month/year."
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
845 }}), source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
846 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
847 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
848 return ok(std::make_pair(local_date(year, static_cast<month_t>(month - 1), day),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
849 token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
850 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
851 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
852 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
853 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
854 return err(format_underline("toml::parse_local_date: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
855 {{source_location(loc), "the next token is not a local_date"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
856 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
857 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
858
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
859 inline result<std::pair<local_time, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
860 parse_local_time(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
861 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
862 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
863 if(const auto token = lex_local_time::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
864 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
865 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
866
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
867 const auto h = lex_time_hour::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
868 if(!h || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
869 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
870 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
871 "toml::parse_local_time: invalid year format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
872 {{source_location(inner_loc), "should be `:`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
873 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
874 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
875 inner_loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
876 const auto m = lex_time_minute::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
877 if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
878 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
879 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
880 "toml::parse_local_time: invalid month format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
881 {{source_location(inner_loc), "should be `:`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
882 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
883 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
884 inner_loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
885 const auto s = lex_time_second::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
886 if(!s)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
887 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
888 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
889 "toml::parse_local_time: invalid second format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
890 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
891 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
892 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
893
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
894 const int hour = from_string<int>(h.unwrap().str(), 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
895 const int minute = from_string<int>(m.unwrap().str(), 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
896 const int second = from_string<int>(s.unwrap().str(), 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
897
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
898 if((hour < 0 || 23 < hour) || (minute < 0 || 59 < minute) ||
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
899 (second < 0 || 60 < second)) // it may be leap second
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
900 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
901 throw syntax_error(format_underline("toml::parse_local_time: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
902 "invalid time: it does not conform RFC3339.", {{
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
903 source_location(loc), "hour should be 00-23, minute should be"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
904 " 00-59, second should be 00-60 (depending on the leap"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
905 " second rules.)"}}), source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
906 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
907
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
908 local_time time(hour, minute, second, 0, 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
909
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
910 const auto before_secfrac = inner_loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
911 if(const auto secfrac = lex_time_secfrac::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
912 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
913 auto sf = secfrac.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
914 sf.erase(sf.begin()); // sf.front() == '.'
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
915 switch(sf.size() % 3)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
916 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
917 case 2: sf += '0'; break;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
918 case 1: sf += "00"; break;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
919 case 0: break;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
920 default: break;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
921 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
922 if(sf.size() >= 9)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
923 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
924 time.millisecond = from_string<std::uint16_t>(sf.substr(0, 3), 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
925 time.microsecond = from_string<std::uint16_t>(sf.substr(3, 3), 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
926 time.nanosecond = from_string<std::uint16_t>(sf.substr(6, 3), 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
927 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
928 else if(sf.size() >= 6)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
929 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
930 time.millisecond = from_string<std::uint16_t>(sf.substr(0, 3), 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
931 time.microsecond = from_string<std::uint16_t>(sf.substr(3, 3), 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
932 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
933 else if(sf.size() >= 3)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
934 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
935 time.millisecond = from_string<std::uint16_t>(sf, 0u);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
936 time.microsecond = 0u;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
937 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
938 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
939 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
940 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
941 if(before_secfrac != inner_loc.iter())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
942 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
943 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
944 "toml::parse_local_time: invalid subsecond format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
945 {{source_location(inner_loc), "here"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
946 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
947 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
948 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
949 return ok(std::make_pair(time, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
950 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
951 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
952 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
953 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
954 return err(format_underline("toml::parse_local_time: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
955 {{source_location(loc), "the next token is not a local_time"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
956 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
957 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
958
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
959 inline result<std::pair<local_datetime, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
960 parse_local_datetime(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
961 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
962 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
963 if(const auto token = lex_local_date_time::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
964 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
965 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
966 const auto date = parse_local_date(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
967 if(!date || inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
968 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
969 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
970 "toml::parse_local_datetime: invalid datetime format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
971 {{source_location(inner_loc), "date, not datetime"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
972 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
973 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
974 const char delim = *(inner_loc.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
975 if(delim != 'T' && delim != 't' && delim != ' ')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
976 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
977 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
978 "toml::parse_local_datetime: invalid datetime format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
979 {{source_location(inner_loc), "should be `T` or ` ` (space)"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
980 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
981 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
982 inner_loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
983 const auto time = parse_local_time(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
984 if(!time)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
985 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
986 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
987 "toml::parse_local_datetime: invalid datetime format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
988 {{source_location(inner_loc), "invalid time format"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
989 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
990 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
991 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
992 local_datetime(date.unwrap().first, time.unwrap().first),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
993 token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
994 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
995 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
996 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
997 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
998 return err(format_underline("toml::parse_local_datetime: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
999 {{source_location(loc), "the next token is not a local_datetime"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1000 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1001 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1002
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1003 inline result<std::pair<offset_datetime, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1004 parse_offset_datetime(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1005 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1006 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1007 if(const auto token = lex_offset_date_time::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1008 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1009 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1010 const auto datetime = parse_local_datetime(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1011 if(!datetime || inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1012 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1013 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1014 "toml::parse_offset_datetime: invalid datetime format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1015 {{source_location(inner_loc), "date, not datetime"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1016 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1017 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1018 time_offset offset(0, 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1019 if(const auto ofs = lex_time_numoffset::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1020 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1021 const auto str = ofs.unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1022
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1023 const auto hour = from_string<int>(str.substr(1,2), 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1024 const auto minute = from_string<int>(str.substr(4,2), 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1025
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1026 if((hour < 0 || 23 < hour) || (minute < 0 || 59 < minute))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1027 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1028 throw syntax_error(format_underline("toml::parse_offset_datetime: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1029 "invalid offset: it does not conform RFC3339.", {{
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1030 source_location(loc), "month should be 01-12, day should be"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1031 " 01-28,29,30,31, depending on month/year."
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1032 }}), source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1033 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1034
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1035 if(str.front() == '+')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1036 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1037 offset = time_offset(hour, minute);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1038 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1039 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1040 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1041 offset = time_offset(-hour, -minute);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1042 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1043 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1044 else if(*inner_loc.iter() != 'Z' && *inner_loc.iter() != 'z')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1045 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1046 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1047 "toml::parse_offset_datetime: invalid datetime format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1048 {{source_location(inner_loc), "should be `Z` or `+HH:MM`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1049 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1050 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1051 return ok(std::make_pair(offset_datetime(datetime.unwrap().first, offset),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1052 token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1053 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1054 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1055 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1056 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1057 return err(format_underline("toml::parse_offset_datetime: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1058 {{source_location(loc), "the next token is not a offset_datetime"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1059 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1060 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1061
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1062 inline result<std::pair<key, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1063 parse_simple_key(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1064 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1065 if(const auto bstr = parse_basic_string(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1066 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1067 return ok(std::make_pair(bstr.unwrap().first.str, bstr.unwrap().second));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1068 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1069 if(const auto lstr = parse_literal_string(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1070 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1071 return ok(std::make_pair(lstr.unwrap().first.str, lstr.unwrap().second));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1072 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1073 if(const auto bare = lex_unquoted_key::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1074 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1075 const auto reg = bare.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1076 return ok(std::make_pair(reg.str(), reg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1077 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1078 return err(format_underline("toml::parse_simple_key: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1079 {{source_location(loc), "the next token is not a simple key"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1080 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1081
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1082 // dotted key become vector of keys
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1083 inline result<std::pair<std::vector<key>, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1084 parse_key(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1085 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1086 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1087 // dotted key -> `foo.bar.baz` where several single keys are chained by
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1088 // dots. Whitespaces between keys and dots are allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1089 if(const auto token = lex_dotted_key::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1090 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1091 const auto reg = token.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1092 location inner_loc(loc.name(), reg.str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1093 std::vector<key> keys;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1094
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1095 while(inner_loc.iter() != inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1096 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1097 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1098 if(const auto k = parse_simple_key(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1099 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1100 keys.push_back(k.unwrap().first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1101 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1102 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1103 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1104 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1105 "toml::parse_key: dotted key contains invalid key",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1106 {{source_location(inner_loc), k.unwrap_err()}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1107 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1108 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1109
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1110 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1111 if(inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1112 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1113 break;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1114 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1115 else if(*inner_loc.iter() == '.')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1116 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1117 inner_loc.advance(); // to skip `.`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1118 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1119 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1120 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1121 throw internal_error(format_underline("toml::parse_key: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1122 "dotted key contains invalid key ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1123 {{source_location(inner_loc), "should be `.`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1124 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1125 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1126 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1127 return ok(std::make_pair(keys, reg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1128 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1129 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1130
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1131 // simple_key: a single (basic_string|literal_string|bare key)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1132 if(const auto smpl = parse_simple_key(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1133 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1134 return ok(std::make_pair(std::vector<key>(1, smpl.unwrap().first),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1135 smpl.unwrap().second));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1136 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1137 return err(format_underline("toml::parse_key: an invalid key appeared.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1138 {{source_location(loc), "is not a valid key"}}, {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1139 "bare keys : non-empty strings composed only of [A-Za-z0-9_-].",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1140 "quoted keys: same as \"basic strings\" or 'literal strings'.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1141 "dotted keys: sequence of bare or quoted keys joined with a dot."
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1142 }));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1143 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1144
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1145 // forward-decl to implement parse_array and parse_table
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1146 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1147 result<Value, std::string> parse_value(location&, const std::size_t n_rec);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1148
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1149 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1150 result<std::pair<typename Value::array_type, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1151 parse_array(location& loc, const std::size_t n_rec)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1152 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1153 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1154 using array_type = typename value_type::array_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1155
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1156 if(n_rec > TOML11_VALUE_RECURSION_LIMIT)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1157 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1158 // parse_array does not have any way to handle recursive error currently...
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1159 throw syntax_error(std::string("toml::parse_array: recursion limit ("
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1160 TOML11_STRINGIZE(TOML11_VALUE_RECURSION_LIMIT) ") exceeded"),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1161 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1162 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1163
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1164 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1165 if(loc.iter() == loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1166 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1167 return err("toml::parse_array: input is empty");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1168 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1169 if(*loc.iter() != '[')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1170 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1171 return err("toml::parse_array: token is not an array");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1172 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1173 loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1174
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1175 using lex_ws_comment_newline = repeat<
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1176 either<lex_wschar, lex_newline, lex_comment>, unlimited>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1177
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1178 array_type retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1179 while(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1180 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1181 lex_ws_comment_newline::invoke(loc); // skip
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1182
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1183 if(loc.iter() != loc.end() && *loc.iter() == ']')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1184 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1185 loc.advance(); // skip ']'
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1186 return ok(std::make_pair(retval,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1187 region(loc, first, loc.iter())));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1188 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1189
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1190 if(auto val = parse_value<value_type>(loc, n_rec+1))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1191 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1192 // After TOML v1.0.0-rc.1, array becomes to be able to have values
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1193 // with different types. So here we will omit this by default.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1194 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1195 // But some of the test-suite checks if the parser accepts a hetero-
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1196 // geneous arrays, so we keep this for a while.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1197 #ifdef TOML11_DISALLOW_HETEROGENEOUS_ARRAYS
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1198 if(!retval.empty() && retval.front().type() != val.as_ok().type())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1199 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1200 auto array_start_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1201 array_start_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1202
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1203 throw syntax_error(format_underline("toml::parse_array: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1204 "type of elements should be the same each other.", {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1205 {source_location(array_start_loc), "array starts here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1206 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1207 retval.front().location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1208 "value has type " + stringize(retval.front().type())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1209 },
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1210 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1211 val.unwrap().location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1212 "value has different type, " + stringize(val.unwrap().type())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1213 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1214 }), source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1215 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1216 #endif
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1217 retval.push_back(std::move(val.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1218 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1219 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1220 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1221 auto array_start_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1222 array_start_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1223
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1224 throw syntax_error(format_underline("toml::parse_array: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1225 "value having invalid format appeared in an array", {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1226 {source_location(array_start_loc), "array starts here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1227 {source_location(loc), "it is not a valid value."}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1228 }), source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1229 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1230
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1231 using lex_array_separator = sequence<maybe<lex_ws_comment_newline>, character<','>>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1232 const auto sp = lex_array_separator::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1233 if(!sp)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1234 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1235 lex_ws_comment_newline::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1236 if(loc.iter() != loc.end() && *loc.iter() == ']')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1237 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1238 loc.advance(); // skip ']'
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1239 return ok(std::make_pair(retval,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1240 region(loc, first, loc.iter())));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1241 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1242 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1243 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1244 auto array_start_loc = loc;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1245 array_start_loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1246
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1247 throw syntax_error(format_underline("toml::parse_array:"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1248 " missing array separator `,` after a value", {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1249 {source_location(array_start_loc), "array starts here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1250 {source_location(loc), "should be `,`"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1251 }), source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1252 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1253 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1254 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1255 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1256 throw syntax_error(format_underline("toml::parse_array: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1257 "array did not closed by `]`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1258 {{source_location(loc), "should be closed"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1259 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1260 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1261
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1262 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1263 result<std::pair<std::pair<std::vector<key>, region>, Value>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1264 parse_key_value_pair(location& loc, const std::size_t n_rec)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1265 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1266 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1267
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1268 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1269 auto key_reg = parse_key(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1270 if(!key_reg)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1271 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1272 std::string msg = std::move(key_reg.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1273 // if the next token is keyvalue-separator, it means that there are no
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1274 // key. then we need to show error as "empty key is not allowed".
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1275 if(const auto keyval_sep = lex_keyval_sep::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1276 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1277 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1278 msg = format_underline("toml::parse_key_value_pair: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1279 "empty key is not allowed.",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1280 {{source_location(loc), "key expected before '='"}});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1281 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1282 return err(std::move(msg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1283 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1284
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1285 const auto kvsp = lex_keyval_sep::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1286 if(!kvsp)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1287 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1288 std::string msg;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1289 // if the line contains '=' after the invalid sequence, possibly the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1290 // error is in the key (like, invalid character in bare key).
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1291 const auto line_end = std::find(loc.iter(), loc.end(), '\n');
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1292 if(std::find(loc.iter(), line_end, '=') != line_end)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1293 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1294 msg = format_underline("toml::parse_key_value_pair: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1295 "invalid format for key",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1296 {{source_location(loc), "invalid character in key"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1297 {"Did you forget '.' to separate dotted-key?",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1298 "Allowed characters for bare key are [0-9a-zA-Z_-]."});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1299 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1300 else // if not, the error is lack of key-value separator.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1301 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1302 msg = format_underline("toml::parse_key_value_pair: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1303 "missing key-value separator `=`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1304 {{source_location(loc), "should be `=`"}});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1305 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1306 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1307 return err(std::move(msg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1308 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1309
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1310 const auto after_kvsp = loc.iter(); // err msg
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1311 auto val = parse_value<value_type>(loc, n_rec);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1312 if(!val)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1313 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1314 std::string msg;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1315 loc.reset(after_kvsp);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1316 // check there is something not a comment/whitespace after `=`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1317 if(sequence<maybe<lex_ws>, maybe<lex_comment>, lex_newline>::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1318 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1319 loc.reset(after_kvsp);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1320 msg = format_underline("toml::parse_key_value_pair: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1321 "missing value after key-value separator '='",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1322 {{source_location(loc), "expected value, but got nothing"}});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1323 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1324 else // there is something not a comment/whitespace, so invalid format.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1325 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1326 msg = std::move(val.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1327 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1328 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1329 return err(msg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1330 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1331 return ok(std::make_pair(std::move(key_reg.unwrap()),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1332 std::move(val.unwrap())));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1333 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1334
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1335 // for error messages.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1336 template<typename InputIterator>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1337 std::string format_dotted_keys(InputIterator first, const InputIterator last)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1338 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1339 static_assert(std::is_same<key,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1340 typename std::iterator_traits<InputIterator>::value_type>::value,"");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1341
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1342 std::string retval(*first++);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1343 for(; first != last; ++first)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1344 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1345 retval += '.';
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1346 retval += *first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1347 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1348 return retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1349 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1350
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1351 // forward decl for is_valid_forward_table_definition
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1352 result<std::pair<std::vector<key>, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1353 parse_table_key(location& loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1354 result<std::pair<std::vector<key>, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1355 parse_array_table_key(location& loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1356 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1357 result<std::pair<typename Value::table_type, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1358 parse_inline_table(location& loc, const std::size_t n_rec);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1359
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1360 // The following toml file is allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1361 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1362 // [a.b.c] # here, table `a` has element `b`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1363 // foo = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1364 // [a] # merge a = {baz = "qux"} to a = {b = {...}}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1365 // baz = "qux"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1366 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1367 // But the following is not allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1368 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1369 // [a]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1370 // b.c.foo = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1371 // [a] # error! the same table [a] defined!
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1372 // baz = "qux"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1373 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1374 // The following is neither allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1375 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1376 // a = { b.c.foo = "bar"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1377 // [a] # error! the same table [a] defined!
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1378 // baz = "qux"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1379 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1380 // Here, it parses region of `tab->at(k)` as a table key and check the depth
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1381 // of the key. If the key region points deeper node, it would be allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1382 // Otherwise, the key points the same node. It would be rejected.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1383 template<typename Value, typename Iterator>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1384 bool is_valid_forward_table_definition(const Value& fwd, const Value& inserting,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1385 Iterator key_first, Iterator key_curr, Iterator key_last)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1386 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1387 // ------------------------------------------------------------------------
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1388 // check type of the value to be inserted/merged
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1389
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1390 std::string inserting_reg = "";
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1391 if(const auto ptr = detail::get_region(inserting))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1392 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1393 inserting_reg = ptr->str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1394 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1395 location inserting_def("internal", std::move(inserting_reg));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1396 if(const auto inlinetable = parse_inline_table<Value>(inserting_def, 0))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1397 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1398 // check if we are overwriting existing table.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1399 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1400 // # NG
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1401 // a.b = 42
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1402 // a = {d = 3.14}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1403 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1404 // Inserting an inline table to a existing super-table is not allowed in
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1405 // any case. If we found it, we can reject it without further checking.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1406 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1407 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1408
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1409 // Valid and invalid cases when inserting to the [a.b] table:
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1410 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1411 // ## Invalid
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1412 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1413 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1414 // # invalid
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1415 // [a]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1416 // b.c.d = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1417 // [a.b] # a.b is already defined and closed
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1418 // d = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1419 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1420 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1421 // # invalid
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1422 // a = {b.c.d = "foo"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1423 // [a.b] # a is already defined and inline table is closed
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1424 // d = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1425 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1426 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1427 // # invalid
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1428 // a.b.c.d = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1429 // [a.b] # a.b is already defined and dotted-key table is closed
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1430 // d = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1431 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1432 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1433 // ## Valid
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1434 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1435 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1436 // # OK. a.b is defined, but is *overwritable*
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1437 // [a.b.c]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1438 // d = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1439 // [a.b]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1440 // d = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1441 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1442 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1443 // # OK. a.b is defined, but is *overwritable*
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1444 // [a]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1445 // b.c.d = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1446 // b.e = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1447 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1448
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1449 // ------------------------------------------------------------------------
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1450 // check table defined before
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1451
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1452 std::string internal = "";
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1453 if(const auto ptr = detail::get_region(fwd))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1454 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1455 internal = ptr->str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1456 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1457 location def("internal", std::move(internal));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1458 if(const auto tabkeys = parse_table_key(def)) // [table.key]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1459 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1460 // table keys always contains all the nodes from the root.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1461 const auto& tks = tabkeys.unwrap().first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1462 if(std::size_t(std::distance(key_first, key_last)) == tks.size() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1463 std::equal(tks.begin(), tks.end(), key_first))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1464 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1465 // the keys are equivalent. it is not allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1466 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1467 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1468 // the keys are not equivalent. it is allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1469 return true;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1470 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1471 // nested array-of-table definition implicitly defines tables.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1472 // those tables can be reopened.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1473 if(const auto atabkeys = parse_array_table_key(def))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1474 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1475 // table keys always contains all the nodes from the root.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1476 const auto& tks = atabkeys.unwrap().first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1477 if(std::size_t(std::distance(key_first, key_last)) == tks.size() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1478 std::equal(tks.begin(), tks.end(), key_first))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1479 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1480 // the keys are equivalent. it is not allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1481 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1482 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1483 // the keys are not equivalent. it is allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1484 return true;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1485 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1486 if(const auto dotkeys = parse_key(def)) // a.b.c = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1487 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1488 // consider the following case.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1489 // [a]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1490 // b.c = {d = 42}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1491 // [a.b.c]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1492 // e = 2.71
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1493 // this defines the table [a.b.c] twice. no?
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1494 if(const auto reopening_dotkey_by_table = parse_table_key(inserting_def))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1495 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1496 // re-opening a dotkey-defined table by a table is invalid.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1497 // only dotkey can append a key-val. Like:
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1498 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1499 // a.b.c = "foo"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1500 // a.b.d = "bar" # OK. reopen `a.b` by dotkey
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1501 // [a.b]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1502 // e = "bar" # Invalid. re-opening `a.b` by [a.b] is not allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1503 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1504 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1505 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1506
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1507 // a dotted key starts from the node representing a table in which the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1508 // dotted key belongs to.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1509 const auto& dks = dotkeys.unwrap().first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1510 if(std::size_t(std::distance(key_curr, key_last)) == dks.size() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1511 std::equal(dks.begin(), dks.end(), key_curr))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1512 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1513 // the keys are equivalent. it is not allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1514 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1515 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1516 // the keys are not equivalent. it is allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1517 return true;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1518 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1519 return false;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1520 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1521
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1522 template<typename Value, typename InputIterator>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1523 result<bool, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1524 insert_nested_key(typename Value::table_type& root, const Value& v,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1525 InputIterator iter, const InputIterator last,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1526 region key_reg,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1527 const bool is_array_of_table = false)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1528 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1529 static_assert(std::is_same<key,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1530 typename std::iterator_traits<InputIterator>::value_type>::value,"");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1531
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1532 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1533 using table_type = typename value_type::table_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1534 using array_type = typename value_type::array_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1535
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1536 const auto first = iter;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1537 assert(iter != last);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1538
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1539 table_type* tab = std::addressof(root);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1540 for(; iter != last; ++iter) // search recursively
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1541 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1542 const key& k = *iter;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1543 if(std::next(iter) == last) // k is the last key
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1544 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1545 // XXX if the value is array-of-tables, there can be several
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1546 // tables that are in the same array. in that case, we need to
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1547 // find the last element and insert it to there.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1548 if(is_array_of_table)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1549 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1550 if(tab->count(k) == 1) // there is already an array of table
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1551 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1552 if(tab->at(k).is_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1553 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1554 // show special err msg for conflicting table
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1555 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1556 "toml::insert_value: array of table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1557 format_dotted_keys(first, last),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1558 "\") cannot be defined"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1559 {tab->at(k).location(), "table already defined"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1560 {v.location(), "this conflicts with the previous table"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1561 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1562 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1563 else if(!(tab->at(k).is_array()))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1564 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1565 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1566 "toml::insert_value: array of table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1567 format_dotted_keys(first, last), "\") collides with"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1568 " existing value"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1569 {tab->at(k).location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1570 concat_to_string("this ", tab->at(k).type(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1571 " value already exists")},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1572 {v.location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1573 "while inserting this array-of-tables"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1574 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1575 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1576 // the above if-else-if checks tab->at(k) is an array
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1577 auto& a = tab->at(k).as_array();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1578 // If table element is defined as [[array_of_tables]], it
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1579 // cannot be an empty array. If an array of tables is
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1580 // defined as `aot = []`, it cannot be appended.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1581 if(a.empty() || !(a.front().is_table()))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1582 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1583 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1584 "toml::insert_value: array of table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1585 format_dotted_keys(first, last), "\") collides with"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1586 " existing value"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1587 {tab->at(k).location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1588 concat_to_string("this ", tab->at(k).type(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1589 " value already exists")},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1590 {v.location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1591 "while inserting this array-of-tables"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1592 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1593 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1594 // avoid conflicting array of table like the following.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1595 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1596 // a = [{b = 42}] # define a as an array of *inline* tables
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1597 // [[a]] # a is an array of *multi-line* tables
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1598 // b = 54
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1599 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1600 // Here, from the type information, these cannot be detected
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1601 // because inline table is also a table.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1602 // But toml v0.5.0 explicitly says it is invalid. The above
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1603 // array-of-tables has a static size and appending to the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1604 // array is invalid.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1605 // In this library, multi-line table value has a region
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1606 // that points to the key of the table (e.g. [[a]]). By
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1607 // comparing the first two letters in key, we can detect
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1608 // the array-of-table is inline or multiline.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1609 if(const auto ptr = detail::get_region(a.front()))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1610 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1611 if(ptr->str().substr(0,2) != "[[")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1612 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1613 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1614 "toml::insert_value: array of table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1615 format_dotted_keys(first, last), "\") collides "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1616 "with existing array-of-tables"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1617 {tab->at(k).location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1618 concat_to_string("this ", tab->at(k).type(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1619 " value has static size")},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1620 {v.location(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1621 "appending it to the statically sized array"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1622 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1623 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1624 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1625 a.push_back(v);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1626 return ok(true);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1627 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1628 else // if not, we need to create the array of table
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1629 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1630 // XXX: Consider the following array of tables.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1631 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1632 // # This is a comment.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1633 // [[aot]]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1634 // foo = "bar"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1635 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1636 // Here, the comment is for `aot`. But here, actually two
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1637 // values are defined. An array that contains tables, named
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1638 // `aot`, and the 0th element of the `aot`, `{foo = "bar"}`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1639 // Those two are different from each other. But both of them
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1640 // points to the same portion of the TOML file, `[[aot]]`,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1641 // so `key_reg.comments()` returns `# This is a comment`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1642 // If it is assigned as a comment of `aot` defined here, the
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1643 // comment will be duplicated. Both the `aot` itself and
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1644 // the 0-th element will have the same comment. This causes
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1645 // "duplication of the same comments" bug when the data is
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1646 // serialized.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1647 // Next, consider the following.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1648 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1649 // # comment 1
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1650 // aot = [
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1651 // # comment 2
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1652 // {foo = "bar"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1653 // ]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1654 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1655 // In this case, we can distinguish those two comments. So
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1656 // here we need to add "comment 1" to the `aot` and
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1657 // "comment 2" to the 0th element of that.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1658 // To distinguish those two, we check the key region.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1659 std::vector<std::string> comments{/* empty by default */};
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1660 if(key_reg.str().substr(0, 2) != "[[")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1661 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1662 comments = key_reg.comments();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1663 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1664 value_type aot(array_type(1, v), key_reg, std::move(comments));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1665 tab->insert(std::make_pair(k, aot));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1666 return ok(true);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1667 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1668 } // end if(array of table)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1669
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1670 if(tab->count(k) == 1)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1671 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1672 if(tab->at(k).is_table() && v.is_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1673 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1674 if(!is_valid_forward_table_definition(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1675 tab->at(k), v, first, iter, last))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1676 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1677 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1678 "toml::insert_value: table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1679 format_dotted_keys(first, last),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1680 "\") already exists."), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1681 {tab->at(k).location(), "table already exists here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1682 {v.location(), "table defined twice"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1683 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1684 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1685 // to allow the following toml file.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1686 // [a.b.c]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1687 // d = 42
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1688 // [a]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1689 // e = 2.71
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1690 auto& t = tab->at(k).as_table();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1691 for(const auto& kv : v.as_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1692 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1693 if(tab->at(k).contains(kv.first))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1694 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1695 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1696 "toml::insert_value: value (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1697 format_dotted_keys(first, last),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1698 "\") already exists."), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1699 {t.at(kv.first).location(), "already exists here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1700 {v.location(), "this defined twice"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1701 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1702 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1703 t[kv.first] = kv.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1704 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1705 detail::change_region(tab->at(k), key_reg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1706 return ok(true);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1707 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1708 else if(v.is_table() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1709 tab->at(k).is_array() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1710 tab->at(k).as_array().size() > 0 &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1711 tab->at(k).as_array().front().is_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1712 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1713 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1714 "toml::insert_value: array of tables (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1715 format_dotted_keys(first, last), "\") already exists."), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1716 {tab->at(k).location(), "array of tables defined here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1717 {v.location(), "table conflicts with the previous array of table"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1718 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1719 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1720 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1721 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1722 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1723 "toml::insert_value: value (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1724 format_dotted_keys(first, last), "\") already exists."), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1725 {tab->at(k).location(), "value already exists here"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1726 {v.location(), "value defined twice"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1727 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1728 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1729 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1730 tab->insert(std::make_pair(k, v));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1731 return ok(true);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1732 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1733 else // k is not the last one, we should insert recursively
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1734 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1735 // if there is no corresponding value, insert it first.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1736 // related: you don't need to write
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1737 // # [x]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1738 // # [x.y]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1739 // to write
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1740 // [x.y.z]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1741 if(tab->count(k) == 0)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1742 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1743 // a table that is defined implicitly doesn't have any comments.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1744 (*tab)[k] = value_type(table_type{}, key_reg, {/*no comment*/});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1745 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1746
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1747 // type checking...
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1748 if(tab->at(k).is_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1749 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1750 // According to toml-lang/toml:36d3091b3 "Clarify that inline
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1751 // tables are immutable", check if it adds key-value pair to an
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1752 // inline table.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1753 if(const auto* ptr = get_region(tab->at(k)))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1754 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1755 // here, if the value is a (multi-line) table, the region
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1756 // should be something like `[table-name]`.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1757 if(ptr->front() == '{')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1758 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1759 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1760 "toml::insert_value: inserting to an inline table (",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1761 format_dotted_keys(first, std::next(iter)),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1762 ") but inline tables are immutable"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1763 {tab->at(k).location(), "inline tables are immutable"},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1764 {v.location(), "inserting this"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1765 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1766 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1767 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1768 tab = std::addressof((*tab)[k].as_table());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1769 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1770 else if(tab->at(k).is_array()) // inserting to array-of-tables?
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1771 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1772 auto& a = (*tab)[k].as_array();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1773 if(!a.back().is_table())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1774 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1775 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1776 "toml::insert_value: target (",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1777 format_dotted_keys(first, std::next(iter)),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1778 ") is neither table nor an array of tables"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1779 {a.back().location(), concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1780 "actual type is ", a.back().type())},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1781 {v.location(), "inserting this"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1782 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1783 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1784 if(a.empty())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1785 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1786 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1787 "toml::insert_value: table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1788 format_dotted_keys(first, last), "\") conflicts with"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1789 " existing value"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1790 {tab->at(k).location(), std::string("this array is not insertable")},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1791 {v.location(), std::string("appending it to the statically sized array")}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1792 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1793 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1794 if(const auto ptr = detail::get_region(a.at(0)))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1795 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1796 if(ptr->str().substr(0,2) != "[[")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1797 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1798 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1799 "toml::insert_value: a table (\"",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1800 format_dotted_keys(first, last), "\") cannot be "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1801 "inserted to an existing inline array-of-tables"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1802 {tab->at(k).location(), std::string("this array of table has a static size")},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1803 {v.location(), std::string("appending it to the statically sized array")}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1804 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1805 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1806 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1807 tab = std::addressof(a.back().as_table());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1808 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1809 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1810 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1811 throw syntax_error(format_underline(concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1812 "toml::insert_value: target (",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1813 format_dotted_keys(first, std::next(iter)),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1814 ") is neither table nor an array of tables"), {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1815 {tab->at(k).location(), concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1816 "actual type is ", tab->at(k).type())},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1817 {v.location(), "inserting this"}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1818 }), v.location());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1819 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1820 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1821 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1822 return err(std::string("toml::detail::insert_nested_key: never reach here"));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1823 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1824
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1825 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1826 result<std::pair<typename Value::table_type, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1827 parse_inline_table(location& loc, const std::size_t n_rec)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1828 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1829 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1830 using table_type = typename value_type::table_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1831
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1832 if(n_rec > TOML11_VALUE_RECURSION_LIMIT)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1833 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1834 throw syntax_error(std::string("toml::parse_inline_table: recursion limit ("
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1835 TOML11_STRINGIZE(TOML11_VALUE_RECURSION_LIMIT) ") exceeded"),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1836 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1837 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1838
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1839 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1840 table_type retval;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1841 if(!(loc.iter() != loc.end() && *loc.iter() == '{'))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1842 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1843 return err(format_underline("toml::parse_inline_table: ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1844 {{source_location(loc), "the next token is not an inline table"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1845 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1846 loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1847
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1848 // check if the inline table is an empty table = { }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1849 maybe<lex_ws>::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1850 if(loc.iter() != loc.end() && *loc.iter() == '}')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1851 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1852 loc.advance(); // skip `}`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1853 return ok(std::make_pair(retval, region(loc, first, loc.iter())));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1854 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1855
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1856 // it starts from "{". it should be formatted as inline-table
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1857 while(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1858 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1859 const auto kv_r = parse_key_value_pair<value_type>(loc, n_rec+1);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1860 if(!kv_r)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1861 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1862 return err(kv_r.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1863 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1864
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1865 const auto& kvpair = kv_r.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1866 const std::vector<key>& keys = kvpair.first.first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1867 const auto& key_reg = kvpair.first.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1868 const value_type& val = kvpair.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1869
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1870 const auto inserted =
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1871 insert_nested_key(retval, val, keys.begin(), keys.end(), key_reg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1872 if(!inserted)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1873 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1874 throw internal_error("toml::parse_inline_table: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1875 "failed to insert value into table: " + inserted.unwrap_err(),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1876 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1877 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1878
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1879 using lex_table_separator = sequence<maybe<lex_ws>, character<','>>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1880 const auto sp = lex_table_separator::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1881
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1882 if(!sp)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1883 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1884 maybe<lex_ws>::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1885
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1886 if(loc.iter() == loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1887 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1888 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1889 "toml::parse_inline_table: missing table separator `}` ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1890 {{source_location(loc), "should be `}`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1891 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1892 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1893 else if(*loc.iter() == '}')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1894 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1895 loc.advance(); // skip `}`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1896 return ok(std::make_pair(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1897 retval, region(loc, first, loc.iter())));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1898 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1899 else if(*loc.iter() == '#' || *loc.iter() == '\r' || *loc.iter() == '\n')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1900 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1901 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1902 "toml::parse_inline_table: missing curly brace `}`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1903 {{source_location(loc), "should be `}`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1904 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1905 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1906 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1907 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1908 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1909 "toml::parse_inline_table: missing table separator `,` ",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1910 {{source_location(loc), "should be `,`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1911 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1912 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1913 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1914 else // `,` is found
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1915 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1916 maybe<lex_ws>::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1917 if(loc.iter() != loc.end() && *loc.iter() == '}')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1918 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1919 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1920 "toml::parse_inline_table: trailing comma is not allowed in"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1921 " an inline table",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1922 {{source_location(loc), "should be `}`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1923 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1924 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1925 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1926 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1927 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1928 throw syntax_error(format_underline("toml::parse_inline_table: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1929 "inline table did not closed by `}`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1930 {{source_location(loc), "should be closed"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1931 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1932 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1933
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1934 inline result<value_t, std::string> guess_number_type(const location& l)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1935 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1936 // This function tries to find some (common) mistakes by checking characters
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1937 // that follows the last character of a value. But it is often difficult
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1938 // because some non-newline characters can appear after a value. E.g.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1939 // spaces, tabs, commas (in an array or inline table), closing brackets
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1940 // (of an array or inline table), comment-sign (#). Since this function
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1941 // does not parse further, those characters are always allowed to be there.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1942 location loc = l;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1943
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1944 if(lex_offset_date_time::invoke(loc)) {return ok(value_t::offset_datetime);}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1945 loc.reset(l.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1946
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1947 if(lex_local_date_time::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1948 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1949 // bad offset may appear after this.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1950 if(loc.iter() != loc.end() && (*loc.iter() == '+' || *loc.iter() == '-'
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1951 || *loc.iter() == 'Z' || *loc.iter() == 'z'))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1952 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1953 return err(format_underline("bad offset: should be [+-]HH:MM or Z",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1954 {{source_location(loc), "[+-]HH:MM or Z"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1955 {"pass: +09:00, -05:30", "fail: +9:00, -5:30"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1956 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1957 return ok(value_t::local_datetime);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1958 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1959 loc.reset(l.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1960
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1961 if(lex_local_date::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1962 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1963 // bad time may appear after this.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1964 // A space is allowed as a delimiter between local time. But there are
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1965 // both cases in which a space becomes valid or invalid.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1966 // - invalid: 2019-06-16 7:00:00
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1967 // - valid : 2019-06-16 07:00:00
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1968 if(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1969 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1970 const auto c = *loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1971 if(c == 'T' || c == 't')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1972 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1973 return err(format_underline("bad time: should be HH:MM:SS.subsec",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1974 {{source_location(loc), "HH:MM:SS.subsec"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1975 {"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1976 "fail: 1979-05-27T7:32:00, 1979-05-27 17:32"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1977 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1978 if('0' <= c && c <= '9')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1979 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1980 return err(format_underline("bad time: missing T",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1981 {{source_location(loc), "T or space required here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1982 {"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1983 "fail: 1979-05-27T7:32:00, 1979-05-27 7:32"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1984 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1985 if(c == ' ' && std::next(loc.iter()) != loc.end() &&
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1986 ('0' <= *std::next(loc.iter()) && *std::next(loc.iter())<= '9'))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1987 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1988 loc.advance();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1989 return err(format_underline("bad time: should be HH:MM:SS.subsec",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1990 {{source_location(loc), "HH:MM:SS.subsec"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1991 {"pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1992 "fail: 1979-05-27T7:32:00, 1979-05-27 7:32"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1993 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1994 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1995 return ok(value_t::local_date);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1996 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1997 loc.reset(l.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1998
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1999 if(lex_local_time::invoke(loc)) {return ok(value_t::local_time);}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2000 loc.reset(l.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2001
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2002 if(lex_float::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2003 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2004 if(loc.iter() != loc.end() && *loc.iter() == '_')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2005 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2006 return err(format_underline("bad float: `_` should be surrounded by digits",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2007 {{source_location(loc), "here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2008 {"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2009 "fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2010 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2011 return ok(value_t::floating);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2012 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2013 loc.reset(l.iter());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2014
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2015 if(lex_integer::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2016 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2017 if(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2018 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2019 const auto c = *loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2020 if(c == '_')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2021 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2022 return err(format_underline("bad integer: `_` should be surrounded by digits",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2023 {{source_location(loc), "here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2024 {"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2025 "fail: 1__000, 0123"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2026 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2027 if('0' <= c && c <= '9')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2028 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2029 // leading zero. point '0'
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2030 loc.retrace();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2031 return err(format_underline("bad integer: leading zero",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2032 {{source_location(loc), "here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2033 {"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2034 "fail: 1__000, 0123"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2035 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2036 if(c == ':' || c == '-')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2037 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2038 return err(format_underline("bad datetime: invalid format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2039 {{source_location(loc), "here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2040 {"pass: 1979-05-27T07:32:00-07:00, 1979-05-27 07:32:00.999999Z",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2041 "fail: 1979-05-27T7:32:00-7:00, 1979-05-27 7:32-00:30"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2042 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2043 if(c == '.' || c == 'e' || c == 'E')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2044 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2045 return err(format_underline("bad float: invalid format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2046 {{source_location(loc), "here"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2047 {"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2048 "fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2049 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2050 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2051 return ok(value_t::integer);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2052 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2053 if(loc.iter() != loc.end() && *loc.iter() == '.')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2054 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2055 return err(format_underline("bad float: invalid format",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2056 {{source_location(loc), "integer part required before this"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2057 {"pass: +1.0, -2e-2, 3.141_592_653_589, inf, nan",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2058 "fail: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2059 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2060 if(loc.iter() != loc.end() && *loc.iter() == '_')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2061 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2062 return err(format_underline("bad number: `_` should be surrounded by digits",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2063 {{source_location(loc), "`_` is not surrounded by digits"}},
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2064 {"pass: -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2065 "fail: 1__000, 0123"}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2066 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2067 return err(format_underline("bad format: unknown value appeared",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2068 {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2069 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2070
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2071 inline result<value_t, std::string> guess_value_type(const location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2072 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2073 switch(*loc.iter())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2074 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2075 case '"' : {return ok(value_t::string); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2076 case '\'': {return ok(value_t::string); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2077 case 't' : {return ok(value_t::boolean); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2078 case 'f' : {return ok(value_t::boolean); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2079 case '[' : {return ok(value_t::array); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2080 case '{' : {return ok(value_t::table); }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2081 case 'i' : {return ok(value_t::floating);} // inf.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2082 case 'n' : {return ok(value_t::floating);} // nan.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2083 default : {return guess_number_type(loc);}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2084 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2085 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2086
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2087 template<typename Value, typename T>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2088 result<Value, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2089 parse_value_helper(result<std::pair<T, region>, std::string> rslt)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2090 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2091 if(rslt.is_ok())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2092 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2093 auto comments = rslt.as_ok().second.comments();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2094 return ok(Value(std::move(rslt.as_ok()), std::move(comments)));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2095 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2096 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2097 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2098 return err(std::move(rslt.as_err()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2099 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2100 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2101
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2102 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2103 result<Value, std::string> parse_value(location& loc, const std::size_t n_rec)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2104 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2105 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2106 if(first == loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2107 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2108 return err(format_underline("toml::parse_value: input is empty",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2109 {{source_location(loc), ""}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2110 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2111
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2112 const auto type = guess_value_type(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2113 if(!type)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2114 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2115 return err(type.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2116 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2117
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2118 switch(type.unwrap())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2119 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2120 case value_t::boolean : {return parse_value_helper<Value>(parse_boolean(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2121 case value_t::integer : {return parse_value_helper<Value>(parse_integer(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2122 case value_t::floating : {return parse_value_helper<Value>(parse_floating(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2123 case value_t::string : {return parse_value_helper<Value>(parse_string(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2124 case value_t::offset_datetime: {return parse_value_helper<Value>(parse_offset_datetime(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2125 case value_t::local_datetime : {return parse_value_helper<Value>(parse_local_datetime(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2126 case value_t::local_date : {return parse_value_helper<Value>(parse_local_date(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2127 case value_t::local_time : {return parse_value_helper<Value>(parse_local_time(loc) );}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2128 case value_t::array : {return parse_value_helper<Value>(parse_array<Value>(loc, n_rec));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2129 case value_t::table : {return parse_value_helper<Value>(parse_inline_table<Value>(loc, n_rec));}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2130 default:
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2131 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2132 const auto msg = format_underline("toml::parse_value: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2133 "unknown token appeared", {{source_location(loc), "unknown"}});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2134 loc.reset(first);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2135 return err(msg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2136 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2137 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2138 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2139
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2140 inline result<std::pair<std::vector<key>, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2141 parse_table_key(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2142 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2143 if(auto token = lex_std_table::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2144 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2145 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2146
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2147 const auto open = lex_std_table_open::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2148 if(!open || inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2149 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2150 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2151 "toml::parse_table_key: no `[`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2152 {{source_location(inner_loc), "should be `[`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2153 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2154 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2155 // to skip [ a . b . c ]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2156 // ^----------- this whitespace
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2157 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2158 const auto keys = parse_key(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2159 if(!keys)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2160 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2161 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2162 "toml::parse_table_key: invalid key",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2163 {{source_location(inner_loc), "not key"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2164 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2165 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2166 // to skip [ a . b . c ]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2167 // ^-- this whitespace
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2168 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2169 const auto close = lex_std_table_close::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2170 if(!close)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2171 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2172 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2173 "toml::parse_table_key: no `]`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2174 {{source_location(inner_loc), "should be `]`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2175 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2176 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2177
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2178 // after [table.key], newline or EOF(empty table) required.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2179 if(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2180 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2181 using lex_newline_after_table_key =
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2182 sequence<maybe<lex_ws>, maybe<lex_comment>, lex_newline>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2183 const auto nl = lex_newline_after_table_key::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2184 if(!nl)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2185 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2186 throw syntax_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2187 "toml::parse_table_key: newline required after [table.key]",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2188 {{source_location(loc), "expected newline"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2189 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2190 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2191 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2192 return ok(std::make_pair(keys.unwrap().first, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2193 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2194 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2195 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2196 return err(format_underline("toml::parse_table_key: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2197 "not a valid table key", {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2198 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2199 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2200
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2201 inline result<std::pair<std::vector<key>, region>, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2202 parse_array_table_key(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2203 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2204 if(auto token = lex_array_table::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2205 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2206 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2207
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2208 const auto open = lex_array_table_open::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2209 if(!open || inner_loc.iter() == inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2210 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2211 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2212 "toml::parse_array_table_key: no `[[`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2213 {{source_location(inner_loc), "should be `[[`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2214 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2215 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2216 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2217 const auto keys = parse_key(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2218 if(!keys)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2219 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2220 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2221 "toml::parse_array_table_key: invalid key",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2222 {{source_location(inner_loc), "not a key"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2223 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2224 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2225 lex_ws::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2226 const auto close = lex_array_table_close::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2227 if(!close)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2228 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2229 throw internal_error(format_underline(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2230 "toml::parse_array_table_key: no `]]`",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2231 {{source_location(inner_loc), "should be `]]`"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2232 source_location(inner_loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2233 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2234
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2235 // after [[table.key]], newline or EOF(empty table) required.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2236 if(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2237 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2238 using lex_newline_after_table_key =
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2239 sequence<maybe<lex_ws>, maybe<lex_comment>, lex_newline>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2240 const auto nl = lex_newline_after_table_key::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2241 if(!nl)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2242 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2243 throw syntax_error(format_underline("toml::"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2244 "parse_array_table_key: newline required after [[table.key]]",
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2245 {{source_location(loc), "expected newline"}}),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2246 source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2247 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2248 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2249 return ok(std::make_pair(keys.unwrap().first, token.unwrap()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2250 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2251 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2252 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2253 return err(format_underline("toml::parse_array_table_key: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2254 "not a valid table key", {{source_location(loc), "here"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2255 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2256 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2257
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2258 // parse table body (key-value pairs until the iter hits the next [tablekey])
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2259 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2260 result<typename Value::table_type, std::string>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2261 parse_ml_table(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2262 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2263 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2264 using table_type = typename value_type::table_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2265
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2266 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2267 if(first == loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2268 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2269 return ok(table_type{});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2270 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2271
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2272 // XXX at lest one newline is needed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2273 using skip_line = repeat<
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2274 sequence<maybe<lex_ws>, maybe<lex_comment>, lex_newline>, at_least<1>>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2275 skip_line::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2276 lex_ws::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2277
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2278 table_type tab;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2279 while(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2280 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2281 lex_ws::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2282 const auto before = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2283 if(const auto tmp = parse_array_table_key(loc)) // next table found
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2284 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2285 loc.reset(before);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2286 return ok(tab);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2287 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2288 if(const auto tmp = parse_table_key(loc)) // next table found
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2289 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2290 loc.reset(before);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2291 return ok(tab);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2292 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2293
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2294 if(const auto kv = parse_key_value_pair<value_type>(loc, 0))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2295 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2296 const auto& kvpair = kv.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2297 const std::vector<key>& keys = kvpair.first.first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2298 const auto& key_reg = kvpair.first.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2299 const value_type& val = kvpair.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2300 const auto inserted =
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2301 insert_nested_key(tab, val, keys.begin(), keys.end(), key_reg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2302 if(!inserted)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2303 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2304 return err(inserted.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2305 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2306 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2307 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2308 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2309 return err(kv.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2310 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2311
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2312 // comment lines are skipped by the above function call.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2313 // However, since the `skip_line` requires at least 1 newline, it fails
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2314 // if the file ends with ws and/or comment without newline.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2315 // `skip_line` matches `ws? + comment? + newline`, not `ws` or `comment`
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2316 // itself. To skip the last ws and/or comment, call lexers.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2317 // It does not matter if these fails, so the return value is discarded.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2318 lex_ws::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2319 lex_comment::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2320
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2321 // skip_line is (whitespace? comment? newline)_{1,}. multiple empty lines
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2322 // and comments after the last key-value pairs are allowed.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2323 const auto newline = skip_line::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2324 if(!newline && loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2325 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2326 const auto before2 = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2327 lex_ws::invoke(loc); // skip whitespace
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2328 const auto msg = format_underline("toml::parse_table: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2329 "invalid line format", {{source_location(loc), concat_to_string(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2330 "expected newline, but got '", show_char(*loc.iter()), "'.")}});
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2331 loc.reset(before2);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2332 return err(msg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2333 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2334
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2335 // the skip_lines only matches with lines that includes newline.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2336 // to skip the last line that includes comment and/or whitespace
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2337 // but no newline, call them one more time.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2338 lex_ws::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2339 lex_comment::invoke(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2340 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2341 return ok(tab);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2342 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2343
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2344 template<typename Value>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2345 result<Value, std::string> parse_toml_file(location& loc)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2346 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2347 using value_type = Value;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2348 using table_type = typename value_type::table_type;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2349
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2350 const auto first = loc.iter();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2351 if(first == loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2352 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2353 // For empty files, return an empty table with an empty region (zero-length).
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2354 // Without the region, error messages would miss the filename.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2355 return ok(value_type(table_type{}, region(loc, first, first), {}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2356 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2357
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2358 // put the first line as a region of a file
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2359 // Here first != loc.end(), so taking std::next is okay
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2360 const region file(loc, first, std::next(loc.iter()));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2361
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2362 // The first successive comments that are separated from the first value
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2363 // by an empty line are for a file itself.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2364 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2365 // # this is a comment for a file.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2366 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2367 // key = "the first value"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2368 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2369 // ```toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2370 // # this is a comment for "the first value".
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2371 // key = "the first value"
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2372 // ```
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2373 std::vector<std::string> comments;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2374 using lex_first_comments = sequence<
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2375 repeat<sequence<maybe<lex_ws>, lex_comment, lex_newline>, at_least<1>>,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2376 sequence<maybe<lex_ws>, lex_newline>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2377 >;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2378 if(const auto token = lex_first_comments::invoke(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2379 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2380 location inner_loc(loc.name(), token.unwrap().str());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2381 while(inner_loc.iter() != inner_loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2382 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2383 maybe<lex_ws>::invoke(inner_loc); // remove ws if exists
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2384 if(lex_newline::invoke(inner_loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2385 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2386 assert(inner_loc.iter() == inner_loc.end());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2387 break; // empty line found.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2388 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2389 auto com = lex_comment::invoke(inner_loc).unwrap().str();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2390 com.erase(com.begin()); // remove # sign
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2391 comments.push_back(std::move(com));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2392 lex_newline::invoke(inner_loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2393 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2394 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2395
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2396 table_type data;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2397 // root object is also a table, but without [tablename]
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2398 if(const auto tab = parse_ml_table<value_type>(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2399 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2400 data = std::move(tab.unwrap());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2401 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2402 else // failed (empty table is regarded as success in parse_ml_table)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2403 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2404 return err(tab.unwrap_err());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2405 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2406 while(loc.iter() != loc.end())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2407 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2408 // here, the region of [table] is regarded as the table-key because
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2409 // the table body is normally too big and it is not so informative
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2410 // if the first key-value pair of the table is shown in the error
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2411 // message.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2412 if(const auto tabkey = parse_array_table_key(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2413 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2414 const auto tab = parse_ml_table<value_type>(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2415 if(!tab){return err(tab.unwrap_err());}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2416
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2417 const auto& tk = tabkey.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2418 const auto& keys = tk.first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2419 const auto& reg = tk.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2420
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2421 const auto inserted = insert_nested_key(data,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2422 value_type(tab.unwrap(), reg, reg.comments()),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2423 keys.begin(), keys.end(), reg,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2424 /*is_array_of_table=*/ true);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2425 if(!inserted) {return err(inserted.unwrap_err());}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2426
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2427 continue;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2428 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2429 if(const auto tabkey = parse_table_key(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2430 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2431 const auto tab = parse_ml_table<value_type>(loc);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2432 if(!tab){return err(tab.unwrap_err());}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2433
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2434 const auto& tk = tabkey.unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2435 const auto& keys = tk.first;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2436 const auto& reg = tk.second;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2437
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2438 const auto inserted = insert_nested_key(data,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2439 value_type(tab.unwrap(), reg, reg.comments()),
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2440 keys.begin(), keys.end(), reg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2441 if(!inserted) {return err(inserted.unwrap_err());}
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2442
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2443 continue;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2444 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2445 return err(format_underline("toml::parse_toml_file: "
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2446 "unknown line appeared", {{source_location(loc), "unknown format"}}));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2447 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2448
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2449 return ok(Value(std::move(data), file, comments));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2450 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2451
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2452 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2453 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2454 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2455 basic_value<Comment, Table, Array>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2456 parse(std::vector<char>& letters, const std::string& fname)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2457 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2458 using value_type = basic_value<Comment, Table, Array>;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2459
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2460 // append LF.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2461 // Although TOML does not require LF at the EOF, to make parsing logic
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2462 // simpler, we "normalize" the content by adding LF if it does not exist.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2463 // It also checks if the last char is CR, to avoid changing the meaning.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2464 // This is not the *best* way to deal with the last character, but is a
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2465 // simple and quick fix.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2466 if(!letters.empty() && letters.back() != '\n' && letters.back() != '\r')
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2467 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2468 letters.push_back('\n');
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2469 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2470
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2471 detail::location loc(std::move(fname), std::move(letters));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2472
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2473 // skip BOM if exists.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2474 // XXX component of BOM (like 0xEF) exceeds the representable range of
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2475 // signed char, so on some (actually, most) of the environment, these cannot
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2476 // be compared to char. However, since we are always out of luck, we need to
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2477 // check our chars are equivalent to BOM. To do this, first we need to
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2478 // convert char to unsigned char to guarantee the comparability.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2479 if(loc.source()->size() >= 3)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2480 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2481 std::array<unsigned char, 3> BOM;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2482 std::memcpy(BOM.data(), loc.source()->data(), 3);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2483 if(BOM[0] == 0xEF && BOM[1] == 0xBB && BOM[2] == 0xBF)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2484 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2485 loc.advance(3); // BOM found. skip.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2486 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2487 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2488
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2489 if (auto data = detail::parse_toml_file<value_type>(loc))
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2490 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2491 return std::move(data).unwrap();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2492 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2493 else
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2494 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2495 throw syntax_error(std::move(data).unwrap_err(), source_location(loc));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2496 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2497 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2498
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2499 } // detail
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2500
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2501 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2502 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2503 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2504 basic_value<Comment, Table, Array>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2505 parse(FILE * file, const std::string& fname)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2506 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2507 const long beg = std::ftell(file);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2508 if (beg == -1l)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2509 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2510 throw file_io_error(errno, "Failed to access", fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2511 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2512
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2513 const int res_seekend = std::fseek(file, 0, SEEK_END);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2514 if (res_seekend != 0)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2515 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2516 throw file_io_error(errno, "Failed to seek", fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2517 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2518
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2519 const long end = std::ftell(file);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2520 if (end == -1l)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2521 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2522 throw file_io_error(errno, "Failed to access", fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2523 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2524
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2525 const auto fsize = end - beg;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2526
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2527 const auto res_seekbeg = std::fseek(file, beg, SEEK_SET);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2528 if (res_seekbeg != 0)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2529 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2530 throw file_io_error(errno, "Failed to seek", fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2531 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2532
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2533 // read whole file as a sequence of char
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2534 assert(fsize >= 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2535 std::vector<char> letters(static_cast<std::size_t>(fsize));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2536 std::fread(letters.data(), sizeof(char), static_cast<std::size_t>(fsize), file);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2537
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2538 return detail::parse<Comment, Table, Array>(letters, fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2539 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2540
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2541 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2542 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2543 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2544 basic_value<Comment, Table, Array>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2545 parse(std::istream& is, std::string fname = "unknown file")
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2546 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2547 const auto beg = is.tellg();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2548 is.seekg(0, std::ios::end);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2549 const auto end = is.tellg();
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2550 const auto fsize = end - beg;
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2551 is.seekg(beg);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2552
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2553 // read whole file as a sequence of char
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2554 assert(fsize >= 0);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2555 std::vector<char> letters(static_cast<std::size_t>(fsize));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2556 is.read(letters.data(), fsize);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2557
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2558 return detail::parse<Comment, Table, Array>(letters, fname);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2559 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2560
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2561 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2562 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2563 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2564 basic_value<Comment, Table, Array> parse(std::string fname)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2565 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2566 std::ifstream ifs(fname, std::ios_base::binary);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2567 if(!ifs.good())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2568 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2569 throw std::ios_base::failure(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2570 "toml::parse: Error opening file \"" + fname + "\"");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2571 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2572 ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2573 return parse<Comment, Table, Array>(ifs, std::move(fname));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2574 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2575
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2576 #ifdef TOML11_HAS_STD_FILESYSTEM
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2577 // This function just forwards `parse("filename.toml")` to std::string version
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2578 // to avoid the ambiguity in overload resolution.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2579 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2580 // Both std::string and std::filesystem::path are convertible from const char*.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2581 // Without this, both parse(std::string) and parse(std::filesystem::path)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2582 // matches to parse("filename.toml"). This breaks the existing code.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2583 //
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2584 // This function exactly matches to the invocation with c-string.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2585 // So this function is preferred than others and the ambiguity disappears.
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2586 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2587 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2588 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2589 basic_value<Comment, Table, Array> parse(const char* fname)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2590 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2591 return parse<Comment, Table, Array>(std::string(fname));
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2592 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2593
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2594 template<typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2595 template<typename ...> class Table = std::unordered_map,
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2596 template<typename ...> class Array = std::vector>
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2597 basic_value<Comment, Table, Array> parse(const std::filesystem::path& fpath)
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2598 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2599 std::ifstream ifs(fpath, std::ios_base::binary);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2600 if(!ifs.good())
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2601 {
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2602 throw std::ios_base::failure(
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2603 "toml::parse: Error opening file \"" + fpath.string() + "\"");
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2604 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2605 ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2606 return parse<Comment, Table, Array>(ifs, fpath.string());
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2607 }
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2608 #endif // TOML11_HAS_STD_FILESYSTEM
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2609
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2610 } // toml
3b355fa948c7 config: use TOML instead of INI
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2611 #endif// TOML11_PARSER_HPP