* Returns location converter. * @param {number} pos position * @returns {LocConverter} location converter
(pos)
| 27 | * @returns {LocConverter} location converter |
| 28 | */ |
| 29 | get(pos) { |
| 30 | if (this.pos !== pos) { |
| 31 | if (this.pos < pos) { |
| 32 | const str = this._input.slice(this.pos, pos); |
| 33 | let i = str.lastIndexOf("\n"); |
| 34 | if (i === -1) { |
| 35 | this.column += str.length; |
| 36 | } else { |
| 37 | this.column = str.length - i - 1; |
| 38 | this.line++; |
| 39 | while (i > 0 && (i = str.lastIndexOf("\n", i - 1)) !== -1) { |
| 40 | this.line++; |
| 41 | } |
| 42 | } |
| 43 | } else { |
| 44 | // Retreat: count newlines crossed in (pos, this.pos), i.e. |
| 45 | // exclude the newline at `this.pos` itself. By convention a |
| 46 | // `\n` is the last column of its line, so when `this.pos` |
| 47 | // sits on one we're already on the line containing it; only |
| 48 | // newlines strictly **before** `this.pos` and at-or-after |
| 49 | // `pos` represent crossed line boundaries. |
| 50 | let i = this._input.lastIndexOf("\n", this.pos - 1); |
| 51 | while (i >= pos) { |
| 52 | this.line--; |
| 53 | i = i > 0 ? this._input.lastIndexOf("\n", i - 1) : -1; |
| 54 | } |
| 55 | this.column = i === -1 ? pos : pos - i - 1; |
| 56 | } |
| 57 | this.pos = pos; |
| 58 | } |
| 59 | return this; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | module.exports = LocConverter; |