| 1613 | } |
| 1614 | |
| 1615 | class PlainCharacterCursor implements CharacterCursor { |
| 1616 | protected state: CursorState; |
| 1617 | protected file: ParseSourceFile; |
| 1618 | protected input: string; |
| 1619 | protected end: number; |
| 1620 | |
| 1621 | constructor(fileOrCursor: PlainCharacterCursor); |
| 1622 | constructor(fileOrCursor: ParseSourceFile, range: LexerRange); |
| 1623 | constructor(fileOrCursor: ParseSourceFile | PlainCharacterCursor, range?: LexerRange) { |
| 1624 | if (fileOrCursor instanceof PlainCharacterCursor) { |
| 1625 | this.file = fileOrCursor.file; |
| 1626 | this.input = fileOrCursor.input; |
| 1627 | this.end = fileOrCursor.end; |
| 1628 | |
| 1629 | const state = fileOrCursor.state; |
| 1630 | // Note: avoid using `{...fileOrCursor.state}` here as that has a severe performance penalty. |
| 1631 | // In ES5 bundles the object spread operator is translated into the `__assign` helper, which |
| 1632 | // is not optimized by VMs as efficiently as a raw object literal. Since this constructor is |
| 1633 | // called in tight loops, this difference matters. |
| 1634 | this.state = { |
| 1635 | peek: state.peek, |
| 1636 | offset: state.offset, |
| 1637 | line: state.line, |
| 1638 | column: state.column, |
| 1639 | }; |
| 1640 | } else { |
| 1641 | if (!range) { |
| 1642 | throw new Error( |
| 1643 | 'Programming error: the range argument must be provided with a file argument.', |
| 1644 | ); |
| 1645 | } |
| 1646 | this.file = fileOrCursor; |
| 1647 | this.input = fileOrCursor.content; |
| 1648 | this.end = range.endPos; |
| 1649 | this.state = { |
| 1650 | peek: -1, |
| 1651 | offset: range.startPos, |
| 1652 | line: range.startLine, |
| 1653 | column: range.startCol, |
| 1654 | }; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | clone(): PlainCharacterCursor { |
| 1659 | return new PlainCharacterCursor(this); |
| 1660 | } |
| 1661 | |
| 1662 | peek() { |
| 1663 | return this.state.peek; |
| 1664 | } |
| 1665 | charsLeft() { |
| 1666 | return this.end - this.state.offset; |
| 1667 | } |
| 1668 | diff(other: this) { |
| 1669 | return this.state.offset - other.state.offset; |
| 1670 | } |
| 1671 | |
| 1672 | advance(): void { |
nothing calls this directly
no outgoing calls
no test coverage detected