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.

cursor.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @fileoverview Define the abstract class about cursors which iterate tokens.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Exports
  8. //------------------------------------------------------------------------------
  9. /**
  10. * The abstract class about cursors which iterate tokens.
  11. *
  12. * This class has 2 abstract methods.
  13. *
  14. * - `current: Token | Comment | null` ... The current token.
  15. * - `moveNext(): boolean` ... Moves this cursor to the next token. If the next token didn't exist, it returns `false`.
  16. *
  17. * This is similar to ES2015 Iterators.
  18. * However, Iterators were slow (at 2017-01), so I created this class as similar to C# IEnumerable.
  19. *
  20. * There are the following known sub classes.
  21. *
  22. * - ForwardTokenCursor .......... The cursor which iterates tokens only.
  23. * - BackwardTokenCursor ......... The cursor which iterates tokens only in reverse.
  24. * - ForwardTokenCommentCursor ... The cursor which iterates tokens and comments.
  25. * - BackwardTokenCommentCursor .. The cursor which iterates tokens and comments in reverse.
  26. * - DecorativeCursor
  27. * - FilterCursor ............ The cursor which ignores the specified tokens.
  28. * - SkipCursor .............. The cursor which ignores the first few tokens.
  29. * - LimitCursor ............. The cursor which limits the count of tokens.
  30. *
  31. */
  32. module.exports = class Cursor {
  33. /**
  34. * Initializes this cursor.
  35. */
  36. constructor() {
  37. this.current = null;
  38. }
  39. /**
  40. * Gets the first token.
  41. * This consumes this cursor.
  42. * @returns {Token|Comment} The first token or null.
  43. */
  44. getOneToken() {
  45. return this.moveNext() ? this.current : null;
  46. }
  47. /**
  48. * Gets the first tokens.
  49. * This consumes this cursor.
  50. * @returns {(Token|Comment)[]} All tokens.
  51. */
  52. getAllTokens() {
  53. const tokens = [];
  54. while (this.moveNext()) {
  55. tokens.push(this.current);
  56. }
  57. return tokens;
  58. }
  59. /**
  60. * Moves this cursor to the next token.
  61. * @returns {boolean} `true` if the next token exists.
  62. * @abstract
  63. */
  64. /* istanbul ignore next */
  65. moveNext() { // eslint-disable-line class-methods-use-this
  66. throw new Error("Not implemented.");
  67. }
  68. };