Skip to content

Commit

Permalink
Trace String_Quoted/Constant lifecycle
Browse files Browse the repository at this point in the history
Check results of dynamic casting between
String_Quoted and String_Constant
  • Loading branch information
saper committed Jun 21, 2015
1 parent 7c38282 commit 811c559
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
20 changes: 16 additions & 4 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#endif

#include "debug.hpp"
#include "util.hpp"
#include "units.hpp"
#include "context.hpp"
Expand Down Expand Up @@ -1367,16 +1368,16 @@ namespace Sass {
public:
String_Constant(ParserState pstate, string val)
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(val)), hash_(0)
{ }
{ TRACEINST(this) << "String_Constant created " << this; }
String_Constant(ParserState pstate, const char* beg)
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg))), hash_(0)
{ }
{ TRACEINST(this) << "String_Constant created " << this; }
String_Constant(ParserState pstate, const char* beg, const char* end)
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg, end-beg))), hash_(0)
{ }
{ TRACEINST(this) << "String_Constant created " << this; }
String_Constant(ParserState pstate, const Token& tok)
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(tok.begin, tok.end))), hash_(0)
{ }
{ TRACEINST(this) << "String_Constant created " << this; }
string type() { return "string"; }
static string type_name() { return "string"; }

Expand Down Expand Up @@ -1404,6 +1405,16 @@ namespace Sass {
static char double_quote() { return '"'; }
static char single_quote() { return '\''; }

friend std::ostream& operator << (std::ostream& os, String_Constant& sq) {
os << "(" << (sq.quote_mark_ ? sq.quote_mark_ : '0') << ",\"" << sq.value_ << "\")";
return os;
}

friend std::ostream& operator << (std::ostream& os, String_Constant* sq) {
os << "(" << (sq->quote_mark_ ? sq->quote_mark_ : '0') << ",\"" << sq->value_ << "\")";
return os;
}

ATTACH_OPERATIONS();
};

Expand All @@ -1416,6 +1427,7 @@ namespace Sass {
: String_Constant(pstate, val)
{
value_ = unquote(value_, &quote_mark_);
TRACEINST(this) << "String_Quoted created " << this;
}
ATTACH_OPERATIONS();
};
Expand Down
21 changes: 20 additions & 1 deletion eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,14 @@ namespace Sass {
}
else if (value->concrete_type() == Expression::STRING) {
if (auto str = dynamic_cast<String_Quoted*>(value)) {
TRACEINST(str) << "dynamic_cast to String_Quoted worked " << str;
value = new (ctx.mem) String_Quoted(*str);
} else if (auto str = dynamic_cast<String_Constant*>(value)) {
if (str->quote_mark()) {
TRACEINST(str) << "dynamic_cast to String_Quoted did not work, but we have quote " << str;
value = new (ctx.mem) String_Quoted(str->pstate(), str->perform(&to_string));
} else {
TRACEINST(str) << "dynamic_cast to String_Quoted did not work, we are String_Constant " << str;
value = new (ctx.mem) String_Constant(str->pstate(), unquote(str->value()));
}
}
Expand Down Expand Up @@ -856,18 +859,26 @@ namespace Sass {

string Eval::interpolation(Expression* s) {
if (String_Quoted* str_quoted = dynamic_cast<String_Quoted*>(s)) {
TRACEINST(str_quoted) << "dynamic_cast to String_Quoted worked " << str_quoted;
if (str_quoted->quote_mark()) {
if (str_quoted->quote_mark() == '*' || str_quoted->is_delayed()) {
TRACEINST(str_quoted) << "... will do interpolation()";
return interpolation(new (ctx.mem) String_Constant(*str_quoted));
} else {
TRACEINST(str_quoted) << "... will string_escape()";
return string_escape(quote(str_quoted->value(), str_quoted->quote_mark()));
}
} else {
TRACEINST(str_quoted) << "dynamic_cast to String_Quoted failed, will evacuate_escapes()";
return evacuate_escapes(str_quoted->value());
}
} else if (String_Constant* str_constant = dynamic_cast<String_Constant*>(s)) {
TRACEINST(str_constant) << "dynamic_cast to String_Constant worked";
string str = str_constant->value();
if (!str_constant->quote_mark()) str = unquote(str);
if (!str_constant->quote_mark()) {
TRACEINST(str_constant) << "... but still need to unquote()";
str = unquote(str);
}
return evacuate_escapes(str);
} else if (String_Schema* str_schema = dynamic_cast<String_Schema*>(s)) {
string res = "";
Expand Down Expand Up @@ -1011,14 +1022,22 @@ namespace Sass {
Expression* feature = e->feature();
feature = (feature ? feature->perform(this) : 0);
if (feature && dynamic_cast<String_Quoted*>(feature)) {
String_Quoted *qfeature = dynamic_cast<String_Quoted*>(feature);
TRACEINST(qfeature) << "dynamic_cast to String_Quoted worked " << qfeature;
feature = new (ctx.mem) String_Constant(feature->pstate(),
dynamic_cast<String_Quoted*>(feature)->value());
} else {
TRACEINST(feature) << "dynamic_cast to String_Quoted did not work for " << feature;
}
Expression* value = e->value();
value = (value ? value->perform(this) : 0);
if (value && dynamic_cast<String_Quoted*>(value)) {
String_Quoted *qvalue = dynamic_cast<String_Quoted*>(value);
TRACEINST(qvalue) << "dynamic_cast to String_Quoted worked " << qvalue;
value = new (ctx.mem) String_Constant(value->pstate(),
dynamic_cast<String_Quoted*>(value)->value());
} else {
TRACEINST(value) << "dynamic_cast to String_Quoted did not work for " << value;
}
return new (ctx.mem) Media_Query_Expression(e->pstate(),
feature,
Expand Down
8 changes: 8 additions & 0 deletions output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,27 +372,35 @@ namespace Sass {

void Output::operator()(String_Quoted* s)
{
TRACEINST(s) << "This should be a quoted string... " << s;
if (s->quote_mark()) {
TRACEINST(s) << "... it even has a quote mark property, sending with quote marks";
append_token(quote(s->value(), s->quote_mark()), s);
} else if (!in_comment) {
TRACEINST(s) << "... no quote mark(?), sending via string_to_output";
append_token(string_to_output(s->value()), s);
} else {
TRACEINST(s) << "... no quote mark(?), sending directly (in comment)";
append_token(s->value(), s);
}
}

void Output::operator()(String_Constant* s)
{
TRACEINST(s) << "This should be a constant string... " << s;
if (String_Quoted* quoted = dynamic_cast<String_Quoted*>(s)) {
TRACEINST(s) << "... but dynamic_cast<String_Quoted*> worked";
return Output::operator()(quoted);
} else {
string value(s->value());
if (s->can_compress_whitespace() && output_style() == COMPRESSED) {
value.erase(std::remove_if(value.begin(), value.end(), ::isspace), value.end());
}
if (!in_comment) {
TRACEINST(s) << "... sending via string_to_output";
append_token(string_to_output(value), s);
} else {
TRACEINST(s) << "... sending directly (in comment)";
append_token(value, s);
}
}
Expand Down
1 change: 1 addition & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,7 @@ namespace Sass {

if (lex< identifier >()) {
String_Constant* str = new (ctx.mem) String_Quoted(pstate, lexed);
TRACEINST(str) << "We have just created a new instance " << str;
// Dont' delay this string if it is a name color. Fixes #652.
str->is_delayed(ctx.names_to_colors.count(unquote(lexed)) == 0);
return str;
Expand Down
3 changes: 2 additions & 1 deletion to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace Sass {
{ return sass_make_color(c->r(), c->g(), c->b(), c->a()); }

Sass_Value* To_C::operator()(String_Constant* s)
{ return sass_make_string(s->value().c_str()); }
{ TRACEINST(s) << "Converting unquoted value to C" << s;
return sass_make_string(s->value().c_str()); }

Sass_Value* To_C::operator()(List* l)
{
Expand Down

0 comments on commit 811c559

Please sign in to comment.