Skip to content

Commit bbfb15e

Browse files
committed
Merge pull request #957 from mgreter/bugfix/issue_945
Add parsing error for empty declaration value
2 parents 5f01135 + 4e5472e commit bbfb15e

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

debugger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
298298
cerr << ind << "Map " << expression << " [Hashed]" << endl;
299299
} else if (dynamic_cast<List*>(node)) {
300300
List* expression = dynamic_cast<List*>(node);
301-
cerr << ind << "List " << expression <<
301+
cerr << ind << "List " << expression << " (" << expression->length() << ") " <<
302302
(expression->separator() == Sass::List::Separator::COMMA ? "Comma " : "Space ") <<
303303
" [delayed: " << expression->is_delayed() << "] " <<
304304
" [interpolant: " << expression->is_interpolant() << "] " <<

parser.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,13 @@ namespace Sass {
954954
return new (ctx.mem) Declaration(prop->pstate(), prop, parse_static_value()/*, lex<important>()*/);
955955
}
956956
else {
957-
return new (ctx.mem) Declaration(prop->pstate(), prop, parse_list()/*, lex<important>()*/);
957+
Expression* list_ex = parse_list();
958+
if (List* list = dynamic_cast<List*>(list_ex)) {
959+
if (list->length() == 0 && !peek< exactly <'{'> >()) {
960+
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
961+
}
962+
}
963+
return new (ctx.mem) Declaration(prop->pstate(), prop, list_ex/*, lex<important>()*/);
958964
}
959965
}
960966

@@ -2191,4 +2197,39 @@ namespace Sass {
21912197
throw Sass_Error(Sass_Error::syntax, ParserState(path, pos.line ? pos : before_token, Offset(0, 0)), msg);
21922198
}
21932199

2200+
// print a css parsing error with actual context information from parsed source
2201+
void Parser::css_error(const string& msg, const string& prefix, const string& middle)
2202+
{
2203+
int max_len = 14;
2204+
const char* pos = peek < optional_spaces >();
2205+
bool ellipsis_left = false;
2206+
const char* pos_left(pos);
2207+
while (*pos_left && pos_left >= source) {
2208+
if (pos - pos_left > max_len) {
2209+
ellipsis_left = true;
2210+
break;
2211+
}
2212+
if (*pos_left == '\r') break;
2213+
if (*pos_left == '\n') break;
2214+
-- pos_left;
2215+
}
2216+
bool ellipsis_right = false;
2217+
const char* pos_right(pos);
2218+
while (*pos_right && pos_right <= end) {
2219+
if (pos_right - pos > max_len) {
2220+
ellipsis_right = true;
2221+
break;
2222+
}
2223+
if (*pos_right == '\r') break;
2224+
if (*pos_right == '\n') break;
2225+
++ pos_right;
2226+
}
2227+
string left(pos_left, pos);
2228+
string right(pos, pos_right);
2229+
if (ellipsis_left) left = ellipsis + left;
2230+
if (ellipsis_right) right = right + ellipsis;
2231+
// now pass new message to the more generic error function
2232+
error(msg + prefix + quote(left) + middle + quote(right), pstate);
2233+
}
2234+
21942235
}

parser.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ namespace Sass {
212212
#endif
213213

214214
void error(string msg, Position pos);
215+
// generate message with given and expected sample
216+
// text before and in the middle are configurable
217+
void css_error(const string& msg,
218+
const string& prefix = " after ",
219+
const string& middle = ", was: ");
215220
void read_bom();
216221

217222
Block* parse();

0 commit comments

Comments
 (0)