diff --git a/parse-css.js b/parse-css.js index f5932f9..f260388 100644 --- a/parse-css.js +++ b/parse-css.js @@ -13,6 +13,11 @@ } }(this, function (exports) { +const config = { + tokenizeErrorHandler: null, + parseErrorHandler: null, +}; + function between(num, first, last) { return num >= first && num <= last; } function digit(code) { return between(code, 0x30,0x39); } function hexdigit(code) { return digit(code) || between(code, 0x41,0x46) || between(code, 0x61,0x66); } @@ -145,7 +150,13 @@ function tokenize(str) { return codepoint == -1; }; var donothing = function() {}; - var parseerror = function() { console.log("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + ".");return true; }; + var parseerror = function() { + if (typeof config.tokenizeErrorHandler === 'function') { + return config.tokenizeErrorHandler(i, code); + } + console.log("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + "."); + return true; + }; var consumeAToken = function() { consumeComments(); @@ -865,8 +876,13 @@ class TokenStream { } } -function parseerror(s, msg) { - console.log("Parse error at token " + s.i + ": " + s.tokens[s.i] + ".\n" + msg); +function parseerror(stream, msg) { + const index = stream.i; + const token = stream.tokens[index]; + if (typeof config.parseErrorHandler === 'function') { + return config.parseErrorHandler(index, token, msg); + } + console.log("Parse error at token " + index + ": " + token + ".\n" + msg); return true; } @@ -1383,6 +1399,7 @@ function printIndent(level) { } return { + config, tokenize, IdentToken, FunctionToken,