Skip to content

Commit

Permalink
Preparations for directive refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Apr 23, 2016
1 parent 57fbfb9 commit d977f42
Show file tree
Hide file tree
Showing 28 changed files with 191 additions and 145 deletions.
4 changes: 2 additions & 2 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace Sass {
bool Compound_Selector::has_parent_ref()
{
for (Simple_Selector* s : *this) {
if (s->has_parent_ref()) return true;
if (s && s->has_parent_ref()) return true;
}
return false;
}
Expand Down Expand Up @@ -1293,7 +1293,7 @@ namespace Sass {
bool Selector_List::has_parent_ref()
{
for (Complex_Selector* s : *this) {
if (s->has_parent_ref()) return true;
if (s && s->has_parent_ref()) return true;
}
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ namespace Sass {
// At-rules -- arbitrary directives beginning with "@" that may have an
// optional statement block.
///////////////////////////////////////////////////////////////////////
class At_Rule : public Has_Block {
class Directive : public Has_Block {
ADD_PROPERTY(std::string, keyword)
ADD_PROPERTY(Selector*, selector)
ADD_PROPERTY(Expression*, value)
public:
At_Rule(ParserState pstate, std::string kwd, Selector* sel = 0, Block* b = 0, Expression* val = 0)
Directive(ParserState pstate, std::string kwd, Selector* sel = 0, Block* b = 0, Expression* val = 0)
: Has_Block(pstate, b), keyword_(kwd), selector_(sel), value_(val) // set value manually if needed
{ statement_type(DIRECTIVE); }
bool bubbles() { return is_keyframes() || is_media(); }
Expand Down Expand Up @@ -1696,13 +1696,13 @@ namespace Sass {
/////////////////////////////////////////////////
// At root expressions (for use inside @at-root).
/////////////////////////////////////////////////
class At_Root_Expression : public Expression {
class At_Root_Query : public Expression {
private:
ADD_PROPERTY(String*, feature)
ADD_PROPERTY(Expression*, value)
ADD_PROPERTY(bool, is_interpolated)
public:
At_Root_Expression(ParserState pstate, String* f = 0, Expression* v = 0, bool i = false)
At_Root_Query(ParserState pstate, String* f = 0, Expression* v = 0, bool i = false)
: Expression(pstate), feature_(f), value_(v), is_interpolated_(i)
{ }
bool exclude(std::string str)
Expand Down Expand Up @@ -1739,17 +1739,17 @@ namespace Sass {
// At-root.
///////////
class At_Root_Block : public Has_Block {
ADD_PROPERTY(At_Root_Expression*, expression)
ADD_PROPERTY(At_Root_Query*, expression)
public:
At_Root_Block(ParserState pstate, Block* b = 0, At_Root_Expression* e = 0)
At_Root_Block(ParserState pstate, Block* b = 0, At_Root_Query* e = 0)
: Has_Block(pstate, b), expression_(e)
{ statement_type(ATROOT); }
bool is_hoistable() { return true; }
bool bubbles() { return true; }
bool exclude_node(Statement* s) {
if (s->statement_type() == Statement::DIRECTIVE)
{
return expression()->exclude(static_cast<At_Rule*>(s)->keyword().erase(0, 1));
return expression()->exclude(static_cast<Directive*>(s)->keyword().erase(0, 1));
}
if (s->statement_type() == Statement::MEDIA)
{
Expand All @@ -1763,7 +1763,7 @@ namespace Sass {
{
return expression()->exclude("supports");
}
if (static_cast<At_Rule*>(s)->is_keyframes())
if (static_cast<Directive*>(s)->is_keyframes())
{
return expression()->exclude("keyframes");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Sass {
Supports_Query* new_Supports_Query(std::string p, size_t l, Supports_Query* f, Block* b);
Media_Query* new_Media_Query(std::string p, size_t l, List* q, Block* b);
At_Root_Block* new_At_Root_Block(std::string p, size_t l, Selector* sel, Block* b);
At_Rule* new_At_Rule(std::string p, size_t l, std::string kwd, Selector* sel, Block* b);
Directive* new_At_Rule(std::string p, size_t l, std::string kwd, Selector* sel, Block* b);
Keyframe_Rule* new_Keyframe_Rule(std::string p, size_t l, Block* b);
Declaration* new_Declaration(std::string p, size_t l, String* prop, List* vals);
Assignment* new_Assignment(std::string p, size_t l, std::string var, Expression* val, bool guarded = false);
Expand Down
4 changes: 2 additions & 2 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sass {
class Bubble;
class Media_Block;
class Supports_Block;
class At_Rule;
class Directive;
class Keyframe_Rule;
class At_Root_Block;
class Declaration;
Expand Down Expand Up @@ -62,7 +62,7 @@ namespace Sass {
class Supports_Negation;
class Supports_Declaration;
class Supports_Interpolation;
class At_Root_Expression;
class At_Root_Query;
class Null;
class Parent_Selector;
// parameters and arguments
Expand Down
26 changes: 18 additions & 8 deletions src/cssize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ namespace Sass {
: ctx(ctx),
block_stack(std::vector<Block*>()),
p_stack(std::vector<Statement*>()),
s_stack(std::vector<Selector_List*>()),
backtrace(bt)
{ }
{
s_stack.push_back(NULL);
}

Statement* Cssize::parent()
{
return p_stack.size() ? p_stack.back() : block_stack.front();
}

Selector_List* Cssize::selector()
{
return s_stack.size() ? s_stack.back() : NULL;
}

Statement* Cssize::operator()(Block* b)
{
Block* bb = SASS_MEMORY_NEW(ctx.mem, Block, b->pstate(), b->length(), b->is_root());
Expand All @@ -31,7 +39,7 @@ namespace Sass {
return bb;
}

Statement* Cssize::operator()(At_Rule* r)
Statement* Cssize::operator()(Directive* r)
{
if (!r->block() || !r->block()->length()) return r;

Expand All @@ -41,7 +49,7 @@ namespace Sass {
}

p_stack.push_back(r);
At_Rule* rr = SASS_MEMORY_NEW(ctx.mem, At_Rule,
Directive* rr = SASS_MEMORY_NEW(ctx.mem, Directive,
r->pstate(),
r->keyword(),
r->selector(),
Expand All @@ -57,15 +65,15 @@ namespace Sass {
else {
s = static_cast<Bubble*>(s)->node();
if (s->statement_type() != Statement::DIRECTIVE) directive_exists = false;
else directive_exists = (static_cast<At_Rule*>(s)->keyword() == rr->keyword());
else directive_exists = (static_cast<Directive*>(s)->keyword() == rr->keyword());
}

}

Block* result = SASS_MEMORY_NEW(ctx.mem, Block, rr->pstate());
if (!(directive_exists || rr->is_keyframes()))
{
At_Rule* empty_node = static_cast<At_Rule*>(rr);
Directive* empty_node = static_cast<Directive*>(rr);
empty_node->block(SASS_MEMORY_NEW(ctx.mem, Block, rr->block() ? rr->block()->pstate() : rr->pstate()));
*result << empty_node;
}
Expand Down Expand Up @@ -93,12 +101,14 @@ namespace Sass {
Statement* Cssize::operator()(Ruleset* r)
{
p_stack.push_back(r);
s_stack.push_back(dynamic_cast<Selector_List*>(r->selector()));
Ruleset* rr = SASS_MEMORY_NEW(ctx.mem, Ruleset,
r->pstate(),
r->selector(),
r->block()->perform(this)->block());
rr->is_root(r->is_root());
// rr->tabs(r->block()->tabs());
s_stack.pop_back();
p_stack.pop_back();

if (!rr->block()) {
Expand Down Expand Up @@ -214,7 +224,7 @@ namespace Sass {
return bubble(m);
}

Statement* Cssize::bubble(At_Rule* m)
Statement* Cssize::bubble(Directive* m)
{
Block* bb = SASS_MEMORY_NEW(ctx.mem, Block, this->parent()->pstate());
Has_Block* new_rule = static_cast<Has_Block*>(shallow_copy(this->parent()));
Expand All @@ -228,7 +238,7 @@ namespace Sass {

Block* wrapper_block = SASS_MEMORY_NEW(ctx.mem, Block, m->block() ? m->block()->pstate() : m->pstate());
*wrapper_block << new_rule;
At_Rule* mm = SASS_MEMORY_NEW(ctx.mem, At_Rule,
Directive* mm = SASS_MEMORY_NEW(ctx.mem, Directive,
m->pstate(),
m->keyword(),
m->selector(),
Expand Down Expand Up @@ -375,7 +385,7 @@ namespace Sass {
case Statement::BUBBLE:
return SASS_MEMORY_NEW(ctx.mem, Bubble, *static_cast<Bubble*>(s));
case Statement::DIRECTIVE:
return SASS_MEMORY_NEW(ctx.mem, At_Rule, *static_cast<At_Rule*>(s));
return SASS_MEMORY_NEW(ctx.mem, Directive, *static_cast<Directive*>(s));
case Statement::SUPPORTS:
return SASS_MEMORY_NEW(ctx.mem, Supports_Block, *static_cast<Supports_Block*>(s));
case Statement::ATROOT:
Expand Down
7 changes: 5 additions & 2 deletions src/cssize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Sass {
Context& ctx;
std::vector<Block*> block_stack;
std::vector<Statement*> p_stack;
std::vector<Selector_List*> s_stack;
Backtrace* backtrace;

Statement* fallback_impl(AST_Node* n);
Expand All @@ -24,14 +25,16 @@ namespace Sass {
Cssize(Context&, Backtrace*);
~Cssize() { }

Selector_List* selector();

Statement* operator()(Block*);
Statement* operator()(Ruleset*);
// Statement* operator()(Propset*);
// Statement* operator()(Bubble*);
Statement* operator()(Media_Block*);
Statement* operator()(Supports_Block*);
Statement* operator()(At_Root_Block*);
Statement* operator()(At_Rule*);
Statement* operator()(Directive*);
Statement* operator()(Keyframe_Rule*);
// Statement* operator()(Declaration*);
// Statement* operator()(Assignment*);
Expand All @@ -53,7 +56,7 @@ namespace Sass {

Statement* parent();
std::vector<std::pair<bool, Block*>> slice_by_bubble(Statement*);
Statement* bubble(At_Rule*);
Statement* bubble(Directive*);
Statement* bubble(At_Root_Block*);
Statement* bubble(Media_Block*);
Statement* bubble(Supports_Block*);
Expand Down
6 changes: 3 additions & 3 deletions src/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ inline void debug_ast(AST_Node* node, std::string ind, Env* env)
std::cerr << " " << has_block->tabs() << std::endl;
if (has_block->selector()) debug_ast(has_block->selector(), ind + "@");
if (has_block->block()) for(auto i : has_block->block()->elements()) { debug_ast(i, ind + " ", env); }
} else if (dynamic_cast<At_Rule*>(node)) {
At_Rule* block = dynamic_cast<At_Rule*>(node);
std::cerr << ind << "At_Rule " << block;
} else if (dynamic_cast<Directive*>(node)) {
Directive* block = dynamic_cast<Directive*>(node);
std::cerr << ind << "Directive " << block;
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " [" << block->keyword() << "] " << block->tabs() << std::endl;
debug_ast(block->selector(), ind + "~", env);
Expand Down
8 changes: 4 additions & 4 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1280,17 +1280,16 @@ namespace Sass {
return cc;
}

Expression* Eval::operator()(At_Root_Expression* e)
Expression* Eval::operator()(At_Root_Query* e)
{
Expression* feature = e->feature();
feature = (feature ? feature->perform(this) : 0);
Expression* value = e->value();
value = (value ? value->perform(this) : 0);
Expression* ee = SASS_MEMORY_NEW(ctx.mem, At_Root_Expression,
Expression* ee = SASS_MEMORY_NEW(ctx.mem, At_Root_Query,
e->pstate(),
static_cast<String*>(feature),
value,
e->is_interpolated());
value);
return ee;
}

Expand Down Expand Up @@ -1682,6 +1681,7 @@ namespace Sass {
{
std::vector<Selector_List*> rv;
Selector_List* sl = SASS_MEMORY_NEW(ctx.mem, Selector_List, s->pstate());
sl->is_optional(s->is_optional());
sl->media_block(s->media_block());
sl->is_optional(s->is_optional());
for (size_t i = 0, iL = s->length(); i < iL; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/eval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Sass {
// Expression* operator()(Selector_List*);
Expression* operator()(Media_Query*);
Expression* operator()(Media_Query_Expression*);
Expression* operator()(At_Root_Expression*);
Expression* operator()(At_Root_Query*);
Expression* operator()(Supports_Operator*);
Expression* operator()(Supports_Negation*);
Expression* operator()(Supports_Declaration*);
Expand Down
8 changes: 4 additions & 4 deletions src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ namespace Sass {
// if (ab) ab->is_root(true);
Expression* ae = a->expression();
if (ae) ae = ae->perform(&eval);
else ae = SASS_MEMORY_NEW(ctx.mem, At_Root_Expression, a->pstate());
else ae = SASS_MEMORY_NEW(ctx.mem, At_Root_Query, a->pstate());
Block* bb = ab ? ab->perform(this)->block() : 0;
At_Root_Block* aa = SASS_MEMORY_NEW(ctx.mem, At_Root_Block,
a->pstate(),
bb,
static_cast<At_Root_Expression*>(ae));
static_cast<At_Root_Query*>(ae));
// aa->block()->is_root(true);
return aa;
}

Statement* Expand::operator()(At_Rule* a)
Statement* Expand::operator()(Directive* a)
{
LOCAL_FLAG(in_keyframes, a->is_keyframes());
Block* ab = a->block();
Expand All @@ -243,7 +243,7 @@ namespace Sass {
if (as) as = dynamic_cast<Selector*>(as->perform(&eval));
selector_stack.pop_back();
Block* bb = ab ? ab->perform(this)->block() : 0;
At_Rule* aa = SASS_MEMORY_NEW(ctx.mem, At_Rule,
Directive* aa = SASS_MEMORY_NEW(ctx.mem, Directive,
a->pstate(),
a->keyword(),
as,
Expand Down
2 changes: 1 addition & 1 deletion src/expand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Sass {
Statement* operator()(Media_Block*);
Statement* operator()(Supports_Block*);
Statement* operator()(At_Root_Block*);
Statement* operator()(At_Rule*);
Statement* operator()(Directive*);
Statement* operator()(Declaration*);
Statement* operator()(Assignment*);
Statement* operator()(Import*);
Expand Down
2 changes: 1 addition & 1 deletion src/extend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ namespace Sass {
pMediaBlock->block()->perform(this);
}

void Extend::operator()(At_Rule* a)
void Extend::operator()(Directive* a)
{
// Selector_List* ls = dynamic_cast<Selector_List*>(a->selector());
// selector_stack.push_back(ls);
Expand Down
2 changes: 1 addition & 1 deletion src/extend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Sass {
void operator()(Ruleset*);
void operator()(Supports_Block*);
void operator()(Media_Block*);
void operator()(At_Rule*);
void operator()(Directive*);

template <typename U>
void fallback(U x) { return fallback_impl(x); }
Expand Down
2 changes: 1 addition & 1 deletion src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ namespace Sass {
}

// Cannot be a Universal selector
Type_Selector* pType = dynamic_cast<Type_Selector*>(base->head()->first());
Type_Selector* pType = dynamic_cast<Type_Selector*>(childSeq->head()->first());
if(pType && pType->name() == "*") {
std::string msg("Can't append `");
msg += childSeq->to_string();
Expand Down
Loading

0 comments on commit d977f42

Please sign in to comment.