1 | /**
|
2 | * @fileoverview Define the cursor which iterates tokens and comments.
|
3 | * @author Toru Nagashima
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const Cursor = require("./cursor");
|
12 | const utils = require("./utils");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Exports
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | /**
|
19 | * The cursor which iterates tokens and comments.
|
20 | */
|
21 | module.exports = class ForwardTokenCommentCursor extends Cursor {
|
22 |
|
23 | /**
|
24 | * Initializes this cursor.
|
25 | * @param {Token[]} tokens The array of tokens.
|
26 | * @param {Comment[]} comments The array of comments.
|
27 | * @param {Object} indexMap The map from locations to indices in `tokens`.
|
28 | * @param {number} startLoc The start location of the iteration range.
|
29 | * @param {number} endLoc The end location of the iteration range.
|
30 | */
|
31 | constructor(tokens, comments, indexMap, startLoc, endLoc) {
|
32 | super();
|
33 | this.tokens = tokens;
|
34 | this.comments = comments;
|
35 | this.tokenIndex = utils.getFirstIndex(tokens, indexMap, startLoc);
|
36 | this.commentIndex = utils.search(comments, startLoc);
|
37 | this.border = endLoc;
|
38 | }
|
39 |
|
40 | /** @inheritdoc */
|
41 | moveNext() {
|
42 | const token = (this.tokenIndex < this.tokens.length) ? this.tokens[this.tokenIndex] : null;
|
43 | const comment = (this.commentIndex < this.comments.length) ? this.comments[this.commentIndex] : null;
|
44 |
|
45 | if (token && (!comment || token.range[0] < comment.range[0])) {
|
46 | this.current = token;
|
47 | this.tokenIndex += 1;
|
48 | } else if (comment) {
|
49 | this.current = comment;
|
50 | this.commentIndex += 1;
|
51 | } else {
|
52 | this.current = null;
|
53 | }
|
54 |
|
55 | return Boolean(this.current) && (this.border === -1 || this.current.range[1] <= this.border);
|
56 | }
|
57 | };
|