[core] Introduce "collator" expressions

Cross platform parsing and evaluation code.
This commit is contained in:
Chris Loer
2018-06-29 15:56:37 -07:00
committed by Chris Loer
parent b9d3ecc990
commit 9ff5d34ef2
15 changed files with 297 additions and 16 deletions

View File

@@ -0,0 +1,29 @@
#pragma once
#include <mbgl/util/feature.hpp>
#include <mbgl/util/optional.hpp>
#include <string>
#include <memory>
namespace mbgl {
namespace style {
namespace expression {
class Collator {
public:
Collator(bool caseSensitive, bool diacriticSensitive, optional<std::string> locale = {});
bool operator==(const Collator& other) const;
int compare(const std::string& lhs, const std::string& rhs) const;
std::string resolvedLocale() const;
private:
class Impl;
std::shared_ptr<Impl> impl;
};
} // namespace expression
} // namespace style
} // namespace mbgl

View File

@@ -0,0 +1,44 @@
#pragma once
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>
#include <memory>
namespace mbgl {
namespace style {
namespace expression {
class CollatorExpression : public Expression {
public:
CollatorExpression(std::unique_ptr<Expression> caseSensitive,
std::unique_ptr<Expression> diacriticSensitive,
optional<std::unique_ptr<Expression>> locale);
EvaluationResult evaluate(const EvaluationContext&) const override;
static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&);
void eachChild(const std::function<void(const Expression&)>&) const override;
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override {
// Technically the set of possible outputs is the combinatoric set of Collators produced
// by all possibleOutputs of locale/caseSensitive/diacriticSensitive
// But for the primary use of Collators in comparison operators, we ignore the Collator's
// possibleOutputs anyway, so we can get away with leaving this undefined for now.
return { nullopt };
}
mbgl::Value serialize() const override;
std::string getOperator() const override { return "collator"; }
private:
std::unique_ptr<Expression> caseSensitive;
std::unique_ptr<Expression> diacriticSensitive;
optional<std::unique_ptr<Expression>> locale;
};
} // namespace expression
} // namespace style
} // namespace mbgl

View File

@@ -1,5 +1,6 @@
#pragma once
#include <mbgl/style/expression/collator_expression.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>
@@ -12,7 +13,7 @@ namespace expression {
class Equals : public Expression {
public:
Equals(std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs, bool negate);
Equals(std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs, optional<std::unique_ptr<Expression>> collator, bool negate);
static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&);
@@ -25,6 +26,7 @@ public:
private:
std::unique_ptr<Expression> lhs;
std::unique_ptr<Expression> rhs;
optional<std::unique_ptr<Expression>> collator;
bool negate;
};

View File

@@ -60,6 +60,12 @@ struct ValueType {
std::string getName() const { return "value"; }
bool operator==(const ValueType&) const { return true; }
};
struct CollatorType {
constexpr CollatorType() {}; // NOLINT
std::string getName() const { return "collator"; }
bool operator==(const CollatorType&) const { return true; }
};
constexpr NullType Null;
constexpr NumberType Number;
@@ -68,6 +74,7 @@ constexpr BooleanType Boolean;
constexpr ColorType Color;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr CollatorType Collator;
constexpr ErrorType Error;
struct Array;
@@ -81,6 +88,7 @@ using Type = variant<
ObjectType,
ValueType,
mapbox::util::recursive_wrapper<Array>,
CollatorType,
ErrorType>;
struct Array {

View File

@@ -1,5 +1,6 @@
#pragma once
#include <mbgl/style/expression/collator.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/types.hpp>
@@ -23,6 +24,7 @@ using ValueBase = variant<
double,
std::string,
Color,
Collator,
mapbox::util::recursive_wrapper<std::vector<Value>>,
mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
struct Value : ValueBase {