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

#include <algorithm>
#include <cctype>
#include <mbgl/style/conversion/get_json_type.hpp>
#include <mbgl/style/expression/parse.hpp>
#include <mbgl/style/expression/let.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 ParseLet {
    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 < 4) {
            ctx.error("Expected at least 3 arguments, but found " + std::to_string(length - 1) + " instead.");
            return ParseResult();
        }
        
        std::map<std::string, std::shared_ptr<Expression>> bindings;
        for(std::size_t i = 1; i < length - 1; i += 2) {
            optional<std::string> name = toString(arrayMember(value, i));
            if (!name) {
                ctx.error("Expected string, but found " + getJSONType(arrayMember(value, i)) + " instead.", i);
                return ParseResult();
            }
            
            bool isValidName = std::all_of(name->begin(), name->end(), [](unsigned char c) {
                return std::isalnum(c) || c == '_';
            });
            if (!isValidName) {
                ctx.error("Variable names must contain only alphanumeric characters or '_'.", 1);
                return ParseResult();
            }
            
            ParseResult bindingValue = parseExpression(arrayMember(value, i + 1), ParsingContext(ctx, i + 1));
            if (!bindingValue) {
                return ParseResult();
            }
            
            bindings.emplace(*name, std::move(*bindingValue));
        }
        
        auto resultContext = ParsingContext(ctx, length - 1, ctx.expected, bindings);
        ParseResult result = parseExpression(arrayMember(value, length - 1), resultContext);
        if (!result) {
            return ParseResult();
        }
        
        return ParseResult(std::make_unique<Let>(std::move(bindings), std::move(*result)));
    }
};

struct ParseVar {
    static ParseResult parse(const mbgl::style::conversion::Value& value, ParsingContext ctx) {
        using namespace mbgl::style::conversion;
        assert(isArray(value));
        
        if (arrayLength(value) != 2 || !toString(arrayMember(value, 1))) {
            ctx.error("'var' expression requires exactly one string literal argument.");
            return ParseResult();
        }
        
        std::string name = *toString(arrayMember(value, 1));
        
        optional<std::shared_ptr<Expression>> bindingValue = ctx.getBinding(name);
        if (!bindingValue) {
            ctx.error(R"(Unknown variable ")" + name +  R"(". Make sure ")" +
                name + R"(" has been bound in an enclosing "let" expression before using it.)", 1);
            return ParseResult();
        }
        
        return ParseResult(std::make_unique<Var>(name, std::move(*bindingValue)));
    }
};

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