16463564e9bd945436811c6874d1f50ab7dffe10025ecf3e957013bd966e2e8827b73ce47878dcab89a663372112a800af18b97a9985a3eb48e302f6970ad9 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * @fileoverview Define the cursor which ignores specified tokens.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const DecorativeCursor = require("./decorative-cursor");
  10. //------------------------------------------------------------------------------
  11. // Exports
  12. //------------------------------------------------------------------------------
  13. /**
  14. * The decorative cursor which ignores specified tokens.
  15. */
  16. module.exports = class FilterCursor extends DecorativeCursor {
  17. /**
  18. * Initializes this cursor.
  19. * @param {Cursor} cursor The cursor to be decorated.
  20. * @param {Function} predicate The predicate function to decide tokens this cursor iterates.
  21. */
  22. constructor(cursor, predicate) {
  23. super(cursor);
  24. this.predicate = predicate;
  25. }
  26. /** @inheritdoc */
  27. moveNext() {
  28. const predicate = this.predicate;
  29. while (super.moveNext()) {
  30. if (predicate(this.current)) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. };