-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.js
105 lines (92 loc) · 2.5 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
import StreamReader from '@emmetio/stream-reader';
import Token from './tokens/token';
import consumeToken from './tokens/index';
import { last, trimFormatting } from './utils';
/**
* Parses CSS rule selector
* @param {String|StreamReader} source
* @return {Token[]}
*/
export function parseSelector(source) {
return parseList(source, 'selector');
}
/**
* Parses CSS property name. Mostly used for LESS where
* property-like entry might be used as a mixin call
* @param {String|StreamReader} source
* @return {Token}
*/
export function parsePropertyName(source) {
const stream = typeof source === 'string' ? new StreamReader(source) : source;
const items = [];
while (!stream.eof()) {
items.push(consumeToken(stream));
}
let token;
if (items.length === 1) {
token = items[0];
} else {
token = new Token(stream, 'property-name', stream.start, stream.end);
for (let i = 0, il = items.length; i < il; i++) {
token.add(items[i]);
}
}
return token;
}
/**
* Parses CSS property value
* @param {String|StreamReader} source
* @return {Token[]}
*/
export function parsePropertyValue(source) {
return parseList(source);
}
/**
* Parses @media CSS rule expression
* @param {String|StreamReader} source
* @return {Token[]}
*/
export function parseMediaExpression(source) {
return parseList(source);
}
/**
* Parses given source into a set of tokens, separated by comma. Each token contains
* parsed sub-items as independent tokens and so on. Mostly used to parse
* selectors and property values
* @param {String|StreamReader} source Source to parse
* @param {String} [tokenType] Type of first-level tokens.
* Default is `item`
* @return {Token[]}
*/
export function parseList(source, tokenType) {
tokenType = tokenType || 'item';
const stream = typeof source === 'string' ? new StreamReader(source) : source;
const items = [];
const fragments = [];
const flush = () => {
const clean = trimFormatting(fragments);
if (clean.length) {
const item = new Token(stream, tokenType, clean[0].start, last(clean).end);
for (let i = 0; i < clean.length; i++) {
item.add(clean[i]);
}
items.push(item);
}
fragments.length = 0;
};
let token;
while (!stream.eof()) {
if (stream.eat(44 /* , */)) {
flush();
} else if (token = consumeToken(stream)) {
if (token.type !== 'comment') {
fragments.push(token);
}
} else {
throw stream.error('Unexpected character');
}
}
flush();
return items;
}