diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-18 15:30:18 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-18 15:30:18 +0100 |
commit | 8ab0709045667f8e1a67a9981c619980c9aebd1a (patch) | |
tree | f39812b3d216566de7c4dcdaacc7c607aa7e646c /src/core/common | |
parent | cd55519130c5f91d95107b3b1e1a8203caa5008a (diff) | |
parent | 372c67e0844654362fc7d440b0b4a31500a6d02a (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core/common')
-rw-r--r-- | src/core/common/Logger.hpp | 10 | ||||
-rw-r--r-- | src/core/common/VariantReader.cpp | 17 | ||||
-rw-r--r-- | src/core/common/VariantReader.hpp | 20 |
3 files changed, 46 insertions, 1 deletions
diff --git a/src/core/common/Logger.hpp b/src/core/common/Logger.hpp index b365a39..767d8ab 100644 --- a/src/core/common/Logger.hpp +++ b/src/core/common/Logger.hpp @@ -243,6 +243,16 @@ public: } /** + * Logs the given loggable exception at the given location. + * + * @param ex is the exception that should be logged. + */ + void log(const LoggableException &ex, const SourceLocation &loc) + { + log(Severity::ERROR, ex.msg, loc.valid() ? loc : ex.getLocation()); + } + + /** * Logs the given message. The file name is set to the topmost file name on * the file name stack. * diff --git a/src/core/common/VariantReader.cpp b/src/core/common/VariantReader.cpp index faad40c..600fd9b 100644 --- a/src/core/common/VariantReader.cpp +++ b/src/core/common/VariantReader.cpp @@ -583,6 +583,7 @@ std::pair<bool, Variant> VariantReader::parseGenericToken( return std::make_pair(true, n.doubleValue()); } } + reader.resetPeek(); } // Try to parse an object @@ -618,8 +619,22 @@ std::pair<bool, Variant> VariantReader::parseGenericToken( v.setMagic(res.second.c_str()); return std::make_pair(res.first, v); } else { - return std::make_pair(res.first, Variant{res.second.c_str()}); + return std::make_pair(res.first, Variant::fromString(res.second)); + } +} + +std::pair<bool, Variant> VariantReader::parseGenericString( + const std::string &str, Logger &logger) +{ + CharReader reader{str}; + LoggerFork loggerFork = logger.fork(); + std::pair<bool, Variant> res = + parseGenericToken(reader, loggerFork, std::unordered_set<char>{}, true); + if (reader.atEnd()) { + loggerFork.commit(); + return res; } + return std::make_pair(true, Variant::fromString(str)); } } diff --git a/src/core/common/VariantReader.hpp b/src/core/common/VariantReader.hpp index abf529c..8aaffd8 100644 --- a/src/core/common/VariantReader.hpp +++ b/src/core/common/VariantReader.hpp @@ -229,6 +229,26 @@ public: CharReader &reader, Logger &logger, const std::unordered_set<char> &delims, bool extractUnescapedStrings = false); + + /** + * Tries to parse the most specific item from the given string. The + * resulting variant represents the value that has been read. If the end of + * the string was not reached while parsing an element, the result is + * returned as string. + * + * @param str is the string from which the value should be read. + * @param logger is the logger instance to which errors or warnings will be + * written. + * @return a pair indicating whether the operation was successful and the + * extracted variant value. Note that the variant value most times contains + * some meaningful data that can be worked with even if the operation was + * not successful (e.g. if a syntax error is encountered while reading an + * array, the successfully read elements will still be in the returned + * variant.) Information on why the operation has failed is passed to the + * logger. + */ + static std::pair<bool, Variant> parseGenericString( + const std::string &str, Logger &logger); }; } |