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 {
15 public class CompressWhite : Object
17 public CompressWhite (TokenStream ts, Packer packer, bool keepWhite) throws CompressWhiteError,TokenStreamError
19 //keepWhite = keepWhite || false;
21 //var str = File.read(fn);
31 if (tok.type == "WHIT") {
37 // just spaces, not \n!
38 //if (tok.data.indexOf("\n") < 0) {
44 if (tok.data == "}") {
46 if (ts.lookTok(0).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
47 ts.look(0,true).outData = ts.look(0,true).data+"\n";
53 // add semi-colon's where linebreaks are used... - not foolproof yet.!
54 if (tok.type == "NAME") {
55 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
56 // a = new function() {}
57 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "NEW" &&
58 ts.lookTok(3).name == "FUNCTION") {
66 // if next is not ';' -> make it so...
68 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.lookTok(1).name == "NEWLINE") {
69 ts.look(0,true).outData = ts.lookTok(0).data +";";
75 // a = function() { ...
77 if (ts.lookTok(1).data == "=" && ts.lookTok(2).name == "FUNCTION") {
79 //println("got = function() ");
82 if (ts.balance("(").size < 1 ){
84 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
87 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!" );
89 //print("AFTER BALANCE (");
90 //ts.dump(cu, ts.cursor);
91 //ts.cursor--; // cursor at the (
92 if (ts.balance("{").size < 1 ){
94 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
97 throw new CompressWhiteError.BRACE( "could not find end lbrace!!!");
99 //print('FN: '+ts.tokens[cu].toString());
100 //print('F1: '+ts.lookTok(1).toString());
101 //print('F2: '+ts.look(1,true).toString());
103 // if next is not ';' -> make it so...
104 // although this var a=function(){},v,c; causes
105 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name == "NEWLINE") {
107 ts.look(0,true).outData = ts.look(0,true).data+";";
108 // print("ADDING SEMI: " + ts.look(0).toString());
109 //ts.dump(cu, ts.cursor+2);
112 //ts.dump(cu, ts.cursor+2);
117 // next item is a name..
118 if ((ts.lookTok(1).type == "NAME" || ts.lookTok(1).type == "KEYW" ) && ts.look(1,true).name == "NEWLINE") {
119 // preserve linebraek
120 ts.look(0,true).outData = ts.look(0,true).data+"\n";
122 // method call followed by name..
123 if (ts.lookTok(1).data == "(") {
127 // although this var a=function(){},v,c; causes
129 if (ts.lookTok(1).type == "NAME" && ts.look(1,true).name == "NEWLINE") {
131 ts.look(0,true).outData = ts.look(0,true).data+"\n";
139 // function a () { ... };
141 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
143 //println("got = function() ");
146 ts.balance("lparen");
147 ts.balance("lbrace");
148 // if next is not ';' -> make it so...
149 // although this var a=function(){},v,c; causes
150 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
151 ts.cur().outData = ts.cur().data+";";
161 if (ts.lookTok(1).data == "=" && ts.lookTok(2).data == "{") {
163 //println("----------*** 3 *** --------------");
166 if (ts.balance("{").size < 1 ){
168 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
171 throw new CompressWhiteError.BRACE("could not find end lbrace!!!");
173 // if next is not ';' -> make it so...
175 if (ts.lookTok(1).data != ";" && ts.lookTok(1).data != "}" && ts.look(1,true).name=="NEWLINE") {
176 ts.look(0,true).outData = ts.look(0,true).data +";";
184 // a = function(....) { }
191 //println("got Token: " + tok.type);
195 switch(tok.data.up()) {
196 // things that need space appending
200 // if next item is a identifier..
201 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!!
202 tok.outData = tok.data + " ";
207 case "RETURN": // if next item is not a semi; (or }
208 if (ts.lookTok(1).data == ";" || ts.lookTok(1).data == "}") {
211 tok.outData = tok.data + " ";
216 case "ELSE": // if next item is not a semi; (or }
217 if (ts.lookTok(1).name != "IF") {
220 // add a space if next element is 'IF'
221 tok.outData = tok.data + " ";
224 case "++": // if previous was a plus or next is a + add a space..
225 case "--": // if previous was a - or next is a - add a space..
227 var p = (tok.data == "--" ? "-" : "+");
229 if (ts.lookTok(1).data == p) {
230 tok.outData = tok.data + " ";
232 if (ts.lookTok(-1).data == p) {
233 tok.outData = " " + tok.data;
238 case "IN": // before and after??
241 tok.outData = " " + tok.data + " ";
244 case "VAR": // always after..
251 tok.outData = tok.data + " ";
255 case "TYPEOF": // what about typeof(
256 if (ts.lookTok(1).data != "(") {
257 tok.outData = tok.data + " ";
261 //remove semicolon before brace --
262 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
274 // NOW OUTPUT THE THING.
275 //var f = new File(minfile, File.NEW);
279 //try { out.length = ts.slen; } catch (e) {} // prealloc.
285 tok = keepWhite ? ts.next() : ts.nextTok();
290 if (tok.type == "COMM") {
294 ///print(tok.type + ':' + tok.data);
296 if (tok.type == "NAME" && tok.identifier
297 && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
298 //f.write(tok.identifier.mungedValue);
299 //print("MUNGED: " + tok.identifier.mungedValue);
300 out += tok.identifier.mungedValue;
304 // at this point we can apply a text translation kit...
306 if ((tok.type == "STRN") && (tok.name== "DOUBLE_QUOTE")) {
307 if (packer && packer.stringHandler) {
308 out += packer.stringHandler(tok);
313 out += tok.outData != "" ? tok.outData : tok.data;
315 if ((tok.outData == ";") && (out.length - outoff > 255)) {
322 // remove the last ';' !!!
323 if (out.substring(out.length-1) == ';') {
324 return out.substring(0,out.length-1);