# All files symbols.js

93.96% Statements 140/149
84.85% Branches 56/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
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371

    1x

    1x
    59x
    59x

    1x
    482x
    482x
    263x
    263x
    387x

    263x

    219x
    423x

    219x

    1x
    45x
    1435x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x

    1x

    1x
    29x
    5x
    57x
    57x
    57x
    47x
    47x

    10x
    10x

    57x

    24x

    1x

    1x
    358x
    358x
    358x
    376x
    376x

    358x
    358x
    376x

    376x
    376x
    376x
    376x
    1422x
    455x
    455x
    1422x
    1422x
    1422x

    376x
    376x
    376x

    358x

    1x
    81x
    81x
    81x
    181x
    131x

    50x

    81x

    1x

    132x
    132x
    264x

    132x
    27x
    27x

    132x

    132x
    277x
    277x

    277x
    277x
    929x
    603x

    326x
    326x
    326x
    326x
    326x
    326x
    326x

    277x

    132x
    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

    132x

    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){
            Eif("type" in s.ast) f.setAttribute("ast_type",s.ast["type"])
            Iif("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 SymbolTemplate{
    //     constructor(name, definition){
    // 	this.name = name;
    // 	this.definition = definition;
    //     }
    //     evaluate(args){
    // 	if(!(args.name)) throw "Template requires 'name' argument";
    // 	let symdef = JSON.parse(JSON.stringify(this.definition));
    // 	let r = function(src){
    // 	    if(Object.prototype.toString.call(src) == "[object String]")
    // 		for(var n in src) src[n].replace(new RegExp("\\{\\$"+n+"\\}"),args[n]);
    // 	    else
    // 		for(var x in src) src[x] = r(src[x]);
    // 	    return src
    // 	};
    // 	return new Symbol(args.name, r(this.definition));
    //     }
    // }

    // class Symbol{
    //     constructor(name, definition){
    // 	this.name = name;
    // 	this.outputs = {};
    // 	for(var o of definition.outputs){
    // 	    this.outputs[o] = Symbol.parse_output(definition.outputs[o]);
    // 	}
    // 	this.args = definition.args;
    // 	this.attrs = definition.attrs;
    // 	this.input = definition.input;
    // 	this.keys = definition.keys;
    // 	this.ast_value = definition.ast.value;
    // 	this.ast_type = definition.ast.type;
    //     }
    //     // 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":[...]}
    //     static parse_output(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;
    //     }

    //     to_node(content, base){
    // 	// content is a list of nodes to insert
    // 	var f = base.createElement("f");
    // 	for(var attr in this.attrs){
    //             f.setAttribute(attr, s.attrs[attr]);
    // 	}
    //         if(this.ast_type) f.setAttribute("ast_type",this.ast_type);
    //         if(this.ast_value) f.setAttribute("ast_value",this.ast_value);

    // 	var first_ref=-1, arglist = [];
    // 	var first, i;

    // 	// Make the b nodes for rendering each output
    // 	for(var t in this.outputs){
    //             var b = base.createElement("b");
    //             b.setAttribute("p",t);

    //             var out = this.outputs[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(this.args && this.args[i]){                    // Set the arguments for the c node based on the symbol
    // 		for(var arg in this.args[i]){
    //                     nc.setAttribute(arg,this.args[i][arg]);
    // 		}
    //             }
    // 	}
    // 	return {"f":f, "first":first, "args":arglist};
    //     }

    // }

    Symbols.add_symbols(DEFAULT_SYMBOLS);
    export default Symbols;