diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/Exceptions.cpp | 4 | ||||
-rw-r--r-- | src/core/Exceptions.hpp | 63 | ||||
-rw-r--r-- | src/core/variant/Reader.cpp | 14 | ||||
-rw-r--r-- | src/core/variant/Reader.hpp | 54 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.cpp | 14 |
5 files changed, 78 insertions, 71 deletions
diff --git a/src/core/Exceptions.cpp b/src/core/Exceptions.cpp index 735dac6..206f5b2 100644 --- a/src/core/Exceptions.cpp +++ b/src/core/Exceptions.cpp @@ -25,8 +25,8 @@ namespace ousia { /* Class LoggableException */ std::string LoggableException::formatMessage(const std::string &msg, - const std::string &file, int line, - int column, bool fatal) + const std::string &file, + bool fatal, int line, int column) { std::stringstream ss; ss << "error "; diff --git a/src/core/Exceptions.hpp b/src/core/Exceptions.hpp index f1bb95a..a5d50d5 100644 --- a/src/core/Exceptions.hpp +++ b/src/core/Exceptions.hpp @@ -81,8 +81,8 @@ private: * reported to the runtime environment. */ static std::string formatMessage(const std::string &msg, - const std::string &file, int line, - int column, bool fatal); + const std::string &file, bool fatal, + int line, int column); public: /** @@ -96,6 +96,12 @@ public: const std::string file; /** + * If set to true, the exception should not be handled as recoverable error + * but as "fatal" error. + */ + const bool fatal; + + /** * Line at which the exception occured. Negative values are ignored. */ const int line; @@ -106,12 +112,6 @@ public: const int column; /** - * If set to true, the exception should not be handled as recoverable error - * but as "fatal" error. - */ - const bool fatal; - - /** * Constructor of the LoggableException class. * * @param msg contains the error message. @@ -120,14 +120,14 @@ public: * @param column is the column in the above file the message refers to. * @param fatal shoudl be set to true if the error is non-recoverable. */ - LoggableException(std::string msg, std::string file, int line = -1, - int column = -1, bool fatal = true) - : OusiaException(formatMessage(msg, file, line, column, fatal)), + LoggableException(std::string msg, std::string file, bool fatal, + int line = -1, int column = -1) + : OusiaException(formatMessage(msg, file, fatal, line, column)), msg(std::move(msg)), file(std::move(file)), + fatal(fatal), line(line), - column(column), - fatal(fatal) + column(column) { } @@ -135,17 +135,16 @@ public: * Constructor of the LoggableException class with empty file. * * @param msg contains the error message. + * @param fatal should be set to true if the error is non-recoverable. * @param line is the line in the above file the message refers to. * @param column is the column in the above file the message refers to. - * @param fatal shoudl be set to true if the error is non-recoverable. */ - LoggableException(std::string msg, int line = -1, int column = -1, - bool fatal = true) - : OusiaException(formatMessage(msg, "", line, column, fatal)), + LoggableException(std::string msg, bool fatal, int line = -1, int column = -1) + : OusiaException(formatMessage(msg, "", fatal, line, column)), msg(std::move(msg)), + fatal(fatal), line(line), - column(column), - fatal(fatal) + column(column) { } @@ -156,11 +155,31 @@ public: * @param fatal should be set to true if the error is non-recoverable. */ LoggableException(std::string msg, bool fatal) - : OusiaException(formatMessage(msg, "", -1, -1, fatal)), + : OusiaException(formatMessage(msg, "", fatal, -1, -1)), msg(std::move(msg)), + fatal(fatal), line(-1), - column(-1), - fatal(fatal) + column(-1) + { + } + + /** + * Constructor of the LoggableException class with empty file and an + * position object. + * + * @param msg is the actual log message. + * @param pos is a const reference to a variable which provides position + * information. + * @param fatal should be set to true if the error is non-recoverable. + */ + template <class PosType> + LoggableException(std::string msg, bool fatal, const PosType &pos) + : OusiaException( + formatMessage(msg, "", fatal, pos.getLine(), pos.getColumn())), + msg(std::move(msg)), + fatal(fatal), + line(pos.getLine()), + column(pos.getColumn()) { } }; diff --git a/src/core/variant/Reader.cpp b/src/core/variant/Reader.cpp index 6142ecf..e9a58a1 100644 --- a/src/core/variant/Reader.cpp +++ b/src/core/variant/Reader.cpp @@ -26,12 +26,17 @@ namespace ousia { namespace variant { +static const char *ERR_UNEXPECTED_CHARACTER = "Unexpected character"; +static const char *ERR_UNEXPECTED_END = "Unexpected end"; +static const char *ERR_UNTERMINATED = "Unterminated literal"; + static const int STATE_INIT = 0; static const int STATE_IN_STRING = 1; static const int STATE_ESCAPE = 2; static std::pair<Err, std::string> parseString( - BufferedCharReader &reader, const unordered_set<char> *delims = nullptr) + BufferedCharReader &reader, const unordered_set<char> *delims = nullptr, + Logger *logger = nullptr) { // Initialize the internal state Err errCode = Err::OK; @@ -51,9 +56,13 @@ static std::pair<Err, std::string> parseString( quote = c; state = STATE_IN_STRING; } else if (delims && delims.count(c)) { + Logger.log(ERR_UNTERMINATED, reader); return std::make_pair(Err::UNEXPECTED_END, res.str()); + } else if (Utils::isWhitespace(c)) { + reader.consumePeek(); + continue; } - reader.consumePeek(); + return std::make_pair(Err::UNEXPECTED_CHARACTER, res.str()); break; case STATE_IN_STRING: if (c == q) { @@ -171,7 +180,6 @@ static std::pair<Err, Variant> parseGeneric(BufferedCharReader &reader, } return std::make_pair(Err::UNEXPECTED_END, res.str()); } - } } diff --git a/src/core/variant/Reader.hpp b/src/core/variant/Reader.hpp index 3f945f0..339127f 100644 --- a/src/core/variant/Reader.hpp +++ b/src/core/variant/Reader.hpp @@ -32,6 +32,7 @@ #include <utility> #include <core/BufferedCharReader.hpp> +#include <core/Logger.hpp> #include "Variant.hpp" @@ -40,44 +41,6 @@ namespace variant { class Reader { public: - // TODO: Pass logger instance instead of using error codes? - - /** - * The Err enum describes possible error codes that may be encountered when - * parsing the microtypes. - */ - enum class Err : int { - /** - * Reached the end of the stream, but expected more data. - */ - ERR_UNEXPECTED_END = -1, - - /** - * The stream is malformed. - */ - ERR_MALFORMED = -2, - - /** - * Unexpected character. - */ - ERR_UNEXPECTED_CHARACTER = -3, - - /** - * Unterminated literal. - */ - ERR_UNTERMINATED = -4, - - /** - * Invalid escape character. - */ - ERR_INVALID_ESCAPE = -5, - - /** - * A value of the requested type was extracted successfully. - */ - OK = 0 - }; - /** * Parses a string which may either be enclosed by " or ', unescapes * entities in the string as specified for JavaScript. @@ -91,9 +54,10 @@ public: * outside). If nullptr is given, no delimiter is used and a complete string * is read. */ - static std::pair<Err, std::string> parseString( + static std::pair<bool, std::string> parseString( BufferedCharReader &reader, - const unordered_set<char> *delims = nullptr); + const unordered_set<char> *delims = nullptr, + Logger *logger = nullptr); /** * Extracts an unescaped string from the given buffered char reader @@ -106,8 +70,9 @@ public: * @param delims is a set of characters which will terminate the string. * These characters are not included in the result. May not be nullptr. */ - static std::pair<Err, std::string> parseUnescapedString( - BufferedCharReader &reader, const unordered_set<char> *delims); + static std::pair<bool, std::string> parseUnescapedString( + BufferedCharReader &reader, const unordered_set<char> *delims, + Logger *logger = nullptr); /** * Tries to parse the most specific item from the given stream until one of @@ -120,8 +85,9 @@ public: * @param delims is a set of characters which will terminate the string. * These characters are not included in the result. May not be nullptr. */ - static std::pair<Err, Variant> parseGeneric( - BufferedCharReader &reader, const unordered_set<char> *delims); + static std::pair<bool, Variant> parseGeneric( + BufferedCharReader &reader, const unordered_set<char> *delims, + Logger *logger = nullptr); }; } } diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index d239359..22752dd 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -125,12 +125,18 @@ void CSSParser::parseSelectors(Rooted<SelectorNode> root, case 2: // as the parseSelector is supposed to parse only a SelectorPath // there should not be more than one leaf. +<<<<<<< HEAD:application/src/core/CSSParser.cpp + throw LoggableException{ + "Internal Error: More than one leaf in SelectorPath!", true, + tokenizer.getInput()}; +======= throw ParserException{ "Internal Error: More than one leaf in SelectorPath!", "", // TODO: Line handling? // tokenizer.getInput().getLine(), // tokenizer.getInput().getColumn() }; +>>>>>>> 8a4203636865b6edc380b731c68d3483ca110a27:application/src/plugins/css/CSSParser.cpp } // if we find a comma, we can proceed parsing selectors. Token t; @@ -292,6 +298,13 @@ bool CSSParser::expect(int expectedType, CodeTokenizer &tokenizer, Token &t, if (end || t.tokenId != expectedType) { if (force) { if (end) { +<<<<<<< HEAD:application/src/core/CSSParser.cpp + throw LoggableException{"Unexpected end of file!", true, + tokenizer.getInput()}; + } else { + throw LoggableException{"Unexpected token!", true, + tokenizer.getInput()}; +======= throw ParserException{ "Unexpected end of file!", "", // TODO: Line handling? @@ -305,6 +318,7 @@ bool CSSParser::expect(int expectedType, CodeTokenizer &tokenizer, Token &t, // tokenizer.getInput().getLine(), // tokenizer.getInput().getColumn() }; +>>>>>>> 8a4203636865b6edc380b731c68d3483ca110a27:application/src/plugins/css/CSSParser.cpp } } else { tokenizer.resetPeek(); |