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);
47 var ar = new Gee.ArrayList<Token>()
48 var pref = new Gee.ArrayList<Token>();
52 for (var i = 0; i < this.tokens.size; i ++) {
53 var tok = this.tokens[i];
54 if (tok.is("COMM") || tok.is("WHIT")) {
63 pref = new Gee.ArrayList<Token>(); // reset pref..
74 collapse : function(ar) {
76 var st = new TokenStream(ar);
80 var tok = st.look(1,true);
81 if (!tok || !tok.is) {
82 // Seed.print(TokenStream.toString(ret));
85 // console.log(tok.data);
106 var start = st.cursor;
108 var add = st.balance(tok.data);
111 console.dump(start + '...' + st.cursor);
112 console.dump(st.tokens);
119 //Seed.print(JSON.stringify(add, null,4));
123 var toks = add ? this.collapse(add) : [];
129 if (tok.data != '{') {
130 // paramters or array elements..
131 tok.items = this.toItems(toks, [',']);
133 // check for types.. it could be a list of statements.. or object
135 var ost = new TokenStream(toks);
136 //console.dump(ost.look(2,true) );
137 if (ost.look(2,true) && ost.look(2,true).data == ":") {
138 tok.props = this.toProps(toks);
140 // list of statemetns..
141 tok.items = this.toItems(toks,[ ';', '{'] );;
153 //Seed.print(" ADD : " + add.length + " ITEMS: " + tok.items.length);
160 ret.push(st.next(1));
166 Seed.print("OOPS" + tok.type);
181 toItems : function(ar,sep)
186 for (var i = 0; i < ar.length; i ++) {
187 if (sep.indexOf(ar[i].data) < 0) {
192 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
209 toProps : function(ar)
214 var g = { key : '', val: [] }
219 for (var i = 0; i < ar.length; i ++) {
228 state =2; // should be ':'
232 if (ar[i].data != ',') {
236 g = { key : '', val: [] }
242 // last.. - if g.val.length is 0 then it's a trailing ','...
243 // we should really throw a syntax error in that case..
244 if (k.length && g.val.length) {