3 Modification information for LGPL compliance
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
25 Copyright (c) 2004 Jan-Klaas Kollhof
27 This file is part of the JavaScript o lait library(jsolait).
29 jsolait is free software; you can redistribute it and/or modify
30 it under the terms of the GNU Lesser General Public License as published by
31 the Free Software Foundation; either version 2.1 of the License, or
32 (at your option) any later version.
34 This software is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU Lesser General Public License for more details.
39 You should have received a copy of the GNU Lesser General Public License
40 along with this software; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 Module providing language services like tokenizing JavaScript code
46 or converting JavaScript objects to and from JSON (see json.org).
47 To customize JSON serialization of Objects just overwrite the toJSON method in your class.
49 Module("lang", "0.3.7", function(mod){
51 var ISODate = function(d){
52 if(/^(\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})/.test(d)){
53 return new Date(Date.UTC(RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
54 }else{ //todo error message
55 throw new mod.Exception("Not an ISO date: " + d);
59 mod.JSONParser=Class("JSONParser", function(publ, supr){
62 var sys = {"ISODate" : ISODate};
63 this.addLib(sys, "sys", ["ISODate"]);
66 publ.addLib = function(obj, name, exports){
68 this.libs[name] = obj;
70 for(var i=0;i<exports.length;i++){
71 this.libs[name + "." + exports[i]] = obj[exports[i]];
79 var parseValue = function(tkns, libs){
80 var tkn = tkns.nextNonWS();
84 return eval(tkn.value);
86 return parseName(tkn.value);
90 return parseArray(tkns, libs);
93 return parseObj(tkns, libs);
100 throw new mod.Exception("expected '[' or '{' but found: '" + tkn.value + "'");
106 var parseArray = function(tkns, libs){
108 while(! tkns.finished()){
109 var v = parseValue(tkns, libs);
114 v = parseValue(tkns, libs);
117 }else if(v != SeqSep){
118 throw new mod.Exception("',' expected but found: '" + v + "'");
122 throw new mod.Exception("']' expected");
125 var parseObj = function(tkns, libs){
128 while(! tkns.finished()){
129 var tkn = tkns.nextNonWS();
130 if(tkn.type == mod.tokens.STR){
131 var nme = eval(tkn.value);
132 tkn = tkns.nextNonWS();
133 if(tkn.value == ":"){
134 var v = parseValue(tkns, libs);
135 if(v == SeqSep || v == EmptyValue){
136 throw new mod.Exception("value expected");
139 v = parseValue(tkns, libs);
141 return transformObj(obj, libs);
142 }else if(v != SeqSep){
143 throw new mod.Exception("',' expected");
147 throw new mod.Exception("':' expected but found: '" + tkn.value + "'");
149 }else if(tkn.value == "}"){
150 return transformObj(obj, libs);
152 throw new mod.Exception("String expected");
155 throw new mod.Exception("'}' expected.")
158 var transformObj = function(obj, libs){
160 if(obj.jsonclass != null){
161 var clsName = obj.jsonclass[0];
162 var params = obj.jsonclass[1]
164 o2 = libs[clsName].apply(this, params);
166 if(nme != "jsonclass"){
167 if(typeof obj[nme] != "function"){
173 throw new mod.Exception("jsonclass not found: " + clsName);
181 var parseName = function(name){
190 throw new mod.Exception("'null', 'true', 'false' expected but found: '" + name + "'");
194 publ.jsonToObj = function(data){
195 var t = new mod.Tokenizer(data);
196 return parseValue(t, this.libs);
199 publ.objToJson=function(obj){
208 mod.parser = new mod.JSONParser();
211 Turns JSON code into JavaScript objects.
212 @param src The source as a String.
214 mod.jsonToObj=function(src){
215 return mod.parser.jsonToObj(src);
219 Turns an object into JSON.
220 This is the same as calling obj.toJSON();
221 @param obj The object to marshall.
223 mod.objToJson=function(obj){
224 return mod.parser.objToJson(obj);
227 ///Token constants for the tokenizer.
236 mod.tokens.COMMENT = 7;
237 mod.tokens.DOCCOMMENT = 8;
238 mod.tokens.REGEXP = 9;
242 mod.Token=Class(function(publ, supr){
244 publ.init=function(type, value, pos, err){
254 Tokenizer Class which incrementally parses JavaScript code and returns the language tokens.
256 mod.Tokenizer=Class("Tokenizer", function(publ, supr){
257 publ.init=function(s){
263 Returns weather or not the code was parsed.
264 @return True if the complete code was parsed, false otherwise.
266 publ.finished=function(){
267 return this._working.length == 0;
270 publ.nextNonWS = function(nlIsWS){
271 var tkn = this.next();
272 while((tkn.type == mod.tokens.WSP) || (nlIsWS && (tkn.type == mod.tokens.NL))){
279 Returns the next token.
280 @return The next token.
282 publ.next = function(){
283 if(this._working ==""){
284 throw new mod.Exception("Empty");
286 var s1 = this._working.charAt(0);
287 var s2 = s1 + this._working.charAt(1);
288 var s3 = s2 + this._working.charAt(2);
293 s1 = extractQString(this._working);
294 rslt= new mod.Token(mod.tokens.STR, s1, this._pos);
296 rslt= new mod.Token(mod.tokens.ERR, s1, this._pos, e);
299 case "\n": case "\r":
300 rslt =new mod.Token(mod.tokens.NL, s1, this._pos);
302 case "-": //check for negative numbers
303 s1=this._working.match(/-\d+\.\d+|-\d+/)[0];
304 if(/^-\d|-\d\.\d/.test(s1)){//number
305 rslt = new mod.Token(mod.tokens.NUM, s1, this._pos);
309 case "{": case "}": case "[": case "]": case "(": case ")":
310 case ":": case ",": case ".": case ";":
311 case "*": case "-": case "+":
312 case "=": case "<": case ">": case "!":
315 case "==": case "!=": case "<>": case "<=": case ">=":case "||": case "&&":
316 rslt = new mod.Token(mod.tokens.OP, s2, this._pos);
319 rslt = new mod.Token(mod.tokens.OP, s1, this._pos);
323 if(s2 == "//" || s3 =="///"){
324 s1 = extractSLComment(this._working);
325 rslt = new mod.Token(s1.charAt(2) != "/" ? mod.tokens.COMMENT:mod.tokens.DOCCOMMENT, s1, this._pos);
326 }else if(s2 == "/*" || s3 =="/**"){
328 s1 = extractMLComment(this._working);
329 rslt = new mod.Token(s3 !="/**" ? mod.tokens.COMMENT: mod.tokens.DOCCOMMENT, s1, this._pos);
331 rslt= new mod.Token(mod.tokens.ERR, s3 != "/**" ? s2 : s3, this._pos, e);
335 s1 = extractRegExp(this._working);
336 rslt = new mod.Token(mod.tokens.REGEXP, s1, this._pos);
338 rslt = new mod.Token(mod.tokens.OP, s1, this._pos, e);
345 while(this._working.charAt(i) == " "){
349 rslt = new mod.Token(mod.tokens.WSP, s, this._pos);
352 s1=this._working.match(/\d+\.\d+|\d+|\w+/)[0];
353 if(/^\d|\d\.\d/.test(s1)){//number
354 rslt = new mod.Token(mod.tokens.NUM, s1, this._pos);
356 rslt =new mod.Token(mod.tokens.NAME, s1, this._pos);
360 this._working=this._working.slice(rslt.value.length);
361 this._pos += rslt.value.length;
365 var searchQoute = function(s, q){
367 return s.search(/[\\']/);
369 return s.search(/[\\"]/);
373 var extractQString=function(s){
374 if(s.charAt(0) == "'"){
381 var p= searchQoute(s, q);
384 if(s.charAt(p) == q){
385 rs += s.slice(0, p+1);
393 p = searchQoute(s, q);
395 throw new mod.Exception("End of String expected.");
398 var extractSLComment=function(s){
399 var p = s.search(/\n/);
401 return s.slice(0,p+1);
407 var extractMLComment=function(s){
408 var p = s.search(/\*\//);
410 return s.slice(0,p+2);
412 throw new mod.Exception("End of comment expected.");
416 var extractRegExp=function(s){
418 for(var i=0;i<s.length;i++){
419 if(s.charAt(i) == "/"){
422 if(s.charAt(i) == "\n"){
426 return s.slice(0,p+1);
431 Converts an object to JSON.
433 Object.prototype.toJSON = function(){
436 if(typeof this[attr] != "function"){
437 v.push('"' + attr + '": ' + mod.objToJson(this[attr]));
440 return "{" + v.join(", ") + "}";
444 Converts a String to JSON.
446 String.prototype.toJSON = function(){
447 var s = '"' + this.replace(/(["\\])/g, '\\$1') + '"';
448 s = s.replace(/(\n)/g,"\\n");
453 Converts a Number to JSON.
455 Number.prototype.toJSON = function(){
456 return this.toString();
460 Converts a Boolean to JSON.
462 Boolean.prototype.toJSON = function(){
463 return this.toString();
467 Converts a Date to JSON.
468 Date representation is not defined in JSON.
470 Date.prototype.toJSON= function(){
471 var padd=function(s, p){
473 return s.substring(s.length - p.length)
475 var y = padd(this.getUTCFullYear(), "0000");
476 var m = padd(this.getUTCMonth() + 1, "00");
477 var d = padd(this.getUTCDate(), "00");
478 var h = padd(this.getUTCHours(), "00");
479 var min = padd(this.getUTCMinutes(), "00");
480 var s = padd(this.getUTCSeconds(), "00");
482 var isodate = y + m + d + "T" + h + ":" + min + ":" + s
484 return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}';
488 Converts an Array to JSON.
490 Array.prototype.toJSON = function(){
492 for(var i=0;i<this.length;i++){
493 v.push(mod.objToJson(this[i])) ;
495 return "[" + v.join(", ") + "]";
500 print(mod.jsonToObj("['sds', -12377,-1212.1212, 12, '-2312']").toJSON());
502 print(e.toTraceString());