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