1 //<script type="text/javscript">
3 imports['Object.js'].load(Object);
5 JSDOC = imports['JSDOC.js'].JSDOC;
6 console = imports['console.js'].console;
7 Token = imports['JSDOC/Token.js'].Token;
8 Lang = imports['JSDOC/Lang.js'].Lang;
14 TokenStream = Object.define(
18 this.tokens = (tokens || []);
19 // Seed.print(this.tokens.length);
24 cursor : -1, // where are we in the stream.
33 look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
34 if (typeof n == "undefined") n = 0;
36 if (considerWhitespace == true) {
37 if (this.cursor+n < 0 || this.cursor+n > this.tokens.length) return {};
38 return this.tokens[this.cursor+n];
45 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
46 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
48 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
49 if (n < 0) i--; else i++;
53 if (count == Math.abs(n)) {
54 return this.tokens[i];
60 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
64 lookFor : function (data)
67 var i = this.cursor < 0 ? 0 : this.cursor ;
70 if (i >= this.tokens.length) return -1;
71 if (this.tokens[i].data == data) {
77 // should not get here!
84 * look ahead (or back) x number of tokens (which are not comment or whitespace)
87 lookTok : function(/**Number*/n) {
88 if (typeof n == "undefined") n = 1;
101 else if (i > this.tokens.length) return false;
103 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
104 if (n < 0) i--; else i++;
108 if (count == Math.abs(n)) {
109 return this.tokens[i];
115 return false; // because null isn't an object and caller always expects an object;
120 @type JSDOC.Token|JSDOC.Token[]| null!
122 next : function(/**Number*/howMany) {
123 if (typeof howMany == "undefined") howMany = 1;
124 if (howMany < 1) return null;
127 for (var i = 1; i <= howMany; i++) {
128 if (this.cursor+i >= this.tokens.length) {
131 got.push(this.tokens[this.cursor+i]);
133 this.cursor += howMany;
140 // what about comments after 'function'...
142 nextTok : function() {
143 return this.nextNonSpace();
145 nextNonSpace : function ()
153 if (tok.is('WHIT') || tok.is('COMM')) {
160 * @type JSDOC.Token[]
161 * @param start {String} token name or data (eg. '{'
162 * @param stop {String} (Optional) token name or data (eg. '}'
164 balance : function(/**String*/start, /**String*/stop) {
166 start = typeof(Lang.matching(start)) == 'undefined' ? Lang.punc(start) : start;
168 if (!stop) stop = Lang.matching(start);
173 //Seed.print("STOP:" + stop);
174 while ((token = this.look())) {
175 if (token.is(start)) {
184 if (token.is(stop)) {
186 if (depth == 0) return got;
188 if (!this.next()) break;
192 getMatchingToken : function(/**String*/start, /**String*/stop) {
194 var cursor = this.cursor;
197 start = Lang.matching(stop);
200 if (!stop) stop = Lang.matching(start);
202 while ((token = this.tokens[cursor])) {
203 if (token.is(start)) {
207 if (token.is(stop) && cursor) {
209 if (depth == 0) return this.tokens[cursor];
215 insertAhead : function(/**JSDOC.Token*/token) {
216 this.tokens.splice(this.cursor+1, 0, token);
219 remaining : function() {
222 var tok = this.look(1,true);
223 if (!tok || !tok.is || tok.is('VOID')) {
226 ret.push(this.next(1));
231 arrayToString : function(ar) {
232 console.log(typeof(ar));
234 ar.forEach(function(e) {
241 this.tokens.forEach(function(t) {