Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cursors.js 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. *
  36. * @param {Token[]} tokens - The array of tokens.
  37. * @param {Comment[]} comments - The array of comments.
  38. * @param {Object} indexMap - The map from locations to indices in `tokens`.
  39. * @param {number} startLoc - The start location of the iteration range.
  40. * @param {number} endLoc - The end location of the iteration range.
  41. * @param {boolean} includeComments - The flag to iterate comments as well.
  42. * @returns {Cursor} The created base cursor.
  43. */
  44. createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments) {
  45. const Cursor = includeComments ? this.TokenCommentCursor : this.TokenCursor;
  46. return new Cursor(tokens, comments, indexMap, startLoc, endLoc);
  47. }
  48. /**
  49. * Creates a cursor that iterates tokens with normalized options.
  50. *
  51. * @param {Token[]} tokens - The array of tokens.
  52. * @param {Comment[]} comments - The array of comments.
  53. * @param {Object} indexMap - The map from locations to indices in `tokens`.
  54. * @param {number} startLoc - The start location of the iteration range.
  55. * @param {number} endLoc - The end location of the iteration range.
  56. * @param {boolean} includeComments - The flag to iterate comments as well.
  57. * @param {Function|null} filter - The predicate function to choose tokens.
  58. * @param {number} skip - The count of tokens the cursor skips.
  59. * @param {number} count - The maximum count of tokens the cursor iterates. Zero is no iteration for backward compatibility.
  60. * @returns {Cursor} The created cursor.
  61. */
  62. createCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments, filter, skip, count) {
  63. let cursor = this.createBaseCursor(tokens, comments, indexMap, startLoc, endLoc, includeComments);
  64. if (filter) {
  65. cursor = new FilterCursor(cursor, filter);
  66. }
  67. if (skip >= 1) {
  68. cursor = new SkipCursor(cursor, skip);
  69. }
  70. if (count >= 0) {
  71. cursor = new LimitCursor(cursor, count);
  72. }
  73. return cursor;
  74. }
  75. }
  76. //------------------------------------------------------------------------------
  77. // Exports
  78. //------------------------------------------------------------------------------
  79. exports.forward = new CursorFactory(ForwardTokenCursor, ForwardTokenCommentCursor);
  80. exports.backward = new CursorFactory(BackwardTokenCursor, BackwardTokenCommentCursor);