JSDOC/Collapse.vala
[gnome.introspection-doc-generator] / JSDOC / Collapse.vala
1
2 /**
3  * 
4  * base class for parsing segments of token array..
5  * 
6  * 
7  * We want to make parsing the whole thing easy..
8  * 
9  * so we do various tricks:
10  * 
11  * 
12  * a) white space collased
13  *    wsPrefix 
14  * b)  toks
15  *     { } - collapse into first element.
16        ( ) - collapse into first element.
17        [ ] - collapse into first element.
18  * c) items = , seperation within the above..
19  * 
20  * usage: x = new Collapse(token_array)
21  * 
22  * 
23  * 
24  * 
25  */ 
26
27 namespace JSDOC {
28
29         public class  Collapse : TokenStream  {
30
31
32                 public Collapse(Gee.ArrayList<Token> tokens) 
33                 {
34                     parent(ar);;
35                     
36                     this.spaces();
37                     
38                     ar = this.collapse(this.tokens);
39                     
40                     this.tokens = ar;
41                     
42                    // console.dump(ar);
43                     
44                 }
45                 
46                 // put spaces into prefix of tokens..
47     
48         void spaces () 
49         {
50             var ar = new Gee.ArrayList<Token>();
51             var pref =  new Gee.ArrayList<Token>();
52             
53
54             
55             for (var i = 0; i < this.tokens.size; i ++) {
56                 var tok = this.tokens[i];
57                 if (tok.is("COMM") || tok.is("WHIT")) {
58                     pref.add(tok);
59                     continue;
60                 }
61                 tok.prefix = '';
62                 if (pref.size > 0) {
63                         foreach(e in pref) {
64                         tok.prefix += e.data;
65                     }
66                     pref =  new Gee.ArrayList<Token>(); // reset pref..
67                 }
68                 
69                 ar.add(tok);
70                 
71
72                 
73             }
74             this.tokens = ar;
75             
76         },
77         
78         
79         
80         Gee.ArrayList<Token>  collapse(ar) 
81         {
82             
83             var st = new TokenStream(ar);
84             var ret = new Gee.ArrayList<Token>();
85             
86             while (true) {
87                 var  tok = st.look(1,true);
88                 if (tok == null) {
89                   //  Seed.print(TokenStream.toString(ret));
90                     return ret;
91                 }
92                 // console.log(tok.data);
93                 
94                 switch(tok.type) {
95                     case "VOID": 
96                         return ret; //EOF
97                         
98                         
99                     case "KEYW": 
100                     case "TOKN":
101                     case "NAME":
102                     case "STRN":
103                     case "NUMB":
104                     case "REGX":
105                         ret.push(st.next(1));
106                         continue;
107                         
108                     case "PUNC":
109                         switch (tok.data) {
110                             case "[":
111                             case "{":
112                             case "(":
113                                 
114                                 var start = st.cursor;
115                                 st.next(1);
116                                 var add = st.balance(tok.data);
117                                // if (!add) {
118                                     //console.dump(tok);
119                                     //console.dump(start + '...' + st.cursor);
120                                     //console.dump(st.tokens);
121                                  
122                                 //}
123                                 if (add.size > 0) {
124                                         add.remove_at(0);  // remove the first element... (as it's the 
125                                 }
126                                 //Seed.print("ADD");
127                                 //Seed.print(JSON.stringify(add, null,4));
128                                 
129                                 
130                                 
131                                 var toks = add.size > 0 ? this.collapse(add) : add;
132                                 
133                                 tok.items = new Gee.ArrayList<Token>(); //?? needed?
134                                 tok.props = new Gee.HashMap<string,Gee.ArrayList<Token>>();
135                                  
136                                 
137                                 if (tok.data != "{") {
138                                     // paramters or array elements..
139                                     tok.items = this.toItems(toks, ","]);
140                                 } else {
141                                     // check for types.. it could be a list of statements.. or object
142                                     // format "{" "xXXX" ":" << looks for the ':'.. seems to work.. not sure if it's foolproof...
143                                     
144                                     var ost = new  TokenStream(toks);
145                                     //console.dump(ost.look(2,true) );
146                                     if (ost.look(2,true) && ost.look(2,true).data == ":") {
147                                                 // object properties...
148                                         tok.props = this.toProps(toks);
149                                     } else {
150                                         // list of statemetns..
151                                         tok.items = this.toItems(toks, ";{");;
152                                     }
153                                     
154                                     
155                                 }
156                                  
157                                 
158                                 
159                                 
160                                 
161                                 
162                                 
163                                 //Seed.print(" ADD : " + add.length  +  " ITEMS: " + tok.items.length);
164                                 
165                                 ret.push(tok);
166                                 
167                                 continue;
168                    
169                             default:
170                                 ret.push(st.next(1));
171                                 continue;
172                         }
173                         Seed.print("OOPS");
174                         continue;
175                     default : 
176                         Seed.print("OOPS" + tok.type);
177                         continue;
178                 }
179             }
180                 
181                 
182             
183             
184             
185             
186             
187             
188             
189             
190         },
191         toItems : function(ar,sep)
192         {
193             var ret = [];
194             var g = [];
195               
196             for (var i = 0; i < ar.length; i ++) {
197                 if (sep.indexOf(ar[i].data) < 0) {
198                     g.push(ar[i]);
199                     continue;
200                 }
201                 // var a=..., b =...
202                 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
203                     g.push(ar[i]);
204                     continue;
205                 }
206                 
207                 g.push(ar[i]);
208                 ret.push(g);
209                 g = [];
210                 
211             }
212             // last..
213             if (g.length) {
214                 ret.push(g);
215             }
216             return ret;
217             
218         },
219         toProps : function(ar)
220         {
221             
222             var ret = { }
223                
224             var g = { key : '', val: [] }
225                
226             
227             var k = '';
228             var state = 0;
229             for (var i = 0; i < ar.length; i ++) {
230                 
231                 switch(state) {
232                     case 0:
233                         k = ar[i].data;
234                         g.key = ar[i];
235                         state = 1;
236                         continue;
237                     case 1:
238                         state =2; // should be ':'
239                         continue;
240                     case 2:
241                         g.val.push(ar[i]);
242                         if (ar[i].data != ',') {
243                             continue;
244                         }
245                         ret[k] = g;
246                         g = { key : '', val: [] }
247                         state = 0;
248                         continue;
249                    
250                 }
251             }
252              // last.. - if g.val.length is 0 then it's a trailing ','...
253              // we should really throw a syntax error in that case..
254             if (k.length && g.val.length) {
255                 ret[k] = g;
256             }
257             return ret;
258             
259             
260         }
261
262     
263     
264 });