JSDOC/TokenStream.js
[app.jsdoc] / JSDOC / TokenStream.js
1 //<script type="text/javscript">
2
3
4 XObject = imports.XObject.XObject;
5  
6
7
8 console = imports.console.console;
9 Token   = imports.Token.Token;
10 Lang    = imports.Lang.Lang;
11
12
13 /**
14  * @class TokenStream
15  * @scope JSDOC
16
17  * 
18  * BC notes:
19  * 
20  * nextT => nextTok
21  * lookT => lookTok
22  * @param {Object} cfg Configuration
23  */
24 TokenStream = XObject.define(
25
26     function(cfg)
27     {
28         XObject.extend(this, cfg);
29         
30         //this.tokens =  this.tokens || []);
31        // Seed.print(this.tokens.length);
32         this.rewind();
33     },
34     Object,
35     {
36         /**
37          * @cfg {Array} tokens Original Tokens from tokenizer
38          */
39         tokens : false,
40         /**
41          * @cfg {String} filename srcFile the source file
42          */
43         
44         filename : '',
45         
46         /**
47          * @type {Number} The current position in the stream.
48          */
49         
50         cursor : -1, // where are we in the stream.
51         
52         rewind : function() {
53             this.cursor = -1;
54         },
55
56         /**
57             @type JSDOC.Token
58         */
59         look : function(/**Number*/n, /**Boolean*/considerWhitespace) {
60             if (typeof n == "undefined") n = 0;
61
62             if (considerWhitespace == true) {
63                 if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
64                     return new Token("", "VOID", "START_OF_STREAM");
65                 }
66                 return this.tokens[this.cursor+n];
67             }
68             else {
69                 var count = 0;
70                 var i = this.cursor;
71
72                 while (true) {
73                     if (i < 0) return new Token("", "VOID", "START_OF_STREAM");
74                     else if (i > this.tokens.length) return new Token("", "VOID", "END_OF_STREAM");
75
76                     if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT"))) {
77                         if (n < 0) i--; else i++;
78                         continue;
79                     }
80                     
81                     if (count == Math.abs(n)) {
82                         return this.tokens[i];
83                     }
84                     count++;
85                     (n < 0)? i-- : i++;
86                 }
87
88                 return new Token("", "VOID", "STREAM_ERROR"); // because null isn't an object and caller always expects an object
89             }
90         },
91
92         lookFor : function (data)
93         {
94             // non tree version..
95             var i = this.cursor < 0 ? 0 : this.cursor ;
96             
97             while (true) {
98                 if (i >= this.tokens.length) return -1;
99                 if (this.tokens[i].data == data) {
100                     return i;
101                 }
102                 i++;
103                 
104             }
105             // should not get here!
106             return -1;
107
108         },
109
110
111         /**
112          * look ahead (or back) x number of tokens (which are not comment or whitespace)
113          * ?? used any more?
114          */
115         lookTok : function(/**Number*/n) {
116             if (typeof n == "undefined") n = 1;
117
118             
119             var count = 0;
120             var i = this.cursor;
121
122             while (true) {
123                // print(i);
124                 if (i < 0) {
125                     if (n > -1) {
126                         i = 0; 
127                         count++;
128                         continue;
129                         
130                     }
131                     return   new Token("", "VOID", "END_OF_STREAM");
132                 }
133                 else if (i > this.tokens.length) return  new Token("", "VOID", "END_OF_STREAM");
134
135                 if (i != this.cursor && (this.tokens[i] === undefined || this.tokens[i].is("WHIT") || this.tokens[i].is("COMM"))) {
136                     if (n < 0) i--; else i++;
137                     continue;
138                 }
139                 
140                 if (count == Math.abs(n)) {
141                     return this.tokens[i];
142                 }
143                 count++;
144                 (n < 0)? i-- : i++;
145             }
146         // should never get here..
147             return false; // because null isn't an object and caller always expects an object;
148             
149         },
150
151         /**
152          *  @return {Token|null}
153          * next token (with white space)
154          */
155             
156            
157         next : function(/**Number*/howMany) {
158             if (typeof howMany == "undefined") howMany = 1;
159             if (howMany < 1) return null;
160             var got = [];
161
162             for (var i = 1; i <= howMany; i++) {
163                 if (this.cursor+i >= this.tokens.length) {
164                     return null;
165                 }
166                 got.push(this.tokens[this.cursor+i]);
167             }
168             this.cursor += howMany;
169
170             if (howMany == 1) {
171                 return got[0];
172             }
173             else return got;
174         },
175         // what about comments after 'function'...
176         // is this used ???
177         nextTok  : function() {
178             return this.nextNonSpace();
179         },
180         nextNonSpace : function ()
181         {
182             
183             while (true) {
184                 tok = this.next(1);
185                 if (!tok) {
186                     return false;
187                 }
188                 if (tok.is('WHIT') ||  tok.is('COMM')) {
189                     continue;
190                 }
191                 return tok;
192             }
193         },
194         /**
195          * @type JSDOC.Token[]
196          * @param start {String}  token name or data (eg. '{'
197          * @param stop {String} (Optional) token name or data (eg. '}'
198          */
199         balance : function(/**String*/start, /**String*/stop) {
200             
201             
202             start = typeof(Lang.punc(start)) == 'undefined' ? start : Lang.punc(start);
203             
204             if (!stop) stop = Lang.matching(start);
205             
206             var depth = 0;
207             var got = [];
208             var started = false;
209             //Seed.print("START:" + start);
210             //Seed.print("STOP:" + stop);
211             while ((token = this.look())) {
212                 if (token.is(start)) {
213               //      Seed.print("balance: START : " + depth + " " + token.data);
214                     depth++;
215                     started = true;
216                 }
217                 
218                 if (started) {
219                     got.push(token);
220                 }
221                 
222                 if (token.is(stop)) {
223                     depth--;
224                 //    Seed.print("balance: STOP: "  + depth + " " + token.data);
225                     if (depth < 1) return got;
226                 }
227                 if (!this.next()) break;
228             }
229         },
230
231         getMatchingToken : function(/**String*/start, /**String*/stop) {
232             var depth = 0;
233             var cursor = this.cursor;
234             
235             if (!start) {
236                 start = Lang.matching(stop);
237                 depth = 1;
238             }
239             if (!stop) stop = Lang.matching(start);
240             
241             while ((token = this.tokens[cursor])) {
242                 if (token.is(start)) {
243                     depth++;
244                 }
245                 
246                 if (token.is(stop) && cursor) {
247                     depth--;
248                     if (depth == 0) return this.tokens[cursor];
249                 }
250                 cursor++;
251             }
252         },
253
254         insertAhead : function(/**JSDOC.Token*/token) {
255             this.tokens.splice(this.cursor+1, 0, token);
256         },
257          
258         remaining : function() {
259             var ret = [];
260             while (true) {
261                 var tok = this.look(1,true);
262                 if (!tok || !tok.is || tok.is('VOID')) {
263                     return ret;
264                 }
265                 ret.push(this.next(1));
266             }
267         },
268          
269
270         arrayToString : function(ar) {
271             console.log(typeof(ar));
272             var ret = [];
273             ar.forEach(function(e) {
274                 ret.push(e.data);
275             });
276             return ret.join('');
277         },
278         dump: function(start, end)
279         {
280             start = Math.max(start || 0, 0);
281             end = Math.min(end || this.tokens.length, this.tokens.length);
282             var out='';
283             for (var i =start;i < end; i++) {
284                 
285                 out += (this.tokens[i].outData == false) ? this.tokens[i].data : this.tokens[i].outData;
286             };
287             print(out);
288         }
289 });
290