Skip to content

Commit cf7c1d1

Browse files
committed
Merge pull request #755 from mgreter/feature/debug-statement
Implement @debug statement
2 parents ccfcfd2 + eae7c0a commit cf7c1d1

24 files changed

+145
-29
lines changed

ast.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,18 @@ namespace Sass {
454454
ATTACH_OPERATIONS();
455455
};
456456

457+
///////////////////////////////
458+
// The Sass `@debug` directive.
459+
///////////////////////////////
460+
class Debug : public Statement {
461+
ADD_PROPERTY(Expression*, value);
462+
public:
463+
Debug(string path, Position position, Expression* val)
464+
: Statement(path, position), value_(val)
465+
{ }
466+
ATTACH_OPERATIONS();
467+
};
468+
457469
///////////////////////////////////////////
458470
// CSS comments. These may be interpolated.
459471
///////////////////////////////////////////

ast_factory.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace Sass {
2525
Import<String*>* new_SASS_Import(string p, size_t l, String* loc);
2626
Warning* new_Warning(string p, size_t l, Expression* msg);
2727
Error* new_Error(string p, size_t l, Expression* msg);
28+
Debug* new_Debug(string p, size_t l, Expression* val);
2829
Comment* new_Comment(string p, size_t l, String* txt);
2930
If* new_If(string p, size_t l, Expression* pred, Block* con, Block* alt = 0);
3031
For* new_For(string p, size_t l, string var, Expression* lo, Expression* hi, Block* b, bool inc);

ast_fwd_decl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Sass {
1818
class Import_Stub;
1919
class Warning;
2020
class Error;
21+
class Debug;
2122
class Comment;
2223
class If;
2324
class For;

backtrace.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include "position.hpp"
77
#endif
88

9+
#ifndef SASS_FILE
10+
#include "file.hpp"
11+
#endif
12+
913
namespace Sass {
1014

1115
using namespace std;
@@ -26,19 +30,35 @@ namespace Sass {
2630

2731
string to_string(bool warning = false)
2832
{
33+
size_t i = -1;
2934
stringstream ss;
35+
string cwd(Sass::File::get_cwd());
3036
Backtrace* this_point = this;
3137

3238
if (!warning) ss << endl << "Backtrace:";
3339
// the first tracepoint (which is parent-less) is an empty placeholder
3440
while (this_point->parent) {
35-
ss << endl
36-
<< "\t"
37-
<< (warning ? " " : "")
38-
<< this_point->path
39-
<< ":"
40-
<< this_point->position.line
41-
<< this_point->parent->caller;
41+
42+
// make path relative to the current directory
43+
string rel_path(Sass::File::resolve_relative_path(this_point->path, cwd, cwd));
44+
45+
if (warning) {
46+
ss << endl
47+
<< "\t"
48+
<< (++i == 0 ? "on" : "from")
49+
<< " line "
50+
<< this_point->position.line
51+
<< " of "
52+
<< rel_path;
53+
} else {
54+
ss << endl
55+
<< "\t"
56+
<< rel_path
57+
<< ":"
58+
<< this_point->position.line
59+
<< this_point->parent->caller;
60+
}
61+
4262
this_point = this_point->parent;
4363
}
4464

constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Sass {
2727
extern const char while_kwd[] = "@while";
2828
extern const char warn_kwd[] = "@warn";
2929
extern const char error_kwd[] = "@error";
30+
extern const char debug_kwd[] = "@debug";
3031
extern const char default_kwd[] = "default";
3132
extern const char global_kwd[] = "global";
3233
extern const char null_kwd[] = "null";

constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Sass {
2727
extern const char while_kwd[];
2828
extern const char warn_kwd[];
2929
extern const char error_kwd[];
30+
extern const char debug_kwd[];
3031
extern const char default_kwd[];
3132
extern const char global_kwd[];
3233
extern const char null_kwd[];

context.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#ifdef _WIN32
2-
#include <direct.h>
3-
#define getcwd _getcwd
42
#define PATH_SEP ';'
53
#else
6-
#include <unistd.h>
74
#define PATH_SEP ':'
85
#endif
96

@@ -266,7 +263,7 @@ namespace Sass {
266263
0, 0
267264
);
268265
import_stack.push_back(import);
269-
Parser p(Parser::from_c_str(queue[i].source, *this, queue[i].load_path, Position(1 + i, 1, 1)));
266+
Parser p(Parser::from_c_str(queue[i].source, *this, queue[i].abs_path, Position(1 + i, 1, 1)));
270267
Block* ast = p.parse();
271268
sass_delete_import(import_stack.back());
272269
import_stack.pop_back();
@@ -359,15 +356,7 @@ namespace Sass {
359356

360357
string Context::get_cwd()
361358
{
362-
const size_t wd_len = 1024;
363-
char wd[wd_len];
364-
string cwd = getcwd(wd, wd_len);
365-
#ifdef _WIN32
366-
//convert backslashes to forward slashes
367-
replace(cwd.begin(), cwd.end(), '\\', '/');
368-
#endif
369-
if (cwd[cwd.length() - 1] != '/') cwd += '/';
370-
return cwd;
359+
return Sass::File::get_cwd();
371360
}
372361

373362
void register_function(Context& ctx, Signature sig, Native_Function f, Env* env)

context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ namespace Sass {
114114

115115
Context(Data);
116116
~Context();
117+
static string get_cwd();
117118
void setup_color_map();
118119
string add_file(string);
119120
Block* parse_file();
@@ -134,7 +135,6 @@ namespace Sass {
134135
void collect_include_paths(const char* paths_str);
135136
void collect_include_paths(const char** paths_array);
136137
string format_source_mapping_url(const string& file);
137-
string get_cwd();
138138

139139
string cwd;
140140

eval.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "file.hpp"
12
#include "eval.hpp"
23
#include "ast.hpp"
34
#include "bind.hpp"
@@ -211,10 +212,9 @@ namespace Sass {
211212

212213
}
213214

214-
string prefix("WARNING: ");
215215
string result(unquote(message->perform(&to_string)));
216-
cerr << prefix << result;
217216
Backtrace top(backtrace, w->path(), w->position(), "");
217+
cerr << "WARNING: " << result;
218218
cerr << top.to_string(true);
219219
cerr << endl << endl;
220220
return 0;
@@ -243,15 +243,45 @@ namespace Sass {
243243

244244
}
245245

246-
string prefix("Error: ");
247246
string result(unquote(message->perform(&to_string)));
248-
cerr << prefix << result;
249247
Backtrace top(backtrace, e->path(), e->position(), "");
248+
cerr << "Error: " << result;
250249
cerr << top.to_string(true);
251250
cerr << endl << endl;
252251
return 0;
253252
}
254253

254+
Expression* Eval::operator()(Debug* d)
255+
{
256+
Expression* message = d->value()->perform(this);
257+
To_String to_string;
258+
259+
// try to use generic function
260+
if (env->has("@debug[f]")) {
261+
262+
Definition* def = static_cast<Definition*>((*env)["@debug[f]"]);
263+
// Block* body = def->block();
264+
// Native_Function func = def->native_function();
265+
Sass_C_Function c_func = def->c_function();
266+
267+
To_C to_c;
268+
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA);
269+
sass_list_set_value(c_args, 0, message->perform(&to_c));
270+
Sass_Value* c_val = c_func(c_args, def->cookie());
271+
sass_delete_value(c_args);
272+
sass_delete_value(c_val);
273+
return 0;
274+
275+
}
276+
277+
string cwd(ctx.get_cwd());
278+
string result(unquote(message->perform(&to_string)));
279+
string rel_path(Sass::File::resolve_relative_path(d->path(), cwd, cwd));
280+
cerr << rel_path << ":" << d->position().line << ":" << " DEBUG: " << result;
281+
cerr << endl;
282+
return 0;
283+
}
284+
255285
Expression* Eval::operator()(List* l)
256286
{
257287
if (l->is_expanded()) return l;

eval.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace Sass {
4747
Expression* operator()(Return*);
4848
Expression* operator()(Warning*);
4949
Expression* operator()(Error*);
50+
Expression* operator()(Debug*);
5051

5152
Expression* operator()(List*);
5253
Expression* operator()(Map*);

expand.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ namespace Sass {
181181
return 0;
182182
}
183183

184+
Statement* Expand::operator()(Debug* d)
185+
{
186+
// eval handles this too, because warnings may occur in functions
187+
d->perform(eval->with(env, backtrace));
188+
return 0;
189+
}
190+
184191
Statement* Expand::operator()(Comment* c)
185192
{
186193
// TODO: eval the text, once we're parsing/storing it as a String_Schema

expand.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace Sass {
5656
Statement* operator()(Import_Stub*);
5757
Statement* operator()(Warning*);
5858
Statement* operator()(Error*);
59+
Statement* operator()(Debug*);
5960
Statement* operator()(Comment*);
6061
Statement* operator()(If*);
6162
Statement* operator()(For*);

file.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#ifdef _WIN32
2+
#include <direct.h>
3+
#define getcwd _getcwd
24
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
5+
#else
6+
#include <unistd.h>
37
#endif
48

59
#include <iostream>
@@ -28,6 +32,19 @@ namespace Sass {
2832
namespace File {
2933
using namespace std;
3034

35+
string get_cwd()
36+
{
37+
const size_t wd_len = 1024;
38+
char wd[wd_len];
39+
string cwd = getcwd(wd, wd_len);
40+
#ifdef _WIN32
41+
//convert backslashes to forward slashes
42+
replace(cwd.begin(), cwd.end(), '\\', '/');
43+
#endif
44+
if (cwd[cwd.length() - 1] != '/') cwd += '/';
45+
return cwd;
46+
}
47+
3148
// no physical check on filesystem
3249
// only a logical cleanup of a path
3350
string make_canonical_path (string path)

file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Sass {
44
using namespace std;
55
struct Context;
66
namespace File {
7+
string get_cwd();
78
string base_name(string);
89
string dir_name(string);
910
string join_paths(string, string);

functions.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ namespace Sass {
5353
Definition* make_c_function(Signature sig, Sass_C_Function f, void* cookie, Context& ctx)
5454
{
5555
Parser sig_parser = Parser::from_c_str(sig, ctx, "[c function]");
56-
// allow to overload generic callback plus @warn and @error with custom functions
56+
// allow to overload generic callback plus @warn, @error and @debug with custom functions
5757
sig_parser.lex < alternatives < identifier, exactly <'*'>,
5858
exactly < Constants::warn_kwd >,
59-
exactly < Constants::error_kwd >
59+
exactly < Constants::error_kwd >,
60+
exactly < Constants::debug_kwd >
6061
> >();
6162
string name(Util::normalize_underscores(sig_parser.lexed));
6263
Parameters* params = sig_parser.parse_parameters();
@@ -124,10 +125,10 @@ namespace Sass {
124125
BYTE rb[8];
125126
CryptAcquireContext(&hp, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
126127
CryptGenRandom(hp, sizeof(rb), rb);
127-
CryptReleaseContext(hp, 0);
128+
CryptReleaseContext(hp, 0);
128129

129130
uint64_t seed;
130-
memcpy(&seed, &rb[0], sizeof(seed));
131+
memcpy(&seed, &rb[0], sizeof(seed));
131132

132133
return seed;
133134
}

inspect.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ namespace Sass {
146146
append_to_buffer(";");
147147
}
148148

149+
void Inspect::operator()(Debug* debug)
150+
{
151+
if (ctx) ctx->source_map.add_mapping(debug);
152+
append_to_buffer("@debug ");
153+
debug->value()->perform(this);
154+
append_to_buffer(";");
155+
}
156+
149157
void Inspect::operator()(Comment* comment)
150158
{
151159
comment->text()->perform(this);

inspect.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace Sass {
4949
virtual void operator()(Import_Stub*);
5050
virtual void operator()(Warning*);
5151
virtual void operator()(Error*);
52+
virtual void operator()(Debug*);
5253
virtual void operator()(Comment*);
5354
virtual void operator()(If*);
5455
virtual void operator()(For*);

operation.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Sass {
2626
virtual T operator()(Import_Stub* x) = 0;
2727
virtual T operator()(Warning* x) = 0;
2828
virtual T operator()(Error* x) = 0;
29+
virtual T operator()(Debug* x) = 0;
2930
virtual T operator()(Comment* x) = 0;
3031
virtual T operator()(If* x) = 0;
3132
virtual T operator()(For* x) = 0;
@@ -95,6 +96,7 @@ namespace Sass {
9596
virtual T operator()(Import_Stub* x) { return static_cast<D*>(this)->fallback(x); }
9697
virtual T operator()(Warning* x) { return static_cast<D*>(this)->fallback(x); }
9798
virtual T operator()(Error* x) { return static_cast<D*>(this)->fallback(x); }
99+
virtual T operator()(Debug* x) { return static_cast<D*>(this)->fallback(x); }
98100
virtual T operator()(Comment* x) { return static_cast<D*>(this)->fallback(x); }
99101
virtual T operator()(If* x) { return static_cast<D*>(this)->fallback(x); }
100102
virtual T operator()(For* x) { return static_cast<D*>(this)->fallback(x); }

output_compressed.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace Sass {
4343
// virtual void operator()(Import_Stub*);
4444
// virtual void operator()(Warning*);
4545
// virtual void operator()(Error*);
46+
// virtual void operator()(Debug*);
4647
virtual void operator()(Comment*);
4748
// virtual void operator()(If*);
4849
// virtual void operator()(For*);

output_nested.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace Sass {
5454
// virtual void operator()(Import_Stub*);
5555
// virtual void operator()(Warning*);
5656
// virtual void operator()(Error*);
57+
// virtual void operator()(Debug*);
5758
// virtual void operator()(Comment*);
5859
// virtual void operator()(If*);
5960
// virtual void operator()(For*);

0 commit comments

Comments
 (0)