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!!!";
90 // if next is not ';' -> make it so...
91 // although this var a=function(){},v,c; causes
92 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
93 ts.look(0).outData = ts.look(0).data+";";
99 // next item is a name..
100 if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) && ts.look(1,true).name == "NEWLINE") {
101 // preserve linebraek
102 ts.look(0).outData = ts.look(0).data+"\n";
104 // method call followed by name..
105 if (ts.lookTok(1).data == "(") {
109 // although this var a=function(){},v,c; causes
111 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
113 ts.look(0).outData = ts.look(0).data+"\n";
121 // function a () { ... };
123 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
125 //println("got = function() ");
128 ts.balance("lparen");
129 ts.balance("lbrace");
130 // if next is not ';' -> make it so...
131 // although this var a=function(){},v,c; causes
132 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
133 ts.cur().outData = ts.cur().data+";";
143 if (ts.lookTok(1).data == '=' && ts.lookTok(2).data == '{') {
145 //println("----------*** 3 *** --------------");
148 if (!ts.balance("{") ){
150 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
153 throw "could not find end lbrace!!!";
155 // if next is not ';' -> make it so...
157 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
158 ts.look(0).outData = ts.look(0).data +";";
166 // a = function(....) { }
173 //println("got Token: " + tok.type);
177 switch(tok.data.toUpperCase()) {
178 // things that need space appending
182 // if next item is a identifier..
183 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
184 tok.outData = tok.data + " ";
189 case "RETURN": // if next item is not a semi; (or }
190 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
193 tok.outData = tok.data + " ";
198 case "ELSE": // if next item is not a semi; (or }
199 if (!ts.lookTok(1).name == "IF") {
203 tok.outData = tok.data + " ";
206 case "++": // if previous was a plus or next is a + add a space..
207 case "--": // if previous was a - or next is a - add a space..
209 var p = (tok.data == "--" ? '-' : '+');
211 if (ts.lookTok(1).data == p) {
212 tok.outData = tok.data + " ";
214 if (ts.lookTok(-1).data == p) {
215 tok.outData = " " + tok.data;
220 case "IN": // before and after??
223 tok.outData = " " + tok.data + " ";
226 case "VAR": // always after..
233 tok.outData = tok.data + " ";
237 case "TYPEOF": // what about typeof(
238 if (ts.lookTok(1).data != '(') {
239 tok.outData = tok.data + " ";
243 //remove semicolon before brace --
244 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
256 // NOW OUTPUT THE THING.
257 //var f = new File(minfile, File.NEW);
261 out.length = ts.slen; // prealloc.
264 var tok = ts.nextTok();
271 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
272 //f.write(tok.identifier.mungedValue);
273 out += tok.identifier.mungedValue;
277 // at this point we can apply a text translation kit...
279 if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
280 if (packer && packer.stringHandler) {
281 out += packer.stringHandler(tok);
286 out += tok.outData !== false ? tok.outData : tok.data;
288 if ((tok.outData == ';') && (out.length - outoff > 255)) {
295 // remove the last ';' !!!
296 if (out.substring(out.length-1) == ';') {
297 return out.substring(0,out.length-1);