4 * base class for parsing segments of token array..
7 * We want to make parsing the whole thing easy..
9 * so we do various tricks:
12 * a) white space collased
15 * { } - collapse into first element.
16 ( ) - collapse into first element.
17 [ ] - collapse into first element.
18 * c) items = , seperation within the above..
20 * usage: x = new Collapse(token_array)
29 public class Collapse : TokenStream {
32 public Collapse(Gee.ArrayList<Token> tokens)
38 ar = this.collapse(this.tokens);
46 // put spaces into prefix of tokens..
50 var ar = new Gee.ArrayList<Token>()
51 var pref = new Gee.ArrayList<Token>();
55 for (var i = 0; i < this.tokens.size; i ++) {
56 var tok = this.tokens[i];
57 if (tok.is("COMM") || tok.is("WHIT")) {
66 pref = new Gee.ArrayList<Token>(); // reset pref..
80 Gee.ArrayList<Token> collapse(ar)
83 var st = new TokenStream(ar);
84 var ret = new Gee.ArrayList<Token>();
87 var tok = st.look(1,true);
89 // Seed.print(TokenStream.toString(ret));
92 // console.log(tok.data);
105 ret.push(st.next(1));
114 var start = st.cursor;
116 var add = st.balance(tok.data);
119 //console.dump(start + '...' + st.cursor);
120 //console.dump(st.tokens);
124 add.remove_at(0); // remove the first element... (as it's the
127 //Seed.print(JSON.stringify(add, null,4));
131 var toks = add.size > 0 ? this.collapse(add) : add;
133 tok.items = new Gee.ArrayList<Token>(); //?? needed?
134 tok.props = new Gee.HashMap<string,Gee.ArrayList<Token>>();
137 if (tok.data != "{") {
138 // paramters or array elements..
139 tok.items = this.toItems(toks, [',']);
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...
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 tok.props = this.toProps(toks);
149 // list of statemetns..
150 tok.items = this.toItems(toks,[ ';', '{'] );;
162 //Seed.print(" ADD : " + add.length + " ITEMS: " + tok.items.length);
169 ret.push(st.next(1));
175 Seed.print("OOPS" + tok.type);
190 toItems : function(ar,sep)
195 for (var i = 0; i < ar.length; i ++) {
196 if (sep.indexOf(ar[i].data) < 0) {
201 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
218 toProps : function(ar)
223 var g = { key : '', val: [] }
228 for (var i = 0; i < ar.length; i ++) {
237 state =2; // should be ':'
241 if (ar[i].data != ',') {
245 g = { key : '', val: [] }
251 // last.. - if g.val.length is 0 then it's a trailing ','...
252 // we should really throw a syntax error in that case..
253 if (k.length && g.val.length) {