All files / src symbols.js

94.63% Statements 141/149
87.88% Branches 58/66
87.5% Functions 7/8
96.9% Lines 125/129

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251    1x   1x 61x 61x     1x 498x 498x 272x 272x 401x   272x     226x 437x   226x       1x 45x 1444x       1x 1x 1x 1x 1x 1x 1x 1x 1x 6x   1x   1x 28x 4x 58x 58x 58x 48x 48x     10x 10x   58x       24x         1x                       1x 361x 361x 361x 376x 376x   361x 361x 376x                             376x 376x 376x 376x 1422x 455x 455x 1422x 1422x 1422x   376x 376x 376x   361x     1x 82x 82x 82x 182x 132x   50x   82x     1x         133x 133x 266x   133x 28x 28x       133x       133x 279x 279x   279x 279x 931x 605x     326x 326x 326x 326x 326x 326x 326x     279x     133x 146x   146x 4x 4x 4x 7x   142x 58x 58x 58x 84x 58x     84x 84x 84x 84x 84x 84x 30x 30x 30x 30x   84x   146x 146x 127x 383x       133x                                                                         1x    
import DEFAULT_SYMBOLS from '../sym/symbols.json'
import Version from './version.js';
var Symbols = {"symbols":{}, "templates":{}};
 
Symbols.make_template_symbol = function(template_name, name, args){
    var template = JSON.parse(JSON.stringify(Symbols.templates[template_name]));
    return Symbols.eval_template(template, name, args);
}
 
Symbols.eval_template = function(template, name, args){
    args['name'] = name;
    if(Object.prototype.toString.call(template) == "[object String]") {
        var ans = template;
        for(var nam in args) {
            ans = ans.replace(new RegExp("\\{\\$"+nam+"\\}"),args[nam]);
        }
        return ans;
    }
    else {
        for(var x in template) {
            template[x] = Symbols.eval_template(template[x], name, args)
        }
        return template;
    }
}
 
Symbols.lookup_type = function(type){
    for(var s in Symbols.symbols){
        if(Symbols.symbols[s].attrs.type == type) return s;
    }
}
 
Symbols.add_symbols = function(syms){
    var version = syms["_version"];
    var collection_name = syms["_name"];
    delete syms["_version"];
    delete syms["_name"];
    Iif(!version || version != Version.SYMBOL_VERSION) Version.SYMBOL_ERROR(collection_name, version);
    var templates = syms["_templates"];
    Eif(templates){
        for(var t in templates){
            Symbols.templates[t] = templates[t];
        }
        delete syms["_templates"];
    }
    for(var s in syms){
        if(syms[s].template){
            for(var v in syms[s].values){
                var name = null;
                var args = null;
                if(Object.prototype.toString.call(syms[s].values) == "[object Array]"){
                    name = syms[s].values[v];
                    args = {}
                }
                else{
                    name = v;
                    args = syms[s].values[v];
                }
                Symbols.symbols[name] = Symbols.make_template_symbol(syms[s].template, name, args);
            }
        }
        else{
            Symbols.symbols[s] = syms[s];
        }
    }
}
 
Symbols.validate = function(){
    for(var sym in Symbols.symbols){
        if(!Symbols.symbols[sym].output.latex) throw "Symbol " + sym + " missing output.latex (needed for display)";
        if(!Symbols.symbols[sym].attrs.name) throw "Symbol " + sym + " missing attrs.name (needed for text output)";
        if(!Symbols.symbols[sym].attrs.group) throw "Symbol " + sym + " missing attrs.group (needed for mobile)";
        //for(var i = 0; i < sym.length; i++)
        //    if(sym.substring(0,i) in Symbols.symbols) throw "WARNING: Symbols are not prefix free: '" + sym.substring(0,i) + "' and '" + sym + "' are both symbols";
    }
}
 
// Returns an array with alternating text and argument elements of the form
// {"type":"text", "val":the_text} or {"type":"arg", "index":the_index, "seperators":[sep1,sep2,...], "template":[...]}
Symbols.split_output = function(output){
    var regex = /\{\$([0-9]+)/g, result, starts = [], indices = [], i;
    var ans = [];
    while ((result = regex.exec(output))){
        starts.push(result.index);
        indices.push(parseInt(result[1]));
    }
    ans.push({"type":"text","val":output.substring(0,starts.length > 0 ? starts[0] : output.length)}); // Push the first text bit
    for(i = 0; i < starts.length; i++){
        var idx = starts[i]+1;
        // Find template (if defined)
        // var tmpl_str = "";
        // var tmpl = [];
        // if(output[idx] == "["){
        //     idx++;
        //     var tmpl_opens = 1;
        //     while(opens > 0 && idx < output.length){
        //         if(output[idx] == "]"){ tmpl_opens--; }
        //         if(output[idx] == "["){ tmpl_opens++; }
        //         if(tmpl_opens > 1){ tmpl_str += output[idx]; }
        //         idx++;
        //     }
        //     tmpl = Symbols.split_output(tmpl_str);
        // }
        var separators = [];
        var sep = "";
        var opens = 1
        while(opens > 0 && idx < output.length){
            if(output[idx] == "}"){
                if(opens == 2){ separators.push(sep); sep = ""; }
                opens--; }
            if(opens >= 2){ sep += output[idx]; }
            if(output[idx] == "{"){ opens++; }
            idx++;
        }
        ans.push({"type":"arg","index":indices[i],"separators":separators});
        var next = (i == starts.length - 1) ? output.length : starts[i+1];
        ans.push({"type":"text","val":output.substring(idx,next)}); // Push the next text bit
    }
    return ans;
}
 
Symbols.add_blanks = function(output, blank){
    var out = Symbols.split_output(output);
    var ans = "";
    for(var i = 0; i < out.length; i++){
        if(out[i]["type"] == "text"){
            ans += out[i]['val'];
        }
        else ans += blank;
    }
    return ans;
}
 
Symbols.symbol_to_node = function(s, content, base){
    
    // s is a symbol
    //
    // content is a list of nodes to insert
    var f = base.createElement("f");
    for(var attr in s.attrs){
        f.setAttribute(attr, s.attrs[attr]);
    }
    if("ast" in s){
        if("type" in s.ast) f.setAttribute("ast_type",s.ast["type"])
        if("value" in s.ast) f.setAttribute("ast_value",s.ast["value"])
    }
    //if(s['char']) f.setAttribute("c","yes");
    
    var first_ref=-1, arglist = [];
    var first, i;
    
    // Make the b nodes for rendering each output    
    for(var t in s["output"]){
        var b = base.createElement("b");
        b.setAttribute("p",t);
 
        var out = Symbols.split_output(s["output"][t]);
        for(i = 0; i < out.length; i++){
            if(out[i]["type"] == "text"){
                if(out[i]["val"].length > 0) b.appendChild(base.createTextNode(out[i]['val']));
            }
            else{
                if(t == 'latex') arglist.push(out[i]);
                var nt = base.createElement("r");
                nt.setAttribute("ref",out[i]["index"]);
                if(out[i]["separators"].length > 0) nt.setAttribute("d",out[i]["separators"].length);
                for(var j = 0; j < out[i]["separators"].length; j++) nt.setAttribute("sep"+j,out[i]["separators"][j]);
                if(t == 'latex' && first_ref == -1) first_ref = out[i]["index"];
                b.appendChild(nt);
            }
        }
        f.appendChild(b);
    }
    // Now make the c/l nodes for storing the content
    for(i = 0; i < arglist.length; i++){
        var a = arglist[i];
        var nc;
        if(i in content && a['separators'].length > 0) {  // If the content for this node is provided and is an array, then dig down to find the first c child
            f.appendChild(content[i][0]);
            nc = content[i][0];
            while(nc.nodeName != "c")
                nc = nc.firstChild;
        }
        else if(i in content) {                                  // If the content for this node is provided and not an array, create the c node and populate its content
            var node_list = content[i];
            nc = base.createElement("c");
            for(var se = 0; se < node_list.length; se++)
                nc.appendChild(node_list[se].cloneNode(true));
            f.appendChild(nc)
        }
        else{                                             // Otherwise create the c node and possibly l nodes
            nc = base.createElement("c");
            var new_e = base.createElement("e");
            new_e.appendChild(base.createTextNode(""));
            nc.appendChild(new_e);
            var par = f;                                  // Now we add nested l elements if this is an array of dimension > 0
            for(j = 0; j < a['separators'].length; j++){
                var nl = base.createElement("l");
                nl.setAttribute("s","1");
                par.appendChild(nl);
                par = nl;
            }
            par.appendChild(nc);
        }
        if(i+1 == first_ref) first = nc.lastChild;        // Note the first node we should visit based on the LaTeX output
        if(s['args'] && s['args'][i]){                    // Set the arguments for the c node based on the symbol
            for(var arg in s['args'][i]){
                nc.setAttribute(arg,s['args'][i][arg]);
            }
        }
    }
    return {"f":f, "first":first, "args":arglist};
}
 
// class Template{
//     constructor(name, definition){
// 	this.name = name;
// 	this.protosym = new Symbol(`__${name}`, definition);
//     }
//     evaluate(args){
// 	let sym = {...this.protosym};
// 	let r = function(src){
// 	    for(var n in args) {
// 		return src.replace(new RegExp("\\{\\$"+n+"\\}"),args[n]);
// 	    }
// 	};
	
// 	Object.keys(sym.outputs).forEach({(x) => sym.outputs[x] = r(sym.outputs[x])});
// 	Object.keys(sym.attrs).forEach({(x) => sym.attrs[x] = r(sym.attrs[x])});
// 	sym.args.forEach({(x) => Object.keys(sym.args[x]).forEach({(y) => sym.args[x][y] = r(sym.args[x][y])});	
//     }
// }
 
// class Symbol{
//     constructor(name, definition){
// 	this.name = name;
// 	this.outputs = config.outputs;
// 	this.args = config.args;
// 	this.attrs = config.attrs;
// 	this.input = config.input;
// 	this.keys = config.keys;
// 	this.ast_value = config.ast.value;
// 	this.ast_type = config.ast.type;
//     }
    
// }
 
 
Symbols.add_symbols(DEFAULT_SYMBOLS);
export default Symbols;