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) {
36 // add semi-colon's where linebreaks are used... - not foolproof yet.!
37 if (tok.type == "NAME")) {
38 //var tokident = ts.look(-1).data + tok.data + ts.look(1).data + ts.look(2).data;
39 // a = new function() {}
40 if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == 'NEW' &&
41 ts.lookTok(3).name == 'FUNCTION') {
49 // if next is not ';' -> make it so...
51 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name = ="NEWLINE") {
52 ts.look(0).outData = ts.cur().data +";";
58 // a = function() { ...
60 if (ts.lookTok(1).data == '=' && ts.lookTok(2).name == "FUNCTION") {
62 //println("got = function() ");
67 // if next is not ';' -> make it so...
68 // although this var a=function(){},v,c; causes
69 if (!ts.lookTok(1).isData(';') && !ts.lookTok(1).isData('}') && ts.lookTok(1,true).isLineBreak()) {
70 ts.cur().outData = ts.look(0).data+";";
76 // function a () { ... };
78 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
80 //println("got = function() ");
85 // if next is not ';' -> make it so...
86 // although this var a=function(){},v,c; causes
87 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
88 ts.cur().outData = ts.cur().data+";";
98 if (ts.look(1).data == '=' && ts.look(2).data == '{')) {
100 //println("----------*** 3 *** --------------");
103 if (!ts.balance("{") ){
104 throw "could not find end lbrace!!!";
106 // if next is not ';' -> make it so...
108 if (!ts.look(1).data == ';' && ts.look(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
109 ts.look(0).outData = ts.look(0).data +";";
122 //println("got Token: " + tok.type);
126 switch(tok.data.toUpperCase()) {
127 // things that need space appending
131 // if next item is a identifier..
132 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
133 tok.outData = tok.data + " ";
138 case "RETURN": // if next item is not a semi; (or }
139 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
142 tok.outData = tok.data + " ";
147 case "ELSE": // if next item is not a semi; (or }
148 if (!ts.lookTok(1).name == "IF") {
152 tok.outData = tok.data + " ";
155 case "++": // if previous was a plus or next is a + add a space..
156 case "--": // if previous was a - or next is a - add a space..
158 var p = (tok.data == "--" ? '-' : '+');
160 if (ts.lookTok(1).data == p) {
161 tok.outData = tok.data + " ";
163 if (ts.lookTok(-1).data == p) {
164 tok.outData = " " + tok.data;
169 case "IN": // before and after??
172 tok.outData = " " + tok.data + " ";
175 case "VAR": // always after..
182 tok.outData = tok.data + " ";
186 case "TYPEOF": // what about typeof(
187 if (ts.lookTok(1).data != '(') {
188 tok.outData = tok.data + " ";
192 //remove semicolon before brace --
193 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
205 // NOW OUTPUT THE THING.
206 //var f = new File(minfile, File.NEW);
210 out.length = ts.slen; // prealloc.
213 var tok = ts.nextTok();
219 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue.length) {
220 //f.write(tok.identifier.mungedValue);
221 out += tok.identifier.mungedValue;
225 // at this point we can apply a text translation kit...
227 if (tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE') {
228 if (packer.stringHandler) {
229 out += packer.stringHandler(tok);
233 //f.write(tok.outData);
236 if ((tok.outData == ';') && (out.length - outoff > 255)) {
243 // remove the last ';' !!!
244 if (out.substring(out.length-1) == ';') {
245 return out.substring(0,out.length-1);