diff options
Diffstat (limited to 'include/mbgl/style/expression/parse.hpp')
-rw-r--r-- | include/mbgl/style/expression/parse.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/include/mbgl/style/expression/parse.hpp b/include/mbgl/style/expression/parse.hpp new file mode 100644 index 0000000000..3d7fad70b3 --- /dev/null +++ b/include/mbgl/style/expression/parse.hpp @@ -0,0 +1,65 @@ +#pragma once + +#include <memory> +#include <mbgl/style/expression/parsing_context.hpp> +#include <mbgl/style/expression/expression.hpp> +#include <mbgl/style/conversion.hpp> + +namespace mbgl { +namespace style { +namespace expression { + +using namespace mbgl::style; + +template <class V> +std::string getJSType(const V& value) { + using namespace mbgl::style::conversion; + if (isUndefined(value)) { + return "undefined"; + } + if (isArray(value) || isObject(value)) { + return "object"; + } + optional<mbgl::Value> v = toValue(value); + assert(v); + return v->match( + [&] (std::string) { return "string"; }, + [&] (bool) { return "boolean"; }, + [&] (auto) { return "number"; } + ); +} + +using ParseResult = variant<CompileError, std::unique_ptr<Expression>>; + +template <class V> +ParseResult parseExpression(const V& value, const ParsingContext& context) +{ + using namespace mbgl::style::conversion; + + if (isArray(value)) { + if (arrayLength(value) == 0) { + CompileError error = { + "Expected an array with at least one element. If you wanted a literal array, use [\"literal\", []].", + context.key() + }; + return error; + } + + const optional<std::string>& op = toString(arrayMember(value, 0)); + if (!op) { + CompileError error = { + "Expression name must be a string, but found " + getJSType(arrayMember(value, 0)) + + " instead. If you wanted a literal array, use [\"literal\", [...]].", + context.key(0) + }; + return error; + } + } + + return LiteralExpression::parse(value, context); +} + + +} // namespace expression +} // namespace style +} // namespace mbgl |