From 14b3a5c80ae0336de0d34d0d05fad6efc994462c Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 28 Nov 2014 14:42:03 +0100 Subject: moved some files --- src/core/Parser.cpp | 32 ---------- src/core/Parser.hpp | 116 ------------------------------------ src/core/parser/Parser.cpp | 32 ++++++++++ src/core/parser/Parser.hpp | 116 ++++++++++++++++++++++++++++++++++++ src/core/parser/XmlParser.cpp | 134 ------------------------------------------ src/core/parser/XmlParser.hpp | 63 -------------------- src/plugins/xml/XmlParser.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++ src/plugins/xml/XmlParser.hpp | 63 ++++++++++++++++++++ 8 files changed, 345 insertions(+), 345 deletions(-) delete mode 100644 src/core/Parser.cpp delete mode 100644 src/core/Parser.hpp create mode 100644 src/core/parser/Parser.cpp create mode 100644 src/core/parser/Parser.hpp delete mode 100644 src/core/parser/XmlParser.cpp delete mode 100644 src/core/parser/XmlParser.hpp create mode 100644 src/plugins/xml/XmlParser.cpp create mode 100644 src/plugins/xml/XmlParser.hpp (limited to 'src') diff --git a/src/core/Parser.cpp b/src/core/Parser.cpp deleted file mode 100644 index bc98ac0..0000000 --- a/src/core/Parser.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -#include "Parser.hpp" - -namespace ousia { - -Rooted Parser::parse(const std::string &str, Handle context, Logger &logger) -{ - std::istringstream is(str); - return parse(is, context, logger); -} - -} - diff --git a/src/core/Parser.hpp b/src/core/Parser.hpp deleted file mode 100644 index 74a1988..0000000 --- a/src/core/Parser.hpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -/** - * @file Parser.hpp - * - * Contains the abstract "Parser" class. Parsers are objects capable of reading - * a certain file format and transforming it into a node. - * - * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) - */ - -#ifndef _OUSIA_PARSER_HPP_ -#define _OUSIA_PARSER_HPP_ - -#include -#include -#include - -#include "Exceptions.hpp" -#include "Node.hpp" -#include "Logger.hpp" - -namespace ousia { - -// TODO: Implement a proper Mimetype class - -/** - * Exception to be thrown whenever an error occurs inside a specific parser. - */ -class ParserException : public LoggableException { -public: - using LoggableException::LoggableException; -}; - -/** - * Abstract parser class. This class builds the basic interface that should be - * used by any parser which reads data from an input stream and transforms it - * into an Ousía node graph. - */ -class Parser { -public: - - Parser() {}; - Parser(const Parser&) = delete; - - /** - * Returns a set containing all mime types supported by the parser. The mime - * types are used to describe the type of the document that is read by the - * parser. The default implementation returns an empty set. This method - * should be overridden by derived classes. - * - * @return a set containing the string value of the supported mime types. - */ - virtual std::set mimetypes() - { - return std::set{}; - }; - - /** - * Parses the given input stream and returns a corresponding node for - * inclusion in the document graph. This method should be overridden by - * derived classes. - * - * @param is is a reference to the input stream that should be parsed. - * @param context defines the context in which the input stream should be - * parsed. The context represents the scope from which element names should - * be looked up. - * @param logger is a reference to the Logger instance that should be used - * to log error messages and warnings that occur while parsing the document. - * @return a reference to the node representing the subgraph that has been - * created. The resulting node may point at not yet resolved entities, the - * calling code will try to resolve these. If no valid node can be produced, - * a corresponding LoggableException must be thrown by the parser. - */ - virtual Rooted parse(std::istream &is, Handle context, - Logger &logger) = 0; - - /** - * Parses the given string and returns a corresponding node for - * inclusion in the document graph. This method should be overridden by - * derived classes. - * - * @param str is the string that should be parsed. - * @param context defines the context in which the input stream should be - * parsed. The context represents the scope from which element names should - * be looked up. - * @param logger is a reference to the Logger instance that should be used - * to log error messages and warnings that occur while parsing the document. - * @return a reference to the node representing the subgraph that has been - * created. The resulting node may point at not yet resolved entities, the - * calling code will try to resolve these. If no valid node can be produced, - * a corresponding ParserException must be thrown by the parser. - */ - Rooted parse(const std::string &str, Handle context, - Logger &logger); -}; -} - -#endif /* _OUSIA_PARSER_HPP_ */ - diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp new file mode 100644 index 0000000..bc98ac0 --- /dev/null +++ b/src/core/parser/Parser.cpp @@ -0,0 +1,32 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include "Parser.hpp" + +namespace ousia { + +Rooted Parser::parse(const std::string &str, Handle context, Logger &logger) +{ + std::istringstream is(str); + return parse(is, context, logger); +} + +} + diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp new file mode 100644 index 0000000..74a1988 --- /dev/null +++ b/src/core/parser/Parser.hpp @@ -0,0 +1,116 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file Parser.hpp + * + * Contains the abstract "Parser" class. Parsers are objects capable of reading + * a certain file format and transforming it into a node. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_PARSER_HPP_ +#define _OUSIA_PARSER_HPP_ + +#include +#include +#include + +#include "Exceptions.hpp" +#include "Node.hpp" +#include "Logger.hpp" + +namespace ousia { + +// TODO: Implement a proper Mimetype class + +/** + * Exception to be thrown whenever an error occurs inside a specific parser. + */ +class ParserException : public LoggableException { +public: + using LoggableException::LoggableException; +}; + +/** + * Abstract parser class. This class builds the basic interface that should be + * used by any parser which reads data from an input stream and transforms it + * into an Ousía node graph. + */ +class Parser { +public: + + Parser() {}; + Parser(const Parser&) = delete; + + /** + * Returns a set containing all mime types supported by the parser. The mime + * types are used to describe the type of the document that is read by the + * parser. The default implementation returns an empty set. This method + * should be overridden by derived classes. + * + * @return a set containing the string value of the supported mime types. + */ + virtual std::set mimetypes() + { + return std::set{}; + }; + + /** + * Parses the given input stream and returns a corresponding node for + * inclusion in the document graph. This method should be overridden by + * derived classes. + * + * @param is is a reference to the input stream that should be parsed. + * @param context defines the context in which the input stream should be + * parsed. The context represents the scope from which element names should + * be looked up. + * @param logger is a reference to the Logger instance that should be used + * to log error messages and warnings that occur while parsing the document. + * @return a reference to the node representing the subgraph that has been + * created. The resulting node may point at not yet resolved entities, the + * calling code will try to resolve these. If no valid node can be produced, + * a corresponding LoggableException must be thrown by the parser. + */ + virtual Rooted parse(std::istream &is, Handle context, + Logger &logger) = 0; + + /** + * Parses the given string and returns a corresponding node for + * inclusion in the document graph. This method should be overridden by + * derived classes. + * + * @param str is the string that should be parsed. + * @param context defines the context in which the input stream should be + * parsed. The context represents the scope from which element names should + * be looked up. + * @param logger is a reference to the Logger instance that should be used + * to log error messages and warnings that occur while parsing the document. + * @return a reference to the node representing the subgraph that has been + * created. The resulting node may point at not yet resolved entities, the + * calling code will try to resolve these. If no valid node can be produced, + * a corresponding ParserException must be thrown by the parser. + */ + Rooted parse(const std::string &str, Handle context, + Logger &logger); +}; +} + +#endif /* _OUSIA_PARSER_HPP_ */ + diff --git a/src/core/parser/XmlParser.cpp b/src/core/parser/XmlParser.cpp deleted file mode 100644 index f9bb43e..0000000 --- a/src/core/parser/XmlParser.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -#include "XmlParser.hpp" - -namespace ousia { - -/** - * The XmlParserData struct holds all information relevant to the expat callback - * functions. - */ -struct XmlParserData { - Rooted context; - Logger &logger; - - XmlParserData(Handle context, Logger &logger) - : context(context), logger(logger) - { - } -}; - -/** - * Wrapper class around the XML_Parser pointer which safely frees it whenever - * the scope is left (e.g. because an exception was thrown). - */ -class ScopedExpatXmlParser { -private: - /** - * Internal pointer to the XML_Parser instance. - */ - XML_Parser parser; - -public: - /** - * Constructor of the ScopedExpatXmlParser class. Calls XML_ParserCreateNS - * from the expat library. Throws a parser exception if the XML parser - * cannot be initialized. - * - * @param encoding is the protocol-defined encoding passed to expat (or - * nullptr if expat should determine the encoding by itself). - * @param namespaceSeparator is the separator used to separate the namespace - * components in the node name given by expat. - */ - ScopedExpatXmlParser(const XML_Char *encoding, XML_Char namespaceSeparator) - : parser(nullptr) - { - parser = XML_ParserCreateNS("UTF-8", ':'); - if (!parser) { - throw ParserException{ - "Internal error: Could not create expat XML parser!"}; - } - } - - /** - * Destuctor of the ScopedExpatXmlParser, frees the XML parser instance. - */ - ~ScopedExpatXmlParser() - { - if (parser) { - XML_ParserFree(parser); - parser = nullptr; - } - } - - /** - * Returns the XML_Parser pointer. - */ - XML_Parser operator&() { return parser; } -}; - -std::set XmlParser::mimetypes() -{ - return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; -} - -Rooted XmlParser::parse(std::istream &is, Handle context, - Logger &logger) -{ - // Create the parser object - ScopedExpatXmlParser p{"UTF-8", ':'}; - - // Set the callback functions, provide a pointer to a XmlParserData instance - // as user data. - XmlParserData ctx{context, logger}; - - // Feed data into expat while there is data to process - const std::streamsize BUFFER_SIZE = 4096; // TODO: Move to own header? - while (true) { - // Fetch a buffer from expat for the input data - char *buf = static_cast(XML_GetBuffer(&p, BUFFER_SIZE)); - if (!buf) { - throw ParserException{"Internal error: XML parser out of memory!"}; - } - - // Read the input data from the stream - const std::streamsize bytesRead = is.read(buf, BUFFER_SIZE).gcount(); - - // Parse the data and handle any XML error - if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) { - const int line = XML_GetCurrentLineNumber(&p); - const int column = XML_GetCurrentColumnNumber(&p); - const XML_Error code = XML_GetErrorCode(&p); - const std::string msg = std::string{XML_ErrorString(code)}; - logger.error("XML: " + msg, line, column); - break; - } - - // Abort once there are no more bytes in the stream - if (bytesRead == 0) { - break; - } - } - - return nullptr; -} -} - diff --git a/src/core/parser/XmlParser.hpp b/src/core/parser/XmlParser.hpp deleted file mode 100644 index f6fb060..0000000 --- a/src/core/parser/XmlParser.hpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -/** - * @file XmlParser.hpp - * - * Contains the parser responsible for reading Ousía XML Documents (extension - * oxd) and Ousía XML Modules (extension oxm). - * - * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) - */ - -#ifndef _OUSIA_XML_PARSER_HPP_ -#define _OUSIA_XML_PARSER_HPP_ - -#include - -namespace ousia { - -/** - * The XmlParser class implements parsing the various types of Ousía XML - * documents using the expat stream XML parser. - */ -class XmlParser : public Parser { -public: - /** - * Returns the mimetype supported by the XmlParser which is - * "text/vnd.ousia.oxm" and "text/vnd.ousia.oxd". - * - * @return a list containing the mimetype supported by Ousía. - */ - std::set mimetypes() override; - - /** - * Parses the given input stream as XML file and returns the parsed - * top-level node. Throws - * - * @param is is the input stream that will be parsed. - */ - Rooted parse(std::istream &is, Handle context, - Logger &logger) override; - - using Parser::parse; -}; -} - -#endif /* _OUSIA_XML_PARSER_HPP_ */ - diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp new file mode 100644 index 0000000..f9bb43e --- /dev/null +++ b/src/plugins/xml/XmlParser.cpp @@ -0,0 +1,134 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include "XmlParser.hpp" + +namespace ousia { + +/** + * The XmlParserData struct holds all information relevant to the expat callback + * functions. + */ +struct XmlParserData { + Rooted context; + Logger &logger; + + XmlParserData(Handle context, Logger &logger) + : context(context), logger(logger) + { + } +}; + +/** + * Wrapper class around the XML_Parser pointer which safely frees it whenever + * the scope is left (e.g. because an exception was thrown). + */ +class ScopedExpatXmlParser { +private: + /** + * Internal pointer to the XML_Parser instance. + */ + XML_Parser parser; + +public: + /** + * Constructor of the ScopedExpatXmlParser class. Calls XML_ParserCreateNS + * from the expat library. Throws a parser exception if the XML parser + * cannot be initialized. + * + * @param encoding is the protocol-defined encoding passed to expat (or + * nullptr if expat should determine the encoding by itself). + * @param namespaceSeparator is the separator used to separate the namespace + * components in the node name given by expat. + */ + ScopedExpatXmlParser(const XML_Char *encoding, XML_Char namespaceSeparator) + : parser(nullptr) + { + parser = XML_ParserCreateNS("UTF-8", ':'); + if (!parser) { + throw ParserException{ + "Internal error: Could not create expat XML parser!"}; + } + } + + /** + * Destuctor of the ScopedExpatXmlParser, frees the XML parser instance. + */ + ~ScopedExpatXmlParser() + { + if (parser) { + XML_ParserFree(parser); + parser = nullptr; + } + } + + /** + * Returns the XML_Parser pointer. + */ + XML_Parser operator&() { return parser; } +}; + +std::set XmlParser::mimetypes() +{ + return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; +} + +Rooted XmlParser::parse(std::istream &is, Handle context, + Logger &logger) +{ + // Create the parser object + ScopedExpatXmlParser p{"UTF-8", ':'}; + + // Set the callback functions, provide a pointer to a XmlParserData instance + // as user data. + XmlParserData ctx{context, logger}; + + // Feed data into expat while there is data to process + const std::streamsize BUFFER_SIZE = 4096; // TODO: Move to own header? + while (true) { + // Fetch a buffer from expat for the input data + char *buf = static_cast(XML_GetBuffer(&p, BUFFER_SIZE)); + if (!buf) { + throw ParserException{"Internal error: XML parser out of memory!"}; + } + + // Read the input data from the stream + const std::streamsize bytesRead = is.read(buf, BUFFER_SIZE).gcount(); + + // Parse the data and handle any XML error + if (!XML_ParseBuffer(&p, bytesRead, bytesRead == 0)) { + const int line = XML_GetCurrentLineNumber(&p); + const int column = XML_GetCurrentColumnNumber(&p); + const XML_Error code = XML_GetErrorCode(&p); + const std::string msg = std::string{XML_ErrorString(code)}; + logger.error("XML: " + msg, line, column); + break; + } + + // Abort once there are no more bytes in the stream + if (bytesRead == 0) { + break; + } + } + + return nullptr; +} +} + diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp new file mode 100644 index 0000000..f6fb060 --- /dev/null +++ b/src/plugins/xml/XmlParser.hpp @@ -0,0 +1,63 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file XmlParser.hpp + * + * Contains the parser responsible for reading Ousía XML Documents (extension + * oxd) and Ousía XML Modules (extension oxm). + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_XML_PARSER_HPP_ +#define _OUSIA_XML_PARSER_HPP_ + +#include + +namespace ousia { + +/** + * The XmlParser class implements parsing the various types of Ousía XML + * documents using the expat stream XML parser. + */ +class XmlParser : public Parser { +public: + /** + * Returns the mimetype supported by the XmlParser which is + * "text/vnd.ousia.oxm" and "text/vnd.ousia.oxd". + * + * @return a list containing the mimetype supported by Ousía. + */ + std::set mimetypes() override; + + /** + * Parses the given input stream as XML file and returns the parsed + * top-level node. Throws + * + * @param is is the input stream that will be parsed. + */ + Rooted parse(std::istream &is, Handle context, + Logger &logger) override; + + using Parser::parse; +}; +} + +#endif /* _OUSIA_XML_PARSER_HPP_ */ + -- cgit v1.2.3 From 235b98e0d1a2e9e60c440076b5a11c8bf64ba071 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 30 Nov 2014 23:41:49 +0100 Subject: backup --- src/core/Exceptions.cpp | 7 +- src/core/Logger.cpp | 2 +- src/core/Logger.hpp | 90 +++++++++- src/core/Registry.cpp | 47 +++++ src/core/Registry.hpp | 51 ++++++ src/core/parser/Parser.cpp | 9 +- src/core/parser/Parser.hpp | 72 +++++--- src/core/parser/Scope.cpp | 26 +++ src/core/parser/Scope.hpp | 170 ++++++++++++++++++ src/plugins/mozjs/MozJsScriptEngine.cpp | 2 + src/plugins/mozjs/MozJsScriptEngine.hpp | 2 + src/plugins/xml/XmlParser.cpp | 65 ++++--- src/plugins/xml/XmlParser.hpp | 14 +- src/plugins/xml/XmlStates.cpp | 53 ++++++ src/plugins/xml/XmlStates.hpp | 301 ++++++++++++++++++++++++++++++++ 15 files changed, 847 insertions(+), 64 deletions(-) create mode 100644 src/core/Registry.cpp create mode 100644 src/core/Registry.hpp create mode 100644 src/core/parser/Scope.cpp create mode 100644 src/core/parser/Scope.hpp create mode 100644 src/plugins/xml/XmlStates.cpp create mode 100644 src/plugins/xml/XmlStates.hpp (limited to 'src') diff --git a/src/core/Exceptions.cpp b/src/core/Exceptions.cpp index 92d9293..735dac6 100644 --- a/src/core/Exceptions.cpp +++ b/src/core/Exceptions.cpp @@ -29,16 +29,17 @@ std::string LoggableException::formatMessage(const std::string &msg, int column, bool fatal) { std::stringstream ss; + ss << "error "; if (!file.empty()) { ss << "while processing \"" << file << "\" "; } if (line >= 0) { - ss << "at line: " << line << " "; + ss << "at line " << line << ", "; if (column >= 0) { - ss << "col: " << column << " "; + ss << "column " << column << " "; } } - ss << "message: " << msg; + ss << "with message: " << msg; return ss.str(); } } diff --git a/src/core/Logger.cpp b/src/core/Logger.cpp index 1a3b6c6..17f55a6 100644 --- a/src/core/Logger.cpp +++ b/src/core/Logger.cpp @@ -149,7 +149,7 @@ void TerminalLogger::process(const Message &msg) os << t.color(Terminal::RED, true) << "error: "; break; case Severity::FATAL_ERROR: - os << t.color(Terminal::RED, true) << "error: "; + os << t.color(Terminal::RED, true) << "fatal: "; break; } os << t.reset(); diff --git a/src/core/Logger.hpp b/src/core/Logger.hpp index 260d010..a30374c 100644 --- a/src/core/Logger.hpp +++ b/src/core/Logger.hpp @@ -251,6 +251,22 @@ public: ex.file.empty() ? currentFilename() : ex.file, ex.line, ex.column); } + /** + * Logs a debug message. The file name is set to the topmost file name on + * the file name stack. + * + * @param msg is the actual log message. + * @param file is the name of the file the message refers to. May be empty. + * @param line is the line in the above file at which the error occured. + * Ignored if negative. + * @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) + { + log(Severity::DEBUG, msg, file, line, column); + } + /** * Logs a debug message. The file name is set to the topmost file name on * the file name stack. @@ -263,7 +279,23 @@ public: */ void debug(const std::string &msg, int line = -1, int column = -1) { - log(Severity::DEBUG, msg, line, column); + debug(msg, currentFilename(), line, column); + } + + /** + * Logs a note. The file name is set to the topmost file name on + * the file name stack. + * + * @param msg is the actual log message. + * @param file is the name of the file the message refers to. May be empty. + * @param line is the line in the above file at which the error occured. + * Ignored if negative. + * @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) + { + log(Severity::NOTE, msg, file, line, column); } /** @@ -278,7 +310,23 @@ public: */ void note(const std::string &msg, int line = -1, int column = -1) { - log(Severity::NOTE, msg, line, column); + note(msg, currentFilename(), line, column); + } + + /** + * Logs a warning. The file name is set to the topmost file name on + * the file name stack. + * + * @param msg is the actual log message. + * @param file is the name of the file the message refers to. May be empty. + * @param line is the line in the above file at which the error occured. + * Ignored if negative. + * @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) + { + log(Severity::WARNING, msg, file, line, column); } /** @@ -293,7 +341,23 @@ public: */ void warning(const std::string &msg, int line = -1, int column = -1) { - log(Severity::WARNING, msg, line, column); + warning(msg, currentFilename(), line, column); + } + + /** + * Logs an error message. The file name is set to the topmost file name on + * the file name stack. + * + * @param msg is the actual log message. + * @param file is the name of the file the message refers to. May be empty. + * @param line is the line in the above file at which the error occured. + * Ignored if negative. + * @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) + { + log(Severity::ERROR, msg, file, line, column); } /** @@ -308,7 +372,23 @@ public: */ void error(const std::string &msg, int line = -1, int column = -1) { - log(Severity::ERROR, msg, line, column); + error(msg, currentFilename(), line, column); + } + + /** + * Logs a fatal error. The file name is set to the topmost file name on + * the file name stack. + * + * @param msg is the actual log message. + * @param file is the name of the file the message refers to. May be empty. + * @param line is the line in the above file at which the error occured. + * Ignored if negative. + * @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) + { + log(Severity::FATAL_ERROR, msg, file, line, column); } /** @@ -323,7 +403,7 @@ public: */ void fatalError(const std::string &msg, int line = -1, int column = -1) { - log(Severity::FATAL_ERROR, msg, line, column); + fatalError(msg, currentFilename(), line, column); } /** diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp new file mode 100644 index 0000000..1961b35 --- /dev/null +++ b/src/core/Registry.cpp @@ -0,0 +1,47 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +namespace ousia { + +using namespace parser; + +/* Class Registry */ + +void Registry::registerParser(parser::Parser *parser) +{ + parsers.push_back(parser); + for (const auto &mime : parser.mimetypes()) { + parserMimetypes.insert(std::make_pair(mime, parser)); + } +} + +Parser* Registry::getParserForMimetype(const std::string &mimetype) +{ + const auto it = parserMimetypes.find(mimetype); + if (it != parserMimetypes.end()) { + return it->second; + } + return nullptr; +} + +} + diff --git a/src/core/Registry.hpp b/src/core/Registry.hpp new file mode 100644 index 0000000..235e427 --- /dev/null +++ b/src/core/Registry.hpp @@ -0,0 +1,51 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _OUSIA_REGISTRY_HPP_ +#define _OUSIA_REGISTRY_HPP_ + +#include +#include + +namespace ousia { + +// TODO: Add support for ScriptEngine type + +class Logger; + +namespace parser { +class Parser; +} + +class Registry { +private: + Logger &logger; + std::vector parsers; + std::map parserMimetypes; + +public: + Registry(Logger &logger) : logger(logger) {} + + void registerParser(parser::Parser *parser); + + parser::Parser *getParserForMimetype(std::string mimetype); +}; +} + +#endif /* _OUSIA_REGISTRY_HPP_ */ + diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp index bc98ac0..23fd9b7 100644 --- a/src/core/parser/Parser.cpp +++ b/src/core/parser/Parser.cpp @@ -21,12 +21,13 @@ #include "Parser.hpp" namespace ousia { +namespace parser { -Rooted Parser::parse(const std::string &str, Handle context, Logger &logger) +Rooted Parser::parse(const std::string &str, ParserContext &ctx) { - std::istringstream is(str); - return parse(is, context, logger); + std::istringstream is{str}; + return parse(is, ctx); +} } - } diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp index 74a1988..b8faf98 100644 --- a/src/core/parser/Parser.hpp +++ b/src/core/parser/Parser.hpp @@ -19,7 +19,7 @@ /** * @file Parser.hpp * - * Contains the abstract "Parser" class. Parsers are objects capable of reading + * Contains the abstract Parser class. Parsers are objects capable of reading * a certain file format and transforming it into a node. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) @@ -32,11 +32,15 @@ #include #include -#include "Exceptions.hpp" -#include "Node.hpp" -#include "Logger.hpp" +#include +#include +#include +#include + +#include "Scope.hpp" namespace ousia { +namespace parser { // TODO: Implement a proper Mimetype class @@ -48,6 +52,40 @@ public: using LoggableException::LoggableException; }; +/** + * Struct containing the objects that are passed to a parser instance. + */ +struct ParserContext { + /** + * Reference to the Scope instance that should be used within the parser. + */ + Scope &scope; + + /** + * Reference to the Registry instance that should be used within the parser. + */ + Registry ®istry; + + /** + * Reference to the Logger the parser should log any messages to. + */ + Logger &logger; + + /** + * Constructor of the ParserContext class. + * + * @param scope is a reference to the Scope instance that should be used to + * lookup names. + * @param registry is a reference at the Registry class, which allows to + * obtain references at parsers for other formats or script engine + * implementations. + * @param logger is a reference to the Logger instance that should be used + * to log error messages and warnings that occur while parsing the document. + */ + ParserContext(Scope &scope, Registry ®istry, Logger &logger) + : scope(scope), registry(registry), logger(logger){}; +}; + /** * Abstract parser class. This class builds the basic interface that should be * used by any parser which reads data from an input stream and transforms it @@ -55,9 +93,8 @@ public: */ class Parser { public: - - Parser() {}; - Parser(const Parser&) = delete; + Parser(){}; + Parser(const Parser &) = delete; /** * Returns a set containing all mime types supported by the parser. The mime @@ -78,18 +115,14 @@ public: * derived classes. * * @param is is a reference to the input stream that should be parsed. - * @param context defines the context in which the input stream should be - * parsed. The context represents the scope from which element names should - * be looked up. - * @param logger is a reference to the Logger instance that should be used - * to log error messages and warnings that occur while parsing the document. + * @param ctx is a reference to the context that should be used while + * parsing the document. * @return a reference to the node representing the subgraph that has been * created. The resulting node may point at not yet resolved entities, the * calling code will try to resolve these. If no valid node can be produced, * a corresponding LoggableException must be thrown by the parser. */ - virtual Rooted parse(std::istream &is, Handle context, - Logger &logger) = 0; + virtual Rooted parse(std::istream &is, ParserContext &ctx) = 0; /** * Parses the given string and returns a corresponding node for @@ -97,20 +130,17 @@ public: * derived classes. * * @param str is the string that should be parsed. - * @param context defines the context in which the input stream should be - * parsed. The context represents the scope from which element names should - * be looked up. - * @param logger is a reference to the Logger instance that should be used - * to log error messages and warnings that occur while parsing the document. + * @param ctx is a reference to the context that should be used while + * parsing the document. * @return a reference to the node representing the subgraph that has been * created. The resulting node may point at not yet resolved entities, the * calling code will try to resolve these. If no valid node can be produced, * a corresponding ParserException must be thrown by the parser. */ - Rooted parse(const std::string &str, Handle context, - Logger &logger); + Rooted parse(const std::string &str, ParserContext &ctx); }; } +} #endif /* _OUSIA_PARSER_HPP_ */ diff --git a/src/core/parser/Scope.cpp b/src/core/parser/Scope.cpp new file mode 100644 index 0000000..a60ade0 --- /dev/null +++ b/src/core/parser/Scope.cpp @@ -0,0 +1,26 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "Scope.hpp" + +namespace ousia { +namespace parser { + + +} +} diff --git a/src/core/parser/Scope.hpp b/src/core/parser/Scope.hpp new file mode 100644 index 0000000..0c37fbd --- /dev/null +++ b/src/core/parser/Scope.hpp @@ -0,0 +1,170 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _OUSIA_PARSER_SCOPE_H_ +#define _OUSIA_PARSER_SCOPE_H_ + +#include + +/** + * @file Scope.hpp + * + * Contains the Scope class used for resolving references based on the current + * parser state. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +namespace ousia { +namespace parser { + +class Scope; + +/** + * The ScopedScope class takes care of pushing a Node instance into the + * name resolution stack of a Scope instance and poping this node once the + * ScopedScope instance is deletes. This way you cannot forget to pop a Node + * from a Scope instance as this operation is performed automatically. + */ +class ScopedScope { +private: + /** + * Reference at the backing scope instance. + */ + Scope *scope; + +public: + /** + * Creates a new ScopedScope instance. + * + * @param scope is the backing Scope instance. + * @param node is the Node instance that should be poped onto the stack of + * the Scope instance. + */ + ScopedScope(Scope *scope, Handle node); + + /** + * Pops the Node given in the constructor form the stack of the Scope + * instance. + */ + ~ScopedScope(); + + /** + * Copying a ScopedScope is invalid. + */ + ScopedScope(const ScopedScope &) = delete; + + /** + * Move constructor of the ScopedScope class. + */ + ScopedScope(ScopedScope &&); + + /** + * Provides access at the underlying Scope instance. + */ + Scope *operator->() { return scope; } + + /** + * Provides access at the underlying Scope instance. + */ + Scope &operator*() { return *scope; } +}; + +/** + * Provides an interface for document parsers to resolve references based on the + * current position in the created document tree. The Scope class itself is + * represented as a chain of Scope objects where each element has a reference to + * a Node object attached to it. The descend method can be used to add a new + * scope element to the chain. + */ +class Scope { +private: + std::deque> nodes; + +public: + /** + * Constructor of the Scope class. + * + * @param rootNode is the top-most Node from which elements can be looked + * up. + */ + Scope(Handle rootNode) { nodes.push_back(rootNode); } + + /** + * Returns a reference at the Manager instance all nodes belong to. + */ + Manager &getManager() { return getRoot()->getManager(); } + + /** + * Pushes a new node onto the scope. + * + * @param node is the node that should be used for local lookup. + */ + void push(Handle node) { nodes.push_back(node); } + + /** + * Removes the last pushed node from the scope. + */ + void pop() { nodes.pop_back(); } + + /** + * Returns a ScopedScope instance, which automatically pushes the given node + * into the Scope stack and pops it once the ScopedScope is destroyed. + */ + ScopedScope descend(Handle node) { return ScopedScope{this, node}; } + + /** + * Returns the top-most Node instance in the Scope hirarchy. + * + * @return a reference at the root node. + */ + Rooted getRoot() { return nodes.front(); } + + /** + * Returns the bottom-most Node instance in the Scope hirarchy, e.g. the + * node that was pushed last onto the stack. + * + * @return a reference at the leaf node. + */ + Rooted getLeaf() { return nodes.back(); } +}; + +/* Class ScopedScope -- inline declaration of some methods */ + +inline ScopedScope::ScopedScope(Scope *scope, Handle node) : scope(scope) +{ + scope->push(node); +} + +inline ScopedScope::~ScopedScope() +{ + if (scope) { + scope->pop(); + } +} + +inline ScopedScope::ScopedScope(ScopedScope &&s) +{ + scope = s.scope; + s.scope = nullptr; +} +} +} + +#endif /* _OUSIA_PARSER_SCOPE_H_ */ + diff --git a/src/plugins/mozjs/MozJsScriptEngine.cpp b/src/plugins/mozjs/MozJsScriptEngine.cpp index f269eb7..47394a0 100644 --- a/src/plugins/mozjs/MozJsScriptEngine.cpp +++ b/src/plugins/mozjs/MozJsScriptEngine.cpp @@ -27,6 +27,7 @@ namespace ousia { namespace script { +namespace mozjs { /* * Some important links to the SpiderMonkey (mozjs) documentation: @@ -504,4 +505,5 @@ MozJsScriptEngineScope *MozJsScriptEngine::createScope() { } } } +} diff --git a/src/plugins/mozjs/MozJsScriptEngine.hpp b/src/plugins/mozjs/MozJsScriptEngine.hpp index 72e8ad7..385c676 100644 --- a/src/plugins/mozjs/MozJsScriptEngine.hpp +++ b/src/plugins/mozjs/MozJsScriptEngine.hpp @@ -46,6 +46,7 @@ typedef Rooted RootedValue; namespace ousia { namespace script { +namespace mozjs { class MozJsScriptEngineScope; @@ -121,6 +122,7 @@ public: }; } } +} #endif /* _MOZ_JS_SCRIPT_ENGINE_HPP_ */ diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index f9bb43e..f6891a8 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -16,25 +16,15 @@ along with this program. If not, see . */ +#include + #include #include "XmlParser.hpp" namespace ousia { - -/** - * The XmlParserData struct holds all information relevant to the expat callback - * functions. - */ -struct XmlParserData { - Rooted context; - Logger &logger; - - XmlParserData(Handle context, Logger &logger) - : context(context), logger(logger) - { - } -}; +namespace parser { +namespace xml { /** * Wrapper class around the XML_Parser pointer which safely frees it whenever @@ -55,13 +45,11 @@ public: * * @param encoding is the protocol-defined encoding passed to expat (or * nullptr if expat should determine the encoding by itself). - * @param namespaceSeparator is the separator used to separate the namespace - * components in the node name given by expat. */ - ScopedExpatXmlParser(const XML_Char *encoding, XML_Char namespaceSeparator) + ScopedExpatXmlParser(const XML_Char *encoding) : parser(nullptr) { - parser = XML_ParserCreateNS("UTF-8", ':'); + parser = XML_ParserCreate(encoding); if (!parser) { throw ParserException{ "Internal error: Could not create expat XML parser!"}; @@ -85,20 +73,43 @@ public: XML_Parser operator&() { return parser; } }; +static void xmlStartElementHandler(void *userData, const XML_Char *name, + const XML_Char **attrs) +{ + std::cout << "start tag: " << name << std::endl; + const XML_Char **attr = attrs; + while (*attr) { + std::cout << "\t" << *attr; + attr++; + std::cout << " -> " << *attr << std::endl; + attr++; + } +} + +static void xmlEndElementHandler(void *userData, const XML_Char *name) { + std::cout << "end tag: " << name << std::endl; +} + + +static void xmlCharacterDataHandler(void *userData, const XML_Char *s, int len) { + std::cout << "\tdata: " << std::string(s, len) << std::endl; +} + std::set XmlParser::mimetypes() { return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; } -Rooted XmlParser::parse(std::istream &is, Handle context, - Logger &logger) +Rooted XmlParser::parse(std::istream &is, ParserContext &ctx) { // Create the parser object - ScopedExpatXmlParser p{"UTF-8", ':'}; + ScopedExpatXmlParser p{"UTF-8"}; + XML_SetUserData(&p, &ctx); - // Set the callback functions, provide a pointer to a XmlParserData instance - // as user data. - XmlParserData ctx{context, logger}; + // Set the callback functions + XML_SetStartElementHandler(&p, xmlStartElementHandler); + XML_SetEndElementHandler(&p, xmlEndElementHandler); + XML_SetCharacterDataHandler(&p, xmlCharacterDataHandler); // Feed data into expat while there is data to process const std::streamsize BUFFER_SIZE = 4096; // TODO: Move to own header? @@ -118,8 +129,8 @@ Rooted XmlParser::parse(std::istream &is, Handle context, const int column = XML_GetCurrentColumnNumber(&p); const XML_Error code = XML_GetErrorCode(&p); const std::string msg = std::string{XML_ErrorString(code)}; - logger.error("XML: " + msg, line, column); - break; + throw ParserException{"XML Syntax Error: " + msg, line, column, + false}; } // Abort once there are no more bytes in the stream @@ -131,4 +142,6 @@ Rooted XmlParser::parse(std::istream &is, Handle context, return nullptr; } } +} +} diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp index f6fb060..b19af1e 100644 --- a/src/plugins/xml/XmlParser.hpp +++ b/src/plugins/xml/XmlParser.hpp @@ -28,9 +28,11 @@ #ifndef _OUSIA_XML_PARSER_HPP_ #define _OUSIA_XML_PARSER_HPP_ -#include +#include namespace ousia { +namespace parser { +namespace xml { /** * The XmlParser class implements parsing the various types of Ousía XML @@ -48,15 +50,19 @@ public: /** * Parses the given input stream as XML file and returns the parsed - * top-level node. Throws + * top-level node. * * @param is is the input stream that will be parsed. + * @param ctx is a reference to the ParserContext instance that should be + * used. */ - Rooted parse(std::istream &is, Handle context, - Logger &logger) override; + Rooted parse(std::istream &is, ParserContext &ctx) override; using Parser::parse; }; + +} +} } #endif /* _OUSIA_XML_PARSER_HPP_ */ diff --git a/src/plugins/xml/XmlStates.cpp b/src/plugins/xml/XmlStates.cpp new file mode 100644 index 0000000..ec0f002 --- /dev/null +++ b/src/plugins/xml/XmlStates.cpp @@ -0,0 +1,53 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "XmlStates.hpp" + +namespace ousia { +namespace parser { +namespace xml { + +std::set StateStack::expectedCommands(State state) +{ + std::set res; + for (const auto &v: handlers) { + if (v.second.parentStates.count(state)) { + res.insert(v.first); + } + } + return res; +} + +void StateStack::start(std::string tagName, char **attrs) { + // Fetch the current handler and the current state + const Handler *h = stack.empty() ? nullptr : stack.top(); + const State currentState = h ? State::NONE : h->state; + + // Fetch all handlers for the given tagName + auto range = handlers.equal_range(tagName); + if (range->first == handlers.end()) { + // There are no handlers registered for this tag name -- check whether + // the current handler supports arbitrary children + if (h && h->arbitraryChildren) + } +} + +} +} +} + diff --git a/src/plugins/xml/XmlStates.hpp b/src/plugins/xml/XmlStates.hpp new file mode 100644 index 0000000..70e95f4 --- /dev/null +++ b/src/plugins/xml/XmlStates.hpp @@ -0,0 +1,301 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _OUSIA_XML_STATES_HPP_ +#define _OUSIA_XML_STATES_HPP_ + +#include + +#include +#include +#include +#include + +namespace ousia { +namespace parser { + +class Scope; +class Registry; +class Logger; + +namespace xml { + +/** + * The State class represents all states the XML parser can be in. These states + * mostly represent single tags. + */ +enum class State : uint8_t { + /* Meta states */ + ALL = -1, + + /* Start state */ + NONE, + + /* Special commands */ + INCLUDE, + INLINE, + + /* Document states */ + DOCUMENT, + HEAD, + BODY, + + /* Domain description states */ + DOMAIN, + + /* Type system states */ + TYPESYSTEM, + TYPE, + TYPE_ELEM +}; + +/** + * The handler class provides a context for handling an XML tag. It has to be + * overridden and registered in the StateStack class to form handlers for + * concrete XML tags. + */ +class Handler { +private: + Rooted node; + +protected: + void setNode(Handle node) { this->node = node; } + +public: + /** + * Reference to the ParserContext instance that should be used to resolve + * references to nodes in the Graph. + */ + const ParserContext &ctx; + + /** + * Contains the name of the tag that is being handled. + */ + const std::string name; + + /** + * Contains the current state of the state machine. + */ + const State state; + + /** + * Contains the state of the state machine when the parent node was handled. + */ + const State parentState; + + /** + * Set to true if the tag that is being handled is not the tag that was + * specified in the state machine but a child tag of that tag. + */ + const bool isChild; + + /** + * Constructor of the Handler class. + * + * @param ctx is the parser context the handler should be executed in. + * @param name is the name of the string. + * @param state is the state this handler was called for. + * @param parentState is the state of the parent command. + * @param isChild specifies whether this handler was called not for the + * command that was specified in the state machine but a child command. + */ + Handler(const ParserContext &ctx, std::string name, State state, + State parentState, bool isChild) + : ctx(ctx), + name(std::move(name)), + state(state), + parentState(parentState), + isChild(isChild){}; + + /** + * Virtual destructor. + */ + virtual ~Handler(); + + /** + * Returns the node instance that was created by the handler. + * + * @return the Node instance created by the handler. May be nullptr if no + * Node was created. + */ + Rooted getNode() { return node; } + + /** + * Called when the command that was specified in the constructor is + * instanciated. + * + * @param attrs contains the attributes that were specified for the command. + * TODO: Replace with StructInstance! + */ + virtual void start(char **attrs) = 0; + + /** + * Called whenever the command for which this handler + */ + virtual void end() = 0; + + /** + * Called whenever raw data (int the form of a string) is available for the + * Handler instance. + * + * TODO: Replace with std::string? + * + * @param data is a pointer at the character data that is available for the + * Handler instance. + */ + virtual void data(char *data, int len){}; + + /** + * Called whenever a direct child element was created and has ended. + * + * @param handler is a reference at the child Handler instance. + */ + virtual void child(Handler *handler){}; +}; + +/** + * HandlerConstructor is a function pointer type used to create concrete + * instances of the Handler class. + */ +using HandlerConstructor = Handler *(*)(const ParserContext &ctx, + std::string name, State state, + State parentState, bool isChild); + +/** + * The StateStack class is a pushdown automaton responsible for turning a + * command stream into a tree of Node instances. + */ +class StateStack { +public: + /** + * Used internlly by StateStack to store Handler instances and parameters + * from HandlerDescriptor that are not stored in the Handler instance + * itself. Instances of the HandlerInstance class can be created using the + * HandlerDescriptor "create" method. + */ + struct HandlerInstance { + /** + * Pointer at the actual handler instance. + */ + std::unique_ptr handler; + + /** + * Value of the arbitraryChildren flag stored in the HandlerDescriptor + * class. + */ + const bool arbitraryChildren; + + HandlerInstance(std::unique_ptr handler, + bool arbitraryChildren) + : handler(handler), arbitraryChildren(arbitraryChildren) + { + } + } + + /** + * Used internally by StateStack to store the pushdown automaton + * description. + */ + struct HandlerDescriptor { + /** + * The valid parent states. + */ + const std::set parentStates; + + /** + * Pointer at a function which creates a new concrete Handler instance. + */ + const HandlerConstructor ctor; + + /** + * The target state for the registered handler. + */ + const State targetState; + + /** + * Set to true if this handler instance allows arbitrary children as + * tags. + */ + const bool arbitraryChildren; + + HandlerDescriptor(std::set parentStates, HandlerConstructor ctor, + State targetState, bool arbitraryChildren = false) + : parentStates(std::move(parentStates)), + ctor(constructor), + targetState(targetState), + arbitraryChildren(arbitraryChildren) + { + } + + HandlerInstance create(const ParserContext &ctx, std::string name, + State parentState, bool isChild) + { + return HandlerInstance{ + ctor(ctx, name, targetState, parentState, isChild), + arbitraryChildren}; + } + }; + +private: + /** + * Map containing all registered command names and the corresponding + * handler + * descriptor. + */ + const std::multimap handlers; + + /** + * Reference at the parser context. + */ + const ParserContext &ctx; + + /** + * Internal stack used for managing the currently active Handler instances. + */ + std::stack stack; + + /** + * Used internally to get all expected command names for the given state + * (does not work if the current Handler instance allows arbitrary + * children). This function is used to build error messages. + * + * @param state is the state for which all expected command names should be + * returned. + */ + std::set expectedCommands(State state); + +public: + /** + * Creates a new instance of the StateStack class. + * + * @param handlers is a map containing the command names and the + * corresponding HandlerDescriptor instances. + */ + StateStack(const ParserContext &ctx, + std::multimap handlers) + : handlers(std::move(handlers)), + ctx(ctx), + currentState(State::NONE), + arbitraryChildren(false); +}; +} +} +} + +#endif /* _OUSIA_XML_STATES_HPP_ */ + -- cgit v1.2.3 From 082165d21269123f2658edc74aa1960669e306c8 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Mon, 1 Dec 2014 19:03:15 +0100 Subject: first version of the ParserStack class --- src/core/parser/XmlStates.cpp | 53 ++++++++ src/core/parser/XmlStates.hpp | 301 ++++++++++++++++++++++++++++++++++++++++++ src/plugins/xml/XmlStates.cpp | 53 -------- src/plugins/xml/XmlStates.hpp | 301 ------------------------------------------ 4 files changed, 354 insertions(+), 354 deletions(-) create mode 100644 src/core/parser/XmlStates.cpp create mode 100644 src/core/parser/XmlStates.hpp delete mode 100644 src/plugins/xml/XmlStates.cpp delete mode 100644 src/plugins/xml/XmlStates.hpp (limited to 'src') diff --git a/src/core/parser/XmlStates.cpp b/src/core/parser/XmlStates.cpp new file mode 100644 index 0000000..ec0f002 --- /dev/null +++ b/src/core/parser/XmlStates.cpp @@ -0,0 +1,53 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "XmlStates.hpp" + +namespace ousia { +namespace parser { +namespace xml { + +std::set StateStack::expectedCommands(State state) +{ + std::set res; + for (const auto &v: handlers) { + if (v.second.parentStates.count(state)) { + res.insert(v.first); + } + } + return res; +} + +void StateStack::start(std::string tagName, char **attrs) { + // Fetch the current handler and the current state + const Handler *h = stack.empty() ? nullptr : stack.top(); + const State currentState = h ? State::NONE : h->state; + + // Fetch all handlers for the given tagName + auto range = handlers.equal_range(tagName); + if (range->first == handlers.end()) { + // There are no handlers registered for this tag name -- check whether + // the current handler supports arbitrary children + if (h && h->arbitraryChildren) + } +} + +} +} +} + diff --git a/src/core/parser/XmlStates.hpp b/src/core/parser/XmlStates.hpp new file mode 100644 index 0000000..70e95f4 --- /dev/null +++ b/src/core/parser/XmlStates.hpp @@ -0,0 +1,301 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _OUSIA_XML_STATES_HPP_ +#define _OUSIA_XML_STATES_HPP_ + +#include + +#include +#include +#include +#include + +namespace ousia { +namespace parser { + +class Scope; +class Registry; +class Logger; + +namespace xml { + +/** + * The State class represents all states the XML parser can be in. These states + * mostly represent single tags. + */ +enum class State : uint8_t { + /* Meta states */ + ALL = -1, + + /* Start state */ + NONE, + + /* Special commands */ + INCLUDE, + INLINE, + + /* Document states */ + DOCUMENT, + HEAD, + BODY, + + /* Domain description states */ + DOMAIN, + + /* Type system states */ + TYPESYSTEM, + TYPE, + TYPE_ELEM +}; + +/** + * The handler class provides a context for handling an XML tag. It has to be + * overridden and registered in the StateStack class to form handlers for + * concrete XML tags. + */ +class Handler { +private: + Rooted node; + +protected: + void setNode(Handle node) { this->node = node; } + +public: + /** + * Reference to the ParserContext instance that should be used to resolve + * references to nodes in the Graph. + */ + const ParserContext &ctx; + + /** + * Contains the name of the tag that is being handled. + */ + const std::string name; + + /** + * Contains the current state of the state machine. + */ + const State state; + + /** + * Contains the state of the state machine when the parent node was handled. + */ + const State parentState; + + /** + * Set to true if the tag that is being handled is not the tag that was + * specified in the state machine but a child tag of that tag. + */ + const bool isChild; + + /** + * Constructor of the Handler class. + * + * @param ctx is the parser context the handler should be executed in. + * @param name is the name of the string. + * @param state is the state this handler was called for. + * @param parentState is the state of the parent command. + * @param isChild specifies whether this handler was called not for the + * command that was specified in the state machine but a child command. + */ + Handler(const ParserContext &ctx, std::string name, State state, + State parentState, bool isChild) + : ctx(ctx), + name(std::move(name)), + state(state), + parentState(parentState), + isChild(isChild){}; + + /** + * Virtual destructor. + */ + virtual ~Handler(); + + /** + * Returns the node instance that was created by the handler. + * + * @return the Node instance created by the handler. May be nullptr if no + * Node was created. + */ + Rooted getNode() { return node; } + + /** + * Called when the command that was specified in the constructor is + * instanciated. + * + * @param attrs contains the attributes that were specified for the command. + * TODO: Replace with StructInstance! + */ + virtual void start(char **attrs) = 0; + + /** + * Called whenever the command for which this handler + */ + virtual void end() = 0; + + /** + * Called whenever raw data (int the form of a string) is available for the + * Handler instance. + * + * TODO: Replace with std::string? + * + * @param data is a pointer at the character data that is available for the + * Handler instance. + */ + virtual void data(char *data, int len){}; + + /** + * Called whenever a direct child element was created and has ended. + * + * @param handler is a reference at the child Handler instance. + */ + virtual void child(Handler *handler){}; +}; + +/** + * HandlerConstructor is a function pointer type used to create concrete + * instances of the Handler class. + */ +using HandlerConstructor = Handler *(*)(const ParserContext &ctx, + std::string name, State state, + State parentState, bool isChild); + +/** + * The StateStack class is a pushdown automaton responsible for turning a + * command stream into a tree of Node instances. + */ +class StateStack { +public: + /** + * Used internlly by StateStack to store Handler instances and parameters + * from HandlerDescriptor that are not stored in the Handler instance + * itself. Instances of the HandlerInstance class can be created using the + * HandlerDescriptor "create" method. + */ + struct HandlerInstance { + /** + * Pointer at the actual handler instance. + */ + std::unique_ptr handler; + + /** + * Value of the arbitraryChildren flag stored in the HandlerDescriptor + * class. + */ + const bool arbitraryChildren; + + HandlerInstance(std::unique_ptr handler, + bool arbitraryChildren) + : handler(handler), arbitraryChildren(arbitraryChildren) + { + } + } + + /** + * Used internally by StateStack to store the pushdown automaton + * description. + */ + struct HandlerDescriptor { + /** + * The valid parent states. + */ + const std::set parentStates; + + /** + * Pointer at a function which creates a new concrete Handler instance. + */ + const HandlerConstructor ctor; + + /** + * The target state for the registered handler. + */ + const State targetState; + + /** + * Set to true if this handler instance allows arbitrary children as + * tags. + */ + const bool arbitraryChildren; + + HandlerDescriptor(std::set parentStates, HandlerConstructor ctor, + State targetState, bool arbitraryChildren = false) + : parentStates(std::move(parentStates)), + ctor(constructor), + targetState(targetState), + arbitraryChildren(arbitraryChildren) + { + } + + HandlerInstance create(const ParserContext &ctx, std::string name, + State parentState, bool isChild) + { + return HandlerInstance{ + ctor(ctx, name, targetState, parentState, isChild), + arbitraryChildren}; + } + }; + +private: + /** + * Map containing all registered command names and the corresponding + * handler + * descriptor. + */ + const std::multimap handlers; + + /** + * Reference at the parser context. + */ + const ParserContext &ctx; + + /** + * Internal stack used for managing the currently active Handler instances. + */ + std::stack stack; + + /** + * Used internally to get all expected command names for the given state + * (does not work if the current Handler instance allows arbitrary + * children). This function is used to build error messages. + * + * @param state is the state for which all expected command names should be + * returned. + */ + std::set expectedCommands(State state); + +public: + /** + * Creates a new instance of the StateStack class. + * + * @param handlers is a map containing the command names and the + * corresponding HandlerDescriptor instances. + */ + StateStack(const ParserContext &ctx, + std::multimap handlers) + : handlers(std::move(handlers)), + ctx(ctx), + currentState(State::NONE), + arbitraryChildren(false); +}; +} +} +} + +#endif /* _OUSIA_XML_STATES_HPP_ */ + diff --git a/src/plugins/xml/XmlStates.cpp b/src/plugins/xml/XmlStates.cpp deleted file mode 100644 index ec0f002..0000000 --- a/src/plugins/xml/XmlStates.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "XmlStates.hpp" - -namespace ousia { -namespace parser { -namespace xml { - -std::set StateStack::expectedCommands(State state) -{ - std::set res; - for (const auto &v: handlers) { - if (v.second.parentStates.count(state)) { - res.insert(v.first); - } - } - return res; -} - -void StateStack::start(std::string tagName, char **attrs) { - // Fetch the current handler and the current state - const Handler *h = stack.empty() ? nullptr : stack.top(); - const State currentState = h ? State::NONE : h->state; - - // Fetch all handlers for the given tagName - auto range = handlers.equal_range(tagName); - if (range->first == handlers.end()) { - // There are no handlers registered for this tag name -- check whether - // the current handler supports arbitrary children - if (h && h->arbitraryChildren) - } -} - -} -} -} - diff --git a/src/plugins/xml/XmlStates.hpp b/src/plugins/xml/XmlStates.hpp deleted file mode 100644 index 70e95f4..0000000 --- a/src/plugins/xml/XmlStates.hpp +++ /dev/null @@ -1,301 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef _OUSIA_XML_STATES_HPP_ -#define _OUSIA_XML_STATES_HPP_ - -#include - -#include -#include -#include -#include - -namespace ousia { -namespace parser { - -class Scope; -class Registry; -class Logger; - -namespace xml { - -/** - * The State class represents all states the XML parser can be in. These states - * mostly represent single tags. - */ -enum class State : uint8_t { - /* Meta states */ - ALL = -1, - - /* Start state */ - NONE, - - /* Special commands */ - INCLUDE, - INLINE, - - /* Document states */ - DOCUMENT, - HEAD, - BODY, - - /* Domain description states */ - DOMAIN, - - /* Type system states */ - TYPESYSTEM, - TYPE, - TYPE_ELEM -}; - -/** - * The handler class provides a context for handling an XML tag. It has to be - * overridden and registered in the StateStack class to form handlers for - * concrete XML tags. - */ -class Handler { -private: - Rooted node; - -protected: - void setNode(Handle node) { this->node = node; } - -public: - /** - * Reference to the ParserContext instance that should be used to resolve - * references to nodes in the Graph. - */ - const ParserContext &ctx; - - /** - * Contains the name of the tag that is being handled. - */ - const std::string name; - - /** - * Contains the current state of the state machine. - */ - const State state; - - /** - * Contains the state of the state machine when the parent node was handled. - */ - const State parentState; - - /** - * Set to true if the tag that is being handled is not the tag that was - * specified in the state machine but a child tag of that tag. - */ - const bool isChild; - - /** - * Constructor of the Handler class. - * - * @param ctx is the parser context the handler should be executed in. - * @param name is the name of the string. - * @param state is the state this handler was called for. - * @param parentState is the state of the parent command. - * @param isChild specifies whether this handler was called not for the - * command that was specified in the state machine but a child command. - */ - Handler(const ParserContext &ctx, std::string name, State state, - State parentState, bool isChild) - : ctx(ctx), - name(std::move(name)), - state(state), - parentState(parentState), - isChild(isChild){}; - - /** - * Virtual destructor. - */ - virtual ~Handler(); - - /** - * Returns the node instance that was created by the handler. - * - * @return the Node instance created by the handler. May be nullptr if no - * Node was created. - */ - Rooted getNode() { return node; } - - /** - * Called when the command that was specified in the constructor is - * instanciated. - * - * @param attrs contains the attributes that were specified for the command. - * TODO: Replace with StructInstance! - */ - virtual void start(char **attrs) = 0; - - /** - * Called whenever the command for which this handler - */ - virtual void end() = 0; - - /** - * Called whenever raw data (int the form of a string) is available for the - * Handler instance. - * - * TODO: Replace with std::string? - * - * @param data is a pointer at the character data that is available for the - * Handler instance. - */ - virtual void data(char *data, int len){}; - - /** - * Called whenever a direct child element was created and has ended. - * - * @param handler is a reference at the child Handler instance. - */ - virtual void child(Handler *handler){}; -}; - -/** - * HandlerConstructor is a function pointer type used to create concrete - * instances of the Handler class. - */ -using HandlerConstructor = Handler *(*)(const ParserContext &ctx, - std::string name, State state, - State parentState, bool isChild); - -/** - * The StateStack class is a pushdown automaton responsible for turning a - * command stream into a tree of Node instances. - */ -class StateStack { -public: - /** - * Used internlly by StateStack to store Handler instances and parameters - * from HandlerDescriptor that are not stored in the Handler instance - * itself. Instances of the HandlerInstance class can be created using the - * HandlerDescriptor "create" method. - */ - struct HandlerInstance { - /** - * Pointer at the actual handler instance. - */ - std::unique_ptr handler; - - /** - * Value of the arbitraryChildren flag stored in the HandlerDescriptor - * class. - */ - const bool arbitraryChildren; - - HandlerInstance(std::unique_ptr handler, - bool arbitraryChildren) - : handler(handler), arbitraryChildren(arbitraryChildren) - { - } - } - - /** - * Used internally by StateStack to store the pushdown automaton - * description. - */ - struct HandlerDescriptor { - /** - * The valid parent states. - */ - const std::set parentStates; - - /** - * Pointer at a function which creates a new concrete Handler instance. - */ - const HandlerConstructor ctor; - - /** - * The target state for the registered handler. - */ - const State targetState; - - /** - * Set to true if this handler instance allows arbitrary children as - * tags. - */ - const bool arbitraryChildren; - - HandlerDescriptor(std::set parentStates, HandlerConstructor ctor, - State targetState, bool arbitraryChildren = false) - : parentStates(std::move(parentStates)), - ctor(constructor), - targetState(targetState), - arbitraryChildren(arbitraryChildren) - { - } - - HandlerInstance create(const ParserContext &ctx, std::string name, - State parentState, bool isChild) - { - return HandlerInstance{ - ctor(ctx, name, targetState, parentState, isChild), - arbitraryChildren}; - } - }; - -private: - /** - * Map containing all registered command names and the corresponding - * handler - * descriptor. - */ - const std::multimap handlers; - - /** - * Reference at the parser context. - */ - const ParserContext &ctx; - - /** - * Internal stack used for managing the currently active Handler instances. - */ - std::stack stack; - - /** - * Used internally to get all expected command names for the given state - * (does not work if the current Handler instance allows arbitrary - * children). This function is used to build error messages. - * - * @param state is the state for which all expected command names should be - * returned. - */ - std::set expectedCommands(State state); - -public: - /** - * Creates a new instance of the StateStack class. - * - * @param handlers is a map containing the command names and the - * corresponding HandlerDescriptor instances. - */ - StateStack(const ParserContext &ctx, - std::multimap handlers) - : handlers(std::move(handlers)), - ctx(ctx), - currentState(State::NONE), - arbitraryChildren(false); -}; -} -} -} - -#endif /* _OUSIA_XML_STATES_HPP_ */ - -- cgit v1.2.3 From 5554f3594d00e267af447a24149f655ceff64d17 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Mon, 1 Dec 2014 21:27:08 +0100 Subject: working version of the ParserStack class plus unit tests --- CMakeLists.txt | 3 + src/core/parser/Parser.hpp | 13 ++ src/core/parser/ParserStack.cpp | 155 +++++++++++++++++ src/core/parser/ParserStack.hpp | 315 +++++++++++++++++++++++++++++++++++ src/core/parser/Scope.hpp | 2 + src/core/parser/XmlStates.cpp | 53 ------ src/core/parser/XmlStates.hpp | 301 --------------------------------- test/core/parser/ParserStackTest.cpp | 165 ++++++++++++++++++ 8 files changed, 653 insertions(+), 354 deletions(-) create mode 100644 src/core/parser/ParserStack.cpp create mode 100644 src/core/parser/ParserStack.hpp delete mode 100644 src/core/parser/XmlStates.cpp delete mode 100644 src/core/parser/XmlStates.hpp create mode 100644 test/core/parser/ParserStackTest.cpp (limited to 'src') diff --git a/CMakeLists.txt b/CMakeLists.txt index da6479b..327f664 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,8 @@ ADD_LIBRARY(ousia_core # src/core/Typesystem src/core/Utils src/core/parser/Parser + src/core/parser/ParserStack + src/core/parser/Scope src/core/script/Function src/core/script/Object src/core/script/ScriptEngine @@ -156,6 +158,7 @@ IF(TEST) test/core/RangeSetTest test/core/TokenizerTest test/core/UtilsTest + test/core/parser/ParserStackTest test/core/script/FunctionTest test/core/script/ObjectTest test/core/script/VariantTest diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp index b8faf98..fa5dd49 100644 --- a/src/core/parser/Parser.hpp +++ b/src/core/parser/Parser.hpp @@ -86,6 +86,19 @@ struct ParserContext { : scope(scope), registry(registry), logger(logger){}; }; +struct StandaloneParserContext : public ParserContext { +private: + Logger logger; + Scope scope; + Registry registry; + +public: + StandaloneParserContext() + : ParserContext(scope, registry, logger), + scope(nullptr), + registry(logger){}; +}; + /** * Abstract parser class. This class builds the basic interface that should be * used by any parser which reads data from an input stream and transforms it diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp new file mode 100644 index 0000000..01fce3f --- /dev/null +++ b/src/core/parser/ParserStack.cpp @@ -0,0 +1,155 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include "ParserStack.hpp" + +#include + +namespace ousia { +namespace parser { + +/* Class HandlerDescriptor */ + +HandlerInstance HandlerDescriptor::create(const ParserContext &ctx, + std::string name, State parentState, + bool isChild, char **attrs) const +{ + Handler *h = ctor(ctx, name, targetState, parentState, isChild); + h->start(attrs); + return HandlerInstance(h, this); +} + +/* Class ParserStack */ + +/** + * Function used internally to turn the elements of a collection into a string + * separated by the given delimiter. + */ +template +static std::string join(T es, const std::string &delim) +{ + std::stringstream res; + bool first = true; + for (auto &e : es) { + if (!first) { + res << delim; + } + res << e; + first = false; + } + return res.str(); +} + +/** + * Returns an Exception that should be thrown when a currently invalid command + * is thrown. + */ +static LoggableException invalidCommand(const std::string &name, + const std::set &expected) +{ + if (expected.empty()) { + return LoggableException{ + std::string{"No nested elements allowed, but got \""} + name + + std::string{"\""}}; + } else { + return LoggableException{ + std::string{"Expected "} + + (expected.size() == 1 ? std::string{"\""} + : std::string{"one of \""}) + + join(expected, "\", \"") + std::string{"\", but got \""} + name + + std::string{"\""}}; + } +} + +std::set ParserStack::expectedCommands(State state) +{ + std::set res; + for (const auto &v : handlers) { + if (v.second.parentStates.count(state)) { + res.insert(v.first); + } + } + return res; +} + +void ParserStack::start(std::string name, char **attrs) +{ + // Fetch the current handler and the current state + const HandlerInstance *h = stack.empty() ? nullptr : &stack.top(); + const State curState = currentState(); + bool isChild = false; + + // Fetch the correct Handler descriptor for this + const HandlerDescriptor *descr = nullptr; + auto range = handlers.equal_range(name); + for (auto it = range.first; it != range.second; it++) { + if (it->second.parentStates.count(curState)) { + descr = &(it->second); + break; + } + } + if (!descr && currentArbitraryChildren()) { + isChild = true; + descr = h->descr; + } + + // No descriptor found, throw an exception. + if (!descr) { + throw invalidCommand(name, expectedCommands(curState)); + } + + // Instantiate the handler and call its start function + stack.emplace(descr->create(ctx, name, curState, isChild, attrs)); +} + +void ParserStack::end() +{ + // Check whether the current command could be ended + if (stack.empty()) { + throw LoggableException{"No command to end."}; + } + + // Remove the current HandlerInstance from the stack + HandlerInstance inst{stack.top()}; + stack.pop(); + + // Call the end function of the last Handler + inst.handler->end(); + + // Call the "child" function of the parent Handler in the stack + // (if one exists). + if (!stack.empty()) { + stack.top().handler->child(inst.handler); + } +} + +void ParserStack::data(const char *data, int len) +{ + // Check whether there is any command the data can be sent to + if (stack.empty()) { + throw LoggableException{"No command to receive data."}; + } + + // Pass the data to the current Handler instance + stack.top().handler->data(data, len); +} +} +} + diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp new file mode 100644 index 0000000..a777b1e --- /dev/null +++ b/src/core/parser/ParserStack.hpp @@ -0,0 +1,315 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file ParserStack.hpp + * + * Helper classes for document or description parsers. Contains the ParserStack + * class, which is an pushdown automaton responsible for accepting commands in + * the correct order and calling specified handlers. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_PARSER_STACK_HPP_ +#define _OUSIA_PARSER_STACK_HPP_ + +#include + +#include +#include +#include +#include +#include + +#include "Parser.hpp" + +namespace ousia { +namespace parser { + +/** + * The State type alias is used to + */ +using State = int8_t; + +static const State STATE_ALL = -2; +static const State STATE_NONE = -1; + +/** + * The handler class provides a context for handling an XML tag. It has to be + * overridden and registered in the StateStack class to form handlers for + * concrete XML tags. + */ +class Handler { +private: + Rooted node; + +protected: + void setNode(Handle node) { this->node = node; } + +public: + /** + * Reference to the ParserContext instance that should be used to resolve + * references to nodes in the Graph. + */ + const ParserContext &ctx; + + /** + * Contains the name of the tag that is being handled. + */ + const std::string name; + + /** + * Contains the current state of the state machine. + */ + const State state; + + /** + * Contains the state of the state machine when the parent node was handled. + */ + const State parentState; + + /** + * Set to true if the tag that is being handled is not the tag that was + * specified in the state machine but a child tag of that tag. + */ + const bool isChild; + + /** + * Constructor of the Handler class. + * + * @param ctx is the parser context the handler should be executed in. + * @param name is the name of the string. + * @param state is the state this handler was called for. + * @param parentState is the state of the parent command. + * @param isChild specifies whether this handler was called not for the + * command that was specified in the state machine but a child command. + */ + Handler(const ParserContext &ctx, std::string name, State state, + State parentState, bool isChild) + : ctx(ctx), + name(std::move(name)), + state(state), + parentState(parentState), + isChild(isChild){}; + + /** + * Virtual destructor. + */ + virtual ~Handler(){}; + + /** + * Returns the node instance that was created by the handler. + * + * @return the Node instance created by the handler. May be nullptr if no + * Node was created. + */ + Rooted getNode() { return node; } + + /** + * Called when the command that was specified in the constructor is + * instanciated. + * + * @param attrs contains the attributes that were specified for the command. + * TODO: Replace with StructInstance! + */ + virtual void start(char **attrs) = 0; + + /** + * Called whenever the command for which this handler + */ + virtual void end() = 0; + + /** + * Called whenever raw data (int the form of a string) is available for the + * Handler instance. + * + * TODO: Replace with std::string? + * + * @param data is a pointer at the character data that is available for the + * Handler instance. + */ + virtual void data(const char *data, int len){}; + + /** + * Called whenever a direct child element was created and has ended. + * + * @param handler is a reference at the child Handler instance. + */ + virtual void child(std::shared_ptr handler){}; +}; + +/** + * HandlerConstructor is a function pointer type used to create concrete + * instances of the Handler class. + */ +using HandlerConstructor = Handler *(*)(const ParserContext &ctx, + std::string name, State state, + State parentState, bool isChild); + +struct HandlerDescriptor; + +/** + * Used internlly by StateStack to store Handler instances and parameters + * from HandlerDescriptor that are not stored in the Handler instance + * itself. Instances of the HandlerInstance class can be created using the + * HandlerDescriptor "create" method. + */ +struct HandlerInstance { + /** + * Pointer at the actual handler instance. + */ + std::shared_ptr handler; + + const HandlerDescriptor *descr; + + HandlerInstance(Handler *handler, const HandlerDescriptor *descr) + : handler(handler), descr(descr) + { + } +}; + +/** + * Used internally by StateStack to store the pushdown automaton + * description. + */ +struct HandlerDescriptor { + /** + * The valid parent states. + */ + const std::set parentStates; + + /** + * Pointer at a function which creates a new concrete Handler instance. + */ + const HandlerConstructor ctor; + + /** + * The target state for the registered handler. + */ + const State targetState; + + /** + * Set to true if this handler instance allows arbitrary children as + * tags. + */ + const bool arbitraryChildren; + + HandlerDescriptor(std::set parentStates, HandlerConstructor ctor, + State targetState, bool arbitraryChildren = false) + : parentStates(std::move(parentStates)), + ctor(ctor), + targetState(targetState), + arbitraryChildren(arbitraryChildren) + { + } + + /** + * Creates an instance of the concrete Handler class represented by the + * HandlerDescriptor and calls its start function. + */ + HandlerInstance create(const ParserContext &ctx, std::string name, + State parentState, bool isChild, char **attrs) const; +}; + +/** + * The ParserStack class is a pushdown automaton responsible for turning a + * command stream into a tree of Node instances. + */ +class ParserStack { +private: + /** + * Reference at the parser context. + */ + const ParserContext &ctx; + + /** + * Map containing all registered command names and the corresponding + * handler + * descriptor. + */ + const std::multimap &handlers; + + /** + * Internal stack used for managing the currently active Handler instances. + */ + std::stack stack; + + /** + * Used internally to get all expected command names for the given state + * (does not work if the current Handler instance allows arbitrary + * children). This function is used to build error messages. + * + * @param state is the state for which all expected command names should be + * returned. + */ + std::set expectedCommands(State state); + +public: + /** + * Creates a new instance of the ParserStack class. + * + * @param handlers is a map containing the command names and the + * corresponding HandlerDescriptor instances. + */ + ParserStack(const ParserContext &ctx, + const std::multimap &handlers) + : ctx(ctx), handlers(handlers){}; + + /** + * Returns the state the ParserStack instance currently is in. + * + * @return the state of the currently active Handler instance or STATE_NONE + * if no handler is on the stack. + */ + State currentState() { + return stack.empty() ? STATE_NONE : stack.top().handler->state; + } + + /** + * Returns the command name that is currently being handled. + * + * @return the name of the command currently being handled by the active + * Handler instance or an empty string if no handler is currently active. + */ + std::string currentName() { + return stack.empty() ? std::string{} : stack.top().handler->name; + } + + /** + * Returns whether the current command handler allows arbitrary children. + * + * @return true if the handler allows arbitrary children, false otherwise. + */ + bool currentArbitraryChildren() { + return stack.empty() ? false : stack.top().descr->arbitraryChildren; + } + + // TODO: Change signature + void start(std::string name, char **attrs); + + void end(); + + // TODO: Change signature + void data(const char *data, int len); +}; +} +} + +#endif /* _OUSIA_PARSER_STACK_HPP_ */ + diff --git a/src/core/parser/Scope.hpp b/src/core/parser/Scope.hpp index 0c37fbd..9c5504f 100644 --- a/src/core/parser/Scope.hpp +++ b/src/core/parser/Scope.hpp @@ -19,6 +19,8 @@ #ifndef _OUSIA_PARSER_SCOPE_H_ #define _OUSIA_PARSER_SCOPE_H_ +#include + #include /** diff --git a/src/core/parser/XmlStates.cpp b/src/core/parser/XmlStates.cpp deleted file mode 100644 index ec0f002..0000000 --- a/src/core/parser/XmlStates.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "XmlStates.hpp" - -namespace ousia { -namespace parser { -namespace xml { - -std::set StateStack::expectedCommands(State state) -{ - std::set res; - for (const auto &v: handlers) { - if (v.second.parentStates.count(state)) { - res.insert(v.first); - } - } - return res; -} - -void StateStack::start(std::string tagName, char **attrs) { - // Fetch the current handler and the current state - const Handler *h = stack.empty() ? nullptr : stack.top(); - const State currentState = h ? State::NONE : h->state; - - // Fetch all handlers for the given tagName - auto range = handlers.equal_range(tagName); - if (range->first == handlers.end()) { - // There are no handlers registered for this tag name -- check whether - // the current handler supports arbitrary children - if (h && h->arbitraryChildren) - } -} - -} -} -} - diff --git a/src/core/parser/XmlStates.hpp b/src/core/parser/XmlStates.hpp deleted file mode 100644 index 70e95f4..0000000 --- a/src/core/parser/XmlStates.hpp +++ /dev/null @@ -1,301 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef _OUSIA_XML_STATES_HPP_ -#define _OUSIA_XML_STATES_HPP_ - -#include - -#include -#include -#include -#include - -namespace ousia { -namespace parser { - -class Scope; -class Registry; -class Logger; - -namespace xml { - -/** - * The State class represents all states the XML parser can be in. These states - * mostly represent single tags. - */ -enum class State : uint8_t { - /* Meta states */ - ALL = -1, - - /* Start state */ - NONE, - - /* Special commands */ - INCLUDE, - INLINE, - - /* Document states */ - DOCUMENT, - HEAD, - BODY, - - /* Domain description states */ - DOMAIN, - - /* Type system states */ - TYPESYSTEM, - TYPE, - TYPE_ELEM -}; - -/** - * The handler class provides a context for handling an XML tag. It has to be - * overridden and registered in the StateStack class to form handlers for - * concrete XML tags. - */ -class Handler { -private: - Rooted node; - -protected: - void setNode(Handle node) { this->node = node; } - -public: - /** - * Reference to the ParserContext instance that should be used to resolve - * references to nodes in the Graph. - */ - const ParserContext &ctx; - - /** - * Contains the name of the tag that is being handled. - */ - const std::string name; - - /** - * Contains the current state of the state machine. - */ - const State state; - - /** - * Contains the state of the state machine when the parent node was handled. - */ - const State parentState; - - /** - * Set to true if the tag that is being handled is not the tag that was - * specified in the state machine but a child tag of that tag. - */ - const bool isChild; - - /** - * Constructor of the Handler class. - * - * @param ctx is the parser context the handler should be executed in. - * @param name is the name of the string. - * @param state is the state this handler was called for. - * @param parentState is the state of the parent command. - * @param isChild specifies whether this handler was called not for the - * command that was specified in the state machine but a child command. - */ - Handler(const ParserContext &ctx, std::string name, State state, - State parentState, bool isChild) - : ctx(ctx), - name(std::move(name)), - state(state), - parentState(parentState), - isChild(isChild){}; - - /** - * Virtual destructor. - */ - virtual ~Handler(); - - /** - * Returns the node instance that was created by the handler. - * - * @return the Node instance created by the handler. May be nullptr if no - * Node was created. - */ - Rooted getNode() { return node; } - - /** - * Called when the command that was specified in the constructor is - * instanciated. - * - * @param attrs contains the attributes that were specified for the command. - * TODO: Replace with StructInstance! - */ - virtual void start(char **attrs) = 0; - - /** - * Called whenever the command for which this handler - */ - virtual void end() = 0; - - /** - * Called whenever raw data (int the form of a string) is available for the - * Handler instance. - * - * TODO: Replace with std::string? - * - * @param data is a pointer at the character data that is available for the - * Handler instance. - */ - virtual void data(char *data, int len){}; - - /** - * Called whenever a direct child element was created and has ended. - * - * @param handler is a reference at the child Handler instance. - */ - virtual void child(Handler *handler){}; -}; - -/** - * HandlerConstructor is a function pointer type used to create concrete - * instances of the Handler class. - */ -using HandlerConstructor = Handler *(*)(const ParserContext &ctx, - std::string name, State state, - State parentState, bool isChild); - -/** - * The StateStack class is a pushdown automaton responsible for turning a - * command stream into a tree of Node instances. - */ -class StateStack { -public: - /** - * Used internlly by StateStack to store Handler instances and parameters - * from HandlerDescriptor that are not stored in the Handler instance - * itself. Instances of the HandlerInstance class can be created using the - * HandlerDescriptor "create" method. - */ - struct HandlerInstance { - /** - * Pointer at the actual handler instance. - */ - std::unique_ptr handler; - - /** - * Value of the arbitraryChildren flag stored in the HandlerDescriptor - * class. - */ - const bool arbitraryChildren; - - HandlerInstance(std::unique_ptr handler, - bool arbitraryChildren) - : handler(handler), arbitraryChildren(arbitraryChildren) - { - } - } - - /** - * Used internally by StateStack to store the pushdown automaton - * description. - */ - struct HandlerDescriptor { - /** - * The valid parent states. - */ - const std::set parentStates; - - /** - * Pointer at a function which creates a new concrete Handler instance. - */ - const HandlerConstructor ctor; - - /** - * The target state for the registered handler. - */ - const State targetState; - - /** - * Set to true if this handler instance allows arbitrary children as - * tags. - */ - const bool arbitraryChildren; - - HandlerDescriptor(std::set parentStates, HandlerConstructor ctor, - State targetState, bool arbitraryChildren = false) - : parentStates(std::move(parentStates)), - ctor(constructor), - targetState(targetState), - arbitraryChildren(arbitraryChildren) - { - } - - HandlerInstance create(const ParserContext &ctx, std::string name, - State parentState, bool isChild) - { - return HandlerInstance{ - ctor(ctx, name, targetState, parentState, isChild), - arbitraryChildren}; - } - }; - -private: - /** - * Map containing all registered command names and the corresponding - * handler - * descriptor. - */ - const std::multimap handlers; - - /** - * Reference at the parser context. - */ - const ParserContext &ctx; - - /** - * Internal stack used for managing the currently active Handler instances. - */ - std::stack stack; - - /** - * Used internally to get all expected command names for the given state - * (does not work if the current Handler instance allows arbitrary - * children). This function is used to build error messages. - * - * @param state is the state for which all expected command names should be - * returned. - */ - std::set expectedCommands(State state); - -public: - /** - * Creates a new instance of the StateStack class. - * - * @param handlers is a map containing the command names and the - * corresponding HandlerDescriptor instances. - */ - StateStack(const ParserContext &ctx, - std::multimap handlers) - : handlers(std::move(handlers)), - ctx(ctx), - currentState(State::NONE), - arbitraryChildren(false); -}; -} -} -} - -#endif /* _OUSIA_XML_STATES_HPP_ */ - diff --git a/test/core/parser/ParserStackTest.cpp b/test/core/parser/ParserStackTest.cpp new file mode 100644 index 0000000..92249ff --- /dev/null +++ b/test/core/parser/ParserStackTest.cpp @@ -0,0 +1,165 @@ +/* + SCAENEA IDL Compiler (scidlc) + Copyright (C) 2014 Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +#include + +namespace ousia { +namespace parser { + +static const State STATE_DOCUMENT = 0; +static const State STATE_BODY = 1; +static const State STATE_EMPTY = 2; + +static int startCount = 0; +static int endCount = 0; +static int dataCount = 0; +static int childCount = 0; + +class TestHandler : public Handler { + +public: + using Handler::Handler; + + void start(char **attrs) override + { + startCount++; +// std::cout << this->name << ": start (isChild: " << (this->isChild) << ")" << std::endl; + } + + void end() override + { + endCount++; +// std::cout << this->name << ": end " << std::endl; + } + + void data(const char *data, int len) override + { + dataCount++; +// std::cout << this->name << ": data \"" << std::string{data, static_cast(len)} << "\"" << std::endl; + } + + void child(std::shared_ptr handler) override + { + childCount++; +// std::cout << this->name << ": has child \"" << handler->name << "\"" << std::endl; + } + +}; + +static Handler* createTestHandler(const ParserContext &ctx, + std::string name, State state, + State parentState, bool isChild) +{ + return new TestHandler(ctx, name, state, parentState, isChild); +} + +// Two nested elements used for testing +static const std::multimap TEST_HANDLERS{ + {"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, + {"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, + {"empty", {{STATE_DOCUMENT}, createTestHandler, STATE_EMPTY}}, +}; + + +TEST(ParserStack, simpleTest) +{ + StandaloneParserContext ctx; + ParserStack s{ctx, TEST_HANDLERS}; + + startCount = 0; + endCount = 0; + dataCount = 0; + childCount = 0; + + ASSERT_EQ("", s.currentName()); + ASSERT_EQ(STATE_NONE, s.currentState()); + + s.start("document", nullptr); + s.data("test1", 5); + + ASSERT_EQ("document", s.currentName()); + ASSERT_EQ(STATE_DOCUMENT, s.currentState()); + ASSERT_EQ(1, startCount); + ASSERT_EQ(1, dataCount); + + s.start("body", nullptr); + s.data("test2", 5); + ASSERT_EQ("body", s.currentName()); + ASSERT_EQ(STATE_BODY, s.currentState()); + ASSERT_EQ(2, startCount); + ASSERT_EQ(2, dataCount); + + s.start("inner", nullptr); + ASSERT_EQ("inner", s.currentName()); + ASSERT_EQ(STATE_BODY, s.currentState()); + s.end(); + ASSERT_EQ(3, startCount); + ASSERT_EQ(1, childCount); + ASSERT_EQ(1, endCount); + + s.end(); + ASSERT_EQ(2, childCount); + ASSERT_EQ(2, endCount); + + ASSERT_EQ("document", s.currentName()); + ASSERT_EQ(STATE_DOCUMENT, s.currentState()); + + s.start("body", nullptr); + s.data("test3", 5); + ASSERT_EQ("body", s.currentName()); + ASSERT_EQ(STATE_BODY, s.currentState()); + s.end(); + ASSERT_EQ(4, startCount); + ASSERT_EQ(3, dataCount); + ASSERT_EQ(3, childCount); + ASSERT_EQ(3, endCount); + + ASSERT_EQ("document", s.currentName()); + ASSERT_EQ(STATE_DOCUMENT, s.currentState()); + + s.end(); + ASSERT_EQ(4, endCount); + + ASSERT_EQ("", s.currentName()); + ASSERT_EQ(STATE_NONE, s.currentState()); +} + +TEST(ParserStack, errorHandling) +{ + StandaloneParserContext ctx; + ParserStack s{ctx, TEST_HANDLERS}; + + ASSERT_THROW(s.start("body", nullptr), OusiaException); + s.start("document", nullptr); + ASSERT_THROW(s.start("document", nullptr), OusiaException); + s.start("empty", nullptr); + ASSERT_THROW(s.start("body", nullptr), OusiaException); + s.end(); + s.end(); + ASSERT_EQ(STATE_NONE, s.currentState()); + ASSERT_THROW(s.end(), OusiaException); + ASSERT_THROW(s.data("test", 1), OusiaException); +} + +} +} + -- cgit v1.2.3 From 0c26390e71193947a67bdd0536915523da38f00f Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 14:59:17 +0100 Subject: added revamped variant type --- CMakeLists.txt | 33 +-- src/core/variant/Variant.cpp | 58 ++++ src/core/variant/Variant.hpp | 624 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 699 insertions(+), 16 deletions(-) create mode 100644 src/core/variant/Variant.cpp create mode 100644 src/core/variant/Variant.hpp (limited to 'src') diff --git a/CMakeLists.txt b/CMakeLists.txt index 327f664..3a52b6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,10 +112,11 @@ ADD_LIBRARY(ousia_core src/core/parser/Parser src/core/parser/ParserStack src/core/parser/Scope - src/core/script/Function - src/core/script/Object - src/core/script/ScriptEngine - src/core/script/Variant +# src/core/script/Function +# src/core/script/Object +# src/core/script/ScriptEngine +# src/core/script/Variant + src/core/variant/Variant ) # ousia_xml library @@ -159,9 +160,9 @@ IF(TEST) test/core/TokenizerTest test/core/UtilsTest test/core/parser/ParserStackTest - test/core/script/FunctionTest - test/core/script/ObjectTest - test/core/script/VariantTest +# test/core/script/FunctionTest +# test/core/script/ObjectTest +# test/core/script/VariantTest ) TARGET_LINK_LIBRARIES(ousia_test_core @@ -179,19 +180,19 @@ IF(TEST) ousia_xml ) - ADD_EXECUTABLE(ousia_test_mozjs - test/plugins/mozjs/MozJsScriptEngineTest - ) +# ADD_EXECUTABLE(ousia_test_mozjs +# test/plugins/mozjs/MozJsScriptEngineTest +# ) - TARGET_LINK_LIBRARIES(ousia_test_mozjs - ${GTEST_LIBRARIES} - ousia_core - ousia_mozjs - ) +# TARGET_LINK_LIBRARIES(ousia_test_mozjs +# ${GTEST_LIBRARIES} +# ousia_core +# ousia_mozjs +# ) # Register the unit tests ADD_TEST(ousia_test_core ousia_test_core) ADD_TEST(ousia_test_xml ousia_test_xml) - ADD_TEST(ousia_test_mozjs ousia_test_mozjs) +# ADD_TEST(ousia_test_mozjs ousia_test_mozjs) ENDIF() diff --git a/src/core/variant/Variant.cpp b/src/core/variant/Variant.cpp new file mode 100644 index 0000000..c86905c --- /dev/null +++ b/src/core/variant/Variant.cpp @@ -0,0 +1,58 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "Variant.hpp" + +namespace ousia { + +/* Class Variant */ + +const char *Variant::getTypeName(Type type) +{ + switch (type) { + case Type::NULLPTR: + return "null"; + case Type::BOOL: + return "boolean"; + case Type::INT: + return "integer"; + case Type::DOUBLE: + return "number"; + case Type::STRING: + return "string"; + case Type::ARRAY: + return "array"; + case Type::MAP: + return "map"; + } + return "unknown"; +} + +/* Class VariantTypeException */ + +Variant::TypeException::TypeException(Type actualType, Type requestedType) + : OusiaException(std::string("Variant: Requested \"") + + Variant::getTypeName(actualType) + + std::string("\" but is \"") + + Variant::getTypeName(requestedType) + std::string("\"")), + actualType(actualType), + requestedType(requestedType) +{ +} +} + diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp new file mode 100644 index 0000000..26b053a --- /dev/null +++ b/src/core/variant/Variant.hpp @@ -0,0 +1,624 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file Variant.hpp + * + * The Variant class is used to efficiently represent a variables of varying + * type. Variant instances are used to represent user data and to exchange + * information between the host application and the script clients. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_VARIANT_HPP_ +#define _OUSIA_VARIANT_HPP_ + +#include +#include +#include +#include + +// TODO: Use +// http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html +// later (will allow to use 8 bytes for a variant) + +#include + +namespace ousia { + +/** + * Instances of the Variant class represent any kind of data that is exchanged + * between the host application and the script engine. Variants are immutable. + */ +class Variant { +public: + /** + * Enum containing the possible types a variant may have. + */ + enum class Type : int16_t { + NULLPTR, + BOOL, + INT, + DOUBLE, + STRING, + ARRAY, + MAP + }; + + /** + * Exception thrown whenever a variant is accessed via a getter function + * that + * is not supported for the current variant type. + */ + class TypeException : public OusiaException { + private: + /** + * Internally used string holding the exception message. + */ + const std::string msg; + + public: + /** + * Contains the actual type of the variant. + */ + const Type actualType; + + /** + * Contains the requested type of the variant. + */ + const Type requestedType; + + /** + * Constructor of the TypeException. + * + * @param actualType describes the actual type of the variant. + * @param requestedType describes the type in which the variant was + * requested. + */ + TypeException(Type actualType, Type requestedType); + }; + + using boolType = bool; + using intType = int32_t; + using doubleType = double; + using stringType = std::string; + using arrayType = std::vector; + using mapType = std::map; + +private: + /** + * Used to store the actual type of the variant. + */ + Type type = Type::NULLPTR; + + /** + * Anonymous union containing the possible value of the variant. + */ + union { + /** + * The boolean value. Only valid if type is Type::BOOL. + */ + boolType boolVal; + /** + * The integer value. Only valid if type is Type::INT. + */ + intType intVal; + /** + * The number value. Only valid if type is Type::DOUBLE. + */ + doubleType doubleVal; + /** + * Pointer to the more complex data structures on the free store. Only + * valid if type is one of Type::STRING, Type::ARRAY, + * Type::MAP. + */ + void *ptrVal = nullptr; + }; + + /** + * Internally used to convert the current pointer value to a reference of + * the specified type. + */ + template + T &asObj(Type requestedType) const + { + const Type actualType = getType(); + if (actualType == requestedType) { + return *(static_cast(ptrVal)); + } + throw TypeException{actualType, requestedType}; + } + + /** + * Used internally to assign the value of another Variant instance to this + * instance. + * + * @param v is the Variant instance that should be copied to this instance. + */ + void copy(const Variant &v) + { + type = v.type; + switch (type) { + case Type::NULLPTR: + break; + case Type::BOOL: + boolVal = v.boolVal; + break; + case Type::INT: + intVal = v.intVal; + break; + case Type::DOUBLE: + doubleVal = v.doubleVal; + break; + case Type::STRING: + ptrVal = new stringType{v.asString()}; + break; + case Type::ARRAY: + ptrVal = new arrayType{v.asArray()}; + break; + case Type::MAP: + ptrVal = new mapType{v.asMap()}; + break; + } + } + + /** + * Used internally to move the value of another Variant instance to this + * instance. + * + * @param v is the Variant instance that should be copied to this instance. + */ + void move(Variant &&v) + { + type = v.type; + switch (type) { + case Type::NULLPTR: + break; + case Type::BOOL: + boolVal = v.boolVal; + break; + case Type::INT: + intVal = v.intVal; + break; + case Type::DOUBLE: + doubleVal = v.doubleVal; + break; + case Type::STRING: + case Type::ARRAY: + case Type::MAP: + ptrVal = v.ptrVal; + v.ptrVal = nullptr; + break; + } + v.type = Type::NULLPTR; + } + + /** + * Used internally to destroy any value that was allocated on the heap. + */ + void destroy() + { + if (ptrVal) { + switch (type) { + case Type::STRING: + delete static_cast(ptrVal); + break; + case Type::ARRAY: + delete static_cast(ptrVal); + break; + case Type::MAP: + delete static_cast(ptrVal); + break; + default: + break; + } + } + } + +public: + /** + * Copy constructor of the Variant class. + * + * @param v is the Variant instance that should be cloned. + */ + Variant(const Variant &v) { copy(v); } + + /** + * Move constructor of the Variant class. + * + * @param v is the reference to the Variant instance that should be moved, + * this instance is invalidated afterwards. + */ + Variant(Variant &&v) { move(std::move(v)); } + + /** + * Default constructor. Type is set to Type:null. + */ + Variant() { setNull(); } + + /** + * Default destructor, frees any memory that was allocated on the heap. + */ + ~Variant() { destroy(); } + + /** + * Constructor for boolean values. + * + * @param b boolean value. + */ + Variant(boolType b) { setBool(b); } + + /** + * Constructor for integer values. + * + * @param i integer value. + */ + Variant(intType i) { setInt(i); } + + /** + * Constructor for double values. + * + * @param d double value. + */ + Variant(doubleType d) { setDouble(d); } + + /** + * Constructor for string values. The given string is copied and managed by + * the new Variant instance. + * + * @param s is a reference to a C-Style string used as string value. + */ + Variant(const char *s) { setString(s); } + + /** + * Constructor for array values. The given array is copied and managed by + * the new Variant instance. + * + * @param a is a reference to the array + */ + Variant(std::vector a) { setArray(std::move(a)); } + + /** + * Constructor for map values. The given map is copied and managed by the + * new Variant instance. + * + * @param m is a reference to the map. + */ + Variant(std::map m) { setMap(std::move(m)); } + + /** + * Copy assignment operator. + */ + Variant &operator=(const Variant &v) + { + copy(v); + return *this; + } + + /** + * Move assignment operator. + */ + Variant &operator=(Variant &&v) + { + move(std::move(v)); + return *this; + } + + /** + * Assign nullptr_t operator (allows to write Variant v = nullptr). + * + * @param p is an instance of std::nullptr_t. + */ + Variant &operator=(std::nullptr_t) + { + setNull(); + return *this; + } + + /** + * Assign a boolean value. + * + * @param b is the boolean value to which the variant should be set. + */ + Variant &operator=(boolType b) + { + setBool(b); + return *this; + } + + /** + * Assign an integer value. + * + * @param i is the integer value to which the variant should be set. + */ + Variant &operator=(intType i) + { + setInt(i); + return *this; + } + + /** + * Assign a double value. + * + * @param i is the integer value to which the variant should be set. + */ + Variant &operator=(doubleType d) + { + setInt(d); + return *this; + } + + /** + * Checks whether this Variant instance represents the nullptr. + * + * @return true if the Variant instance represents the nullptr, false + * otherwise. + */ + bool isNull() const { return type == Type::NULLPTR; } + + /** + * Checks whether this Variant instance is a boolean. + * + * @return true if the Variant instance is a boolean, false otherwise. + */ + bool isBool() const { return type == Type::BOOL; } + + /** + * Checks whether this Variant instance is an integer. + * + * @return true if the Variant instance is an integer, false otherwise. + */ + bool isInt() const { return type == Type::INT; } + + /** + * Checks whether this Variant instance is a double. + * + * @return true if the Variant instance is a double, false otherwise. + */ + bool isDouble() const { return type == Type::DOUBLE; } + + /** + * Checks whether this Variant instance is a string. + * + * @return true if the Variant instance is a string, false otherwise. + */ + bool isString() const { return type == Type::STRING; } + + /** + * Checks whether this Variant instance is an array. + * + * @return true if the Variant instance is an array, false otherwise. + */ + bool isArray() const { return type == Type::ARRAY; } + + /** + * Checks whether this Variant instance is a map. + * + * @return true if the Variant instance is a map, false otherwise. + */ + bool isMap() const { return type == Type::MAP; } + + /** + * Returns the Variant boolean value. Performs no type conversion. Throws an + * exception if the underlying type is not a boolean. + * + * @return the boolean value. + */ + boolType asBool() const + { + if (isBool()) { + return boolVal; + } + throw TypeException{getType(), Type::BOOL}; + } + + /** + * Returns the Variant integer value. Performs no type conversion. Throws an + * exception if the underlying type is not an integer. + * + * @return the integer value. + */ + intType asInt() const + { + if (isInt()) { + return intVal; + } + throw TypeException{getType(), Type::INT}; + } + + /** + * Returns the Variant double value. Performs no type conversion. Throws an + * exception if the underlying type is not a double. + * + * @return the double value. + */ + doubleType asDouble() const + { + if (isDouble()) { + return doubleVal; + } + throw TypeException{getType(), Type::DOUBLE}; + } + + /** + * Returns a const reference to the string value. Performs no type + * conversion. Throws an exception if the underlying type is not a string. + * + * @return the string value as const reference. + */ + const stringType &asString() const + { + return asObj(Type::STRING); + } + + /** + * Returns a const reference to the string value. Performs no type + * conversion. Throws an exception if the underlying type is not a string. + * + * @return the string value as reference. + */ + stringType &asString() { return asObj(Type::STRING); } + + /** + * Returns a const reference to the array value. Performs no type + * conversion. Throws an exception if the underlying type is not an array. + * + * @return the array value as const reference. + */ + const arrayType &asArray() const { return asObj(Type::ARRAY); } + + /** + * Returns a const reference to the array value. Performs no type + * conversion. Throws an exception if the underlying type is not an array. + * + * @return the array value as reference. + */ + arrayType &asArray() { return asObj(Type::ARRAY); } + + /** + * Returns a const reference to the map value. Performs no type + * conversion. Throws an exception if the underlying type is not a map. + * + * @return the map value as const reference. + */ + const mapType &asMap() const { return asObj(Type::MAP); } + + /** + * Returns a reference to the map value. Performs no type conversion. + * Throws an exception if the underlying type is not a map. + * + * @return the map value as reference. + */ + mapType &asMap() { return asObj(Type::MAP); } + + /** + * Sets the variant to null. + */ + void setNull() + { + destroy(); + type = Type::NULLPTR; + ptrVal = nullptr; + } + + /** + * Sets the variant to the given boolean value. + * + * @param b is the new boolean value. + */ + void setBool(boolType b) + { + destroy(); + type = Type::BOOL; + boolVal = b; + } + + /** + * Sets the variant to the given integer value. + * + * @param i is the new integer value. + */ + void setInt(intType i) + { + destroy(); + type = Type::INT; + intVal = i; + } + + /** + * Sets the variant to the given double value. + * + * @param d is the new double value. + */ + void setDouble(doubleType d) + { + destroy(); + type = Type::DOUBLE; + doubleVal = d; + } + + /** + * Sets the variant to the given string value. + * + * @param d is the new string value. + */ + void setString(const char *s) + { + if (isString()) { + asString().assign(s); + } else { + destroy(); + type = Type::STRING; + ptrVal = new stringType{s}; + } + } + + /** + * Sets the variant to the given array value. + * + * @param a is the new array value. + */ + void setArray(arrayType a) + { + if (isArray()) { + asArray().swap(a); + } else { + destroy(); + type = Type::ARRAY; + ptrVal = new arrayType{std::move(a)}; + } + } + + /** + * Sets the variant to the given map value. + * + * @param a is the new map value. + */ + void setMap(mapType m) + { + if (isMap()) { + asMap().swap(m); + } else { + destroy(); + type = Type::MAP; + ptrVal = new mapType{std::move(m)}; + } + } + + /** + * Returns the current type of the Variant. + * + * @return the current type of the Variant. + */ + Type getType() const { return type; } + + /** + * Returns the name of the given variant type as C-style string. + */ + static const char *getTypeName(Type type); + + /** + * Returns the name of the type of this variant instance. + */ + const char *getTypeName() { return Variant::getTypeName(getType()); } +}; +} + +#endif /* _OUSIA_VARIANT_HPP_ */ + -- cgit v1.2.3 From 35554e6d32a5e66819f8a7bf869f1853e0d6fede Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 14:59:44 +0100 Subject: continued working on the xml parser class --- src/core/parser/ParserStack.cpp | 3 ++- src/core/parser/ParserStack.hpp | 3 ++- src/plugins/xml/XmlParser.cpp | 43 ++++++++++++++++++++++++++++++++++++ test/core/BufferedCharReaderTest.cpp | 4 ++-- test/core/parser/ParserStackTest.cpp | 9 ++++---- 5 files changed, 54 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 01fce3f..7bc7af3 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -100,7 +100,8 @@ void ParserStack::start(std::string name, char **attrs) const HandlerDescriptor *descr = nullptr; auto range = handlers.equal_range(name); for (auto it = range.first; it != range.second; it++) { - if (it->second.parentStates.count(curState)) { + const std::set &parentStates = it->second.parentStates; + if (parentStates.count(curState) || parentStates.count(STATE_ALL)) { descr = &(it->second); break; } diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index a777b1e..18fc8d9 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -45,7 +45,7 @@ namespace parser { /** * The State type alias is used to */ -using State = int8_t; +using State = int16_t; static const State STATE_ALL = -2; static const State STATE_NONE = -1; @@ -140,6 +140,7 @@ public: * Handler instance. * * TODO: Replace with std::string? + * TODO: Per default: Allow no data except for whitespace characters! * * @param data is a pointer at the character data that is available for the * Handler instance. diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index f6891a8..42e0dd4 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -20,12 +20,55 @@ #include +#include + #include "XmlParser.hpp" namespace ousia { namespace parser { namespace xml { +/* Document structure */ +static const State STATE_DOCUMENT = 0; +static const State STATE_HEAD = 1; +static const State STATE_BODY = 2; + +/* Special commands */ +static const State STATE_USE = 100; +static const State STATE_INCLUDE = 101; +static const State STATE_INLINE = 102; + +/* Type system definitions */ +static const State STATE_TYPES = 200; +static const State STATE_CONSTANT = 201; +static const State STATE_ENUM = 202; +static const State STATE_STRUCT = 203; + +static Handler* createTestHandler(const ParserContext &ctx, + std::string name, State state, + State parentState, bool isChild) +{ + return nullptr; +} + +static const std::multimap XML_HANDLERS{ + /* Documents */ + {"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, + {"head", {{STATE_DOCUMENT}, createTestHandler, STATE_HEAD}}, + {"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, + + /* Special commands */ + {"use", {{STATE_HEAD}, createTestHandler, STATE_USE}}, + {"include", {{STATE_ALL}, createTestHandler, STATE_INCLUDE}}, + {"inline", {{STATE_ALL}, createTestHandler, STATE_INLINE}}, + + /* Typesystem definitions */ + {"types", {{STATE_NONE, STATE_HEAD}, createTestHandler, STATE_TYPES}}, + {"enum", {{STATE_TYPES}, createTestHandler, STATE_ENUM}}, + {"struct", {{STATE_TYPES}, createTestHandler, STATE_STRUCT}}, + {"constant", {{STATE_TYPES}, createTestHandler, STATE_CONSTANT}} +}; + /** * Wrapper class around the XML_Parser pointer which safely frees it whenever * the scope is left (e.g. because an exception was thrown). diff --git a/test/core/BufferedCharReaderTest.cpp b/test/core/BufferedCharReaderTest.cpp index b0955c2..b3498f7 100644 --- a/test/core/BufferedCharReaderTest.cpp +++ b/test/core/BufferedCharReaderTest.cpp @@ -1,6 +1,6 @@ /* - SCAENEA IDL Compiler (scidlc) - Copyright (C) 2014 Andreas Stöckel + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/test/core/parser/ParserStackTest.cpp b/test/core/parser/ParserStackTest.cpp index 92249ff..1f4a4e2 100644 --- a/test/core/parser/ParserStackTest.cpp +++ b/test/core/parser/ParserStackTest.cpp @@ -1,6 +1,6 @@ /* - SCAENEA IDL Compiler (scidlc) - Copyright (C) 2014 Andreas Stöckel + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -72,14 +72,13 @@ static Handler* createTestHandler(const ParserContext &ctx, return new TestHandler(ctx, name, state, parentState, isChild); } -// Two nested elements used for testing static const std::multimap TEST_HANDLERS{ {"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, {"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, {"empty", {{STATE_DOCUMENT}, createTestHandler, STATE_EMPTY}}, + {"special", {{STATE_ALL}, createTestHandler, STATE_EMPTY}}, }; - TEST(ParserStack, simpleTest) { StandaloneParserContext ctx; @@ -153,6 +152,8 @@ TEST(ParserStack, errorHandling) ASSERT_THROW(s.start("document", nullptr), OusiaException); s.start("empty", nullptr); ASSERT_THROW(s.start("body", nullptr), OusiaException); + s.start("special", nullptr); + s.end(); s.end(); s.end(); ASSERT_EQ(STATE_NONE, s.currentState()); -- cgit v1.2.3 From 65778eb19e1b4d7d5d145bb2167df0eb01935da7 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 15:58:48 +0100 Subject: added new unit test for the Variant class and fixed some bugs --- CMakeLists.txt | 1 + src/core/variant/Variant.hpp | 48 +++++++++++----- test/core/variant/VariantTest.cpp | 118 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 14 deletions(-) create mode 100644 test/core/variant/VariantTest.cpp (limited to 'src') diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a52b6e..cb4d073 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -163,6 +163,7 @@ IF(TEST) # test/core/script/FunctionTest # test/core/script/ObjectTest # test/core/script/VariantTest + test/core/variant/VariantTest ) TARGET_LINK_LIBRARIES(ousia_test_core diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index 26b053a..6b5d03f 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -128,7 +128,7 @@ private: * valid if type is one of Type::STRING, Type::ARRAY, * Type::MAP. */ - void *ptrVal = nullptr; + void *ptrVal; }; /** @@ -153,6 +153,7 @@ private: */ void copy(const Variant &v) { + destroy(); type = v.type; switch (type) { case Type::NULLPTR: @@ -186,6 +187,7 @@ private: */ void move(Variant &&v) { + destroy(); type = v.type; switch (type) { case Type::NULLPTR: @@ -237,7 +239,7 @@ public: * * @param v is the Variant instance that should be cloned. */ - Variant(const Variant &v) { copy(v); } + Variant(const Variant &v) : ptrVal(nullptr) { copy(v); } /** * Move constructor of the Variant class. @@ -245,12 +247,12 @@ public: * @param v is the reference to the Variant instance that should be moved, * this instance is invalidated afterwards. */ - Variant(Variant &&v) { move(std::move(v)); } + Variant(Variant &&v) : ptrVal(nullptr) { move(std::move(v)); } /** * Default constructor. Type is set to Type:null. */ - Variant() { setNull(); } + Variant() : ptrVal(nullptr) { setNull(); } /** * Default destructor, frees any memory that was allocated on the heap. @@ -262,21 +264,21 @@ public: * * @param b boolean value. */ - Variant(boolType b) { setBool(b); } + Variant(boolType b) : ptrVal(nullptr) { setBool(b); } /** * Constructor for integer values. * * @param i integer value. */ - Variant(intType i) { setInt(i); } + Variant(intType i) : ptrVal(nullptr) { setInt(i); } /** * Constructor for double values. * * @param d double value. */ - Variant(doubleType d) { setDouble(d); } + Variant(doubleType d) : ptrVal(nullptr) { setDouble(d); } /** * Constructor for string values. The given string is copied and managed by @@ -284,7 +286,7 @@ public: * * @param s is a reference to a C-Style string used as string value. */ - Variant(const char *s) { setString(s); } + Variant(const char *s) : ptrVal(nullptr) { setString(s); } /** * Constructor for array values. The given array is copied and managed by @@ -292,7 +294,10 @@ public: * * @param a is a reference to the array */ - Variant(std::vector a) { setArray(std::move(a)); } + Variant(arrayType a) : ptrVal(nullptr) + { + setArray(std::move(a)); + } /** * Constructor for map values. The given map is copied and managed by the @@ -300,7 +305,10 @@ public: * * @param m is a reference to the map. */ - Variant(std::map m) { setMap(std::move(m)); } + Variant(mapType m) : ptrVal(nullptr) + { + setMap(std::move(m)); + } /** * Copy assignment operator. @@ -356,11 +364,23 @@ public: /** * Assign a double value. * - * @param i is the integer value to which the variant should be set. + * @param d is the double value to which the variant should be set. */ Variant &operator=(doubleType d) { - setInt(d); + setDouble(d); + return *this; + } + + /** + * Assign a zero terminated const char array. + * + * @param s is the zero terminated const char array to which the variant + * should be set. + */ + Variant &operator=(const char *s) + { + setString(s); return *this; } @@ -581,7 +601,7 @@ public: } else { destroy(); type = Type::ARRAY; - ptrVal = new arrayType{std::move(a)}; + ptrVal = new arrayType(std::move(a)); } } @@ -597,7 +617,7 @@ public: } else { destroy(); type = Type::MAP; - ptrVal = new mapType{std::move(m)}; + ptrVal = new mapType(std::move(m)); } } diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp new file mode 100644 index 0000000..dfa2f1b --- /dev/null +++ b/test/core/variant/VariantTest.cpp @@ -0,0 +1,118 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +namespace ousia { + +TEST(Variant, nullValue) +{ + Variant v; + ASSERT_TRUE(v.isNull()); + + v = 1; + ASSERT_FALSE(v.isNull()); + + v = nullptr; + ASSERT_TRUE(v.isNull()); +} + +TEST(Variant, booleanValue) +{ + Variant v{true}; + ASSERT_TRUE(v.isBool()); + ASSERT_TRUE(v.asBool()); + + v = false; + ASSERT_TRUE(v.isBool()); + ASSERT_FALSE(v.asBool()); + + v.setBool(true); + ASSERT_TRUE(v.isBool()); + ASSERT_TRUE(v.asBool()); + + v = nullptr; + ASSERT_FALSE(v.isBool()); +} + +TEST(Variant, intValue) +{ + Variant v{42}; + ASSERT_TRUE(v.isInt()); + ASSERT_EQ(42, v.asInt()); + + v = 43; + ASSERT_TRUE(v.isInt()); + ASSERT_EQ(43, v.asInt()); + + v = false; + ASSERT_FALSE(v.isInt()); +} + +TEST(Variant, doubleValue) +{ + Variant v{42.5}; + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(42.5, v.asDouble()); + + v = 42; + ASSERT_FALSE(v.isDouble()); + + v = 43.5; + ASSERT_TRUE(v.isDouble()); + ASSERT_EQ(43.5, v.asDouble()); +} + +TEST(Variant, stringValue) +{ + Variant v{"Hello World"}; + ASSERT_TRUE(v.isString()); + ASSERT_EQ("Hello World", v.asString()); + + v = "Goodbye World"; + ASSERT_TRUE(v.isString()); + ASSERT_EQ("Goodbye World", v.asString()); + + v = 42; + ASSERT_FALSE(v.isString()); +} + +TEST(Variant, arrayValue) +{ + const Variant v{{"test1", 42}}; + ASSERT_EQ(2, v.asArray().size()); + ASSERT_EQ("test1", v.asArray()[0].asString()); + ASSERT_EQ(42, v.asArray()[1].asInt()); +} + +TEST(Variant, mapValue) +{ + const Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}}; + + auto map = v.asMap(); + ASSERT_EQ(2, map.size()); + + ASSERT_EQ("entry1", map.find("key1")->second.asString()); + ASSERT_EQ("entry2", map.find("key2")->second.asString()); +} + + +} + -- cgit v1.2.3 From 80c32c744807afa81178f45af1867fb7c3366c81 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 16:21:23 +0100 Subject: expanded unit test and fixed further stack overflow caused by missuse of braced initializer list --- src/core/variant/Variant.hpp | 8 ++++---- test/core/variant/VariantTest.cpp | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index 6b5d03f..f438d3e 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -168,13 +168,13 @@ private: doubleVal = v.doubleVal; break; case Type::STRING: - ptrVal = new stringType{v.asString()}; + ptrVal = new stringType(v.asString()); break; case Type::ARRAY: - ptrVal = new arrayType{v.asArray()}; + ptrVal = new arrayType(v.asArray()); break; case Type::MAP: - ptrVal = new mapType{v.asMap()}; + ptrVal = new mapType(v.asMap()); break; } } @@ -585,7 +585,7 @@ public: } else { destroy(); type = Type::STRING; - ptrVal = new stringType{s}; + ptrVal = new stringType(s); } } diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index dfa2f1b..3a23887 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -111,6 +111,8 @@ TEST(Variant, mapValue) ASSERT_EQ("entry1", map.find("key1")->second.asString()); ASSERT_EQ("entry2", map.find("key2")->second.asString()); + + const Variant v2{{{"key1", Variant::arrayType{1, 2, 3}}, {"key2", "entry2"}}}; } -- cgit v1.2.3 From b143fe4e0df319a88df9cba22c5dd707000810d4 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:01:03 +0100 Subject: added trim, join and isWhitespace function to Utils --- src/core/Utils.cpp | 22 +++++++++++++++++++++- src/core/Utils.hpp | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/core/Utils.cpp b/src/core/Utils.cpp index 184fdd0..c460ed4 100644 --- a/src/core/Utils.cpp +++ b/src/core/Utils.cpp @@ -16,10 +16,31 @@ along with this program. If not, see . */ +#include +#include + #include "Utils.hpp" namespace ousia { +std::string Utils::trim(const std::string &s) +{ + size_t firstNonWhitespace = std::numeric_limits::max(); + size_t lastNonWhitespace = 0; + for (size_t i = 0; i < s.size(); i++) { + if (!isWhitespace(s[i])) { + firstNonWhitespace = std::min(i, firstNonWhitespace); + lastNonWhitespace = std::max(i, lastNonWhitespace); + } + } + + if (firstNonWhitespace < lastNonWhitespace) { + return s.substr(firstNonWhitespace, + lastNonWhitespace - firstNonWhitespace + 1); + } + return std::string{}; +} + bool Utils::isIdentifier(const std::string &name) { bool first = true; @@ -34,6 +55,5 @@ bool Utils::isIdentifier(const std::string &name) } return true; } - } diff --git a/src/core/Utils.hpp b/src/core/Utils.hpp index 2fcd794..14bd7b4 100644 --- a/src/core/Utils.hpp +++ b/src/core/Utils.hpp @@ -16,18 +16,16 @@ along with this program. If not, see . */ - #ifndef _OUSIA_UTILS_H_ #define _OUSIA_UTILS_H_ +#include #include namespace ousia { class Utils { - public: - /** * Returns true if the given character is in [A-Za-z] */ @@ -39,10 +37,7 @@ public: /** * Returns true if the given character is in [0-9] */ - static bool isNumeric(const char c) - { - return (c >= '0') && (c <= '9'); - } + static bool isNumeric(const char c) { return (c >= '0') && (c <= '9'); } /** * Returns true if the given character is in [A-Za-z0-9] @@ -57,8 +52,49 @@ public: */ static bool isIdentifier(const std::string &name); -}; + /** + * Returns true if the given character is a whitespace character. + */ + static bool isWhitespace(const char c) + { + return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'); + } + /** + * Removes whitespace at the beginning and the end of the given string. + */ + static std::string trim(const std::string &s); + + /** + * Turns the elements of a collection into a string separated by the + * given delimiter. + * + * @param es is an iterable container of elements that can be appended to an + * output stream (the << operator must be implemented). + * @param delim is the delimiter that should be used to separate the items. + * @param start is a character sequence that should be prepended to the + * result. + * @param end is a character sequence that should be appended to the result. + */ + template + static std::string join(T es, const std::string &delim, + const std::string &start = "", + const std::string &end = "") + { + std::stringstream res; + bool first = true; + res << start; + for (const auto &e : es) { + if (!first) { + res << delim; + } + res << e; + first = false; + } + res << end; + return res.str(); + } +}; } #endif /* _OUSIA_UTILS_H_ */ -- cgit v1.2.3 From 2ca83f15d5ca81ce8b45fd99d959aee49a6f2eea Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:03:01 +0100 Subject: added type conversion functions and creation from nullptr --- src/core/variant/Variant.cpp | 115 +++++++++++++++++++++++++++++++++++--- src/core/variant/Variant.hpp | 69 +++++++++++++++++++---- test/core/variant/VariantTest.cpp | 8 ++- 3 files changed, 172 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/core/variant/Variant.cpp b/src/core/variant/Variant.cpp index c86905c..d33cd4f 100644 --- a/src/core/variant/Variant.cpp +++ b/src/core/variant/Variant.cpp @@ -16,10 +16,26 @@ along with this program. If not, see . */ +#include + +#include + #include "Variant.hpp" namespace ousia { +/* Class Variant::TypeException */ + +Variant::TypeException::TypeException(Type actualType, Type requestedType) + : OusiaException(std::string("Variant: Requested \"") + + Variant::getTypeName(requestedType) + + std::string("\" but is \"") + + Variant::getTypeName(actualType) + std::string("\"")), + actualType(actualType), + requestedType(requestedType) +{ +} + /* Class Variant */ const char *Variant::getTypeName(Type type) @@ -32,7 +48,7 @@ const char *Variant::getTypeName(Type type) case Type::INT: return "integer"; case Type::DOUBLE: - return "number"; + return "double"; case Type::STRING: return "string"; case Type::ARRAY: @@ -43,16 +59,97 @@ const char *Variant::getTypeName(Type type) return "unknown"; } -/* Class VariantTypeException */ +Variant::boolType Variant::toBool() const +{ + switch (getType()) { + case Type::NULLPTR: + return false; + case Type::BOOL: + return asBool(); + case Type::INT: + return asInt() != 0; + case Type::DOUBLE: + return asDouble() != 0.0; + case Type::STRING: + return true; + case Type::ARRAY: + return true; + case Type::MAP: + return true; + } + return false; +} -Variant::TypeException::TypeException(Type actualType, Type requestedType) - : OusiaException(std::string("Variant: Requested \"") + - Variant::getTypeName(actualType) + - std::string("\" but is \"") + - Variant::getTypeName(requestedType) + std::string("\"")), - actualType(actualType), - requestedType(requestedType) +Variant::intType Variant::toInt() const { + switch (getType()) { + case Type::NULLPTR: + return 0; + case Type::BOOL: + return asBool() ? 1 : 0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0; // TODO: Parse string as int + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toInt() : 0; + } + case Type::MAP: + return 0; + } + return false; } + +Variant::doubleType Variant::toDouble() const +{ + switch (getType()) { + case Type::NULLPTR: + return 0.0; + case Type::BOOL: + return asBool() ? 1.0 : 0.0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0.0; // TODO: Parse string as double + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toDouble() : 0; + } + case Type::MAP: + return 0; + } + return false; +} + +Variant::stringType Variant::toString(bool escape) const +{ + switch (getType()) { + case Type::NULLPTR: + return "null"; + case Type::BOOL: + return asBool() ? "true" : "false"; + case Type::INT: + return std::to_string(asInt()); + case Type::DOUBLE: + return std::to_string(asDouble()); + case Type::STRING: { + // TODO: Use proper serialization function + std::stringstream ss; + ss << "\"" << asString() << "\""; + return ss.str(); + } + case Type::ARRAY: + return Utils::join(asArray(), ", ", "[", "]"); + case Type::MAP: + return Utils::join(asMap(), ", ", "{", "}"); + } + return ""; +} + } diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index f438d3e..d65e14a 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -20,8 +20,8 @@ * @file Variant.hpp * * The Variant class is used to efficiently represent a variables of varying - * type. Variant instances are used to represent user data and to exchange - * information between the host application and the script clients. + * type. Variant instances are used to represent data given by the end user and + * to exchange information between the host application and the script clients. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ @@ -33,6 +33,7 @@ #include #include #include +#include // TODO: Use // http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html @@ -259,6 +260,11 @@ public: */ ~Variant() { destroy(); } + /** + * Constructor for null values. Initializes the variant as null value. + */ + Variant(std::nullptr_t) : ptrVal(nullptr) { setNull(); } + /** * Constructor for boolean values. * @@ -294,10 +300,7 @@ public: * * @param a is a reference to the array */ - Variant(arrayType a) : ptrVal(nullptr) - { - setArray(std::move(a)); - } + Variant(arrayType a) : ptrVal(nullptr) { setArray(std::move(a)); } /** * Constructor for map values. The given map is copied and managed by the @@ -305,10 +308,7 @@ public: * * @param m is a reference to the map. */ - Variant(mapType m) : ptrVal(nullptr) - { - setMap(std::move(m)); - } + Variant(mapType m) : ptrVal(nullptr) { setMap(std::move(m)); } /** * Copy assignment operator. @@ -527,6 +527,36 @@ public: */ mapType &asMap() { return asObj(Type::MAP); } + /** + * Returns the value of the Variant as boolean, performs type conversion. + * + * @return the Variant value converted to a boolean value. + */ + boolType toBool() const; + + /** + * Returns the value of the Variant as integer, performs type conversion. + * + * @return the Variant value converted to an integer value. + */ + intType toInt() const; + + /** + * Returns the value of the Variant as double, performs type conversion. + * + * @return the Variant value converted to a double value. + */ + doubleType toDouble() const; + + /** + * Returns the value of the Variant as string, performs type conversion. + * + * @return the value of the variant as string. + * @param escape if set to true, adds double quotes to strings and escapes + * them properly (resulting in a more or less JSONesque output). + */ + stringType toString(bool escape = false) const; + /** * Sets the variant to null. */ @@ -637,7 +667,26 @@ public: * Returns the name of the type of this variant instance. */ const char *getTypeName() { return Variant::getTypeName(getType()); } + + /** + * Prints the Variant to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, const Variant &v) + { + return os << v.toString(true); + } + + /** + * Prints a key value pair to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, + const mapType::value_type &v) + { + // TODO: Use proper serialization function + return os << "\"" << v.first << "\": " << v.second.toString(true); + } }; + } #endif /* _OUSIA_VARIANT_HPP_ */ diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index 3a23887..270c350 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -16,6 +16,8 @@ along with this program. If not, see . */ +#include + #include #include @@ -32,6 +34,9 @@ TEST(Variant, nullValue) v = nullptr; ASSERT_TRUE(v.isNull()); + + Variant v2{nullptr}; + ASSERT_TRUE(v.isNull()); } TEST(Variant, booleanValue) @@ -112,7 +117,8 @@ TEST(Variant, mapValue) ASSERT_EQ("entry1", map.find("key1")->second.asString()); ASSERT_EQ("entry2", map.find("key2")->second.asString()); - const Variant v2{{{"key1", Variant::arrayType{1, 2, 3}}, {"key2", "entry2"}}}; + const Variant v2{{{"key1", Variant::arrayType{1, 2}}, {"key2", "entry2"}}}; + ASSERT_EQ(2, v2.asMap().find("key1")->second.asArray()[1].asInt()); } -- cgit v1.2.3 From 59177921e8c81c1604e4154503a63190db66989c Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:04:05 +0100 Subject: continued work on XML parser and underlying ParserStack class --- src/core/parser/ParserStack.cpp | 46 +++++++--------- src/core/parser/ParserStack.hpp | 57 ++++++++++++++------ src/plugins/xml/XmlParser.cpp | 102 +++++++++++++++++++++++------------ test/core/parser/ParserStackTest.cpp | 14 ++--- test/plugins/xml/XmlParserTest.cpp | 36 ++++++------- 5 files changed, 151 insertions(+), 104 deletions(-) (limited to 'src') diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 7bc7af3..dca7f35 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -20,43 +20,37 @@ #include "ParserStack.hpp" +#include #include namespace ousia { namespace parser { +/* Class Handler */ + +void Handler::data(const std::string &data, int field) +{ + for (auto &c : data) { + if (!Utils::isWhitespace(c)) { + throw LoggableException{"No data allowed here."}; + } + } +} + /* Class HandlerDescriptor */ HandlerInstance HandlerDescriptor::create(const ParserContext &ctx, std::string name, State parentState, - bool isChild, char **attrs) const + bool isChild, + const Variant &args) const { Handler *h = ctor(ctx, name, targetState, parentState, isChild); - h->start(attrs); + h->start(args); return HandlerInstance(h, this); } /* Class ParserStack */ -/** - * Function used internally to turn the elements of a collection into a string - * separated by the given delimiter. - */ -template -static std::string join(T es, const std::string &delim) -{ - std::stringstream res; - bool first = true; - for (auto &e : es) { - if (!first) { - res << delim; - } - res << e; - first = false; - } - return res.str(); -} - /** * Returns an Exception that should be thrown when a currently invalid command * is thrown. @@ -73,7 +67,7 @@ static LoggableException invalidCommand(const std::string &name, std::string{"Expected "} + (expected.size() == 1 ? std::string{"\""} : std::string{"one of \""}) + - join(expected, "\", \"") + std::string{"\", but got \""} + name + + Utils::join(expected, "\", \"") + std::string{"\", but got \""} + name + std::string{"\""}}; } } @@ -89,7 +83,7 @@ std::set ParserStack::expectedCommands(State state) return res; } -void ParserStack::start(std::string name, char **attrs) +void ParserStack::start(std::string name, const Variant &args) { // Fetch the current handler and the current state const HandlerInstance *h = stack.empty() ? nullptr : &stack.top(); @@ -117,7 +111,7 @@ void ParserStack::start(std::string name, char **attrs) } // Instantiate the handler and call its start function - stack.emplace(descr->create(ctx, name, curState, isChild, attrs)); + stack.emplace(descr->create(ctx, name, curState, isChild, args)); } void ParserStack::end() @@ -141,7 +135,7 @@ void ParserStack::end() } } -void ParserStack::data(const char *data, int len) +void ParserStack::data(const std::string &data, int field) { // Check whether there is any command the data can be sent to if (stack.empty()) { @@ -149,7 +143,7 @@ void ParserStack::data(const char *data, int len) } // Pass the data to the current Handler instance - stack.top().handler->data(data, len); + stack.top().handler->data(data, field); } } } diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index 18fc8d9..c5ed4e4 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -37,6 +37,8 @@ #include #include +#include + #include "Parser.hpp" namespace ousia { @@ -125,10 +127,9 @@ public: * Called when the command that was specified in the constructor is * instanciated. * - * @param attrs contains the attributes that were specified for the command. - * TODO: Replace with StructInstance! + * @param args is a map from strings to variants (argument name and value). */ - virtual void start(char **attrs) = 0; + virtual void start(const Variant &args) = 0; /** * Called whenever the command for which this handler @@ -137,15 +138,15 @@ public: /** * Called whenever raw data (int the form of a string) is available for the - * Handler instance. - * - * TODO: Replace with std::string? - * TODO: Per default: Allow no data except for whitespace characters! + * Handler instance. In the default handler an exception is raised if the + * received data contains non-whitespace characters. * * @param data is a pointer at the character data that is available for the * Handler instance. + * @param field is the field number (the interpretation of this value + * depends on the format that is being parsed). */ - virtual void data(const char *data, int len){}; + virtual void data(const std::string &data, int field); /** * Called whenever a direct child element was created and has ended. @@ -225,7 +226,8 @@ struct HandlerDescriptor { * HandlerDescriptor and calls its start function. */ HandlerInstance create(const ParserContext &ctx, std::string name, - State parentState, bool isChild, char **attrs) const; + State parentState, bool isChild, + const Variant &args) const; }; /** @@ -239,6 +241,11 @@ private: */ const ParserContext &ctx; + /** + * User specified data that will be passed to all handlers. + */ + void *userData; + /** * Map containing all registered command names and the corresponding * handler @@ -278,7 +285,8 @@ public: * @return the state of the currently active Handler instance or STATE_NONE * if no handler is on the stack. */ - State currentState() { + State currentState() + { return stack.empty() ? STATE_NONE : stack.top().handler->state; } @@ -288,7 +296,8 @@ public: * @return the name of the command currently being handled by the active * Handler instance or an empty string if no handler is currently active. */ - std::string currentName() { + std::string currentName() + { return stack.empty() ? std::string{} : stack.top().handler->name; } @@ -297,17 +306,33 @@ public: * * @return true if the handler allows arbitrary children, false otherwise. */ - bool currentArbitraryChildren() { + bool currentArbitraryChildren() + { return stack.empty() ? false : stack.top().descr->arbitraryChildren; } - // TODO: Change signature - void start(std::string name, char **attrs); + /** + * Function that should be called whenever a new command starts. + * + * @param name is the name of the command. + * @param args is a map from strings to variants (argument name and value). + */ + void start(std::string name, const Variant &args); + /** + * Function called whenever a command ends. + */ void end(); - // TODO: Change signature - void data(const char *data, int len); + /** + * Function that should be called whenever data is available for the + * command. + * + * @param data is the data that should be passed to the handler. + * @param field is the field number (the interpretation of this value + * depends on the format that is being parsed). + */ + void data(const std::string &data, int field = 0); }; } } diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index 42e0dd4..afc7f14 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -20,6 +20,7 @@ #include +#include #include #include "XmlParser.hpp" @@ -44,30 +45,54 @@ static const State STATE_CONSTANT = 201; static const State STATE_ENUM = 202; static const State STATE_STRUCT = 203; -static Handler* createTestHandler(const ParserContext &ctx, - std::string name, State state, - State parentState, bool isChild) +class TestHandler : public Handler { +public: + using Handler::Handler; + + void start(const Variant &args) override + { + std::cout << this->name << ": start (isChild: " << (this->isChild) + << ", args: " << args << ")" << std::endl; + } + + void end() override + { + // TODO + } + + void data(const std::string &data, int field) override + { + std::cout << this->name << ": data \"" << data << "\"" << std::endl; + } + + void child(std::shared_ptr handler) override + { + // TODO + } +}; + +static Handler *createTestHandler(const ParserContext &ctx, std::string name, + State state, State parentState, bool isChild) { - return nullptr; + return new TestHandler{ctx, name, state, parentState, isChild}; } static const std::multimap XML_HANDLERS{ - /* Documents */ - {"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, - {"head", {{STATE_DOCUMENT}, createTestHandler, STATE_HEAD}}, - {"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, - - /* Special commands */ - {"use", {{STATE_HEAD}, createTestHandler, STATE_USE}}, - {"include", {{STATE_ALL}, createTestHandler, STATE_INCLUDE}}, - {"inline", {{STATE_ALL}, createTestHandler, STATE_INLINE}}, - - /* Typesystem definitions */ - {"types", {{STATE_NONE, STATE_HEAD}, createTestHandler, STATE_TYPES}}, - {"enum", {{STATE_TYPES}, createTestHandler, STATE_ENUM}}, - {"struct", {{STATE_TYPES}, createTestHandler, STATE_STRUCT}}, - {"constant", {{STATE_TYPES}, createTestHandler, STATE_CONSTANT}} -}; + /* Documents */ + {"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, + {"head", {{STATE_DOCUMENT}, createTestHandler, STATE_HEAD}}, + {"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, + + /* Special commands */ + {"use", {{STATE_HEAD}, createTestHandler, STATE_USE}}, + {"include", {{STATE_ALL}, createTestHandler, STATE_INCLUDE}}, + {"inline", {{STATE_ALL}, createTestHandler, STATE_INLINE}}, + + /* Typesystem definitions */ + {"typesystem", {{STATE_NONE, STATE_HEAD}, createTestHandler, STATE_TYPES}}, + {"enum", {{STATE_TYPES}, createTestHandler, STATE_ENUM}}, + {"struct", {{STATE_TYPES}, createTestHandler, STATE_STRUCT}}, + {"constant", {{STATE_TYPES}, createTestHandler, STATE_CONSTANT}}}; /** * Wrapper class around the XML_Parser pointer which safely frees it whenever @@ -89,8 +114,7 @@ public: * @param encoding is the protocol-defined encoding passed to expat (or * nullptr if expat should determine the encoding by itself). */ - ScopedExpatXmlParser(const XML_Char *encoding) - : parser(nullptr) + ScopedExpatXmlParser(const XML_Char *encoding) : parser(nullptr) { parser = XML_ParserCreate(encoding); if (!parser) { @@ -116,28 +140,36 @@ public: XML_Parser operator&() { return parser; } }; +/* Adapter Expat -> ParserStack */ + static void xmlStartElementHandler(void *userData, const XML_Char *name, const XML_Char **attrs) { - std::cout << "start tag: " << name << std::endl; + Variant::mapType args; const XML_Char **attr = attrs; while (*attr) { - std::cout << "\t" << *attr; - attr++; - std::cout << " -> " << *attr << std::endl; - attr++; + const std::string key{*(attr++)}; + args.emplace(std::make_pair(key, Variant{*(attr++)})); } + (static_cast(userData))->start(std::string(name), args); } -static void xmlEndElementHandler(void *userData, const XML_Char *name) { - std::cout << "end tag: " << name << std::endl; +static void xmlEndElementHandler(void *userData, const XML_Char *name) +{ + (static_cast(userData))->end(); } - -static void xmlCharacterDataHandler(void *userData, const XML_Char *s, int len) { - std::cout << "\tdata: " << std::string(s, len) << std::endl; +static void xmlCharacterDataHandler(void *userData, const XML_Char *s, int len) +{ + const std::string data = + Utils::trim(std::string{s, static_cast(len)}); + if (!data.empty()) { + (static_cast(userData))->data(data); + } } +/* Class XmlParser */ + std::set XmlParser::mimetypes() { return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; @@ -147,7 +179,11 @@ Rooted XmlParser::parse(std::istream &is, ParserContext &ctx) { // Create the parser object ScopedExpatXmlParser p{"UTF-8"}; - XML_SetUserData(&p, &ctx); + + // Create the parser stack instance and pass the reference to the state + // machine descriptor + ParserStack stack{ctx, XML_HANDLERS}; + XML_SetUserData(&p, &stack); // Set the callback functions XML_SetStartElementHandler(&p, xmlStartElementHandler); diff --git a/test/core/parser/ParserStackTest.cpp b/test/core/parser/ParserStackTest.cpp index 1f4a4e2..5dab979 100644 --- a/test/core/parser/ParserStackTest.cpp +++ b/test/core/parser/ParserStackTest.cpp @@ -39,28 +39,24 @@ class TestHandler : public Handler { public: using Handler::Handler; - void start(char **attrs) override + void start(const Variant &args) override { startCount++; -// std::cout << this->name << ": start (isChild: " << (this->isChild) << ")" << std::endl; } void end() override { endCount++; -// std::cout << this->name << ": end " << std::endl; } - void data(const char *data, int len) override + void data(const std::string &data, int field) override { dataCount++; -// std::cout << this->name << ": data \"" << std::string{data, static_cast(len)} << "\"" << std::endl; } void child(std::shared_ptr handler) override { childCount++; -// std::cout << this->name << ": has child \"" << handler->name << "\"" << std::endl; } }; @@ -93,7 +89,7 @@ TEST(ParserStack, simpleTest) ASSERT_EQ(STATE_NONE, s.currentState()); s.start("document", nullptr); - s.data("test1", 5); + s.data("test1"); ASSERT_EQ("document", s.currentName()); ASSERT_EQ(STATE_DOCUMENT, s.currentState()); @@ -101,7 +97,7 @@ TEST(ParserStack, simpleTest) ASSERT_EQ(1, dataCount); s.start("body", nullptr); - s.data("test2", 5); + s.data("test2"); ASSERT_EQ("body", s.currentName()); ASSERT_EQ(STATE_BODY, s.currentState()); ASSERT_EQ(2, startCount); @@ -123,7 +119,7 @@ TEST(ParserStack, simpleTest) ASSERT_EQ(STATE_DOCUMENT, s.currentState()); s.start("body", nullptr); - s.data("test3", 5); + s.data("test3"); ASSERT_EQ("body", s.currentName()); ASSERT_EQ(STATE_BODY, s.currentState()); s.end(); diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp index 98a5a34..ecc9438 100644 --- a/test/plugins/xml/XmlParserTest.cpp +++ b/test/plugins/xml/XmlParserTest.cpp @@ -26,26 +26,14 @@ namespace ousia { namespace parser { namespace xml { -struct TestParserContext : public ParserContext { - -private: - Logger log; - Registry r; - Scope s; - -public: - TestParserContext() : ParserContext(s, r, log), r(log), s(nullptr) {}; - -}; - TEST(XmlParser, mismatchedTagException) { - TestParserContext ctx; + StandaloneParserContext ctx; XmlParser p; bool hadException = false; try { - p.parse("data\n", ctx); + p.parse("\n", ctx); } catch (ParserException ex) { ASSERT_EQ(2, ex.line); @@ -55,19 +43,27 @@ TEST(XmlParser, mismatchedTagException) ASSERT_TRUE(hadException); } -const char* TEST_DATA = "\n" - "\n" - " \n" - "\n"; +const char *TEST_DATA = + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " Dies ist ein Test>\n" + " \n" + "\n"; TEST(XmlParser, namespaces) { - TestParserContext ctx; + StandaloneParserContext ctx; XmlParser p; p.parse(TEST_DATA, ctx); } - } } } -- cgit v1.2.3