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('FN: '+ts.tokens[cu].toString());
89 print('F1: '+ts.lookTok(1).toString());
90 print('F2: '+ts.look(1,true).toString());
92 // if next is not ';' -> make it so...
93 // although this var a=function(){},v,c; causes
94 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name == "NEWLINE") {
96 ts.look(0).outData = ts.look(0).data+";";
97 print("ADDING SEMI: " + ts.look(0).toString());
103 // next item is a name..
104 if ((ts.lookTok(1).type == 'NAME' || ts.lookTok(1).type == 'KEYW' ) && ts.look(1,true).name == "NEWLINE") {
105 // preserve linebraek
106 ts.look(0).outData = ts.look(0).data+"\n";
108 // method call followed by name..
109 if (ts.lookTok(1).data == "(") {
113 // although this var a=function(){},v,c; causes
115 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
117 ts.look(0).outData = ts.look(0).data+"\n";
125 // function a () { ... };
127 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
129 //println("got = function() ");
132 ts.balance("lparen");
133 ts.balance("lbrace");
134 // if next is not ';' -> make it so...
135 // although this var a=function(){},v,c; causes
136 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
137 ts.cur().outData = ts.cur().data+";";
147 if (ts.lookTok(1).data == '=' && ts.lookTok(2).data == '{') {
149 //println("----------*** 3 *** --------------");
152 if (!ts.balance("{") ){
154 print(">>>>>>>>>>>>>>>>>HERE>>>>>>>>>>>>");
157 throw "could not find end lbrace!!!";
159 // if next is not ';' -> make it so...
161 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
162 ts.look(0).outData = ts.look(0).data +";";
170 // a = function(....) { }
177 //println("got Token: " + tok.type);
181 switch(tok.data.toUpperCase()) {
182 // things that need space appending
186 // if next item is a identifier..
187 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
188 tok.outData = tok.data + " ";
193 case "RETURN": // if next item is not a semi; (or }
194 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
197 tok.outData = tok.data + " ";
202 case "ELSE": // if next item is not a semi; (or }
203 if (!ts.lookTok(1).name == "IF") {
207 tok.outData = tok.data + " ";
210 case "++": // if previous was a plus or next is a + add a space..
211 case "--": // if previous was a - or next is a - add a space..
213 var p = (tok.data == "--" ? '-' : '+');
215 if (ts.lookTok(1).data == p) {
216 tok.outData = tok.data + " ";
218 if (ts.lookTok(-1).data == p) {
219 tok.outData = " " + tok.data;
224 case "IN": // before and after??
227 tok.outData = " " + tok.data + " ";
230 case "VAR": // always after..
237 tok.outData = tok.data + " ";
241 case "TYPEOF": // what about typeof(
242 if (ts.lookTok(1).data != '(') {
243 tok.outData = tok.data + " ";
247 //remove semicolon before brace --
248 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
260 // NOW OUTPUT THE THING.
261 //var f = new File(minfile, File.NEW);
265 out.length = ts.slen; // prealloc.
268 var tok = ts.nextTok();
275 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
276 //f.write(tok.identifier.mungedValue);
277 out += tok.identifier.mungedValue;
281 // at this point we can apply a text translation kit...
283 if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
284 if (packer && packer.stringHandler) {
285 out += packer.stringHandler(tok);
290 out += tok.outData !== false ? tok.outData : tok.data;
292 if ((tok.outData == ';') && (out.length - outoff > 255)) {
299 // remove the last ';' !!!
300 if (out.substring(out.length-1) == ';') {
301 return out.substring(0,out.length-1);