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