Skip to content

Commit 31002ec

Browse files
committed
Add Custom_Warning/Error for exchange with C-API
1 parent 8fd455b commit 31002ec

File tree

11 files changed

+102
-1
lines changed

11 files changed

+102
-1
lines changed

ast.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,32 @@ namespace Sass {
11731173
return string();
11741174
}
11751175

1176+
bool Custom_Warning::operator== (Expression* rhs) const
1177+
{
1178+
if (Custom_Warning* r = dynamic_cast<Custom_Warning*>(rhs)) {
1179+
return message() == r->message();
1180+
}
1181+
return false;
1182+
}
1183+
1184+
bool Custom_Warning::operator== (Expression& rhs) const
1185+
{
1186+
return operator==(&rhs);
1187+
}
1188+
1189+
bool Custom_Error::operator== (Expression* rhs) const
1190+
{
1191+
if (Custom_Error* r = dynamic_cast<Custom_Error*>(rhs)) {
1192+
return message() == r->message();
1193+
}
1194+
return false;
1195+
}
1196+
1197+
bool Custom_Error::operator== (Expression& rhs) const
1198+
{
1199+
return operator==(&rhs);
1200+
}
1201+
11761202
bool Number::operator== (Expression* rhs) const
11771203
{
11781204
if (Number* r = dynamic_cast<Number*>(rhs)) {
@@ -1585,5 +1611,14 @@ namespace Sass {
15851611
return quote_mark_ ? quote(value_, quote_mark_, true) : value_;
15861612
}
15871613

1614+
string Custom_Error::to_string(bool compressed, int precision) const
1615+
{
1616+
return message();
1617+
}
1618+
string Custom_Warning::to_string(bool compressed, int precision) const
1619+
{
1620+
return message();
1621+
}
1622+
15881623
}
15891624

ast.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ namespace Sass {
103103
MAP,
104104
SELECTOR,
105105
NULL_VAL,
106+
C_WARNING,
107+
C_ERROR,
106108
NUM_TYPES
107109
};
108110
private:
@@ -1250,6 +1252,36 @@ namespace Sass {
12501252
ATTACH_OPERATIONS()
12511253
};
12521254

1255+
//////////////////////////////
1256+
// Errors from Sass_Values.
1257+
//////////////////////////////
1258+
class Custom_Error : public Value {
1259+
ADD_PROPERTY(string, message)
1260+
public:
1261+
Custom_Error(ParserState pstate, string msg)
1262+
: Value(pstate), message_(msg)
1263+
{ concrete_type(C_ERROR); }
1264+
virtual bool operator== (Expression& rhs) const;
1265+
virtual bool operator== (Expression* rhs) const;
1266+
virtual string to_string(bool compressed = false, int precision = 5) const;
1267+
ATTACH_OPERATIONS()
1268+
};
1269+
1270+
//////////////////////////////
1271+
// Warnings from Sass_Values.
1272+
//////////////////////////////
1273+
class Custom_Warning : public Value {
1274+
ADD_PROPERTY(string, message)
1275+
public:
1276+
Custom_Warning(ParserState pstate, string msg)
1277+
: Value(pstate), message_(msg)
1278+
{ concrete_type(C_WARNING); }
1279+
virtual bool operator== (Expression& rhs) const;
1280+
virtual bool operator== (Expression* rhs) const;
1281+
virtual string to_string(bool compressed = false, int precision = 5) const;
1282+
ATTACH_OPERATIONS()
1283+
};
1284+
12531285
////////////
12541286
// Booleans.
12551287
////////////

ast_factory.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace Sass {
2424
Assignment* new_Assignment(string p, size_t l, string var, Expression* val, bool guarded = false);
2525
Import<Function_Call*>* new_CSS_Import(string p, size_t l, Function_Call* loc);
2626
Import<String*>* new_SASS_Import(string p, size_t l, String* loc);
27+
Custom_Warning* new_Custom_Warning(string msg, size_t l, string msg);
28+
Custom_Error* new_Custom_Error(string p, size_t l, string msg);
2729
Warning* new_Warning(string p, size_t l, Expression* msg);
2830
Error* new_Error(string p, size_t l, Expression* msg);
2931
Debug* new_Debug(string p, size_t l, Expression* val);

ast_fwd_decl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace Sass {
4646
class Unary_Expression;
4747
class Function_Call;
4848
class Function_Call_Schema;
49+
class Custom_Warning;
50+
class Custom_Error;
4951
class Variable;
5052
class Textual;
5153
class Number;

debugger.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
557557
case Expression::Concrete_Type::MAP: cerr << " [MAP]"; break;
558558
case Expression::Concrete_Type::SELECTOR: cerr << " [SELECTOR]"; break;
559559
case Expression::Concrete_Type::NULL_VAL: cerr << " [NULL_VAL]"; break;
560+
case Expression::Concrete_Type::C_WARNING: cerr << " [C_WARNING]"; break;
561+
case Expression::Concrete_Type::C_ERROR: cerr << " [C_ERROR]"; break;
560562
case Expression::Concrete_Type::NUM_TYPES: cerr << " [NUM_TYPES]"; break;
561563
}
562564
cerr << endl;

inspect.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ namespace Sass {
5757
virtual void operator()(Unary_Expression*);
5858
virtual void operator()(Function_Call*);
5959
virtual void operator()(Function_Call_Schema*);
60+
// virtual void operator()(Custom_Warning*);
61+
// virtual void operator()(Custom_Error*);
6062
virtual void operator()(Variable*);
6163
virtual void operator()(Textual*);
6264
virtual void operator()(Number*);

operation.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ namespace Sass {
4848
virtual T operator()(Unary_Expression* x) = 0;
4949
virtual T operator()(Function_Call* x) = 0;
5050
virtual T operator()(Function_Call_Schema* x) = 0;
51+
virtual T operator()(Custom_Warning* x) = 0;
52+
virtual T operator()(Custom_Error* x) = 0;
5153
virtual T operator()(Variable* x) = 0;
5254
virtual T operator()(Textual* x) = 0;
5355
virtual T operator()(Number* x) = 0;
@@ -123,6 +125,8 @@ namespace Sass {
123125
virtual T operator()(Unary_Expression* x) { return static_cast<D*>(this)->fallback(x); }
124126
virtual T operator()(Function_Call* x) { return static_cast<D*>(this)->fallback(x); }
125127
virtual T operator()(Function_Call_Schema* x) { return static_cast<D*>(this)->fallback(x); }
128+
virtual T operator()(Custom_Warning* x) { return static_cast<D*>(this)->fallback(x); }
129+
virtual T operator()(Custom_Error* x) { return static_cast<D*>(this)->fallback(x); }
126130
virtual T operator()(Variable* x) { return static_cast<D*>(this)->fallback(x); }
127131
virtual T operator()(Textual* x) { return static_cast<D*>(this)->fallback(x); }
128132
virtual T operator()(Number* x) { return static_cast<D*>(this)->fallback(x); }

to_c.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ namespace Sass {
77
using namespace std;
88

99
union Sass_Value* To_C::fallback_impl(AST_Node* n)
10-
{ return sass_make_null(); }
10+
{ return sass_make_error("unknown type for C-API"); }
1111

1212
union Sass_Value* To_C::operator()(Boolean* b)
1313
{ return sass_make_boolean(b->value()); }
1414

1515
union Sass_Value* To_C::operator()(Number* n)
1616
{ return sass_make_number(n->value(), n->unit().c_str()); }
1717

18+
union Sass_Value* To_C::operator()(Custom_Warning* w)
19+
{ return sass_make_warning(w->message().c_str()); }
20+
21+
union Sass_Value* To_C::operator()(Custom_Error* e)
22+
{ return sass_make_error(e->message().c_str()); }
23+
1824
union Sass_Value* To_C::operator()(Color* c)
1925
{ return sass_make_color(c->r(), c->g(), c->b(), c->a()); }
2026

to_c.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace Sass {
2424
union Sass_Value* operator()(Color*);
2525
union Sass_Value* operator()(String_Constant*);
2626
union Sass_Value* operator()(String_Quoted*);
27+
union Sass_Value* operator()(Custom_Warning*);
28+
union Sass_Value* operator()(Custom_Error*);
2729
union Sass_Value* operator()(List*);
2830
union Sass_Value* operator()(Map*);
2931
union Sass_Value* operator()(Null*);

to_value.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ namespace Sass {
1515
return 0;
1616
}
1717

18+
// Custom_Error is a valid value
19+
Value* To_Value::operator()(Custom_Error* e)
20+
{
21+
return e;
22+
}
23+
24+
// Custom_Warning is a valid value
25+
Value* To_Value::operator()(Custom_Warning* w)
26+
{
27+
return w;
28+
}
29+
1830
// Boolean is a valid value
1931
Value* To_Value::operator()(Boolean* b)
2032
{

0 commit comments

Comments
 (0)