summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/parse/coalesce.hpp
blob: 2e902cdc412ba1809e8b6971f27ef11ed04af879 (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
#pragma once

#include <vector>
#include <memory>
#include <mbgl/style/expression/coalesce.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/conversion.hpp>

namespace mbgl {
namespace style {
namespace expression {

struct ParseCoalesce {
    static ParseResult parse(const mbgl::style::conversion::Value& value, ParsingContext ctx) {
        using namespace mbgl::style::conversion;
        assert(isArray(value));
        auto length = arrayLength(value);
        if (length < 2) {
            ctx.error("Expected at least one argument.");
            return ParseResult();
        }
        
        Coalesce::Args args;
        optional<type::Type> outputType;
        if (ctx.expected && *ctx.expected != type::Value) {
            outputType = ctx.expected;
        }

        for (std::size_t i = 1; i < length; i++) {
            auto parsed = parseExpression(arrayMember(value, i), ParsingContext(ctx, i, outputType));
            if (!parsed) {
                return parsed;
            }
            if (!outputType) {
                outputType = (*parsed)->getType();
            }
            args.push_back(std::move(*parsed));
        }
        
        assert(outputType);
        return ParseResult(std::make_unique<Coalesce>(*outputType, std::move(args)));
    }
};

} // namespace expression
} // namespace style
} // namespace mbgl