.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-selector-parser / dist / tokenize.js
1 'use strict';
2
3 exports.__esModule = true;
4 exports.default = tokenize;
5
6 var _tokenTypes = require('./tokenTypes');
7
8 var t = _interopRequireWildcard(_tokenTypes);
9
10 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
11
12 var wordEnd = /[ \n\t\r\(\)\*:;!&'"\+\|~>,=$^\[\]\\]|\/(?=\*)/g;
13
14 function tokenize(input) {
15     var tokens = [];
16     var css = input.css.valueOf();
17     var _css = css,
18         length = _css.length;
19
20     var offset = -1;
21     var line = 1;
22     var start = 0;
23     var end = 0;
24
25     var code = void 0,
26         content = void 0,
27         endColumn = void 0,
28         endLine = void 0,
29         escaped = void 0,
30         escapePos = void 0,
31         last = void 0,
32         lines = void 0,
33         next = void 0,
34         nextLine = void 0,
35         nextOffset = void 0,
36         quote = void 0,
37         tokenType = void 0;
38
39     function unclosed(what, fix) {
40         if (input.safe) {
41             // fyi: this is never set to true.
42             css += fix;
43             next = css.length - 1;
44         } else {
45             throw input.error('Unclosed ' + what, line, start - offset, start);
46         }
47     }
48
49     while (start < length) {
50         code = css.charCodeAt(start);
51
52         if (code === t.newline) {
53             offset = start;
54             line += 1;
55         }
56
57         switch (code) {
58             case t.newline:
59             case t.space:
60             case t.tab:
61             case t.cr:
62             case t.feed:
63                 next = start;
64                 do {
65                     next += 1;
66                     code = css.charCodeAt(next);
67                     if (code === t.newline) {
68                         offset = next;
69                         line += 1;
70                     }
71                 } while (code === t.space || code === t.newline || code === t.tab || code === t.cr || code === t.feed);
72
73                 tokenType = t.space;
74                 endLine = line;
75                 endColumn = start - offset;
76                 end = next;
77                 break;
78
79             case t.plus:
80             case t.greaterThan:
81             case t.tilde:
82             case t.pipe:
83                 next = start;
84                 do {
85                     next += 1;
86                     code = css.charCodeAt(next);
87                 } while (code === t.plus || code === t.greaterThan || code === t.tilde || code === t.pipe);
88
89                 tokenType = t.combinator;
90                 endLine = line;
91                 endColumn = start - offset;
92                 end = next;
93                 break;
94
95             // Consume these characters as single tokens.
96             case t.asterisk:
97             case t.ampersand:
98             case t.comma:
99             case t.equals:
100             case t.dollar:
101             case t.caret:
102             case t.openSquare:
103             case t.closeSquare:
104             case t.colon:
105             case t.semicolon:
106             case t.openParenthesis:
107             case t.closeParenthesis:
108                 next = start;
109                 tokenType = code;
110                 endLine = line;
111                 endColumn = start - offset;
112                 end = next + 1;
113                 break;
114
115             case t.singleQuote:
116             case t.doubleQuote:
117                 quote = code === t.singleQuote ? "'" : '"';
118                 next = start;
119                 do {
120                     escaped = false;
121                     next = css.indexOf(quote, next + 1);
122                     if (next === -1) {
123                         unclosed('quote', quote);
124                     }
125                     escapePos = next;
126                     while (css.charCodeAt(escapePos - 1) === t.backslash) {
127                         escapePos -= 1;
128                         escaped = !escaped;
129                     }
130                 } while (escaped);
131
132                 tokenType = t.str;
133                 endLine = line;
134                 endColumn = start - offset;
135                 end = next + 1;
136                 break;
137
138             case t.backslash:
139                 next = start;
140                 escaped = true;
141                 while (css.charCodeAt(next + 1) === t.backslash) {
142                     next += 1;
143                     escaped = !escaped;
144                 }
145                 code = css.charCodeAt(next + 1);
146                 if (escaped && code !== t.slash && code !== t.space && code !== t.newline && code !== t.tab && code !== t.cr && code !== t.feed) {
147                     next += 1;
148                 }
149
150                 tokenType = t.word;
151                 endLine = line;
152                 endColumn = next - offset;
153                 end = next + 1;
154                 break;
155
156             default:
157                 if (code === t.slash && css.charCodeAt(start + 1) === t.asterisk) {
158                     next = css.indexOf('*/', start + 2) + 1;
159                     if (next === 0) {
160                         unclosed('comment', '*/');
161                     }
162
163                     content = css.slice(start, next + 1);
164                     lines = content.split('\n');
165                     last = lines.length - 1;
166
167                     if (last > 0) {
168                         nextLine = line + last;
169                         nextOffset = next - lines[last].length;
170                     } else {
171                         nextLine = line;
172                         nextOffset = offset;
173                     }
174
175                     tokenType = t.comment;
176                     line = nextLine;
177                     endLine = nextLine;
178                     endColumn = next - nextOffset;
179                 } else {
180                     wordEnd.lastIndex = start + 1;
181                     wordEnd.test(css);
182                     if (wordEnd.lastIndex === 0) {
183                         next = css.length - 1;
184                     } else {
185                         next = wordEnd.lastIndex - 2;
186                     }
187
188                     tokenType = t.word;
189                     endLine = line;
190                     endColumn = next - offset;
191                 }
192
193                 end = next + 1;
194                 break;
195         }
196
197         // Ensure that the token structure remains consistent
198         tokens.push([tokenType, // [0] Token type
199         line, // [1] Starting line
200         start - offset, // [2] Starting column
201         endLine, // [3] Ending line
202         endColumn, // [4] Ending column
203         start, // [5] Start position / Source index
204         end] // [6] End position
205         );
206
207         // Reset offset for the next token
208         if (nextOffset) {
209             offset = nextOffset;
210             nextOffset = null;
211         }
212
213         start = end;
214     }
215
216     return tokens;
217 }
218 module.exports = exports['default'];