summaryrefslogtreecommitdiff
path: root/src/core/parser
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-11-30 23:41:49 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-11-30 23:41:49 +0100
commit235b98e0d1a2e9e60c440076b5a11c8bf64ba071 (patch)
tree9aed0e0dfaef57da4bc4ac4df49a4eb0793c4994 /src/core/parser
parent14b3a5c80ae0336de0d34d0d05fad6efc994462c (diff)
backup
Diffstat (limited to 'src/core/parser')
-rw-r--r--src/core/parser/Parser.cpp9
-rw-r--r--src/core/parser/Parser.hpp72
-rw-r--r--src/core/parser/Scope.cpp26
-rw-r--r--src/core/parser/Scope.hpp170
4 files changed, 252 insertions, 25 deletions
diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp
index bc98ac0..23fd9b7 100644
--- a/src/core/parser/Parser.cpp
+++ b/src/core/parser/Parser.cpp
@@ -21,12 +21,13 @@
#include "Parser.hpp"
namespace ousia {
+namespace parser {
-Rooted<Node> Parser::parse(const std::string &str, Handle<Node> context, Logger &logger)
+Rooted<Node> Parser::parse(const std::string &str, ParserContext &ctx)
{
- std::istringstream is(str);
- return parse(is, context, logger);
+ std::istringstream is{str};
+ return parse(is, ctx);
+}
}
-
}
diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp
index 74a1988..b8faf98 100644
--- a/src/core/parser/Parser.hpp
+++ b/src/core/parser/Parser.hpp
@@ -19,7 +19,7 @@
/**
* @file Parser.hpp
*
- * Contains the abstract "Parser" class. Parsers are objects capable of reading
+ * Contains the abstract Parser class. Parsers are objects capable of reading
* a certain file format and transforming it into a node.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
@@ -32,11 +32,15 @@
#include <set>
#include <string>
-#include "Exceptions.hpp"
-#include "Node.hpp"
-#include "Logger.hpp"
+#include <core/Exceptions.hpp>
+#include <core/Node.hpp>
+#include <core/Logger.hpp>
+#include <core/Registry.hpp>
+
+#include "Scope.hpp"
namespace ousia {
+namespace parser {
// TODO: Implement a proper Mimetype class
@@ -49,15 +53,48 @@ public:
};
/**
+ * Struct containing the objects that are passed to a parser instance.
+ */
+struct ParserContext {
+ /**
+ * Reference to the Scope instance that should be used within the parser.
+ */
+ Scope &scope;
+
+ /**
+ * Reference to the Registry instance that should be used within the parser.
+ */
+ Registry &registry;
+
+ /**
+ * Reference to the Logger the parser should log any messages to.
+ */
+ Logger &logger;
+
+ /**
+ * Constructor of the ParserContext class.
+ *
+ * @param scope is a reference to the Scope instance that should be used to
+ * lookup names.
+ * @param registry is a reference at the Registry class, which allows to
+ * obtain references at parsers for other formats or script engine
+ * implementations.
+ * @param logger is a reference to the Logger instance that should be used
+ * to log error messages and warnings that occur while parsing the document.
+ */
+ ParserContext(Scope &scope, Registry &registry, Logger &logger)
+ : scope(scope), registry(registry), logger(logger){};
+};
+
+/**
* Abstract parser class. This class builds the basic interface that should be
* used by any parser which reads data from an input stream and transforms it
* into an Ousía node graph.
*/
class Parser {
public:
-
- Parser() {};
- Parser(const Parser&) = delete;
+ Parser(){};
+ Parser(const Parser &) = delete;
/**
* Returns a set containing all mime types supported by the parser. The mime
@@ -78,18 +115,14 @@ public:
* derived classes.
*
* @param is is a reference to the input stream that should be parsed.
- * @param context defines the context in which the input stream should be
- * parsed. The context represents the scope from which element names should
- * be looked up.
- * @param logger is a reference to the Logger instance that should be used
- * to log error messages and warnings that occur while parsing the document.
+ * @param ctx is a reference to the context that should be used while
+ * parsing the document.
* @return a reference to the node representing the subgraph that has been
* created. The resulting node may point at not yet resolved entities, the
* calling code will try to resolve these. If no valid node can be produced,
* a corresponding LoggableException must be thrown by the parser.
*/
- virtual Rooted<Node> parse(std::istream &is, Handle<Node> context,
- Logger &logger) = 0;
+ virtual Rooted<Node> parse(std::istream &is, ParserContext &ctx) = 0;
/**
* Parses the given string and returns a corresponding node for
@@ -97,20 +130,17 @@ public:
* derived classes.
*
* @param str is the string that should be parsed.
- * @param context defines the context in which the input stream should be
- * parsed. The context represents the scope from which element names should
- * be looked up.
- * @param logger is a reference to the Logger instance that should be used
- * to log error messages and warnings that occur while parsing the document.
+ * @param ctx is a reference to the context that should be used while
+ * parsing the document.
* @return a reference to the node representing the subgraph that has been
* created. The resulting node may point at not yet resolved entities, the
* calling code will try to resolve these. If no valid node can be produced,
* a corresponding ParserException must be thrown by the parser.
*/
- Rooted<Node> parse(const std::string &str, Handle<Node> context,
- Logger &logger);
+ Rooted<Node> parse(const std::string &str, ParserContext &ctx);
};
}
+}
#endif /* _OUSIA_PARSER_HPP_ */
diff --git a/src/core/parser/Scope.cpp b/src/core/parser/Scope.cpp
new file mode 100644
index 0000000..a60ade0
--- /dev/null
+++ b/src/core/parser/Scope.cpp
@@ -0,0 +1,26 @@
+/*
+ Ousía
+ Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "Scope.hpp"
+
+namespace ousia {
+namespace parser {
+
+
+}
+}
diff --git a/src/core/parser/Scope.hpp b/src/core/parser/Scope.hpp
new file mode 100644
index 0000000..0c37fbd
--- /dev/null
+++ b/src/core/parser/Scope.hpp
@@ -0,0 +1,170 @@
+/*
+ Ousía
+ Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _OUSIA_PARSER_SCOPE_H_
+#define _OUSIA_PARSER_SCOPE_H_
+
+#include <core/Node.hpp>
+
+/**
+ * @file Scope.hpp
+ *
+ * Contains the Scope class used for resolving references based on the current
+ * parser state.
+ *
+ * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
+ */
+
+namespace ousia {
+namespace parser {
+
+class Scope;
+
+/**
+ * The ScopedScope class takes care of pushing a Node instance into the
+ * name resolution stack of a Scope instance and poping this node once the
+ * ScopedScope instance is deletes. This way you cannot forget to pop a Node
+ * from a Scope instance as this operation is performed automatically.
+ */
+class ScopedScope {
+private:
+ /**
+ * Reference at the backing scope instance.
+ */
+ Scope *scope;
+
+public:
+ /**
+ * Creates a new ScopedScope instance.
+ *
+ * @param scope is the backing Scope instance.
+ * @param node is the Node instance that should be poped onto the stack of
+ * the Scope instance.
+ */
+ ScopedScope(Scope *scope, Handle<Node> node);
+
+ /**
+ * Pops the Node given in the constructor form the stack of the Scope
+ * instance.
+ */
+ ~ScopedScope();
+
+ /**
+ * Copying a ScopedScope is invalid.
+ */
+ ScopedScope(const ScopedScope &) = delete;
+
+ /**
+ * Move constructor of the ScopedScope class.
+ */
+ ScopedScope(ScopedScope &&);
+
+ /**
+ * Provides access at the underlying Scope instance.
+ */
+ Scope *operator->() { return scope; }
+
+ /**
+ * Provides access at the underlying Scope instance.
+ */
+ Scope &operator*() { return *scope; }
+};
+
+/**
+ * Provides an interface for document parsers to resolve references based on the
+ * current position in the created document tree. The Scope class itself is
+ * represented as a chain of Scope objects where each element has a reference to
+ * a Node object attached to it. The descend method can be used to add a new
+ * scope element to the chain.
+ */
+class Scope {
+private:
+ std::deque<Rooted<Node>> nodes;
+
+public:
+ /**
+ * Constructor of the Scope class.
+ *
+ * @param rootNode is the top-most Node from which elements can be looked
+ * up.
+ */
+ Scope(Handle<Node> rootNode) { nodes.push_back(rootNode); }
+
+ /**
+ * Returns a reference at the Manager instance all nodes belong to.
+ */
+ Manager &getManager() { return getRoot()->getManager(); }
+
+ /**
+ * Pushes a new node onto the scope.
+ *
+ * @param node is the node that should be used for local lookup.
+ */
+ void push(Handle<Node> node) { nodes.push_back(node); }
+
+ /**
+ * Removes the last pushed node from the scope.
+ */
+ void pop() { nodes.pop_back(); }
+
+ /**
+ * Returns a ScopedScope instance, which automatically pushes the given node
+ * into the Scope stack and pops it once the ScopedScope is destroyed.
+ */
+ ScopedScope descend(Handle<Node> node) { return ScopedScope{this, node}; }
+
+ /**
+ * Returns the top-most Node instance in the Scope hirarchy.
+ *
+ * @return a reference at the root node.
+ */
+ Rooted<Node> getRoot() { return nodes.front(); }
+
+ /**
+ * Returns the bottom-most Node instance in the Scope hirarchy, e.g. the
+ * node that was pushed last onto the stack.
+ *
+ * @return a reference at the leaf node.
+ */
+ Rooted<Node> getLeaf() { return nodes.back(); }
+};
+
+/* Class ScopedScope -- inline declaration of some methods */
+
+inline ScopedScope::ScopedScope(Scope *scope, Handle<Node> node) : scope(scope)
+{
+ scope->push(node);
+}
+
+inline ScopedScope::~ScopedScope()
+{
+ if (scope) {
+ scope->pop();
+ }
+}
+
+inline ScopedScope::ScopedScope(ScopedScope &&s)
+{
+ scope = s.scope;
+ s.scope = nullptr;
+}
+}
+}
+
+#endif /* _OUSIA_PARSER_SCOPE_H_ */
+