-
-struct ParserError : public std::runtime_error
-{
- ParserError(const std::string& message)
- : runtime_error("Config is not valid. " + message),
- position(0),
- error(message)
- {}
- ParserError(size_t pos, const std::string& message)
- : runtime_error("Parsing error at position " + x2str(pos) + ". " + message),
- position(pos),
- error(message)
- {}
- virtual ~ParserError() throw() {}
-
- size_t position;
- std::string error;
-};
-
-size_t skipSpaces(const std::string& value, size_t start)
-{
- while (start < value.length() && std::isspace(value[start]))
- ++start;
- return start;
-}
-
-size_t checkChar(const std::string& value, size_t start, char ch)
-{
- if (start >= value.length())
- throw ParserError(start, "Unexpected end of string. Expected '" + std::string(1, ch) + "'.");
- if (value[start] != ch)
- throw ParserError(start, "Expected '" + std::string(1, ch) + "', got '" + std::string(1, value[start]) + "'.");
- return start + 1;
-}
-
-std::pair<size_t, std::string> readString(const std::string& value, size_t start)
-{
- std::string dest;
- while (start < value.length() && !std::isspace(value[start]) &&
- value[start] != ',' && value[start] != '(' && value[start] != ')')
- dest.push_back(value[start++]);
- if (dest.empty()) {
- if (start == value.length())
- throw ParserError(start, "Unexpected end of string. Expected string.");
- else
- throw ParserError(start, "Unexpected whitespace. Expected string.");
- }
- return std::make_pair(start, dest);
-}
-
-Config::Pairs toPairs(const std::vector<std::string>& values)
-{
- if (values.empty())
- return Config::Pairs();
- std::string value(values[0]);
- Config::Pairs res;
- size_t start = 0;
- while (start < value.size()) {
- Config::Pair pair;
- start = skipSpaces(value, start);
- if (!res.empty())