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() { ...
76 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "FUNCTION") {
78 //println("got = function() ");
81 if (ts.balance("(").size < 1 ){
83 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
86 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
88 //print("AFTER BALANCE (");
89 //ts.dump(cu, ts.cursor);
90 //ts.cursor--; // cursor at the (
91 if (ts.balance("{").size < 1 ){
93 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
96 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
98 //print('FN: '+ts.tokens[cu].toString());
99 //print('F1: '+ts.lookTok(1).toString());
100 //print('F2: '+ts.look(1,true).toString());
102 // if next is not ';' -> make it so...
103 // although this var a=function(){},v,c; causes
104 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
106 ts.look(0,true).outData = ts.look(0,true).data+";";
107 // print("ADDING SEMI: " + ts.look(0).toString());
108 //ts.dump(cu, ts.cursor+2);
111 //ts.dump(cu, ts.cursor+2);
116 // next item is a name..
117 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
118 // preserve linebraek
119 ts.look(0,true).outData = ts.look(0,true).data+"\n";
121 // method call followed by name..
122 if (ts.lookTok(1).data == "(") {
126 // although this var a=function(){},v,c; causes
128 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
130 ts.look(0,true).outData = ts.look(0,true).data+"\n";
138 // function a () { ... };
140 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
142 //println("got = function() ");
145 ts.balance("lparen");
146 ts.balance("lbrace");
147 // if next is not ';' -> make it so...
148 // although this var a=function(){},v,c; causes
149 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
150 ts.cur().outData = ts.cur().data+";";
160 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
162 //println("----------*** 3 *** --------------");
165 if (ts.balance("{").size < 1 ){
167 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
170 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
172 // if next is not ';' -> make it so...
174 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
175 ts.look(0,true).outData = ts.look(0,true).data +";";
183 // a = function(....) { }
190 //println("got Token: " + tok.type);
194 switch(tok.data.up()) {
195 // things that need space appending
199 // if next item is a identifier..
200 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!!
201 tok.outData = tok.data + " ";
206 case "RETURN": // if next item is not a semi; (or }
207 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
210 tok.outData = tok.data + " ";
215 case "ELSE": // if next item is not a semi; (or }
216 if (ts.lookTok(1).name != "IF") {
219 // add a space if next element is 'IF'
220 tok.outData = tok.data + " ";
223 case "++": // if previous was a plus or next is a + add a space..
224 case "--": // if previous was a - or next is a - add a space..
226 var p = (tok.data == "--" ? "-" : "+");
228 if (ts.lookTok(1).data == p) {
229 tok.outData = tok.data + " ";
231 if (ts.lookTok(-1).data == p) {
232 tok.outData = " " + tok.data;
237 case "IN": // before and after??
240 tok.outData = " " + tok.data + " ";
243 case "VAR": // always after..
250 tok.outData = tok.data + " ";
254 case "TYPEOF": // what about typeof(
255 if (ts.lookTok(1).data != "(") {
256 tok.outData = tok.data + " ";
260 //remove semicolon before brace --
261 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
273 // NOW OUTPUT THE THING.
274 //var f = new File(minfile, File.NEW);
278 //try { out.length = ts.slen; } catch (e) {} // prealloc.
284 tok = keepWhite ? ts.next() : ts.nextTok();
289 if (tok.type == "COMM") {
293 ///print(tok.type + ':' + tok.data);
295 if (tok.type == "NAME" &&
296 tok.identifier != null &&
297 tok.identifier.mungedValue.length > 0) {
298 //f.write(tok.identifier.mungedValue);
299 //print("MUNGED: " + tok.identifier.mungedValue);
300 outstr += tok.identifier.mungedValue;
304 // at this point we can apply a text translation kit...
306 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
307 // if (packer && packer.stringHandler) {
308 // outstr += packer.stringHandler(tok);
313 outstr += tok.outData != "" ? tok.outData : tok.data;
315 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
316 outoff = outstr.length;
322 // remove the last ';' !!!
323 if (out.substring(out.length-1) == ';') {
324 return out.substring(0,out.length-1);