diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-27 16:01:53 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-27 16:01:53 +0100 |
commit | eb6ecdcc85ece4eb84b90f3c9bb920dc1ad2b6d1 (patch) | |
tree | caaaaa969471552a13f5315a3de6e9db15b02a8b /src | |
parent | 07d326d02415467ba7f5f238a8e72a9e4b7f1549 (diff) |
Parsers do no longer return the node they have parsed (as this may be ill-defined -- if a parser only parses a partial document via include, there may be many to no nodes that are returned). Parsers should just use the ParserScope.push funciton. All nodes pushed onto the top-level of the ParserScope are added treated as the nodes the parser has parsed. Adapted all code and all tests accordingly.
Diffstat (limited to 'src')
-rw-r--r-- | src/core/parser/Parser.cpp | 8 | ||||
-rw-r--r-- | src/core/parser/Parser.hpp | 27 | ||||
-rw-r--r-- | src/core/parser/ParserContext.cpp | 4 | ||||
-rw-r--r-- | src/core/parser/ParserContext.hpp | 5 | ||||
-rw-r--r-- | src/core/resource/ResourceManager.cpp | 39 | ||||
-rw-r--r-- | src/core/resource/ResourceManager.hpp | 15 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.cpp | 11 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.hpp | 3 | ||||
-rw-r--r-- | src/plugins/xml/XmlParser.cpp | 3 | ||||
-rw-r--r-- | src/plugins/xml/XmlParser.hpp | 2 |
10 files changed, 58 insertions, 59 deletions
diff --git a/src/core/parser/Parser.cpp b/src/core/parser/Parser.cpp index 2978669..4e5016d 100644 --- a/src/core/parser/Parser.cpp +++ b/src/core/parser/Parser.cpp @@ -24,15 +24,15 @@ namespace ousia { /* Class Parser */ -Rooted<Node> Parser::parse(CharReader &reader, ParserContext &ctx) +void Parser::parse(CharReader &reader, ParserContext &ctx) { - return doParse(reader, ctx); + doParse(reader, ctx); } -Rooted<Node> Parser::parse(const std::string &str, ParserContext &ctx) +void Parser::parse(const std::string &str, ParserContext &ctx) { CharReader reader{str}; - return doParse(reader, ctx); + doParse(reader, ctx); } } diff --git a/src/core/parser/Parser.hpp b/src/core/parser/Parser.hpp index e4419f5..54c5bfc 100644 --- a/src/core/parser/Parser.hpp +++ b/src/core/parser/Parser.hpp @@ -32,9 +32,6 @@ #include <set> #include <string> -#include <core/managed/Managed.hpp> -#include <core/model/Node.hpp> - namespace ousia { // Forward declarations @@ -61,7 +58,7 @@ protected: * 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> doParse(CharReader &reader, ParserContext &ctx) = 0; + virtual void doParse(CharReader &reader, ParserContext &ctx) = 0; public: /** @@ -82,32 +79,30 @@ public: /** * Parses the given input stream and returns a corresponding node for * inclusion in the document graph. This method should be overridden by - * derived classes. + * derived classes. The created nodes should be placed onto the ParserScope + * using the "push" methods and removed using the "pop" methods. Nodes + * pushed to the top level of the ParserScope are considered as the subgraph + * the parser has created. * * @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 ParserException must be thrown by the parser. */ - Rooted<Node> parse(CharReader &reader, ParserContext &ctx); + void parse(CharReader &reader, ParserContext &ctx); /** * Parses the given string and returns a corresponding node for * inclusion in the document graph. This method should be overridden by - * derived classes. + * derived classes. The created nodes should be placed onto the ParserScope + * using the "push" methods and removed using the "pop" methods. Nodes + * pushed to the top level of the ParserScope are considered as the subgraph + * the parser has created. * * @param str is the string that should be parsed. * @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, ParserContext &ctx); + void parse(const std::string &str, ParserContext &ctx); }; } diff --git a/src/core/parser/ParserContext.cpp b/src/core/parser/ParserContext.cpp index 3a651f0..cc09b5e 100644 --- a/src/core/parser/ParserContext.cpp +++ b/src/core/parser/ParserContext.cpp @@ -39,7 +39,7 @@ ParserContext::ParserContext(Registry ®istry, { } -Rooted<Node> ParserContext::link(const std::string &path, +NodeVector<Node> ParserContext::link(const std::string &path, const std::string mimetype, const std::string rel, const RttiSet &supportedTypes) @@ -47,7 +47,7 @@ Rooted<Node> ParserContext::link(const std::string &path, return resourceManager.link(*this, path, mimetype, rel, supportedTypes); } -Rooted<Node> ParserContext::include(const std::string &path, +NodeVector<Node> ParserContext::include(const std::string &path, const std::string mimetype, const std::string rel, const RttiSet &supportedTypes) diff --git a/src/core/parser/ParserContext.hpp b/src/core/parser/ParserContext.hpp index 9b6eca0..f6ae89c 100644 --- a/src/core/parser/ParserContext.hpp +++ b/src/core/parser/ParserContext.hpp @@ -30,6 +30,7 @@ #include <core/common/Location.hpp> #include <core/common/Rtti.hpp> +#include <core/model/Node.hpp> #include <core/model/Project.hpp> namespace ousia { @@ -116,7 +117,7 @@ public: * checked, not the actual result. * @return the parsed node or nullptr if something goes wrong. */ - Rooted<Node> link(const std::string &path, const std::string mimetype, + NodeVector<Node> link(const std::string &path, const std::string mimetype, const std::string rel, const RttiSet &supportedTypes); /** @@ -137,7 +138,7 @@ public: * checked, not the actual result. * @return the parsed node or nullptr if something goes wrong. */ - Rooted<Node> include(const std::string &path, const std::string mimetype, + NodeVector<Node> include(const std::string &path, const std::string mimetype, const std::string rel, const RttiSet &supportedTypes); /** diff --git a/src/core/resource/ResourceManager.cpp b/src/core/resource/ResourceManager.cpp index 610176b..2e15c85 100644 --- a/src/core/resource/ResourceManager.cpp +++ b/src/core/resource/ResourceManager.cpp @@ -70,11 +70,9 @@ void ResourceManager::purgeResource(SourceId sourceId) contextReaders.erase(sourceId); } -Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path, - const std::string &mimetype, - const std::string &rel, - const RttiSet &supportedTypes, - ParseMode mode) +NodeVector<Node> ResourceManager::parse( + ParserContext &ctx, const std::string &path, const std::string &mimetype, + const std::string &rel, const RttiSet &supportedTypes, ParseMode mode) { // Some references used for convenience Registry ®istry = ctx.getRegistry(); @@ -88,7 +86,7 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path, Resource resource; if (!req.deduce(registry, logger) || !req.locate(registry, logger, resource, relativeTo)) { - return nullptr; + return NodeVector<Node>{}; } // Allocate a new SourceId handle for this Resource @@ -104,7 +102,7 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path, // Fetch the input stream and create a char reader std::unique_ptr<std::istream> is = resource.stream(); - CharReader reader(*is, sourceId); + CharReader reader(*is, sourceId); // Actually parse the input stream, distinguish the LINK and the // INCLUDE mode @@ -165,13 +163,12 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path, catch (LoggableException ex) { // Log the exception and return nullptr logger.log(ex); - return nullptr; - // return NodeVector<Node>{}; + return NodeVector<Node>{}; } // Make sure the parsed nodes fulfill the "supportedTypes" constraint, // remove nodes that do not the result - for (auto it = parsedNodes.begin(); it != parsedNodes.end(); ) { + for (auto it = parsedNodes.begin(); it != parsedNodes.end();) { const Rtti &type = (*it)->type(); if (!type.isOneOf(supportedTypes)) { logger.error(std::string("Node of internal type ") + type.name + @@ -183,23 +180,23 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, const std::string &path, } } - // TODO: Return parsed nodes - return nullptr; + return parsedNodes; } -Rooted<Node> ResourceManager::link(ParserContext &ctx, const std::string &path, - const std::string &mimetype, - const std::string &rel, - const RttiSet &supportedTypes) +NodeVector<Node> ResourceManager::link(ParserContext &ctx, + const std::string &path, + const std::string &mimetype, + const std::string &rel, + const RttiSet &supportedTypes) { return parse(ctx, path, mimetype, rel, supportedTypes, ParseMode::LINK); } -Rooted<Node> ResourceManager::include(ParserContext &ctx, - const std::string &path, - const std::string &mimetype, - const std::string &rel, - const RttiSet &supportedTypes) +NodeVector<Node> ResourceManager::include(ParserContext &ctx, + const std::string &path, + const std::string &mimetype, + const std::string &rel, + const RttiSet &supportedTypes) { return parse(ctx, path, mimetype, rel, supportedTypes, ParseMode::INCLUDE); } diff --git a/src/core/resource/ResourceManager.hpp b/src/core/resource/ResourceManager.hpp index e98e8f4..559112b 100644 --- a/src/core/resource/ResourceManager.hpp +++ b/src/core/resource/ResourceManager.hpp @@ -35,14 +35,13 @@ #include <core/common/Location.hpp> #include <core/common/Rtti.hpp> #include <core/common/SourceContextReader.hpp> -#include <core/managed/Managed.hpp> +#include <core/model/Node.hpp> #include "Resource.hpp" namespace ousia { // Forward declarations -class Node; class Parser; class ParserContext; class ResourceRequest; @@ -130,9 +129,9 @@ private: * can deal with. Note that only the types the parser claims to return are * checked, not the actual result. * @param mode describes whether the file should be included or linked. - * @return the parsed node or nullptr if something goes wrong. + * @return the parsed nodes or an empty list if something went wrong. */ - Rooted<Node> parse(ParserContext &ctx, const std::string &path, + NodeVector<Node> parse(ParserContext &ctx, const std::string &path, const std::string &mimetype, const std::string &rel, const RttiSet &supportedTypes, ParseMode mode); @@ -159,9 +158,9 @@ public: * @param supportedTypes contains the types of the returned Node the caller * can deal with. Note that only the types the parser claims to return are * checked, not the actual result. - * @return the parsed node or nullptr if something goes wrong. + * @return the parsed nodes or an empty list if something went wrong. */ - Rooted<Node> link(ParserContext &ctx, const std::string &path, + NodeVector<Node> link(ParserContext &ctx, const std::string &path, const std::string &mimetype, const std::string &rel, const RttiSet &supportedTypes); @@ -190,9 +189,9 @@ public: * @param supportedTypes contains the types of the returned Node the caller * can deal with. Note that only the types the parser claims to return are * checked, not the actual result. - * @return the parsed node or nullptr if something goes wrong. + * @return the parsed nodes or an empty list if something went wrong. */ - Rooted<Node> include(ParserContext &ctx, const std::string &path, + NodeVector<Node> include(ParserContext &ctx, const std::string &path, const std::string &mimetype, const std::string &rel, const RttiSet &supportedTypes); diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index 40179bf..a09a69f 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -74,15 +74,22 @@ static const std::map<int, CodeTokenDescriptor> CSS_DESCRIPTORS = { {ESCAPE, {CodeTokenMode::ESCAPE, ESCAPE}}, {LINEBREAK, {CodeTokenMode::LINEBREAK, LINEBREAK}}}; -Rooted<Node> CSSParser::doParse(CharReader &reader, ParserContext &ctx) +void CSSParser::doParse(CharReader &reader, ParserContext &ctx) { CodeTokenizer tokenizer{reader, CSS_ROOT, CSS_DESCRIPTORS}; tokenizer.ignoreComments = true; tokenizer.ignoreLinebreaks = true; + + // Create the root node and push it onto the parser scope Rooted<model::SelectorNode> root = { new model::SelectorNode{ctx.getManager(), "root"}}; + ctx.getScope().push(root); + + // Parse the document into the root node parseDocument(root, tokenizer, ctx); - return root; + + // Remove the element from the parser scope + ctx.getScope().pop(); } void CSSParser::parseDocument(Rooted<model::SelectorNode> root, diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp index 2f37e6a..b2a760f 100644 --- a/src/plugins/css/CSSParser.hpp +++ b/src/plugins/css/CSSParser.hpp @@ -35,6 +35,7 @@ #include <core/common/CharReader.hpp> #include <core/model/Style.hpp> #include <core/parser/Parser.hpp> +#include <core/parser/ParserScope.hpp> namespace ousia { @@ -158,7 +159,7 @@ protected: * @return returns the root node of the resulting SelectorTree. For more * information on the return conventions consult the Parser.hpp. */ - Rooted<Node> doParse(CharReader &reader, ParserContext &ctx) override; + void doParse(CharReader &reader, ParserContext &ctx) override; }; } diff --git a/src/plugins/xml/XmlParser.cpp b/src/plugins/xml/XmlParser.cpp index bb9d678..51c52bc 100644 --- a/src/plugins/xml/XmlParser.cpp +++ b/src/plugins/xml/XmlParser.cpp @@ -289,7 +289,7 @@ static void xmlCharacterDataHandler(void *p, const XML_Char *s, int len) /* Class XmlParser */ -Rooted<Node> XmlParser::doParse(CharReader &reader, ParserContext &ctx) +void XmlParser::doParse(CharReader &reader, ParserContext &ctx) { // Create the parser object ScopedExpatXmlParser p{"UTF-8"}; @@ -335,7 +335,6 @@ Rooted<Node> XmlParser::doParse(CharReader &reader, ParserContext &ctx) break; } } - return nullptr; } } diff --git a/src/plugins/xml/XmlParser.hpp b/src/plugins/xml/XmlParser.hpp index 3c0ffb7..c8b6302 100644 --- a/src/plugins/xml/XmlParser.hpp +++ b/src/plugins/xml/XmlParser.hpp @@ -46,7 +46,7 @@ protected: * @param ctx is a reference to the ParserContext instance that should be * used. */ - Rooted<Node> doParse(CharReader &reader, ParserContext &ctx) override; + void doParse(CharReader &reader, ParserContext &ctx) override; }; } |