Skip to content

Commit

Permalink
Refactor directive (i.e. At_Rule) parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Apr 22, 2016
1 parent d364580 commit c4f4ef3
Show file tree
Hide file tree
Showing 10 changed files with 704 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Miscellaneous stuff

/sassc
/sass-spec

VERSION
.DS_Store
.sass-cache
Expand Down
79 changes: 78 additions & 1 deletion src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#include "color_maps.hpp"
#include <set>
#include <iomanip>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>

namespace Sass {

Expand All @@ -25,6 +28,80 @@ namespace Sass {
dynamic_cast<Supports_Operator*>(cond);
}

std::string & str_ltrim(std::string & str)
{
auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
str.erase( str.begin() , it2);
return str;
}

std::string & str_rtrim(std::string & str)
{
auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
str.erase( it1.base() , str.end() );
return str;
}

void String_Constant::rtrim()
{
value_ = str_rtrim(value_);
}
void String_Constant::ltrim()
{
value_ = str_ltrim(value_);
}
void String_Constant::trim()
{
rtrim();
ltrim();
}

void String_Schema::rtrim()
{
if (!empty()) {
if (String* str = dynamic_cast<String*>(last())) str->rtrim();
}
}
void String_Schema::ltrim()
{
if (!empty()) {
if (String* str = dynamic_cast<String*>(first())) str->ltrim();
}
}
void String_Schema::trim()
{
rtrim();
ltrim();
}

bool At_Root_Query::exclude(std::string str)
{
bool with = feature() && unquote(feature()->to_string()).compare("with") == 0;
List* l = static_cast<List*>(value());
std::string v;

if (with)
{
if (!l || l->length() == 0) return str.compare("rule") != 0;
for (size_t i = 0, L = l->length(); i < L; ++i)
{
v = unquote((*l)[i]->to_string());
if (v.compare("all") == 0 || v == str) return false;
}
return true;
}
else
{
if (!l || !l->length()) return str.compare("rule") == 0;
for (size_t i = 0, L = l->length(); i < L; ++i)
{
v = unquote((*l)[i]->to_string());
if (v.compare("all") == 0 || v == str) return true;
}
return false;
}
}

void AST_Node::update_pstate(const ParserState& pstate)
{
pstate_.offset += pstate - pstate_ + pstate.offset;
Expand Down
44 changes: 13 additions & 31 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,9 @@ namespace Sass {
{ concrete_type(STRING); }
static std::string type_name() { return "string"; }
virtual ~String() = 0;
virtual void rtrim() = 0;
virtual void ltrim() = 0;
virtual void trim() = 0;
virtual bool operator==(const Expression& rhs) const = 0;
ATTACH_OPERATIONS()
};
Expand Down Expand Up @@ -1499,6 +1502,9 @@ namespace Sass {
}
return false;
}
virtual void rtrim();
virtual void ltrim();
virtual void trim();

virtual size_t hash()
{
Expand Down Expand Up @@ -1539,6 +1545,9 @@ namespace Sass {
std::string type() { return "string"; }
static std::string type_name() { return "string"; }
virtual bool is_invisible() const;
virtual void rtrim();
virtual void ltrim();
virtual void trim();

virtual size_t hash()
{
Expand Down Expand Up @@ -1698,40 +1707,13 @@ namespace Sass {
/////////////////////////////////////////////////
class At_Root_Query : public Expression {
private:
ADD_PROPERTY(String*, feature)
ADD_PROPERTY(Expression*, feature)
ADD_PROPERTY(Expression*, value)
ADD_PROPERTY(bool, is_interpolated)
public:
At_Root_Query(ParserState pstate, String* f = 0, Expression* v = 0, bool i = false)
: Expression(pstate), feature_(f), value_(v), is_interpolated_(i)
At_Root_Query(ParserState pstate, Expression* f = 0, Expression* v = 0, bool i = false)
: Expression(pstate), feature_(f), value_(v)
{ }
bool exclude(std::string str)
{
bool with = feature() && unquote(feature()->to_string()).compare("with") == 0;
List* l = static_cast<List*>(value());
std::string v;

if (with)
{
if (!l || l->length() == 0) return str.compare("rule") != 0;
for (size_t i = 0, L = l->length(); i < L; ++i)
{
v = unquote((*l)[i]->to_string());
if (v.compare("all") == 0 || v == str) return false;
}
return true;
}
else
{
if (!l || !l->length()) return str.compare("rule") == 0;
for (size_t i = 0, L = l->length(); i < L; ++i)
{
v = unquote((*l)[i]->to_string());
if (v.compare("all") == 0 || v == str) return true;
}
return false;
}
}
bool exclude(std::string str);
ATTACH_OPERATIONS()
};

Expand Down
2 changes: 2 additions & 0 deletions src/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ namespace Sass {
extern const char expression_kwd[] = "expression";
extern const char calc_fn_kwd[] = "calc";

extern const char almost_any_value_class[] = "\"'#!;{}";

// css selector keywords
extern const char sel_deep_kwd[] = "/deep/";

Expand Down
3 changes: 3 additions & 0 deletions src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ namespace Sass {
extern const char expression_kwd[];
extern const char calc_fn_kwd[];

// char classes for "regular expressions"
extern const char almost_any_value_class[];

// css selector keywords
extern const char sel_deep_kwd[];

Expand Down
6 changes: 4 additions & 2 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,10 @@ namespace Sass {
if (!dynamic_cast<String_Quoted*>((*s)[0]) && !dynamic_cast<String_Quoted*>((*s)[L - 1])) {
if (String_Constant* l = dynamic_cast<String_Constant*>((*s)[0])) {
if (String_Constant* r = dynamic_cast<String_Constant*>((*s)[L - 1])) {
if (l->value()[0] == '"' && r->value()[r->value().size() - 1] == '"') into_quotes = true;
if (l->value()[0] == '\'' && r->value()[r->value().size() - 1] == '\'') into_quotes = true;
if (r->value().size() > 0) {
if (l->value()[0] == '"' && r->value()[r->value().size() - 1] == '"') into_quotes = true;
if (l->value()[0] == '\'' && r->value()[r->value().size() - 1] == '\'') into_quotes = true;
}
}
}
}
Expand Down
Loading

0 comments on commit c4f4ef3

Please sign in to comment.