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..
49 var ar = new Gee.ArrayList<Token>()
50 var pref = new Gee.ArrayList<Token>();
54 for (var i = 0; i < this.tokens.size; i ++) {
55 var tok = this.tokens[i];
56 if (tok.is("COMM") || tok.is("WHIT")) {
65 pref = new Gee.ArrayList<Token>(); // reset pref..
79 collapse : function(ar) {
81 var st = new TokenStream(ar);
85 var tok = st.look(1,true);
86 if (!tok || !tok.is) {
87 // Seed.print(TokenStream.toString(ret));
90 // console.log(tok.data);
102 ret.push(st.next(1));
111 var start = st.cursor;
113 var add = st.balance(tok.data);
116 console.dump(start + '...' + st.cursor);
117 console.dump(st.tokens);
124 //Seed.print(JSON.stringify(add, null,4));
128 var toks = add ? this.collapse(add) : [];
134 if (tok.data != '{') {
135 // paramters or array elements..
136 tok.items = this.toItems(toks, [',']);
138 // check for types.. it could be a list of statements.. or object
140 var ost = new TokenStream(toks);
141 //console.dump(ost.look(2,true) );
142 if (ost.look(2,true) && ost.look(2,true).data == ":") {
143 tok.props = this.toProps(toks);
145 // list of statemetns..
146 tok.items = this.toItems(toks,[ ';', '{'] );;
158 //Seed.print(" ADD : " + add.length + " ITEMS: " + tok.items.length);
165 ret.push(st.next(1));
171 Seed.print("OOPS" + tok.type);
186 toItems : function(ar,sep)
191 for (var i = 0; i < ar.length; i ++) {
192 if (sep.indexOf(ar[i].data) < 0) {
197 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
214 toProps : function(ar)
219 var g = { key : '', val: [] }
224 for (var i = 0; i < ar.length; i ++) {
233 state =2; // should be ':'
237 if (ar[i].data != ',') {
241 g = { key : '', val: [] }
247 // last.. - if g.val.length is 0 then it's a trailing ','...
248 // we should really throw a syntax error in that case..
249 if (k.length && g.val.length) {