back to devpro   download
/** JSMap - JavaScript Source Map For JavaScript
 * @author          Andrea Giammarchi
 * @blog            webreflection.blogspot.com
 * @license         Mit Style License
 * @version         0.1b
 * @compatibility   I guess all
 */
function JSMap(CODE, type){
    if(!type)
        type = JSMap.DEFAULT
    ;
    for(var
        Map = [],
        length = JSMap.parser.length, i = 0,
        a, b, exec, parser, value;
        i < length; ++i
    ){
        parser = JSMap.parser[i];
        if(parser.type & type){
            while(exec = parser.test.exec(CODE)){
                value = exec[0];
                Map.push({start:exec.index, end:exec.index + value.length, value:value, type:parser.type});
            };
        };
    };
    type = JSMap.CODE & type;
    if(length = Map.length){
        Map.sort(JSMap.sort);
        if(type && 0 < (i = Map[0].start))
            Map.unshift({start:0, end:i, value:CODE.substring(0, i), type:JSMap.CODE})
        ;
        for(length = Map.length, i = 1, a = Map[0]; i < length; ++i){
            b = Map[i];
            switch(true){
                case a.end < b.start:
                    if(type){
                        Map.splice(i, 0, {start:a.end, end:b.start, value:CODE.substring(a.end, b.start), type:JSMap.CODE});
                        ++length;
                        ++i;
                    };
                case a.end === b.start:
                    a = b;
                    break;
                default:
                    Map.splice(i, 1);
                    --length;
                    --i;
                    break;
            };
        };
        if(type && (i = Map[i - 1].end) < CODE.length)
            Map.push({start:i, end:CODE.length, value:CODE.substring(i, CODE.length), type:JSMap.CODE})
        ;
    } else if(type)
        Map.push({start:0, end:CODE.length, value:CODE, type:JSMap.CODE})
    ;
    return Map;
};

JSMap.CODE      = 1;
JSMap.COMMENT_SL= 2;
JSMap.COMMENT_ML= 4;
JSMap.STRING    = 8;
JSMap.REGEXP    = 16;
JSMap.E4X       = 32; // not fully supported
JSMap.ALL       = 63;
JSMap.DEFAULT   = ~JSMap.E4X;

JSMap.parser = [
    {
        test:/\/\/[^\1]*?(\r\n|\r|\n)/g,
        type:JSMap.COMMENT_SL,
        place:function(value, a){
            return value.charAt(2) === "@" ? value : a;
        }
    },{
        test:/\/\*[^\1]*?(\*\/)/g,
        type:JSMap.COMMENT_ML,
        place:function(value){
            return value.charAt(value.length - 3) === "@" ? value : JSMap.parser[0].place(value, "");
        }
    },{
        test:/(["'])(?:(?=(\\?))\2.)*?\1/g,
        type:JSMap.STRING
    },{
        test:/\/(?:\[(?:(?=(\\?))\1.)*?\]|(?=(\\?))\2.)+?\/[igm]*/g,
        type:JSMap.REGEXP
    },{
        test:/<>[^\1]*?(<\/>)|<(\w+|\{\w+\})(?:\s*\/|[^\>]*?>.*?<\/\2\s*)>/g,
        type:JSMap.E4X
    }
];

JSMap.sort=function(a,b){return b.start < a.start ? 1 : -1};
back to devpro   download
Creative Commons License