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() ");
85 if (ts.lookTok(1).data != "(" || ts.balance("(").size < 1 ){
87 print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
89 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
92 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
96 cu = ts.cursor; // set the cursor to here.. so the next bit of the code will check inside the method.
98 //print("AFTER BALANCE (");
99 //ts.dump(cu, ts.cursor);
100 //ts.cursor--; // cursor at the (
101 if (tok.data != "{" || ts.balance("{").size < 1 ){
104 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
107 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
109 //print('FN: '+ts.tokens[cu].toString());
110 //print('F1: '+ts.lookTok(1).toString());
111 //print('F2: '+ts.look(1,true).toString());
113 // if next is not ';' -> make it so...
114 // although this var a=function(){},v,c; causes
115 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
117 ts.look(0,true).outData = ts.look(0,true).data+";";
118 // print("ADDING SEMI: " + ts.look(0).toString());
119 //ts.dump(cu, ts.cursor+2);
122 //ts.dump(cu, ts.cursor+2);
127 // next item is a name..
128 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
129 // preserve linebraek
130 ts.look(0,true).outData = ts.look(0,true).data+"\n";
132 // method call followed by name..
133 if (ts.lookTok(1).data == "(") {
137 // although this var a=function(){},v,c; causes
139 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
141 ts.look(0,true).outData = ts.look(0,true).data+"\n";
149 // function a () { ... };
151 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
153 //println("got = function() ");
156 ts.balance("lparen");
157 ts.balance("lbrace");
158 // if next is not ';' -> make it so...
159 // although this var a=function(){},v,c; causes
160 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
161 ts.cur().outData = ts.cur().data+";";
171 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
173 //println("----------*** 3 *** --------------");
176 if (ts.balance("{").size < 1 ){
179 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
182 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
184 // if next is not ';' -> make it so...
186 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
187 ts.look(0,true).outData = ts.look(0,true).data +";";
195 // a = function(....) { }
202 //println("got Token: " + tok.type);
206 switch(tok.data.up()) {
207 // things that need space appending
211 // if next item is a identifier..
212 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!!
213 tok.outData = tok.data + " ";
218 case "RETURN": // if next item is not a semi; (or }
219 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
222 tok.outData = tok.data + " ";
227 case "ELSE": // if next item is not a semi; (or }
228 if (ts.lookTok(1).name != "IF") {
231 // add a space if next element is 'IF'
232 tok.outData = tok.data + " ";
235 case "++": // if previous was a plus or next is a + add a space..
236 case "--": // if previous was a - or next is a - add a space..
238 var p = (tok.data == "--" ? "-" : "+");
240 if (ts.lookTok(1).data == p) {
241 tok.outData = tok.data + " ";
243 if (ts.lookTok(-1).data == p) {
244 tok.outData = " " + tok.data;
249 case "IN": // before and after??
252 tok.outData = " " + tok.data + " ";
255 case "VAR": // always after..
262 tok.outData = tok.data + " ";
266 case "TYPEOF": // what about typeof(
267 if (ts.lookTok(1).data != "(") {
268 tok.outData = tok.data + " ";
272 //remove semicolon before brace --
273 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
285 // NOW OUTPUT THE THING.
286 //var f = new File(minfile, File.NEW);
290 //try { out.length = ts.slen; } catch (e) {} // prealloc.
296 tok = keepWhite ? ts.next() : ts.nextTok();
301 if (tok.type == "COMM") {
305 ///print(tok.type + ':' + tok.data);
307 if (tok.type == "NAME" &&
308 tok.identifier != null &&
309 tok.identifier.mungedValue.length > 0) {
310 //f.write(tok.identifier.mungedValue);
311 //print("MUNGED: " + tok.identifier.mungedValue);
312 outstr += tok.identifier.mungedValue;
316 // at this point we can apply a text translation kit...
318 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
319 // if (packer && packer.stringHandler) {
320 // outstr += packer.stringHandler(tok);
325 outstr += tok.outData != "" ? tok.outData : tok.data;
327 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
328 outoff = outstr.length;
334 // remove the last ';' !!!
335 if (out.substring(out.length-1) == ';') {
336 return out.substring(0,out.length-1);