59 lines
No EOL
1.9 KiB
TypeScript
59 lines
No EOL
1.9 KiB
TypeScript
import { Parser } from './tokenizer/parser.class.js';
|
|
import { Token } from './tokenizer/token.interfance.js';
|
|
|
|
Array.from(document.getElementsByClassName('tokenized')).map(tokenized => tokenized as HTMLDivElement).forEach(tokenized => {
|
|
const run = () => {
|
|
const source: string = tokenized.innerText;
|
|
|
|
console.time('tokenized');
|
|
const tokens: Array<Token> = new Parser(source).tokenize();
|
|
console.timeEnd('tokenized');
|
|
|
|
tokenized.innerHTML = '';
|
|
|
|
const lines: Array<string> = source.split('\n');
|
|
|
|
for (let line = 0; line < lines.length; line++) {
|
|
const lineValue: string = lines[line];
|
|
|
|
for (let column = 0; column < lineValue.length; column++) {
|
|
let found: boolean = false;
|
|
|
|
for (const token of tokens) {
|
|
if (token.position.starts(line, column)) {
|
|
found = true;
|
|
|
|
const element: HTMLSpanElement = document.createElement('span');
|
|
|
|
element.style.color = token.type;
|
|
element.innerText = lineValue.substring(token.position.column, token.position.length + token.position.column);
|
|
|
|
tokenized.appendChild(element);
|
|
|
|
break;
|
|
}
|
|
|
|
if (token.position.intersects(line, column)) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (found) continue;
|
|
if (column >= lineValue.length) break;
|
|
|
|
tokenized.innerHTML += lineValue[column];
|
|
}
|
|
|
|
const element: HTMLSpanElement = document.createElement('span');
|
|
element.innerText = '\n';
|
|
tokenized.appendChild(element);
|
|
}
|
|
|
|
tokenized.lastChild.remove();
|
|
};
|
|
|
|
tokenized.addEventListener('focusout', run);
|
|
|
|
run();
|
|
}); |