1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#pragma once
#include <memory>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/expression/check_subtype.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/parse/array_assertion.hpp>
#include <mbgl/style/expression/parse/case.hpp>
#include <mbgl/style/expression/parse/coalesce.hpp>
#include <mbgl/style/expression/parse/compound_expression.hpp>
#include <mbgl/style/expression/parse/curve.hpp>
#include <mbgl/style/expression/parse/literal.hpp>
#include <mbgl/style/expression/parse/match.hpp>
#include <mbgl/style/expression/parsing_context.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 = conversion::toValue(value);
assert(v);
return v->match(
[&] (std::string) { return "string"; },
[&] (bool) { return "boolean"; },
[&] (auto) { return "number"; }
);
}
template <class V>
ParseResult parseExpression(const V& value, ParsingContext context)
{
using namespace mbgl::style::conversion;
ParseResult parsed;
if (isArray(value)) {
const std::size_t length = arrayLength(value);
if (length == 0) {
context.error(R"(Expected an array with at least one element. If you wanted a literal array, use ["literal", []].)");
return ParseResult();
}
const optional<std::string>& op = toString(arrayMember(value, 0));
if (!op) {
context.error(
"Expression name must be a string, but found " + getJSType(arrayMember(value, 0)) +
R"( instead. If you wanted a literal array, use ["literal", [...]].)",
0
);
return ParseResult();
}
if (*op == "literal") {
if (length != 2) {
context.error(
"'literal' expression requires exactly one argument, but found " + std::to_string(length - 1) + " instead."
);
return ParseResult();
}
parsed = ParseLiteral::parse(arrayMember(value, 1), context);
} else if (*op == "match") {
parsed = ParseMatch::parse(value, context);
} else if (*op == "curve") {
parsed = ParseCurve::parse(value, context);
} else if (*op == "coalesce") {
parsed = ParseCoalesce::parse(value, context);
} else if (*op == "case") {
parsed = ParseCase::parse(value, context);
} else if (*op == "array") {
parsed = ParseArrayAssertion::parse(value, context);
} else {
parsed = ParseCompoundExpression::parse(value, context);
}
} else {
if (isObject(value)) {
context.error(R"(Bare objects invalid. Use ["literal", {...}] instead.)");
return ParseResult();
}
parsed = ParseLiteral::parse(value, context);
}
if (!parsed) {
assert(context.errors.size() > 0);
} else if (context.expected) {
checkSubtype(*(context.expected), (*parsed)->getType(), context);
if (context.errors.size() > 0) {
return ParseResult();
}
}
return parsed;
}
} // namespace expression
} // namespace style
} // namespace mbgl
|