token_iterator.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * Tokenizes all the items from the current point to the row prior in the document.
  56. * @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.
  57. **/
  58. this.stepBackward = function() {
  59. this.$tokenIndex -= 1;
  60. while (this.$tokenIndex < 0) {
  61. this.$row -= 1;
  62. if (this.$row < 0) {
  63. this.$row = 0;
  64. return null;
  65. }
  66. this.$rowTokens = this.$session.getTokens(this.$row);
  67. this.$tokenIndex = this.$rowTokens.length - 1;
  68. }
  69. return this.$rowTokens[this.$tokenIndex];
  70. };
  71. /**
  72. * 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.
  73. * @returns {String}
  74. **/
  75. this.stepForward = function() {
  76. this.$tokenIndex += 1;
  77. var rowCount;
  78. while (this.$tokenIndex >= this.$rowTokens.length) {
  79. this.$row += 1;
  80. if (!rowCount)
  81. rowCount = this.$session.getLength();
  82. if (this.$row >= rowCount) {
  83. this.$row = rowCount - 1;
  84. return null;
  85. }
  86. this.$rowTokens = this.$session.getTokens(this.$row);
  87. this.$tokenIndex = 0;
  88. }
  89. return this.$rowTokens[this.$tokenIndex];
  90. };
  91. /**
  92. *
  93. * Returns the current tokenized string.
  94. * @returns {String}
  95. **/
  96. this.getCurrentToken = function () {
  97. return this.$rowTokens[this.$tokenIndex];
  98. };
  99. /**
  100. *
  101. * Returns the current row.
  102. * @returns {Number}
  103. **/
  104. this.getCurrentTokenRow = function () {
  105. return this.$row;
  106. };
  107. /**
  108. *
  109. * Returns the current column.
  110. * @returns {Number}
  111. **/
  112. this.getCurrentTokenColumn = function() {
  113. var rowTokens = this.$rowTokens;
  114. var tokenIndex = this.$tokenIndex;
  115. // If a column was cached by EditSession.getTokenAt, then use it
  116. var column = rowTokens[tokenIndex].start;
  117. if (column !== undefined)
  118. return column;
  119. column = 0;
  120. while (tokenIndex > 0) {
  121. tokenIndex -= 1;
  122. column += rowTokens[tokenIndex].value.length;
  123. }
  124. return column;
  125. };
  126. /**
  127. * Return the current token position.
  128. * @returns {Position}
  129. */
  130. this.getCurrentTokenPosition = function() {
  131. return {row: this.$row, column: this.getCurrentTokenColumn()};
  132. };
  133. }).call(TokenIterator.prototype);
  134. exports.TokenIterator = TokenIterator;
  135. });