| 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 | // Note: it is important that any transformations of the input string |
| 68 | // *before* processing do NOT change the length of the string. This |
| 69 | // would invalidate the mechanism used to track source locations. |
| 70 | if (input[0] === '\uFEFF') input = ' ' + 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 = '' |
| 81 | let closingBracketStack = '' |
| 82 | |
| 83 | // 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 | // Skip over the CR in CRLF. This allows code below to only check for a line |
| 92 | // break even if we're looking at a Windows newline. Peeking the input still |
| 93 | // 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 | // Current character is a `\` therefore the next character is escaped, |
| 100 | // consume it together with the next character and continue. |
| 101 | // |
| 102 | // E.g.: |
| 103 | // |
| 104 | // ```css |
| 105 | // .hover\:foo:hover {} |
| 106 | // ^ |
| 107 | // ``` |
| 108 | // |
| 109 | if (currentChar === BACKSLASH) { |
| 110 | if (buffer === '') bufferStart = i |
| 111 | buffer += input.slice(i, i + 2) |
| 112 | i += 1 |
| 113 | } |
| 114 | |
| 115 | // Start of a comment. |
| 116 | // |
| 117 | // E.g.: |
| 118 | // |
| 119 | // ```css |
| 120 | // /* Example */ |
| 121 | // ^^^^^^^^^^^^^ |