All files parser.js

90.28% Statements 195/216
80% Branches 48/60
74.42% Functions 32/43
93.26% Lines 180/193

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 2751x 3x 3x 3x   3x         17x   3x 80x 80x 80x 8x 2x     72x 72x 72x 72x 72x   80x     3x   507x     507x 152x 152x   355x 355x 355x 355x 355x 355x 110x 245x 110x 110x     135x 2x 2x 2x 133x 85x 85x 48x 48x 48x 48x       355x 355x 355x 355x 355x     3x   239x 239x 239x 239x 75x 75x 75x   239x     3x 42x 42x 55x   42x     3x 3x 3x 8x 8x   3x     3x 3x   3x 3x   3x 3x 48x   3x 3x 85x   3x 3x 3x   3x 3x 88x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 13x     3x 153x 153x 153x 153x 153x       1x 230x 230x 278x 278x 772x 772x 772x 772x 277x 277x 277x 277x 277x     278x 1x 1x 1x 1x 1x   1x 1x             230x     1x   62x 62x     31x 79x       1x   19x 19x     43x 15x           1x 1x 1x 15x 9x 9x 9x 7x 10x 10x 7x   3x     9x 9x     6x       1x   4x 4x     17x 2x 5x       1x 1x 1x 7x   7x 8x 8x 8x 8x     7x 2x                
var Parser = function(token_types){
    var self = this;
    this.token_types = token_types;
    this.symbol_table = {};
 
    this.original_symbol = {
        nud: function () { throw Error("Undefined"); },
        led: function () { throw Error("Missing operator"); }
    };
 
    this.mul = function(left){ return ["*", [left, this.nud()]]; };
    
    this.symbol = function (id, bp) {
        var s = self.symbol_table[id];
        bp = bp || 0;
        if (s) {
            if (bp >= s.lbp) {
                s.lbp = bp;
            }
        } else {
            s = Object.create(self.original_symbol);
            s.id = s.value = id;
            s.lbp = bp;
            s.parent = self;
            self.symbol_table[id] = s;
        }
        return s;
    };
    
    this.advance = function (id) {
        var a, o, t, v;
        Iif (id && this.token.id !== id) {
            throw Error("Expected '" + id + "'");
        }
        if (self.token_nr >= self.tokens.length) {
            self.token = this.symbol_table["(end)"];
            return;
        }
        t = self.tokens[self.token_nr];
        self.token_nr += 1;
        v = t.value;
        var args = null;
        a = t.type;
        if (a === "name") {
            o = this.symbol_table["(var)"];
        } else if (a === "operator") {
            o = this.symbol_table[v];
            Iif (!o) {
                throw Error("Unknown operator");
            }
        } else if (a === "pass") {
            a = "pass";
            o = this.symbol_table["(pass)"];
            args = t.args;
        } else if (a === "number") {
            a = "literal";
            o = this.symbol_table["(literal)"];
        } else Eif (a === "function") {
            a = "function";
            o = this.symbol_table["(function)"];
            args = t.args;
        } else {
            throw Error("Unexpected token",t);
        }
        self.token = Object.create(o);
        self.token.type = a;
        self.token.value = v;
        if(args) self.token.args = args;
        return self.token;
    };
 
    this.expression = function (rbp) {
        var left;
        var t = self.token;
        self.advance();
        left = t.nud();
        while (rbp < self.token.lbp) {
            t = self.token;
            self.advance();
            left = t.led(left);
        }
        return left;
    };
 
    this.infix = function (id, bp, led) {
        var s = this.symbol(id, bp);
        s.led = led || function (left) {
            return [this.value, [left, self.expression(bp)]];
        };
        return s;
    };
    
    this.prefix = function (id, nud) {
        var s = self.symbol(id);
        s.nud = nud || function () {
            Eif(this.value == "-") this.value = "neg";
            return [this.value, [self.expression(70)]];
        };
        return s;
    }
 
    this.symbol("(end)");
    var s = null;
    
    s = this.symbol("(blank)", 60);
    s.nud = function(){ return ["blank"];};
 
    s = this.symbol("(function)", 60);
    s.led = this.mul;
    s.nud = function(){ return [this.value, this.args || []];};
    
    s = this.symbol("(literal)", 60);
    s.led = this.mul;
    s.nud = function(){ return ["val", [this.value]] };
 
    s = this.symbol("(pass)", 60);
    s.led = this.mul;
    s.nud = function(){ return this.args[0] };
    
    s = this.symbol("(var)", 60);
    s.led = this.mul;
    s.nud = function(){ return ["var", [this.value]] };
        
    this.token_nr = 0;
    this.tokens = [];
    
    this.infix("=", 40);
    this.infix("!=", 40);
    this.infix("<", 40);
    this.infix(">", 40);
    this.infix("<=", 40);
    this.infix(">=", 40);
    this.infix("+", 50);
    this.infix("-", 50);
    this.infix("*", 60);
    this.infix("/", 60);
    this.infix("!", 70, function(left){ return ["factorial", [left]]; });
    this.infix("^", 70, function(left){ return ["exponential", [left, self.expression(70)]]; });
    this.infix("_", 70, function(left){ return ["subscript", [left, self.expression(70)]]; });
    this.infix("(", 80, self.mul);
    this.symbol("(").nud = function(){ var ans = self.expression(0); self.advance(")"); return ans; }
    this.symbol(")");
    this.symbol("{").nud = function(){ var ans = self.expression(0); self.advance("}"); return ans; }
    this.symbol("}");
    this.symbol(",");
    this.prefix("-");
 
    this.tokenise_and_parse = function(str){
        return this.parse(this.tokenise(str));
    }
    
    this.parse = function(tokens){
        this.tokens = tokens;
        this.token_nr = 0;
        Iif(this.tokens.length == 0) return ["blank"];
        this.advance();
        return this.expression(10);
    }
}
 
Parser.prototype.tokenise = function(text){
    var ans = [];
    while(text.length > 0){
        var ok = false;
        for(var i = 0; i < this.token_types.length; i++){
            var t = this.token_types[i];
            var re = RegExp(t.re);
            var m = re.exec(text);
            if(m){
                m = m[0];
                text = text.substring(m.length);
                ok = true;
                Eif(t.type != "space") ans.push({"type":t.type, "value": t.value(m)})
                break;
            }
        }
        if(!ok){
            Eif(text.charCodeAt(0) > 128){
                var c = "";
                for(var ch of text){
                    c = ch;
                    break;
                }
                ans.push({"type":"name", "value":c});
                text = text.substring(c.length);
            }
            else{
                return [];
            }
        }
    }
    return ans;
}
 
var EParser = new Parser([
    {"type":"number", "re":"^[0-9.]+", "value":function(m){
        Iif(isNaN(Number(m))) throw Error("Invalid number: "+m);
        return Number(m);
    }},
    {"type":"operator", "re":"^(<=|>=|!=|>|<|=)", "value":function(m){return m}},
    {"type":"operator", "re":"^[-+*/!]", "value":function(m){return m}},
    {"type":"name", "re":"^[a-zA-Z]", "value":function(m){return m}},
    {"type":"space", "re":"^\\s+", "value":function(m){return m}}
]);
 
var TextParser = new Parser([
    {"type":"number", "re":"^[0-9.]+", "value":function(m){
        Iif(isNaN(Number(m))) throw Error("Invalid number: "+m);
        return Number(m);
    }},
    {"type":"operator", "re":"^(!=|>=|<=)", "value":function(m){return m;}},
    {"type":"operator", "re":"^[-+*/,!()=<>_^]", "value":function(m){return m}},
    {"type":"name", "re":"^[a-zA-Z_]*[a-zA-Z]", "value":function(m){return m}},
    {"type":"comma", "re":"^,", "value":function(m){return m}},
    {"type":"space", "re":"^\\s+", "value":function(m){return m}}
]);
 
 
var s = TextParser.symbol("(var)", 60);
s.led = TextParser.mul;
s.nud = function(){
    if(this.parent.token.id == "("){
        var args = [];
        TextParser.advance()
        if(this.parent.token.id !== ")"){
            while(true){
                args.push(TextParser.expression(0));
                if (this.parent.token.id !== ",") {
                    break;
                }
                TextParser.advance(",");
            }
        }
        TextParser.advance(")");
        return [this.value, args];
    }
    else{
        return ["var", [this.value]]
    }
};
 
var LaTeXParser = new Parser([
    {"type":"number", "re":"^[0-9.]+", "value":function(m){
        Iif(isNaN(Number(m))) throw Error("Invalid number: "+m);
        return Number(m);
    }},
    {"type":"operator", "re":"^(!=|>=|<=)", "value":function(m){return m;}},
    {"type":"operator", "re":"^[-+*/,!()=<>_^}{]", "value":function(m){return m}},
    {"type":"name", "re":"^[a-zA-Z_]*[a-zA-Z]", "value":function(m){return m}},
    {"type":"name", "re":"^\\\\[a-zA-Z]*[a-zA-Z]", "value":function(m){return m.substring(1)}},
    {"type":"space", "re":"^\\s+", "value":function(m){return m}}
]);
 
s = LaTeXParser.symbol("(var)", 60);
s.led = LaTeXParser.mul;
s.nud = function(){
    var args = [];
    
    while(this.parent.token.id == "{"){
        LaTeXParser.advance()
        Eif(this.parent.token.id !== "}"){
            args.push(LaTeXParser.expression(0));
            LaTeXParser.advance("}");
        }
    }
    if(args.length > 0) return [this.value, args];
    else return ["var", [this.value]]
};
 
export default {"Parser":Parser,
                "TextParser":TextParser,
                "LaTeXParser":LaTeXParser,
                "EParser": EParser};