summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2014-12-05 14:16:27 +0100
committerBenjamin Paassen <bpaassen@techfak.uni-bielefeld.de>2014-12-05 14:16:27 +0100
commit30765a8bbf30aafad89a632afc39966c5b4029b8 (patch)
treeb9540230f7ac8ac68d66caf7d727e7413a56f631
parentfddd8a6fc3c9a7971111a345a83283a0a1662f9f (diff)
parentbf59bc2edbb1f3f4d12bfbd8ed2663fbbb1900c0 (diff)
Merge branch 'master' of somweyr.de:ousia
-rw-r--r--CMakeLists.txt3
-rw-r--r--src/core/Exceptions.cpp4
-rw-r--r--src/core/Exceptions.hpp44
-rw-r--r--src/core/Logger.hpp29
-rw-r--r--src/core/variant/Reader.cpp14
-rw-r--r--src/core/variant/Reader.hpp54
-rw-r--r--src/plugins/css/CSSParser.cpp23
-rw-r--r--src/plugins/xml/XmlParser.cpp3
-rw-r--r--test/core/LoggerTest.cpp16
-rw-r--r--test/plugins/xml/XmlParserTest.cpp1
10 files changed, 81 insertions, 110 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3e469a1..98b7acb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -114,6 +114,7 @@ ADD_LIBRARY(ousia_core
# src/core/script/Object
# src/core/script/ScriptEngine
# src/core/script/Variant
+# src/core/variant/Reader
src/core/variant/Variant
)
@@ -165,6 +166,7 @@ IF(TEST)
# test/core/script/FunctionTest
# test/core/script/ObjectTest
# test/core/script/VariantTest
+# test/core/variant/ReaderTest
test/core/variant/VariantTest
)
@@ -206,6 +208,7 @@ IF(TEST)
# Register the unit tests
ADD_TEST(ousia_test_core ousia_test_core)
ADD_TEST(ousia_test_xml ousia_test_xml)
+ ADD_TEST(ousia_test_css ousia_test_css)
# ADD_TEST(ousia_test_mozjs ousia_test_mozjs)
ENDIF()
diff --git a/src/core/Exceptions.cpp b/src/core/Exceptions.cpp
index 735dac6..d064f35 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,
+ int line, int column)
{
std::stringstream ss;
ss << "error ";
diff --git a/src/core/Exceptions.hpp b/src/core/Exceptions.hpp
index f1bb95a..00d6106 100644
--- a/src/core/Exceptions.hpp
+++ b/src/core/Exceptions.hpp
@@ -82,7 +82,7 @@ private:
*/
static std::string formatMessage(const std::string &msg,
const std::string &file, int line,
- int column, bool fatal);
+ int column);
public:
/**
@@ -106,28 +106,20 @@ 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.
* @param file provides the context the message refers to. May be empty.
* @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, std::string file, int line = -1,
- int column = -1, bool fatal = true)
- : OusiaException(formatMessage(msg, file, line, column, fatal)),
+ int column = -1)
+ : OusiaException(formatMessage(msg, file, line, column)),
msg(std::move(msg)),
file(std::move(file)),
line(line),
- column(column),
- fatal(fatal)
+ column(column)
{
}
@@ -137,30 +129,30 @@ public:
* @param msg contains the error message.
* @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, int line = -1, int column = -1)
+ : OusiaException(formatMessage(msg, "", line, column)),
msg(std::move(msg)),
line(line),
- column(column),
- fatal(fatal)
+ column(column)
{
}
/**
- * Constructor of the LoggableException class with empty file.
+ * Constructor of the LoggableException class with empty file and an
+ * position object.
*
- * @param msg contains the error message.
- * @param fatal should be set to true if the error is non-recoverable.
+ * @param msg is the actual log message.
+ * @param pos is a const reference to a variable which provides position
+ * information.
*/
- LoggableException(std::string msg, bool fatal)
- : OusiaException(formatMessage(msg, "", -1, -1, fatal)),
+ template <class PosType>
+ LoggableException(std::string msg, const PosType &pos)
+ : OusiaException(
+ formatMessage(msg, "", pos.getLine(), pos.getColumn())),
msg(std::move(msg)),
- line(-1),
- column(-1),
- fatal(fatal)
+ line(pos.getLine()),
+ column(pos.getColumn())
{
}
};
diff --git a/src/core/Logger.hpp b/src/core/Logger.hpp
index fd7bb08..e6b97f4 100644
--- a/src/core/Logger.hpp
+++ b/src/core/Logger.hpp
@@ -251,7 +251,7 @@ public:
* @tparam PosType is the actual type of pos and must implement a getLine
* and getColumn function.
*/
- template<class PosType>
+ template <class PosType>
void logAt(Severity severity, const std::string &msg, const PosType &pos)
{
log(severity, msg, pos.getLine(), pos.getColumn());
@@ -264,7 +264,7 @@ public:
*/
void log(const LoggableException &ex)
{
- log(ex.fatal ? Severity::FATAL_ERROR : Severity::ERROR, ex.msg,
+ log(Severity::ERROR, ex.msg,
ex.file.empty() ? currentFilename() : ex.file, ex.line, ex.column);
}
@@ -279,7 +279,8 @@ public:
* @param column is the column in the above file at which the error occured.
* Ignored if negative.
*/
- void debug(const std::string &msg, const std::string &file, int line = -1, int column = -1)
+ void debug(const std::string &msg, const std::string &file, int line = -1,
+ int column = -1)
{
log(Severity::DEBUG, msg, file, line, column);
}
@@ -308,7 +309,7 @@ public:
* @param pos is a const reference to a variable which provides position
* information.
*/
- template<class PosType>
+ template <class PosType>
void debugAt(const std::string &msg, const PosType &pos)
{
debug(msg, pos.getLine(), pos.getColumn());
@@ -325,7 +326,8 @@ public:
* @param column is the column in the above file at which the error occured.
* Ignored if negative.
*/
- void note(const std::string &msg, const std::string &file, int line = -1, int column = -1)
+ void note(const std::string &msg, const std::string &file, int line = -1,
+ int column = -1)
{
log(Severity::NOTE, msg, file, line, column);
}
@@ -353,7 +355,7 @@ public:
* @param pos is a const reference to a variable which provides position
* information.
*/
- template<class PosType>
+ template <class PosType>
void noteAt(const std::string &msg, const PosType &pos)
{
note(msg, pos.getLine(), pos.getColumn());
@@ -370,7 +372,8 @@ public:
* @param column is the column in the above file at which the error occured.
* Ignored if negative.
*/
- void warning(const std::string &msg, const std::string &file, int line = -1, int column = -1)
+ void warning(const std::string &msg, const std::string &file, int line = -1,
+ int column = -1)
{
log(Severity::WARNING, msg, file, line, column);
}
@@ -383,7 +386,7 @@ public:
* @param pos is a const reference to a variable which provides position
* information.
*/
- template<class PosType>
+ template <class PosType>
void warningAt(const std::string &msg, const PosType &pos)
{
warning(msg, pos.getLine(), pos.getColumn());
@@ -415,7 +418,8 @@ public:
* @param column is the column in the above file at which the error occured.
* Ignored if negative.
*/
- void error(const std::string &msg, const std::string &file, int line = -1, int column = -1)
+ void error(const std::string &msg, const std::string &file, int line = -1,
+ int column = -1)
{
log(Severity::ERROR, msg, file, line, column);
}
@@ -443,7 +447,7 @@ public:
* @param pos is a const reference to a variable which provides position
* information.
*/
- template<class PosType>
+ template <class PosType>
void errorAt(const std::string &msg, const PosType &pos)
{
error(msg, pos.getLine(), pos.getColumn());
@@ -460,7 +464,8 @@ public:
* @param column is the column in the above file at which the error occured.
* Ignored if negative.
*/
- void fatalError(const std::string &msg, const std::string &file, int line = -1, int column = -1)
+ void fatalError(const std::string &msg, const std::string &file,
+ int line = -1, int column = -1)
{
log(Severity::FATAL_ERROR, msg, file, line, column);
}
@@ -488,7 +493,7 @@ public:
* @param pos is a const reference to a variable which provides position
* information.
*/
- template<class PosType>
+ template <class PosType>
void fatalErrorAt(const std::string &msg, const PosType &pos)
{
fatalError(msg, pos.getLine(), 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 00b5af5..85d8858 100644
--- a/src/plugins/css/CSSParser.cpp
+++ b/src/plugins/css/CSSParser.cpp
@@ -135,11 +135,8 @@ void CSSParser::parseSelectors(Rooted<SelectorNode> root,
// as the parseSelector is supposed to parse only a SelectorPath
// there should not be more than one leaf.
throw ParserException{
- "Internal Error: More than one leaf in SelectorPath!", "",
- // TODO: Line handling?
- // tokenizer.getInput().getLine(),
- // tokenizer.getInput().getColumn()
- };
+ "Internal Error: More than one leaf in SelectorPath!",
+ tokenizer.getInput()};
}
// if we find a comma, we can proceed parsing selectors.
Token t;
@@ -301,19 +298,11 @@ bool CSSParser::expect(int expectedType, CodeTokenizer &tokenizer, Token &t,
if (end || t.tokenId != expectedType) {
if (force) {
if (end) {
- throw ParserException{
- "Unexpected end of file!", "",
- // TODO: Line handling?
- // tokenizer.getInput().getLine(),
- // tokenizer.getInput().getColumn()
- };
+ throw ParserException{"Unexpected end of file!",
+ tokenizer.getInput()};
} else {
- throw ParserException{
- "Unexpected token!", "",
- // TODO: Line handling?
- // tokenizer.getInput().getLine(),
- // tokenizer.getInput().getColumn()
- };
+ throw ParserException{"Unexpected token!",
+ tokenizer.getInput()};
}
} else {
tokenizer.resetPeek();
diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp
index afc7f14..ce2857e 100644
--- a/src/plugins/xml/XmlParser.cpp
+++ b/src/plugins/xml/XmlParser.cpp
@@ -208,8 +208,7 @@ Rooted<Node> XmlParser::parse(std::istream &is, ParserContext &ctx)
const int column = XML_GetCurrentColumnNumber(&p);
const XML_Error code = XML_GetErrorCode(&p);
const std::string msg = std::string{XML_ErrorString(code)};
- throw ParserException{"XML Syntax Error: " + msg, line, column,
- false};
+ throw ParserException{"XML Syntax Error: " + msg, line, column};
}
// Abort once there are no more bytes in the stream
diff --git a/test/core/LoggerTest.cpp b/test/core/LoggerTest.cpp
index b4549ed..abb76de 100644
--- a/test/core/LoggerTest.cpp
+++ b/test/core/LoggerTest.cpp
@@ -26,7 +26,7 @@ namespace ousia {
struct Pos {
int line, column;
- Pos(int line, int column) : line(line), column(column) {};
+ Pos(int line, int column) : line(line), column(column){};
int getLine() const { return line; }
int getColumn() const { return column; }
};
@@ -52,13 +52,23 @@ TEST(TerminalLogger, log)
logger.fatalError("This is a test fatal error!", 10, 20);
try {
- throw LoggableException{"A fatal exception"};
+ throw LoggableException{"An exception"};
}
catch (const LoggableException &ex) {
logger.log(ex);
}
- logger.logAt(Severity::ERROR, "This is a positioned error", Pos(10, 20));
+ try {
+ throw LoggableException{"An exception at position", Pos(10, 20)};
+ }
+ catch (const LoggableException &ex) {
+ logger.log(ex);
+ }
+
+ logger.logAt(Severity::ERROR, "This is a positioned log message",
+ Pos(10, 20));
+ logger.debugAt("This is a positioned debug message", Pos(10, 20));
+ logger.noteAt("This is a positioned log error", Pos(10, 20));
}
}
diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp
index ecc9438..7dc8c24 100644
--- a/test/plugins/xml/XmlParserTest.cpp
+++ b/test/plugins/xml/XmlParserTest.cpp
@@ -37,7 +37,6 @@ TEST(XmlParser, mismatchedTagException)
}
catch (ParserException ex) {
ASSERT_EQ(2, ex.line);
- ASSERT_FALSE(ex.fatal);
hadException = true;
}
ASSERT_TRUE(hadException);