UNPKG

3.83 kBJavaScriptView Raw
1/**
2 * @fileoverview Define 2 token factories; forward and backward.
3 * @author Toru Nagashima
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Requirements
9//------------------------------------------------------------------------------
10
11const BackwardTokenCommentCursor = require("./backward-token-comment-cursor");
12const BackwardTokenCursor = require("./backward-token-cursor");
13const FilterCursor = require("./filter-cursor");
14const ForwardTokenCommentCursor = require("./forward-token-comment-cursor");
15const ForwardTokenCursor = require("./forward-token-cursor");
16const LimitCursor = require("./limit-cursor");
17const SkipCursor = require("./skip-cursor");
18
19//------------------------------------------------------------------------------
20// Helpers
21//------------------------------------------------------------------------------
22
23/**
24 * The cursor factory.
25 * @private
26 */
27class CursorFactory {
28
29 /**
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.
33 */
34 constructor(TokenCursor, TokenCommentCursor) {
35 this.TokenCursor = TokenCursor;
36 this.TokenCommentCursor = TokenCommentCursor;
37 }
38
39 /**
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.
48 */
49 createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
50 const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
51
52 return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
53 }
54
55 /**
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.
67 */
68 createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
69 let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
70
71 if (filter) {
72 cursor = new FilterCursor(cursor, filter);
73 }
74 if (skip >= 1) {
75 cursor = new SkipCursor(cursor, skip);
76 }
77 if (count >= 0) {
78 cursor = new LimitCursor(cursor, count);
79 }
80
81 return cursor;
82 }
83}
84
85//------------------------------------------------------------------------------
86// Exports
87//------------------------------------------------------------------------------
88
89exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
90exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);