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