MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parsing_context.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/util/string.hpp>
6 
7 #include <iterator>
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <utility>
13 #include <vector>
14 #include <optional>
15 
16 namespace mbgl {
17 namespace style {
18 namespace expression {
19 
20 class Expression;
21 
22 struct ParsingError {
25  bool operator==(const ParsingError& rhs) const { return message == rhs.message && key == rhs.key; }
26 };
27 
28 using ParseResult = std::optional<std::unique_ptr<Expression>>;
29 
30 namespace detail {
31 
32 class Scope {
33 public:
34  Scope(const std::map<std::string, std::shared_ptr<Expression>>& bindings_, std::shared_ptr<Scope> parent_ = nullptr) :
35  bindings(bindings_),
36  parent(std::move(parent_))
37  {}
38 
39  const std::map<std::string, std::shared_ptr<Expression>>& bindings;
40  std::shared_ptr<Scope> parent;
41 
42  std::optional<std::shared_ptr<Expression>> get(const std::string& name) {
43  auto it = bindings.find(name);
44  if (it != bindings.end()) {
45  return {it->second};
46  } else if (parent) {
47  return parent->get(name);
48  } else {
49  return std::optional<std::shared_ptr<Expression>>();
50  }
51  }
52 };
53 
54 } // namespace detail
55 
70  coerce,
71  assert,
72  omit
73 };
74 
76 public:
77  ParsingContext() : errors(std::make_shared<std::vector<ParsingError>>()) {}
78  ParsingContext(std::string key_) : key(std::move(key_)), errors(std::make_shared<std::vector<ParsingError>>()) {}
79  explicit ParsingContext(type::Type expected_)
80  : expected(std::move(expected_)),
81  errors(std::make_shared<std::vector<ParsingError>>())
82  {}
84 
85  ParsingContext(const ParsingContext&) = delete;
87 
88  std::string getKey() const { return key; }
89  std::optional<type::Type> getExpected() const { return expected; }
90  const std::vector<ParsingError>& getErrors() const { return *errors; }
92 
97  const std::optional<TypeAnnotationOption>& = std::nullopt);
98 
105 
110  std::size_t,
111  std::optional<type::Type> = std::nullopt,
112  const std::optional<TypeAnnotationOption>& = std::nullopt);
113 
118  std::size_t index,
119  std::optional<type::Type>,
120  const std::map<std::string, std::shared_ptr<Expression>>&);
121 
125  std::optional<std::string> checkType(const type::Type& t);
126 
127  std::optional<std::shared_ptr<Expression>> getBinding(const std::string& name) {
128  if (!scope) return std::optional<std::shared_ptr<Expression>>();
129  return scope->get(name);
130  }
131 
132  void error(std::string message) { errors->push_back({std::move(message), key}); }
133 
134  void error(std::string message, std::size_t child) {
135  errors->push_back({std::move(message), key + "[" + util::toString(child) + "]"});
136  }
137 
138  void error(std::string message, std::size_t child, std::size_t grandchild) {
139  errors->push_back(
140  {std::move(message), key + "[" + util::toString(child) + "][" + util::toString(grandchild) + "]"});
141  }
142 
144  errors->reserve(errors->size() + ctx.errors->size());
145  std::move(ctx.errors->begin(), ctx.errors->end(), std::inserter(*errors, errors->end()));
146  ctx.errors->clear();
147  }
148 
149  void clearErrors() {
150  errors->clear();
151  }
152 
153 private:
155  std::shared_ptr<std::vector<ParsingError>> errors_,
156  std::optional<type::Type> expected_,
157  std::shared_ptr<detail::Scope> scope_)
158  : key(std::move(key_)),
159  expected(std::move(expected_)),
160  scope(std::move(scope_)),
161  errors(std::move(errors_))
162  {}
163 
164 
171  ParseResult parse(const mbgl::style::conversion::Convertible& value, const std::optional<TypeAnnotationOption>& = std::nullopt);
172 
173  std::string key;
174  std::optional<type::Type> expected;
175  std::shared_ptr<detail::Scope> scope;
176  std::shared_ptr<std::vector<ParsingError>> errors;
177 };
178 
180 
181 } // namespace expression
182 } // namespace style
183 } // namespace mbgl
ParseResult parseExpression(const mbgl::style::conversion::Convertible &value, const std::optional< TypeAnnotationOption > &=std::nullopt)
const std::vector< ParsingError > & getErrors() const
std::optional< std::string > checkType(const type::Type &t)
std::optional< std::shared_ptr< Expression > > getBinding(const std::string &name)
void appendErrors(ParsingContext &&ctx)
ParsingContext(const ParsingContext &)=delete
ParseResult parse(const mbgl::style::conversion::Convertible &, std::size_t index, std::optional< type::Type >, const std::map< std::string, std::shared_ptr< Expression >> &)
ParsingContext(ParsingContext &&)=default
ParseResult parse(const mbgl::style::conversion::Convertible &, std::size_t, std::optional< type::Type >=std::nullopt, const std::optional< TypeAnnotationOption > &=std::nullopt)
void error(std::string message, std::size_t child)
ParseResult parseLayerPropertyExpression(const mbgl::style::conversion::Convertible &value)
std::optional< type::Type > getExpected() const
void error(std::string message, std::size_t child, std::size_t grandchild)
ParsingContext & operator=(const ParsingContext &)=delete
std::optional< std::shared_ptr< Expression > > get(const std::string &name)
const std::map< std::string, std::shared_ptr< Expression > > & bindings
Scope(const std::map< std::string, std::shared_ptr< Expression >> &bindings_, std::shared_ptr< Scope > parent_=nullptr)
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
variant< NullType, NumberType, BooleanType, StringType, ColorType, ObjectType, ValueType, mapbox::util::recursive_wrapper< Array >, CollatorType, FormattedType, ErrorType, ImageType > Type
Definition: type.hpp:108
bool isExpression(const conversion::Convertible &value)
std::optional< std::unique_ptr< Expression > > ParseResult
std::string toString(const CanonicalTileID &)
Definition: actor.hpp:15
nonstd::expected< T, E > expected
Definition: expected.hpp:8
Definition: tile_id.hpp:256
bool operator==(const ParsingError &rhs) const