-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathparser.js
More file actions
361 lines (330 loc) · 9.41 KB
/
parser.js
File metadata and controls
361 lines (330 loc) · 9.41 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
(function (root, factory) {
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
// Rhino, and plain browser loading.
if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory(root);
}
}(this, function (exports) {
function parse(tokens) {
var mode = 'top-level';
var i = -1;
var token;
var stylesheet = new Stylesheet;
var stack = [stylesheet];
var rule = stack[0];
var consume = function(advance) {
if(advance === undefined) advance = 1;
i += advance;
if(i < tokens.length)
token = tokens[i];
else
token = new EOFToken;
return true;
};
var reprocess = function() {
i--;
return true;
}
var next = function() {
return tokens[i+1];
};
var switchto = function(newmode) {
if(newmode === undefined) {
if(rule.fillType !== '')
mode = rule.fillType;
else if(rule.type == 'STYLESHEET')
mode = 'top-level'
else { console.log("Unknown rule-type while switching to current rule's content mode: ",rule); mode = ''; }
} else {
mode = newmode;
}
return true;
}
var push = function(newRule) {
rule = newRule;
stack.push(rule);
return true;
}
var parseerror = function(msg) {
console.log("Parse error at token " + i + ": " + token + ".\n" + msg);
return true;
}
var pop = function() {
var oldrule = stack.pop();
rule = stack[stack.length - 1];
rule.append(oldrule);
return true;
}
var discard = function() {
stack.pop();
rule = stack[stack.length - 1];
return true;
}
var finish = function() {
while(stack.length > 1) {
pop();
}
}
for(;;) {
consume();
if (token.tokenType === 'DELIM' && token.value === '\r')
continue;
switch(mode) {
case "top-level":
switch(token.tokenType) {
case "CDO":
case "CDC":
case "WHITESPACE": break;
case "AT-KEYWORD": push(new AtRule(token.value)) && switchto('at-rule'); break;
case "{": parseerror("Attempt to open a curly-block at top-level.") && consumeAPrimitive(); break;
case "EOF": finish(); return stylesheet;
default: push(new StyleRule) && switchto('selector') && reprocess();
}
break;
case "at-rule":
switch(token.tokenType) {
case ";": pop() && switchto(); break;
case "{":
if(rule.fillType !== '') switchto(rule.fillType);
else parseerror("Attempt to open a curly-block in a statement-type at-rule.") && discard() && switchto('next-block') && reprocess();
break;
case "EOF": finish(); return stylesheet;
default: rule.appendPrelude(consumeAPrimitive());
}
break;
case "rule":
switch(token.tokenType) {
case "WHITESPACE": break;
case "}": pop() && switchto(); break;
case "AT-KEYWORD": push(new AtRule(token.value)) && switchto('at-rule'); break;
case "EOF": finish(); return stylesheet;
default: push(new StyleRule) && switchto('selector') && reprocess();
}
break;
case "selector":
switch(token.tokenType) {
case "{": switchto('declaration'); break;
case "EOF": discard() && finish(); return stylesheet;
default: rule.appendSelector(consumeAPrimitive());
}
break;
case "declaration":
switch(token.tokenType) {
case "WHITESPACE":
case ";": break;
case "}": pop() && switchto(); break;
case "AT-RULE": push(new AtRule(token.value)) && switchto('at-rule'); break;
case "IDENT": push(new Declaration(token.value)) && switchto('after-declaration-name'); break;
case "EOF": finish(); return stylesheet;
default: parseerror() && discard() && switchto('next-declaration');
}
break;
case "after-declaration-name":
switch(token.tokenType) {
case "WHITESPACE": break;
case ":": switchto('declaration-value'); break;
case ";": parseerror("Incomplete declaration - semicolon after property name.") && discard() && switchto(); break;
case "EOF": discard() && finish(); return stylesheet;
default: parseerror("Invalid declaration - additional token after property name") && discard() && switchto('next-declaration');
}
break;
case "declaration-value":
switch(token.tokenType) {
case "DELIM":
if(token.value == "!" && next().tokenType == 'IDENTIFIER' && next().value.toLowerCase() == "important") {
consume();
rule.important = true;
switchto('declaration-end');
} else {
rule.append(token);
}
break;
case ";": pop() && switchto(); break;
case "}": pop() && pop() && switchto(); break;
case "EOF": finish(); return stylesheet;
default: rule.append(consumeAPrimitive());
}
break;
case "declaration-end":
switch(token.tokenType) {
case "WHITESPACE": break;
case ";": pop() && switchto(); break;
case "}": pop() && pop() && switchto(); break;
case "EOF": finish(); return stylesheet;
default: parseerror("Invalid declaration - additional token after !important.") && discard() && switchto('next-declaration');
}
break;
case "next-block":
switch(token.tokenType) {
case "{": consumeAPrimitive() && switchto(); break;
case "EOF": finish(); return stylesheet;
default: consumeAPrimitive(); break;
}
break;
case "next-declaration":
switch(token.tokenType) {
case ";": switchto('declaration'); break;
case "}": switchto('declaration') && reprocess(); break;
case "EOF": finish(); return stylesheet;
default: consumeAPrimitive(); break;
}
break;
default:
// If you hit this, it's because one of the switchto() calls is typo'd.
console.log('Unknown parsing mode: ' + mode);
return;
}
}
function consumeAPrimitive() {
switch(token.tokenType) {
case "(":
case "[":
case "{": return consumeASimpleBlock();
case "FUNCTION": return consumeAFunc();
default: return token;
}
}
function consumeASimpleBlock() {
var endingTokenType = {"(":")", "[":"]", "{":"}"}[token.tokenType];
var block = new SimpleBlock(token.tokenType);
for(;;) {
consume();
switch(token.tokenType) {
case "EOF":
case endingTokenType: return block;
default: block.append(consumeAPrimitive());
}
}
}
function consumeAFunc() {
var func = new Func(token.value);
var arg = new FuncArg();
for(;;) {
consume();
switch(token.tokenType) {
case "EOF":
case ")": func.append(arg); return func;
case "DELIM":
if(token.value == ",") {
func.append(arg);
arg = new FuncArg();
} else {
arg.append(token);
}
break;
default: arg.append(consumeAPrimitive());
}
}
}
}
function CSSParserRule() { return this; }
CSSParserRule.prototype.fillType = '';
CSSParserRule.prototype.toString = function(indent) {
return JSON.stringify(this.toJSON(),null,indent);
}
CSSParserRule.prototype.append = function(val) {
this.value.push(val);
return this;
}
function Stylesheet() {
this.value = [];
return this;
}
Stylesheet.prototype = new CSSParserRule;
Stylesheet.prototype.type = "STYLESHEET";
Stylesheet.prototype.toJSON = function() {
return {type:'stylesheet', value: this.value.map(function(e){return e.toJSON();})};
}
function AtRule(name) {
this.name = name;
this.prelude = [];
this.value = [];
if(name in AtRule.registry)
this.fillType = AtRule.registry[name];
return this;
}
AtRule.prototype = new CSSParserRule;
AtRule.prototype.type = "AT-RULE";
AtRule.prototype.appendPrelude = function(val) {
this.prelude.push(val);
return this;
}
AtRule.prototype.toJSON = function() {
return {type:'at', name:this.name, prelude:this.prelude.map(function(e){return e.toJSON();}), value:this.value.map(function(e){return e.toJSON();})};
}
AtRule.registry = {
'import': '',
'media': 'rule',
'font-face': 'declaration',
'page': 'declaration',
'keyframes': 'rule',
'namespace': '',
'counter-style': 'declaration',
'supports': 'rule',
'document': 'rule',
'font-feature-values': 'declaration',
'viewport': '',
'region-style': 'rule'
};
function StyleRule() {
this.selector = [];
this.value = [];
return this;
}
StyleRule.prototype = new CSSParserRule;
StyleRule.prototype.type = "STYLE-RULE";
StyleRule.prototype.fillType = 'declaration';
StyleRule.prototype.appendSelector = function(val) {
this.selector.push(val);
return this;
}
StyleRule.prototype.toJSON = function() {
return {type:'selector', selector:this.selector.map(function(e){return e.toJSON();}), value:this.value.map(function(e){return e.toJSON();})};
}
function Declaration(name) {
this.name = name;
this.value = [];
return this;
}
Declaration.prototype = new CSSParserRule;
Declaration.prototype.type = "DECLARATION";
Declaration.prototype.toJSON = function() {
return {type:'declaration', name:this.name, value:this.value.map(function(e){return e.toJSON();})};
}
function SimpleBlock(type) {
this.name = type;
this.value = [];
return this;
}
SimpleBlock.prototype = new CSSParserRule;
SimpleBlock.prototype.type = "BLOCK";
SimpleBlock.prototype.toJSON = function() {
return {type:'block', name:this.name, value:this.value.map(function(e){return e.toJSON();})};
}
function Func(name) {
this.name = name;
this.value = [];
return this;
}
Func.prototype = new CSSParserRule;
Func.prototype.type = "FUNCTION";
Func.prototype.toJSON = function() {
return {type:'func', name:this.name, value:this.value.map(function(e){return e.toJSON();})};
}
function FuncArg() {
this.value = [];
return this;
}
FuncArg.prototype = new CSSParserRule;
FuncArg.prototype.type = "FUNCTION-ARG";
FuncArg.prototype.toJSON = function() {
return this.value.map(function(e){return e.toJSON();});
}
// Exportation.
// TODO: also export the various rule objects?
exports.parse = parse;
}));