forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax-scope-map.js
182 lines (156 loc) · 5.83 KB
/
syntax-scope-map.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const parser = require('postcss-selector-parser');
module.exports = class SyntaxScopeMap {
constructor(resultsBySelector) {
this.namedScopeTable = {};
this.anonymousScopeTable = {};
for (let selector in resultsBySelector) {
this.addSelector(selector, resultsBySelector[selector]);
}
setTableDefaults(this.namedScopeTable, true);
setTableDefaults(this.anonymousScopeTable, false);
}
addSelector(selector, result) {
parser(parseResult => {
for (let selectorNode of parseResult.nodes) {
let currentTable = null;
let currentIndexValue = null;
for (let i = selectorNode.nodes.length - 1; i >= 0; i--) {
const termNode = selectorNode.nodes[i];
switch (termNode.type) {
case 'tag':
if (!currentTable) currentTable = this.namedScopeTable;
if (!currentTable[termNode.value])
currentTable[termNode.value] = {};
currentTable = currentTable[termNode.value];
if (currentIndexValue != null) {
if (!currentTable.indices) currentTable.indices = {};
if (!currentTable.indices[currentIndexValue])
currentTable.indices[currentIndexValue] = {};
currentTable = currentTable.indices[currentIndexValue];
currentIndexValue = null;
}
break;
case 'string':
if (!currentTable) currentTable = this.anonymousScopeTable;
const value = termNode.value.slice(1, -1).replace(/\\"/g, '"');
if (!currentTable[value]) currentTable[value] = {};
currentTable = currentTable[value];
if (currentIndexValue != null) {
if (!currentTable.indices) currentTable.indices = {};
if (!currentTable.indices[currentIndexValue])
currentTable.indices[currentIndexValue] = {};
currentTable = currentTable.indices[currentIndexValue];
currentIndexValue = null;
}
break;
case 'universal':
if (currentTable) {
if (!currentTable['*']) currentTable['*'] = {};
currentTable = currentTable['*'];
} else {
if (!this.namedScopeTable['*']) {
this.namedScopeTable['*'] = this.anonymousScopeTable[
'*'
] = {};
}
currentTable = this.namedScopeTable['*'];
}
if (currentIndexValue != null) {
if (!currentTable.indices) currentTable.indices = {};
if (!currentTable.indices[currentIndexValue])
currentTable.indices[currentIndexValue] = {};
currentTable = currentTable.indices[currentIndexValue];
currentIndexValue = null;
}
break;
case 'combinator':
if (currentIndexValue != null) {
rejectSelector(selector);
}
if (termNode.value === '>') {
if (!currentTable.parents) currentTable.parents = {};
currentTable = currentTable.parents;
} else {
rejectSelector(selector);
}
break;
case 'pseudo':
if (termNode.value === ':nth-child') {
currentIndexValue = termNode.nodes[0].nodes[0].value;
} else {
rejectSelector(selector);
}
break;
default:
rejectSelector(selector);
}
}
currentTable.result = result;
}
}).process(selector);
}
get(nodeTypes, childIndices, leafIsNamed = true) {
let result;
let i = nodeTypes.length - 1;
let currentTable = leafIsNamed
? this.namedScopeTable[nodeTypes[i]]
: this.anonymousScopeTable[nodeTypes[i]];
if (!currentTable) currentTable = this.namedScopeTable['*'];
while (currentTable) {
if (currentTable.indices && currentTable.indices[childIndices[i]]) {
currentTable = currentTable.indices[childIndices[i]];
}
if (currentTable.result != null) {
result = currentTable.result;
}
if (i === 0) break;
i--;
currentTable =
currentTable.parents &&
(currentTable.parents[nodeTypes[i]] || currentTable.parents['*']);
}
return result;
}
};
function setTableDefaults(table, allowWildcardSelector) {
const defaultTypeTable = allowWildcardSelector ? table['*'] : null;
for (let type in table) {
let typeTable = table[type];
if (typeTable === defaultTypeTable) continue;
if (defaultTypeTable) {
mergeTable(typeTable, defaultTypeTable);
}
if (typeTable.parents) {
setTableDefaults(typeTable.parents, true);
}
for (let key in typeTable.indices) {
const indexTable = typeTable.indices[key];
mergeTable(indexTable, typeTable, false);
if (indexTable.parents) {
setTableDefaults(indexTable.parents, true);
}
}
}
}
function mergeTable(table, defaultTable, mergeIndices = true) {
if (mergeIndices && defaultTable.indices) {
if (!table.indices) table.indices = {};
for (let key in defaultTable.indices) {
if (!table.indices[key]) table.indices[key] = {};
mergeTable(table.indices[key], defaultTable.indices[key]);
}
}
if (defaultTable.parents) {
if (!table.parents) table.parents = {};
for (let key in defaultTable.parents) {
if (!table.parents[key]) table.parents[key] = {};
mergeTable(table.parents[key], defaultTable.parents[key]);
}
}
if (defaultTable.result != null && table.result == null) {
table.result = defaultTable.result;
}
}
function rejectSelector(selector) {
throw new TypeError(`Unsupported selector '${selector}'`);
}