From 6decad0b8e7e369bd8215f31a45dd3eae982d2a9 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 21 Jan 2015 01:17:49 +0100 Subject: Some further refactoring -- renamed Scope to ParserScope, got rid of parser namespace, added new functionality to RegistryClass, wrote documentation, added function for extracting file extensions to Utils --- CMakeLists.txt | 3 +- src/core/Registry.cpp | 69 +++- src/core/Registry.hpp | 115 ++++++- src/core/common/Utils.cpp | 23 ++ src/core/common/Utils.hpp | 20 ++ src/core/parser/Parser.cpp | 13 +- src/core/parser/Parser.hpp | 101 ++---- src/core/parser/ParserContext.cpp | 36 ++ src/core/parser/ParserContext.hpp | 92 +++++ src/core/parser/ParserScope.cpp | 162 +++++++++ src/core/parser/ParserScope.hpp | 427 ++++++++++++++++++++++++ src/core/parser/ParserStack.cpp | 2 - src/core/parser/ParserStack.hpp | 5 +- src/core/parser/Scope.cpp | 188 ----------- src/core/parser/Scope.hpp | 481 --------------------------- src/plugins/css/CSSParser.cpp | 8 +- src/plugins/css/CSSParser.hpp | 20 +- src/plugins/xml/XmlParser.cpp | 12 +- src/plugins/xml/XmlParser.hpp | 18 +- test/core/RegistryTest.cpp | 67 +++- test/core/common/UtilsTest.cpp | 19 ++ test/core/parser/ParserStackTest.cpp | 3 - test/core/parser/StandaloneParserContext.hpp | 7 +- test/plugins/css/CSSParserTest.cpp | 4 - test/plugins/xml/XmlParserTest.cpp | 5 +- 25 files changed, 1060 insertions(+), 840 deletions(-) create mode 100644 src/core/parser/ParserContext.cpp create mode 100644 src/core/parser/ParserContext.hpp create mode 100644 src/core/parser/ParserScope.cpp create mode 100644 src/core/parser/ParserScope.hpp delete mode 100644 src/core/parser/Scope.cpp delete mode 100644 src/core/parser/Scope.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d19b18..2b8efc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,8 +145,9 @@ ADD_LIBRARY(ousia_core src/core/model/Project src/core/model/Typesystem src/core/parser/Parser + src/core/parser/ParserContext + src/core/parser/ParserScope src/core/parser/ParserStack - src/core/parser/Scope src/core/resource/Resource src/core/resource/ResourceLocator # src/core/script/ScriptEngine diff --git a/src/core/Registry.cpp b/src/core/Registry.cpp index 86665a2..c42a97a 100644 --- a/src/core/Registry.cpp +++ b/src/core/Registry.cpp @@ -16,6 +16,8 @@ along with this program. If not, see . */ +#include +#include #include #include #include @@ -24,32 +26,71 @@ namespace ousia { -using namespace parser; - /* Class Registry */ -void Registry::registerParser(parser::Parser &parser) +void Registry::registerParser(const std::set &mimetypes, + const RttiSet &types, Parser *parser) +{ + for (const std::string &mimetype : mimetypes) { + // Make sure no other parser was given for this mimetype + auto it = parsers.find(mimetype); + if (it != parsers.end()) { + throw OusiaException{std::string{"Parser for mimetype "} + + mimetype + + std::string{" already registered."}}; + } + + // Store a reference at the parser and a copy of the given RttiSet + parsers[mimetype] = std::pair{parser, types}; + } +} + +static const std::pair NullParser{nullptr, RttiSet{}}; + +const std::pair &Registry::getParserForMimetype( + const std::string &mimetype) const { - parsers.push_back(&parser); - for (const auto &mime : parser.mimetypes()) { - //TODO: This does not allow for multiple parsers with the same mimetype. - // Is that how its supposed to be? - parserMimetypes.insert(std::make_pair(mime, &parser)); + const auto it = parsers.find(mimetype); + if (it != parsers.end()) { + return it->second; + } + return NullParser; +} + +void Registry::registerExtension(const std::string &extension, + const std::string &mimetype) +{ + // Always use extensions in lower case + std::string ext = Utils::toLower(extension); + + // Make sure the extension is unique + auto it = extensions.find(ext); + if (it != extensions.end()) { + throw OusiaException{std::string{"Extension "} + extension + + std::string{" already registered."}}; } + + // Register the mimetype + extensions[ext] = mimetype; } -Parser *Registry::getParserForMimetype(const std::string &mimetype) const +std::string Registry::getMimetypeForExtension( + const std::string &extension) const { - const auto it = parserMimetypes.find(mimetype); - if (it != parserMimetypes.end()) { + // Always use extensions in lower case + std::string ext = Utils::toLower(extension); + + // Try to find the extension + auto it = extensions.find(ext); + if (it != extensions.end()) { return it->second; } - return nullptr; + return std::string{}; } -void Registry::registerResourceLocator(ResourceLocator &locator) +void Registry::registerResourceLocator(ResourceLocator *locator) { - locators.push_back(&locator); + locators.push_back(locator); } bool Registry::locateResource(Resource &resource, const std::string &path, diff --git a/src/core/Registry.hpp b/src/core/Registry.hpp index 40eede1..965f336 100644 --- a/src/core/Registry.hpp +++ b/src/core/Registry.hpp @@ -16,37 +16,126 @@ along with this program. If not, see . */ +/** + * @file Registry.hpp + * + * Class used for registering plugin classes. The Registry is one of the central + * classes needed for parsing and transforming an Ousía document. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + #ifndef _OUSIA_REGISTRY_HPP_ #define _OUSIA_REGISTRY_HPP_ #include +#include #include +#include #include namespace ousia { -// TODO: Add support for ScriptEngine type - -namespace parser { +// Forward declarations class Parser; -} class ResourceLocator; +/** + * The Registry class is the central class which is used to store references to + * all available plugins. + */ class Registry { private: - std::vector parsers; - std::map parserMimetypes; - + /** + * Mapping between parser mimetypes and pairs of parsers and their supported + * Rtti types. + */ + std::map> parsers; + + /** + * Map from file extensions to registered mimetypes. + */ + std::map extensions; + + /** + * List containing all registered ResourceLocator instances. + */ std::vector locators; public: - void registerParser(parser::Parser &parser); - - parser::Parser *getParserForMimetype(const std::string &mimetype) const; - - void registerResourceLocator(ResourceLocator &locator); - + /** + * Registers a new parser instance for the given set of mimetypes. Throws + * an exception if a parser is already registered for one of the mimetypes. + * + * @param mimetypes is a set of mimetypes for which the Parser should be + * registered. + * @param types is a set of node the parser is known to return. This + * information is needed in order to prevent inclusion of the wrong Node + * types + * @param parser is the parser instance that is registered for the given + * mimetypes. + */ + void registerParser(const std::set &mimetypes, + const RttiSet &types, Parser *parser); + + /** + * Returns a pointer pointing at a Parser that was registered for handling + * the given mimetype. + * + * @param mimetype is the mimetype for which a Parser instance should be + * looked up. + * @return a pair containing a pointer at the parser and the RttiTypes + * supported by the parser. The pointer is set to a nullptr if no such + * parser could be found. + */ + const std::pair &getParserForMimetype( + const std::string &mimetype) const; + + /** + * Registers a file extension with a certain mimetype. Throws an exception + * if a mimetype is already registered for this extension. + * + * @param extension is the file extension for which the mimetype should be + * registered. The extension has to be provided without a leading dot. The + * extensions are handled case insensitive. + * @param mimetype is the mimetype that should be registered for the + * extension. + */ + void registerExtension(const std::string &extension, + const std::string &mimetype); + + /** + * Returns the mimetype for the given extension. + * + * @param extension is the file extension for which the mimetype should be + * looked up. The extension has to be provided without a leading dot. The + * extensions are handled case insensitive. + * @return the registered mimetype or an empty string of none was found. + */ + std::string getMimetypeForExtension(const std::string &extension) const; + + /** + * Registers a ResourceLocator instance that should be used for locating + * resources. Two registered ResourceLocator should not be capable of + * accessing Resources at the same location. If this happens, the resource + * locator that was registered first has precedence. + * + * @param locator is the ResourceLocator instance that should be registered. + */ + void registerResourceLocator(ResourceLocator *locator); + + /** + * Locates a resource using the registered ResourceLocator instances. + * + * @param resource is the resource descriptor to which the result will be + * written. + * @param path is the path under which the resource should be looked up. + * @param type is the ResourceType. Specifying a resource type may help to + * locate the resource. + * @param relativeTo is another resource relative to which the resource may + * be looked up. + */ bool locateResource(Resource &resource, const std::string &path, ResourceType type = ResourceType::UNKNOWN, const Resource &relativeTo = NullResource) const; diff --git a/src/core/common/Utils.cpp b/src/core/common/Utils.cpp index 5fde29c..c8fcdc6 100644 --- a/src/core/common/Utils.cpp +++ b/src/core/common/Utils.cpp @@ -17,7 +17,9 @@ */ #include +#include #include +#include #include "Utils.hpp" @@ -74,5 +76,26 @@ std::vector Utils::split(const std::string &s, char delim) return res; } +std::string Utils::toLower(std::string s) +{ + std::transform(s.begin(), s.end(), s.begin(), tolower); + return s; +} + +std::string Utils::extractFileExtension(const std::string &filename) +{ + size_t n = 0; + for (ssize_t i = filename.size() - 1; i >= 0; i--) { + if (filename[i] == '/' || filename[i] == '\\') { + return std::string{}; + } + if (filename[i] == '.') { + return toLower(filename.substr(i + 1, n)); + } + n++; + } + return std::string{}; +} + } diff --git a/src/core/common/Utils.hpp b/src/core/common/Utils.hpp index 1f7f142..22e0fd3 100644 --- a/src/core/common/Utils.hpp +++ b/src/core/common/Utils.hpp @@ -114,6 +114,26 @@ public: * @return a vector of strings containing the splitted sub-strings. */ static std::vector split(const std::string &s, char delim); + + /** + * Converts the given string to lowercase (only works for ANSI characters). + * + * @param s is the string that should be converted to lowercase. + * @return s in lowercase. + */ + static std::string toLower(std::string s); + + /** + * Reads the file extension of the given filename. + * + * @param filename is the filename from which the extension should be + * extracted. + * @return the extension, excluding any leading dot. The extension is + * defined as the substring after the last dot in the given string, if the + * dot is after a slash or backslash. The extension is converted to + * lowercase. + */ + static std::string extractFileExtension(const std::string &filename); }; } diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp index b5d9656..2978669 100644 --- a/src/core/parser/Parser.cpp +++ b/src/core/parser/Parser.cpp @@ -16,16 +16,23 @@ along with this program. If not, see . */ +#include + #include "Parser.hpp" namespace ousia { -namespace parser { + +/* Class Parser */ + +Rooted Parser::parse(CharReader &reader, ParserContext &ctx) +{ + return doParse(reader, ctx); +} Rooted Parser::parse(const std::string &str, ParserContext &ctx) { CharReader reader{str}; - return parse(reader, ctx); -} + return doParse(reader, ctx); } } diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp index 049ee4e..e4419f5 100644 --- a/src/core/parser/Parser.hpp +++ b/src/core/parser/Parser.hpp @@ -32,94 +32,52 @@ #include #include -#include -#include -#include -#include +#include #include -#include - -#include "Scope.hpp" namespace ousia { -namespace parser { -// TODO: Implement a proper Mimetype class +// Forward declarations +class CharReader; +class ParserContext; /** - * Struct containing the objects that are passed to a parser instance. + * 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. */ -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; - +class Parser { +protected: /** - * Reference to the Manager the parser should append nodes to. + * 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 reader is a reference to the CharReader that should be used. + * @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. */ - Manager &manager; + virtual Rooted doParse(CharReader &reader, ParserContext &ctx) = 0; +public: /** - * Project instance into which the new content should be parsed. + * Default constructor. */ - Rooted project; + Parser() {} /** - * 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. - * @param manager is a Reference to the Manager the parser should append - * nodes to. - * @param project is the project into which the content should be parsed. + * No copy construction. */ - ParserContext(Scope &scope, Registry ®istry, Logger &logger, - Manager &manager, Handle project) - : scope(scope), - registry(registry), - logger(logger), - manager(manager), - project(project){}; -}; - -/** - * 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 destructor. */ - virtual std::set mimetypes() - { - return std::set{}; - }; + virtual ~Parser(){}; /** * Parses the given input stream and returns a corresponding node for @@ -132,9 +90,9 @@ public: * @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. + * a corresponding ParserException must be thrown by the parser. */ - virtual Rooted parse(CharReader &reader, ParserContext &ctx) = 0; + Rooted parse(CharReader &reader, ParserContext &ctx); /** * Parses the given string and returns a corresponding node for @@ -152,7 +110,6 @@ public: Rooted parse(const std::string &str, ParserContext &ctx); }; } -} #endif /* _OUSIA_PARSER_HPP_ */ diff --git a/src/core/parser/ParserContext.cpp b/src/core/parser/ParserContext.cpp new file mode 100644 index 0000000..fa26c59 --- /dev/null +++ b/src/core/parser/ParserContext.cpp @@ -0,0 +1,36 @@ +/* + 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 "ParserContext.hpp" + +namespace ousia { + +/* Class ParserContext */ + +ParserContext::ParserContext(ParserScope &scope, Registry ®istry, + Logger &logger, Manager &manager, + Handle project) + : scope(scope), + registry(registry), + logger(logger), + manager(manager), + project(project) +{ +} +} + diff --git a/src/core/parser/ParserContext.hpp b/src/core/parser/ParserContext.hpp new file mode 100644 index 0000000..88d1f52 --- /dev/null +++ b/src/core/parser/ParserContext.hpp @@ -0,0 +1,92 @@ +/* + 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 ParserContext.hpp + * + * Contains the ParserContext, a struct containing references to all the + * important structures a Parser needs to access while parsing an input stream. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +#ifndef _OUSIA_PARSER_CONTEXT_HPP_ +#define _OUSIA_PARSER_CONTEXT_HPP_ + +#include +#include +#include + +namespace ousia { + +// Forward declaration +class Logger; +class ParserScope; +class Registry; + +/** + * Struct containing the objects that are passed to a parser instance. + */ +struct ParserContext { + /** + * Reference to the ParserScope instance that should be used within the parser. + */ + ParserScope &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; + + /** + * Reference to the Manager the parser should append nodes to. + */ + Manager &manager; + + /** + * Project instance into which the new content should be parsed. + */ + Rooted project; + + /** + * Constructor of the ParserContext class. + * + * @param scope is a reference to the ParserScope 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. + * @param manager is a Reference to the Manager the parser should append + * nodes to. + * @param project is the project into which the content should be parsed. + */ + ParserContext(ParserScope &scope, Registry ®istry, Logger &logger, + Manager &manager, Handle project); +}; + +} + +#endif /* _OUSIA_PARSER_CONTEXT_HPP_ */ + diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp new file mode 100644 index 0000000..b236a1f --- /dev/null +++ b/src/core/parser/ParserScope.cpp @@ -0,0 +1,162 @@ +/* + 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 "ParserScope.hpp" + +namespace ousia { + +/* Class ParserScopeBase */ + +Rooted ParserScopeBase::resolve(const std::vector &path, + const Rtti &type, Logger &logger) +{ + // Go up the stack and try to resolve the + for (auto it = nodes.rbegin(); it != nodes.rend(); it++) { + std::vector res = (*it)->resolve(path, type); + + // Abort if the object could not be resolved + if (res.empty()) { + continue; + } + + // Log an error if the object is not unique + if (res.size() > 1) { + logger.error(std::string("The reference \"") + + Utils::join(path, ".") + ("\" is ambigous!")); + logger.note("Referenced objects are:"); + for (const ResolutionResult &r : res) { + logger.note(Utils::join(r.path(), ".")); + } + } + return res[0].node; + } + return nullptr; +} + +/* Class DeferredResolution */ + +DeferredResolution::DeferredResolution(const NodeVector &nodes, + const std::vector &path, + const Rtti &type, + ResolutionResultCallback resultCallback, + const SourceLocation &location) + : scope(nodes), + resultCallback(resultCallback), + path(path), + type(type), + location(location) +{ +} + +bool DeferredResolution::resolve(Logger &logger) +{ + Rooted res = scope.resolve(path, type, logger); + if (res != nullptr) { + try { + resultCallback(res, logger); + } + catch (LoggableException ex) { + logger.log(ex); + } + return true; + } + return false; +} + +/* Class ParserScope */ + +void ParserScope::push(Handle node) { nodes.push_back(node); } + +void ParserScope::pop() { nodes.pop_back(); } + +Rooted ParserScope::getRoot() const { return nodes.front(); } + +Rooted ParserScope::getLeaf() { return nodes.back(); } + +bool ParserScope::resolve(const std::vector &path, const Rtti &type, + Logger &logger, ResolutionImposterCallback imposterCallback, + ResolutionResultCallback resultCallback, + const SourceLocation &location) +{ + if (!resolve(path, type, logger, resultCallback, location)) { + resultCallback(imposterCallback(), logger); + return false; + } + return true; +} + +bool ParserScope::resolve(const std::vector &path, const Rtti &type, + Logger &logger, ResolutionResultCallback resultCallback, + const SourceLocation &location) +{ + Rooted res = ParserScopeBase::resolve(path, type, logger); + if (res != nullptr) { + try { + resultCallback(res, logger); + } + catch (LoggableException ex) { + logger.log(ex, location); + } + return true; + } + deferred.emplace_back(nodes, path, type, resultCallback, location); + return false; +} + +bool ParserScope::performDeferredResolution(Logger &logger) +{ + // Repeat the resolution process as long as something has changed in the + // last iteration (resolving a node may cause other nodes to be resolvable). + while (true) { + // Iterate over all deferred resolution processes, + bool hasChange = false; + for (auto it = deferred.begin(); it != deferred.end();) { + if (it->resolve(logger)) { + it = deferred.erase(it); + hasChange = true; + } else { + it++; + } + } + + // Abort if nothing has changed in the last iteration + if (!hasChange) { + break; + } + } + + // We were successful if there are no more deferred resolutions + if (deferred.empty()) { + return true; + } + + // Output error messages for all elements for which resolution did not + // succeed. + for (const auto &failed : deferred) { + logger.error(std::string("Could not resolve ") + failed.type.name + + std::string(" \"") + Utils::join(failed.path, ".") + + std::string("\""), + failed.location); + } + deferred.clear(); + return false; +} +} + diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp new file mode 100644 index 0000000..a759738 --- /dev/null +++ b/src/core/parser/ParserScope.hpp @@ -0,0 +1,427 @@ +/* + 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_HPP_ +#define _OUSIA_PARSER_SCOPE_HPP_ + +#include +#include +#include + +#include +#include +#include +#include + +/** + * @file ParserScope.hpp + * + * Contains the ParserScope class used for resolving references based on the current + * parser state. + * + * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) + */ + +namespace ousia { + +// Forward declaration +class CharReader; +class Registry; +class Logger; +class ParserScope; + +/** + * Callback function type used for creating a dummy object while no correct + * object is available for resolution. + */ +using ResolutionImposterCallback = std::function()>; + +/** + * Callback function type called whenever the result of a resolution is + * available. + */ +using ResolutionResultCallback = + std::function, Logger &logger)>; + +/** + * Base class for the + */ +class ParserScopeBase { +protected: + /** + * List containing all nodes currently on the scope, with the newest nodes + * being pushed to the back of the list. + */ + NodeVector nodes; + +public: + /** + * Default constructor, creates an empty ParserScope instance. + */ + ParserScopeBase() {} + + /** + * Creates a new instance of the ParserScopeBase class, copying the the given + * nodes as initial start value of the node stack. This could for example + * be initialized with the path of a node. + * + * @param nodes is a node vector containing the current node stack. + */ + ParserScopeBase(const NodeVector &nodes) : nodes(nodes) {} + + /** + * Tries to resolve a node for the given type and path for all nodes that + * are currently in the stack, starting with the topmost node on the stack. + * + * @param path is the path for which a node should be resolved. + * @param type is the type of the node that should be resolved. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @return a reference at a resolved node or nullptr if no node could be + * found. + */ + Rooted resolve(const std::vector &path, + const Rtti &type, Logger &logger); +}; + +/** + * Class used for representing a deferred resolution. A deferred resolution is + * triggered whenever an object cannot be resolved, but there may be a chance + * that it can be resolved in the future. This happens e.g. if a document is + * just being parsed and the object that is being refered to has not been + * reached yet. + */ +class DeferredResolution { +private: + /** + * Copy of the scope at the time when the resolution was first triggered. + */ + ParserScopeBase scope; + + /** + * Callback function to be called when an element is successfully resolved. + */ + ResolutionResultCallback resultCallback; + +public: + /** + * Path queried for the resolution. + */ + std::vector path; + + /** + * Reference at the type of the object that should be resolved. + */ + const Rtti &type; + + /** + * Position at which the resolution was triggered. + */ + const SourceLocation location; + + /** + * Constructor of the DeferredResolutionScope class. Copies the given + * arguments. + * + * @param nodes is a reference at the current internal node stack of the + * ParserScope class. + * @param path is the path that was queried when the resolution failed the + * first time. + * @param type is the Rtti of the element that should be queried. + * @param resultCallback is the callback function that should be called if + * the desired element has indeed been found. + * @param location is the location at which the resolution was triggered. + */ + DeferredResolution(const NodeVector &nodes, + const std::vector &path, + const Rtti &type, + ResolutionResultCallback resultCallback, + const SourceLocation &location = SourceLocation{}); + + /** + * Performs the actual deferred resolution and calls the resultCallback + * callback function in case the resolution is sucessful. In this case + * returns true, false otherwise. + * + * @param logger is the logger instance to which error messages should be + * logged. + * @return true if the resolution was successful, false otherwise. + */ + bool resolve(Logger &logger); +}; + +/** + * Provides an interface for document parsers to resolve references based on the + * current position in the created document tree. The ParserScope class itself + * is represented as a chain of ParserScope objects where each element has a + * reference to a Node object attached to it. + */ +class ParserScope : public ParserScopeBase { +private: + /** + * List containing all deferred resolution descriptors. + */ + std::list deferred; + +public: + /** + * Default constructor of the ParserScope class, creates an empty ParserScope with no + * element on the internal stack. + */ + ParserScope() {} + + /** + * Pushes a new node onto the scope. + * + * @param node is the node that should be used for local lookup. + */ + void push(Handle node); + + /** + * Removes the last pushed node from the scope. + */ + void pop(); + + /** + * Returns the top-most Node instance in the ParserScope hirarchy. + * + * @return a reference at the root node. + */ + Rooted getRoot() const; + + /** + * Returns the bottom-most Node instance in the ParserScope hirarchy, e.g. the + * node that was pushed last onto the stack. + * + * @return a reference at the leaf node. + */ + Rooted getLeaf(); + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * Calls the "imposterCallback" function for obtaining a temporary result if + * a node cannot be resolved right now. The "resultCallback" is at most + * called twice: Once when this method is called (probably with the + * temporary) and another time if the resolution turned out to be successful + * at a later point in time. + * + * @param path is the path for which a node should be resolved. + * @param type is the type of the node that should be resolved. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param imposterCallback is the callback function that is called if + * the node cannot be resolved at this moment. It gives the caller the + * possibility to create an imposter (a temporary object) that may be used + * later in the resolution process. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called at least once + * either with the imposter (if the resolution was not successful) or the + * resolved object directly when this function is called. If the resolution + * was not successful the first time, it may be called another time later + * in the context of the "performDeferredResolution" function. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + bool resolve(const std::vector &path, const Rtti &type, + Logger &logger, ResolutionImposterCallback imposterCallback, + ResolutionResultCallback resultCallback, + const SourceLocation &location = SourceLocation{}); + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * The "resultCallback" is called when the resolution was successful, which + * may be at a later point in time. + * + * @param path is the path for which a node should be resolved. + * @param type is the type of the node that should be resolved. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called once the + * resolution was successful. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + bool resolve(const std::vector &path, const Rtti &type, + Logger &logger, ResolutionResultCallback resultCallback, + const SourceLocation &location = SourceLocation{}); + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * Calls the "imposterCallback" function for obtaining a temporary result if + * a node cannot be resolved right now. The "resultCallback" is at most + * called twice: Once when this method is called (probably with the + * temporary) and another time if the resolution turned out to because + * successful at a later point in time. + * + * @tparam T is the type of the node that should be resolved. + * @param path is the path for which a node should be resolved. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param imposterCallback is the callback function that is called if + * the node cannot be resolved at this moment. It gives the caller the + * possibility to create an imposter (a temporary object) that may be used + * later in the resolution process. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called at least once + * either with the imposter (if the resolution was not successful) or the + * resolved object directly when this function is called. If the resolution + * was not successful the first time, it may be called another time later + * in the context of the "performDeferredResolution" function. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + template + bool resolve(const std::vector &path, Logger &logger, + std::function()> imposterCallback, + std::function, Logger &)> resultCallback, + const SourceLocation &location = SourceLocation{}) + { + return resolve( + path, typeOf(), logger, + [imposterCallback]() -> Rooted { return imposterCallback(); }, + [resultCallback](Handle node, Logger &logger) { + resultCallback(node.cast(), logger); + }, + location); + } + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * The "resultCallback" is called when the resolution was successful, which + * may be at a later point in time. + * + * @tparam T is the type of the node that should be resolved. + * @param path is the path for which a node should be resolved. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called once the + * resolution was successful. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + template + bool resolve(const std::vector &path, Logger &logger, + std::function, Logger &)> resultCallback, + const SourceLocation &location = SourceLocation{}) + { + return resolve(path, typeOf(), logger, + [resultCallback](Handle node, Logger &logger) { + resultCallback(node.cast(), logger); + }, + location); + } + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * Calls the "imposterCallback" function for obtaining a temporary result if + * a node cannot be resolved right now. The "resultCallback" is at most + * called twice: Once when this method is called (probably with the + * temporary) and another time if the resolution turned out to because + * successful at a later point in time. + * + * @tparam T is the type of the node that should be resolved. + * @param name is the path for which a node should be resolved. The name is + * split at '.' to form a path. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param imposterCallback is the callback function that is called if + * the node cannot be resolved at this moment. It gives the caller the + * possibility to create an imposter (a temporary object) that may be used + * later in the resolution process. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called at least once + * either with the imposter (if the resolution was not successful) or the + * resolved object directly when this function is called. If the resolution + * was not successful the first time, it may be called another time later + * in the context of the "performDeferredResolution" function. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + template + bool resolve(const std::string &name, Logger &logger, + std::function()> imposterCallback, + std::function, Logger &)> resultCallback, + const SourceLocation &location = SourceLocation{}) + { + return resolve(Utils::split(name, '.'), logger, imposterCallback, + resultCallback, location); + } + + /** + * Tries to resolve a node for the given type and path for all nodes + * currently on the stack, starting with the topmost node on the stack. + * The "resultCallback" is called when the resolution was successful, which + * may be at a later point in time. + * + * @tparam T is the type of the node that should be resolved. + * @param name is the path for which a node should be resolved. The name is + * split at '.' to form a path. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @param resultCallback is the callback function to which the result of + * the resolution process is passed. This function is called once the + * resolution was successful. + * @param location is the location in the current source file in which the + * resolution was triggered. + * @return true if the resolution was immediately successful. This does not + * mean, that the resolved object does not exist, as it may be resolved + * later. + */ + template + bool resolve(const std::string &name, Logger &logger, + std::function, Logger &)> resultCallback, + const SourceLocation &location = SourceLocation{}) + { + return resolve(Utils::split(name, '.'), logger, resultCallback, + location); + } + + /** + * Tries to resolve all currently deferred resolution steps. The list of + * pending deferred resolutions is cleared after this function has run. + * + * @param logger is the logger instance into which errors should be logged. + */ + bool performDeferredResolution(Logger &logger); +}; +} + +#endif /* _OUSIA_PARSER_SCOPE_HPP_ */ + diff --git a/src/core/parser/ParserStack.cpp b/src/core/parser/ParserStack.cpp index 9cf782f..3792ee8 100644 --- a/src/core/parser/ParserStack.cpp +++ b/src/core/parser/ParserStack.cpp @@ -24,7 +24,6 @@ #include namespace ousia { -namespace parser { /* A default handler */ @@ -186,5 +185,4 @@ void ParserStack::data(const std::string &data, int field) stack.top().handler->data(data, field); } } -} diff --git a/src/core/parser/ParserStack.hpp b/src/core/parser/ParserStack.hpp index 492ab9c..4af88f9 100644 --- a/src/core/parser/ParserStack.hpp +++ b/src/core/parser/ParserStack.hpp @@ -42,9 +42,9 @@ #include #include "Parser.hpp" +#include "ParserContext.hpp" namespace ousia { -namespace parser { /** * The State type alias is used to @@ -139,7 +139,7 @@ public: const std::string &name() { return handlerData.name; } - Scope &scope() { return handlerData.ctx.scope; } + ParserScope &scope() { return handlerData.ctx.scope; } Registry ®istry() { return handlerData.ctx.registry; } @@ -421,7 +421,6 @@ public: ParserContext &getContext() { return ctx; } }; } -} #endif /* _OUSIA_PARSER_STACK_HPP_ */ diff --git a/src/core/parser/Scope.cpp b/src/core/parser/Scope.cpp deleted file mode 100644 index 01292df..0000000 --- a/src/core/parser/Scope.cpp +++ /dev/null @@ -1,188 +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 "Scope.hpp" - -namespace ousia { -namespace parser { - -/* Class GuardedScope */ - -GuardedScope::GuardedScope(Scope *scope, Handle node) : scope(scope) -{ - scope->push(node); -} - -GuardedScope::~GuardedScope() -{ - if (scope) { - scope->pop(); - } -} - -GuardedScope::GuardedScope(GuardedScope &&s) -{ - scope = s.scope; - s.scope = nullptr; -} - -/* Class ScopeBase */ - -Rooted ScopeBase::resolve(const std::vector &path, - const Rtti &type, Logger &logger) -{ - // Go up the stack and try to resolve the - for (auto it = nodes.rbegin(); it != nodes.rend(); it++) { - std::vector res = (*it)->resolve(path, type); - - // Abort if the object could not be resolved - if (res.empty()) { - continue; - } - - // Log an error if the object is not unique - if (res.size() > 1) { - logger.error(std::string("The reference \"") + - Utils::join(path, ".") + ("\" is ambigous!")); - logger.note("Referenced objects are:"); - for (const ResolutionResult &r : res) { - logger.note(Utils::join(r.path(), ".")); - } - } - return res[0].node; - } - return nullptr; -} - -/* Class DeferredResolution */ - -DeferredResolution::DeferredResolution(const NodeVector &nodes, - const std::vector &path, - const Rtti &type, - ResolutionResultCallback resultCallback, - const SourceLocation &location) - : scope(nodes), - resultCallback(resultCallback), - path(path), - type(type), - location(location) -{ -} - -bool DeferredResolution::resolve(Logger &logger) -{ - Rooted res = scope.resolve(path, type, logger); - if (res != nullptr) { - try { - resultCallback(res, logger); - } - catch (LoggableException ex) { - logger.log(ex); - } - return true; - } - return false; -} - -/* Class Scope */ - -void Scope::push(Handle node) { nodes.push_back(node); } - -void Scope::pop() { nodes.pop_back(); } - -GuardedScope Scope::descend(Handle node) -{ - return GuardedScope{this, node}; -} - -Rooted Scope::getRoot() const { return nodes.front(); } - -Rooted Scope::getLeaf() { return nodes.back(); } - -bool Scope::resolve(const std::vector &path, const Rtti &type, - Logger &logger, ResolutionImposterCallback imposterCallback, - ResolutionResultCallback resultCallback, - const SourceLocation &location) -{ - if (!resolve(path, type, logger, resultCallback, location)) { - resultCallback(imposterCallback(), logger); - return false; - } - return true; -} - -bool Scope::resolve(const std::vector &path, const Rtti &type, - Logger &logger, ResolutionResultCallback resultCallback, - const SourceLocation &location) -{ - Rooted res = ScopeBase::resolve(path, type, logger); - if (res != nullptr) { - try { - resultCallback(res, logger); - } - catch (LoggableException ex) { - logger.log(ex, location); - } - return true; - } - deferred.emplace_back(nodes, path, type, resultCallback, location); - return false; -} - -bool Scope::performDeferredResolution(Logger &logger) -{ - // Repeat the resolution process as long as something has changed in the - // last iteration (resolving a node may cause other nodes to be resolvable). - while (true) { - // Iterate over all deferred resolution processes, - bool hasChange = false; - for (auto it = deferred.begin(); it != deferred.end();) { - if (it->resolve(logger)) { - it = deferred.erase(it); - hasChange = true; - } else { - it++; - } - } - - // Abort if nothing has changed in the last iteration - if (!hasChange) { - break; - } - } - - // We were successful if there are no more deferred resolutions - if (deferred.empty()) { - return true; - } - - // Output error messages for all elements for which resolution did not - // succeed. - for (const auto &failed : deferred) { - logger.error(std::string("Could not resolve ") + failed.type.name + std::string(" \"") + - Utils::join(failed.path, ".") + - std::string("\""), - failed.location); - } - deferred.clear(); - return false; -} -} -} diff --git a/src/core/parser/Scope.hpp b/src/core/parser/Scope.hpp deleted file mode 100644 index b9b7f80..0000000 --- a/src/core/parser/Scope.hpp +++ /dev/null @@ -1,481 +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_PARSER_SCOPE_H_ -#define _OUSIA_PARSER_SCOPE_H_ - -#include -#include -#include - -#include -#include -#include -#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 { - -// Forward declaration -class Scope; - -/** - * Callback function type used for creating a dummy object while no correct - * object is available for resolution. - */ -using ResolutionImposterCallback = std::function()>; - -/** - * Callback function type called whenever the result of a resolution is - * available. - */ -using ResolutionResultCallback = - std::function, Logger &logger)>; - -/** - * The GuardedScope 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 GuardedScope { -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 pushed onto the stack of - * the Scope instance. - */ - GuardedScope(Scope *scope, Handle node); - - /** - * Pops the Node given in the constructor form the stack of the Scope - * instance. - */ - ~GuardedScope(); - - /** - * Move constructor of the ScopedScope class. - */ - GuardedScope(GuardedScope &&); - - // No copy construction - GuardedScope(const GuardedScope &) = delete; - - /** - * Provides access at the underlying Scope instance. - */ - Scope *operator->() { return scope; } - - /** - * Provides access at the underlying Scope instance. - */ - Scope &operator*() { return *scope; } -}; - -/** - * Base class for the - */ -class ScopeBase { -protected: - /** - * List containing all nodes currently on the scope, with the newest nodes - * being pushed to the back of the list. - */ - NodeVector nodes; - -public: - /** - * Default constructor, creates an empty Scope instance. - */ - ScopeBase() {} - - /** - * Creates a new instance of the ScopeBase class, copying the the given - * nodes as initial start value of the node stack. This could for example - * be initialized with the path of a node. - * - * @param nodes is a node vector containing the current node stack. - */ - ScopeBase(const NodeVector &nodes) : nodes(nodes) {} - - /** - * Tries to resolve a node for the given type and path for all nodes that - * are currently in the stack, starting with the topmost node on the stack. - * - * @param path is the path for which a node should be resolved. - * @param type is the type of the node that should be resolved. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @return a reference at a resolved node or nullptr if no node could be - * found. - */ - Rooted resolve(const std::vector &path, - const Rtti &type, Logger &logger); -}; - -/** - * Class used for representing a deferred resolution. A deferred resolution is - * triggered whenever an object cannot be resolved, but there may be a chance - * that it can be resolved in the future. This happens e.g. if a document is - * just being parsed and the object that is being refered to has not been - * reached yet. - */ -class DeferredResolution { -private: - /** - * Copy of the scope at the time when the resolution was first triggered. - */ - ScopeBase scope; - - /** - * Callback function to be called when an element is successfully resolved. - */ - ResolutionResultCallback resultCallback; - -public: - /** - * Path queried for the resolution. - */ - std::vector path; - - /** - * Reference at the type of the object that should be resolved. - */ - const Rtti &type; - - /** - * Position at which the resolution was triggered. - */ - const SourceLocation location; - - /** - * Constructor of the DeferredResolutionScope class. Copies the given - * arguments. - * - * @param nodes is a reference at the current internal node stack of the - * Scope class. - * @param path is the path that was queried when the resolution failed the - * first time. - * @param type is the Rtti of the element that should be queried. - * @param resultCallback is the callback function that should be called if - * the desired element has indeed been found. - * @param location is the location at which the resolution was triggered. - */ - DeferredResolution(const NodeVector &nodes, - const std::vector &path, - const Rtti &type, - ResolutionResultCallback resultCallback, - const SourceLocation &location = SourceLocation{}); - - /** - * Performs the actual deferred resolution and calls the resultCallback - * callback function in case the resolution is sucessful. In this case - * returns true, false otherwise. - * - * @param logger is the logger instance to which error messages should be - * logged. - * @return true if the resolution was successful, false otherwise. - */ - bool resolve(Logger &logger); -}; - -/** - * 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 : public ScopeBase { -private: - /** - * List containing all deferred resolution descriptors. - */ - std::list deferred; - -public: - /** - * Default constructor of the Scope class, creates an empty Scope with no - * element on the internal stack. - */ - Scope() {} - - /** - * Pushes a new node onto the scope. - * - * @param node is the node that should be used for local lookup. - */ - void push(Handle node); - - /** - * Removes the last pushed node from the scope. - */ - void pop(); - - /** - * Returns a ScopedScope instance, which automatically pushes the given node - * into the Scope stack and pops it once the ScopedScope is destroyed. - */ - GuardedScope descend(Handle node); - - /** - * Returns the top-most Node instance in the Scope hirarchy. - * - * @return a reference at the root node. - */ - Rooted getRoot() const; - - /** - * 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(); - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * Calls the "imposterCallback" function for obtaining a temporary result if - * a node cannot be resolved right now. The "resultCallback" is at most - * called twice: Once when this method is called (probably with the - * temporary) and another time if the resolution turned out to be successful - * at a later point in time. - * - * @param path is the path for which a node should be resolved. - * @param type is the type of the node that should be resolved. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param imposterCallback is the callback function that is called if - * the node cannot be resolved at this moment. It gives the caller the - * possibility to create an imposter (a temporary object) that may be used - * later in the resolution process. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called at least once - * either with the imposter (if the resolution was not successful) or the - * resolved object directly when this function is called. If the resolution - * was not successful the first time, it may be called another time later - * in the context of the "performDeferredResolution" function. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - bool resolve(const std::vector &path, const Rtti &type, - Logger &logger, ResolutionImposterCallback imposterCallback, - ResolutionResultCallback resultCallback, - const SourceLocation &location = SourceLocation{}); - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * The "resultCallback" is called when the resolution was successful, which - * may be at a later point in time. - * - * @param path is the path for which a node should be resolved. - * @param type is the type of the node that should be resolved. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called once the - * resolution was successful. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - bool resolve(const std::vector &path, const Rtti &type, - Logger &logger, ResolutionResultCallback resultCallback, - const SourceLocation &location = SourceLocation{}); - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * Calls the "imposterCallback" function for obtaining a temporary result if - * a node cannot be resolved right now. The "resultCallback" is at most - * called twice: Once when this method is called (probably with the - * temporary) and another time if the resolution turned out to because - * successful at a later point in time. - * - * @tparam T is the type of the node that should be resolved. - * @param path is the path for which a node should be resolved. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param imposterCallback is the callback function that is called if - * the node cannot be resolved at this moment. It gives the caller the - * possibility to create an imposter (a temporary object) that may be used - * later in the resolution process. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called at least once - * either with the imposter (if the resolution was not successful) or the - * resolved object directly when this function is called. If the resolution - * was not successful the first time, it may be called another time later - * in the context of the "performDeferredResolution" function. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - template - bool resolve(const std::vector &path, Logger &logger, - std::function()> imposterCallback, - std::function, Logger &)> resultCallback, - const SourceLocation &location = SourceLocation{}) - { - return resolve( - path, typeOf(), logger, - [imposterCallback]() -> Rooted { return imposterCallback(); }, - [resultCallback](Handle node, Logger &logger) { - resultCallback(node.cast(), logger); - }, - location); - } - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * The "resultCallback" is called when the resolution was successful, which - * may be at a later point in time. - * - * @tparam T is the type of the node that should be resolved. - * @param path is the path for which a node should be resolved. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called once the - * resolution was successful. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - template - bool resolve(const std::vector &path, Logger &logger, - std::function, Logger &)> resultCallback, - const SourceLocation &location = SourceLocation{}) - { - return resolve(path, typeOf(), logger, - [resultCallback](Handle node, Logger &logger) { - resultCallback(node.cast(), logger); - }, - location); - } - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * Calls the "imposterCallback" function for obtaining a temporary result if - * a node cannot be resolved right now. The "resultCallback" is at most - * called twice: Once when this method is called (probably with the - * temporary) and another time if the resolution turned out to because - * successful at a later point in time. - * - * @tparam T is the type of the node that should be resolved. - * @param name is the path for which a node should be resolved. The name is - * split at '.' to form a path. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param imposterCallback is the callback function that is called if - * the node cannot be resolved at this moment. It gives the caller the - * possibility to create an imposter (a temporary object) that may be used - * later in the resolution process. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called at least once - * either with the imposter (if the resolution was not successful) or the - * resolved object directly when this function is called. If the resolution - * was not successful the first time, it may be called another time later - * in the context of the "performDeferredResolution" function. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - template - bool resolve(const std::string &name, Logger &logger, - std::function()> imposterCallback, - std::function, Logger &)> resultCallback, - const SourceLocation &location = SourceLocation{}) - { - return resolve(Utils::split(name, '.'), logger, imposterCallback, - resultCallback, location); - } - - /** - * Tries to resolve a node for the given type and path for all nodes - * currently on the stack, starting with the topmost node on the stack. - * The "resultCallback" is called when the resolution was successful, which - * may be at a later point in time. - * - * @tparam T is the type of the node that should be resolved. - * @param name is the path for which a node should be resolved. The name is - * split at '.' to form a path. - * @param logger is the logger instance into which resolution problems - * should be logged. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called once the - * resolution was successful. - * @param location is the location in the current source file in which the - * resolution was triggered. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. - */ - template - bool resolve(const std::string &name, Logger &logger, - std::function, Logger &)> resultCallback, - const SourceLocation &location = SourceLocation{}) - { - return resolve(Utils::split(name, '.'), logger, resultCallback, - location); - } - - /** - * Tries to resolve all currently deferred resolution steps. The list of - * pending deferred resolutions is cleared after this function has run. - * - * @param logger is the logger instance into which errors should be logged. - */ - bool performDeferredResolution(Logger &logger); -}; -} -} - -#endif /* _OUSIA_PARSER_SCOPE_H_ */ - diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index 40486cc..8cb41ea 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -19,10 +19,9 @@ #include "CSSParser.hpp" #include +#include namespace ousia { -namespace parser { -namespace css { // CSS code tokens static const int CURLY_OPEN = 1; @@ -75,7 +74,7 @@ static const std::map CSS_DESCRIPTORS = { {ESCAPE, {CodeTokenMode::ESCAPE, ESCAPE}}, {LINEBREAK, {CodeTokenMode::LINEBREAK, LINEBREAK}}}; -Rooted CSSParser::parse(CharReader &reader, ParserContext &ctx) +Rooted CSSParser::doParse(CharReader &reader, ParserContext &ctx) { CodeTokenizer tokenizer{reader, CSS_ROOT, CSS_DESCRIPTORS}; tokenizer.ignoreComments = true; @@ -362,5 +361,4 @@ bool CSSParser::expect(int expectedType, CodeTokenizer &tokenizer, Token &t, return true; } } -} -} + diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp index 1ec54f5..c6594f6 100644 --- a/src/plugins/css/CSSParser.hpp +++ b/src/plugins/css/CSSParser.hpp @@ -24,6 +24,7 @@ * * @author Benjamin Paassen - bpaassen@techfak.uni-bielefeld.de */ + #ifndef _OUSIA_CSS_PARSER_HPP_ #define _OUSIA_CSS_PARSER_HPP_ @@ -36,8 +37,6 @@ #include namespace ousia { -namespace parser { -namespace css { /** * This is a context free, recursive parser for a subset of the CSS3 language @@ -139,7 +138,7 @@ private: bool expect(int expectedType, CodeTokenizer &tokenizer, Token &t, bool force, ParserContext &ctx); -public: +protected: /** * This parses the given input as CSS content as specified by the grammar * seen above. The return value is a Rooted reference to the root of the @@ -157,21 +156,8 @@ public: * @return returns the root node of the resulting SelectorTree. For more * information on the return conventions consult the Parser.hpp. */ - Rooted parse(CharReader &reader, ParserContext &ctx) override; - - using Parser::parse; - - /** - * As befits a class called CSSParser, this Parser parses CSS. - */ - std::set mimetypes() - { - std::set out{"text/css"}; - return out; - } + Rooted doParse(CharReader &reader, ParserContext &ctx) override; }; } -} -} #endif diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index 434a72c..ef738d8 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -25,13 +25,12 @@ #include #include #include +#include #include #include "XmlParser.hpp" namespace ousia { -namespace parser { -namespace xml { using namespace ousia::model; @@ -291,12 +290,7 @@ static void xmlCharacterDataHandler(void *p, const XML_Char *s, int len) /* Class XmlParser */ -std::set XmlParser::mimetypes() -{ - return std::set{{"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}}; -} - -Rooted XmlParser::parse(CharReader &reader, ParserContext &ctx) +Rooted XmlParser::doParse(CharReader &reader, ParserContext &ctx) { // Create the parser object ScopedExpatXmlParser p{"UTF-8"}; @@ -346,6 +340,4 @@ Rooted XmlParser::parse(CharReader &reader, ParserContext &ctx) return nullptr; } } -} -} diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp index 62f0128..3c0ffb7 100644 --- a/src/plugins/xml/XmlParser.hpp +++ b/src/plugins/xml/XmlParser.hpp @@ -31,23 +31,13 @@ #include namespace ousia { -namespace parser { -namespace xml { /** * 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; - +protected: /** * Parses the given input stream as XML file and returns the parsed * top-level node. @@ -56,13 +46,9 @@ public: * @param ctx is a reference to the ParserContext instance that should be * used. */ - Rooted parse(CharReader &reader, ParserContext &ctx) override; - - using Parser::parse; + Rooted doParse(CharReader &reader, ParserContext &ctx) override; }; -} -} } #endif /* _OUSIA_XML_PARSER_HPP_ */ diff --git a/test/core/RegistryTest.cpp b/test/core/RegistryTest.cpp index 45e09d3..21195f2 100644 --- a/test/core/RegistryTest.cpp +++ b/test/core/RegistryTest.cpp @@ -20,18 +20,83 @@ #include +#include +#include +#include #include #include namespace ousia { +namespace { +class TestParser : public Parser { +protected: + Rooted doParse(CharReader &reader, ParserContext &ctx) override + { + return new Node{ctx.manager}; + } +}; +} + +static const Rtti rtti1{"rtti1"}; +static const Rtti rtti2{"rtti2"}; + +TEST(Registry, parsers) +{ + Registry registry; + + TestParser parser1; + TestParser parser2; + + registry.registerParser({"text/vnd.ousia.oxm", "text/vnd.ousia.oxd"}, + {&rtti1, &rtti2}, &parser1); + registry.registerParser({"text/vnd.ousia.opd"}, {&rtti2}, &parser2); + + ASSERT_THROW( + registry.registerParser({"text/vnd.ousia.opd"}, {&rtti2}, &parser1), + OusiaException); + + { + auto res = registry.getParserForMimetype("text/vnd.ousia.oxm"); + ASSERT_EQ(&parser1, res.first); + ASSERT_EQ(RttiSet({&rtti1, &rtti2}), res.second); + } + + { + auto res = registry.getParserForMimetype("text/vnd.ousia.opd"); + ASSERT_EQ(&parser2, res.first); + ASSERT_EQ(RttiSet({&rtti2}), res.second); + } + + { + auto res = registry.getParserForMimetype("application/javascript"); + ASSERT_EQ(nullptr, res.first); + ASSERT_EQ(RttiSet({}), res.second); + } +} + +TEST(Registry, extensions) +{ + Registry registry; + + registry.registerExtension("oxm", "text/vnd.ousia.oxm"); + registry.registerExtension("oxd", "text/vnd.ousia.oxd"); + ASSERT_EQ("text/vnd.ousia.oxm", registry.getMimetypeForExtension("oxm")); + ASSERT_EQ("text/vnd.ousia.oxm", registry.getMimetypeForExtension("OXM")); + ASSERT_EQ("text/vnd.ousia.oxd", registry.getMimetypeForExtension("OxD")); + ASSERT_EQ("", registry.getMimetypeForExtension("pdf")); + + ASSERT_THROW(registry.registerExtension("oxm", "text/vnd.ousia.oxm"), + OusiaException); +} + TEST(Registry, locateResource) { StaticResourceLocator locator; locator.store("path", "test"); Registry registry; - registry.registerResourceLocator(locator); + registry.registerResourceLocator(&locator); Resource res; ASSERT_TRUE( diff --git a/test/core/common/UtilsTest.cpp b/test/core/common/UtilsTest.cpp index 53beb79..c8f6922 100644 --- a/test/core/common/UtilsTest.cpp +++ b/test/core/common/UtilsTest.cpp @@ -54,5 +54,24 @@ TEST(Utils, split) ASSERT_EQ(std::vector({"", "a", "be", "c", ""}), Utils::split(".a.be.c.", '.')); } + +TEST(Utils, toLower) +{ + ASSERT_EQ("", Utils::toLower("")); + ASSERT_EQ("foo00", Utils::toLower("foo00")); + ASSERT_EQ("foo00", Utils::toLower("fOO00")); +} + +TEST(Utils, extractFileExtension) +{ + ASSERT_EQ("", Utils::extractFileExtension("")); + ASSERT_EQ("", Utils::extractFileExtension("test")); + ASSERT_EQ("ext", Utils::extractFileExtension("test.ext")); + ASSERT_EQ("", Utils::extractFileExtension("foo.bar/test")); + ASSERT_EQ("", Utils::extractFileExtension("foo.bar\\test")); + ASSERT_EQ("ext", Utils::extractFileExtension("foo.bar/test.ext")); + ASSERT_EQ("ext", Utils::extractFileExtension("foo.bar/test.EXT")); +} + } diff --git a/test/core/parser/ParserStackTest.cpp b/test/core/parser/ParserStackTest.cpp index 69978b0..81160da 100644 --- a/test/core/parser/ParserStackTest.cpp +++ b/test/core/parser/ParserStackTest.cpp @@ -24,7 +24,6 @@ #include namespace ousia { -namespace parser { static const State STATE_DOCUMENT = 0; static const State STATE_BODY = 1; @@ -168,7 +167,5 @@ TEST(ParserStack, validation) ASSERT_FALSE(logger.hasError()); s.end(); } - -} } diff --git a/test/core/parser/StandaloneParserContext.hpp b/test/core/parser/StandaloneParserContext.hpp index 347d34f..51cd1e6 100644 --- a/test/core/parser/StandaloneParserContext.hpp +++ b/test/core/parser/StandaloneParserContext.hpp @@ -23,16 +23,18 @@ #include #include +#include +#include +#include namespace ousia { -namespace parser { struct StandaloneParserContext { public: Manager manager; Logger logger; - Scope scope; Registry registry; + ParserScope scope; Rooted project; ParserContext context; @@ -47,7 +49,6 @@ public: context(scope, registry, externalLogger, manager, project){}; }; } -} #endif /* _OUSIA_STANDALONE_PARSER_CONTEXT_ */ diff --git a/test/plugins/css/CSSParserTest.cpp b/test/plugins/css/CSSParserTest.cpp index 84522b3..774c345 100644 --- a/test/plugins/css/CSSParserTest.cpp +++ b/test/plugins/css/CSSParserTest.cpp @@ -26,8 +26,6 @@ #include namespace ousia { -namespace parser { -namespace css { TEST(CSSParser, testParseSelectors) { // create a string describing a SelectorTree @@ -296,5 +294,3 @@ TEST(CSSParser, testParseExceptions) assertException("A > "); } } -} -} diff --git a/test/plugins/xml/XmlParserTest.cpp b/test/plugins/xml/XmlParserTest.cpp index f1956e0..7785ae2 100644 --- a/test/plugins/xml/XmlParserTest.cpp +++ b/test/plugins/xml/XmlParserTest.cpp @@ -20,14 +20,13 @@ #include +#include #include #include #include namespace ousia { -namespace parser { -namespace xml { static TerminalLogger logger(std::cerr, true); @@ -94,6 +93,4 @@ TEST(XmlParser, namespaces) } } } -} -} -- cgit v1.2.3