2 * @fileoverview Define 2 token factories; forward and backward.
3 * @author Toru Nagashima
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const BackwardTokenCommentCursor = require("./backward-token-comment-cursor");
12 const BackwardTokenCursor = require("./backward-token-cursor");
13 const FilterCursor = require("./filter-cursor");
14 const ForwardTokenCommentCursor = require("./forward-token-comment-cursor");
15 const ForwardTokenCursor = require("./forward-token-cursor");
16 const LimitCursor = require("./limit-cursor");
17 const SkipCursor = require("./skip-cursor");
19 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
30 * Initializes this cursor.
31 * @param {Function} TokenCursor The class of the cursor which iterates tokens only.
32 * @param {Function} TokenCommentCursor The class of the cursor which iterates the mix of tokens and comments.
34 constructor(TokenCursor, TokenCommentCursor) {
35 this.TokenCursor = TokenCursor;
36 this.TokenCommentCursor = TokenCommentCursor;
40 * Creates a base cursor instance that can be decorated by createCursor.
41 * @param {Token[]} tokens The array of tokens.
42 * @param {Comment[]} comments The array of comments.
43 * @param {Object} indexMap The map from locations to indices in `tokens`.
44 * @param {number} startLoc The start location of the iteration range.
45 * @param {number} endLoc The end location of the iteration range.
46 * @param {boolean} includeComments The flag to iterate comments as well.
47 * @returns {Cursor} The created base cursor.
49 createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
50 const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
52 return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
56 * Creates a cursor that iterates tokens with normalized options.
57 * @param {Token[]} tokens The array of tokens.
58 * @param {Comment[]} comments The array of comments.
59 * @param {Object} indexMap The map from locations to indices in `tokens`.
60 * @param {number} startLoc The start location of the iteration range.
61 * @param {number} endLoc The end location of the iteration range.
62 * @param {boolean} includeComments The flag to iterate comments as well.
63 * @param {Function|null} filter The predicate function to choose tokens.
64 * @param {number} skip The count of tokens the cursor skips.
65 * @param {number} count The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
66 * @returns {Cursor} The created cursor.
68 createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
69 let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
72 cursor = new FilterCursor(cursor, filter);
75 cursor = new SkipCursor(cursor, skip);
78 cursor = new LimitCursor(cursor, count);
85 //------------------------------------------------------------------------------
87 //------------------------------------------------------------------------------
89 exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
90 exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);