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
|
/**
* The approach here owes a LOT to https://github.com/substack/html-tokenize.
*/
var Transform = require('readable-stream').Transform;
var inherits = require('inherits');
inherits(Tokenize, Transform);
module.exports = Tokenize;
var codes = {
fslash: '/'.charCodeAt(0),
bslash: '\\'.charCodeAt(0),
lparen: '('.charCodeAt(0),
rparen: ')'.charCodeAt(0),
lbrace: '{'.charCodeAt(0),
rbrace: '}'.charCodeAt(0),
lbrack: '['.charCodeAt(0),
rbrack: ']'.charCodeAt(0),
squote: '\''.charCodeAt(0),
dquote: '"'.charCodeAt(0),
at: '@'.charCodeAt(0),
semi: ';'.charCodeAt(0),
asterisk: '*'.charCodeAt(0)
}
var sequences = {
comment_start: '/*',
comment_end: '*/'
}
function charCodes(s) { return s.split('').map(function(c){return c.charCodeAt(0)}) }
var sets = {
space: charCodes(' \t\n\r\f'),
open_brackets: [codes.lparen, codes.lbrace, codes.lbrack],
newline: charCodes('\n\r\f')
}
function Tokenize() {
if (!(this instanceof Tokenize)) return new Tokenize;
Transform.call(this);
this._readableState.objectMode = true;
this.state = [['root']];
this._input = null; // buffer the input for read-ahead
this._position = 0; // current position in this._input
this.buffers = []; // collect data for current token
}
Tokenize.prototype._transform = function(buf, enc, next) {
var input = this._input = this._input ? Buffer.concat([ this._input, buf ]) : buf;
for(var i = this._position; i < input.length; i++) {
var c = input[i];
var state = this.state[this.state.length - 1][0],
stateData = this.state[this.state.length - 1][1],
end = null;
// console.log(i, c, this.state);
/* comments */
if(i === input.length - 1
&& (('comment' === state && c === codes.asterisk)
|| c === codes.fslash)) {
// need more data: save unprocessed input and bail out.
this._input = this._input.slice(this._position);
break;
}
else if('comment' !== state
&& c === codes.fslash && input[i+1] === codes.asterisk) {
if('root' !== state) end = [].concat(state);
i--; //backup to save the '/*' for the comment token.
this.state.push(['comment'])
}
else if('comment' === state
&& c === codes.asterisk && input[i+1] === codes.fslash) {
i++;
end = this.state.pop();
}
/* strings */
else if('string' === state
&& c === stateData) {
this.state.pop();
}
else if('string' !== state
&& codes.squote === c || codes.dquote === c) {
this.state.push(['string', c]);
}
/* brackets */
// else if(codes.lparen === c) {
// this.state.push(['brackets', codes.rparen]);
// }
// else if(codes.lbrack === c) {
// this.state.push(['brackets', codes.rbrack]);
// }
// else if('brackets' === state
// && c === stateData) {
// this.state.pop();
// }
/* rules */
else if('rule_start' === state
&& c === codes.lbrace) {
end = this.state.pop();
this.state.push(['rule']);
}
else if('atrule_start' === state
&& c === codes.lbrace) {
end = this.state.pop();
this.state.push(['atrule']);
}
else if(('rule' === state || 'atrule' === state)
&& c === codes.rbrace) {
end = this.state.pop();
i--; // backup to save the ending curly brace for the rule_end token.
this.state.push([ state + '_end' ]);
}
else if(('rule_end' === state || 'atrule_end' === state)
&& c === codes.rbrace) {
end = this.state.pop();
}
else if('root' === state
&& c === codes.at) {
end = ['space'];
i--;
this.state.push(['atrule_start'])
}
// rules can start from the root or nested within at-rules.
else if(sets.space.indexOf(c) < 0)
{
if('root' === state) {
end = ['root'];
i--;
this.state.push(['rule_start'])
}
else if('atrule' === state) {
end = ['atrule'];
i--;
this.state.push(['rule_start']);
}
}
if(end && i >= this._position) {
var out;
this.push(out = [end[0], input.slice(this._position, i+1)]);
this._position = i+1;
end = null;
}
}
if(this._position < this._input.length) {
this._input = this._input.slice(this._position);
this._position = 0;
}
else {
this._input = null;
this._position = 0;
}
next();
}
Tokenize.prototype._flush = function (next) {
if(this._input)
this.push([this.state.pop()[0], this._input.slice(this._position)]);
if(this.state.length !== 0) {
console.warn("[css-tokenize] unfinished business", this.state);
}
this.push(null);
next();
};
|