Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 48e97c2

Browse files
committed
Merge pull request #715 from mgreter/feature/overload-error-fn
Implement @error function
2 parents efe46c6 + d31c960 commit 48e97c2

26 files changed

+151
-19
lines changed

ast.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,18 @@ namespace Sass {
442442
ATTACH_OPERATIONS();
443443
};
444444

445+
///////////////////////////////
446+
// The Sass `@error` directive.
447+
///////////////////////////////
448+
class Error : public Statement {
449+
ADD_PROPERTY(Expression*, message);
450+
public:
451+
Error(string path, Position position, Expression* msg)
452+
: Statement(path, position), message_(msg)
453+
{ }
454+
ATTACH_OPERATIONS();
455+
};
456+
445457
///////////////////////////////////////////
446458
// CSS comments. These may be interpolated.
447459
///////////////////////////////////////////

ast_factory.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Sass {
2424
Import<Function_Call*>* new_CSS_Import(string p, size_t l, Function_Call* loc);
2525
Import<String*>* new_SASS_Import(string p, size_t l, String* loc);
2626
Warning* new_Warning(string p, size_t l, Expression* msg);
27+
Error* new_Error(string p, size_t l, Expression* msg);
2728
Comment* new_Comment(string p, size_t l, String* txt);
2829
If* new_If(string p, size_t l, Expression* pred, Block* con, Block* alt = 0);
2930
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
@@ -17,6 +17,7 @@ namespace Sass {
1717
class Import;
1818
class Import_Stub;
1919
class Warning;
20+
class Error;
2021
class Comment;
2122
class If;
2223
class For;

constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Sass {
2626
extern const char in_kwd[] = "in";
2727
extern const char while_kwd[] = "@while";
2828
extern const char warn_kwd[] = "@warn";
29+
extern const char error_kwd[] = "@error";
2930
extern const char default_kwd[] = "default";
3031
extern const char global_kwd[] = "global";
3132
extern const char null_kwd[] = "null";

constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Sass {
2626
extern const char in_kwd[];
2727
extern const char while_kwd[];
2828
extern const char warn_kwd[];
29+
extern const char error_kwd[];
2930
extern const char default_kwd[];
3031
extern const char global_kwd[];
3132
extern const char null_kwd[];

error_handling.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Sass {
99

10-
Error::Error(Type type, string path, Position position, string message)
10+
Sass_Error::Sass_Error(Type type, string path, Position position, string message)
1111
: type(type), path(path), position(position), message(message)
1212
{ }
1313

1414
void error(string msg, string path, Position position)
15-
{ throw Error(Error::syntax, path, position, msg); }
15+
{ throw Sass_Error(Sass_Error::syntax, path, position, msg); }
1616

1717
void error(string msg, string path, Position position, Backtrace* bt)
1818
{
@@ -22,7 +22,7 @@ namespace Sass {
2222
Backtrace top(bt, path, position, "");
2323
msg += top.to_string();
2424

25-
throw Error(Error::syntax, path, position, msg);
25+
throw Sass_Error(Sass_Error::syntax, path, position, msg);
2626
}
2727

2828
}

error_handling.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ namespace Sass {
1010

1111
struct Backtrace;
1212

13-
struct Error {
13+
struct Sass_Error {
1414
enum Type { read, write, syntax, evaluation };
1515

1616
Type type;
1717
string path;
1818
Position position;
1919
string message;
2020

21-
Error(Type type, string path, Position position, string message);
21+
Sass_Error(Type type, string path, Position position, string message);
2222

2323
};
2424

eval.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,38 @@ namespace Sass {
218218
return 0;
219219
}
220220

221+
Expression* Eval::operator()(Error* e)
222+
{
223+
Expression* message = e->message()->perform(this);
224+
To_String to_string;
225+
226+
// try to use generic function
227+
if (env->has("@error[f]")) {
228+
229+
Definition* def = static_cast<Definition*>((*env)["@error[f]"]);
230+
// Block* body = def->block();
231+
// Native_Function func = def->native_function();
232+
Sass_C_Function c_func = def->c_function();
233+
234+
To_C to_c;
235+
union Sass_Value* c_args = sass_make_list(1, SASS_COMMA);
236+
sass_list_set_value(c_args, 0, message->perform(&to_c));
237+
Sass_Value* c_val = c_func(c_args, def->cookie());
238+
sass_delete_value(c_args);
239+
sass_delete_value(c_val);
240+
return 0;
241+
242+
}
243+
244+
string prefix("Error: ");
245+
string result(unquote(message->perform(&to_string)));
246+
cerr << prefix << result;
247+
Backtrace top(backtrace, e->path(), e->position(), "");
248+
cerr << top.to_string(true);
249+
cerr << endl << endl;
250+
return 0;
251+
}
252+
221253
Expression* Eval::operator()(List* l)
222254
{
223255
if (l->is_expanded()) return l;
@@ -450,6 +482,8 @@ namespace Sass {
450482
Sass_Value* c_val = c_func(c_args, def->cookie());
451483
if (sass_value_get_tag(c_val) == SASS_ERROR) {
452484
error("error in C function " + c->name() + ": " + sass_error_get_message(c_val), c->path(), c->position(), backtrace);
485+
} else if (sass_value_get_tag(c_val) == SASS_WARNING) {
486+
error("warning in C function " + c->name() + ": " + sass_warning_get_message(c_val), c->path(), c->position(), backtrace);
453487
}
454488
result = cval_to_astnode(c_val, ctx, backtrace, c->path(), c->position());
455489

@@ -1037,7 +1071,10 @@ namespace Sass {
10371071
e = new (ctx.mem) Null(path, position);
10381072
} break;
10391073
case SASS_ERROR: {
1040-
error("error in C function: " + string(sass_error_get_message(v)), path, position, backtrace);
1074+
error("Error in C function: " + string(sass_error_get_message(v)), path, position, backtrace);
1075+
} break;
1076+
case SASS_WARNING: {
1077+
error("Warning in C function: " + string(sass_warning_get_message(v)), path, position, backtrace);
10411078
} break;
10421079
}
10431080
return e;

eval.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace Sass {
4646
Expression* operator()(While*);
4747
Expression* operator()(Return*);
4848
Expression* operator()(Warning*);
49+
Expression* operator()(Error*);
4950

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

expand.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ namespace Sass {
174174
return 0;
175175
}
176176

177+
Statement* Expand::operator()(Error* e)
178+
{
179+
// eval handles this too, because errors may occur in functions
180+
e->perform(eval->with(env, backtrace));
181+
return 0;
182+
}
183+
177184
Statement* Expand::operator()(Comment* c)
178185
{
179186
// TODO: eval the text, once we're parsing/storing it as a String_Schema

0 commit comments

Comments
 (0)