Debian Codegraph @

parser.js

raw
1 let { AtRule, Comment, Declaration, Root, Rule } = require('postcss')
2
3 module.exports = class Parser {
4 constructor(input) {
5 this.input = input
6
7 this.pos = 0
8 this.root = new Root()
9 this.current = this.root
10 this.spaces = ''
11
12 this.extraIndent = false
13 this.prevIndent = undefined
14 this.step = undefined
15
16 this.root.source = { input, start: { column: 1, line: 1, offset: 0 } }
17 }
18
19 atrule(part) {
20 let atword = part.tokens[0]
21 let params = part.tokens.slice(1)
22
23 let node = new AtRule()
24 node.name = atword[1].slice(1)
25 this.init(node, part)
26
27 if (node.name === '') this.unnamedAtrule(atword)
28
29 while (!part.end && part.lastComma) {
30 this.pos += 1
31 part = this.parts[this.pos]
32 params.push(['space', part.before + part.indent])
33 params = params.concat(part.tokens)
34 }
35
36 node.raws.afterName = this.firstSpaces(params)
37 this.keepTrailingSpace(node, params)
38 this.checkSemicolon(params)
39 this.checkCurly(params)
40 this.raw(node, 'params', params, atword)
41 }
42
43 badProp(token) {
44 let pos = this.getPosition(token[2])
45 this.error('Unexpected separator in property', pos.offset)
46 }
47
48 checkCurly(tokens) {
49 for (let token of tokens) {
50 if (token[0] === '{') {
51 let pos = this.getPosition(token[2])
52 this.error('Unnecessary curly bracket', pos.offset)
53 }
54 }
55 }
56
57 checkSemicolon(tokens) {
58 for (let token of tokens) {
59 if (token[0] === ';') {
60 let pos = this.getPosition(token[2])
61 this.error('Unnecessary semicolon', pos.offset)
62 }
63 }
64 }
65
66 comment(part) {
67 let token = part.tokens[0]
68 let node = new Comment()
69 this.init(node, part)
70 node.source.end = this.getPosition(token[3])
71 this.commentText(node, token)
72 }
73
74 /* Helpers */
75
76 commentText(node, token) {
77 let text = token[1]
78 if (token[4] === 'inline') {
79 node.raws.inline = true
80 text = text.slice(2)
81 } else {
82 text = text.slice(2, -2)
83 }
84
85 let match = text.match(/^(\s*)([^]*\S)(\s*)\n?$/)
86
87 if (match) {
88 node.text = match[2]
89 node.raws.left = match[1]
90 node.raws.inlineRight = match[3]
91 } else {
92 node.text = ''
93 node.raws.left = ''
94 node.raws.inlineRight = ''
95 }
96 }
97
98 decl(part) {
99 let node = new Declaration()
100 this.init(node, part)
101
102 let between = ''
103 let colon = 0
104 let value = []
105 let prop = ''
106 for (let i = 0; i < part.tokens.length; i++) {
107 let token = part.tokens[i]
108 if (token[0] === ':') {
109 between += token[1]
110 colon = token
111 value = part.tokens.slice(i + 1)
112 break
113 } else if (token[0] === 'comment' || token[0] === 'space') {
114 between += token[1]
115 } else if (between !== '') {
116 this.badProp(token)
117 } else {
118 prop += token[1]
119 }
120 }
121
122 if (prop === '') this.unnamedDecl(part.tokens[0])
123 node.prop = prop
124
125 let next = this.parts[this.pos + 1]
126
127 while (
128 !next.end &&
129 !next.atrule &&
130 !next.colon &&
131 next.indent.length > part.indent.length
132 ) {
133 value.push(['space', next.before + next.indent])
134 value = value.concat(next.tokens)
135 this.pos += 1
136 next = this.parts[this.pos + 1]
137 }
138
139 let last = value[value.length - 1]
140 if (last && last[0] === 'comment') {
141 value.pop()
142 let comment = new Comment()
143 this.current.push(comment)
144 comment.source = {
145 end: this.getPosition(last[3]),
146 input: this.input,
147 start: this.getPosition(last[2])
148 }
149 let prev = value[value.length - 1]
150 if (prev && prev[0] === 'space') {
151 value.pop()
152 comment.raws.before = prev[1]
153 }
154 this.commentText(comment, last)
155 }
156
157 for (let i = value.length - 1; i > 0; i--) {
158 let t = value[i][0]
159 if (t === 'word' && value[i][1] === '!important') {
160 node.important = true
161 if (i > 0 && value[i - 1][0] === 'space') {
162 node.raws.important = value[i - 1][1] + '!important'
163 value.splice(i - 1, 2)
164 } else {
165 node.raws.important = '!important'
166 value.splice(i, 1)
167 }
168 break
169 } else if (t !== 'space' && t !== 'newline' && t !== 'comment') {
170 break
171 }
172 }
173
174 node.raws.between = between + this.firstSpaces(value)
175 this.checkSemicolon(value)
176 this.raw(node, 'value', value, colon)
177 }
178
179 error(msg, offset) {
180 let pos = this.getPosition(offset)
181 throw this.input.error(msg, pos.line, pos.column)
182 }
183
184 firstSpaces(tokens) {
185 let result = ''
186 for (let i = 0; i < tokens.length; i++) {
187 if (tokens[i][0] === 'space' || tokens[i][0] === 'newline') {
188 result += tokens.shift()[1]
189 i -= 1
190 } else {
191 break
192 }
193 }
194 return result
195 }
196
197 getPosition(offset) {
198 let pos = this.input.fromOffset(offset)
199 return {
200 column: pos.col,
201 line: pos.line,
202 offset
203 }
204 }
205
206 indent(part) {
207 let indent = part.indent.length
208 let isPrev = typeof this.prevIndent !== 'undefined'
209
210 if (!isPrev && indent) this.indentedFirstLine(part)
211
212 if (!this.step && indent) {
213 this.step = indent
214 this.root.raws.indent = part.indent
215 }
216
217 if (isPrev && this.prevIndent !== indent) {
218 let diff = indent - this.prevIndent
219 if (diff > 0) {
220 if (diff !== this.step) {
221 this.wrongIndent(this.prevIndent + this.step, indent, part)
222 } else if (this.current.last.push) {
223 this.current = this.current.last
224 } else {
225 this.extraIndent = ''
226 for (let i = 0; i < diff; i++) {
227 this.extraIndent += ' '
228 }
229 }
230 } else if (diff % this.step !== 0) {
231 let m = indent + (diff % this.step)
232 this.wrongIndent(`${m} or ${m + this.step}`, indent, part)
233 } else {
234 for (let i = 0; i < -diff / this.step; i++) {
235 this.current = this.current.parent
236 }
237 }
238 }
239
240 this.prevIndent = indent
241 }
242
243 indentedFirstLine(part) {
244 let pos = this.getPosition(part.tokens[0][2])
245 this.error('First line should not have indent', pos.offset)
246 }
247
248 init(node, part) {
249 this.indent(part)
250
251 if (!this.current.nodes) this.current.nodes = []
252 this.current.push(node)
253
254 node.raws.before = part.before + part.indent
255 if (this.extraIndent) {
256 node.raws.extraIndent = this.extraIndent
257 this.extraIndent = false
258 }
259 node.source = {
260 input: this.input,
261 start: this.getPosition(part.tokens[0][2])
262 }
263 }
264
265 keepTrailingSpace(node, tokens) {
266 let lastSpace = tokens[tokens.length - 1]
267 if (lastSpace && lastSpace[0] === 'space') {
268 tokens.pop()
269 node.raws.sssBetween = lastSpace[1]
270 }
271 }
272
273 loop() {
274 let part
275 while (this.pos < this.parts.length) {
276 part = this.parts[this.pos]
277
278 if (part.comment) {
279 this.comment(part)
280 } else if (part.atrule) {
281 this.atrule(part)
282 } else if (part.colon) {
283 let next = this.nextNonComment(this.pos)
284
285 if (next.end || next.atrule) {
286 this.decl(part)
287 } else {
288 let moreIndent = next.indent.length > part.indent.length
289 if (!moreIndent) {
290 this.decl(part)
291 } else if (moreIndent && next.colon) {
292 this.rule(part)
293 } else if (moreIndent && !next.colon) {
294 this.decl(part)
295 }
296 }
297 } else if (part.end) {
298 this.root.raws.after = part.before
299 } else {
300 this.rule(part)
301 }
302
303 this.pos += 1
304 }
305
306 for (let i = this.tokens.length - 1; i >= 0; i--) {
307 if (this.tokens[i].length > 3) {
308 let last = this.tokens[i]
309 this.root.source.end = this.getPosition(last[3])
310 break
311 }
312 }
313 }
314
315 // Errors
316
317 nextNonComment(pos) {
318 let next = pos
319 let part
320 while (next < this.parts.length) {
321 next += 1
322 part = this.parts[next]
323 if (part.end || !part.comment) break
324 }
325 return part
326 }
327
328 raw(node, prop, tokens, altLast) {
329 let token, type
330 let length = tokens.length
331 let value = ''
332 let clean = true
333 for (let i = 0; i < length; i += 1) {
334 token = tokens[i]
335 type = token[0]
336 if (type === 'comment' || (type === 'space' && i === length - 1)) {
337 clean = false
338 } else {
339 value += token[1]
340 }
341 }
342 if (!clean) {
343 let sss = tokens.reduce((all, i) => all + i[1], '')
344 let raw = tokens.reduce((all, i) => {
345 if (i[0] === 'comment' && i[4] === 'inline') {
346 return all + '/* ' + i[1].slice(2).trim() + ' */'
347 } else {
348 return all + i[1]
349 }
350 }, '')
351 node.raws[prop] = { raw, value }
352 if (sss !== raw) node.raws[prop].sss = sss
353 }
354 node[prop] = value
355
356 let last
357 for (let i = tokens.length - 1; i >= 0; i--) {
358 if (tokens[i].length > 2) {
359 last = tokens[i]
360 break
361 }
362 }
363 if (!last) last = altLast
364
365 node.source.end = this.getPosition(last[3])
366 }
367
368 rule(part) {
369 let node = new Rule()
370 this.init(node, part)
371
372 let selector = part.tokens
373 let next = this.parts[this.pos + 1]
374
375 while (!next.end && next.indent.length === part.indent.length) {
376 selector.push(['space', next.before + next.indent])
377 selector = selector.concat(next.tokens)
378 this.pos += 1
379 next = this.parts[this.pos + 1]
380 }
381
382 this.keepTrailingSpace(node, selector)
383 this.checkCurly(selector)
384 this.raw(node, 'selector', selector)
385 }
386
387 unnamedAtrule(token) {
388 let pos = this.getPosition(token[2])
389 this.error('At-rule without name', pos.offset)
390 }
391
392 unnamedDecl(token) {
393 let pos = this.getPosition(token[2])
394 this.error('Declaration without name', pos.offset)
395 }
396
397 wrongIndent(expected, real, part) {
398 let pos = this.getPosition(part.tokens[0][2])
399 let msg = `Expected ${expected} indent, but get ${real}`
400 this.error(msg, pos.offset)
401 }
402 }