/
xverizex
/
codeview-js
Обзор
Документация
Войти
/
xverizex
/
codeview-js
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
codeview-js.js
348 строк
7 KB
Dmitry
[*] Переименовал
03 июл 2026, 02:19
03 июл 2026, 02:19
23c1ced
Код
Авторство
О чём код?
const NO_TYPE = 'none'; const TYPE_VARIABLE = 'variable'; const TYPE_NUMBER = 'number'; const TYPE_STRING = 'string'; const TYPE_LABEL = 'label'; const TYPE_REGS = 'regs'; class Token { constructor(type_token, text_buf) { this.type = type_token; this.content = text_buf; } } class Language { constructor(type, func_literals) { this.type = type; this.func_literals = func_literals; } } function buf_is_hex_number (buf) { let length = buf.length; if (length < 3) return false; if ((buf[0] == '0') && (buf[1] == 'x')) { for (let i = 2; i < length; i++) { if ((buf[i] >= '0') && (buf[i] <= '9')) { continue; } if ((buf[i] >= 'A') && (buf[i] <= 'F')) { continue; } if ((buf[i] >= 'a') && (buf[i] <= 'f')) { continue; } return false; } return true; } return false; } function buf_is_number (buf) { let length = buf.length; if (length == 0) return false; for (let i = 0; i < length; i++) { if ((buf[i] >= '0') && (buf[i] <= '9')) { continue; } return false; } return true; } function get_token_from_buf_c (buf) { if (buf_is_number (buf)) { return TYPE_NUMBER; } if (buf_is_hex_number (buf)) { return TYPE_NUMBER; } switch (buf) { case 'int': case 'float': case 'uint32_t': case 'uint8_t': case 'int32_t': case 'int8_t': case 'void': case 'const': case 'sizeof': case 'char': return TYPE_VARIABLE; default: return NO_TYPE; } } function get_token_from_buf_asm_intel (buf) { if (buf_is_number (buf)) { return TYPE_NUMBER; } if (buf_is_hex_number (buf)) { return TYPE_NUMBER; } switch (buf) { case 'mov': case 'jmp': return TYPE_VARIABLE; case 'eax': case 'ebp': case 'ecx': case 'edx': return TYPE_REGS; default: return NO_TYPE; } } function is_delimeter (sym) { switch (sym) { case ' ': case ';': case '*': case '/': case '.': case '-': case '\\': case '(': case ')': case '&': case '"': case "'": case '~': case '!': case '@': case '#': case '$': case '^': case '=': case '{': case '}': case ':': case '[': case ']': case ',': case '\t': case '\n': return true; default: return false; } } function tok_is_gt (tok) { let length = tok.length; if (length < 3) { return false; } if ( (tok[length - 1].content == ";") && (tok[length - 2].content == "gt") && (tok[length - 3].content == "&")) { return true; } return false; } function tok_is_lt (tok) { let length = tok.length; if (length < 3) { return false; } if ( (tok[length - 1].content == ";") && (tok[length - 2].content == "lt") && (tok[length - 3].content == "&")) { return true; } return false; } function tok_is_label (tok, sym) { if (tok.length > 0) { console.log ('sym: ' + sym); console.log ('tok: ' + tok[tok.length - 1].type); console.log ('name: ' + tok[tok.length - 1].content); if ((sym == ':') && (tok[tok.length - 1].type == NO_TYPE)) { return true; } } return false; } function add_to_tok (tok, i, code, buf, lang) { if (buf.length > 0) { let type_token = lang.func_literals (buf); let token = new Token(type_token, buf); tok.push(token); if (tok_is_gt (tok)) { tok.pop(); tok.pop(); tok.pop(); tok.push(new Token ('none', '>')); } if (tok_is_lt (tok)) { tok.pop(); tok.pop(); tok.pop(); tok.push(new Token ('none', '<')); } if (tok_is_label (tok, code[i])) { let token = tok.pop(); token.type = TYPE_LABEL; tok.push(token); } } } function add_to_tok_string (tok, i, code, buf, lang) { if (buf.length > 0) { let token = new Token("string", buf); tok.push(token); if (tok_is_gt (tok)) { tok.pop(); tok.pop(); tok.pop(); tok.push(new Token ('string', '>')); } if (tok_is_lt (tok)) { tok.pop(); tok.pop(); tok.pop(); tok.push(new Token ('string', '<')); } } token = new Token('string', code[i]); tok.push(token); } function get_code_tokens (code, lang) { let length = code.length; let buf = ''; let tok = []; let quotes = 0; let mode_string = 0; let is_new_line = 1; for (let i = 0; i < length; i++) { if (is_delimeter (code[i])) { let found_string = 0; if (code[i] == '"') { if (mode_string == 1) { add_to_tok_string (tok, i, code, buf, lang); mode_string = 0; buf = ''; continue; } mode_string = 1; } if (mode_string == 1) { add_to_tok_string (tok, i, code, buf, lang); buf = ''; continue; } if (buf.length > 0) add_to_tok (tok, i, code, buf, lang); if ((i > 0) && (code[i] == '\n')) { buf = '<br/>'; } else if (code[i] == ' ') buf = ' '; else buf = code[i]; token = new Token('none', buf); tok.push(token); buf = ''; continue; } buf += code[i]; } return tok; } function set_colors (code_node, token) { let buf = ''; let length = token.length; for (let i = 0; i < length; i++) { let tok = token[i]; let text = "<p class='" + tok.type + "'>" + tok.content + "</p>"; buf += text; } return buf; } function parse_code (lang) { let code_array = document.getElementsByClassName(lang.type); let code_array_length = code_array.length; for (let i = 0; i < code_array_length; i++) { let code_node = code_array[i]; let code = code_node.textContent; let tokens = get_code_tokens (code, lang); let buf = set_colors (code_node, tokens); code_node.innerHTML = buf; } } window.onload = function () { let lang = new Language ('code-c', get_token_from_buf_c); parse_code (lang); lang = new Language ('code-intel', get_token_from_buf_asm_intel); parse_code (lang); }