1 | var TYPE = require('../../tokenizer').TYPE;
|
---|
2 | var rawMode = require('./Raw').mode;
|
---|
3 |
|
---|
4 | var WHITESPACE = TYPE.WhiteSpace;
|
---|
5 | var COMMENT = TYPE.Comment;
|
---|
6 | var SEMICOLON = TYPE.Semicolon;
|
---|
7 | var ATKEYWORD = TYPE.AtKeyword;
|
---|
8 | var LEFTCURLYBRACKET = TYPE.LeftCurlyBracket;
|
---|
9 | var RIGHTCURLYBRACKET = TYPE.RightCurlyBracket;
|
---|
10 |
|
---|
11 | function consumeRaw(startToken) {
|
---|
12 | return this.Raw(startToken, null, true);
|
---|
13 | }
|
---|
14 | function consumeRule() {
|
---|
15 | return this.parseWithFallback(this.Rule, consumeRaw);
|
---|
16 | }
|
---|
17 | function consumeRawDeclaration(startToken) {
|
---|
18 | return this.Raw(startToken, rawMode.semicolonIncluded, true);
|
---|
19 | }
|
---|
20 | function consumeDeclaration() {
|
---|
21 | if (this.scanner.tokenType === SEMICOLON) {
|
---|
22 | return consumeRawDeclaration.call(this, this.scanner.tokenIndex);
|
---|
23 | }
|
---|
24 |
|
---|
25 | var node = this.parseWithFallback(this.Declaration, consumeRawDeclaration);
|
---|
26 |
|
---|
27 | if (this.scanner.tokenType === SEMICOLON) {
|
---|
28 | this.scanner.next();
|
---|
29 | }
|
---|
30 |
|
---|
31 | return node;
|
---|
32 | }
|
---|
33 |
|
---|
34 | module.exports = {
|
---|
35 | name: 'Block',
|
---|
36 | structure: {
|
---|
37 | children: [[
|
---|
38 | 'Atrule',
|
---|
39 | 'Rule',
|
---|
40 | 'Declaration'
|
---|
41 | ]]
|
---|
42 | },
|
---|
43 | parse: function(isDeclaration) {
|
---|
44 | var consumer = isDeclaration ? consumeDeclaration : consumeRule;
|
---|
45 |
|
---|
46 | var start = this.scanner.tokenStart;
|
---|
47 | var children = this.createList();
|
---|
48 |
|
---|
49 | this.eat(LEFTCURLYBRACKET);
|
---|
50 |
|
---|
51 | scan:
|
---|
52 | while (!this.scanner.eof) {
|
---|
53 | switch (this.scanner.tokenType) {
|
---|
54 | case RIGHTCURLYBRACKET:
|
---|
55 | break scan;
|
---|
56 |
|
---|
57 | case WHITESPACE:
|
---|
58 | case COMMENT:
|
---|
59 | this.scanner.next();
|
---|
60 | break;
|
---|
61 |
|
---|
62 | case ATKEYWORD:
|
---|
63 | children.push(this.parseWithFallback(this.Atrule, consumeRaw));
|
---|
64 | break;
|
---|
65 |
|
---|
66 | default:
|
---|
67 | children.push(consumer.call(this));
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (!this.scanner.eof) {
|
---|
72 | this.eat(RIGHTCURLYBRACKET);
|
---|
73 | }
|
---|
74 |
|
---|
75 | return {
|
---|
76 | type: 'Block',
|
---|
77 | loc: this.getLocation(start, this.scanner.tokenStart),
|
---|
78 | children: children
|
---|
79 | };
|
---|
80 | },
|
---|
81 | generate: function(node) {
|
---|
82 | this.chunk('{');
|
---|
83 | this.children(node, function(prev) {
|
---|
84 | if (prev.type === 'Declaration') {
|
---|
85 | this.chunk(';');
|
---|
86 | }
|
---|
87 | });
|
---|
88 | this.chunk('}');
|
---|
89 | },
|
---|
90 | walkContext: 'block'
|
---|
91 | };
|
---|