JSFiddle - React, Tailwind, and code Playground

by Glutamat

JavaScript

//TODO: 
/*

Implement all syntax elements.
Complete the lexer and parser to match all es5 specs
Refactor Tokenizer !! //Pass token objects with more metadata around instead of making tokens from values
Refactor Parser !!
Dedicate Compiler
Add optimization methods
    minification
    uglification
    prettification
Standardize generated AST 
*/

var KEYWORDS = ["function", "return", "opt", "var", "while", "for", "if", "else", "throw", "new", "typeof"];
var END = {
    cmt: function (c) {
        var f = this.peak(-1);
        return !( !! ~ ["#", "*"].indexOf(f) && "/" === c)
    },
    str: function (c) {
        console.log(c)
        if ("\\" === c) {
            c = this.next();
            return true;
        }
        if (c === "n") alert("asd")
        return c !== "'" && c !== '"';
    },
    num: function (c) {
        return /[0-9]/.test(c);
    },
    name: function (c) {
        return /[a-zA-Z0-9_]/.test(c);
    }
}
var START = {
    cmt: function (c) {
        var s = this.peak(1);
        return ("/" === c && (s === "/" || s === "*" || s === "#"))
    },
    ws: function (c) {
        if ("\n" === c) {
            this.line++;
        }
        return c <= " ";
    },
    str: function (c) {
        return c === "'" || c === '"';
    },
    num: function (c) {
        return Number.isFinite(parseInt(c, 10));
    },
    name: function (c) {
        return /[a-zA-Z]/.test(c);
    }
}


var Tokenize = function (src) {
    this.from = 0;
    this.to = 0;
    this.line = 0;
    this.src = src;
    this.tokens = [];
}

Tokenize.prototype.make = function (type, value) {
    this.tokens.push({
        type: type,
        value: value,
        from: this.from,
        to: this.to,
        line: this.line
    });
}

Tokenize.prototype.tokenize = function () {
    var c, v, l = 0,
        f;
    while ((c = this.peak())) {
        if (START.ws.call(this, c)) {
            this.next();
        } else if (START.cmt.call(this, c)) {
            var i = 0;
    ...