Skip to content

Commit 4c4af04

Browse files
authored
Merge pull request #2338 from mgreter/perf-refactor/remove-textual
Remove Textual intermediate AST node
2 parents 58fa66b + aabd8b9 commit 4c4af04

File tree

11 files changed

+134
-177
lines changed

11 files changed

+134
-177
lines changed

src/ast.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,8 @@ namespace Sass {
17901790

17911791
void Number::normalize(const std::string& prefered, bool strict)
17921792
{
1793+
// no conversion if unit is empty
1794+
if (prefered.empty() && numerator_units_.size() == 0 && denominator_units_.size() == 0) return;
17931795

17941796
// first make sure same units cancel each other out
17951797
// it seems that a map table will fit nicely to do this
@@ -2428,7 +2430,6 @@ namespace Sass {
24282430
IMPLEMENT_AST_OPERATORS(Function_Call_Schema);
24292431
IMPLEMENT_AST_OPERATORS(Block);
24302432
IMPLEMENT_AST_OPERATORS(Content);
2431-
IMPLEMENT_AST_OPERATORS(Textual);
24322433
IMPLEMENT_AST_OPERATORS(Trace);
24332434
IMPLEMENT_AST_OPERATORS(Keyframe_Rule);
24342435
IMPLEMENT_AST_OPERATORS(Bubble);

src/ast.hpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,56 +1540,6 @@ namespace Sass {
15401540
ATTACH_OPERATIONS()
15411541
};
15421542

1543-
////////////////////////////////////////////////////////////////////////////
1544-
// Textual (i.e., unevaluated) numeric data. Variants are distinguished with
1545-
// a type tag.
1546-
////////////////////////////////////////////////////////////////////////////
1547-
class Textual : public Expression {
1548-
public:
1549-
enum Type { NUMBER, PERCENTAGE, DIMENSION, HEX };
1550-
private:
1551-
HASH_PROPERTY(Type, valtype)
1552-
HASH_CONSTREF(std::string, value)
1553-
size_t hash_;
1554-
public:
1555-
Textual(ParserState pstate, Type t, std::string val)
1556-
: Expression(pstate, DELAYED), valtype_(t), value_(val),
1557-
hash_(0)
1558-
{ }
1559-
Textual(const Textual* ptr)
1560-
: Expression(ptr),
1561-
valtype_(ptr->valtype_),
1562-
value_(ptr->value_),
1563-
hash_(ptr->hash_)
1564-
{ }
1565-
1566-
virtual bool operator==(const Expression& rhs) const
1567-
{
1568-
try
1569-
{
1570-
Textual_Ptr_Const e = Cast<Textual>(&rhs);
1571-
return e && value() == e->value() && type() == e->type();
1572-
}
1573-
catch (std::bad_cast&)
1574-
{
1575-
return false;
1576-
}
1577-
catch (...) { throw; }
1578-
}
1579-
1580-
virtual size_t hash()
1581-
{
1582-
if (hash_ == 0) {
1583-
hash_ = std::hash<std::string>()(value_);
1584-
hash_combine(hash_, std::hash<int>()(valtype_));
1585-
}
1586-
return hash_;
1587-
}
1588-
1589-
ATTACH_AST_OPERATIONS(Textual)
1590-
ATTACH_OPERATIONS()
1591-
};
1592-
15931543
////////////////////////////////////////////////
15941544
// Numbers, percentages, dimensions, and colors.
15951545
////////////////////////////////////////////////

src/ast_fwd_decl.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ namespace Sass {
158158
class Variable;
159159
typedef Variable* Variable_Ptr;
160160
typedef Variable const* Variable_Ptr_Const;
161-
class Textual;
162-
typedef Textual* Textual_Ptr;
163-
typedef Textual const* Textual_Ptr_Const;
164161
class Number;
165162
typedef Number* Number_Ptr;
166163
typedef Number const* Number_Ptr_Const;
@@ -321,7 +318,6 @@ namespace Sass {
321318
IMPL_MEM_OBJ(Custom_Warning);
322319
IMPL_MEM_OBJ(Custom_Error);
323320
IMPL_MEM_OBJ(Variable);
324-
IMPL_MEM_OBJ(Textual);
325321
IMPL_MEM_OBJ(Number);
326322
IMPL_MEM_OBJ(Color);
327323
IMPL_MEM_OBJ(Boolean);

src/debugger.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -488,18 +488,6 @@ inline void debug_ast(AST_Node_Ptr node, std::string ind, Env* env)
488488
std::cerr << (block->is_invisible() ? " [INVISIBLE]" : "");
489489
std::cerr << " [indent: " << block->tabs() << "]" << std::endl;
490490
for(const Statement_Obj& i : block->elements()) { debug_ast(i, ind + " ", env); }
491-
} else if (Cast<Textual>(node)) {
492-
Textual_Ptr expression = Cast<Textual>(node);
493-
std::cerr << ind << "Textual " << expression;
494-
std::cerr << " (" << pstate_source_position(node) << ")";
495-
if (expression->valtype() == Textual::NUMBER) std::cerr << " [NUMBER]";
496-
else if (expression->valtype() == Textual::PERCENTAGE) std::cerr << " [PERCENTAGE]";
497-
else if (expression->valtype() == Textual::DIMENSION) std::cerr << " [DIMENSION]";
498-
else if (expression->valtype() == Textual::HEX) std::cerr << " [HEX]";
499-
std::cerr << " [" << expression->value() << "]";
500-
std::cerr << " [interpolant: " << expression->is_interpolant() << "] ";
501-
if (expression->is_delayed()) std::cerr << " [delayed]";
502-
std::cerr << std::endl;
503491
} else if (Cast<Variable>(node)) {
504492
Variable_Ptr expression = Cast<Variable>(node);
505493
std::cerr << ind << "Variable " << expression;

src/eval.cpp

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -641,17 +641,15 @@ namespace Sass {
641641
std::string value(str->value());
642642
const char* start = value.c_str();
643643
if (Prelexer::sequence < Prelexer::dimension, Prelexer::end_of_file >(start) != 0) {
644-
Textual_Obj l = SASS_MEMORY_NEW(Textual, b->pstate(), Textual::DIMENSION, str->value());
645-
lhs = l->perform(this);
644+
lhs = Parser::lexed_dimension(b->pstate(), str->value());
646645
}
647646
}
648647
// If possible upgrade RHS to a number (for string to number compare)
649648
if (String_Constant_Ptr str = Cast<String_Constant>(rhs)) {
650649
std::string value(str->value());
651650
const char* start = value.c_str();
652651
if (Prelexer::sequence < Prelexer::dimension, Prelexer::number >(start) != 0) {
653-
Textual_Obj r = SASS_MEMORY_NEW(Textual, b->pstate(), Textual::DIMENSION, str->value());
654-
rhs = r->perform(this);
652+
rhs = Parser::lexed_dimension(b->pstate(), str->value());
655653
}
656654
}
657655
}
@@ -660,16 +658,6 @@ namespace Sass {
660658
Value_Obj v_l = Cast<Value>(lhs->perform(&to_value));
661659
Value_Obj v_r = Cast<Value>(rhs->perform(&to_value));
662660

663-
if (s2 && s2->has_interpolants() && s2->length()) {
664-
Textual_Obj front = Cast<Textual>(s2->elements().front());
665-
if (front && !front->is_interpolant())
666-
{
667-
// XXX: this is never hit via spec tests
668-
schema_op = true;
669-
rhs = front->perform(this);
670-
}
671-
}
672-
673661
if (force_delay) {
674662
std::string str("");
675663
str += v_l->to_string(ctx.c_options);
@@ -1025,81 +1013,6 @@ namespace Sass {
10251013
return value.detach();
10261014
}
10271015

1028-
Expression_Ptr Eval::operator()(Textual_Ptr t)
1029-
{
1030-
using Prelexer::number;
1031-
Expression_Obj result = 0;
1032-
size_t L = t->value().length();
1033-
bool zero = !( (L > 0 && t->value().substr(0, 1) == ".") ||
1034-
(L > 1 && t->value().substr(0, 2) == "0.") ||
1035-
(L > 1 && t->value().substr(0, 2) == "-.") ||
1036-
(L > 2 && t->value().substr(0, 3) == "-0.")
1037-
);
1038-
1039-
const std::string& text = t->value();
1040-
size_t num_pos = text.find_first_not_of(" \n\r\t");
1041-
if (num_pos == std::string::npos) num_pos = text.length();
1042-
size_t unit_pos = text.find_first_not_of("-+0123456789.", num_pos);
1043-
if (unit_pos == std::string::npos) unit_pos = text.length();
1044-
const std::string& num = text.substr(num_pos, unit_pos - num_pos);
1045-
1046-
switch (t->valtype())
1047-
{
1048-
case Textual::NUMBER:
1049-
result = SASS_MEMORY_NEW(Number,
1050-
t->pstate(),
1051-
sass_atof(num.c_str()),
1052-
"",
1053-
zero);
1054-
break;
1055-
case Textual::PERCENTAGE:
1056-
result = SASS_MEMORY_NEW(Number,
1057-
t->pstate(),
1058-
sass_atof(num.c_str()),
1059-
"%",
1060-
true);
1061-
break;
1062-
case Textual::DIMENSION:
1063-
result = SASS_MEMORY_NEW(Number,
1064-
t->pstate(),
1065-
sass_atof(num.c_str()),
1066-
Token(number(text.c_str())),
1067-
zero);
1068-
break;
1069-
case Textual::HEX: {
1070-
if (t->value().substr(0, 1) != "#") {
1071-
result = SASS_MEMORY_NEW(String_Quoted, t->pstate(), t->value());
1072-
break;
1073-
}
1074-
std::string hext(t->value().substr(1)); // chop off the '#'
1075-
if (hext.length() == 6) {
1076-
std::string r(hext.substr(0,2));
1077-
std::string g(hext.substr(2,2));
1078-
std::string b(hext.substr(4,2));
1079-
result = SASS_MEMORY_NEW(Color,
1080-
t->pstate(),
1081-
static_cast<double>(strtol(r.c_str(), NULL, 16)),
1082-
static_cast<double>(strtol(g.c_str(), NULL, 16)),
1083-
static_cast<double>(strtol(b.c_str(), NULL, 16)),
1084-
1, // alpha channel
1085-
t->value());
1086-
}
1087-
else {
1088-
result = SASS_MEMORY_NEW(Color,
1089-
t->pstate(),
1090-
static_cast<double>(strtol(std::string(2,hext[0]).c_str(), NULL, 16)),
1091-
static_cast<double>(strtol(std::string(2,hext[1]).c_str(), NULL, 16)),
1092-
static_cast<double>(strtol(std::string(2,hext[2]).c_str(), NULL, 16)),
1093-
1, // alpha channel
1094-
t->value());
1095-
}
1096-
} break;
1097-
default: break;
1098-
}
1099-
result->is_interpolant(t->is_interpolant());
1100-
return result.detach();
1101-
}
1102-
11031016
Expression_Ptr Eval::operator()(Color_Ptr c)
11041017
{
11051018
return c;
@@ -1177,7 +1090,6 @@ namespace Sass {
11771090
}
11781091

11791092
// Value
1180-
// Textual
11811093
// Function_Call
11821094
// Selector_List
11831095
// String_Quoted

src/eval.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ namespace Sass {
4949
Expression_Ptr operator()(Function_Call_Ptr);
5050
Expression_Ptr operator()(Function_Call_Schema_Ptr);
5151
Expression_Ptr operator()(Variable_Ptr);
52-
Expression_Ptr operator()(Textual_Ptr);
5352
Expression_Ptr operator()(Number_Ptr);
5453
Expression_Ptr operator()(Color_Ptr);
5554
Expression_Ptr operator()(Boolean_Ptr);

src/inspect.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,6 @@ namespace Sass {
519519
append_token(var->name(), var);
520520
}
521521

522-
void Inspect::operator()(Textual_Ptr txt)
523-
{
524-
append_token(txt->value(), txt);
525-
}
526-
527522
void Inspect::operator()(Number_Ptr n)
528523
{
529524

src/inspect.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ namespace Sass {
5656
// virtual void operator()(Custom_Warning_Ptr);
5757
// virtual void operator()(Custom_Error_Ptr);
5858
virtual void operator()(Variable_Ptr);
59-
virtual void operator()(Textual_Ptr);
6059
virtual void operator()(Number_Ptr);
6160
virtual void operator()(Color_Ptr);
6261
virtual void operator()(Boolean_Ptr);

src/operation.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ namespace Sass {
4747
virtual T operator()(Custom_Warning_Ptr x) = 0;
4848
virtual T operator()(Custom_Error_Ptr x) = 0;
4949
virtual T operator()(Variable_Ptr x) = 0;
50-
virtual T operator()(Textual_Ptr x) = 0;
5150
virtual T operator()(Number_Ptr x) = 0;
5251
virtual T operator()(Color_Ptr x) = 0;
5352
virtual T operator()(Boolean_Ptr x) = 0;
@@ -129,7 +128,6 @@ namespace Sass {
129128
T operator()(Custom_Warning_Ptr x) { return static_cast<D*>(this)->fallback(x); }
130129
T operator()(Custom_Error_Ptr x) { return static_cast<D*>(this)->fallback(x); }
131130
T operator()(Variable_Ptr x) { return static_cast<D*>(this)->fallback(x); }
132-
T operator()(Textual_Ptr x) { return static_cast<D*>(this)->fallback(x); }
133131
T operator()(Number_Ptr x) { return static_cast<D*>(this)->fallback(x); }
134132
T operator()(Color_Ptr x) { return static_cast<D*>(this)->fallback(x); }
135133
T operator()(Boolean_Ptr x) { return static_cast<D*>(this)->fallback(x); }

0 commit comments

Comments
 (0)