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() ");
83 print("CUR: %s\n", ts.lookTok(0).asString());
84 print("NEXT = should be brace: %s\n", ts.lookTok(1).asString());
88 if (ts.lookTok(1).data != "(" || ts.balance("(").size < 1 ){
89 print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
91 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
94 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
98 cu = ts.cursor; // set the cursor to here.. so the next bit of the code will check inside the method.
100 //print("AFTER BALANCE (");
101 //ts.dump(cu, ts.cursor);
102 //ts.cursor--; // cursor at the (
103 if (tok.data != "{" || ts.balance("{").size < 1 ){
106 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
109 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
111 //print('FN: '+ts.tokens[cu].toString());
112 //print('F1: '+ts.lookTok(1).toString());
113 //print('F2: '+ts.look(1,true).toString());
115 // if next is not ';' -> make it so...
116 // although this var a=function(){},v,c; causes
117 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
119 ts.look(0,true).outData = ts.look(0,true).data+";";
120 // print("ADDING SEMI: " + ts.look(0).toString());
121 //ts.dump(cu, ts.cursor+2);
124 //ts.dump(cu, ts.cursor+2);
129 // next item is a name..
130 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
131 // preserve linebraek
132 ts.look(0,true).outData = ts.look(0,true).data+"\n";
134 // method call followed by name..
135 if (ts.lookTok(1).data == "(") {
139 // although this var a=function(){},v,c; causes
141 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
143 ts.look(0,true).outData = ts.look(0,true).data+"\n";
151 // function a () { ... };
153 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
155 //println("got = function() ");
158 ts.balance("lparen");
159 ts.balance("lbrace");
160 // if next is not ';' -> make it so...
161 // although this var a=function(){},v,c; causes
162 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
163 ts.cur().outData = ts.cur().data+";";
173 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
175 //println("----------*** 3 *** --------------");
178 if (ts.balance("{").size < 1 ){
181 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
184 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
186 // if next is not ';' -> make it so...
188 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
189 ts.look(0,true).outData = ts.look(0,true).data +";";
197 // a = function(....) { }
204 //println("got Token: " + tok.type);
208 switch(tok.data.up()) {
209 // things that need space appending
213 // if next item is a identifier..
214 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!!
215 tok.outData = tok.data + " ";
220 case "RETURN": // if next item is not a semi; (or }
221 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
224 tok.outData = tok.data + " ";
229 case "ELSE": // if next item is not a semi; (or }
230 if (ts.lookTok(1).name != "IF") {
233 // add a space if next element is 'IF'
234 tok.outData = tok.data + " ";
237 case "++": // if previous was a plus or next is a + add a space..
238 case "--": // if previous was a - or next is a - add a space..
240 var p = (tok.data == "--" ? "-" : "+");
242 if (ts.lookTok(1).data == p) {
243 tok.outData = tok.data + " ";
245 if (ts.lookTok(-1).data == p) {
246 tok.outData = " " + tok.data;
251 case "IN": // before and after??
254 tok.outData = " " + tok.data + " ";
257 case "VAR": // always after..
264 tok.outData = tok.data + " ";
268 case "TYPEOF": // what about typeof(
269 if (ts.lookTok(1).data != "(") {
270 tok.outData = tok.data + " ";
274 //remove semicolon before brace --
275 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
287 // NOW OUTPUT THE THING.
288 //var f = new File(minfile, File.NEW);
292 //try { out.length = ts.slen; } catch (e) {} // prealloc.
298 tok = keepWhite ? ts.next() : ts.nextTok();
303 if (tok.type == "COMM") {
307 ///print(tok.type + ':' + tok.data);
309 if (tok.type == "NAME" &&
310 tok.identifier != null &&
311 tok.identifier.mungedValue.length > 0) {
312 //f.write(tok.identifier.mungedValue);
313 //print("MUNGED: " + tok.identifier.mungedValue);
314 outstr += tok.identifier.mungedValue;
318 // at this point we can apply a text translation kit...
320 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
321 // if (packer && packer.stringHandler) {
322 // outstr += packer.stringHandler(tok);
327 outstr += tok.outData != "" ? tok.outData : tok.data;
329 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
330 outoff = outstr.length;
336 // remove the last ';' !!!
337 if (out.substring(out.length-1) == ';') {
338 return out.substring(0,out.length-1);