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         void spaces () {
47             var ar = new Gee.ArrayList<Token>()
48             var pref =  new Gee.ArrayList<Token>();
49             
50
51             
52             for (var i = 0; i < this.tokens.size; i ++) {
53                 var tok = this.tokens[i];
54                 if (tok.is("COMM") || tok.is("WHIT")) {
55                     pref.add(tok);
56                     continue;
57                 }
58                 tok.prefix = '';
59                 if (pref.size > 0) {
60                     pref.forEach( function(e) {
61                         if (!e) {
62                             return;
63                         }
64                         tok.prefix += e.data;
65                     })
66                 }
67                 
68                 ar.push(tok);
69                 pref=  [];
70                 
71             }
72             this.tokens = ar;
73             
74         },
75         collapse : function(ar) {
76             
77             var st = new  TokenStream(ar);
78             var ret = [];
79             
80             while (true) {
81                 var  tok = st.look(1,true);
82                 if (!tok || !tok.is) {
83                   //  Seed.print(TokenStream.toString(ret));
84                     return ret;
85                 }
86                 // console.log(tok.data);
87                 switch(tok.type) {
88                     case "VOID": 
89                         return ret; //EOF
90                         
91                         
92                     case "KEYW": 
93                     case "TOKN":
94                     case "NAME":
95                     case "STRN":
96                     case "NUMB":
97                     case "REGX":
98                         ret.push(st.next(1));
99                         continue;
100                         
101                     case "PUNC":
102                         switch (tok.data) {
103                             case "[":
104                             case "{":
105                             case "(":
106                                 
107                                 var start = st.cursor;
108                                 st.next(1);
109                                 var add = st.balance(tok.data);
110                                 if (!add) {
111                                     console.dump(tok);
112                                     console.dump(start + '...' + st.cursor);
113                                     console.dump(st.tokens);
114                                  
115                                 }
116                                 if (add) {
117                                     add.shift();
118                                 }
119                                 //Seed.print("ADD");
120                                 //Seed.print(JSON.stringify(add, null,4));
121                                 
122                                 
123                                 
124                                 var toks = add ? this.collapse(add) : [];
125                                 tok.items = false;
126                                 tok.props = false;
127                                 
128                                 
129                                 
130                                 if (tok.data != '{') {
131                                     // paramters or array elements..
132                                     tok.items = this.toItems(toks, [',']);
133                                 } else {
134                                     // check for types.. it could be a list of statements.. or object
135                                     
136                                     var ost = new  TokenStream(toks);
137                                     //console.dump(ost.look(2,true) );
138                                     if (ost.look(2,true) && ost.look(2,true).data == ":") {
139                                         tok.props = this.toProps(toks);
140                                     } else {
141                                         // list of statemetns..
142                                         tok.items = this.toItems(toks,[ ';', '{'] );;
143                                     }
144                                     
145                                     
146                                 }
147                                  
148                                 
149                                 
150                                 
151                                 
152                                 
153                                 
154                                 //Seed.print(" ADD : " + add.length  +  " ITEMS: " + tok.items.length);
155                                 
156                                 ret.push(tok);
157                                 
158                                 continue;
159                    
160                             default:
161                                 ret.push(st.next(1));
162                                 continue;
163                         }
164                         Seed.print("OOPS");
165                         continue;
166                     default : 
167                         Seed.print("OOPS" + tok.type);
168                         continue;
169                 }
170             }
171                 
172                 
173             
174             
175             
176             
177             
178             
179             
180             
181         },
182         toItems : function(ar,sep)
183         {
184             var ret = [];
185             var g = [];
186               
187             for (var i = 0; i < ar.length; i ++) {
188                 if (sep.indexOf(ar[i].data) < 0) {
189                     g.push(ar[i]);
190                     continue;
191                 }
192                 // var a=..., b =...
193                 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
194                     g.push(ar[i]);
195                     continue;
196                 }
197                 
198                 g.push(ar[i]);
199                 ret.push(g);
200                 g = [];
201                 
202             }
203             // last..
204             if (g.length) {
205                 ret.push(g);
206             }
207             return ret;
208             
209         },
210         toProps : function(ar)
211         {
212             
213             var ret = { }
214                
215             var g = { key : '', val: [] }
216                
217             
218             var k = '';
219             var state = 0;
220             for (var i = 0; i < ar.length; i ++) {
221                 
222                 switch(state) {
223                     case 0:
224                         k = ar[i].data;
225                         g.key = ar[i];
226                         state = 1;
227                         continue;
228                     case 1:
229                         state =2; // should be ':'
230                         continue;
231                     case 2:
232                         g.val.push(ar[i]);
233                         if (ar[i].data != ',') {
234                             continue;
235                         }
236                         ret[k] = g;
237                         g = { key : '', val: [] }
238                         state = 0;
239                         continue;
240                    
241                 }
242             }
243              // last.. - if g.val.length is 0 then it's a trailing ','...
244              // we should really throw a syntax error in that case..
245             if (k.length && g.val.length) {
246                 ret[k] = g;
247             }
248             return ret;
249             
250             
251         }
252
253     
254     
255 });