PageRenderTime 141ms CodeModel.GetById 41ms app.highlight 53ms RepoModel.GetById 27ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/lang.js

http://pyjamas.googlecode.com/
JavaScript | 377 lines | 377 code | 0 blank | 0 comment | 51 complexity | 29d7231b0a4d2340abfdc900eff45600 MD5 | raw file
  1Module("lang", "0.3.7", function(mod){
  2var ISODate = function(d){
  3if(/^(\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})/.test(d)){
  4return new Date(Date.UTC(RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
  5}else{ 
  6throw new mod.Exception("Not an ISO date: " + d);
  7}
  8}
  9mod.JSONParser=Class("JSONParser", function(publ, supr){
 10publ.init=function(){
 11this.libs = {};
 12var sys = {"ISODate" : ISODate};
 13this.addLib(sys, "sys", ["ISODate"]);
 14}
 15publ.addLib = function(obj, name, exports){
 16if(exports == null){
 17this.libs[name] = obj;
 18}else{
 19for(var i=0;i<exports.length;i++){
 20this.libs[name + "." + exports[i]] = obj[exports[i]];
 21}
 22}
 23}
 24var EmptyValue = {};
 25var SeqSep = {};
 26var parseValue = function(tkns, libs){
 27var tkn = tkns.nextNonWS();
 28switch(tkn.type){
 29case mod.tokens.STR:
 30case mod.tokens.NUM:
 31return eval(tkn.value);
 32case mod.tokens.NAME:
 33return parseName(tkn.value);
 34case mod.tokens.OP:
 35switch(tkn.value){
 36case "[":
 37return parseArray(tkns, libs);
 38break;
 39case "{":
 40return parseObj(tkns, libs);
 41break;
 42case "}": case "]":
 43return EmptyValue;
 44case ",":
 45return SeqSep;
 46default:
 47throw new mod.Exception("expected '[' or '{' but found: '" + tkn.value + "'");
 48}
 49}
 50return EmptyValue;
 51}
 52var parseArray = function(tkns, libs){
 53var a = [];
 54while(! tkns.finished()){
 55var v = parseValue(tkns, libs);
 56if(v == EmptyValue){
 57return a;
 58}else{
 59a.push(v);
 60v = parseValue(tkns, libs);
 61if(v == EmptyValue){
 62return a;
 63}else if(v != SeqSep){
 64throw new mod.Exception("',' expected but found: '" + v + "'");
 65}
 66}
 67}
 68throw new mod.Exception("']' expected");
 69}
 70var parseObj = function(tkns, libs){
 71var obj = {};
 72var nme ="";
 73while(! tkns.finished()){
 74var tkn = tkns.nextNonWS();
 75if(tkn.type == mod.tokens.STR){
 76var nme =  eval(tkn.value);
 77tkn = tkns.nextNonWS();
 78if(tkn.value == ":"){
 79var v = parseValue(tkns, libs);
 80if(v == SeqSep || v == EmptyValue){
 81throw new mod.Exception("value expected");
 82}else{
 83obj[nme] = v;
 84v = parseValue(tkns, libs);
 85if(v == EmptyValue){
 86return transformObj(obj, libs);
 87}else if(v != SeqSep){
 88throw new mod.Exception("',' expected");
 89}
 90}
 91}else{
 92throw new mod.Exception("':' expected but found: '" + tkn.value + "'");
 93}
 94}else if(tkn.value == "}"){
 95return transformObj(obj, libs);
 96}else{
 97throw new mod.Exception("String expected");
 98}
 99}
100throw new mod.Exception("'}' expected.")
101}
102var transformObj = function(obj, libs){
103var o2;
104if(obj.jsonclass != null){
105var clsName = obj.jsonclass[0];
106var params = obj.jsonclass[1]
107if(libs[clsName]){
108o2 = libs[clsName].apply(this, params);
109for(var nme in obj){
110if(nme != "jsonclass"){
111if(typeof obj[nme] != "function"){
112o2[nme] = obj[nme];
113}
114}
115}
116}else{
117throw new mod.Exception("jsonclass not found: " + clsName);
118}
119}else{
120o2 = obj;
121}
122return o2;
123}
124var parseName = function(name){
125switch(name){
126case "null":
127return null;
128case "true":
129return true;
130case "false":
131return false;
132default:
133throw new mod.Exception("'null', 'true', 'false' expected but found: '" + name + "'");
134}
135}
136publ.jsonToObj = function(data){
137var t = new mod.Tokenizer(data);
138return parseValue(t, this.libs);
139}
140publ.objToJson=function(obj){
141if(obj == null){
142return "null";
143}else{
144return obj.toJSON();
145}
146}
147})
148mod.parser = new mod.JSONParser();
149mod.jsonToObj=function(src){
150return mod.parser.jsonToObj(src);
151}
152mod.objToJson=function(obj){
153return mod.parser.objToJson(obj);
154}
155mod.tokens = {};
156mod.tokens.WSP = 0;
157mod.tokens.OP =1;
158mod.tokens.STR = 2;
159mod.tokens.NAME = 3;
160mod.tokens.NUM = 4;
161mod.tokens.ERR = 5;
162mod.tokens.NL = 6;
163mod.tokens.COMMENT = 7;
164mod.tokens.DOCCOMMENT = 8;
165mod.tokens.REGEXP = 9;
166mod.Token=Class(function(publ, supr){
167publ.init=function(type, value, pos, err){
168this.type = type;
169this.value = value;
170this.pos = pos;
171this.err= err;
172}
173})
174mod.Tokenizer=Class("Tokenizer", function(publ, supr){
175publ.init=function(s){
176this._working = s;
177this._pos = 0;
178}
179publ.finished=function(){
180return this._working.length == 0;
181}
182publ.nextNonWS = function(nlIsWS){
183var tkn = this.next();
184while((tkn.type == mod.tokens.WSP) ||  (nlIsWS && (tkn.type == mod.tokens.NL))){
185tkn = this.next();
186}
187return tkn;
188}
189publ.next = function(){
190if(this._working ==""){
191throw new mod.Exception("Empty");
192} 
193var s1 = this._working.charAt(0);
194var s2 = s1 + this._working.charAt(1);
195var s3 = s2 + this._working.charAt(2);
196var rslt=[];
197switch(s1){
198case '"': case "'":
199try{
200s1 = extractQString(this._working);
201rslt= new mod.Token(mod.tokens.STR, s1, this._pos);
202}catch(e){
203rslt= new mod.Token(mod.tokens.ERR, s1, this._pos, e);
204}
205break;
206case "\n": case "\r":
207rslt =new mod.Token(mod.tokens.NL, s1, this._pos);
208break;   
209case "-": 
210s1=this._working.match(/-\d+\.\d+|-\d+/)[0];
211if(/^-\d|-\d\.\d/.test(s1)){
212rslt =  new mod.Token(mod.tokens.NUM, s1, this._pos);
213break;
214}
215case "{": case "}": case "[": case "]": case "(": case ")":
216case ":": case ",": case ".": case ";":
217case "*": case "-": case "+":
218case "=": case "<": case ">":  case "!":   
219case "|": case "&":
220switch(s2){
221case "==": case "!=": case "<>":  case "<=": case ">=":case "||": case "&&":
222rslt = new mod.Token(mod.tokens.OP, s2, this._pos);
223break;
224default:
225rslt = new mod.Token(mod.tokens.OP, s1, this._pos);
226}
227break;
228case "/":
229if(s2 == "//" || s3 =="///"){
230s1 = extractSLComment(this._working);
231rslt = new mod.Token(s1.charAt(2) != "/" ? mod.tokens.COMMENT:mod.tokens.DOCCOMMENT, s1, this._pos);
232}else if(s2 == "/*" || s3 =="/**"){
233try{
234s1 = extractMLComment(this._working);
235rslt = new mod.Token(s3 !="/**" ? mod.tokens.COMMENT: mod.tokens.DOCCOMMENT, s1, this._pos);
236}catch(e){
237rslt= new mod.Token(mod.tokens.ERR, s3 != "/**" ? s2 : s3, this._pos, e);
238}
239}else{
240try{
241s1 = extractRegExp(this._working);
242rslt  = new mod.Token(mod.tokens.REGEXP, s1, this._pos);
243}catch(e){
244rslt = new mod.Token(mod.tokens.OP, s1, this._pos, e);
245}
246}
247break;
248case " ":
249var i = 0;
250var s="";
251while(this._working.charAt(i) == " "){
252s+=" ";
253i++;
254}
255rslt = new mod.Token(mod.tokens.WSP, s, this._pos);
256break;
257default:
258s1=this._working.match(/\d+\.\d+|\d+|\w+/)[0];
259if(/^\d|\d\.\d/.test(s1)){
260rslt =  new mod.Token(mod.tokens.NUM, s1, this._pos);
261}else{
262rslt =new mod.Token(mod.tokens.NAME, s1, this._pos);
263}
264}
265this._working=this._working.slice(rslt.value.length);
266this._pos += rslt.value.length;
267return rslt;
268}
269var searchQoute = function(s, q){
270if(q=="'"){
271return s.search(/[\\']/);
272}else{
273return s.search(/[\\"]/);
274}
275}
276var extractQString=function(s){
277if(s.charAt(0) == "'"){
278var q="'";
279}else{
280var q='"';
281}
282s=s.slice(1);
283var rs="";
284var p= searchQoute(s, q);
285while(p >= 0){
286if(p >=0){
287if(s.charAt(p) == q){
288rs += s.slice(0, p+1);
289s = s.slice(p+1);
290return q + rs;
291}else{
292rs+=s.slice(0, p+2);
293s = s.slice(p+2);
294}
295}
296p = searchQoute(s, q);
297}
298throw new mod.Exception("End of String expected.");
299}
300var extractSLComment=function(s){
301var p = s.search(/\n/);
302if(p>=0){
303return s.slice(0,p+1);
304}else{
305return s;
306}
307}
308var extractMLComment=function(s){
309var p = s.search(/\*\//);
310            if(p>=0){
311return s.slice(0,p+2);
312}else{
313throw new mod.Exception("End of comment expected.");
314}
315}
316var extractRegExp=function(s){
317var p=0;
318for(var i=0;i<s.length;i++){
319if(s.charAt(i) == "/"){
320p=i;
321}
322if(s.charAt(i) == "\n"){
323i = s.length;
324}
325}
326return s.slice(0,p+1);
327}
328})
329Object.prototype.toJSON = function(){
330var v=[];
331for(attr in this){
332if(typeof this[attr] != "function"){
333v.push('"' + attr + '": ' + mod.objToJson(this[attr]));
334}
335}
336return "{" + v.join(", ") + "}";
337}
338String.prototype.toJSON = function(){
339var s = '"' + this.replace(/(["\\])/g, '\\$1') + '"';
340s = s.replace(/(\n)/g,"\\n");
341return s;
342}
343Number.prototype.toJSON = function(){
344return this.toString();
345}
346Boolean.prototype.toJSON = function(){
347return this.toString();
348}
349Date.prototype.toJSON= function(){
350var padd=function(s, p){
351s=p+s
352return s.substring(s.length - p.length)
353}
354var y = padd(this.getUTCFullYear(), "0000");
355var m = padd(this.getUTCMonth() + 1, "00");
356var d = padd(this.getUTCDate(), "00");
357var h = padd(this.getUTCHours(), "00");
358var min = padd(this.getUTCMinutes(), "00");
359var s = padd(this.getUTCSeconds(), "00");
360var isodate = y +  m  + d + "T" + h +  ":" + min + ":" + s;
361return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}';
362}
363Array.prototype.toJSON = function(){
364var v = [];
365for(var i=0;i<this.length;i++){
366v.push(mod.objToJson(this[i])) ;
367}
368return "[" + v.join(", ") + "]";
369}
370mod.test=function(){
371try{
372print(mod.jsonToObj("['sds', -12377,-1212.1212, 12, '-2312']").toJSON());
373}catch(e){
374print(e.toTraceString());
375}
376}
377})