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(var 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(Gee.ArrayList<Token>  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         // array of arrays of tokens
192         Gee.ArrayList<Gee.ArrayList<Token>>  toItems(Gee.ArrayList<Token>  ar, string sep)
193         {
194             var ret = new Gee.ArrayList<Gee.ArrayList<Token>>() ;
195             var g =  new Gee.ArrayList<Token>() ;
196               
197             for (var i = 0; i < ar.length; i ++) {
198                 if (sep.index_of(ar.get(i).data) < 0) {
199                     g.add(ar.get(i));
200                     continue;
201                 }
202                 // var a=..., b =...
203                 if ((ar.get(i).data != ";") && g.size> 0  && (g[0].name == "VAR")) {;
204                     g.add(ar.get(i));
205                     continue;
206                 }
207                 
208                 g.add(ar.get(i));
209                 ret.add(g);
210                 g =  new Gee.ArrayList<Token>() ;
211                 
212             }
213             // last..
214             if (g.size) {
215                 ret.add(g);
216             }
217             return ret;
218             
219         },
220         toProps : function(ar)
221         {
222             
223             var ret = { }
224                
225             var g = { key : '', val: [] }
226                
227             
228             var k = '';
229             var state = 0;
230             for (var i = 0; i < ar.length; i ++) {
231                 
232                 switch(state) {
233                     case 0:
234                         k = ar[i].data;
235                         g.key = ar[i];
236                         state = 1;
237                         continue;
238                     case 1:
239                         state =2; // should be ':'
240                         continue;
241                     case 2:
242                         g.val.push(ar[i]);
243                         if (ar[i].data != ',') {
244                             continue;
245                         }
246                         ret[k] = g;
247                         g = { key : '', val: [] }
248                         state = 0;
249                         continue;
250                    
251                 }
252             }
253              // last.. - if g.val.length is 0 then it's a trailing ','...
254              // we should really throw a syntax error in that case..
255             if (k.length && g.val.length) {
256                 ret[k] = g;
257             }
258             return ret;
259             
260             
261         }
262
263     
264     
265 });