1 //<script type="text/javscript">
2 const XObject = imports.XObject.XObject;
5 const console = imports.console.console;
7 // make sure parent is loaded..
8 const TokenStream = imports.TokenStream.TokenStream;
12 * base class for parsing segments of token array..
15 * We want to make parsing the whole thing easy..
17 * so we do various tricks:
20 * a) white space collased
23 * { } - collapse into first element.
24 ( ) - collapse into first element.
25 [ ] - collapse into first element.
26 * c) items = , seperation within the above..
28 * usage: x = new Collapse(token_array)
35 const Collapse = XObject.define(
38 Collapse.superclass.constructor.call(this, ar);
41 ar = this.collapse(this.tokens);
51 spaces : function () {
57 for (var i = 0; i < this.tokens.length; i ++) {
59 if (tok.is("COMM") || tok.is("WHIT")) {
65 pref.forEach( function(e) {
80 collapse : function(ar) {
82 var st = new TokenStream(ar);
86 var tok = st.look(1,true);
87 if (!tok || !tok.is) {
88 // Seed.print(TokenStream.toString(ret));
91 // console.log(tok.data);
103 ret.push(st.next(1));
112 var start = st.cursor;
114 var add = st.balance(tok.data);
117 console.dump(start + '...' + st.cursor);
118 console.dump(st.tokens);
125 //Seed.print(JSON.stringify(add, null,4));
129 var toks = add ? this.collapse(add) : [];
135 if (tok.data != '{') {
136 // paramters or array elements..
137 tok.items = this.toItems(toks, [',']);
139 // check for types.. it could be a list of statements.. or object
141 var ost = new TokenStream(toks);
142 //console.dump(ost.look(2,true) );
143 if (ost.look(2,true) && ost.look(2,true).data == ":") {
144 tok.props = this.toProps(toks);
146 // list of statemetns..
147 tok.items = this.toItems(toks,[ ';', '{'] );;
159 //Seed.print(" ADD : " + add.length + " ITEMS: " + tok.items.length);
166 ret.push(st.next(1));
172 Seed.print("OOPS" + tok.type);
187 toItems : function(ar,sep)
192 for (var i = 0; i < ar.length; i ++) {
193 if (sep.indexOf(ar[i].data) < 0) {
198 if ((ar[i].data != ';') && g.length && (g[0].name == "VAR")) {;
215 toProps : function(ar)
220 var g = { key : '', val: [] }
225 for (var i = 0; i < ar.length; i ++) {
234 state =2; // should be ':'
238 if (ar[i].data != ',') {
242 g = { key : '', val: [] }
248 // last.. - if g.val.length is 0 then it's a trailing ','...
249 // we should really throw a syntax error in that case..
250 if (k.length && g.val.length) {