summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/parse.hpp
blob: 79a32762277cb1fb2e73d0cd69bf71250c907cee (plain)
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/array_assertion.hpp>
#include <mbgl/style/expression/case.hpp>
#include <mbgl/style/expression/check_subtype.hpp>
#include <mbgl/style/expression/coalesce.hpp>
#include <mbgl/style/expression/compound_expression.hpp>
#include <mbgl/style/expression/curve.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/literal.hpp>
#include <mbgl/style/expression/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 = Literal::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 = Coalesce::parse(value, context);
        } else if (*op == "case") {
            parsed = Case::parse(value, context);
        } else if (*op == "array") {
            parsed = ArrayAssertion::parse(value, context);
        } else {
            parsed = CompoundExpressions::parse(value, context);
        }
    } else {
        if (isObject(value)) {
            context.error(R"(Bare objects invalid. Use ["literal", {...}] instead.)");
            return ParseResult();
        }
        
        parsed = Literal::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