1 // <script type="text/javascript">
4 * pack a javascript file, and return a shorter version!
6 * a bit picky at present with ; and crlf reading...
7 * @arg ts {TokenStream}
11 CompressWhite = function (ts, packer)
15 //var str = File.read(fn);
23 if (tok.type == "WHIT") {
28 // just spaces, not \n!
29 //if (tok.data.indexOf("\n") < 0) {
35 if (tok.data == "}") {
37 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
39 ts.look(0).outData = ts.look(0).data+"\n";
45 // add semi-colon's where linebreaks are used... - not foolproof yet.!
46 if (tok.type == "NAME") {
47 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
48 // a = new function() {}
49 if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == 'NEW' &&
50 ts.lookTok(3).name == 'FUNCTION') {
58 // if next is not ';' -> make it so...
60 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
61 ts.look(0).outData = ts.cur().data +";";
67 // a = function() { ...
69 if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == "FUNCTION") {
71 //println("got = function() ");
74 if (!ts.balance("(") ){
76 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
79 throw "could not find end lbrace!!!";
81 //print("AFTER BALANCE (");
82 //ts.dump(cu, ts.cursor);
83 //ts.cursor--; // cursor at the (
84 if (!ts.balance("{") ){
86 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
89 throw "could not find end lbrace!!!";
91 //print('FN: '+ts.tokens[cu].toString());
92 //print('F1: '+ts.lookTok(1).toString());
93 //print('F2: '+ts.look(1,true).toString());
95 // if next is not ';' -> make it so...
96 // although this var a=function(){},v,c; causes
97 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
99 ts.look(0).outData = ts.look(0).data+";";
100 // print("ADDING SEMI: " + ts.look(0).toString());
101 //ts.dump(cu, ts.cursor+2);
104 //ts.dump(cu, ts.cursor+2);
109 // next item is a name..
110 if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) && ts.look(1,true).name == "NEWLINE") {
111 // preserve linebraek
112 ts.look(0).outData = ts.look(0).data+"\n";
114 // method call followed by name..
115 if (ts.lookTok(1).data == "(") {
119 // although this var a=function(){},v,c; causes
121 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
123 ts.look(0).outData = ts.look(0).data+"\n";
131 // function a () { ... };
133 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
135 //println("got = function() ");
138 ts.balance("lparen");
139 ts.balance("lbrace");
140 // if next is not ';' -> make it so...
141 // although this var a=function(){},v,c; causes
142 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
143 ts.cur().outData = ts.cur().data+";";
153 if (ts.lookTok(1).data == '=' && ts.lookTok(2).data == '{') {
155 //println("----------*** 3 *** --------------");
158 if (!ts.balance("{") ){
160 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
163 throw "could not find end lbrace!!!";
165 // if next is not ';' -> make it so...
167 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
168 ts.look(0).outData = ts.look(0).data +";";
176 // a = function(....) { }
183 //println("got Token: " + tok.type);
187 switch(tok.data.toUpperCase()) {
188 // things that need space appending
192 // if next item is a identifier..
193 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
194 tok.outData = tok.data + " ";
199 case "RETURN": // if next item is not a semi; (or }
200 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
203 tok.outData = tok.data + " ";
208 case "ELSE": // if next item is not a semi; (or }
209 if (!ts.lookTok(1).name == "IF") {
213 tok.outData = tok.data + " ";
216 case "++": // if previous was a plus or next is a + add a space..
217 case "--": // if previous was a - or next is a - add a space..
219 var p = (tok.data == "--" ? '-' : '+');
221 if (ts.lookTok(1).data == p) {
222 tok.outData = tok.data + " ";
224 if (ts.lookTok(-1).data == p) {
225 tok.outData = " " + tok.data;
230 case "IN": // before and after??
233 tok.outData = " " + tok.data + " ";
236 case "VAR": // always after..
243 tok.outData = tok.data + " ";
247 case "TYPEOF": // what about typeof(
248 if (ts.lookTok(1).data != '(') {
249 tok.outData = tok.data + " ";
253 //remove semicolon before brace --
254 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
266 // NOW OUTPUT THE THING.
267 //var f = new File(minfile, File.NEW);
271 out.length = ts.slen; // prealloc.
274 var tok = ts.nextTok();
281 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
282 //f.write(tok.identifier.mungedValue);
283 out += tok.identifier.mungedValue;
287 // at this point we can apply a text translation kit...
289 if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
290 if (packer && packer.stringHandler) {
291 out += packer.stringHandler(tok);
296 out += tok.outData !== false ? tok.outData : tok.data;
298 if ((tok.outData == ';') && (out.length - outoff > 255)) {
305 // remove the last ';' !!!
306 if (out.substring(out.length-1) == ';') {
307 return out.substring(0,out.length-1);