Skip to content

Commit

Permalink
Implement css error function for parser
Browse files Browse the repository at this point in the history
Print context from actual parsed source as ruby sass does!
  • Loading branch information
mgreter committed Mar 17, 2015
1 parent 1b1fc93 commit abb11c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ inline void debug_ast(AST_Node* node, string ind = "", Env* env = 0)
cerr << ind << "Map " << expression << " [Hashed]" << endl;
} else if (dynamic_cast<List*>(node)) {
List* expression = dynamic_cast<List*>(node);
cerr << ind << "List " << expression <<
cerr << ind << "List " << expression << " (" << expression->length() << ") " <<
(expression->separator() == Sass::List::Separator::COMMA ? "Comma " : "Space ") <<
" [delayed: " << expression->is_delayed() << "] " <<
" [interpolant: " << expression->is_interpolant() << "] " <<
Expand Down
35 changes: 35 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2187,4 +2187,39 @@ namespace Sass {
throw Sass_Error(Sass_Error::syntax, ParserState(path, pos.line ? pos : before_token, Offset(0, 0)), msg);
}

// print a css parsing error with actual context information from parsed source
void Parser::css_error(const string& msg, const string& prefix, const string& middle)
{
int max_len = 14;
const char* pos = peek < optional_spaces >();
bool ellipsis_left = false;
const char* pos_left(pos);
while (*pos_left && pos_left >= source) {
if (pos - pos_left > max_len) {
ellipsis_left = true;
break;
}
if (*pos_left == '\r') break;
if (*pos_left == '\n') break;
-- pos_left;
}
bool ellipsis_right = false;
const char* pos_right(pos);
while (*pos_right && pos_right <= end) {
if (pos_right - pos > max_len) {
ellipsis_right = true;
break;
}
if (*pos_right == '\r') break;
if (*pos_right == '\n') break;
++ pos_right;
}
string left(pos_left, pos);
string right(pos, pos_right);
if (ellipsis_left) left = ellipsis + left;
if (ellipsis_right) right = right + ellipsis;
// now pass new message to the more generic error function
error(msg + prefix + quote(left) + middle + quote(right), pstate);
}

}
5 changes: 5 additions & 0 deletions parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ namespace Sass {
#endif

void error(string msg, Position pos);
// generate message with given and expected sample
// text before and in the middle are configurable
void css_error(const string& msg,
const string& prefix = " after ",
const string& middle = ", was: ");
void read_bom();

Block* parse();
Expand Down

0 comments on commit abb11c4

Please sign in to comment.