JSFiddle - React, Tailwind, and code Playground

by Glutamat

JavaScript

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) {
        if ("\\" === c) {
            this.next();
            return true;
        }
        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;
            f = this.next () + this.next ();
            while (true && c !== undefined) {
                c = this.next ();
                //console.warn (f,c,this.peak (0),(c === "*" || c=== "#") && this.peak (0) === "/");
                if (f === "//" && c === "\n") {
                     break   
                } else if (f !== "//") {
                    if ((c === "*" || c=== "#") && this.peak (0) === "/") {
           ...