| 1 | /**
|
|---|
| 2 | * @fileoverview Define the abstract class about cursors which iterate tokens.
|
|---|
| 3 | * @author Toru Nagashima
|
|---|
| 4 | */
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | //------------------------------------------------------------------------------
|
|---|
| 8 | // Exports
|
|---|
| 9 | //------------------------------------------------------------------------------
|
|---|
| 10 |
|
|---|
| 11 | /**
|
|---|
| 12 | * The abstract class about cursors which iterate tokens.
|
|---|
| 13 | *
|
|---|
| 14 | * This class has 2 abstract methods.
|
|---|
| 15 | *
|
|---|
| 16 | * - `current: Token | Comment | null` ... The current token.
|
|---|
| 17 | * - `moveNext(): boolean` ... Moves this cursor to the next token. If the next token didn't exist, it returns `false`.
|
|---|
| 18 | *
|
|---|
| 19 | * This is similar to ES2015 Iterators.
|
|---|
| 20 | * However, Iterators were slow (at 2017-01), so I created this class as similar to C# IEnumerable.
|
|---|
| 21 | *
|
|---|
| 22 | * There are the following known sub classes.
|
|---|
| 23 | *
|
|---|
| 24 | * - ForwardTokenCursor .......... The cursor which iterates tokens only.
|
|---|
| 25 | * - BackwardTokenCursor ......... The cursor which iterates tokens only in reverse.
|
|---|
| 26 | * - ForwardTokenCommentCursor ... The cursor which iterates tokens and comments.
|
|---|
| 27 | * - BackwardTokenCommentCursor .. The cursor which iterates tokens and comments in reverse.
|
|---|
| 28 | * - DecorativeCursor
|
|---|
| 29 | * - FilterCursor ............ The cursor which ignores the specified tokens.
|
|---|
| 30 | * - SkipCursor .............. The cursor which ignores the first few tokens.
|
|---|
| 31 | * - LimitCursor ............. The cursor which limits the count of tokens.
|
|---|
| 32 | *
|
|---|
| 33 | */
|
|---|
| 34 | module.exports = class Cursor {
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Initializes this cursor.
|
|---|
| 38 | */
|
|---|
| 39 | constructor() {
|
|---|
| 40 | this.current = null;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Gets the first token.
|
|---|
| 45 | * This consumes this cursor.
|
|---|
| 46 | * @returns {Token|Comment} The first token or null.
|
|---|
| 47 | */
|
|---|
| 48 | getOneToken() {
|
|---|
| 49 | return this.moveNext() ? this.current : null;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Gets the first tokens.
|
|---|
| 54 | * This consumes this cursor.
|
|---|
| 55 | * @returns {(Token|Comment)[]} All tokens.
|
|---|
| 56 | */
|
|---|
| 57 | getAllTokens() {
|
|---|
| 58 | const tokens = [];
|
|---|
| 59 |
|
|---|
| 60 | while (this.moveNext()) {
|
|---|
| 61 | tokens.push(this.current);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return tokens;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Moves this cursor to the next token.
|
|---|
| 69 | * @returns {boolean} `true` if the next token exists.
|
|---|
| 70 | * @abstract
|
|---|
| 71 | */
|
|---|
| 72 | /* c8 ignore next */
|
|---|
| 73 | moveNext() { // eslint-disable-line class-methods-use-this -- Unused
|
|---|
| 74 | throw new Error("Not implemented.");
|
|---|
| 75 | }
|
|---|
| 76 | };
|
|---|