|
| 1 | +/* @flow */ |
| 2 | +import { types as tt } from './types'; |
| 3 | + |
| 4 | +import type { TokenType } from './types'; |
| 5 | + |
| 6 | +type Token = { |
| 7 | + type: TokenType; |
| 8 | + value?: any; |
| 9 | +}; |
| 10 | + |
| 11 | +export default function *tokenize(input: string): Generator<Token, Token, void> { |
| 12 | + // Current position in the input string |
| 13 | + let position = 0; |
| 14 | + |
| 15 | + while (position < input.length) { |
| 16 | + // Current char we are looking at |
| 17 | + let currentChar = input[position]; |
| 18 | + |
| 19 | + let wasWhitespace = false; |
| 20 | + if (isWhitespace(currentChar)) { |
| 21 | + currentChar = input[++position]; |
| 22 | + while (isWhitespace(currentChar)) { |
| 23 | + currentChar = input[++position]; |
| 24 | + } |
| 25 | + yield { type: tt.whitespace }; |
| 26 | + // No continue needed here since we consumed all whitespace and a guaranteed to find something meaningful now |
| 27 | + } |
| 28 | + |
| 29 | + if (currentChar === '/') { |
| 30 | + currentChar = input[++position]; |
| 31 | + if (currentChar === '*') { |
| 32 | + // Inside a comment |
| 33 | + console.log('Inside a comment'); |
| 34 | + currentChar = input[++position]; |
| 35 | + let nextChar = input[position + 1]; |
| 36 | + console.log('Current Char', currentChar); |
| 37 | + console.log('Next Char', nextChar); |
| 38 | + console.log('Start while'); |
| 39 | + while (!(currentChar === '*' && nextChar === '/')) { |
| 40 | + currentChar = input[++position]; |
| 41 | + nextChar = input[position + 1]; |
| 42 | + console.log('Current Char', currentChar); |
| 43 | + console.log('Next Char', nextChar); |
| 44 | + } |
| 45 | + console.log('End while'); |
| 46 | + currentChar = input[position + 2]; |
| 47 | + position += 2; |
| 48 | + console.log('Current Char', currentChar); |
| 49 | + } else { |
| 50 | + throw new UnexpectedCharacterError('/', currentChar); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (currentChar === '[') { |
| 55 | + yield { type: tt.bracketL }; |
| 56 | + ++position; |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (currentChar === ']') { |
| 61 | + yield { type: tt.bracketR }; |
| 62 | + ++position; |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (currentChar === ':') { |
| 67 | + yield { type: tt.colon }; |
| 68 | + ++position; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + if (currentChar === ',') { |
| 73 | + yield { type: tt.comma }; |
| 74 | + ++position; |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + if (currentChar === '.') { |
| 79 | + yield { type: tt.dot }; |
| 80 | + ++position; |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (currentChar === '>') { |
| 85 | + yield { type: tt.greater }; |
| 86 | + ++position; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (currentChar === '#') { |
| 91 | + yield { type: tt.hash }; |
| 92 | + ++position; |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (currentChar === '(') { |
| 97 | + yield { type: tt.parenL }; |
| 98 | + ++position; |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if (currentChar === ')') { |
| 103 | + yield { type: tt.parenR }; |
| 104 | + ++position; |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if (currentChar === '%') { |
| 109 | + yield { type: tt.percentage }; |
| 110 | + ++position; |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if (currentChar === '+') { |
| 115 | + yield { type: tt.plus }; |
| 116 | + ++position; |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (currentChar === '~') { |
| 121 | + yield { type: tt.tilde }; |
| 122 | + ++position; |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (currentChar === '|') { |
| 127 | + currentChar= input[++position]; |
| 128 | + if (currentChar === '=') { |
| 129 | + yield { type: tt.dashmatch }; |
| 130 | + ++position; |
| 131 | + continue; |
| 132 | + } else { |
| 133 | + throw new UnexpectedCharacterError(currentChar, '='); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (currentChar === '~') { |
| 138 | + currentChar= input[++position]; |
| 139 | + if (currentChar === '=') { |
| 140 | + yield { type: tt.includes }; |
| 141 | + ++position; |
| 142 | + continue; |
| 143 | + } else { |
| 144 | + throw new UnexpectedCharacterError(currentChar, '='); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (currentChar === '^') { |
| 149 | + currentChar= input[++position]; |
| 150 | + if (currentChar === '=') { |
| 151 | + yield { type: tt.prefixmatch }; |
| 152 | + ++position; |
| 153 | + continue; |
| 154 | + } else { |
| 155 | + throw new UnexpectedCharacterError(currentChar, '='); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (currentChar === '*') { |
| 160 | + currentChar= input[++position]; |
| 161 | + if (currentChar === '=') { |
| 162 | + yield { type: tt.substringmatch }; |
| 163 | + ++position; |
| 164 | + continue; |
| 165 | + } else { |
| 166 | + throw new UnexpectedCharacterError(currentChar, '='); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (currentChar === '$') { |
| 171 | + currentChar= input[++position]; |
| 172 | + if (currentChar === '=') { |
| 173 | + yield { type: tt.suffixmatch }; |
| 174 | + ++position; |
| 175 | + continue; |
| 176 | + } else { |
| 177 | + throw new UnexpectedCharacterError(currentChar, '='); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + return { |
| 184 | + type: tt.eof, |
| 185 | + }; |
| 186 | +} |
| 187 | + |
| 188 | +function isWhitespace(char: string) { |
| 189 | + return (char === ' ' || char === "\t" || char === "\r" || char === "\n" || char === "\f"); |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +class UnexpectedCharacterError extends Error { |
| 194 | + constructor(actual, expected) { |
| 195 | + super(`Unexpected char "${actual}", expected "${expected}".`); |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +class UnexpectedEofError extends Error { |
| 200 | + constructor(expected) { |
| 201 | + super(`Unexpected end of input, expected "${expected}".`); |
| 202 | + } |
| 203 | +} |
0 commit comments