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 if (!ts.balance("{") ){
83 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
86 throw "could not find end lbrace!!!";
88 print('F1: ts.lookTok(1).toString());
89 print('F2: ts.look(1,true).toString());
91 // if next is not ';' -> make it so...
92 // although this var a=function(){},v,c; causes
93 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
94 ts.look(0).outData = ts.look(0).data+";";
100 // next item is a name..
101 if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) && ts.look(1,true).name == "NEWLINE") {
102 // preserve linebraek
103 ts.look(0).outData = ts.look(0).data+"\n";
105 // method call followed by name..
106 if (ts.lookTok(1).data == "(") {
110 // although this var a=function(){},v,c; causes
112 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
114 ts.look(0).outData = ts.look(0).data+"\n";
122 // function a () { ... };
124 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
126 //println("got = function() ");
129 ts.balance("lparen");
130 ts.balance("lbrace");
131 // if next is not ';' -> make it so...
132 // although this var a=function(){},v,c; causes
133 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
134 ts.cur().outData = ts.cur().data+";";
144 if (ts.lookTok(1).data == '=' && ts.lookTok(2).data == '{') {
146 //println("----------*** 3 *** --------------");
149 if (!ts.balance("{") ){
151 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
154 throw "could not find end lbrace!!!";
156 // if next is not ';' -> make it so...
158 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
159 ts.look(0).outData = ts.look(0).data +";";
167 // a = function(....) { }
174 //println("got Token: " + tok.type);
178 switch(tok.data.toUpperCase()) {
179 // things that need space appending
183 // if next item is a identifier..
184 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
185 tok.outData = tok.data + " ";
190 case "RETURN": // if next item is not a semi; (or }
191 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
194 tok.outData = tok.data + " ";
199 case "ELSE": // if next item is not a semi; (or }
200 if (!ts.lookTok(1).name == "IF") {
204 tok.outData = tok.data + " ";
207 case "++": // if previous was a plus or next is a + add a space..
208 case "--": // if previous was a - or next is a - add a space..
210 var p = (tok.data == "--" ? '-' : '+');
212 if (ts.lookTok(1).data == p) {
213 tok.outData = tok.data + " ";
215 if (ts.lookTok(-1).data == p) {
216 tok.outData = " " + tok.data;
221 case "IN": // before and after??
224 tok.outData = " " + tok.data + " ";
227 case "VAR": // always after..
234 tok.outData = tok.data + " ";
238 case "TYPEOF": // what about typeof(
239 if (ts.lookTok(1).data != '(') {
240 tok.outData = tok.data + " ";
244 //remove semicolon before brace --
245 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
257 // NOW OUTPUT THE THING.
258 //var f = new File(minfile, File.NEW);
262 out.length = ts.slen; // prealloc.
265 var tok = ts.nextTok();
272 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
273 //f.write(tok.identifier.mungedValue);
274 out += tok.identifier.mungedValue;
278 // at this point we can apply a text translation kit...
280 if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
281 if (packer && packer.stringHandler) {
282 out += packer.stringHandler(tok);
287 out += tok.outData !== false ? tok.outData : tok.data;
289 if ((tok.outData == ';') && (out.length - outoff > 255)) {
296 // remove the last ';' !!!
297 if (out.substring(out.length-1) == ';') {
298 return out.substring(0,out.length-1);