Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 2x 2x 2918x 2918x 107x 107x 2x 616x 2x 2x 272x 272x 422x 422x 418x 272x 87x 87x 865x 739x 865x 219x 87x 755x 740x 799x 799x 56x 56x 4x 4x 739x 739x 850x 95x 755x 755x 2918x 87x 87x 272x 272x 272x 655x 655x 655x 39x 39x 616x 391x 8x 8x 383x 383x 383x 225x 225x 272x 15x 15x 15x 15x 161x 161x 161x 8x 8x 153x 15x 15x 15x 138x 138x 15x 15x 865x 865x 865x 865x 865x 865x 142x 755x 755x 1x 754x 15x 739x 739x 739x 78x 78x 661x 24x 24x 637x 148x 148x 489x 24x 24x 15x 15x 9x 465x 13x 13x 452x 9x 9x 443x 47x 47x 396x 47x 47x 349x 68x 68x 281x 9x 9x 272x | import { nameTok, colonTok, dotTok, starTok, bangTok, bracketLTok, bracketRTok, bracketDRTok, expandTok, parenLTok, parenRTok, commaTok, eofTok, ignoreTok, braceLTok, braceRTok, bracketDLTok } from './tokens' import { bracketDContext } from './contexts' const nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/ const fullCharCodeAtPos = (input, pos) => { const code = input.charCodeAt(pos) if (code <= 0xd7ff || code >= 0xe000) return code const next = input.charCodeAt(pos + 1) return (code << 10) + next - 0x35fdc00 } const isRewordCode = code => code === 42 || code === 46 || code === 33 || code === 91 || code === 93 || code === 40 || code === 41 || code === 44 || code === 58 || code === 126 || code === 123 || code === 125 const getError = (message, props) => { const err = new Error(message) Object.assign(err, props) return err } const slice = (string, start, end) => { let str = '' for (let i = start; i < end; i++) { let ch = string.charAt(i) if (ch !== '\\') { str += ch } } return str } export class Tokenizer { constructor(input) { this.input = input this.state = { context: [], type: null, pos: 0 } } unexpect(type) { type = type || this.state.type return getError( `Unexpect token "${type.flag}" in ${this.state.pos} char.`, { pos: this.state.pos } ) } expectNext(type, next) { if (type && type.expectNext) { Iif (next && !type.expectNext.call(this, next)) { throw getError( `Unexpect token "${next.flag}" token should not be behind "${type.flag}" token.(${this.state.pos}th char)`, { pos: this.state.pos } ) } } } expectPrev(type, prev) { if (type && type.expectPrev) { Iif (prev && !type.expectPrev.call(this, prev)) { throw getError( `Unexpect token "${type.flag}" should not be behind "${prev.flag}"(${this.state.pos}th char).`, { pos: this.state.pos } ) } } } match(type) { return this.state.type === type } skipSpace() { if (this.curContext() === bracketDContext) return loop: while (this.state.pos < this.input.length) { const ch = this.input.charCodeAt(this.state.pos) switch (ch) { case 32: case 160: ++this.state.pos break case 13: if (this.input.charCodeAt(this.state.pos + 1) === 10) { ++this.state.pos } case 10: case 8232: case 8233: ++this.state.pos break default: Iif ( (ch > 8 && ch < 14) || (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) ) { ++this.state.pos } else { break loop } } } } next() { if (this.input.length <= this.state.pos) { return this.finishToken(eofTok) } this.skipSpace() this.readToken( this.getCode(), this.state.pos > 0 ? this.getCode(this.state.pos - 1) : -Infinity ) } getCode(pos = this.state.pos) { return fullCharCodeAtPos(this.input, pos) } eat(type) { Iif (this.match(type)) { this.next() return true } else { return false } } readKeyWord() { let startPos = this.state.pos, string = '' while (true) { let code = this.getCode() let prevCode = this.getCode(this.state.pos - 1) if (this.input.length === this.state.pos) { string = slice(this.input, startPos, this.state.pos + 1) break } if (!isRewordCode(code) || prevCode === 92) { if ( code === 32 || code === 160 || code === 10 || code === 8232 || code === 8233 ) { string = slice(this.input, startPos, this.state.pos) break } Iif (code === 13 && this.input.charCodeAt(this.state.pos + 1) === 10) { string = slice(this.input, startPos, this.state.pos) break } Iif ( (code > 8 && code < 14) || (code >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(code))) ) { string = slice(this.input, startPos, this.state.pos) break } this.state.pos++ } else { string = slice(this.input, startPos, this.state.pos) break } } this.finishToken(nameTok, string) } readIngoreString() { let startPos = this.state.pos, prevCode, sliced = false, string = '' while (true) { let code = this.getCode() Iif (this.state.pos >= this.input.length) break if ((code === 91 || code === 93) && prevCode === 92) { this.state.pos++ prevCode = '' } else if (code == 93 && prevCode === 93) { string = this.input .slice(startPos, this.state.pos - 1) .replace(/\\([\[\]])/g, '$1') this.state.pos++ break } else { this.state.pos++ prevCode = code } } this.finishToken(ignoreTok, string) this.finishToken(bracketDRTok) } finishToken(type, value) { const preType = this.state.type this.state.type = type if (value !== undefined) this.state.value = value this.expectNext(preType, type) this.expectPrev(type, preType) if (type.updateContext) { type.updateContext.call(this, preType) } } readToken(code, prevCode) { Iif (prevCode === 92) { return this.readKeyWord() } if (this.input.length <= this.state.pos) { this.finishToken(eofTok) } else if (this.curContext() === bracketDContext) { this.readIngoreString() } else Iif (code === 123) { this.state.pos++ this.finishToken(braceLTok) } else Iif (code === 125) { this.state.pos++ this.finishToken(braceRTok) } else if (code === 42) { this.state.pos++ this.finishToken(starTok) } else if (code === 33) { this.state.pos++ this.finishToken(bangTok) } else if (code === 46) { this.state.pos++ this.finishToken(dotTok) } else if (code === 91) { this.state.pos++ if (this.getCode() === 91) { this.state.pos++ return this.finishToken(bracketDLTok) } this.finishToken(bracketLTok) } else if (code === 126) { this.state.pos++ this.finishToken(expandTok) } else if (code === 93) { this.state.pos++ this.finishToken(bracketRTok) } else if (code === 40) { this.state.pos++ this.finishToken(parenLTok) } else if (code === 41) { this.state.pos++ this.finishToken(parenRTok) } else if (code === 44) { this.state.pos++ this.finishToken(commaTok) } else if (code === 58) { this.state.pos++ this.finishToken(colonTok) } else { this.readKeyWord() } } } |