2 * @fileoverview Define the abstract class about cursors which iterate tokens.
3 * @author Toru Nagashima
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
12 * The abstract class about cursors which iterate tokens.
14 * This class has 2 abstract methods.
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`.
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.
22 * There are the following known sub classes.
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.
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.
34 module.exports = class Cursor {
37 * Initializes this cursor.
44 * Gets the first token.
45 * This consumes this cursor.
46 * @returns {Token|Comment} The first token or null.
49 return this.moveNext() ? this.current : null;
53 * Gets the first tokens.
54 * This consumes this cursor.
55 * @returns {(Token|Comment)[]} All tokens.
60 while (this.moveNext()) {
61 tokens.push(this.current);
68 * Moves this cursor to the next token.
69 * @returns {boolean} `true` if the next token exists.
72 /* istanbul ignore next */
73 moveNext() { // eslint-disable-line class-methods-use-this
74 throw new Error("Not implemented.");