Skip to content

Commit dc7484c

Browse files
committed
Implement css error function for parser
Print context from actual parsed source as ruby sass does!
1 parent dea27e5 commit dc7484c

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,4 +2187,39 @@ namespace Sass {
21872187
throw Sass_Error(Sass_Error::syntax, ParserState(path, pos.line ? pos : before_token, Offset(0, 0)), msg);
21882188
}
21892189

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

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)