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() ");
84 ts.cursor+=2; // move two along, and balance...
86 if (tok.data != "(" || ts.balance("(").size < 1 ){
88 print("balance ( issue on line %d\n", ts.toArray().get(cu).line);
90 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
93 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
96 //print("AFTER BALANCE (");
97 //ts.dump(cu, ts.cursor);
98 //ts.cursor--; // cursor at the (
99 if (tok.data != "{" || ts.balance("{").size < 1 ){
102 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
105 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
107 //print('FN: '+ts.tokens[cu].toString());
108 //print('F1: '+ts.lookTok(1).toString());
109 //print('F2: '+ts.look(1,true).toString());
111 // if next is not ';' -> make it so...
112 // although this var a=function(){},v,c; causes
113 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
115 ts.look(0,true).outData = ts.look(0,true).data+";";
116 // print("ADDING SEMI: " + ts.look(0).toString());
117 //ts.dump(cu, ts.cursor+2);
120 //ts.dump(cu, ts.cursor+2);
125 // next item is a name..
126 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
127 // preserve linebraek
128 ts.look(0,true).outData = ts.look(0,true).data+"\n";
130 // method call followed by name..
131 if (ts.lookTok(1).data == "(") {
135 // although this var a=function(){},v,c; causes
137 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
139 ts.look(0,true).outData = ts.look(0,true).data+"\n";
147 // function a () { ... };
149 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
151 //println("got = function() ");
154 ts.balance("lparen");
155 ts.balance("lbrace");
156 // if next is not ';' -> make it so...
157 // although this var a=function(){},v,c; causes
158 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
159 ts.cur().outData = ts.cur().data+";";
169 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
171 //println("----------*** 3 *** --------------");
174 if (ts.balance("{").size < 1 ){
177 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
180 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
182 // if next is not ';' -> make it so...
184 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
185 ts.look(0,true).outData = ts.look(0,true).data +";";
193 // a = function(....) { }
200 //println("got Token: " + tok.type);
204 switch(tok.data.up()) {
205 // things that need space appending
209 // if next item is a identifier..
210 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!!
211 tok.outData = tok.data + " ";
216 case "RETURN": // if next item is not a semi; (or }
217 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
220 tok.outData = tok.data + " ";
225 case "ELSE": // if next item is not a semi; (or }
226 if (ts.lookTok(1).name != "IF") {
229 // add a space if next element is 'IF'
230 tok.outData = tok.data + " ";
233 case "++": // if previous was a plus or next is a + add a space..
234 case "--": // if previous was a - or next is a - add a space..
236 var p = (tok.data == "--" ? "-" : "+");
238 if (ts.lookTok(1).data == p) {
239 tok.outData = tok.data + " ";
241 if (ts.lookTok(-1).data == p) {
242 tok.outData = " " + tok.data;
247 case "IN": // before and after??
250 tok.outData = " " + tok.data + " ";
253 case "VAR": // always after..
260 tok.outData = tok.data + " ";
264 case "TYPEOF": // what about typeof(
265 if (ts.lookTok(1).data != "(") {
266 tok.outData = tok.data + " ";
270 //remove semicolon before brace --
271 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
283 // NOW OUTPUT THE THING.
284 //var f = new File(minfile, File.NEW);
288 //try { out.length = ts.slen; } catch (e) {} // prealloc.
294 tok = keepWhite ? ts.next() : ts.nextTok();
299 if (tok.type == "COMM") {
303 ///print(tok.type + ':' + tok.data);
305 if (tok.type == "NAME" &&
306 tok.identifier != null &&
307 tok.identifier.mungedValue.length > 0) {
308 //f.write(tok.identifier.mungedValue);
309 //print("MUNGED: " + tok.identifier.mungedValue);
310 outstr += tok.identifier.mungedValue;
314 // at this point we can apply a text translation kit...
316 //if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
317 // if (packer && packer.stringHandler) {
318 // outstr += packer.stringHandler(tok);
323 outstr += tok.outData != "" ? tok.outData : tok.data;
325 if ((tok.outData == ";") && (outstr.length - outoff > 255)) {
326 outoff = outstr.length;
332 // remove the last ';' !!!
333 if (out.substring(out.length-1) == ';') {
334 return out.substring(0,out.length-1);