From abb11c4e3593fad7d2691b7d2838726f4b27ab4c Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Mon, 16 Mar 2015 18:04:46 +0100 Subject: [PATCH] Implement css error function for parser Print context from actual parsed source as ruby sass does! --- debugger.hpp | 2 +- parser.cpp | 35 +++++++++++++++++++++++++++++++++++ parser.hpp | 5 +++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/debugger.hpp b/debugger.hpp index 19a49e27a0..fdd2003c1d 100644 --- a/debugger.hpp +++ b/debugger.hpp @@ -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(node)) { List* expression = dynamic_cast(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() << "] " << diff --git a/parser.cpp b/parser.cpp index 2a229c5592..1300185b7a 100644 --- a/parser.cpp +++ b/parser.cpp @@ -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); + } + } diff --git a/parser.hpp b/parser.hpp index 3dea229af7..e9577ec17e 100644 --- a/parser.hpp +++ b/parser.hpp @@ -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();