diff options
Diffstat (limited to 'include')
5 files changed, 44 insertions, 59 deletions
diff --git a/include/mbgl/style/conversion/data_driven_property_value.hpp b/include/mbgl/style/conversion/data_driven_property_value.hpp index b4297816df..07ed201c99 100644 --- a/include/mbgl/style/conversion/data_driven_property_value.hpp +++ b/include/mbgl/style/conversion/data_driven_property_value.hpp @@ -4,12 +4,12 @@ #include <mbgl/style/conversion.hpp> #include <mbgl/style/conversion/constant.hpp> #include <mbgl/style/conversion/function.hpp> -#include <mbgl/style/conversion/expression.hpp> #include <mbgl/style/expression/is_expression.hpp> #include <mbgl/style/expression/is_constant.hpp> #include <mbgl/style/expression/find_zoom_curve.hpp> #include <mbgl/style/expression/literal.hpp> #include <mbgl/style/expression/value.hpp> +#include <mbgl/style/expression/parsing_context.hpp> #include <unordered_set> @@ -22,15 +22,15 @@ template <class T> struct Converter<DataDrivenPropertyValue<T>> { optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error) const { + using namespace mbgl::style::expression; + if (isUndefined(value)) { return DataDrivenPropertyValue<T>(); - } else if (expression::isExpression(value)) { - optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>( - value, - error, - valueTypeToExpressionType<T>()); - + } else if (isExpression(value)) { + ParsingContext ctx(valueTypeToExpressionType<T>()); + ParseResult expression = ctx.parseLayerPropertyExpression(value); if (!expression) { + error = { ctx.getCombinedErrors() }; return {}; } diff --git a/include/mbgl/style/conversion/expression.hpp b/include/mbgl/style/conversion/expression.hpp deleted file mode 100644 index c5fcf906a7..0000000000 --- a/include/mbgl/style/conversion/expression.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include <mbgl/style/expression/parsing_context.hpp> -#include <mbgl/style/expression/type.hpp> -#include <mbgl/style/conversion.hpp> - -#include <memory> - -namespace mbgl { -namespace style { -namespace conversion { - -using namespace mbgl::style::expression; - -template<> struct Converter<std::unique_ptr<Expression>> { - optional<std::unique_ptr<Expression>> operator()(const Convertible& value, Error& error, type::Type expected) const { - ParsingContext ctx(optional<type::Type> {expected}); - ParseResult parsed = ctx.parse(value); - if (parsed) { - return std::move(*parsed); - } - std::string combinedError; - for (const ParsingError& parsingError : ctx.getErrors()) { - if (combinedError.size() > 0) { - combinedError += "\n"; - } - if (parsingError.key.size() > 0) { - combinedError += parsingError.key + ": "; - } - combinedError += parsingError.message; - } - error = { combinedError }; - return {}; - }; -}; - -} // namespace conversion -} // namespace style -} // namespace mbgl diff --git a/include/mbgl/style/conversion/heatmap_color_property_value.hpp b/include/mbgl/style/conversion/heatmap_color_property_value.hpp index e3689c524c..a4710792d2 100644 --- a/include/mbgl/style/conversion/heatmap_color_property_value.hpp +++ b/include/mbgl/style/conversion/heatmap_color_property_value.hpp @@ -4,11 +4,11 @@ #include <mbgl/style/conversion.hpp> #include <mbgl/style/conversion/constant.hpp> #include <mbgl/style/conversion/function.hpp> -#include <mbgl/style/conversion/expression.hpp> #include <mbgl/style/expression/value.hpp> #include <mbgl/style/expression/is_constant.hpp> #include <mbgl/style/expression/is_expression.hpp> #include <mbgl/style/expression/find_zoom_curve.hpp> +#include <mbgl/style/expression/parsing_context.hpp> namespace mbgl { namespace style { @@ -17,11 +17,14 @@ namespace conversion { template <> struct Converter<HeatmapColorPropertyValue> { optional<HeatmapColorPropertyValue> operator()(const Convertible& value, Error& error) const { + using namespace mbgl::style::expression; if (isUndefined(value)) { return HeatmapColorPropertyValue(); } else if (isExpression(value)) { - optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>(value, error, expression::type::Color); + ParsingContext ctx(type::Color); + ParseResult expression = ctx.parseLayerPropertyExpression(value); if (!expression) { + error = { ctx.getCombinedErrors() }; return {}; } assert(*expression); diff --git a/include/mbgl/style/conversion/property_value.hpp b/include/mbgl/style/conversion/property_value.hpp index 97117de2ec..3130661f61 100644 --- a/include/mbgl/style/conversion/property_value.hpp +++ b/include/mbgl/style/conversion/property_value.hpp @@ -4,11 +4,11 @@ #include <mbgl/style/conversion.hpp> #include <mbgl/style/conversion/constant.hpp> #include <mbgl/style/conversion/function.hpp> -#include <mbgl/style/conversion/expression.hpp> #include <mbgl/style/expression/value.hpp> #include <mbgl/style/expression/is_constant.hpp> #include <mbgl/style/expression/is_expression.hpp> #include <mbgl/style/expression/find_zoom_curve.hpp> +#include <mbgl/style/expression/parsing_context.hpp> namespace mbgl { namespace style { @@ -17,13 +17,18 @@ namespace conversion { template <class T> struct Converter<PropertyValue<T>> { optional<PropertyValue<T>> operator()(const Convertible& value, Error& error) const { + using namespace mbgl::style::expression; + if (isUndefined(value)) { return PropertyValue<T>(); } else if (isExpression(value)) { - optional<std::unique_ptr<Expression>> expression = convert<std::unique_ptr<Expression>>(value, error, valueTypeToExpressionType<T>()); + ParsingContext ctx(valueTypeToExpressionType<T>()); + ParseResult expression = ctx.parseLayerPropertyExpression(value); if (!expression) { + error = { ctx.getCombinedErrors() }; return {}; } + if (isFeatureConstant(**expression)) { return { CameraFunction<T>(std::move(*expression)) }; } else { diff --git a/include/mbgl/style/expression/parsing_context.hpp b/include/mbgl/style/expression/parsing_context.hpp index f92a4c95ea..c19974a4f7 100644 --- a/include/mbgl/style/expression/parsing_context.hpp +++ b/include/mbgl/style/expression/parsing_context.hpp @@ -55,7 +55,7 @@ class ParsingContext { public: ParsingContext() : errors(std::make_shared<std::vector<ParsingError>>()) {} ParsingContext(std::string key_) : key(std::move(key_)), errors(std::make_shared<std::vector<ParsingError>>()) {} - explicit ParsingContext(optional<type::Type> expected_) + explicit ParsingContext(type::Type expected_) : expected(std::move(expected_)), errors(std::make_shared<std::vector<ParsingError>>()) {} @@ -67,6 +67,7 @@ public: std::string getKey() const { return key; } optional<type::Type> getExpected() const { return expected; } const std::vector<ParsingError>& getErrors() const { return *errors; } + const std::string getCombinedErrors() const; enum TypeAnnotationOption { includeTypeAnnotations, @@ -74,16 +75,21 @@ public: }; /* - Parse the given style-spec JSON value into an Expression object. - Specifically, this function is responsible for determining the expression - type (either Literal, or the one named in value[0]) and dispatching to the - appropriate ParseXxxx::parse(const V&, ParsingContext) method. + Parse the given style-spec JSON value as an expression. */ - ParseResult parse(const mbgl::style::conversion::Convertible& value, - TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); + ParseResult parseExpression(const mbgl::style::conversion::Convertible& value, + TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); /* - Parse a child expression. + Parse the given style-spec JSON value as an expression intended to be used + in a layout or paint property. This entails checking additional constraints + that exist in that context but not, e.g., for filters. + */ + ParseResult parseLayerPropertyExpression(const mbgl::style::conversion::Convertible& value, + TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); + + /* + Parse a child expression. For use by individual Expression::parse() methods. */ ParseResult parse(const mbgl::style::conversion::Convertible&, std::size_t, @@ -91,7 +97,7 @@ public: TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); /* - Parse a child expression. + Parse a child expression. For use by individual Expression::parse() methods. */ ParseResult parse(const mbgl::style::conversion::Convertible&, std::size_t index, @@ -141,6 +147,16 @@ private: errors(std::move(errors_)) {} + + /* + Parse the given style-spec JSON value into an Expression object. + Specifically, this function is responsible for determining the expression + type (either Literal, or the one named in value[0]) and dispatching to the + appropriate ParseXxxx::parse(const V&, ParsingContext) method. + */ + ParseResult parse(const mbgl::style::conversion::Convertible& value, + TypeAnnotationOption typeAnnotationOption = includeTypeAnnotations); + std::string key; optional<type::Type> expected; std::shared_ptr<detail::Scope> scope; |