syntax highlighting
This commit is contained in:
parent
b9218c8ace
commit
d9b351ad64
5 changed files with 213 additions and 9 deletions
|
@ -1,4 +1,5 @@
|
|||
import { CodeJar } from "../vendor/codejar.js";
|
||||
import { compileSyntax, highlight } from "./literate-programming/highlight.js";
|
||||
|
||||
let literatePrograms = new Map();
|
||||
|
||||
|
@ -34,9 +35,81 @@ function getLiterateProgramWorkerCommands(name) {
|
|||
}
|
||||
|
||||
class InputMode {
|
||||
static JAVASCRIPT = compileSyntax({
|
||||
patterns: [
|
||||
{ regex: /\/\/.*/, as: "comment" },
|
||||
{ regex: /\/\*.*?\*\//ms, as: "comment" },
|
||||
{ regex: /[A-Z_][a-zA-Z0-9_]*/, as: "keyword2" },
|
||||
{ regex: /[a-zA-Z_][a-zA-Z0-9_]*(?=\()/, as: "function" },
|
||||
{ regex: /[a-zA-Z_][a-zA-Z0-9_]*/, as: "identifier" },
|
||||
{ regex: /0[bB][01_]+n?/, as: "literal" },
|
||||
{ regex: /0[oO][0-7_]+n?/, as: "literal" },
|
||||
{ regex: /0[xX][0-9a-fA-F_]+n?/, as: "literal" },
|
||||
{ regex: /[0-9_]+n/, as: "literal" },
|
||||
{ regex: /[0-9_]+(\.[0-9_]*([eE][-+]?[0-9_]+)?)?/, as: "literal" },
|
||||
{ regex: /'(\\'|[^'])*'/, as: "string" },
|
||||
{ regex: /"(\\"|[^"])*"/, as: "string" },
|
||||
{ regex: /`(\\`|[^"])*`/, as: "string" },
|
||||
// TODO: RegExp literals?
|
||||
{ regex: /[+=/*^%<>!~|&\.-]+/, as: "operator" },
|
||||
{ regex: /[,;]/, as: "punct" },
|
||||
],
|
||||
keywords: new Map([
|
||||
["as", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["async", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["await", { into: "keyword1" }],
|
||||
["break", { into: "keyword1" }],
|
||||
["case", { into: "keyword1" }],
|
||||
["catch", { into: "keyword1" }],
|
||||
["class", { into: "keyword1" }],
|
||||
["const", { into: "keyword1" }],
|
||||
["continue", { into: "keyword1" }],
|
||||
["debugger", { into: "keyword1" }],
|
||||
["default", { into: "keyword1" }],
|
||||
["delete", { into: "keyword1" }],
|
||||
["do", { into: "keyword1" }],
|
||||
["else", { into: "keyword1" }],
|
||||
["export", { into: "keyword1" }],
|
||||
["extends", { into: "keyword1" }],
|
||||
["finally", { into: "keyword1" }],
|
||||
["for", { into: "keyword1" }],
|
||||
["from", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["function", { into: "keyword1" }],
|
||||
["get", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["if", { into: "keyword1" }],
|
||||
["import", { into: "keyword1" }],
|
||||
["in", { into: "keyword1" }],
|
||||
["instanceof", { into: "keyword1" }],
|
||||
["let", { into: "keyword1" }],
|
||||
["new", { into: "keyword1" }],
|
||||
["of", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["return", { into: "keyword1" }],
|
||||
["set", { into: "keyword1", onlyReplaces: "identifier" }],
|
||||
["static", { into: "keyword1" }],
|
||||
["switch", { into: "keyword1" }],
|
||||
["throw", { into: "keyword1" }],
|
||||
["try", { into: "keyword1" }],
|
||||
["typeof", { into: "keyword1" }],
|
||||
["var", { into: "keyword1" }],
|
||||
["void", { into: "keyword1" }],
|
||||
["while", { into: "keyword1" }],
|
||||
["with", { into: "keyword1" }],
|
||||
["yield", { into: "keyword1" }],
|
||||
|
||||
["super", { into: "keyword2" }],
|
||||
["this", { into: "keyword2" }],
|
||||
|
||||
["false", { into: "literal" }],
|
||||
["true", { into: "literal" }],
|
||||
["undefined", { into: "literal" }],
|
||||
["null", { into: "literal" }],
|
||||
]),
|
||||
})
|
||||
|
||||
constructor(frame) {
|
||||
this.frame = frame;
|
||||
|
||||
InputMode.highlight(frame);
|
||||
this.codeJar = CodeJar(frame, InputMode.highlight);
|
||||
this.codeJar.onUpdate(() => {
|
||||
for (let handler of frame.program.onChanged) {
|
||||
|
@ -48,7 +121,7 @@ class InputMode {
|
|||
}
|
||||
|
||||
static highlight(frame) {
|
||||
// TODO: Syntax highlighting
|
||||
highlight(frame, InputMode.JAVASCRIPT);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue