ace/lib/ace/token_iterator.js
2012-09-06 22:48:34 +02:00

154 lines
5.2 KiB
JavaScript

/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
/**
* class TokenIterator
*
* This class provides an essay way to treat the document as a stream of tokens, and provides methods to iterate over these tokens.
*
**/
/**
* new TokenIterator(session, initialRow, initialColumn)
* - session (EditSession): The session to associate with
* - initialRow (Number): The row to start the tokenizing at
* - initialColumn (Number): The column to start the tokenizing at
*
* Creates a new token iterator object. The inital token index is set to the provided row and column coordinates.
*
**/
var TokenIterator = function(session, initialRow, initialColumn) {
this.$session = session;
this.$row = initialRow;
this.$rowTokens = session.getTokens(initialRow);
var token = session.getTokenAt(initialRow, initialColumn);
this.$tokenIndex = token ? token.index : -1;
};
(function() {
/**
* TokenIterator.stepBackward() -> [String]
* + (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.
*
* Tokenizes all the items from the current point to the row prior in the document.
**/
this.stepBackward = function() {
this.$tokenIndex -= 1;
while (this.$tokenIndex < 0) {
this.$row -= 1;
if (this.$row < 0) {
this.$row = 0;
return null;
}
this.$rowTokens = this.$session.getTokens(this.$row);
this.$tokenIndex = this.$rowTokens.length - 1;
}
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.stepForward() -> String
*
* 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.
**/
this.stepForward = function() {
var rowCount = this.$session.getLength();
this.$tokenIndex += 1;
while (this.$tokenIndex >= this.$rowTokens.length) {
this.$row += 1;
if (this.$row >= rowCount) {
this.$row = rowCount - 1;
return null;
}
this.$rowTokens = this.$session.getTokens(this.$row);
this.$tokenIndex = 0;
}
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.getCurrentToken() -> String
*
* Returns the current tokenized string.
*
**/
this.getCurrentToken = function () {
return this.$rowTokens[this.$tokenIndex];
};
/**
* TokenIterator.getCurrentTokenRow() -> Number
*
* Returns the current row.
*
**/
this.getCurrentTokenRow = function () {
return this.$row;
};
/**
* TokenIterator.getCurrentTokenColumn() -> Number
*
* Returns the current column.
*
**/
this.getCurrentTokenColumn = function() {
var rowTokens = this.$rowTokens;
var tokenIndex = this.$tokenIndex;
// If a column was cached by EditSession.getTokenAt, then use it
var column = rowTokens[tokenIndex].start;
if (column !== undefined)
return column;
column = 0;
while (tokenIndex > 0) {
tokenIndex -= 1;
column += rowTokens[tokenIndex].value.length;
}
return column;
};
}).call(TokenIterator.prototype);
exports.TokenIterator = TokenIterator;
});