1
1
import { comment , rule , type AstNode , type Comment , type Declaration , type Rule } from './ast'
2
- import * as Token from './tokens'
2
+
3
+ const BACKSLASH = 0x5c
4
+ const SLASH = 0x2f
5
+ const ASTERISK = 0x2a
6
+ const DOUBLE_QUOTE = 0x22
7
+ const SINGLE_QUOTE = 0x27
8
+ const COLON = 0x3a
9
+ const SEMICOLON = 0x3b
10
+ const LINE_BREAK = 0x0a
11
+ const SPACE = 0x20
12
+ const TAB = 0x09
13
+ const OPEN_CURLY = 0x7b
14
+ const CLOSE_CURLY = 0x7d
15
+ const OPEN_PAREN = 0x28
16
+ const CLOSE_PAREN = 0x29
17
+ const OPEN_BRACKET = 0x5b
18
+ const CLOSE_BRACKET = 0x5d
19
+ const DASH = 0x2d
20
+ const AT_SIGN = 0x40
21
+ const EXCLAMATION_MARK = 0x21
3
22
4
23
export function parse ( input : string ) {
5
24
input = input . replaceAll ( '\r\n' , '\n' )
@@ -30,7 +49,7 @@ export function parse(input: string) {
30
49
// ^
31
50
// ```
32
51
//
33
- if ( currentChar === Token . BACKSLASH ) {
52
+ if ( currentChar === BACKSLASH ) {
34
53
buffer += input . slice ( i , i + 2 )
35
54
i += 1
36
55
}
@@ -51,19 +70,19 @@ export function parse(input: string) {
51
70
// ^^^^^^^^^^^^^
52
71
// }
53
72
// ```
54
- else if ( currentChar === Token . SLASH && input . charCodeAt ( i + 1 ) === Token . ASTERISK ) {
73
+ else if ( currentChar === SLASH && input . charCodeAt ( i + 1 ) === ASTERISK ) {
55
74
let start = i
56
75
57
76
for ( let j = i + 2 ; j < input . length ; j ++ ) {
58
77
peekChar = input . charCodeAt ( j )
59
78
60
79
// Current character is a `\` therefore the next character is escaped.
61
- if ( peekChar === Token . BACKSLASH ) {
80
+ if ( peekChar === BACKSLASH ) {
62
81
j += 1
63
82
}
64
83
65
84
// End of the comment
66
- else if ( peekChar === Token . ASTERISK && input . charCodeAt ( j + 1 ) === Token . SLASH ) {
85
+ else if ( peekChar === ASTERISK && input . charCodeAt ( j + 1 ) === SLASH ) {
67
86
i = j + 1
68
87
break
69
88
}
@@ -73,13 +92,13 @@ export function parse(input: string) {
73
92
74
93
// Collect all license comments so that we can hoist them to the top of
75
94
// the AST.
76
- if ( commentString . charCodeAt ( 2 ) === Token . EXCLAMATION_MARK ) {
95
+ if ( commentString . charCodeAt ( 2 ) === EXCLAMATION_MARK ) {
77
96
licenseComments . push ( comment ( commentString . slice ( 2 , - 2 ) ) )
78
97
}
79
98
}
80
99
81
100
// Start of a string.
82
- else if ( currentChar === Token . SINGLE_QUOTE || currentChar === Token . DOUBLE_QUOTE ) {
101
+ else if ( currentChar === SINGLE_QUOTE || currentChar === DOUBLE_QUOTE ) {
83
102
let start = i
84
103
85
104
// We need to ensure that the closing quote is the same as the opening
@@ -96,7 +115,7 @@ export function parse(input: string) {
96
115
for ( let j = i + 1 ; j < input . length ; j ++ ) {
97
116
peekChar = input . charCodeAt ( j )
98
117
// Current character is a `\` therefore the next character is escaped.
99
- if ( peekChar === Token . BACKSLASH ) {
118
+ if ( peekChar === BACKSLASH ) {
100
119
j += 1
101
120
}
102
121
@@ -116,7 +135,7 @@ export function parse(input: string) {
116
135
// ^ Missing "
117
136
// }
118
137
// ```
119
- else if ( peekChar === Token . SEMICOLON && input . charCodeAt ( j + 1 ) === Token . LINE_BREAK ) {
138
+ else if ( peekChar === SEMICOLON && input . charCodeAt ( j + 1 ) === LINE_BREAK ) {
120
139
throw new Error (
121
140
`Unterminated string: ${ input . slice ( start , j + 1 ) + String . fromCharCode ( currentChar ) } ` ,
122
141
)
@@ -132,7 +151,7 @@ export function parse(input: string) {
132
151
// ^ Missing "
133
152
// }
134
153
// ```
135
- else if ( peekChar === Token . LINE_BREAK ) {
154
+ else if ( peekChar === LINE_BREAK ) {
136
155
throw new Error (
137
156
`Unterminated string: ${ input . slice ( start , j ) + String . fromCharCode ( currentChar ) } ` ,
138
157
)
@@ -146,21 +165,19 @@ export function parse(input: string) {
146
165
// Skip whitespace if the next character is also whitespace. This allows us
147
166
// to reduce the amount of whitespace in the AST.
148
167
else if (
149
- ( currentChar === Token . SPACE ||
150
- currentChar === Token . LINE_BREAK ||
151
- currentChar === Token . TAB ) &&
168
+ ( currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB ) &&
152
169
( peekChar = input . charCodeAt ( i + 1 ) ) &&
153
- ( peekChar === Token . SPACE || peekChar === Token . LINE_BREAK || peekChar === Token . TAB )
170
+ ( peekChar === SPACE || peekChar === LINE_BREAK || peekChar === TAB )
154
171
) {
155
172
continue
156
173
}
157
174
158
175
// Replace new lines with spaces.
159
- else if ( currentChar === Token . LINE_BREAK ) {
176
+ else if ( currentChar === LINE_BREAK ) {
160
177
if ( buffer . length === 0 ) continue
161
178
162
179
peekChar = buffer . charCodeAt ( buffer . length - 1 )
163
- if ( peekChar !== Token . SPACE && peekChar !== Token . LINE_BREAK && peekChar !== Token . TAB ) {
180
+ if ( peekChar !== SPACE && peekChar !== LINE_BREAK && peekChar !== TAB ) {
164
181
buffer += ' '
165
182
}
166
183
}
@@ -171,11 +188,7 @@ export function parse(input: string) {
171
188
// character, even `;` and ` }`. Therefore we have to make sure that we are
172
189
// at the correct "end" of the custom property by making sure everything is
173
190
// balanced.
174
- else if (
175
- currentChar === Token . DASH &&
176
- input . charCodeAt ( i + 1 ) === Token . DASH &&
177
- buffer . length === 0
178
- ) {
191
+ else if ( currentChar === DASH && input . charCodeAt ( i + 1 ) === DASH && buffer . length === 0 ) {
179
192
let closingBracketStack = ''
180
193
181
194
let start = i
@@ -185,45 +198,45 @@ export function parse(input: string) {
185
198
peekChar = input . charCodeAt ( j )
186
199
187
200
// Current character is a `\` therefore the next character is escaped.
188
- if ( peekChar === Token . BACKSLASH ) {
201
+ if ( peekChar === BACKSLASH ) {
189
202
j += 1
190
203
}
191
204
192
205
// Start of a comment.
193
- else if ( peekChar === Token . SLASH && input . charCodeAt ( j + 1 ) === Token . ASTERISK ) {
206
+ else if ( peekChar === SLASH && input . charCodeAt ( j + 1 ) === ASTERISK ) {
194
207
for ( let k = j + 2 ; k < input . length ; k ++ ) {
195
208
peekChar = input . charCodeAt ( k )
196
209
// Current character is a `\` therefore the next character is escaped.
197
- if ( peekChar === Token . BACKSLASH ) {
210
+ if ( peekChar === BACKSLASH ) {
198
211
k += 1
199
212
}
200
213
201
214
// End of the comment
202
- else if ( peekChar === Token . ASTERISK && input . charCodeAt ( k + 1 ) === Token . SLASH ) {
215
+ else if ( peekChar === ASTERISK && input . charCodeAt ( k + 1 ) === SLASH ) {
203
216
j = k + 1
204
217
break
205
218
}
206
219
}
207
220
}
208
221
209
222
// End of the "property" of the property-value pair.
210
- else if ( colonIdx === - 1 && peekChar === Token . COLON ) {
223
+ else if ( colonIdx === - 1 && peekChar === COLON ) {
211
224
colonIdx = buffer . length + j - start
212
225
}
213
226
214
227
// End of the custom property.
215
- else if ( peekChar === Token . SEMICOLON && closingBracketStack . length === 0 ) {
228
+ else if ( peekChar === SEMICOLON && closingBracketStack . length === 0 ) {
216
229
buffer += input . slice ( start , j )
217
230
i = j
218
231
break
219
232
}
220
233
221
234
// Start of a block.
222
- else if ( peekChar === Token . OPEN_PAREN ) {
235
+ else if ( peekChar === OPEN_PAREN ) {
223
236
closingBracketStack += ')'
224
- } else if ( peekChar === Token . OPEN_BRACKET ) {
237
+ } else if ( peekChar === OPEN_BRACKET ) {
225
238
closingBracketStack += ']'
226
- } else if ( peekChar === Token . OPEN_CURLY ) {
239
+ } else if ( peekChar === OPEN_CURLY ) {
227
240
closingBracketStack += '}'
228
241
}
229
242
@@ -239,7 +252,7 @@ export function parse(input: string) {
239
252
// }
240
253
// ```
241
254
else if (
242
- ( peekChar === Token . CLOSE_CURLY || input . length - 1 === j ) &&
255
+ ( peekChar === CLOSE_CURLY || input . length - 1 === j ) &&
243
256
closingBracketStack . length === 0
244
257
) {
245
258
i = j - 1
@@ -249,9 +262,9 @@ export function parse(input: string) {
249
262
250
263
// End of a block.
251
264
else if (
252
- peekChar === Token . CLOSE_PAREN ||
253
- peekChar === Token . CLOSE_BRACKET ||
254
- peekChar === Token . CLOSE_CURLY
265
+ peekChar === CLOSE_PAREN ||
266
+ peekChar === CLOSE_BRACKET ||
267
+ peekChar === CLOSE_CURLY
255
268
) {
256
269
if (
257
270
closingBracketStack . length > 0 &&
@@ -280,7 +293,7 @@ export function parse(input: string) {
280
293
// @charset "UTF-8";
281
294
// ^
282
295
// ```
283
- else if ( currentChar === Token . SEMICOLON && buffer . charCodeAt ( 0 ) === Token . AT_SIGN ) {
296
+ else if ( currentChar === SEMICOLON && buffer . charCodeAt ( 0 ) === AT_SIGN ) {
284
297
node = rule ( buffer , [ ] )
285
298
286
299
// At-rule is nested inside of a rule, attach it to the parent.
@@ -309,7 +322,7 @@ export function parse(input: string) {
309
322
// }
310
323
// ```
311
324
//
312
- else if ( currentChar === Token . SEMICOLON ) {
325
+ else if ( currentChar === SEMICOLON ) {
313
326
let declaration = parseDeclaration ( buffer )
314
327
if ( parent ) {
315
328
parent . nodes . push ( declaration )
@@ -321,7 +334,7 @@ export function parse(input: string) {
321
334
}
322
335
323
336
// Start of a block.
324
- else if ( currentChar === Token . OPEN_CURLY ) {
337
+ else if ( currentChar === OPEN_CURLY ) {
325
338
closingBracketStack += '}'
326
339
327
340
// At this point `buffer` should resemble a selector or an at-rule.
@@ -346,7 +359,7 @@ export function parse(input: string) {
346
359
}
347
360
348
361
// End of a block.
349
- else if ( currentChar === Token . CLOSE_CURLY ) {
362
+ else if ( currentChar === CLOSE_CURLY ) {
350
363
if ( closingBracketStack === '' ) {
351
364
throw new Error ( 'Missing opening {' )
352
365
}
@@ -367,7 +380,7 @@ export function parse(input: string) {
367
380
// ^
368
381
// }
369
382
// ```
370
- if ( buffer . charCodeAt ( 0 ) === Token . AT_SIGN ) {
383
+ if ( buffer . charCodeAt ( 0 ) === AT_SIGN ) {
371
384
node = rule ( buffer . trim ( ) , [ ] )
372
385
373
386
// At-rule is nested inside of a rule, attach it to the parent.
@@ -439,9 +452,7 @@ export function parse(input: string) {
439
452
// Skip whitespace at the start of a new node.
440
453
if (
441
454
buffer . length === 0 &&
442
- ( currentChar === Token . SPACE ||
443
- currentChar === Token . LINE_BREAK ||
444
- currentChar === Token . TAB )
455
+ ( currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB )
445
456
) {
446
457
continue
447
458
}
0 commit comments