SOURCE

console 命令行工具 X clear

                    
>
console
class Token extends Array {
    inlineLevel = {
        string: {
            t1: /^(['"]\1).*?\1/,
            t2: /^`.*?\`/s,
            t3: /^['"].*/,
            t4: /^`.*/s
        },
        regExp: /^\/.+\//,
        comment: {
            t1: /^\/\/.*/,
            t2: /^\/\*.*?\*\//s,
            t3: /^\/\*.*/s
        },
        operator: /^[\+\-\*\/\%\=\<\>\&\|\!\~\^]+/,
        punctuation: /^[\~\`\!\@\#\%\^\&\*\(\)\-\_\+\=\{\}\[\]\:\;\'\"\<\>\,\.\?\/\\]+/,
        space: /^ +/,
        number: /^\d+(\.\d+)*/,
        func: /^[^\~\`\!\@\#\%\^\&\*\(\)\-\_\+\=\{\}\[\]\:\;\'\"\<\>\,\.\?\/\\\s]+(?=\s*\()/,
        word: /^[^\~\`\!\@\#\%\^\&\*\(\)\-\_\+\=\{\}\[\]\:\;\'\"\<\>\,\.\?\/\\\s]+/,
        newline: /^\n/
    };
    constructor(str) {
        super();
        this.input = str;
        let cap = null;
        let g = this.inlineLevel;
        let self = this;
        let index = 0;
        let arr = [];
        let i = 0;
        while (str) {
            if ((cap = g.string.t1.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "string",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.string.t2.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "string",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.string.t3.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "string",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.string.t4.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "string",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.regExp.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "RegExp",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.comment.t1.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "comment",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.comment.t2.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "comment",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.comment.t3.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "comment",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.space.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "space",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.number.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "number",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.operator.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "operator",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.punctuation.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "punctuation",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.func.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "function",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if ((cap = g.word.exec(str))) {
                let match = cap[0];
                let len = match.length;
                let token = {
                    type: "word",
                    text: match,
                    color: ""
                };
                token.color = this.colorize(token);
                arr.push(token);
                str = str.slice(len);
            }
            if (g.newline.test(str)) {
                self.push(arr);
                arr = [];
                str = str.slice(1);
            }
        }
    }
    colorize(option) {
        let type = option.type.toLowerCase();
        let text = option.text;
        let keywords = ["var", "const", "let", "for", "do", "while", "if", "else", "try", "catch"
            , "finally", "with", "debugger", "delete", "in", "constructor", "class", "extends", "void",
            "yield", "async", "await", "new", "switch", "case", "default", "of", "function", "import", "export"
            , "as", "typeof", "instanceof", "break", "continue", "return", "throw"];
        if (type == "string") {
            return "#98c379";
        }
        if (type == "number"
            || type == "regexp"
            || text == "true"
            || text == 'false'
            || text == "null"
            || text == "undefined") {
            return "#d19a66"
        }
        if (type == "comment") {
            return "#7f848e";
        }
        if (type == 'operator') {
            return "#56b6c2";
        }
        if (type == "punctuation" || type == "space") {
            return "#abb2bf";
        }
        if (text == "this"
            || text == "super"
            || text == "document"
            || text == "window"
            || /^[A-Z]/.test(text)) {
            return "#e5c07b";
        }
        if (keywords.indexOf(text) != -1) {
            return "#c678dd";
        }
        if (type == "function") {
            return "#61afef";
        }
        if (type == "word") {
            return "#e06c75";
        }
    }
}
let code = `let a = 10;
console.log(a);
function add(a,b) {
    return a + b;
}
`
let token = new Token(code);
console.log(token)
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=, initial-scale=">
	<meta http-equiv="X-UA-Compatible" content="">
	<title></title>
</head>
<body>
	
</body>
</html>