11import { 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
322
423export function parse ( input : string ) {
524 input = input . replaceAll ( '\r\n' , '\n' )
@@ -30,7 +49,7 @@ export function parse(input: string) {
3049 // ^
3150 // ```
3251 //
33- if ( currentChar === Token . BACKSLASH ) {
52+ if ( currentChar === BACKSLASH ) {
3453 buffer += input . slice ( i , i + 2 )
3554 i += 1
3655 }
@@ -51,19 +70,19 @@ export function parse(input: string) {
5170 // ^^^^^^^^^^^^^
5271 // }
5372 // ```
54- else if ( currentChar === Token . SLASH && input . charCodeAt ( i + 1 ) === Token . ASTERISK ) {
73+ else if ( currentChar === SLASH && input . charCodeAt ( i + 1 ) === ASTERISK ) {
5574 let start = i
5675
5776 for ( let j = i + 2 ; j < input . length ; j ++ ) {
5877 peekChar = input . charCodeAt ( j )
5978
6079 // Current character is a `\` therefore the next character is escaped.
61- if ( peekChar === Token . BACKSLASH ) {
80+ if ( peekChar === BACKSLASH ) {
6281 j += 1
6382 }
6483
6584 // 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 ) {
6786 i = j + 1
6887 break
6988 }
@@ -73,13 +92,13 @@ export function parse(input: string) {
7392
7493 // Collect all license comments so that we can hoist them to the top of
7594 // the AST.
76- if ( commentString . charCodeAt ( 2 ) === Token . EXCLAMATION_MARK ) {
95+ if ( commentString . charCodeAt ( 2 ) === EXCLAMATION_MARK ) {
7796 licenseComments . push ( comment ( commentString . slice ( 2 , - 2 ) ) )
7897 }
7998 }
8099
81100 // Start of a string.
82- else if ( currentChar === Token . SINGLE_QUOTE || currentChar === Token . DOUBLE_QUOTE ) {
101+ else if ( currentChar === SINGLE_QUOTE || currentChar === DOUBLE_QUOTE ) {
83102 let start = i
84103
85104 // We need to ensure that the closing quote is the same as the opening
@@ -96,7 +115,7 @@ export function parse(input: string) {
96115 for ( let j = i + 1 ; j < input . length ; j ++ ) {
97116 peekChar = input . charCodeAt ( j )
98117 // Current character is a `\` therefore the next character is escaped.
99- if ( peekChar === Token . BACKSLASH ) {
118+ if ( peekChar === BACKSLASH ) {
100119 j += 1
101120 }
102121
@@ -116,7 +135,7 @@ export function parse(input: string) {
116135 // ^ Missing "
117136 // }
118137 // ```
119- else if ( peekChar === Token . SEMICOLON && input . charCodeAt ( j + 1 ) === Token . LINE_BREAK ) {
138+ else if ( peekChar === SEMICOLON && input . charCodeAt ( j + 1 ) === LINE_BREAK ) {
120139 throw new Error (
121140 `Unterminated string: ${ input . slice ( start , j + 1 ) + String . fromCharCode ( currentChar ) } ` ,
122141 )
@@ -132,7 +151,7 @@ export function parse(input: string) {
132151 // ^ Missing "
133152 // }
134153 // ```
135- else if ( peekChar === Token . LINE_BREAK ) {
154+ else if ( peekChar === LINE_BREAK ) {
136155 throw new Error (
137156 `Unterminated string: ${ input . slice ( start , j ) + String . fromCharCode ( currentChar ) } ` ,
138157 )
@@ -146,21 +165,19 @@ export function parse(input: string) {
146165 // Skip whitespace if the next character is also whitespace. This allows us
147166 // to reduce the amount of whitespace in the AST.
148167 else if (
149- ( currentChar === Token . SPACE ||
150- currentChar === Token . LINE_BREAK ||
151- currentChar === Token . TAB ) &&
168+ ( currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB ) &&
152169 ( peekChar = input . charCodeAt ( i + 1 ) ) &&
153- ( peekChar === Token . SPACE || peekChar === Token . LINE_BREAK || peekChar === Token . TAB )
170+ ( peekChar === SPACE || peekChar === LINE_BREAK || peekChar === TAB )
154171 ) {
155172 continue
156173 }
157174
158175 // Replace new lines with spaces.
159- else if ( currentChar === Token . LINE_BREAK ) {
176+ else if ( currentChar === LINE_BREAK ) {
160177 if ( buffer . length === 0 ) continue
161178
162179 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 ) {
164181 buffer += ' '
165182 }
166183 }
@@ -171,11 +188,7 @@ export function parse(input: string) {
171188 // character, even `;` and ` }`. Therefore we have to make sure that we are
172189 // at the correct "end" of the custom property by making sure everything is
173190 // 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 ) {
179192 let closingBracketStack = ''
180193
181194 let start = i
@@ -185,45 +198,45 @@ export function parse(input: string) {
185198 peekChar = input . charCodeAt ( j )
186199
187200 // Current character is a `\` therefore the next character is escaped.
188- if ( peekChar === Token . BACKSLASH ) {
201+ if ( peekChar === BACKSLASH ) {
189202 j += 1
190203 }
191204
192205 // 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 ) {
194207 for ( let k = j + 2 ; k < input . length ; k ++ ) {
195208 peekChar = input . charCodeAt ( k )
196209 // Current character is a `\` therefore the next character is escaped.
197- if ( peekChar === Token . BACKSLASH ) {
210+ if ( peekChar === BACKSLASH ) {
198211 k += 1
199212 }
200213
201214 // 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 ) {
203216 j = k + 1
204217 break
205218 }
206219 }
207220 }
208221
209222 // End of the "property" of the property-value pair.
210- else if ( colonIdx === - 1 && peekChar === Token . COLON ) {
223+ else if ( colonIdx === - 1 && peekChar === COLON ) {
211224 colonIdx = buffer . length + j - start
212225 }
213226
214227 // End of the custom property.
215- else if ( peekChar === Token . SEMICOLON && closingBracketStack . length === 0 ) {
228+ else if ( peekChar === SEMICOLON && closingBracketStack . length === 0 ) {
216229 buffer += input . slice ( start , j )
217230 i = j
218231 break
219232 }
220233
221234 // Start of a block.
222- else if ( peekChar === Token . OPEN_PAREN ) {
235+ else if ( peekChar === OPEN_PAREN ) {
223236 closingBracketStack += ')'
224- } else if ( peekChar === Token . OPEN_BRACKET ) {
237+ } else if ( peekChar === OPEN_BRACKET ) {
225238 closingBracketStack += ']'
226- } else if ( peekChar === Token . OPEN_CURLY ) {
239+ } else if ( peekChar === OPEN_CURLY ) {
227240 closingBracketStack += '}'
228241 }
229242
@@ -239,7 +252,7 @@ export function parse(input: string) {
239252 // }
240253 // ```
241254 else if (
242- ( peekChar === Token . CLOSE_CURLY || input . length - 1 === j ) &&
255+ ( peekChar === CLOSE_CURLY || input . length - 1 === j ) &&
243256 closingBracketStack . length === 0
244257 ) {
245258 i = j - 1
@@ -249,9 +262,9 @@ export function parse(input: string) {
249262
250263 // End of a block.
251264 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
255268 ) {
256269 if (
257270 closingBracketStack . length > 0 &&
@@ -280,7 +293,7 @@ export function parse(input: string) {
280293 // @charset "UTF-8";
281294 // ^
282295 // ```
283- else if ( currentChar === Token . SEMICOLON && buffer . charCodeAt ( 0 ) === Token . AT_SIGN ) {
296+ else if ( currentChar === SEMICOLON && buffer . charCodeAt ( 0 ) === AT_SIGN ) {
284297 node = rule ( buffer , [ ] )
285298
286299 // At-rule is nested inside of a rule, attach it to the parent.
@@ -309,7 +322,7 @@ export function parse(input: string) {
309322 // }
310323 // ```
311324 //
312- else if ( currentChar === Token . SEMICOLON ) {
325+ else if ( currentChar === SEMICOLON ) {
313326 let declaration = parseDeclaration ( buffer )
314327 if ( parent ) {
315328 parent . nodes . push ( declaration )
@@ -321,7 +334,7 @@ export function parse(input: string) {
321334 }
322335
323336 // Start of a block.
324- else if ( currentChar === Token . OPEN_CURLY ) {
337+ else if ( currentChar === OPEN_CURLY ) {
325338 closingBracketStack += '}'
326339
327340 // At this point `buffer` should resemble a selector or an at-rule.
@@ -346,7 +359,7 @@ export function parse(input: string) {
346359 }
347360
348361 // End of a block.
349- else if ( currentChar === Token . CLOSE_CURLY ) {
362+ else if ( currentChar === CLOSE_CURLY ) {
350363 if ( closingBracketStack === '' ) {
351364 throw new Error ( 'Missing opening {' )
352365 }
@@ -367,7 +380,7 @@ export function parse(input: string) {
367380 // ^
368381 // }
369382 // ```
370- if ( buffer . charCodeAt ( 0 ) === Token . AT_SIGN ) {
383+ if ( buffer . charCodeAt ( 0 ) === AT_SIGN ) {
371384 node = rule ( buffer . trim ( ) , [ ] )
372385
373386 // At-rule is nested inside of a rule, attach it to the parent.
@@ -439,9 +452,7 @@ export function parse(input: string) {
439452 // Skip whitespace at the start of a new node.
440453 if (
441454 buffer . length === 0 &&
442- ( currentChar === Token . SPACE ||
443- currentChar === Token . LINE_BREAK ||
444- currentChar === Token . TAB )
455+ ( currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB )
445456 ) {
446457 continue
447458 }
0 commit comments