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) {
124                                     add.shift();
125                                 }
126                                 //Seed.print("ADD");
127                                 //Seed.print(JSON.stringify(add, null,4));
128                                 
129                                 
130                                 
131                                 var toks = add ? this.collapse(add) : [];
132                                 tok.items = false;
133                                 tok.props = false;
134                                 
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                                     
143                                     var ost = new  TokenStream(toks);
144                                     //console.dump(ost.look(2,true) );
145                                     if (ost.look(2,true) && ost.look(2,true).data == ":") {
146                                         tok.props = this.toProps(toks);
147                                     } else {
148                                         // list of statemetns..
149                                         tok.items = this.toItems(toks,[ ';', '{'] );;
150                                     }
151                                     
152                                     
153                                 }
154                                  
155                                 
156                                 
157                                 
158                                 
159                                 
160                                 
161                                 //Seed.print(" ADD : " + add.length  +  " ITEMS: " + tok.items.length);
162                                 
163                                 ret.push(tok);
164                                 
165                                 continue;
166                    
167                             default:
168                                 ret.push(st.next(1));
169                                 continue;
170                         }
171                         Seed.print("OOPS");
172                         continue;
173                     default : 
174                         Seed.print("OOPS" + tok.type);
175                         continue;
176                 }
177             }
178                 
179                 
180             
181             
182             
183             
184             
185             
186             
187             
188         },
189         toItems : function(ar,sep)
190         {
191             var ret = [];
192             var g = [];
193               
194             for (var i = 0; i < ar.length; i ++) {
195                 if (sep.indexOf(ar[i].data) < 0) {
196                     g.push(ar[i]);
197                     continue;
198                 }
199                 // var a=..., b =...
200                 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
201                     g.push(ar[i]);
202                     continue;
203                 }
204                 
205                 g.push(ar[i]);
206                 ret.push(g);
207                 g = [];
208                 
209             }
210             // last..
211             if (g.length) {
212                 ret.push(g);
213             }
214             return ret;
215             
216         },
217         toProps : function(ar)
218         {
219             
220             var ret = { }
221                
222             var g = { key : '', val: [] }
223                
224             
225             var k = '';
226             var state = 0;
227             for (var i = 0; i < ar.length; i ++) {
228                 
229                 switch(state) {
230                     case 0:
231                         k = ar[i].data;
232                         g.key = ar[i];
233                         state = 1;
234                         continue;
235                     case 1:
236                         state =2; // should be ':'
237                         continue;
238                     case 2:
239                         g.val.push(ar[i]);
240                         if (ar[i].data != ',') {
241                             continue;
242                         }
243                         ret[k] = g;
244                         g = { key : '', val: [] }
245                         state = 0;
246                         continue;
247                    
248                 }
249             }
250              // last.. - if g.val.length is 0 then it's a trailing ','...
251              // we should really throw a syntax error in that case..
252             if (k.length && g.val.length) {
253                 ret[k] = g;
254             }
255             return ret;
256             
257             
258         }
259
260     
261     
262 });