1 | /**
|
2 | * @fileoverview Define 2 token factories; forward and backward.
|
3 | * @author Toru Nagashima
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
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");
|
18 |
|
19 | //------------------------------------------------------------------------------
|
20 | // Helpers
|
21 | //------------------------------------------------------------------------------
|
22 |
|
23 | /**
|
24 | * The cursor factory.
|
25 | * @private
|
26 | */
|
27 | class 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 | *
|
42 | * @param {Token[]} tokens - The array of tokens.
|
43 | * @param {Comment[]} comments - The array of comments.
|
44 | * @param {Object} indexMap - The map from locations to indices in `tokens`.
|
45 | * @param {number} startLoc - The start location of the iteration range.
|
46 | * @param {number} endLoc - The end location of the iteration range.
|
47 | * @param {boolean} includeComments - The flag to iterate comments as well.
|
48 | * @returns {Cursor} The created base cursor.
|
49 | */
|
50 | createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
|
51 | const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
|
52 |
|
53 | return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
|
54 | }
|
55 |
|
56 | /**
|
57 | * Creates a cursor that iterates tokens with normalized options.
|
58 | *
|
59 | * @param {Token[]} tokens - The array of tokens.
|
60 | * @param {Comment[]} comments - The array of comments.
|
61 | * @param {Object} indexMap - The map from locations to indices in `tokens`.
|
62 | * @param {number} startLoc - The start location of the iteration range.
|
63 | * @param {number} endLoc - The end location of the iteration range.
|
64 | * @param {boolean} includeComments - The flag to iterate comments as well.
|
65 | * @param {Function|null} filter - The predicate function to choose tokens.
|
66 | * @param {number} skip - The count of tokens the cursor skips.
|
67 | * @param {number} count - The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
|
68 | * @returns {Cursor} The created cursor.
|
69 | */
|
70 | createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
|
71 | let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
|
72 |
|
73 | if (filter) {
|
74 | cursor = new FilterCursor(cursor, filter);
|
75 | }
|
76 | if (skip >= 1) {
|
77 | cursor = new SkipCursor(cursor, skip);
|
78 | }
|
79 | if (count >= 0) {
|
80 | cursor = new LimitCursor(cursor, count);
|
81 | }
|
82 |
|
83 | return cursor;
|
84 | }
|
85 | }
|
86 |
|
87 | //------------------------------------------------------------------------------
|
88 | // Exports
|
89 | //------------------------------------------------------------------------------
|
90 |
|
91 | exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
|
92 | exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);
|