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