started on number literals

This commit is contained in:
Kyler Olsen 2023-12-01 12:09:25 -07:00
parent cd7d174388
commit 38ede66b0b
3 changed files with 100 additions and 4 deletions

View File

@ -56,6 +56,7 @@ November 2023
.idle-light .operator { color: #000000 }
.idle-light .punctuation { color: #000000 }
.idle-light .whitespace { color: #000000 }
.idle-light .token-sep { display: none; }
.idle-dark .view-code { background-color: #002240; color: #ffffff; }
.idle-dark .edit-code { caret-color: #ffffff; }
@ -81,6 +82,7 @@ November 2023
.idle-dark .operator { color: #ffffff }
.idle-dark .punctuation { color: #ffffff }
.idle-dark .whitespace { color: #ffffff }
.idle-dark .token-sep { display: none; }
.idle-test .view-code { background-color: #442240; color: #ff0000; }
.idle-test .edit-code { caret-color: #ff0000; }
@ -106,3 +108,4 @@ November 2023
.idle-test .operator { color: #000080 }
.idle-test .punctuation { color: #8000ff }
.idle-test .whitespace { background-color: #ffffff }
.idle-test .token-sep { background-color: #ff0000; width: 5px; }

View File

@ -60,9 +60,7 @@ function insertTab() {
function syntax_highlight(text) {
const whitespace = " \n\t";
const digit_start = "0123456789.";
const digit_second = "bBoO";
const digit_continue = "0123456789._";
const string_start = `'"`;
const string_alt_start = [
`r'`, `r"`, `rf`, `rb`, `rF`, `rB`,
@ -73,6 +71,20 @@ function syntax_highlight(text) {
`B'`, `B"`, `Br`, `BR`,
];
const number_start = "0123456789.";
const number_second = "0123456789bBeEjJ0OxX._";
const number_dec_start = "123456789.";
const number_dec_continue = "0123456789._";
const number_bin_start = ["0b", "0B"];
const number_bin_continue = "01_";
const number_oct_start = ["0o", "0O"];
const number_oct_continue = "01234567_";
const number_hex_start = ["0x", "0X"];
const number_hex_continue = "0123456789abcdefABCEDF_";
const number_exp_start = "eE";
const number_exp_second = "+-0123456789";
const number_exp_continue = "0123456789_";
const tokens = [];
var char = text.substr(0, 1); text = text.substr(1);
@ -133,6 +145,78 @@ function syntax_highlight(text) {
}
}
// Number literals
else if (number_start.includes(char)) {
token.token_type = "number";
if (number_dec_start.includes(char)) {
while (text.length && number_dec_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
if (number_exp_start.includes(char) && number_exp_second.includes(text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
while (text.length && number_exp_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
}
if ("jJ".includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
} else if (number_bin_start.includes(char + text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
while (text.length && number_bin_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
} else if (number_oct_start.includes(char + text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
while (text.length && number_oct_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
} else if (number_hex_start.includes(char + text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
while (text.length && number_hex_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
} else if (char == 0 && "eE".includes(text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
if (number_exp_start.includes(char) && number_exp_second.includes(text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
while (text.length && number_exp_continue.includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
}
if ("jJ".includes(char)) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
} else if (char == 0 && !number_second.includes(text.substr(0, 1))) {
token.value += char;
char = text.substr(0, 1); text = text.substr(1);
}
}
else {
token.token_type = "token";
token.value = char;
@ -140,6 +224,7 @@ function syntax_highlight(text) {
}
tokens.push(token);
tokens.push({'token_type': 'sep', 'value':'·'});
}
return tokens;
@ -159,7 +244,12 @@ function syntax_highlight_html(text) {
else if (token.token_type.substr(-6) == "string") {
element.classList.add("literal");
element.classList.add("string");
} else
} else if (token.token_type == "number") {
element.classList.add("literal");
element.classList.add("number");
} else if (token.token_type == "sep")
element.classList.add("token-sep");
else
element.classList.add("token");
element.innerText = token.value;
elements.push(element);

View File

@ -22,6 +22,9 @@ November 2023
.code {
flex-grow: 1;
}
.code.idle-test {
flex-grow: 2;
}
</style>
<link rel="stylesheet" href="editor.css">