Skip to content

Commit ae566d1

Browse files
committed
Merge pull request NV#37 from Munter/gh-pages
Improve error information about parse errors
2 parents 9e843b8 + deb43e8 commit ae566d1

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/parse.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ CSSOM.parse = function parse(token) {
5555

5656
var atKeyframesRegExp = /@(-(?:\w+-)+)?keyframes/g;
5757

58+
var parseError = function (msg) {
59+
var tmp = token.substring(0, i).split('\n'),
60+
lines = tmp.length,
61+
chars = tmp.pop().length;
62+
var err = new Error(msg + ' (line ' + lines + ', char ' + chars + ')');
63+
err.line = lines;
64+
err.char = chars;
65+
err.styleSheet = styleSheet;
66+
throw err;
67+
};
68+
69+
5870
for (var character; character = token.charAt(i); i++) {
5971

6072
switch (character) {
@@ -73,7 +85,7 @@ CSSOM.parse = function parse(token) {
7385
case '"':
7486
index = token.indexOf('"', i + 1) + 1;
7587
if (!index) {
76-
throw '" is missing';
88+
parseError('Unmatched "');
7789
}
7890
buffer += token.slice(i, index);
7991
i = index - 1;
@@ -90,7 +102,7 @@ CSSOM.parse = function parse(token) {
90102
case "'":
91103
index = token.indexOf("'", i + 1) + 1;
92104
if (!index) {
93-
throw "' is missing";
105+
parseError("Unmatched '");
94106
}
95107
buffer += token.slice(i, index);
96108
i = index - 1;
@@ -110,7 +122,7 @@ CSSOM.parse = function parse(token) {
110122
i += 2;
111123
index = token.indexOf("*/", i);
112124
if (index === -1) {
113-
throw new SyntaxError("Missing */");
125+
parseError("Missing */");
114126
} else {
115127
i = index + 1;
116128
}
@@ -214,7 +226,7 @@ CSSOM.parse = function parse(token) {
214226
if (state === 'value') {
215227
index = token.indexOf(')', i + 1);
216228
if (index === -1) {
217-
throw i + ': unclosed "("';
229+
parseError('Unmatched "("');
218230
}
219231
buffer += token.slice(i, index + 1);
220232
i = index;
@@ -283,10 +295,10 @@ CSSOM.parse = function parse(token) {
283295
case "selector":
284296
// End of media rule.
285297
if (!parentRule) {
286-
throw "unexpected }";
298+
parseError("Unexpected }");
287299
}
288300
currentScope.__ends = i + 1;
289-
// Nesting rules aren’t supported yet
301+
// Nesting rules aren't supported yet
290302
styleSheet.cssRules.push(currentScope);
291303
currentScope = styleSheet;
292304
parentRule = null;

0 commit comments

Comments
 (0)