4 * pack a javascript file, and return a shorter version!
6 * a bit picky at present with ; and crlf reading...
7 * @arg ts {TokenStream}
12 public errordomain CompressWhiteError {
16 public string CompressWhite (TokenStream ts, Packer packer, bool keepWhite) // throws CompressWhiteError,TokenStreamError
18 //keepWhite = keepWhite || false;
20 //var str = File.read(fn);
30 if (tok.type == "WHIT") {
36 // just spaces, not \n!
37 //if (tok.data.indexOf("\n") < 0) {
43 if (tok.data == "}") {
45 if (ts.lookTok(0).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
46 ts.look(0,true).outData = ts.look(0,true).data+"\n";
52 // add semi-colon's where linebreaks are used... - not foolproof yet.!
53 if (tok.type == "NAME") {
54 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
55 // a = new function() {}
56 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "NEW" &&
57 ts.lookTok(3).name == "FUNCTION") {
65 // if next is not ';' -> make it so...
67 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.lookTok(1).name == "NEWLINE") {
68 ts.look(0,true).outData = ts.lookTok(0).data +";";
74 // a = function() { ... -- add a semi colon a tthe end if not one there..
76 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "FUNCTION") {
78 //println("got = function() ");
84 print("CUR: %s\n", ts.lookTok(0).asString());
85 print("NEXT = should be brace: %s\n", ts.lookTok(1).asString());
89 if (tok.data != "(" || ts.balance("(").size < 1 ){
90 print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
92 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
95 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
99 cu = ts.cursor; // set the cursor to here.. so the next bit of the code will check inside the method.
101 //print("AFTER BALANCE (");
102 //ts.dump(cu, ts.cursor);
103 //ts.cursor--; // cursor at the (
104 if (tok.data != "{" || ts.balance("{").size < 1 ){
107 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
110 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
112 //print('FN: '+ts.tokens[cu].toString());
113 //print('F1: '+ts.lookTok(1).toString());
114 //print('F2: '+ts.look(1,true).toString());
116 // if next is not ';' -> make it so...
117 // although this var a=function(){},v,c; causes
118 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
120 ts.look(0,true).outData = ts.look(0,true).data+";";
121 // print("ADDING SEMI: " + ts.look(0).toString());
122 //ts.dump(cu, ts.cursor+2);
125 //ts.dump(cu, ts.cursor+2);
130 // next item is a name..
131 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
132 // preserve linebraek
133 ts.look(0,true).outData = ts.look(0,true).data+"\n";
135 // method call followed by name..
136 if (ts.lookTok(1).data == "(") {
140 // although this var a=function(){},v,c; causes
142 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
144 ts.look(0,true).outData = ts.look(0,true).data+"\n";
152 // function a () { ... };
154 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
156 //println("got = function() ");
159 ts.balance("lparen");
160 ts.balance("lbrace");
161 // if next is not ';' -> make it so...
162 // although this var a=function(){},v,c; causes
163 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
164 ts.cur().outData = ts.cur().data+";";
174 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
176 //println("----------*** 3 *** --------------");
179 if (ts.balance("{").size < 1 ){
182 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
185 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
187 // if next is not ';' -> make it so...
189 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
190 ts.look(0,true).outData = ts.look(0,true).data +";";
198 // a = function(....) { }
205 //println("got Token: " + tok.type);
209 switch(tok.data.up()) {
210 // things that need space appending
214 // if next item is a identifier..
215 if (ts.lookTok(1).type == "NAME" || Regex.match_simple("^[a-z]+$", ts.lookTok(1).data, GLib.RegexCompileFlags.CASELESS) ) { // as include is a keyword for us!!
216 tok.outData = tok.data + " ";
221 case "RETURN": // if next item is not a semi; (or }
222 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
225 tok.outData = tok.data + " ";
230 case "ELSE": // if next item is not a semi; (or }
231 if (ts.lookTok(1).name != "IF") {
234 // add a space if next element is 'IF'
235 tok.outData = tok.data + " ";
238 case "++": // if previous was a plus or next is a + add a space..
239 case "--": // if previous was a - or next is a - add a space..
241 var p = (tok.data == "--" ? "-" : "+");
243 if (ts.lookTok(1).data == p) {
244 tok.outData = tok.data + " ";
246 if (ts.lookTok(-1).data == p) {
247 tok.outData = " " + tok.data;
252 case "IN": // before and after??
255 tok.outData = " " + tok.data + " ";
258 case "VAR": // always after..
265 tok.outData = tok.data + " ";
269 case "TYPEOF": // what about typeof(
270 if (ts.lookTok(1).data != "(") {
271 tok.outData = tok.data + " ";
275 //remove semicolon before brace --
276 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
288 // NOW OUTPUT THE THING.
289 //var f = new File(minfile, File.NEW);
293 //try { out.length = ts.slen; } catch (e) {} // prealloc.
299 tok = keepWhite ? ts.next() : ts.nextTok();
304 if (tok.type == "COMM") {
308 ///print(tok.type + ':' + tok.data);
310 if (tok.type == "NAME" &&
311 tok.identifier != null &&
312 tok.identifier.mungedValue.length > 0) {
313 //f.write(tok.identifier.mungedValue);
314 //print("MUNGED: " + tok.identifier.mungedValue);
315 outstr += tok.identifier.mungedValue;
319 // at this point we can apply a text translation kit...
321 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
322 // if (packer && packer.stringHandler) {
323 // outstr += packer.stringHandler(tok);
328 outstr += tok.outData != "" ? tok.outData : tok.data;
330 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
331 outoff = outstr.length;
337 // remove the last ';' !!!
338 if (out.substring(out.length-1) == ';') {
339 return out.substring(0,out.length-1);