|
13 | 13 | }
|
14 | 14 | }(this, function (exports) {
|
15 | 15 |
|
| 16 | +const config = { |
| 17 | + tokenizeErrorHandler: null, |
| 18 | + parseErrorHandler: null, |
| 19 | +}; |
| 20 | + |
16 | 21 | function between(num, first, last) { return num >= first && num <= last; }
|
17 | 22 | function digit(code) { return between(code, 0x30,0x39); }
|
18 | 23 | function hexdigit(code) { return digit(code) || between(code, 0x41,0x46) || between(code, 0x61,0x66); }
|
@@ -145,7 +150,13 @@ function tokenize(str) {
|
145 | 150 | return codepoint == -1;
|
146 | 151 | };
|
147 | 152 | 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 | + }; |
149 | 160 |
|
150 | 161 | var consumeAToken = function() {
|
151 | 162 | consumeComments();
|
@@ -865,8 +876,13 @@ class TokenStream {
|
865 | 876 | }
|
866 | 877 | }
|
867 | 878 |
|
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); |
870 | 886 | return true;
|
871 | 887 | }
|
872 | 888 |
|
@@ -1383,6 +1399,7 @@ function printIndent(level) {
|
1383 | 1399 | }
|
1384 | 1400 |
|
1385 | 1401 | return {
|
| 1402 | + config, |
1386 | 1403 | tokenize,
|
1387 | 1404 | IdentToken,
|
1388 | 1405 | FunctionToken,
|
|
0 commit comments