Commit 6863a86f by Aaron Leung

Implemented the @warn directive. A bit glitchy if you try to pass it a list with…

Implemented the @warn directive. A bit glitchy if you try to pass it a list with arithmetic expressions and interpolants, but it generally works without barfing.
parent a210fc7f
...@@ -168,6 +168,7 @@ namespace Sass { ...@@ -168,6 +168,7 @@ namespace Sass {
Node parse_while_directive(Node surrounding_ruleset, Node::Type inside_of = Node::none); Node parse_while_directive(Node surrounding_ruleset, Node::Type inside_of = Node::none);
Node parse_media_query(Node::Type inside_of = Node::none); Node parse_media_query(Node::Type inside_of = Node::none);
Node parse_media_expression(); Node parse_media_expression();
Node parse_warning();
Selector_Lookahead lookahead_for_selector(const char* start = 0); Selector_Lookahead lookahead_for_selector(const char* start = 0);
......
...@@ -54,6 +54,10 @@ namespace Sass { ...@@ -54,6 +54,10 @@ namespace Sass {
else if (peek< media >()) { else if (peek< media >()) {
root << parse_media_query(Node::none); root << parse_media_query(Node::none);
} }
else if (peek< warn >()) {
root << parse_warning();
if (!lex< exactly<';'> >()) throw_syntax_error("top-level @warn directive must be terminated by ';'");
}
else { else {
lex< spaces_and_comments >(); lex< spaces_and_comments >();
throw_syntax_error("invalid top-level expression"); throw_syntax_error("invalid top-level expression");
...@@ -514,6 +518,10 @@ namespace Sass { ...@@ -514,6 +518,10 @@ namespace Sass {
block << ret_expr; block << ret_expr;
semicolon = true; semicolon = true;
} }
else if (peek< warn >()) {
block << parse_warning();
semicolon = true;
}
else if (inside_of == Node::function) { else if (inside_of == Node::function) {
throw_syntax_error("only variable declarations and control directives are allowed inside functions"); throw_syntax_error("only variable declarations and control directives are allowed inside functions");
} }
...@@ -882,6 +890,7 @@ namespace Sass { ...@@ -882,6 +890,7 @@ namespace Sass {
break; break;
} }
} }
schema.should_eval() = true;
return schema; return schema;
} }
...@@ -1097,6 +1106,15 @@ namespace Sass { ...@@ -1097,6 +1106,15 @@ namespace Sass {
return media_expr; return media_expr;
} }
Node Document::parse_warning()
{
lex< warn >();
Node warning(context.new_Node(Node::warning, path, line, 1));
warning << parse_list();
warning[0].should_eval() = true;
return warning;
}
Selector_Lookahead Document::lookahead_for_selector(const char* start) Selector_Lookahead Document::lookahead_for_selector(const char* start)
{ {
const char* p = start ? start : position; const char* p = start ? start : position;
......
...@@ -320,6 +320,7 @@ namespace Sass { ...@@ -320,6 +320,7 @@ namespace Sass {
case Node::string_schema: case Node::string_schema:
case Node::value_schema: case Node::value_schema:
case Node::identifier_schema: { case Node::identifier_schema: {
cerr << "evaluating a string schema: " << expr.to_string() << endl;
for (size_t i = 0, S = expr.size(); i < S; ++i) { for (size_t i = 0, S = expr.size(); i < S; ++i) {
expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx); expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx);
} }
...@@ -407,6 +408,11 @@ namespace Sass { ...@@ -407,6 +408,11 @@ namespace Sass {
} }
} break; } break;
case Node::warning: {
expr[0] = eval(expr[0], prefix, env, f_env, new_Node, ctx);
return expr;
} break;
default: { default: {
return expr; return expr;
} break; } break;
......
...@@ -161,6 +161,8 @@ namespace Sass { ...@@ -161,6 +161,8 @@ namespace Sass {
return_directive, return_directive,
content_directive, content_directive,
warning,
variable, variable,
assignment assignment
}; };
...@@ -293,7 +295,8 @@ namespace Sass { ...@@ -293,7 +295,8 @@ namespace Sass {
case Node::comment: case Node::comment:
case Node::css_import: case Node::css_import:
case Node::rule: case Node::rule:
case Node::propset: has_statements = true; break; case Node::propset:
case Node::warning: has_statements = true; break;
case Node::media_query: case Node::media_query:
case Node::ruleset: has_blocks = true; break; case Node::ruleset: has_blocks = true; break;
......
...@@ -324,6 +324,22 @@ namespace Sass { ...@@ -324,6 +324,22 @@ namespace Sass {
return result; return result;
} break; } break;
case warning: {
string prefix("WARNING: ");
string indent(" ");
Node contents(at(0));
string result(contents.to_string());
if (contents.type() == string_constant || contents.type() == string_schema) {
result = result.substr(1, result.size()-2); // unquote if it's a single string
}
stringstream ss;
ss << prefix << result << endl;
ss << indent << "on line " << at(0).line() << " of " << at(0).path();
ss << endl << endl;
cerr << ss.str();
return "";
} break;
default: { default: {
// return content.token.to_string(); // return content.token.to_string();
if (!has_children()) return token().to_string(); if (!has_children()) return token().to_string();
...@@ -354,7 +370,7 @@ namespace Sass { ...@@ -354,7 +370,7 @@ namespace Sass {
buf << " {"; buf << " {";
for (size_t i = 0, S = block.size(); i < S; ++i) { for (size_t i = 0, S = block.size(); i < S; ++i) {
Type stm_type = block[i].type(); Type stm_type = block[i].type();
if (stm_type == comment || stm_type == rule || stm_type == css_import || stm_type == propset) { if (stm_type == comment || stm_type == rule || stm_type == css_import || stm_type == propset || stm_type == warning) {
block[i].emit_nested_css(buf, depth+1); block[i].emit_nested_css(buf, depth+1);
} }
} }
......
...@@ -179,6 +179,12 @@ namespace Sass { ...@@ -179,6 +179,12 @@ namespace Sass {
exactly<'-'>, exactly<'-'>,
exactly<'_'> > >(src); exactly<'_'> > >(src);
} }
extern const char warn_kwd[] = "@warn";
const char* warn(const char* src) {
return exactly<warn_kwd>(src);
}
// Match CSS type selectors // Match CSS type selectors
const char* namespace_prefix(const char* src) { const char* namespace_prefix(const char* src) {
return sequence< optional< alternatives< identifier, exactly<'*'> > >, return sequence< optional< alternatives< identifier, exactly<'*'> > >,
......
...@@ -329,6 +329,8 @@ namespace Sass { ...@@ -329,6 +329,8 @@ namespace Sass {
const char* while_directive(const char* src); const char* while_directive(const char* src);
const char* warn(const char* src);
// Match CSS type selectors // Match CSS type selectors
const char* namespace_prefix(const char* src); const char* namespace_prefix(const char* src);
const char* type_selector(const char* src); const char* type_selector(const char* src);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment