Skip to content

Commit

Permalink
Rename some parser function and variables
Browse files Browse the repository at this point in the history
Improves and fixes a few minor code issues
  • Loading branch information
mgreter committed Jul 13, 2015
1 parent e44a65c commit 40ab322
Show file tree
Hide file tree
Showing 49 changed files with 435 additions and 448 deletions.
88 changes: 22 additions & 66 deletions ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,40 +422,40 @@ namespace Sass {
// catch-all
return false;
}

Selector_List* Complex_Selector::unify_with(Complex_Selector* other, Context& ctx) {
To_String to_string;

Compound_Selector* thisBase = base();
Compound_Selector* rhsBase = other->base();

if( thisBase == 0 || rhsBase == 0 ) return 0;

// Not sure about this check, but closest way I could check to see if this is a ruby 'SimpleSequence' equivalent
if( tail()->combinator() != Combinator::ANCESTOR_OF || other->tail()->combinator() != Combinator::ANCESTOR_OF ) return 0;

Compound_Selector* unified = rhsBase->unify_with(thisBase, ctx);
if( unified == 0 ) return 0;

Node lhsNode = complexSelectorToNode(this, ctx);
Node rhsNode = complexSelectorToNode(other, ctx);

// Create a temp Complex_Selector, turn it into a Node, and combine it with the existing RHS node
Complex_Selector* fakeComplexSelector = new (ctx.mem) Complex_Selector(ParserState("[NODE]"), Complex_Selector::ANCESTOR_OF, unified, NULL);
Node unifiedNode = complexSelectorToNode(fakeComplexSelector, ctx);
rhsNode.plus(unifiedNode);

Node node = Extend::subweave(lhsNode, rhsNode, ctx);

Selector_List* result = new (ctx.mem) Selector_List(pstate());
for (NodeDeque::iterator iter = node.collection()->begin(), iterEnd = node.collection()->end(); iter != iterEnd; iter++) {
Node childNode = *iter;
childNode = Node::naiveTrim(childNode, ctx);

Complex_Selector* childNodeAsComplexSelector = nodeToComplexSelector(childNode, ctx);
if( childNodeAsComplexSelector ) { (*result) << childNodeAsComplexSelector; }
}

return result->length() ? result : 0;
}

Expand Down Expand Up @@ -597,16 +597,15 @@ namespace Sass {
}
return false;
}

Selector_List* Selector_List::unify_with(Selector_List* rhs, Context& ctx) {

vector<Complex_Selector*> unified_complex_selectors;
// Unify all of children with RHS's children, storing the results in `unified_complex_selectors`
for (size_t lhs_i = 0, lhs_L = length(); lhs_i < lhs_L; ++lhs_i) {
Complex_Selector* seq1 = (*this)[lhs_i];
for(size_t rhs_i = 0, rhs_L = rhs->length(); rhs_i < rhs_L; ++rhs_i) {
Complex_Selector* seq2 = (*rhs)[rhs_i];

Selector_List* result = seq1->unify_with(seq2, ctx);
if( result ) {
for(size_t i = 0, L = result->length(); i < L; ++i) {
Expand All @@ -615,24 +614,23 @@ namespace Sass {
}
}
}

// Creates the final Selector_List by combining all the complex selectors
Selector_List* final_result = new (ctx.mem) Selector_List(pstate());
for (auto itr = unified_complex_selectors.begin(); itr != unified_complex_selectors.end(); ++itr) {
*final_result << *itr;
}

return final_result;
}

void Selector_List::populate_extends(Selector_List* extendee, Context& ctx, ExtensionSubsetMap& extends) {
To_String to_string;

Selector_List* extender = this;
for (auto complex_sel : extendee->elements()) {
Complex_Selector* c = complex_sel;


// Ignore any parent selectors, until we find the first non Selector_Reference head
Compound_Selector* compound_sel = c->head();
Complex_Selector* pIter = complex_sel;
Expand All @@ -642,63 +640,21 @@ namespace Sass {
compound_sel = pHead;
break;
}

pIter = pIter->tail();
}

if (!pIter->head() || pIter->tail()) {
error("nested selectors may not be extended", c->pstate());
}

compound_sel->is_optional(extendee->is_optional());

for (size_t i = 0, L = extender->length(); i < L; ++i) {
// let's test this out
cerr << "REGISTERING EXTENSION REQUEST: " << (*extender)[i]->perform(&to_string) << " <- " << compound_sel->perform(&to_string) << endl;
extends.put(compound_sel->to_str_vec(), make_pair((*extender)[i], compound_sel));
}
}
};


/* not used anymore - remove?
Selector_Placeholder* Selector_List::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Complex_Selector::find_placeholder()
{
if (has_placeholder()) {
if (head() && head()->has_placeholder()) return head()->find_placeholder();
else if (tail() && tail()->has_placeholder()) return tail()->find_placeholder();
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Compound_Selector::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
// return this;
}
return 0;
}*/

/* not used anymore - remove?
Selector_Placeholder* Selector_Placeholder::find_placeholder()
{
return this;
}*/

vector<string> Compound_Selector::to_str_vec()
{
Expand Down
51 changes: 21 additions & 30 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace Sass {
RULESET,
MEDIA,
DIRECTIVE,
FEATURE,
SUPPORTS,
ATROOT,
BUBBLE,
KEYFRAMERULE
Expand Down Expand Up @@ -398,16 +398,16 @@ namespace Sass {
ATTACH_OPERATIONS()
};

///////////////////
// Feature queries.
///////////////////
class Feature_Block : public Has_Block {
ADD_PROPERTY(Feature_Query*, feature_queries)
//////////////////
// Query features.
//////////////////
class Supports_Block : public Has_Block {
ADD_PROPERTY(Supports_Query*, queries)
ADD_PROPERTY(Selector*, selector)
public:
Feature_Block(ParserState pstate, Feature_Query* fqs, Block* b)
: Has_Block(pstate, b), feature_queries_(fqs), selector_(0)
{ statement_type(FEATURE); }
Supports_Block(ParserState pstate, Supports_Query* queries = 0, Block* block = 0)
: Has_Block(pstate, block), queries_(queries), selector_(0)
{ statement_type(SUPPORTS); }
bool is_hoistable() { return true; }
bool bubbles() { return true; }
ATTACH_OPERATIONS()
Expand Down Expand Up @@ -663,15 +663,13 @@ namespace Sass {
ADD_PROPERTY(Native_Function, native_function)
ADD_PROPERTY(Sass_Function_Entry, c_function)
ADD_PROPERTY(void*, cookie)
ADD_PROPERTY(Context*, ctx)
ADD_PROPERTY(bool, is_overload_stub)
ADD_PROPERTY(Signature, signature)
public:
Definition(ParserState pstate,
string n,
Parameters* params,
Block* b,
Context* ctx,
Type t)
: Has_Block(pstate, b),
name_(n),
Expand All @@ -681,7 +679,6 @@ namespace Sass {
native_function_(0),
c_function_(0),
cookie_(0),
ctx_(ctx),
is_overload_stub_(false),
signature_(0)
{ }
Expand All @@ -690,7 +687,6 @@ namespace Sass {
string n,
Parameters* params,
Native_Function func_ptr,
Context* ctx,
bool overload_stub = false)
: Has_Block(pstate, 0),
name_(n),
Expand All @@ -700,7 +696,6 @@ namespace Sass {
native_function_(func_ptr),
c_function_(0),
cookie_(0),
ctx_(ctx),
is_overload_stub_(overload_stub),
signature_(sig)
{ }
Expand All @@ -709,7 +704,6 @@ namespace Sass {
string n,
Parameters* params,
Sass_Function_Entry c_func,
Context* ctx,
bool whatever,
bool whatever2)
: Has_Block(pstate, 0),
Expand All @@ -720,7 +714,6 @@ namespace Sass {
native_function_(0),
c_function_(c_func),
cookie_(sass_function_get_cookie(c_func)),
ctx_(ctx),
is_overload_stub_(false),
signature_(sig)
{ }
Expand Down Expand Up @@ -1472,18 +1465,18 @@ namespace Sass {
///////////////////
// Feature queries.
///////////////////
class Feature_Query : public Expression, public Vectorized<Feature_Query_Condition*> {
class Supports_Query : public Expression, public Vectorized<Supports_Condition*> {
public:
Feature_Query(ParserState pstate, size_t s = 0)
: Expression(pstate), Vectorized<Feature_Query_Condition*>(s)
Supports_Query(ParserState pstate, size_t s = 0)
: Expression(pstate), Vectorized<Supports_Condition*>(s)
{ }
ATTACH_OPERATIONS()
};

////////////////////////////////////////////////////////
// Feature expressions (for use inside feature queries).
////////////////////////////////////////////////////////
class Feature_Query_Condition : public Expression, public Vectorized<Feature_Query_Condition*> {
class Supports_Condition : public Expression, public Vectorized<Supports_Condition*> {
public:
enum Operand { NONE, AND, OR, NOT };
private:
Expand All @@ -1492,9 +1485,9 @@ namespace Sass {
ADD_PROPERTY(Operand, operand)
ADD_PROPERTY(bool, is_root)
public:
Feature_Query_Condition(ParserState pstate, size_t s = 0, String* f = 0,
Supports_Condition(ParserState pstate, size_t s = 0, String* f = 0,
Expression* v = 0, Operand o = NONE, bool r = false)
: Expression(pstate), Vectorized<Feature_Query_Condition*>(s),
: Expression(pstate), Vectorized<Supports_Condition*>(s),
feature_(f), value_(v), operand_(o), is_root_(r)
{ }
ATTACH_OPERATIONS()
Expand Down Expand Up @@ -1567,7 +1560,7 @@ namespace Sass {
{
return expression()->exclude("rule");
}
if (s->statement_type() == Statement::FEATURE)
if (s->statement_type() == Statement::SUPPORTS)
{
return expression()->exclude("supports");
}
Expand Down Expand Up @@ -1947,7 +1940,6 @@ namespace Sass {
: Selector(pstate),
Vectorized<Simple_Selector*>(s)
{ }

Compound_Selector* unify_with(Compound_Selector* rhs, Context& ctx);
// virtual Selector_Placeholder* find_placeholder();
Simple_Selector* base()
Expand Down Expand Up @@ -2007,9 +1999,9 @@ namespace Sass {
ADD_PROPERTY(Complex_Selector*, tail)
public:
Complex_Selector(ParserState pstate,
Combinator c,
Compound_Selector* h,
Complex_Selector* t)
Combinator c = ANCESTOR_OF,
Compound_Selector* h = 0,
Complex_Selector* t = 0)
: Selector(pstate), combinator_(c), head_(h), tail_(t)
{
if ((h && h->has_reference()) || (t && t->has_reference())) has_reference(true);
Expand All @@ -2023,7 +2015,7 @@ namespace Sass {
bool is_superselector_of(Complex_Selector* sub);
bool is_superselector_of(Selector_List* sub);
// virtual Selector_Placeholder* find_placeholder();

Selector_List* unify_with(Complex_Selector* rhs, Context& ctx);

Combinator clear_innermost();
Expand Down Expand Up @@ -2093,7 +2085,6 @@ namespace Sass {
};

typedef deque<Complex_Selector*> ComplexSelectorDeque;

typedef Subset_Map<string, pair<Complex_Selector*, Compound_Selector*> > ExtensionSubsetMap;

///////////////////////////////////
Expand All @@ -2117,7 +2108,7 @@ namespace Sass {

Selector_List* unify_with(Selector_List*, Context&);
void populate_extends(Selector_List*, Context&, ExtensionSubsetMap&);

virtual unsigned long specificity()
{
unsigned long sum = 0;
Expand Down
4 changes: 2 additions & 2 deletions ast_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sass {
Block* new_Block(string p, size_t l, size_t s = 0, bool r = false);
Ruleset* new_Ruleset(string p, size_t l, Selector* s, Block* b);
Propset* new_Propset(string p, size_t l, String* pf, Block* b);
Feature_Query* new_Feature_Query(string p, size_t l, Feature_Query* f, Block* b);
Supports_Query* new_Supports_Query(string p, size_t l, Supports_Query* f, Block* b);
Media_Query* new_Media_Query(string p, size_t l, List* q, Block* b);
At_Root_Block* new_At_Root_Block(string p, size_t l, Selector* sel, Block* b);
At_Rule* new_At_Rule(string p, size_t l, string kwd, Selector* sel, Block* b);
Expand Down Expand Up @@ -67,7 +67,7 @@ namespace Sass {
String_Constant* new_String_Constant(string p, size_t l, string val);
String_Constant* new_String_Constant(string p, size_t l, const char* beg);
String_Constant* new_String_Constant(string p, size_t l, const char* beg, const char* end);
Feature_Query_Condition* new_Feature_Query_Condition(string p, size_t l, String* f, Expression* v);
Supports_Condition* new_Supports_Condition(string p, size_t l, String* f, Expression* v);
Media_Expression* new_Media_Expression(string p, size_t l, String* f, Expression* v);
Parent_Selector* new_Parent_Selector(string p, size_t l, Selector* s);
// parameters and arguments
Expand Down
6 changes: 3 additions & 3 deletions ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Sass {
class Propset;
class Bubble;
class Media_Block;
class Feature_Block;
class Supports_Block;
class At_Rule;
class Keyframe_Rule;
class At_Root_Block;
Expand Down Expand Up @@ -56,8 +56,8 @@ namespace Sass {
class String_Quoted;
class Media_Query;
class Media_Query_Expression;
class Feature_Query;
class Feature_Query_Condition;
class Supports_Query;
class Supports_Condition;
class At_Root_Expression;
class Null;
class Parent_Selector;
Expand Down
Loading

0 comments on commit 40ab322

Please sign in to comment.