Manually highlight code using the lexer
This commit is contained in:
parent
d3089905d4
commit
6c898b0d01
@ -1,15 +1,14 @@
|
|||||||
---
|
---
|
||||||
|
import { lex } from "../lexer/lexer";
|
||||||
const {code} = Astro.props;
|
const {code} = Astro.props;
|
||||||
|
|
||||||
let c = trimAndDedent(code);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the following:
|
* Performs the following:
|
||||||
* - Removes the first & last line, if they are empty
|
* - Removes the first & last line, if they are empty
|
||||||
* - Picks the indentation level from the first non-white line
|
* - Picks the indentation level from the first non-white line
|
||||||
* - Dedents the following lines
|
* - Dedents the following lines
|
||||||
*/
|
*/
|
||||||
function trimAndDedent(input: string): string {
|
function trimAndDedent(input: string): Array<string> {
|
||||||
let lines = input.split("\n");
|
let lines = input.split("\n");
|
||||||
|
|
||||||
// Remove empty lines at the start
|
// Remove empty lines at the start
|
||||||
@ -70,16 +69,36 @@ function trimAndDedent(input: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines.join("\n");
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Apply syntax highlighting
|
function highlightCode(lines: Array<string>): string {
|
||||||
|
let outLines: Array<string> = [];
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const tokens = lex(line);
|
||||||
|
const lineArray = [];
|
||||||
|
|
||||||
|
for (const token of tokens) {
|
||||||
|
if (token.token_type !== "") {
|
||||||
|
lineArray.push(`<span class="token ${token.token_type}">${token.v}</span>`);
|
||||||
|
} else {
|
||||||
|
lineArray.push(token.v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outLines.push(lineArray.join(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
return outLines.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const codeHtml = highlightCode(trimAndDedent(code));
|
||||||
---
|
---
|
||||||
|
|
||||||
<div class="bg-black rounded">
|
<div class="bg-black rounded">
|
||||||
<span class="inline-block bg-[var(--code-theme-bg-acolor)] px-2 rounded-tl rounded-tr font-mono text-sm">thp code</span>
|
<span class="inline-block bg-[var(--code-theme-bg-acolor)] px-2 rounded-tl rounded-tr font-mono text-sm">thp code</span>
|
||||||
<pre class="language-thp" style="margin: 0; border-top-left-radius: 0;" data-disabled><code>{c}</code></pre>
|
<pre class="language-thp" style="margin: 0; border-top-left-radius: 0;" data-disabled><code set:html={codeHtml}></code></pre>
|
||||||
<div class="grid grid-cols-2 font-mono text-sm">
|
<div class="grid grid-cols-2 font-mono text-sm">
|
||||||
<div>
|
<div>
|
||||||
<div class="p-1 border-b border-r border-white">stdout</div>
|
<div class="p-1 border-b border-r border-white">stdout</div>
|
||||||
|
@ -16,6 +16,17 @@ Then run `thp hello.thp`
|
|||||||
|
|
||||||
## Try
|
## Try
|
||||||
|
|
||||||
|
```thp
|
||||||
|
val x = 322
|
||||||
|
val y = 322
|
||||||
|
|
||||||
|
fun f(Int x, Int y) {
|
||||||
|
print("gaaaa {x} {y}")
|
||||||
|
}
|
||||||
|
|
||||||
|
f(x, y)
|
||||||
|
```
|
||||||
|
|
||||||
<InteractiveCode
|
<InteractiveCode
|
||||||
code={`
|
code={`
|
||||||
val x = 322
|
val x = 322
|
||||||
|
Loading…
Reference in New Issue
Block a user