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 -1)) {
38 return new Token("", "VOID", "START_OF_STREAM");
40 return this.tokens[this.cursor+n];
47 if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
48 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
50 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
51 if (n < 0) i--; else i++;
55 if (count == Math.abs(n)) {
56 return this.tokens[i];
62 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
66 lookFor : function (data)
69 var i = this.cursor < 0 ? 0 : this.cursor ;
72 if (i >= this.tokens.length) return -1;
73 if (this.tokens[i].data == data) {
79 // should not get here!
86 * look ahead (or back) x number of tokens (which are not comment or whitespace)
89 lookTok : function(/**Number*/n) {
90 if (typeof n == "undefined") n = 1;
102 return new Token("", "VOID", "END_OF_STREAM");
104 else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
106 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
107 if (n < 0) i--; else i++;
111 if (count == Math.abs(n)) {
112 return this.tokens[i];
117 // should never get here..
118 return false; // because null isn't an object and caller always expects an object;
123 @type JSDOC.Token|JSDOC.Token[]| null!
125 next : function(/**Number*/howMany) {
126 if (typeof howMany == "undefined") howMany = 1;
127 if (howMany < 1) return null;
130 for (var i = 1; i <= howMany; i++) {
131 if (this.cursor+i >= this.tokens.length) {
134 got.push(this.tokens[this.cursor+i]);
136 this.cursor += howMany;
143 // what about comments after 'function'...
145 nextTok : function() {
146 return this.nextNonSpace();
148 nextNonSpace : function ()
156 if (tok.is('WHIT') || tok.is('COMM')) {
163 * @type JSDOC.Token[]
164 * @param start {String} token name or data (eg. '{'
165 * @param stop {String} (Optional) token name or data (eg. '}'
167 balance : function(/**String*/start, /**String*/stop) {
169 start = typeof(Lang.matching(start)) == 'undefined' ? Lang.punc(start) : start;
171 if (!stop) stop = Lang.matching(start);
176 //Seed.print("STOP:" + stop);
177 while ((token = this.look())) {
178 if (token.is(start)) {
187 if (token.is(stop)) {
189 if (depth == 0) return got;
191 if (!this.next()) break;
195 getMatchingToken : function(/**String*/start, /**String*/stop) {
197 var cursor = this.cursor;
200 start = Lang.matching(stop);
203 if (!stop) stop = Lang.matching(start);
205 while ((token = this.tokens[cursor])) {
206 if (token.is(start)) {
210 if (token.is(stop) && cursor) {
212 if (depth == 0) return this.tokens[cursor];
218 insertAhead : function(/**JSDOC.Token*/token) {
219 this.tokens.splice(this.cursor+1, 0, token);
222 remaining : function() {
225 var tok = this.look(1,true);
226 if (!tok || !tok.is || tok.is('VOID')) {
229 ret.push(this.next(1));
234 arrayToString : function(ar) {
235 console.log(typeof(ar));
237 ar.forEach(function(e) {
244 this.tokens.forEach(function(t) {