diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-10 02:41:23 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-10 02:41:23 +0100 |
commit | a8f78252f05327d37aec3b853109b6d1359af452 (patch) | |
tree | 1eb136057bc59c6cf19ac52d2830c5a186149ce8 | |
parent | d84efdc312a9c5d0b6757c826810809a8e78f1e2 (diff) | |
parent | 325d78169620094178513303d65c71d933182ae4 (diff) |
Merge branch 'master' of somweyr.de:ousia
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/core/CSS.hpp | 12 | ||||
-rw-r--r-- | src/core/ResourceLocator.hpp | 120 | ||||
-rw-r--r-- | src/core/variant/Variant.hpp | 73 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.cpp | 29 | ||||
-rw-r--r-- | src/plugins/css/CSSParser.hpp | 2 | ||||
-rw-r--r-- | test/core/ResourceLocatorTest.cpp | 69 | ||||
-rw-r--r-- | test/core/variant/VariantTest.cpp | 15 | ||||
-rw-r--r-- | test/plugins/css/CSSParserTest.cpp | 2 |
9 files changed, 300 insertions, 24 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index efcac0b..804276d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ ADD_LIBRARY(ousia_core src/core/Logger src/core/Managed src/core/Node + src/core/ResourceLocator src/core/Tokenizer # src/core/Typesystem src/core/Utils @@ -161,6 +162,7 @@ IF(TEST) test/core/ManagedContainersTest test/core/NodeTest test/core/RangeSetTest + test/core/ResourceLocatorTest test/core/TokenizerTest test/core/UtilsTest test/core/parser/ParserStackTest diff --git a/src/core/CSS.hpp b/src/core/CSS.hpp index aa701b5..1510f3a 100644 --- a/src/core/CSS.hpp +++ b/src/core/CSS.hpp @@ -70,7 +70,7 @@ struct Specificity { */ class RuleSet : public Managed { private: - std::map<std::string, variant::Variant> rules; + std::map<std::string, Variant> rules; public: /** @@ -78,9 +78,9 @@ public: */ RuleSet(Manager &mgr) : Managed(mgr), rules() {} - std::map<std::string, variant::Variant> &getRules() { return rules; } + std::map<std::string, Variant> &getRules() { return rules; } - const std::map<std::string, variant::Variant> &getRules() const + const std::map<std::string, Variant> &getRules() const { return rules; } @@ -127,11 +127,11 @@ public: class PseudoSelector { private: const std::string name; - const std::vector<std::string> args; + const Variant::arrayType args; const bool generative; public: - PseudoSelector(std::string name, std::vector<std::string> args, + PseudoSelector(std::string name, Variant::arrayType args, bool generative) : name(std::move(name)), args(std::move(args)), generative(generative) { @@ -144,7 +144,7 @@ public: const std::string &getName() const { return name; } - const std::vector<std::string> &getArgs() const { return args; } + const Variant::arrayType &getArgs() const { return args; } const bool &isGenerative() const { return generative; } }; diff --git a/src/core/ResourceLocator.hpp b/src/core/ResourceLocator.hpp new file mode 100644 index 0000000..aaea8a5 --- /dev/null +++ b/src/core/ResourceLocator.hpp @@ -0,0 +1,120 @@ +/* + 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_RESOURCE_LOCATOR_HPP_ +#define _OUSIA_RESOURCE_LOCATOR_HPP_ + +#include <istream> +#include <memory> + +namespace ousia { + +/** + * A ResourceLocator is a class able to locate resources in some way, usually + * on the hard drive. + * + * We specify this as an abstract superclass to have an interface layer between + * the program core and possible future extensions in terms of resource + * locations (e.g. online resources, .zip files, etc.). + */ +class ResourceLocator { +public: + /** + * This enum contains all possible types of includable resources in Ousía. + */ + enum class Type { + // A Domain description + DOMAIN, + // An ECMA/JavaScript + SCRIPT, + // A Type System + TYPESYSTEM, + // TODO: Aren't documents and attribute descriptors missing? + // TODO: What is the purpose of these two? + GENERIC_MODULE, + GENERIC_INCLUDE + }; + + /** + * A Location contains the location of a Resource, e.g. a file path + * on a hard drive. Note that the 'found' flag might be set to false + * indicating that a resource was not found. + */ + struct Location { + const bool found; + const ResourceLocator &locator; + const Type type; + const std::string location; + + Location(const bool found, const ResourceLocator &locator, + const Type type, const std::string location) + : found(found), locator(locator), type(type), location(location) + { + } + + /** + * This calls the 'stream' method of the underlying ResourceLocator that + * found this location and returns a stream containing the data of the + * Resource at this location. + * + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. + */ + std::unique_ptr<std::istream> stream() const + { + return std::move(locator.stream(location)); + } + }; + + /** + * The locate function uses this ResourceLocator to search for a given + * Resource name (path parameter). It returns a Location with the + * 'found' flag set accordingly. + * + * @param path is the resource name. + * @param relativeTo TODO: What is the meaning of this parameter? + * @param type is the type of this resource. + * + * @return A Location containing either the found location of the + * Resource and the found flag set to 'true' or an empty location + * and the found flag set to 'false'. + */ + virtual Location locate(const std::string &path, + const std::string &relativeTo, + const Type type) const = 0; + + /** + * This method returns a strem containing the data of the resource at the + * given location. + * + * @param location is a found location, most likely from a Location. + * + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. + */ + virtual std::unique_ptr<std::istream> stream( + const std::string &location) const = 0; +}; +} + +#endif /* _RESOURCE_LOCATOR_HPP_ */ + diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index 6476780..1e62644 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -65,8 +65,7 @@ public: /** * Exception thrown whenever a variant is accessed via a getter function - * that - * is not supported for the current variant type. + * that is not supported for the current variant type. */ class TypeException : public OusiaException { private: @@ -686,6 +685,76 @@ public: // TODO: Use proper serialization function return os << "\"" << v.first << "\": " << v.second.toString(true); } + + /* + * Comprison operators. + */ + + friend bool operator<(const Variant &lhs, const Variant &rhs) + { + // If the types do not match, we can not do a meaningful comparison. + if (lhs.getType() != rhs.getType()) { + throw TypeException(lhs.getType(), rhs.getType()); + } + switch (lhs.getType()) { + case Type::NULLPTR: + return false; + case Type::BOOL: + return lhs.boolVal < rhs.boolVal; + case Type::INT: + return lhs.intVal < rhs.intVal; + case Type::DOUBLE: + return lhs.doubleVal < rhs.doubleVal; + case Type::STRING: + return lhs.asString() < rhs.asString(); + case Type::ARRAY: + return lhs.asArray() < rhs.asArray(); + case Type::MAP: + return lhs.asMap() < rhs.asMap(); + } + throw OusiaException("Internal Error! Unknown type!"); + } + friend bool operator>(const Variant &lhs, const Variant &rhs) + { + return rhs < lhs; + } + friend bool operator<=(const Variant &lhs, const Variant &rhs) + { + return !(lhs > rhs); + } + friend bool operator>=(const Variant &lhs, const Variant &rhs) + { + return !(lhs < rhs); + } + + friend bool operator==(const Variant &lhs, const Variant &rhs) + { + if (lhs.getType() != rhs.getType()) { + return false; + } + switch (lhs.getType()) { + case Type::NULLPTR: + return true; + case Type::BOOL: + return lhs.boolVal == rhs.boolVal; + case Type::INT: + return lhs.intVal == rhs.intVal; + case Type::DOUBLE: + return lhs.doubleVal == rhs.doubleVal; + case Type::STRING: + return lhs.asString() == rhs.asString(); + case Type::ARRAY: + return lhs.asArray() == rhs.asArray(); + case Type::MAP: + return lhs.asMap() == rhs.asMap(); + } + throw OusiaException("Internal Error! Unknown type!"); + } + + friend bool operator!=(const Variant &lhs, const Variant &rhs) + { + return !(lhs == rhs); + } }; } diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index 51b2fd2..4bbcc18 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -122,8 +122,7 @@ void CSSParser::parseSelectors(Rooted<SelectorNode> root, { auto tuple = parseSelector(tokenizer, ctx); // append the SelectorPath to the root node. - std::vector<Rooted<SelectorNode>> unmergedLeafs = - root->append(tuple.first); + std::vector<Rooted<SelectorNode>> unmergedLeafs = root->append(tuple.first); // append the leaf to the leafList. switch (unmergedLeafs.size()) { case 0: @@ -167,8 +166,8 @@ std::pair<Rooted<SelectorNode>, Rooted<SelectorNode>> CSSParser::parseSelector( // so we parse the rest of the subsequent SelectorPath auto tuple = parseSelector(tokenizer, ctx); // then we establish the DESCENDANT relationship - s->getEdges().push_back(new SelectorNode::SelectorEdge( - ctx.manager, tuple.first)); + s->getEdges().push_back( + new SelectorNode::SelectorEdge(ctx.manager, tuple.first)); // and we return this node as well as the leaf. return std::make_pair(s, tuple.second); } @@ -226,14 +225,16 @@ Rooted<SelectorNode> CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, return n; } // parse the argument list. - std::vector<std::string> args; + Variant::arrayType args; // we require at least one argument, if parantheses are used - expect(TOKEN_TEXT, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back(variant::Reader::parseGeneric(tokenizer.getInput(), + ctx.logger, + {',', ')'}).second); while (expect(COMMA, tokenizer, t, false, ctx)) { // as long as we find commas we expect new arguments. - expect(TOKEN_TEXT, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back( + variant::Reader::parseGeneric( + tokenizer.getInput(), ctx.logger, {',', ')'}).second); } expect(PAREN_CLOSE, tokenizer, t, true, ctx); // and we return with the finished Selector. @@ -247,7 +248,7 @@ Rooted<SelectorNode> CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // so we expect an ID now. Token t; expect(TOKEN_TEXT, tokenizer, t, true, ctx); - std::vector<std::string> args{t.content}; + Variant::arrayType args{Variant(t.content.c_str())}; // and we return the finished Selector Rooted<SelectorNode> n{ new SelectorNode(ctx.manager, name, {"has_id", args, false})}; @@ -262,7 +263,7 @@ Rooted<SelectorNode> CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // in both cases the attribute name comes first. Token t; expect(TOKEN_TEXT, tokenizer, t, true, ctx); - std::vector<std::string> args{t.content}; + Variant::arrayType args{Variant(t.content.c_str())}; if (!expect(EQUALS, tokenizer, t, false, ctx)) { // if no equals sign follows we have a has_attribute // PseudoSelector @@ -276,7 +277,7 @@ Rooted<SelectorNode> CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // with an equals sign we have a has_value PseudoSelector and // expect the value next. expect(STRING, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back(Variant(t.content.c_str())); // then we expect a closing bracket. expect(BRACKET_CLOSE, tokenizer, t, true, ctx); // and then we can return the result. @@ -313,14 +314,14 @@ void CSSParser::parseRules(CodeTokenizer &tokenizer, Rooted<RuleSet> ruleSet, ParserContext &ctx) { std::string key; - variant::Variant value; + Variant value; while (parseRule(tokenizer, ctx, key, value)) { ruleSet->getRules().insert({key, value}); } } bool CSSParser::parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, - std::string &key, variant::Variant &value) + std::string &key, Variant &value) { Token t; if (!expect(TOKEN_TEXT, tokenizer, t, false, ctx)) { diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp index a4d8cdc..82f0cd1 100644 --- a/src/plugins/css/CSSParser.hpp +++ b/src/plugins/css/CSSParser.hpp @@ -112,7 +112,7 @@ private: * @return true if a rule was found. */ bool parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, - std::string &key, variant::Variant &value); + std::string &key, Variant &value); /** * A convenience function to wrap around the tokenizer peek() function that diff --git a/test/core/ResourceLocatorTest.cpp b/test/core/ResourceLocatorTest.cpp new file mode 100644 index 0000000..38cfc3e --- /dev/null +++ b/test/core/ResourceLocatorTest.cpp @@ -0,0 +1,69 @@ +/* + 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 <gtest/gtest.h> + +#include <core/ResourceLocator.hpp> + +#include <sstream> + +namespace ousia { + +class TestResourceLocator : public ResourceLocator { +public: + ResourceLocator::Location locate( + const std::string &path, const std::string &relativeTo, + const ResourceLocator::Type type) const override + { + // trivial test implementation. + return ResourceLocator::Location(true, *this, type, path); + } + + std::unique_ptr<std::istream> stream( + const std::string &location) const override + { + // trivial test implementation. + std::unique_ptr<std::stringstream> ss(new std::stringstream()); + (*ss) << "test"; + return std::move(ss); + } +}; + +TEST(ResourceLocator, locate) +{ + TestResourceLocator instance; + ResourceLocator::Location location = + instance.locate("path", "", ResourceLocator::Type::DOMAIN); + ASSERT_TRUE(location.found); + ASSERT_EQ(ResourceLocator::Type::DOMAIN, location.type); + ASSERT_EQ("path", location.location); +} + +TEST(ResourceLocator, stream) +{ + TestResourceLocator instance; + ResourceLocator::Location location = + instance.locate("path", "", ResourceLocator::Type::DOMAIN); + std::unique_ptr<std::istream> is = location.stream(); + + std::string str; + *is >> str; + + ASSERT_EQ("test", str); +} +} diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index 270c350..e51cf36 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -121,6 +121,21 @@ TEST(Variant, mapValue) ASSERT_EQ(2, v2.asMap().find("key1")->second.asArray()[1].asInt()); } +TEST(Variant, relationalOperators){ + Variant a{4}; + Variant b{4}; + + ASSERT_EQ(a,b); + + b.setInt(5); + ASSERT_TRUE(a < b); + + b.setDouble(4); + ASSERT_FALSE(a == b); + + a.setDouble(4); + ASSERT_EQ(a,b); +} } diff --git a/test/plugins/css/CSSParserTest.cpp b/test/plugins/css/CSSParserTest.cpp index 8873673..6499375 100644 --- a/test/plugins/css/CSSParserTest.cpp +++ b/test/plugins/css/CSSParserTest.cpp @@ -120,7 +120,7 @@ TEST(CSSParser, testParseSelectors) Rooted<SelectorNode> Ag = children[1]; ASSERT_EQ("A", Ag->getName()); { - PseudoSelector select{"g", {"4", "2", "3"}, true}; + PseudoSelector select{"g", {Variant(4), Variant(2), Variant(3)}, true}; ASSERT_EQ(select, Ag->getPseudoSelector()); } ASSERT_EQ(0, Ag->getEdges().size()); |