forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error when declarations are printed without ruleset
This is introduces a vastly reduced `CheckNesting` visitor from Ruby Sass as discussed in sass#2061. Doing this properly will require some small changes to the parser, and probably eval, which currently try to do some nesting checking of their own. This is just first step to properly introducing the proper nesting checking. Closes sass#2061 Fixes sass#1732
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "sass.hpp" | ||
#include <vector> | ||
|
||
#include "check_nesting.hpp" | ||
#include "context.hpp" | ||
// #include "debugger.hpp" | ||
|
||
namespace Sass { | ||
|
||
CheckNesting::CheckNesting(Context& ctx) | ||
: ctx(ctx), | ||
parent_stack(std::vector<AST_Node*>()) | ||
{ } | ||
|
||
AST_Node* CheckNesting::parent() | ||
{ | ||
if (parent_stack.size() > 0) | ||
return parent_stack.back(); | ||
return 0; | ||
} | ||
|
||
Statement* CheckNesting::operator()(Block* b) | ||
{ | ||
parent_stack.push_back(b); | ||
append_block(b); | ||
parent_stack.pop_back(); | ||
return b; | ||
} | ||
|
||
Statement* CheckNesting::operator()(Declaration* d) | ||
{ | ||
if (!is_valid_prop_parent(parent())) { | ||
throw Exception::InvalidSass(d->pstate(), "Properties are only allowed " | ||
"within rules, directives, mixin includes, or other properties."); | ||
} | ||
return static_cast<Statement*>(d); | ||
} | ||
|
||
Statement* CheckNesting::fallback_impl(AST_Node* n) | ||
{ | ||
return static_cast<Statement*>(n); | ||
} | ||
|
||
bool CheckNesting::is_valid_prop_parent(AST_Node* p) | ||
{ | ||
if (Definition* def = dynamic_cast<Definition*>(p)) { | ||
return def->type() == Definition::MIXIN; | ||
} | ||
|
||
return dynamic_cast<Ruleset*>(p) || | ||
dynamic_cast<Keyframe_Rule*>(p) || | ||
dynamic_cast<Propset*>(p) || | ||
dynamic_cast<Directive*>(p) || | ||
dynamic_cast<Mixin_Call*>(p); | ||
} | ||
|
||
void CheckNesting::append_block(Block* b) | ||
{ | ||
for (size_t i = 0, L = b->length(); i < L; ++i) { | ||
Statement* ith = (*b)[i]->perform(this); | ||
if (ith) { | ||
(*b)[i] = ith; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef SASS_CHECK_NESTING_H | ||
#define SASS_CHECK_NESTING_H | ||
|
||
#include "ast.hpp" | ||
#include "context.hpp" | ||
#include "operation.hpp" | ||
|
||
namespace Sass { | ||
|
||
typedef Environment<AST_Node*> Env; | ||
|
||
class CheckNesting : public Operation_CRTP<Statement*, CheckNesting> { | ||
|
||
Context& ctx; | ||
std::vector<Block*> block_stack; | ||
std::vector<AST_Node*> parent_stack; | ||
|
||
AST_Node* parent(); | ||
|
||
Statement* fallback_impl(AST_Node* n); | ||
|
||
public: | ||
CheckNesting(Context&); | ||
~CheckNesting() { } | ||
|
||
Statement* operator()(Block*); | ||
Statement* operator()(Declaration*); | ||
|
||
template <typename U> | ||
Statement* fallback(U x) { return fallback_impl(x); } | ||
|
||
bool is_valid_prop_parent(AST_Node*); | ||
void append_block(Block*); | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters