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).data != ';' && ts.lookTok(1).data != '}' && ts.lookTok(1,true).name == "NEWLINE") {
70 ts.look(0).outData = ts.look(0).data+";";
77 if (ts.lookTok(1).type == 'NAME' && ts.look(1,true).name == "NEWLINE") {
79 ts.look(0).outData = ts.look(0).data+"\n";
82 // function a () { ... };
84 if (ts.look(-1).isTypeN(Script.TOKfunction) && ts.look(1).isTypeN(Script.TOKlparen)) {
86 //println("got = function() ");
91 // if next is not ';' -> make it so...
92 // although this var a=function(){},v,c; causes
93 if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
94 ts.cur().outData = ts.cur().data+";";
104 if (ts.lookTok(1).data == '=' && ts.lookTok(2).data == '{') {
106 //println("----------*** 3 *** --------------");
109 if (!ts.balance("{") ){
110 throw "could not find end lbrace!!!";
112 // if next is not ';' -> make it so...
114 if (ts.lookTok(1).data != ';' && ts.lookTok(1).data != '}' && ts.look(1,true).name=="NEWLINE") {
115 ts.look(0).outData = ts.look(0).data +";";
128 //println("got Token: " + tok.type);
132 switch(tok.data.toUpperCase()) {
133 // things that need space appending
137 // if next item is a identifier..
138 if (ts.lookTok(1).type == "NAME" || ts.lookTok(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
139 tok.outData = tok.data + " ";
144 case "RETURN": // if next item is not a semi; (or }
145 if (ts.lookTok(1).data == ';' || ts.lookTok(1).data == '}') {
148 tok.outData = tok.data + " ";
153 case "ELSE": // if next item is not a semi; (or }
154 if (!ts.lookTok(1).name == "IF") {
158 tok.outData = tok.data + " ";
161 case "++": // if previous was a plus or next is a + add a space..
162 case "--": // if previous was a - or next is a - add a space..
164 var p = (tok.data == "--" ? '-' : '+');
166 if (ts.lookTok(1).data == p) {
167 tok.outData = tok.data + " ";
169 if (ts.lookTok(-1).data == p) {
170 tok.outData = " " + tok.data;
175 case "IN": // before and after??
178 tok.outData = " " + tok.data + " ";
181 case "VAR": // always after..
188 tok.outData = tok.data + " ";
192 case "TYPEOF": // what about typeof(
193 if (ts.lookTok(1).data != '(') {
194 tok.outData = tok.data + " ";
198 //remove semicolon before brace --
199 //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
211 // NOW OUTPUT THE THING.
212 //var f = new File(minfile, File.NEW);
216 out.length = ts.slen; // prealloc.
219 var tok = ts.nextTok();
225 if (tok.type == "NAME" && tok.identifier && tok.identifier.mungedValue && tok.identifier.mungedValue.length) {
226 //f.write(tok.identifier.mungedValue);
227 out += tok.identifier.mungedValue;
231 // at this point we can apply a text translation kit...
233 if ((tok.type == 'STRN') && (tok.name== 'DOUBLE_QUOTE')) {
234 if (packer && packer.stringHandler) {
235 out += packer.stringHandler(tok);
239 //f.write(tok.outData);
240 out += tok.outData !== false ? tok.outData : tok.data;
242 if ((tok.outData == ';') && (out.length - outoff > 255)) {
249 // remove the last ';' !!!
250 if (out.substring(out.length-1) == ';') {
251 return out.substring(0,out.length-1);