/* Copyright (c) 2004 Jan-Klaas Kollhof This file is part of the JavaScript o lait library(jsolait). jsolait is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this software; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** Module providing language services like tokenizing JavaScript code or converting JavaScript objects to and from JSON (see json.org). To customize JSON serialization of Objects just overwrite the toJSON method in your class. */ Module("lang", "0.3.5", function(mod){ var ISODate = function(d){ if(/^(\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})/.test(d)){ return new Date(Date.UTC(RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6)); }else{ //todo error message throw "Not an ISO date: " + d; } } mod.JSONParser=Class("JSONParser", function(publ, supr){ publ.init=function(){ this.libs = {}; var sys = {"ISODate" : ISODate}; this.addLib(sys, "sys", ["ISODate"]); } publ.addLib = function(obj, name, exports){ if(exports == null){ this.libs[name] = obj; }else{ for(var i=0;i": case "!": switch(s2){ case "==": case "!=": case "<>": case "<=": case ">=": rslt = new mod.Token(mod.tokens.OP, s2, this._pos); break; default: rslt = new mod.Token(mod.tokens.OP, s1, this._pos); } break; case "/": if(s2 == "//" || s3 =="///"){ s1 = extractSLComment(this._working); rslt = new mod.Token(s1.charAt(2) != "/" ? mod.tokens.COMMENT:mod.tokens.DOCCOMMENT, s1, this._pos); }else if(s2 == "/*" || s3 =="/**"){ try{ s1 = extractMLComment(this._working); rslt = new mod.Token(s3 !="/**" ? mod.tokens.COMMENT: mod.tokens.DOCCOMMENT, s1, this._pos); }catch(e){ rslt= new mod.Token(mod.tokens.ERR, s3 != "/**" ? s2 : s3, this._pos, e); } }else{ try{ s1 = extractRegExp(this._working); rslt = new mod.Token(mod.tokens.REGEXP, s1, this._pos); }catch(e){ rslt = new mod.Token(mod.tokens.OP, s1, this._pos, e); } } break; default: s1=this._working.match(/\d+\.\d+|\d+|\w+|\s+/)[0]; if(s1.replace(/\s+/g,"") == ""){ //whitespace rslt = new mod.Token(mod.tokens.WSP, s1, this._pos); }else if(/^\d|\d\.\d/.test(s1)){//number rslt = new mod.Token(mod.tokens.NUM, s1, this._pos); }else{//name rslt =new mod.Token(mod.tokens.NAME, s1, this._pos); } } this._working=this._working.slice(rslt.value.length); this._pos += rslt.value.length; return rslt; } var searchQoute = function(s, q){ if(q=="'"){ return s.search(/[\\']/); }else{ return s.search(/[\\"]/); } } var extractQString=function(s){ if(s.charAt(0) == "'"){ var q="'"; }else{ var q='"'; } s=s.slice(1); var rs=""; var p= searchQoute(s, q); while(p >= 0){ if(p >=0){ if(s.charAt(p) == q){ rs += s.slice(0, p+1); s = s.slice(p+1); return q + rs; }else{ rs+=s.slice(0, p+2); s = s.slice(p+2); } } p = searchQoute(s, q); } throw "End of String expected."; } var extractSLComment=function(s){ var p = s.search(/\n/); if(p>=0){ return s.slice(0,p+1); }else{ return s; } } var extractMLComment=function(s){ var p = s.search(/\*\//); if(p>=0){ return s.slice(0,p+2); }else{ throw "End of comment expected."; } } var extractRegExp=function(s){ var p=0; for(var i=0;i