📄 lang.js
字号:
/*
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.7", 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 new mod.Exception("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<exports.length;i++){
this.libs[name + "." + exports[i]] = obj[exports[i]];
}
}
}
var EmptyValue = {};
var SeqSep = {};
var parseValue = function(tkns, libs){
var tkn = tkns.nextNonWS();
switch(tkn.type){
case mod.tokens.STR:
case mod.tokens.NUM:
return eval(tkn.value);
case mod.tokens.NAME:
return parseName(tkn.value);
case mod.tokens.OP:
switch(tkn.value){
case "[":
return parseArray(tkns, libs);
break;
case "{":
return parseObj(tkns, libs);
break;
case "}": case "]":
return EmptyValue;
case ",":
return SeqSep;
default:
throw new mod.Exception("expected '[' or '{' but found: '" + tkn.value + "'");
}
}
return EmptyValue;
}
var parseArray = function(tkns, libs){
var a = [];
while(! tkns.finished()){
var v = parseValue(tkns, libs);
if(v == EmptyValue){
return a;
}else{
a.push(v);
v = parseValue(tkns, libs);
if(v == EmptyValue){
return a;
}else if(v != SeqSep){
throw new mod.Exception("',' expected but found: '" + v + "'");
}
}
}
throw new mod.Exception("']' expected");
}
var parseObj = function(tkns, libs){
var obj = {};
var nme =""
while(! tkns.finished()){
var tkn = tkns.nextNonWS();
if(tkn.type == mod.tokens.STR){
var nme = eval(tkn.value);
tkn = tkns.nextNonWS();
if(tkn.value == ":"){
var v = parseValue(tkns, libs);
if(v == SeqSep || v == EmptyValue){
throw new mod.Exception("value expected");
}else{
obj[nme] = v;
v = parseValue(tkns, libs);
if(v == EmptyValue){
return transformObj(obj, libs);
}else if(v != SeqSep){
throw new mod.Exception("',' expected");
}
}
}else{
throw new mod.Exception("':' expected but found: '" + tkn.value + "'");
}
}else if(tkn.value == "}"){
return transformObj(obj, libs);
}else{
throw new mod.Exception("String expected");
}
}
throw new mod.Exception("'}' expected.")
}
var transformObj = function(obj, libs){
var o2;
if(obj.jsonclass != null){
var clsName = obj.jsonclass[0];
var params = obj.jsonclass[1]
if(libs[clsName]){
o2 = libs[clsName].apply(this, params);
for(var nme in obj){
if(nme != "jsonclass"){
if(typeof obj[nme] != "function"){
o2[nme] = obj[nme];
}
}
}
}else{
throw new mod.Exception("jsonclass not found: " + clsName);
}
}else{
o2 = obj;
}
return o2;
}
var parseName = function(name){
switch(name){
case "null":
return null;
case "true":
return true;
case "false":
return false;
default:
throw new mod.Exception("'null', 'true', 'false' expected but found: '" + name + "'");
}
}
publ.jsonToObj = function(data){
var t = new mod.Tokenizer(data);
return parseValue(t, this.libs);
}
publ.objToJson=function(obj){
if(obj == null){
return "null";
}else{
return obj.toJSON();
}
}
})
mod.parser = new mod.JSONParser();
/**
Turns JSON code into JavaScript objects.
@param src The source as a String.
*/
mod.jsonToObj=function(src){
return mod.parser.jsonToObj(src);
}
/**
Turns an object into JSON.
This is the same as calling obj.toJSON();
@param obj The object to marshall.
*/
mod.objToJson=function(obj){
return mod.parser.objToJson(obj);
}
///Token constants for the tokenizer.
mod.tokens = {};
mod.tokens.WSP = 0;
mod.tokens.OP =1;
mod.tokens.STR = 2;
mod.tokens.NAME = 3;
mod.tokens.NUM = 4;
mod.tokens.ERR = 5;
mod.tokens.NL = 6;
mod.tokens.COMMENT = 7;
mod.tokens.DOCCOMMENT = 8;
mod.tokens.REGEXP = 9;
//todo:doc
mod.Token=Class(function(publ, supr){
publ.init=function(type, value, pos, err){
this.type = type;
this.value = value;
this.pos = pos;
this.err= err;
}
})
/**
Tokenizer Class which incrementally parses JavaScript code and returns the language tokens.
*/
mod.Tokenizer=Class("Tokenizer", function(publ, supr){
publ.init=function(s){
this._working = s;
this._pos = 0;
}
/**
Returns weather or not the code was parsed.
@return True if the complete code was parsed, false otherwise.
*/
publ.finished=function(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -