diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-21 01:17:49 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-21 01:17:49 +0100 |
commit | 6decad0b8e7e369bd8215f31a45dd3eae982d2a9 (patch) | |
tree | 96d22db47629956c554d11a9e56bc68a2fc9b40b /src/core/parser | |
parent | 311a770805dff2cdffc1ecbfbbf0c5aae44c8878 (diff) |
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
Diffstat (limited to 'src/core/parser')
-rw-r--r-- | src/core/parser/Parser.cpp | 13 | ||||
-rw-r--r-- | src/core/parser/Parser.hpp | 101 | ||||
-rw-r--r-- | src/core/parser/ParserContext.cpp | 36 | ||||
-rw-r--r-- | src/core/parser/ParserContext.hpp | 92 | ||||
-rw-r--r-- | src/core/parser/ParserScope.cpp (renamed from src/core/parser/Scope.cpp) | 58 | ||||
-rw-r--r-- | src/core/parser/ParserScope.hpp (renamed from src/core/parser/Scope.hpp) | 102 | ||||
-rw-r--r-- | src/core/parser/ParserStack.cpp | 2 | ||||
-rw-r--r-- | src/core/parser/ParserStack.hpp | 5 |
8 files changed, 209 insertions, 200 deletions
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 <http://www.gnu.org/licenses/>. */ +#include <core/common/CharReader.hpp> + #include "Parser.hpp" namespace ousia { -namespace parser { + +/* Class Parser */ + +Rooted<Node> Parser::parse(CharReader &reader, ParserContext &ctx) +{ + return doParse(reader, ctx); +} Rooted<Node> 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 <set> #include <string> -#include <core/Registry.hpp> -#include <core/common/CharReader.hpp> -#include <core/common/Exceptions.hpp> -#include <core/common/Logger.hpp> +#include <core/managed/Managed.hpp> #include <core/model/Node.hpp> -#include <core/model/Project.hpp> - -#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<Node> doParse(CharReader &reader, ParserContext &ctx) = 0; +public: /** - * Project instance into which the new content should be parsed. + * Default constructor. */ - Rooted<model::Project> 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<model::Project> 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<std::string> mimetypes() - { - return std::set<std::string>{}; - }; + 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<Node> parse(CharReader &reader, ParserContext &ctx) = 0; + Rooted<Node> parse(CharReader &reader, ParserContext &ctx); /** * Parses the given string and returns a corresponding node for @@ -152,7 +110,6 @@ public: Rooted<Node> 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 <http://www.gnu.org/licenses/>. +*/ + +#include "ParserContext.hpp" + +namespace ousia { + +/* Class ParserContext */ + +ParserContext::ParserContext(ParserScope &scope, Registry ®istry, + Logger &logger, Manager &manager, + Handle<model::Project> 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 <http://www.gnu.org/licenses/>. +*/ + +/** + * @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 <core/managed/Managed.hpp> +#include <core/model/Node.hpp> +#include <core/model/Project.hpp> + +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<model::Project> 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<model::Project> project); +}; + +} + +#endif /* _OUSIA_PARSER_CONTEXT_HPP_ */ + diff --git a/src/core/parser/Scope.cpp b/src/core/parser/ParserScope.cpp index 01292df..b236a1f 100644 --- a/src/core/parser/Scope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -18,35 +18,14 @@ #include <core/common/Utils.hpp> -#include "Scope.hpp" +#include "ParserScope.hpp" namespace ousia { -namespace parser { -/* Class GuardedScope */ +/* Class ParserScopeBase */ -GuardedScope::GuardedScope(Scope *scope, Handle<Node> 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<Node> ScopeBase::resolve(const std::vector<std::string> &path, - const Rtti &type, Logger &logger) +Rooted<Node> ParserScopeBase::resolve(const std::vector<std::string> &path, + const Rtti &type, Logger &logger) { // Go up the stack and try to resolve the for (auto it = nodes.rbegin(); it != nodes.rend(); it++) { @@ -101,22 +80,17 @@ bool DeferredResolution::resolve(Logger &logger) return false; } -/* Class Scope */ - -void Scope::push(Handle<Node> node) { nodes.push_back(node); } +/* Class ParserScope */ -void Scope::pop() { nodes.pop_back(); } +void ParserScope::push(Handle<Node> node) { nodes.push_back(node); } -GuardedScope Scope::descend(Handle<Node> node) -{ - return GuardedScope{this, node}; -} +void ParserScope::pop() { nodes.pop_back(); } -Rooted<Node> Scope::getRoot() const { return nodes.front(); } +Rooted<Node> ParserScope::getRoot() const { return nodes.front(); } -Rooted<Node> Scope::getLeaf() { return nodes.back(); } +Rooted<Node> ParserScope::getLeaf() { return nodes.back(); } -bool Scope::resolve(const std::vector<std::string> &path, const Rtti &type, +bool ParserScope::resolve(const std::vector<std::string> &path, const Rtti &type, Logger &logger, ResolutionImposterCallback imposterCallback, ResolutionResultCallback resultCallback, const SourceLocation &location) @@ -128,11 +102,11 @@ bool Scope::resolve(const std::vector<std::string> &path, const Rtti &type, return true; } -bool Scope::resolve(const std::vector<std::string> &path, const Rtti &type, +bool ParserScope::resolve(const std::vector<std::string> &path, const Rtti &type, Logger &logger, ResolutionResultCallback resultCallback, const SourceLocation &location) { - Rooted<Node> res = ScopeBase::resolve(path, type, logger); + Rooted<Node> res = ParserScopeBase::resolve(path, type, logger); if (res != nullptr) { try { resultCallback(res, logger); @@ -146,7 +120,7 @@ bool Scope::resolve(const std::vector<std::string> &path, const Rtti &type, return false; } -bool Scope::performDeferredResolution(Logger &logger) +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). @@ -176,8 +150,8 @@ bool Scope::performDeferredResolution(Logger &logger) // 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, ".") + + logger.error(std::string("Could not resolve ") + failed.type.name + + std::string(" \"") + Utils::join(failed.path, ".") + std::string("\""), failed.location); } @@ -185,4 +159,4 @@ bool Scope::performDeferredResolution(Logger &logger) return false; } } -} + diff --git a/src/core/parser/Scope.hpp b/src/core/parser/ParserScope.hpp index b9b7f80..a759738 100644 --- a/src/core/parser/Scope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -16,8 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef _OUSIA_PARSER_SCOPE_H_ -#define _OUSIA_PARSER_SCOPE_H_ +#ifndef _OUSIA_PARSER_SCOPE_HPP_ +#define _OUSIA_PARSER_SCOPE_HPP_ #include <functional> #include <list> @@ -29,19 +29,21 @@ #include <core/model/Node.hpp> /** - * @file Scope.hpp + * @file ParserScope.hpp * - * Contains the Scope class used for resolving references based on the current + * 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 { -namespace parser { // Forward declaration -class Scope; +class CharReader; +class Registry; +class Logger; +class ParserScope; /** * Callback function type used for creating a dummy object while no correct @@ -57,57 +59,9 @@ using ResolutionResultCallback = std::function<void(Handle<Node>, 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> 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 { +class ParserScopeBase { protected: /** * List containing all nodes currently on the scope, with the newest nodes @@ -117,18 +71,18 @@ protected: public: /** - * Default constructor, creates an empty Scope instance. + * Default constructor, creates an empty ParserScope instance. */ - ScopeBase() {} + ParserScopeBase() {} /** - * Creates a new instance of the ScopeBase class, copying the the given + * 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. */ - ScopeBase(const NodeVector<Node> &nodes) : nodes(nodes) {} + ParserScopeBase(const NodeVector<Node> &nodes) : nodes(nodes) {} /** * Tries to resolve a node for the given type and path for all nodes that @@ -157,7 +111,7 @@ private: /** * Copy of the scope at the time when the resolution was first triggered. */ - ScopeBase scope; + ParserScopeBase scope; /** * Callback function to be called when an element is successfully resolved. @@ -185,7 +139,7 @@ public: * arguments. * * @param nodes is a reference at the current internal node stack of the - * Scope class. + * 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. @@ -213,12 +167,11 @@ public: /** * 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. + * 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 Scope : public ScopeBase { +class ParserScope : public ParserScopeBase { private: /** * List containing all deferred resolution descriptors. @@ -227,10 +180,10 @@ private: public: /** - * Default constructor of the Scope class, creates an empty Scope with no + * Default constructor of the ParserScope class, creates an empty ParserScope with no * element on the internal stack. */ - Scope() {} + ParserScope() {} /** * Pushes a new node onto the scope. @@ -245,20 +198,14 @@ public: 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> node); - - /** - * Returns the top-most Node instance in the Scope hirarchy. + * Returns the top-most Node instance in the ParserScope hirarchy. * * @return a reference at the root node. */ Rooted<Node> getRoot() const; /** - * Returns the bottom-most Node instance in the Scope hirarchy, e.g. the + * 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. @@ -475,7 +422,6 @@ public: bool performDeferredResolution(Logger &logger); }; } -} -#endif /* _OUSIA_PARSER_SCOPE_H_ */ +#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 <core/common/Exceptions.hpp> 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 <core/common/Argument.hpp> #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_ */ |