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() ");
80 ts.cursor+=2; // move two along, and balance...
82 if (ts.balance("(").size < 1 ){
84 print("balnaced issue\n");
86 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
89 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
91 //print("AFTER BALANCE (");
92 //ts.dump(cu, ts.cursor);
93 //ts.cursor--; // cursor at the (
94 if (ts.balance("{").size < 1 ){
97 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
100 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
102 //print('FN: '+ts.tokens[cu].toString());
103 //print('F1: '+ts.lookTok(1).toString());
104 //print('F2: '+ts.look(1,true).toString());
106 // if next is not ';' -> make it so...
107 // although this var a=function(){},v,c; causes
108 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
110 ts.look(0,true).outData = ts.look(0,true).data+";";
111 // print("ADDING SEMI: " + ts.look(0).toString());
112 //ts.dump(cu, ts.cursor+2);
115 //ts.dump(cu, ts.cursor+2);
120 // next item is a name..
121 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
122 // preserve linebraek
123 ts.look(0,true).outData = ts.look(0,true).data+"\n";
125 // method call followed by name..
126 if (ts.lookTok(1).data == "(") {
130 // although this var a=function(){},v,c; causes
132 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
134 ts.look(0,true).outData = ts.look(0,true).data+"\n";
142 // function a () { ... };
144 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
146 //println("got = function() ");
149 ts.balance("lparen");
150 ts.balance("lbrace");
151 // if next is not ';' -> make it so...
152 // although this var a=function(){},v,c; causes
153 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
154 ts.cur().outData = ts.cur().data+";";
164 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
166 //println("----------*** 3 *** --------------");
169 if (ts.balance("{").size < 1 ){
171 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
174 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
176 // if next is not ';' -> make it so...
178 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
179 ts.look(0,true).outData = ts.look(0,true).data +";";
187 // a = function(....) { }
194 //println("got Token: " + tok.type);
198 switch(tok.data.up()) {
199 // things that need space appending
203 // if next item is a identifier..
204 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!!
205 tok.outData = tok.data + " ";
210 case "RETURN": // if next item is not a semi; (or }
211 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
214 tok.outData = tok.data + " ";
219 case "ELSE": // if next item is not a semi; (or }
220 if (ts.lookTok(1).name != "IF") {
223 // add a space if next element is 'IF'
224 tok.outData = tok.data + " ";
227 case "++": // if previous was a plus or next is a + add a space..
228 case "--": // if previous was a - or next is a - add a space..
230 var p = (tok.data == "--" ? "-" : "+");
232 if (ts.lookTok(1).data == p) {
233 tok.outData = tok.data + " ";
235 if (ts.lookTok(-1).data == p) {
236 tok.outData = " " + tok.data;
241 case "IN": // before and after??
244 tok.outData = " " + tok.data + " ";
247 case "VAR": // always after..
254 tok.outData = tok.data + " ";
258 case "TYPEOF": // what about typeof(
259 if (ts.lookTok(1).data != "(") {
260 tok.outData = tok.data + " ";
264 //remove semicolon before brace --
265 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
277 // NOW OUTPUT THE THING.
278 //var f = new File(minfile, File.NEW);
282 //try { out.length = ts.slen; } catch (e) {} // prealloc.
288 tok = keepWhite ? ts.next() : ts.nextTok();
293 if (tok.type == "COMM") {
297 ///print(tok.type + ':' + tok.data);
299 if (tok.type == "NAME" &&
300 tok.identifier != null &&
301 tok.identifier.mungedValue.length > 0) {
302 //f.write(tok.identifier.mungedValue);
303 //print("MUNGED: " + tok.identifier.mungedValue);
304 outstr += tok.identifier.mungedValue;
308 // at this point we can apply a text translation kit...
310 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
311 // if (packer && packer.stringHandler) {
312 // outstr += packer.stringHandler(tok);
317 outstr += tok.outData != "" ? tok.outData : tok.data;
319 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
320 outoff = outstr.length;
326 // remove the last ';' !!!
327 if (out.substring(out.length-1) == ';') {
328 return out.substring(0,out.length-1);