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