| 62 | } |
| 63 | |
| 64 | export function parse(input: string, opts?: ParseOptions) { |
| 65 | let source: Source | null = opts?.from ? { file: opts.from, code: input } : null |
| 66 | |
| 67 | class="cm">// Note: it is important that any transformations of the input string |
| 68 | class="cm">// *before* processing do NOT change the length of the string. This |
| 69 | class="cm">// would invalidate the mechanism used to track source locations. |
| 70 | if (input[0] === class="st">'\uFEFF') input = class="st">' ' + input.slice(1) |
| 71 | |
| 72 | let ast: AstNode[] = [] |
| 73 | let licenseComments: Comment[] = [] |
| 74 | |
| 75 | let stack: (Rule | null)[] = [] |
| 76 | |
| 77 | let parent = null as Rule | null |
| 78 | let node = null as AstNode | null |
| 79 | |
| 80 | let buffer = class="st">'' |
| 81 | let closingBracketStack = class="st">'' |
| 82 | |
| 83 | class="cm">// The start of the first non-whitespace character in the buffer |
| 84 | let bufferStart = 0 |
| 85 | |
| 86 | let peekChar |
| 87 | |
| 88 | for (let i = 0; i < input.length; i++) { |
| 89 | let currentChar = input.charCodeAt(i) |
| 90 | |
| 91 | class="cm">// Skip over the CR in CRLF. This allows code below to only check for a line |
| 92 | class="cm">// break even if we're looking at a Windows newline. Peeking the input still |
| 93 | class="cm">// has to check for CRLF but that happens less often. |
| 94 | if (currentChar === CARRIAGE_RETURN) { |
| 95 | peekChar = input.charCodeAt(i + 1) |
| 96 | if (peekChar === LINE_BREAK) continue |
| 97 | } |
| 98 | |
| 99 | class="cm">// Current character is a `\` therefore the next character is escaped, |
| 100 | class="cm">// consume it together with the next character and continue. |
| 101 | class="cm">// |
| 102 | class="cm">// E.g.: |
| 103 | class="cm">// |
| 104 | class="cm">// ```css |
| 105 | class="cm">// .hover\:foo:hover {} |
| 106 | class="cm">// ^ |
| 107 | class="cm">// ``` |
| 108 | class="cm">// |
| 109 | if (currentChar === BACKSLASH) { |
| 110 | if (buffer === class="st">'') bufferStart = i |
| 111 | buffer += input.slice(i, i + 2) |
| 112 | i += 1 |
| 113 | } |
| 114 | |
| 115 | class="cm">// Start of a comment. |
| 116 | class="cm">// |
| 117 | class="cm">// E.g.: |
| 118 | class="cm">// |
| 119 | class="cm">// ```css |
| 120 | class="cm">// /* Example */ |
| 121 | class="cm">// ^^^^^^^^^^^^^ |