Skip to content

Commit

Permalink
Support config
Browse files Browse the repository at this point in the history
  • Loading branch information
danny0838 committed Apr 4, 2024
1 parent f61008c commit 349d1ce
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions parse-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1383,6 +1399,7 @@ function printIndent(level) {
}

return {
config,
tokenize,
IdentToken,
FunctionToken,
Expand Down

0 comments on commit 349d1ce

Please sign in to comment.