Skip to content

Commit 83d401b

Browse files
committed
Refactor directive (i.e. At_Rule) parsing
1 parent d977f42 commit 83d401b

File tree

10 files changed

+704
-47
lines changed

10 files changed

+704
-47
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Miscellaneous stuff
22

3+
/sassc
4+
/sass-spec
5+
36
VERSION
47
.DS_Store
58
.sass-cache

src/ast.cpp

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
#include "color_maps.hpp"
88
#include <set>
99
#include <iomanip>
10-
#include <algorithm>
1110
#include <iostream>
11+
#include <algorithm>
12+
#include <functional>
13+
#include <cctype>
14+
#include <locale>
1215

1316
namespace Sass {
1417

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

31+
std::string & str_ltrim(std::string & str)
32+
{
33+
auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
34+
str.erase( str.begin() , it2);
35+
return str;
36+
}
37+
38+
std::string & str_rtrim(std::string & str)
39+
{
40+
auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
41+
str.erase( it1.base() , str.end() );
42+
return str;
43+
}
44+
45+
void String_Constant::rtrim()
46+
{
47+
value_ = str_rtrim(value_);
48+
}
49+
void String_Constant::ltrim()
50+
{
51+
value_ = str_ltrim(value_);
52+
}
53+
void String_Constant::trim()
54+
{
55+
rtrim();
56+
ltrim();
57+
}
58+
59+
void String_Schema::rtrim()
60+
{
61+
if (!empty()) {
62+
if (String* str = dynamic_cast<String*>(last())) str->rtrim();
63+
}
64+
}
65+
void String_Schema::ltrim()
66+
{
67+
if (!empty()) {
68+
if (String* str = dynamic_cast<String*>(first())) str->ltrim();
69+
}
70+
}
71+
void String_Schema::trim()
72+
{
73+
rtrim();
74+
ltrim();
75+
}
76+
77+
bool At_Root_Query::exclude(std::string str)
78+
{
79+
bool with = feature() && unquote(feature()->to_string()).compare("with") == 0;
80+
List* l = static_cast<List*>(value());
81+
std::string v;
82+
83+
if (with)
84+
{
85+
if (!l || l->length() == 0) return str.compare("rule") != 0;
86+
for (size_t i = 0, L = l->length(); i < L; ++i)
87+
{
88+
v = unquote((*l)[i]->to_string());
89+
if (v.compare("all") == 0 || v == str) return false;
90+
}
91+
return true;
92+
}
93+
else
94+
{
95+
if (!l || !l->length()) return str.compare("rule") == 0;
96+
for (size_t i = 0, L = l->length(); i < L; ++i)
97+
{
98+
v = unquote((*l)[i]->to_string());
99+
if (v.compare("all") == 0 || v == str) return true;
100+
}
101+
return false;
102+
}
103+
}
104+
28105
void AST_Node::update_pstate(const ParserState& pstate)
29106
{
30107
pstate_.offset += pstate - pstate_ + pstate.offset;

src/ast.hpp

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,9 @@ namespace Sass {
14711471
{ concrete_type(STRING); }
14721472
static std::string type_name() { return "string"; }
14731473
virtual ~String() = 0;
1474+
virtual void rtrim() = 0;
1475+
virtual void ltrim() = 0;
1476+
virtual void trim() = 0;
14741477
virtual bool operator==(const Expression& rhs) const = 0;
14751478
ATTACH_OPERATIONS()
14761479
};
@@ -1499,6 +1502,9 @@ namespace Sass {
14991502
}
15001503
return false;
15011504
}
1505+
virtual void rtrim();
1506+
virtual void ltrim();
1507+
virtual void trim();
15021508

15031509
virtual size_t hash()
15041510
{
@@ -1539,6 +1545,9 @@ namespace Sass {
15391545
std::string type() { return "string"; }
15401546
static std::string type_name() { return "string"; }
15411547
virtual bool is_invisible() const;
1548+
virtual void rtrim();
1549+
virtual void ltrim();
1550+
virtual void trim();
15421551

15431552
virtual size_t hash()
15441553
{
@@ -1698,40 +1707,13 @@ namespace Sass {
16981707
/////////////////////////////////////////////////
16991708
class At_Root_Query : public Expression {
17001709
private:
1701-
ADD_PROPERTY(String*, feature)
1710+
ADD_PROPERTY(Expression*, feature)
17021711
ADD_PROPERTY(Expression*, value)
1703-
ADD_PROPERTY(bool, is_interpolated)
17041712
public:
1705-
At_Root_Query(ParserState pstate, String* f = 0, Expression* v = 0, bool i = false)
1706-
: Expression(pstate), feature_(f), value_(v), is_interpolated_(i)
1713+
At_Root_Query(ParserState pstate, Expression* f = 0, Expression* v = 0, bool i = false)
1714+
: Expression(pstate), feature_(f), value_(v)
17071715
{ }
1708-
bool exclude(std::string str)
1709-
{
1710-
bool with = feature() && unquote(feature()->to_string()).compare("with") == 0;
1711-
List* l = static_cast<List*>(value());
1712-
std::string v;
1713-
1714-
if (with)
1715-
{
1716-
if (!l || l->length() == 0) return str.compare("rule") != 0;
1717-
for (size_t i = 0, L = l->length(); i < L; ++i)
1718-
{
1719-
v = unquote((*l)[i]->to_string());
1720-
if (v.compare("all") == 0 || v == str) return false;
1721-
}
1722-
return true;
1723-
}
1724-
else
1725-
{
1726-
if (!l || !l->length()) return str.compare("rule") == 0;
1727-
for (size_t i = 0, L = l->length(); i < L; ++i)
1728-
{
1729-
v = unquote((*l)[i]->to_string());
1730-
if (v.compare("all") == 0 || v == str) return true;
1731-
}
1732-
return false;
1733-
}
1734-
}
1716+
bool exclude(std::string str);
17351717
ATTACH_OPERATIONS()
17361718
};
17371719

src/constants.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ namespace Sass {
8989
extern const char expression_kwd[] = "expression";
9090
extern const char calc_fn_kwd[] = "calc";
9191

92+
extern const char almost_any_value_class[] = "\"'#!;{}";
93+
9294
// css selector keywords
9395
extern const char sel_deep_kwd[] = "/deep/";
9496

src/constants.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ namespace Sass {
8989
extern const char expression_kwd[];
9090
extern const char calc_fn_kwd[];
9191

92+
// char classes for "regular expressions"
93+
extern const char almost_any_value_class[];
94+
9295
// css selector keywords
9396
extern const char sel_deep_kwd[];
9497

src/eval.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,10 @@ namespace Sass {
11861186
if (!dynamic_cast<String_Quoted*>((*s)[0]) && !dynamic_cast<String_Quoted*>((*s)[L - 1])) {
11871187
if (String_Constant* l = dynamic_cast<String_Constant*>((*s)[0])) {
11881188
if (String_Constant* r = dynamic_cast<String_Constant*>((*s)[L - 1])) {
1189-
if (l->value()[0] == '"' && r->value()[r->value().size() - 1] == '"') into_quotes = true;
1190-
if (l->value()[0] == '\'' && r->value()[r->value().size() - 1] == '\'') into_quotes = true;
1189+
if (r->value().size() > 0) {
1190+
if (l->value()[0] == '"' && r->value()[r->value().size() - 1] == '"') into_quotes = true;
1191+
if (l->value()[0] == '\'' && r->value()[r->value().size() - 1] == '\'') into_quotes = true;
1192+
}
11911193
}
11921194
}
11931195
}

0 commit comments

Comments
 (0)