source: imaps-frontend/node_modules/css-tree/lib/lexer/error.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.3 KB
Line 
1const createCustomError = require('../utils/createCustomError');
2const generate = require('../definition-syntax/generate');
3const defaultLoc = { offset: 0, line: 1, column: 1 };
4
5function locateMismatch(matchResult, node) {
6 const tokens = matchResult.tokens;
7 const longestMatch = matchResult.longestMatch;
8 const mismatchNode = longestMatch < tokens.length ? tokens[longestMatch].node || null : null;
9 const badNode = mismatchNode !== node ? mismatchNode : null;
10 let mismatchOffset = 0;
11 let mismatchLength = 0;
12 let entries = 0;
13 let css = '';
14 let start;
15 let end;
16
17 for (let i = 0; i < tokens.length; i++) {
18 const token = tokens[i].value;
19
20 if (i === longestMatch) {
21 mismatchLength = token.length;
22 mismatchOffset = css.length;
23 }
24
25 if (badNode !== null && tokens[i].node === badNode) {
26 if (i <= longestMatch) {
27 entries++;
28 } else {
29 entries = 0;
30 }
31 }
32
33 css += token;
34 }
35
36 if (longestMatch === tokens.length || entries > 1) { // last
37 start = fromLoc(badNode || node, 'end') || buildLoc(defaultLoc, css);
38 end = buildLoc(start);
39 } else {
40 start = fromLoc(badNode, 'start') ||
41 buildLoc(fromLoc(node, 'start') || defaultLoc, css.slice(0, mismatchOffset));
42 end = fromLoc(badNode, 'end') ||
43 buildLoc(start, css.substr(mismatchOffset, mismatchLength));
44 }
45
46 return {
47 css,
48 mismatchOffset,
49 mismatchLength,
50 start,
51 end
52 };
53}
54
55function fromLoc(node, point) {
56 const value = node && node.loc && node.loc[point];
57
58 if (value) {
59 return 'line' in value ? buildLoc(value) : value;
60 }
61
62 return null;
63}
64
65function buildLoc({ offset, line, column }, extra) {
66 const loc = {
67 offset,
68 line,
69 column
70 };
71
72 if (extra) {
73 const lines = extra.split(/\n|\r\n?|\f/);
74
75 loc.offset += extra.length;
76 loc.line += lines.length - 1;
77 loc.column = lines.length === 1 ? loc.column + extra.length : lines.pop().length + 1;
78 }
79
80 return loc;
81}
82
83const SyntaxReferenceError = function(type, referenceName) {
84 const error = createCustomError(
85 'SyntaxReferenceError',
86 type + (referenceName ? ' `' + referenceName + '`' : '')
87 );
88
89 error.reference = referenceName;
90
91 return error;
92};
93
94const SyntaxMatchError = function(message, syntax, node, matchResult) {
95 const error = createCustomError('SyntaxMatchError', message);
96 const {
97 css,
98 mismatchOffset,
99 mismatchLength,
100 start,
101 end
102 } = locateMismatch(matchResult, node);
103
104 error.rawMessage = message;
105 error.syntax = syntax ? generate(syntax) : '<generic>';
106 error.css = css;
107 error.mismatchOffset = mismatchOffset;
108 error.mismatchLength = mismatchLength;
109 error.message = message + '\n' +
110 ' syntax: ' + error.syntax + '\n' +
111 ' value: ' + (css || '<empty string>') + '\n' +
112 ' --------' + new Array(error.mismatchOffset + 1).join('-') + '^';
113
114 Object.assign(error, start);
115 error.loc = {
116 source: (node && node.loc && node.loc.source) || '<unknown>',
117 start,
118 end
119 };
120
121 return error;
122};
123
124module.exports = {
125 SyntaxReferenceError,
126 SyntaxMatchError
127};
Note: See TracBrowser for help on using the repository browser.