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

#include <mbgl/style/expression/parse.hpp>
#include <mbgl/style/expression/at.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/expression/type.hpp>

namespace mbgl {
namespace style {
namespace expression {

struct ParseAt {
    static ParseResult parse(const mbgl::style::conversion::Value& value, ParsingContext ctx) {
        using namespace mbgl::style::conversion;
        assert(isArray(value));
        
        std::size_t length = arrayLength(value);
        if (length != 3) {
            ctx.error("Expected 2 arguments, but found " + std::to_string(length - 1) + " instead.");
            return ParseResult();
        }

        ParseResult index = parseExpression(arrayMember(value, 1), ParsingContext(ctx, 1, {type::Number}));
        ParseResult input = parseExpression(arrayMember(value, 2),
                                            ParsingContext(ctx, 2, {type::Array(ctx.expected ? *ctx.expected : type::Value)}));

        if (!index || !input) return ParseResult();

        return ParseResult(std::make_unique<At>(std::move(*index), std::move(*input)));

    }
};

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