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 //ts.dumpAllFlat(); GLib.Process.exit(1);
22 //var str = File.read(fn);
32 if (tok.type == "WHIT") {
38 // just spaces, not \n!
39 //if (tok.data.indexOf("\n") < 0) {
45 if (tok.data == "}") {
47 if (ts.lookTok(0).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
48 ts.look(0,true).outData = ts.look(0,true).data+"\n";
54 // add semi-colon's where linebreaks are used... - not foolproof yet.!
55 if (tok.type == "NAME") {
56 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
57 // a = new function() {}
58 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "NEW" &&
59 ts.lookTok(3).name == "FUNCTION") {
67 // if next is not ';' -> make it so...
69 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.lookTok(1).name == "NEWLINE") {
70 ts.look(0,true).outData = ts.lookTok(0).data +";";
76 // a = function() { ... -- add a semi colon a tthe end if not one there..
78 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "FUNCTION") {
80 //println("got = function() ");
86 print("NEXT = should be brac: %s\n", ts.lookTok(1).asString());
88 print("cursor = %d", ts.cursor);
90 if (ts.lookTok(1).data != "(" || ts.balance("(").size < 1 ){
91 print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
93 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
96 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
98 print("cursor = %d", ts.cursor);
99 print("CUR = should be ): %s\n", ts.lookTok(0).asString());
102 print("CUR = should be {: %s\n", ts.lookTok(0).asString());
103 cu = ts.cursor; // set the cursor to here.. so the next bit of the code will check inside the method.
104 print("cursor = %d", ts.cursor);
105 //print("AFTER BALANCE (");
106 //ts.dump(cu, ts.cursor);
107 //ts.cursor--; // cursor at the (
108 if (tok.data != "{" || ts.balance("{").size < 1 ){
111 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
114 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
116 //print('FN: '+ts.tokens[cu].toString());
117 //print('F1: '+ts.lookTok(1).toString());
118 //print('F2: '+ts.look(1,true).toString());
120 // if next is not ';' -> make it so...
121 // although this var a=function(){},v,c; causes
122 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
124 ts.look(0,true).outData = ts.look(0,true).data+";";
125 // print("ADDING SEMI: " + ts.look(0).toString());
126 //ts.dump(cu, ts.cursor+2);
129 //ts.dump(cu, ts.cursor+2);
134 // next item is a name..
135 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
136 // preserve linebraek
137 ts.look(0,true).outData = ts.look(0,true).data+"\n";
139 // method call followed by name..
140 if (ts.lookTok(1).data == "(") {
144 // although this var a=function(){},v,c; causes
146 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
148 ts.look(0,true).outData = ts.look(0,true).data+"\n";
156 // function a () { ... };
158 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
160 //println("got = function() ");
163 ts.balance("lparen");
164 ts.balance("lbrace");
165 // if next is not ';' -> make it so...
166 // although this var a=function(){},v,c; causes
167 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
168 ts.cur().outData = ts.cur().data+";";
178 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
180 //println("----------*** 3 *** --------------");
183 if (ts.balance("{").size < 1 ){
186 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
189 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
191 // if next is not ';' -> make it so...
193 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
194 ts.look(0,true).outData = ts.look(0,true).data +";";
202 // a = function(....) { }
209 //println("got Token: " + tok.type);
213 switch(tok.data.up()) {
214 // things that need space appending
218 // if next item is a identifier..
219 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!!
220 tok.outData = tok.data + " ";
225 case "RETURN": // if next item is not a semi; (or }
226 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
229 tok.outData = tok.data + " ";
234 case "ELSE": // if next item is not a semi; (or }
235 if (ts.lookTok(1).name != "IF") {
238 // add a space if next element is 'IF'
239 tok.outData = tok.data + " ";
242 case "++": // if previous was a plus or next is a + add a space..
243 case "--": // if previous was a - or next is a - add a space..
245 var p = (tok.data == "--" ? "-" : "+");
247 if (ts.lookTok(1).data == p) {
248 tok.outData = tok.data + " ";
250 if (ts.lookTok(-1).data == p) {
251 tok.outData = " " + tok.data;
256 case "IN": // before and after??
259 tok.outData = " " + tok.data + " ";
262 case "VAR": // always after..
269 tok.outData = tok.data + " ";
273 case "TYPEOF": // what about typeof(
274 if (ts.lookTok(1).data != "(") {
275 tok.outData = tok.data + " ";
279 //remove semicolon before brace --
280 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
292 // NOW OUTPUT THE THING.
293 //var f = new File(minfile, File.NEW);
297 //try { out.length = ts.slen; } catch (e) {} // prealloc.
303 tok = keepWhite ? ts.next() : ts.nextTok();
308 if (tok.type == "COMM") {
312 ///print(tok.type + ':' + tok.data);
314 if (tok.type == "NAME" &&
315 tok.identifier != null &&
316 tok.identifier.mungedValue.length > 0) {
317 //f.write(tok.identifier.mungedValue);
318 //print("MUNGED: " + tok.identifier.mungedValue);
319 outstr += tok.identifier.mungedValue;
323 // at this point we can apply a text translation kit...
325 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
326 // if (packer && packer.stringHandler) {
327 // outstr += packer.stringHandler(tok);
332 outstr += tok.outData != "" ? tok.outData : tok.data;
334 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
335 outoff = outstr.length;
341 // remove the last ';' !!!
342 if (out.substring(out.length-1) == ';') {
343 return out.substring(0,out.length-1);