|
276 | 276 | Lexer.prototype.setState = function(state) { |
277 | 277 | if (!state || this.state === state) return |
278 | 278 | this.state = state |
279 | | - var index = this.re ? this.re.lastIndex : 0 |
280 | 279 | var info = this.states[state] |
281 | 280 | this.groups = info.groups |
282 | 281 | this.error = info.error |
283 | 282 | this.re = info.regexp |
284 | | - this.re.lastIndex = index |
285 | 283 | } |
286 | 284 |
|
287 | 285 | Lexer.prototype.popState = function() { |
|
312 | 310 | var re = this.re |
313 | 311 | var buffer = this.buffer |
314 | 312 |
|
315 | | - if (re.lastIndex === buffer.length) { |
| 313 | + if (this.index === buffer.length) { |
316 | 314 | return // EOF |
317 | 315 | } |
318 | 316 |
|
319 | | - var start = re.lastIndex |
| 317 | + var index = re.lastIndex = this.index |
320 | 318 | var match = this.eat(re) |
321 | 319 | var group, value, text |
322 | 320 | if (match === null) { |
|
327 | 325 | } |
328 | 326 |
|
329 | 327 | // consume rest of buffer |
330 | | - text = value = buffer.slice(start) |
| 328 | + text = value = buffer.slice(index) |
331 | 329 | re.lastIndex = buffer.length |
332 | 330 |
|
333 | 331 | } else { |
|
366 | 364 | type: group.tokenType, |
367 | 365 | value: value, |
368 | 366 | toString: tokenToString, |
369 | | - offset: start, |
| 367 | + offset: index, |
370 | 368 | size: size, |
371 | 369 | lineBreaks: lineBreaks, |
372 | 370 | line: this.line, |
|
377 | 375 | else if (group.push) this.pushState(group.push) |
378 | 376 | else if (group.next) this.setState(group.next) |
379 | 377 |
|
| 378 | + this.index += size |
380 | 379 | this.line += lineBreaks |
381 | 380 | if (lineBreaks !== 0) { |
382 | 381 | this.col = size - nl + 1 |
|
403 | 402 |
|
404 | 403 | Lexer.prototype.reset = function(data, state) { |
405 | 404 | this.buffer = data || '' |
406 | | - this.re.lastIndex = 0 |
| 405 | + this.index = 0 |
407 | 406 | this.line = state ? state.line : 1 |
408 | 407 | this.col = state ? state.col : 1 |
409 | 408 | return this |
|
421 | 420 | return this |
422 | 421 | } |
423 | 422 |
|
424 | | - Lexer.prototype.clone = function(input) { |
425 | | - var map = Object.create(null) |
426 | | - var keys = Object.getOwnPropertyNames(this.states) |
427 | | - for (var i = 0; i < keys.length; i++) { |
428 | | - var key = keys[i] |
429 | | - var s = this.states[key] |
430 | | - map[key] = { |
431 | | - groups: s.groups, |
432 | | - regexp: new RegExp(s.regexp.source, s.regexp.flags), |
433 | | - error: s.error, |
434 | | - } |
435 | | - } |
436 | | - return new Lexer(map, this.state, input) |
| 423 | + Lexer.prototype.clone = function() { |
| 424 | + return new Lexer(this.states, this.state) |
437 | 425 | } |
438 | 426 |
|
439 | 427 |
|
|
0 commit comments