Fixed syntax highlighting for 0x-style literals

This commit is contained in:
PaddiM8 2021-12-31 21:18:30 +01:00
parent 95a5718e38
commit a922eb84d5
2 changed files with 19 additions and 5 deletions

View File

@ -97,6 +97,7 @@ impl Highlighter for LineHighlighter {
let reg = Regex::new( let reg = Regex::new(
r"(?x) r"(?x)
(?P<op>([+\-/*%^!×÷]|if|otherwise)) | (?P<op>([+\-/*%^!×÷]|if|otherwise)) |
(?P<radix>0[box][a-zA-Z0-9]+) |
(?P<identifier>[^!-@\s_|^\[\]\{\}¹²³]+(_\d+)?)", (?P<identifier>[^!-@\s_|^\[\]\{\}¹²³]+(_\d+)?)",
) )
.unwrap(); .unwrap();

View File

@ -127,7 +127,7 @@
const [result, success] = calculate(kalk, input); const [result, success] = calculate(kalk, input);
output = success output = success
? highlight(result)[0] ? highlight(result, true)[0]
: `<span style="color: ${errorcolor}">${result}</span>`; : `<span style="color: ${errorcolor}">${result}</span>`;
} }
@ -274,13 +274,26 @@
if (autofocus) element.focus(); if (autofocus) element.focus();
} }
function highlight(input: string): [string, number] { function highlight(
input: string,
isOutput: boolean = false
): [string, number] {
if (!input) return ["", 0]; if (!input) return ["", 0];
let result = input; let result = input;
let offset = 0; let offset = 0;
result = result.replace( result = result.replace(
/(?<comparison>(!=|[<>]=?))|(?<html>[<>&]|(\n\s*\}?|\s+))|(?<op>([+\-/*%^!≈×÷]|if|otherwise)|(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈\[\]\{\}≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)\(?)/g, /(?<radix>0[box][a-zA-Z0-9]+)|(?<comparison>(!=|[<>]=?))|(?<html>[<>&]|(\n\s*\}?|\s+))|(?<op>([+\-/*%^!≈×÷]|if|otherwise)|(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈\[\]\{\}≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)\(?)/g,
(substring, _, comparison, _2, html, _3, op, identifier) => { (
substring,
_radix,
_,
comparison,
_2,
html,
_3,
op,
identifier
) => {
if (comparison) { if (comparison) {
if (substring == "<=") return "≤"; if (substring == "<=") return "≤";
if (substring == ">=") return "≥"; if (substring == ">=") return "≥";
@ -296,7 +309,7 @@
return "<br />}"; return "<br />}";
} else { } else {
if (!substring.match(/\n\s\s/)) offset += 2; if (!substring.match(/\n\s\s/)) offset += 2;
return "<br />&nbsp;&nbsp;"; return isOutput ? "<br />" : "<br />&nbsp;&nbsp;";
} }
} }
if (substring.match(/\s+/)) { if (substring.match(/\s+/)) {