diff options
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/style/conversion/function.cpp | 4 | ||||
-rw-r--r-- | src/mbgl/style/expression/array_assertion.cpp | 106 | ||||
-rw-r--r-- | src/mbgl/style/expression/assertion.cpp | 79 | ||||
-rw-r--r-- | src/mbgl/style/expression/parsing_context.cpp | 12 |
4 files changed, 77 insertions, 124 deletions
diff --git a/src/mbgl/style/conversion/function.cpp b/src/mbgl/style/conversion/function.cpp index 2ce2f4eafd..7cc599be1d 100644 --- a/src/mbgl/style/conversion/function.cpp +++ b/src/mbgl/style/conversion/function.cpp @@ -6,7 +6,6 @@ #include <mbgl/style/expression/interpolate.hpp> #include <mbgl/style/expression/match.hpp> #include <mbgl/style/expression/case.hpp> -#include <mbgl/style/expression/array_assertion.hpp> #include <mbgl/util/string.hpp> #include <cassert> @@ -679,8 +678,7 @@ optional<std::unique_ptr<Expression>> convertFunctionToExpression(type::Type typ return toColor(get(literal(*property))); }, [&] (const type::Array& array) -> optional<std::unique_ptr<Expression>> { - return std::unique_ptr<Expression>( - std::make_unique<ArrayAssertion>(array, get(literal(*property)))); + return assertion(array, get(literal(*property))); }, [&] (const auto&) -> optional<std::unique_ptr<Expression>> { assert(false); // No properties use this type. diff --git a/src/mbgl/style/expression/array_assertion.cpp b/src/mbgl/style/expression/array_assertion.cpp deleted file mode 100644 index 9df586bdc3..0000000000 --- a/src/mbgl/style/expression/array_assertion.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include <mbgl/style/expression/array_assertion.hpp> -#include <mbgl/style/expression/check_subtype.hpp> -#include <mbgl/style/conversion_impl.hpp> -#include <mbgl/util/string.hpp> - -namespace mbgl { -namespace style { -namespace expression { - -EvaluationResult ArrayAssertion::evaluate(const EvaluationContext& params) const { - auto result = input->evaluate(params); - if (!result) { - return result.error(); - } - type::Type expected = getType(); - type::Type actual = typeOf(*result); - if (checkSubtype(expected, actual)) { - return EvaluationError { - "Expected value to be of type " + toString(expected) + - ", but found " + toString(actual) + " instead." - }; - } - return *result; -} - -void ArrayAssertion::eachChild(const std::function<void(const Expression&)>& visit) const { - visit(*input); -} - -using namespace mbgl::style::conversion; -ParseResult ArrayAssertion::parse(const Convertible& value, ParsingContext& ctx) { - - static std::unordered_map<std::string, type::Type> itemTypes { - {"string", type::String}, - {"number", type::Number}, - {"boolean", type::Boolean} - }; - - auto length = arrayLength(value); - if (length < 2 || length > 4) { - ctx.error("Expected 1, 2, or 3 arguments, but found " + util::toString(length - 1) + " instead."); - return ParseResult(); - } - - optional<type::Type> itemType; - optional<std::size_t> N; - if (length > 2) { - optional<std::string> itemTypeName = toString(arrayMember(value, 1)); - auto it = itemTypeName ? itemTypes.find(*itemTypeName) : itemTypes.end(); - if (it == itemTypes.end()) { - ctx.error( - R"(The item type argument of "array" must be one of string, number, boolean)", - 1 - ); - return ParseResult(); - } - itemType = it->second; - } else { - itemType = {type::Value}; - } - - if (length > 3) { - auto n = toNumber(arrayMember(value, 2)); - if (!n || *n != std::floor(*n)) { - ctx.error( - R"(The length argument to "array" must be a positive integer literal.)", - 2 - ); - return ParseResult(); - } - N = optional<std::size_t>(*n); - } - - auto input = ctx.parse(arrayMember(value, length - 1), length - 1, {type::Value}); - if (!input) { - return input; - } - - return ParseResult(std::make_unique<ArrayAssertion>( - type::Array(*itemType, N), - std::move(*input) - )); -} - -mbgl::Value ArrayAssertion::serialize() const { - std::vector<mbgl::Value> serialized; - serialized.emplace_back(getOperator()); - - - const auto array = getType().get<type::Array>(); - if (array.itemType.is<type::StringType>() - || array.itemType.is<type::NumberType>() - || array.itemType.is<type::BooleanType>()) { - serialized.emplace_back(type::toString(array.itemType)); - if (array.N) { - serialized.emplace_back(uint64_t(*array.N)); - } - } - - serialized.emplace_back(input->serialize()); - return serialized; -} - -} // namespace expression -} // namespace style -} // namespace mbgl diff --git a/src/mbgl/style/expression/assertion.cpp b/src/mbgl/style/expression/assertion.cpp index 7e93003ac3..8e5a8b555d 100644 --- a/src/mbgl/style/expression/assertion.cpp +++ b/src/mbgl/style/expression/assertion.cpp @@ -30,22 +30,64 @@ ParseResult Assertion::parse(const Convertible& value, ParsingContext& ctx) { return ParseResult(); } - auto it = types.find(*toString(arrayMember(value, 0))); - assert(it != types.end()); - + std::size_t i = 1; + type::Type type; + + std::string name = *toString(arrayMember(value, 0)); + if (name == "array") { + optional<type::Type> itemType; + if (length > 2) { + optional<std::string> itemTypeName = toString(arrayMember(value, 1)); + auto it = itemTypeName ? types.find(*itemTypeName) : types.end(); + if (it == types.end() || it->second == type::Object) { + ctx.error( + R"(The item type argument of "array" must be one of string, number, boolean)", + 1 + ); + return ParseResult(); + } + itemType = it->second; + i++; + } else { + itemType = {type::Value}; + } + + optional<std::size_t> N; + if (length > 3) { + auto m = arrayMember(value, 2); + optional<float> n = toNumber(m); + if (!isUndefined(m) && + (!n || *n < 0 || *n != std::floor(*n))) { + ctx.error( + R"(The length argument to "array" must be a positive integer literal.)", + 2 + ); + return ParseResult(); + } + if (n) { + N = optional<std::size_t>(*n); + } + i++; + } + + type = type::Array(*itemType, N); + } else { + type = types.at(name); + } + std::vector<std::unique_ptr<Expression>> parsed; parsed.reserve(length - 1); - for (std::size_t i = 1; i < length; i++) { + for (; i < length; i++) { ParseResult input = ctx.parse(arrayMember(value, i), i, {type::Value}); if (!input) return ParseResult(); parsed.push_back(std::move(*input)); } - return ParseResult(std::make_unique<Assertion>(it->second, std::move(parsed))); + return ParseResult(std::make_unique<Assertion>(type, std::move(parsed))); } std::string Assertion::getOperator() const { - return type::toString(getType()); + return getType().is<type::Array>() ? "array" : type::toString(getType()); } EvaluationResult Assertion::evaluate(const EvaluationContext& params) const { @@ -90,6 +132,31 @@ std::vector<optional<Value>> Assertion::possibleOutputs() const { return result; } +mbgl::Value Assertion::serialize() const { + std::vector<mbgl::Value> serialized; + serialized.emplace_back(getOperator()); + + if (getType().is<type::Array>()) { + const auto array = getType().get<type::Array>(); + if (array.itemType.is<type::StringType>() + || array.itemType.is<type::NumberType>() + || array.itemType.is<type::BooleanType>()) { + serialized.emplace_back(type::toString(array.itemType)); + if (array.N) { + serialized.emplace_back(uint64_t(*array.N)); + } else if (inputs.size() > 1) { + serialized.emplace_back(mbgl::NullValue()); + } + } + } + + for (const auto& input : inputs) { + serialized.emplace_back(input->serialize()); + } + + return serialized; +} + } // namespace expression } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/expression/parsing_context.cpp b/src/mbgl/style/expression/parsing_context.cpp index 3fe50aa1e4..29d04d96a2 100644 --- a/src/mbgl/style/expression/parsing_context.cpp +++ b/src/mbgl/style/expression/parsing_context.cpp @@ -6,7 +6,6 @@ #include <mbgl/style/expression/expression.hpp> #include <mbgl/style/expression/at.hpp> -#include <mbgl/style/expression/array_assertion.hpp> #include <mbgl/style/expression/assertion.hpp> #include <mbgl/style/expression/boolean_operator.hpp> #include <mbgl/style/expression/case.hpp> @@ -46,8 +45,7 @@ bool isConstant(const Expression& expression) { } bool isTypeAnnotation = expression.getKind() == Kind::Coercion || - expression.getKind() == Kind::Assertion || - expression.getKind() == Kind::ArrayAssertion; + expression.getKind() == Kind::Assertion; bool childrenConstant = true; expression.eachChild([&](const Expression& child) { @@ -105,7 +103,7 @@ const ExpressionRegistry& getExpressionRegistry() { {"<=", parseComparison}, {"all", All::parse}, {"any", Any::parse}, - {"array", ArrayAssertion::parse}, + {"array", Assertion::parse}, {"at", At::parse}, {"boolean", Assertion::parse}, {"case", Case::parse}, @@ -171,14 +169,10 @@ ParseResult ParsingContext::parse(const Convertible& value, TypeAnnotationOption if (expected) { const type::Type actual = (*parsed)->getType(); - if ((*expected == type::String || *expected == type::Number || *expected == type::Boolean || *expected == type::Object) && actual == type::Value) { + if ((*expected == type::String || *expected == type::Number || *expected == type::Boolean || *expected == type::Object || expected->is<type::Array>()) && actual == type::Value) { if (typeAnnotationOption == includeTypeAnnotations) { parsed = { std::make_unique<Assertion>(*expected, array(std::move(*parsed))) }; } - } else if (expected->is<type::Array>() && actual == type::Value) { - if (typeAnnotationOption == includeTypeAnnotations) { - parsed = { std::make_unique<ArrayAssertion>(expected->get<type::Array>(), std::move(*parsed)) }; - } } else if (*expected == type::Color && (actual == type::Value || actual == type::String)) { if (typeAnnotationOption == includeTypeAnnotations) { parsed = { std::make_unique<Coercion>(*expected, array(std::move(*parsed))) }; |