token_iterator.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. /**
  33. *
  34. *
  35. * This class provides an essay way to treat the document as a stream of tokens, and provides methods to iterate over these tokens.
  36. * @class TokenIterator
  37. **/
  38. /**
  39. * Creates a new token iterator object. The inital token index is set to the provided row and column coordinates.
  40. * @param {EditSession} session The session to associate with
  41. * @param {Number} initialRow The row to start the tokenizing at
  42. * @param {Number} initialColumn The column to start the tokenizing at
  43. *
  44. * @constructor
  45. **/
  46. var TokenIterator = function(session, initialRow, initialColumn) {
  47. this.$session = session;
  48. this.$row = initialRow;
  49. this.$rowTokens = session.getTokens(initialRow);
  50. var token = session.getTokenAt(initialRow, initialColumn);
  51. this.$tokenIndex = token ? token.index : -1;
  52. };
  53. (function() {
  54. /**
  55. *
  56. * Tokenizes all the items from the current point to the row prior in the document.
  57. * @returns {[String]} If the current point is not at the top of the file, this function returns `null`. Otherwise, it returns an array of the tokenized strings.
  58. **/
  59. this.stepBackward = function() {
  60. this.$tokenIndex -= 1;
  61. while (this.$tokenIndex < 0) {
  62. this.$row -= 1;
  63. if (this.$row < 0) {
  64. this.$row = 0;
  65. return null;
  66. }
  67. this.$rowTokens = this.$session.getTokens(this.$row);
  68. this.$tokenIndex = this.$rowTokens.length - 1;
  69. }
  70. return this.$rowTokens[this.$tokenIndex];
  71. };
  72. /**
  73. *
  74. * Tokenizes all the items from the current point until the next row in the document. If the current point is at the end of the file, this function returns `null`. Otherwise, it returns the tokenized string.
  75. * @returns {String}
  76. **/
  77. this.stepForward = function() {
  78. this.$tokenIndex += 1;
  79. var rowCount;
  80. while (this.$tokenIndex >= this.$rowTokens.length) {
  81. this.$row += 1;
  82. if (!rowCount)
  83. rowCount = this.$session.getLength();
  84. if (this.$row >= rowCount) {
  85. this.$row = rowCount - 1;
  86. return null;
  87. }
  88. this.$rowTokens = this.$session.getTokens(this.$row);
  89. this.$tokenIndex = 0;
  90. }
  91. return this.$rowTokens[this.$tokenIndex];
  92. };
  93. /**
  94. *
  95. * Returns the current tokenized string.
  96. * @returns {String}
  97. **/
  98. this.getCurrentToken = function () {
  99. return this.$rowTokens[this.$tokenIndex];
  100. };
  101. /**
  102. *
  103. * Returns the current row.
  104. * @returns {Number}
  105. **/
  106. this.getCurrentTokenRow = function () {
  107. return this.$row;
  108. };
  109. /**
  110. *
  111. * Returns the current column.
  112. * @returns {Number}
  113. **/
  114. this.getCurrentTokenColumn = function() {
  115. var rowTokens = this.$rowTokens;
  116. var tokenIndex = this.$tokenIndex;
  117. // If a column was cached by EditSession.getTokenAt, then use it
  118. var column = rowTokens[tokenIndex].start;
  119. if (column !== undefined)
  120. return column;
  121. column = 0;
  122. while (tokenIndex > 0) {
  123. tokenIndex -= 1;
  124. column += rowTokens[tokenIndex].value.length;
  125. }
  126. return column;
  127. };
  128. /**
  129. * Return the current token position.
  130. * @returns {Position}
  131. */
  132. this.getCurrentTokenPosition = function() {
  133. return {row: this.$row, column: this.getCurrentTokenColumn()};
  134. };
  135. }).call(TokenIterator.prototype);
  136. exports.TokenIterator = TokenIterator;
  137. });