cursors.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @fileoverview Define 2 token factories; forward and backward.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const BackwardTokenCommentCursor = require("./backward-token-comment-cursor");
  10. const BackwardTokenCursor = require("./backward-token-cursor");
  11. const FilterCursor = require("./filter-cursor");
  12. const ForwardTokenCommentCursor = require("./forward-token-comment-cursor");
  13. const ForwardTokenCursor = require("./forward-token-cursor");
  14. const LimitCursor = require("./limit-cursor");
  15. const SkipCursor = require("./skip-cursor");
  16. //------------------------------------------------------------------------------
  17. // Helpers
  18. //------------------------------------------------------------------------------
  19. /**
  20. * The cursor factory.
  21. * @private
  22. */
  23. class CursorFactory {
  24. /**
  25. * Initializes this cursor.
  26. * @param {Function} TokenCursor The class of the cursor which iterates tokens only.
  27. * @param {Function} TokenCommentCursor The class of the cursor which iterates the mix of tokens and comments.
  28. */
  29. constructor(TokenCursor, TokenCommentCursor) {
  30. this.TokenCursor = TokenCursor;
  31. this.TokenCommentCursor = TokenCommentCursor;
  32. }
  33. /**
  34. * Creates a base cursor instance that can be decorated by createCursor.
  35. * @param {Token[]} tokens The array of tokens.
  36. * @param {Comment[]} comments The array of comments.
  37. * @param {Object} indexMap The map from locations to indices in `tokens`.
  38. * @param {number} startLoc The start location of the iteration range.
  39. * @param {number} endLoc The end location of the iteration range.
  40. * @param {boolean} includeComments The flag to iterate comments as well.
  41. * @returns {Cursor} The created base cursor.
  42. */
  43. createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
  44. const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
  45. return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
  46. }
  47. /**
  48. * Creates a cursor that iterates tokens with normalized options.
  49. * @param {Token[]} tokens The array of tokens.
  50. * @param {Comment[]} comments The array of comments.
  51. * @param {Object} indexMap The map from locations to indices in `tokens`.
  52. * @param {number} startLoc The start location of the iteration range.
  53. * @param {number} endLoc The end location of the iteration range.
  54. * @param {boolean} includeComments The flag to iterate comments as well.
  55. * @param {Function|null} filter The predicate function to choose tokens.
  56. * @param {number} skip The count of tokens the cursor skips.
  57. * @param {number} count The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
  58. * @returns {Cursor} The created cursor.
  59. */
  60. createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
  61. let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
  62. if (filter) {
  63. cursor = new FilterCursor(cursor, filter);
  64. }
  65. if (skip >= 1) {
  66. cursor = new SkipCursor(cursor, skip);
  67. }
  68. if (count >= 0) {
  69. cursor = new LimitCursor(cursor, count);
  70. }
  71. return cursor;
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. // Exports
  76. //------------------------------------------------------------------------------
  77. exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
  78. exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);