Skip to content

Commit

Permalink
Merge pull request sass#2769 from mgreter/feature/improve-compare-ope…
Browse files Browse the repository at this point in the history
…rators

improve compare operators (merging in good faith that appveyor CI will come back green too).
  • Loading branch information
mgreter authored Nov 29, 2018
2 parents 7141c01 + 41e0775 commit c3e4ca5
Show file tree
Hide file tree
Showing 16 changed files with 817 additions and 352 deletions.
22 changes: 10 additions & 12 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace Sass {
//////////////////////////////////////////////////////////////////////
class Expression : public AST_Node {
public:
enum Concrete_Type {
enum Type {
NONE,
BOOLEAN,
NUMBER,
Expand All @@ -148,21 +148,15 @@ namespace Sass {
VARIABLE,
NUM_TYPES
};
enum Simple_Type {
SIMPLE,
ATTR_SEL,
PSEUDO_SEL,
WRAPPED_SEL,
};
private:
// expressions in some contexts shouldn't be evaluated
ADD_PROPERTY(bool, is_delayed)
ADD_PROPERTY(bool, is_expanded)
ADD_PROPERTY(bool, is_interpolant)
ADD_PROPERTY(Concrete_Type, concrete_type)
ADD_PROPERTY(Type, concrete_type)
public:
Expression(ParserState pstate,
bool d = false, bool e = false, bool i = false, Concrete_Type ct = NONE)
bool d = false, bool e = false, bool i = false, Type ct = NONE)
: AST_Node(pstate),
is_delayed_(d),
is_expanded_(e),
Expand All @@ -183,7 +177,10 @@ namespace Sass {
static std::string type_name() { return ""; }
virtual bool is_false() { return false; }
// virtual bool is_true() { return !is_false(); }
virtual bool operator< (const Expression& rhs) const { return false; }
virtual bool operator== (const Expression& rhs) const { return false; }
inline bool operator>(const Expression& rhs) const { return rhs < *this; }
inline bool operator!=(const Expression& rhs) const { return !(rhs == *this); }
virtual bool eq(const Expression& rhs) const { return *this == rhs; };
virtual void set_delayed(bool delayed) { is_delayed(delayed); }
virtual bool has_interpolant() const { return is_interpolant(); }
Expand Down Expand Up @@ -244,6 +241,7 @@ namespace Sass {
T& operator[](size_t i) { return elements_[i]; }
virtual const T& at(size_t i) const { return elements_.at(i); }
virtual T& at(size_t i) { return elements_.at(i); }
const T& get(size_t i) const { return elements_[i]; }
const T& operator[](size_t i) const { return elements_[i]; }
virtual void append(T element)
{
Expand Down Expand Up @@ -361,7 +359,7 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////
class Statement : public AST_Node {
public:
enum Statement_Type {
enum Type {
NONE,
RULESET,
MEDIA,
Expand All @@ -387,11 +385,11 @@ namespace Sass {
IF
};
private:
ADD_PROPERTY(Statement_Type, statement_type)
ADD_PROPERTY(Type, statement_type)
ADD_PROPERTY(size_t, tabs)
ADD_PROPERTY(bool, group_end)
public:
Statement(ParserState pstate, Statement_Type st = NONE, size_t t = 0)
Statement(ParserState pstate, Type st = NONE, size_t t = 0)
: AST_Node(pstate), statement_type_(st), tabs_(t), group_end_(false)
{ }
Statement(const Statement* ptr)
Expand Down
27 changes: 19 additions & 8 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ namespace Sass {
class Placeholder_Selector;
typedef Placeholder_Selector* Placeholder_Selector_Ptr;
typedef Placeholder_Selector const* Placeholder_Selector_Ptr_Const;
class Element_Selector;
typedef Element_Selector* Element_Selector_Ptr;
typedef Element_Selector const* Element_Selector_Ptr_Const;
class Type_Selector;
typedef Type_Selector* Type_Selector_Ptr;
typedef Type_Selector const* Type_Selector_Ptr_Const;
class Class_Selector;
typedef Class_Selector* Class_Selector_Ptr;
typedef Class_Selector const* Class_Selector_Ptr_Const;
Expand Down Expand Up @@ -345,7 +345,7 @@ namespace Sass {
IMPL_MEM_OBJ(Selector_Schema);
IMPL_MEM_OBJ(Simple_Selector);
IMPL_MEM_OBJ(Placeholder_Selector);
IMPL_MEM_OBJ(Element_Selector);
IMPL_MEM_OBJ(Type_Selector);
IMPL_MEM_OBJ(Class_Selector);
IMPL_MEM_OBJ(Id_Selector);
IMPL_MEM_OBJ(Attribute_Selector);
Expand All @@ -365,21 +365,29 @@ namespace Sass {
return ex.isNull() ? 0 : ex->hash();
}
};
template <class T>
bool OrderFunction(const T& lhs, const T& rhs) {
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
};
struct OrderNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
return !lhs.isNull() && !rhs.isNull() && *lhs < *rhs;
return OrderFunction<T>(lhs, rhs);
}
};
struct CompareNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
template <class T>
bool CompareFunction(const T& lhs, const T& rhs) {
// code around sass logic issue. 1px == 1 is true
// but both items are still different keys in maps
if (dynamic_cast<Number*>(lhs.ptr()))
if (dynamic_cast<Number*>(rhs.ptr()))
return lhs->hash() == rhs->hash();
return !lhs.isNull() && !rhs.isNull() && *lhs == *rhs;
}
struct CompareNodes {
template <class T>
bool operator() (const T& lhs, const T& rhs) const {
return CompareFunction<T>(lhs, rhs);
}
};

Expand Down Expand Up @@ -423,6 +431,9 @@ namespace Sass {
typedef std::pair<Complex_Selector_Obj, SubSetMapPairs> SubSetMapResult;
typedef std::vector<SubSetMapResult> SubSetMapResults;

#define OrderSelectors OrderFunction<Selector_Obj>
typedef std::set<Selector_Obj, OrderNodes> SelectorSet;

typedef std::deque<Complex_Selector_Obj> ComplexSelectorDeque;
typedef std::set<Simple_Selector_Obj, OrderNodes> SimpleSelectorSet;
typedef std::set<Complex_Selector_Obj, OrderNodes> ComplexSelectorSet;
Expand Down
Loading

0 comments on commit c3e4ca5

Please sign in to comment.