Skip to content

Commit 349d1ce

Browse files
committed
Support config
1 parent f61008c commit 349d1ce

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

parse-css.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
}
1414
}(this, function (exports) {
1515

16+
const config = {
17+
tokenizeErrorHandler: null,
18+
parseErrorHandler: null,
19+
};
20+
1621
function between(num, first, last) { return num >= first && num <= last; }
1722
function digit(code) { return between(code, 0x30,0x39); }
1823
function hexdigit(code) { return digit(code) || between(code, 0x41,0x46) || between(code, 0x61,0x66); }
@@ -145,7 +150,13 @@ function tokenize(str) {
145150
return codepoint == -1;
146151
};
147152
var donothing = function() {};
148-
var parseerror = function() { console.log("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + ".");return true; };
153+
var parseerror = function() {
154+
if (typeof config.tokenizeErrorHandler === 'function') {
155+
return config.tokenizeErrorHandler(i, code);
156+
}
157+
console.log("Parse error at index " + i + ", processing codepoint 0x" + code.toString(16) + ".");
158+
return true;
159+
};
149160

150161
var consumeAToken = function() {
151162
consumeComments();
@@ -865,8 +876,13 @@ class TokenStream {
865876
}
866877
}
867878

868-
function parseerror(s, msg) {
869-
console.log("Parse error at token " + s.i + ": " + s.tokens[s.i] + ".\n" + msg);
879+
function parseerror(stream, msg) {
880+
const index = stream.i;
881+
const token = stream.tokens[index];
882+
if (typeof config.parseErrorHandler === 'function') {
883+
return config.parseErrorHandler(index, token, msg);
884+
}
885+
console.log("Parse error at token " + index + ": " + token + ".\n" + msg);
870886
return true;
871887
}
872888

@@ -1383,6 +1399,7 @@ function printIndent(level) {
13831399
}
13841400

13851401
return {
1402+
config,
13861403
tokenize,
13871404
IdentToken,
13881405
FunctionToken,

0 commit comments

Comments
 (0)