diff options
-rw-r--r-- | src/core/parser/ParserScope.cpp | 70 | ||||
-rw-r--r-- | src/core/parser/ParserScope.hpp | 103 |
2 files changed, 105 insertions, 68 deletions
diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index efd07d1..42225ca 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -61,6 +61,38 @@ Rooted<Node> ParserScopeBase::resolve(const Rtti &type, return nullptr; } +bool ParserScopeBase::isEmpty() const { return nodes.empty(); } + +Rooted<Node> ParserScopeBase::getRoot() const { return nodes.front(); } + +Rooted<Node> ParserScopeBase::getLeaf() const { return nodes.back(); } + +const NodeVector<Node> &ParserScopeBase::getStack() const { return nodes; } + +std::vector<Rtti const *> ParserScopeBase::getStackTypeSignature() const +{ + std::vector<Rtti const *> res; + res.reserve(nodes.size()); + for (size_t i = 0; i < nodes.size(); i++) { + res.push_back(&(nodes[i]->type())); + } + return res; +} + +Rooted<Node> ParserScopeBase::select(RttiSet types, int maxDepth) +{ + ssize_t minDepth = 0; + if (maxDepth >= 0) { + minDepth = static_cast<ssize_t>(nodes.size()) - (maxDepth + 1); + } + for (ssize_t i = nodes.size() - 1; i >= minDepth; i--) { + if (nodes[i]->type().isOneOf(types)) { + return nodes[i]; + } + } + return nullptr; +} + /* Class DeferredResolution */ DeferredResolution::DeferredResolution(const NodeVector<Node> &nodes, @@ -190,24 +222,6 @@ void ParserScope::pop() NodeVector<Node> ParserScope::getTopLevelNodes() const { return topLevelNodes; } -Rooted<Node> ParserScope::getRoot() const { return nodes.front(); } - -Rooted<Node> ParserScope::getLeaf() const { return nodes.back(); } - -Rooted<Node> ParserScope::select(RttiSet types, int maxDepth) -{ - ssize_t minDepth = 0; - if (maxDepth >= 0) { - minDepth = static_cast<ssize_t>(nodes.size()) - (maxDepth + 1); - } - for (ssize_t i = nodes.size() - 1; i >= minDepth; i--) { - if (nodes[i]->type().isOneOf(types)) { - return nodes[i]; - } - } - return nullptr; -} - void ParserScope::setFlag(ParserFlag flag, bool value) { // Fetch the current stack depth @@ -368,17 +382,17 @@ bool ParserScope::resolveTypeWithValue(const std::vector<std::string> &path, ParserScope scope = fork(); Variant *valuePtr = &value; - return resolveType(path, owner, logger, - [=](Handle<Node> resolved, Handle<Node> owner, - Logger &logger) mutable { - if (resolved != nullptr) { - Rooted<Type> type = resolved.cast<Type>(); - scope.resolveValue(*valuePtr, type, owner, logger); - } + return resolveType( + path, owner, logger, + [=](Handle<Node> resolved, Handle<Node> owner, Logger &logger) mutable { + if (resolved != nullptr) { + Rooted<Type> type = resolved.cast<Type>(); + scope.resolveValue(*valuePtr, type, owner, logger); + } - // Call the result callback with the type - resultCallback(resolved, owner, logger); - }); + // Call the result callback with the type + resultCallback(resolved, owner, logger); + }); } bool ParserScope::resolveTypeWithValue(const std::string &name, diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp index 2ef33d9..30db92f 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -104,6 +104,69 @@ public: */ Rooted<Node> resolve(const Rtti &type, const std::vector<std::string> &path, Logger &logger); + + /** + * Returns true if the stack is empty. + * + * @return true if there is no element on the stack. + */ + bool isEmpty() const; + + /** + * Returns a reference at the internal node stack. + * + * @return a const reference at the internal node stack. + */ + const NodeVector<Node> &getStack() const; + + /** + * Returns a list containing the Rtti type of each Node that is currently + * in the stack. + * + * @return the type signature of the current node stack. + */ + std::vector<Rtti const *> getStackTypeSignature() const; + + /** + * Returns the top-most Node instance in the ParserScopeBase hirarchy. + * + * @return a reference at the root node. + */ + Rooted<Node> getRoot() const; + + /** + * Returns the bottom-most Node instance in the ParserScopeBase hirarchy, + * e.g. the node that was pushed last onto the stack. + * + * @return a reference at the leaf node. + */ + Rooted<Node> getLeaf() const; + + /** + * Ascends in the stack starting with the leaf node, returns the first node + * that matches the type given in the RttiSet or nullptr if none matches. + * + * @param types is a set of Rtti types for which should be searched in the + * stack. + * @param maxDepth is the maximum number of stack entries the selection + * function may ascend. A negative value indicates no limitation. + */ + Rooted<Node> select(RttiSet types, int maxDepth = -1); + + /** + * Ascends in the stack starting with the leaf node, returns the first node + * that matches the given type or nullptr if none matches. + * + * @tparam T is the type that should be searched in the stack. + * @param maxDepth is the maximum number of stack entries the selection + * function may ascend. A negative value indicates no limitation. + */ + template <class T> + Rooted<T> select(int maxDepth = -1) + { + return select(RttiSet{&typeOf<T>()}, maxDepth).cast<T>(); + } + }; /** @@ -344,46 +407,6 @@ public: NodeVector<Node> getTopLevelNodes() const; /** - * 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 ParserScope hirarchy, e.g. - * the node that was pushed last onto the stack. - * - * @return a reference at the leaf node. - */ - Rooted<Node> getLeaf() const; - - /** - * Ascends in the stack starting with the leaf node, returns the first node - * that matches the type given in the RttiSet or nullptr if none matches. - * - * @param types is a set of Rtti types for which should be searched in the - * stack. - * @param maxDepth is the maximum number of stack entries the selection - * function may ascend. A negative value indicates no limitation. - */ - Rooted<Node> select(RttiSet types, int maxDepth = -1); - - /** - * Ascends in the stack starting with the leaf node, returns the first node - * that matches the given type or nullptr if none matches. - * - * @tparam T is the type that should be searched in the stack. - * @param maxDepth is the maximum number of stack entries the selection - * function may ascend. A negative value indicates no limitation. - */ - template <class T> - Rooted<T> select(int maxDepth = -1) - { - return select(RttiSet{&typeOf<T>()}, maxDepth).cast<T>(); - } - - /** * Sets a parser flag for the current stack depth. * * @param flag is the flag that should be set. |