Skip to content

Distribute and run LLMs with a single file.

Port remaining highlighting code to javascript

Justine Tunneyfdfdb136c50e
diff --git a/llama.cpp/common.cpp b/llama.cpp/common.cpp
index 1cff40f8..c4086d74 100644
--- a/llama.cpp/common.cpp
+++ b/llama.cpp/common.cpp
@@ -284,6 +284,7 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
if (params.n_threads_batch <= 0) {
params.n_threads_batch = cpu_get_num_math(); // [jart]
}
+ FLAG_threads_batch = params.n_threads_batch; // [jart]
return true;
}
if (arg == "-td" || arg == "--threads-draft") {
@@ -832,6 +833,7 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
invalid_param = true;
return true;
}
+ FLAG_split_mode = params.split_mode; // [jart]
// #ifndef GGML_USE_CUDA_SYCL_VULKAN // [jart]
// fprintf(stderr, "warning: llama.cpp was compiled without CUDA/SYCL/Vulkan. Setting the split mode has no effect.\n");
// #endif // GGML_USE_CUDA_SYCL_VULKAN
diff --git a/llamafile/flags.cpp b/llamafile/flags.cpp
index aacc4406..f85af82d 100644
--- a/llamafile/flags.cpp
+++ b/llamafile/flags.cpp
@@ -68,6 +68,7 @@ int FLAG_seed = LLAMA_DEFAULT_SEED;
int FLAG_slots = 1;
int FLAG_split_mode = LLAMA_SPLIT_MODE_LAYER;
int FLAG_threads;
+int FLAG_threads_batch;
int FLAG_token_burst = 100;
int FLAG_token_cidr = 24;
int FLAG_ubatch = 512;
@@ -315,6 +316,13 @@ void llamafile_get_flags(int argc, char **argv) {
continue;
}
+ if (!strcmp(flag, "-tb") || !strcmp(flag, "--threads-batch")) {
+ if (i == argc)
+ missing("--threads-batch");
+ FLAG_threads_batch = atoi(argv[i++]);
+ continue;
+ }
+
if (!strcmp(flag, "-b") || !strcmp(flag, "--batch-size")) {
if (i == argc)
missing("--batch-size");
diff --git a/llamafile/highlight_markdown.cpp b/llamafile/highlight_markdown.cpp
index 1b4928ba..f3f3c680 100644
--- a/llamafile/highlight_markdown.cpp
+++ b/llamafile/highlight_markdown.cpp
@@ -35,6 +35,7 @@ enum {
INCODE,
INCODE2,
INCODE2_TICK,
+ INCODE2_TICK2,
EMPHASIS,
EMPHASIS_BACKSLASH,
};
@@ -81,6 +82,7 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {
// handle \*\*not bold\*\* etc.
t_ = BACKSLASH;
*r += '\\';
+ bol_ = false;
} else {
lf::append_wchar(r, c);
}
@@ -119,6 +121,7 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {
if (c == '\\')
t_ = EMPHASIS_BACKSLASH;
}
+ bol_ = false;
break;
case EMPHASIS:
@@ -171,13 +174,20 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {
case TICK:
if (c == '`') {
- t_ = TICK_TICK;
+ if (bol_) {
+ t_ = TICK_TICK;
+ } else {
+ *r += HI_INCODE;
+ *r += "``";
+ t_ = INCODE2;
+ }
} else {
*r += HI_INCODE;
*r += '`';
lf::append_wchar(r, c);
t_ = INCODE;
}
+ bol_ = false;
break;
case INCODE:
@@ -202,13 +212,22 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {
case INCODE2_TICK:
lf::append_wchar(r, c);
if (c == '`') {
- *r += HI_RESET;
- t_ = NORMAL;
+ t_ = INCODE2_TICK2;
} else {
t_ = INCODE2;
}
break;
+ case INCODE2_TICK2:
+ if (c == '`') {
+ *r += '`';
+ } else {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
case TICK_TICK:
if (c == '`') {
t_ = LANG;
@@ -300,6 +319,7 @@ void HighlightMarkdown::flush(std::string *r) {
case INCODE:
case INCODE2:
case INCODE2_TICK:
+ case INCODE2_TICK2:
case STRONG:
case STRONG_BACKSLASH:
case STRONG_STAR:
diff --git a/llamafile/llamafile.h b/llamafile/llamafile.h
index ade93ea4..c94fa750 100644
--- a/llamafile/llamafile.h
+++ b/llamafile/llamafile.h
@@ -43,6 +43,7 @@ extern int FLAG_seed;
extern int FLAG_slots;
extern int FLAG_split_mode;
extern int FLAG_threads;
+extern int FLAG_threads_batch;
extern int FLAG_token_burst;
extern int FLAG_token_cidr;
extern int FLAG_ubatch;
diff --git a/llamafile/server/client.cpp b/llamafile/server/client.cpp
index dd775dca..bebea08b 100644
--- a/llamafile/server/client.cpp
+++ b/llamafile/server/client.cpp
@@ -636,6 +636,8 @@ Client::dispatcher()
return embedding();
if (p1 == "v1/chat/completions")
return v1_chat_completions();
+ if (p1 == "slotz")
+ return slotz();
// serve static endpoints
int infd;
diff --git a/llamafile/server/client.h b/llamafile/server/client.h
index de84b710..a7fd9fcf 100644
--- a/llamafile/server/client.h
+++ b/llamafile/server/client.h
@@ -114,4 +114,6 @@ struct Client
bool v1_chat_completions() __wur;
bool get_v1_chat_completions_params(V1ChatCompletionParams*) __wur;
+
+ bool slotz() __wur;
};
diff --git a/llamafile/server/slot.cpp b/llamafile/server/slot.cpp
index 18e8873e..1d550086 100644
--- a/llamafile/server/slot.cpp
+++ b/llamafile/server/slot.cpp
@@ -72,10 +72,11 @@ Slot::start()
cparams.n_batch = FLAG_batch;
cparams.n_ubatch = FLAG_ubatch;
cparams.n_seq_max = 1;
- cparams.n_threads = FLAG_threads;
- cparams.n_threads_batch = MIN(FLAG_threads, 20);
- cparams.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_NONE;
- cparams.pooling_type = LLAMA_POOLING_TYPE_NONE;
+ cparams.n_threads = MIN(FLAG_threads, 20);
+ cparams.n_threads_batch = FLAG_threads;
+ cparams.rope_scaling_type = LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
+ cparams.pooling_type = LLAMA_POOLING_TYPE_UNSPECIFIED;
+ cparams.attention_type = LLAMA_ATTENTION_TYPE_UNSPECIFIED;
cparams.rope_freq_base = 0;
cparams.yarn_ext_factor = -1;
cparams.yarn_attn_factor = 1;
@@ -83,8 +84,9 @@ Slot::start()
cparams.yarn_beta_slow = 1;
cparams.yarn_orig_ctx = 0;
cparams.defrag_thold = -1;
- cparams.type_k = GGML_TYPE_F16;
- cparams.type_v = GGML_TYPE_F16;
+ cparams.offload_kqv = true;
+ cparams.type_k = X86_HAVE(AVX512_BF16) ? GGML_TYPE_BF16 : GGML_TYPE_F16;
+ cparams.type_v = X86_HAVE(AVX512_BF16) ? GGML_TYPE_BF16 : GGML_TYPE_F16;
cparams.flash_attn = FLAG_flash_attn;
system_fingerprint_ = generate_system_fingerprint(&cparams);
if (!(ctx_ = llama_new_context_with_model(model_, cparams)))
@@ -155,3 +157,12 @@ Slot::prefill(const std::vector<int>& tokens)
reuse_count);
return eval_tokens(new_tokens);
}
+
+std::string
+Slot::dump()
+{
+ std::string r;
+ for (size_t i = 0; i < history_.size(); ++i)
+ r += llama_token_to_piece(ctx_, history_[i], RENDER_SPECIAL_TOKENS);
+ return r;
+}
diff --git a/llamafile/server/slot.h b/llamafile/server/slot.h
index 4fe7a6ba..c7349c08 100644
--- a/llamafile/server/slot.h
+++ b/llamafile/server/slot.h
@@ -16,6 +16,7 @@
// limitations under the License.
#pragma once
+#include <string>
#include <time.h>
#include <utility>
#include <vector>
@@ -38,4 +39,5 @@ struct Slot
bool eval_token(int);
bool eval_tokens(std::vector<int>);
bool prefill(const std::vector<int>&);
+ std::string dump();
};
diff --git a/llamafile/server/slots.cpp b/llamafile/server/slots.cpp
index c102c629..4d8e7293 100644
--- a/llamafile/server/slots.cpp
+++ b/llamafile/server/slots.cpp
@@ -48,6 +48,7 @@ Slots::start(int count)
if (slot->start()) {
++made;
slots_.emplace(slot);
+ all_slots_.push_back(slot);
} else {
delete slot;
}
diff --git a/llamafile/server/slots.h b/llamafile/server/slots.h
index 64c4b24a..00cb4f4b 100644
--- a/llamafile/server/slots.h
+++ b/llamafile/server/slots.h
@@ -28,6 +28,7 @@ struct Slots
{
llama_model* model_;
std::multiset<SlotEntry> slots_;
+ std::vector<Slot*> all_slots_;
pthread_mutex_t lock_;
pthread_cond_t cond_;
diff --git a/llamafile/server/slotz.cpp b/llamafile/server/slotz.cpp
new file mode 100644
index 00000000..362b7c68
--- /dev/null
+++ b/llamafile/server/slotz.cpp
@@ -0,0 +1,39 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
+//
+// Copyright 2024 Mozilla Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "client.h"
+#include "server.h"
+#include "slot.h"
+#include "slots.h"
+#include "utils.h"
+#include "worker.h"
+
+bool
+Client::slotz()
+{
+ std::string s = std::string(or_empty(param("add_special")));
+ int id = atoi(s.c_str());
+ if (id < 0)
+ return send_error(400);
+ if (id >= worker_->server_->slots_->all_slots_.size())
+ return send_error(404);
+ Slot* slot = worker_->server_->slots_->all_slots_[id];
+ std::string dump = slot->dump();
+ char* p = append_http_response_message(obuf_.p, 200);
+ p = stpcpy(p, "Content-Type: text/plain\r\n");
+ return send_response(obuf_.p, p, dump);
+}
diff --git a/llamafile/server/www/chatbot.css b/llamafile/server/www/chatbot.css
index 744388dc..75d09dd9 100644
--- a/llamafile/server/www/chatbot.css
+++ b/llamafile/server/www/chatbot.css
@@ -11,6 +11,11 @@ body {
background: #f5f5f5;
}
+p {
+ margin: 1rem auto;
+ box-sizing: border-box;
+}
+
.chat-container {
max-width: 800px;
margin: 2rem auto;
@@ -30,9 +35,12 @@ body {
}
.chat-header h1 {
- font-size: 1.25rem;
+ font-size: 2rem;
color: #212529;
- vertical-align: center;
+}
+
+.chat-header img {
+ vertical-align: middle;
}
.chat-messages {
@@ -105,8 +113,29 @@ body {
cursor: not-allowed;
}
+.stop-button {
+ padding: 0.75rem 1.5rem;
+ background: #dc3545;
+ color: white;
+ border: none;
+ border-radius: 6px;
+ cursor: pointer;
+ font-size: 1rem;
+ transition: background-color 0.2s;
+}
+
+.stop-button:hover {
+ background: #bb2d3b;
+}
+
+.stop-button:disabled {
+ background: #6c757d;
+ cursor: not-allowed;
+}
+
.message pre {
- background: #f8f9fa;
+ margin: 1rem auto;
+ background: #fefefe;
padding: 0.5rem;
border-radius: 4px;
overflow-x: auto;
diff --git a/llamafile/server/www/chatbot.js b/llamafile/server/www/chatbot.js
index 2f5dc4b2..7e48c147 100644
--- a/llamafile/server/www/chatbot.js
+++ b/llamafile/server/www/chatbot.js
@@ -19,7 +19,9 @@ const chatMessages = document.getElementById("chat-messages");
const chatInput = document.getElementById("chat-input");
const sendButton = document.getElementById("send-button");
const typingIndicator = document.getElementById("typing-indicator");
+const stopButton = document.getElementById('stop-button');
+let abortController = null;
let streamingMessageContent = [];
let chatHistory = [
@@ -47,7 +49,16 @@ function scrollToBottom() {
function setTypingIndicatorVisibility(visible) {
typingIndicator.style.display = visible ? "flex" : "none";
+}
+
+function cleanupAfterMessage() {
+ setTypingIndicatorVisibility(false);
chatMessages.scrollTop = chatMessages.scrollHeight;
+ chatInput.disabled = false;
+ sendButton.style.display = 'inline-block';
+ stopButton.style.display = 'none';
+ abortController = null;
+ chatInput.focus();
}
async function handleChatStream(response) {
@@ -91,13 +102,19 @@ async function handleChatStream(response) {
buffer = lines[lines.length - 1];
}
} catch (error) {
- console.error("Error reading stream:", error);
+ if (error.name !== 'AbortError') {
+ console.error("Error reading stream:", error);
+ }
} finally {
high.flush();
- setTypingIndicatorVisibility(false);
- sendButton.disabled = false;
- chatInput.disabled = false;
- chatInput.focus();
+ cleanupAfterMessage();
+ }
+}
+
+function stopMessage() {
+ if (abortController) {
+ abortController.abort();
+ cleanupAfterMessage();
}
}
@@ -108,7 +125,10 @@ async function sendMessage() {
// disable input while processing
chatInput.value = "";
chatInput.disabled = true;
- sendButton.disabled = true;
+ sendButton.style.display = 'none';
+ stopButton.style.display = "inline-block";
+ stopButton.focus();
+ abortController = new AbortController();
// add user message to chat
const userMessageElement = createMessageElement(message, "user");
@@ -131,8 +151,10 @@ async function sendMessage() {
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: chatHistory,
+ temperature: 0.0,
stream: true
- })
+ }),
+ signal: abortController.signal
});
if (!response.ok)
@@ -146,14 +168,14 @@ async function sendMessage() {
chatHistory.push({ role: "assistant", content: lastMessage });
} catch (error) {
- console.error("Error:", error);
- const errorMessage = createMessageElement(
- "Sorry, there was an error processing your request.",
- "system");
- chatMessages.appendChild(errorMessage);
- setTypingIndicatorVisibility(false);
- sendButton.disabled = false;
- chatInput.disabled = false;
+ if (error.name !== 'AbortError') {
+ console.error("Error:", error);
+ const errorMessage = createMessageElement(
+ "Sorry, there was an error processing your request.",
+ "system");
+ chatMessages.appendChild(errorMessage);
+ }
+ cleanupAfterMessage();
}
}
@@ -166,6 +188,7 @@ for (let i = 0; i < chatHistory.length; i++) {
// setup events
sendButton.addEventListener("click", sendMessage);
+stopButton.addEventListener("click", stopMessage);
chatInput.addEventListener("keypress", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
diff --git a/llamafile/server/www/ctype.js b/llamafile/server/www/ctype.js
index 80e2342c..6a50ffb3 100644
--- a/llamafile/server/www/ctype.js
+++ b/llamafile/server/www/ctype.js
@@ -17,6 +17,11 @@ function isascii(c) {
return 0 <= i && i <= 127;
}
+function isdigit(c) {
+ let i = c.charCodeAt(0);
+ return (48 <= i && i <= 57);
+}
+
function isalpha(c) {
let i = c.charCodeAt(0);
return (65 <= i && i <= 90) ||
@@ -31,14 +36,65 @@ function isalnum(c) {
}
function isspace(c) {
- return c == ' ' ||
- c == '\t' ||
- c == '\n' ||
- c == '\r' ||
- c == '\v';
+ switch (c) {
+ case ' ':
+ case '\t':
+ case '\r':
+ case '\n':
+ case '\v':
+ return true;
+ default:
+ return false;
+ }
}
function isblank(c) {
- return c == ' ' ||
- c == '\t';
+ switch (c) {
+ case ' ':
+ case '\n':
+ return true;
+ default:
+ return false;
+ }
+}
+
+function ispunct(c) {
+ switch (c) {
+ case '!':
+ case '"':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '\'':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case '[':
+ case '\\':
+ case ']':
+ case '^':
+ case '_':
+ case '`':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ case '}':
+ return true;
+ default:
+ return false;
+ }
}
diff --git a/llamafile/server/www/highlight.js b/llamafile/server/www/highlight.js
index adb7b82f..db47525b 100644
--- a/llamafile/server/www/highlight.js
+++ b/llamafile/server/www/highlight.js
@@ -116,7 +116,6 @@ class Highlighter extends Highlight {
super();
this.delegate = delegate;
this.state = 0;
- this.delta = 1;
}
push(tagName, className) {
diff --git a/llamafile/server/www/highlight_ada.js b/llamafile/server/www/highlight_ada.js
index 8170c7c8..99af173f 100644
--- a/llamafile/server/www/highlight_ada.js
+++ b/llamafile/server/www/highlight_ada.js
@@ -226,6 +226,7 @@ class HighlightAda extends Highlighter {
}
this.state = HighlightAda.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_asm.js b/llamafile/server/www/highlight_asm.js
index f68d0d79..1adaeb14 100644
--- a/llamafile/server/www/highlight_asm.js
+++ b/llamafile/server/www/highlight_asm.js
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-const ASM_PREFIXS = new Set([
+const ASM_PREFIXES = new Set([
'addr32',
'cs',
'data16',
@@ -75,17 +75,375 @@ const ASM_QUALIFIERS = new Set([
class HighlightAsm extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static BACKSLASH = 3;
+ static SLASH0 = 4;
+ static SLASH = 5;
+ static REG0 = 6;
+ static REG = 7;
+ static SLASH_SLASH = 8;
+ static SLASH_STAR = 9;
+ static SLASH_STAR_STAR = 10;
+ static QUOTE = 11;
+ static QUOTE_BACKSLASH = 12;
+ static QUOTE_FINISH = 13;
+ static DQUOTE = 14;
+ static DQUOTE_BACKSLASH = 15;
+ static DOLLAR = 16;
+ static IMMEDIATE = 17;
+ static IMMEDIATE_QUOTE = 18;
+ static IMMEDIATE_QUOTE_BACKSLASH = 19;
+ static IMMEDIATE_QUOTE_FINISH = 20;
+ static HASH = 21;
+
constructor(delegate) {
super(delegate);
+ this.c = 0;
+ this.last = 0;
+ this.col = 0;
+ this.is_preprocessor = false;
+ this.is_first_thing_on_line = true;
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ this.last = this.c;
+ this.c = c;
+ switch (this.state) {
+
+ case HighlightAsm.NORMAL:
+ if (!isascii(c) || //
+ isalnum(c) || //
+ c == '-' || //
+ c == '.' || //
+ c == '@' || //
+ c == '_' || //
+ (c == '#' && this.col == 0)) {
+ this.state = HighlightAsm.WORD;
+ this.word += c;
+ break;
+ } else if (c == '#' && this.col && isspace(this.last)) {
+ this.state = HighlightAsm.HASH;
+ } else if ((c == ';' || c == '!') && (!this.col || isspace(this.last))) {
+ this.state = HighlightAsm.COMMENT;
+ this.push("span", "comment");
+ this.append(c);
+ } else if (c == '/' && this.col == 0) {
+ // bell system five allowed single slash comments
+ // anywhere on the line, but we limit that a bit.
+ this.state = HighlightAsm.SLASH0;
+ this.push("span", "comment");
+ this.append('/');
+ } else if (c == '/') {
+ this.state = HighlightAsm.SLASH;
+ this.is_first_thing_on_line = false;
+ } else if (c == '$') {
+ this.append('$');
+ this.state = HighlightAsm.DOLLAR;
+ this.is_first_thing_on_line = false;
+ } else if (c == '%') {
+ this.state = HighlightAsm.REG0;
+ this.is_first_thing_on_line = false;
+ } else if (c == '\\') {
+ this.state = HighlightAsm.BACKSLASH;
+ this.push("span", "escape");
+ this.append('\\');
+ this.is_first_thing_on_line = false;
+ } else if (c == '\'') {
+ this.state = HighlightAsm.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightAsm.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ if (c == '\n')
+ this.is_preprocessor = false;
+ if (!isspace(c))
+ this.is_first_thing_on_line = false;
+ if (c == ':')
+ this.is_first_thing_on_line = true;
+ this.append(c);
+ }
+ break;
+
+ case HighlightAsm.DOLLAR:
+ if (is_immediate(c) || c == '\'') {
+ this.push("span", "immediate");
+ this.state = HighlightAsm.IMMEDIATE;
+ } else {
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ // fallthrough
+
+ case HighlightAsm.IMMEDIATE:
+ if (is_immediate(c)) {
+ this.append(c);
+ } else if (c == '\'') {
+ this.append(c);
+ this.state = HighlightAsm.IMMEDIATE_QUOTE;
+ } else {
+ this.pop();
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ break;
+
+ case HighlightAsm.IMMEDIATE_QUOTE:
+ if (c == '\\') {
+ this.append(c);
+ this.state = HighlightAsm.IMMEDIATE_QUOTE_BACKSLASH;
+ } else if (c == '\n') {
+ this.pop();
+ this.epsilon(HighlightAsm.NORMAL);
+ } else {
+ this.append(c);
+ this.state = HighlightAsm.IMMEDIATE_QUOTE_FINISH;
+ }
+ break;
+
+ case HighlightAsm.IMMEDIATE_QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightAsm.IMMEDIATE_QUOTE_FINISH;
+ break;
+
+ case HighlightAsm.IMMEDIATE_QUOTE_FINISH:
+ if (c == '\'') {
+ this.append(c);
+ this.state = HighlightAsm.IMMEDIATE;
+ } else {
+ // yes '" means '"' in bell system five
+ this.epsilon(HighlightAsm.IMMEDIATE);
+ }
+ break;
+
+ case HighlightAsm.BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightAsm.NORMAL;
+ break;
+
+ case HighlightAsm.HASH:
+ if (isspace(c)) {
+ this.push("span", "comment");
+ this.append('#');
+ this.epsilon(HighlightAsm.COMMENT);
+ } else {
+ this.word += '#';
+ this.state = HighlightAsm.WORD;
+ }
+ // fallthrough
+
+ case HighlightAsm.WORD:
+ if (!isascii(c) || isalnum(c) || c == '$' || c == '_' || c == '-' || c == '.') {
+ this.word += c;
+ } else {
+ if (this.is_first_thing_on_line) {
+ if (this.word.length > 1 &&
+ this.word[0] == '#' &&
+ C_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ this.is_first_thing_on_line = false;
+ this.is_preprocessor = true;
+ } else if (c == ':') {
+ this.push("span", "label");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word == "C" || this.word == "dnl" || this.word == "m4_dnl") {
+ this.push("span", "comment");
+ this.append(this.word);
+ this.word = '';
+ this.epsilon(HighlightAsm.COMMENT);
+ } else {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ if (!ASM_PREFIXES.has(this.word.toLowerCase()))
+ this.is_first_thing_on_line = false;
+ }
+ } else if (this.is_preprocessor && C_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (ASM_QUALIFIERS.has(this.word.toLowerCase())) {
+ this.push("span", "qualifier");
+ this.append(this.word);
+ this.pop();
+ } else if (C_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ break;
+
+ case HighlightAsm.REG0:
+ if (isalpha(c) || c == '(' || c == ')') {
+ this.state = HighlightAsm.REG;
+ this.push("span", "register");
+ this.append('%');
+ } else {
+ this.append('%');
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ // fallthrough
+
+ case HighlightAsm.REG:
+ if (isalnum(c)) {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ break;
+
+ case HighlightAsm.QUOTE:
+ this.append(c);
+ if (c == '\'' || c == '\n') {
+ this.pop();
+ this.state = HighlightAsm.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightAsm.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightAsm.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightAsm.QUOTE;
+ break;
+
+ case HighlightAsm.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightAsm.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightAsm.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightAsm.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightAsm.DQUOTE;
+ break;
+
+ case HighlightAsm.SLASH0:
+ if (c == '*') {
+ this.append('*');
+ this.state = HighlightAsm.SLASH_STAR;
+ } else {
+ this.epsilon(HighlightAsm.COMMENT);
+ }
+ break;
+
+ case HighlightAsm.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightAsm.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightAsm.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightAsm.NORMAL);
+ }
+ break;
+
+ case HighlightAsm.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightAsm.SLASH_STAR_STAR;
+ break;
+
+ case HighlightAsm.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightAsm.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightAsm.SLASH_STAR;
+ }
+ break;
+
+ case HighlightAsm.COMMENT:
+ case HighlightAsm.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightAsm.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ if (c != '\n') {
+ this.col += 1;
+ } else {
+ this.col = 0;
+ this.is_first_thing_on_line = true;
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightAsm.WORD:
+ this.append(this.word);
+ this.word = '';
+ break;
+ case HighlightAsm.HASH:
+ this.append('#');
+ break;
+ case HighlightAsm.REG0:
+ this.append('%');
+ break;
+ case HighlightAsm.SLASH:
+ this.append('/');
+ break;
+ case HighlightAsm.REG:
+ case HighlightAsm.SLASH0:
+ case HighlightAsm.COMMENT:
+ case HighlightAsm.BACKSLASH:
+ case HighlightAsm.SLASH_SLASH:
+ case HighlightAsm.SLASH_STAR:
+ case HighlightAsm.SLASH_STAR_STAR:
+ case HighlightAsm.QUOTE:
+ case HighlightAsm.QUOTE_BACKSLASH:
+ case HighlightAsm.DQUOTE:
+ case HighlightAsm.DQUOTE_BACKSLASH:
+ case HighlightAsm.IMMEDIATE:
+ case HighlightAsm.IMMEDIATE_QUOTE:
+ case HighlightAsm.IMMEDIATE_QUOTE_BACKSLASH:
+ case HighlightAsm.IMMEDIATE_QUOTE_FINISH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightAsm.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
Highlighter.REGISTRY['asm'] = HighlightAsm;
+Highlighter.REGISTRY['assembler'] = HighlightAsm;
+Highlighter.REGISTRY['assembly'] = HighlightAsm;
+Highlighter.REGISTRY['fasm'] = HighlightAsm;
+Highlighter.REGISTRY['nasm'] = HighlightAsm;
+Highlighter.REGISTRY['s'] = HighlightAsm;
+Highlighter.REGISTRY['yasm'] = HighlightAsm;
diff --git a/llamafile/server/www/highlight_basic.js b/llamafile/server/www/highlight_basic.js
index df3ca95c..62ed6da7 100644
--- a/llamafile/server/www/highlight_basic.js
+++ b/llamafile/server/www/highlight_basic.js
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-const BASIC_KEYWORDSS = new Set([
+const BASIC_KEYWORDS = new Set([
'#const',
'#else',
'#elseif',
@@ -433,29 +433,55 @@ class HighlightBasic extends Highlighter {
switch (this.state) {
case HighlightBasic.NORMAL:
- if (!isascii(c) || isalpha(c)) {
- this.epsilon(HighlightBasic.WORD);
- } else if (c === '-') {
- this.state = HighlightBasic.HYPHEN;
- } else if (c === '\'' && this.last !== ')') {
- this.push('span', 'string');
- this.append(c);
- this.state = HighlightBasic.QUOTE;
- } else if (c === '"') {
- this.push('span', 'string');
- this.append(c);
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightBasic.WORD;
+ this.word += c;
+ } else if (c == '\'') {
+ this.state = HighlightBasic.COMMENT;
+ this.push("span", "comment");
+ this.append('\'');
+ } else if (c == '"') {
this.state = HighlightBasic.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '#' && this.is_bol) {
+ this.state = HighlightBasic.DIRECTIVE;
+ this.push("span", "directive");
+ this.append('#');
+ } else if (isdigit(c) && this.is_bol) {
+ this.push("span", "lineno");
+ this.append(c);
+ this.state = HighlightBasic.LINENO;
} else {
this.append(c);
}
break;
case HighlightBasic.WORD:
- if (!isascii(c) || isalnum(c) || c === '_' || c === '\'') {
+ if (!isascii(c) || isalnum(c) || c == '_') {
this.word += c;
} else {
- if (is_keyword_basic(this.word)) {
- this.push('span', 'keyword');
+ if (BASIC_KEYWORDS.has(this.word.toLowerCase())) {
+ if (this.word.toLowerCase() == "rem") {
+ this.push("span", "comment");
+ this.append(this.word);
+ this.epsilon(HighlightBasic.COMMENT);
+ this.word = '';
+ } else {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ }
+ } else if (BASIC_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (BASIC_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (BASIC_CONSTANTS.has(this.word.toLowerCase())) {
+ this.push("span", "constant");
this.append(this.word);
this.pop();
} else {
@@ -466,38 +492,30 @@ class HighlightBasic extends Highlighter {
}
break;
- case HighlightBasic.HYPHEN:
- if (c === '-') {
- this.push('span', 'comment');
- this.append('--');
- this.state = HighlightBasic.COMMENT;
- } else {
- this.append('-');
- this.epsilon(HighlightBasic.NORMAL);
- }
- break;
-
- case HighlightBasic.COMMENT:
+ case HighlightBasic.DQUOTE:
this.append(c);
- if (c === '\n') {
+ if (c == '"' || c == '\n') {
this.pop();
this.state = HighlightBasic.NORMAL;
}
break;
- case HighlightBasic.QUOTE:
- this.append(c);
- if (c === '\'') {
+ case HighlightBasic.LINENO:
+ if (isdigit(c)) {
+ this.append(c);
+ } else {
this.pop();
- this.state = HighlightBasic.NORMAL;
+ this.epsilon(HighlightBasic.NORMAL);
}
break;
- case HighlightBasic.DQUOTE:
+ case HighlightBasic.COMMENT:
+ case HighlightBasic.DIRECTIVE:
this.append(c);
- if (c === '"') {
+ if (c == '\n') {
this.pop();
this.state = HighlightBasic.NORMAL;
+ } else {
}
break;
@@ -517,20 +535,37 @@ class HighlightBasic extends Highlighter {
flush() {
switch (this.state) {
case HighlightBasic.WORD:
- if (is_keyword_basic(this.word)) {
- this.push('span', 'keyword');
+ if (BASIC_KEYWORDS.has(this.word.toLowerCase())) {
+ if (this.word.toLowerCase() == "rem") {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ }
+ } else if (BASIC_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (BASIC_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
this.append(this.word);
+ this.pop();
+ } else if (BASIC_CONSTANTS.has(this.word.toLowerCase())) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
} else {
this.append(this.word);
}
this.word = '';
break;
- case HighlightBasic.HYPHEN:
- this.append('-');
- break;
- case HighlightBasic.QUOTE:
+ case HighlightBasic.LINENO:
case HighlightBasic.DQUOTE:
case HighlightBasic.COMMENT:
+ case HighlightBasic.DIRECTIVE:
this.pop();
break;
default:
@@ -538,6 +573,7 @@ class HighlightBasic extends Highlighter {
}
this.state = HighlightBasic.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_c.js b/llamafile/server/www/highlight_c.js
index 4e3efd82..90363176 100644
--- a/llamafile/server/www/highlight_c.js
+++ b/llamafile/server/www/highlight_c.js
@@ -1978,7 +1978,7 @@ class HighlightC extends Highlighter {
case HighlightC.RAW:
this.append(c);
if (this.heredoc[this.i] == c) {
- if (++this.i == this.heredoc.size()) {
+ if (++this.i == this.heredoc.length) {
this.state = HighlightC.NORMAL;
this.pop();
}
@@ -2081,6 +2081,7 @@ class HighlightC extends Highlighter {
this.is_bol = true;
this.state = HighlightC.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_cobol.js b/llamafile/server/www/highlight_cobol.js
index 557ff5d4..992ab104 100644
--- a/llamafile/server/www/highlight_cobol.js
+++ b/llamafile/server/www/highlight_cobol.js
@@ -435,16 +435,164 @@ const COBOL_KEYWORDS = new Set([
class HighlightCobol extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static COMMENT = 6;
+
constructor(delegate) {
super(delegate);
+ this.col = -1;
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ ++this.col;
+ if (c == '\n')
+ this.col = -1;
+
+ if (this.state == HighlightCobol.NORMAL) {
+ if (this.col == 6 && c == '*') {
+ this.state = HighlightCobol.COMMENT;
+ this.push("span", "comment");
+ } else if (this.col == 6 && c == '-') {
+ this.push("span", "contin");
+ this.append(c);
+ this.pop();
+ continue;
+ } else if (this.col < 6 && isdigit(c)) {
+ this.push("span", "lineno");
+ this.append(c);
+ this.pop();
+ continue;
+ }
+ }
+
+ switch (this.state) {
+
+ case HighlightCobol.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '-') {
+ this.word += c;
+ this.state = HighlightCobol.WORD;
+ } else if (c == '!') {
+ this.state = HighlightCobol.COMMENT;
+ this.push("span", "comment");
+ this.append(c);
+ } else if (c == '\'') {
+ this.state = HighlightCobol.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightCobol.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightCobol.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '-') {
+ this.word += c;
+ } else if (c == '.' && this.word[0] == '.') {
+ this.word += c;
+ if (COBOL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.state = HighlightCobol.NORMAL;
+ } else {
+ if (COBOL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightCobol.NORMAL);
+ }
+ break;
+
+ case HighlightCobol.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightCobol.NORMAL;
+ }
+ break;
+
+ case HighlightCobol.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightCobol.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightCobol.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightCobol.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightCobol.QUOTE;
+ break;
+
+ case HighlightCobol.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightCobol.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightCobol.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightCobol.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightCobol.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightCobol.WORD:
+ if (COBOL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightCobol.QUOTE:
+ case HighlightCobol.QUOTE_BACKSLASH:
+ case HighlightCobol.DQUOTE:
+ case HighlightCobol.DQUOTE_BACKSLASH:
+ case HighlightCobol.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightBasic.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_csharp.js b/llamafile/server/www/highlight_csharp.js
index 51884fad..b55565a1 100644
--- a/llamafile/server/www/highlight_csharp.js
+++ b/llamafile/server/www/highlight_csharp.js
@@ -141,16 +141,246 @@ const CSHARP_CONSTANTS = new Set([
class HighlightCsharp extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static SLASH = 4;
+ static SLASH_SLASH = 5;
+ static SLASH_STAR = 6;
+ static SLASH_STAR_STAR = 7;
+ static DQUOTE = 8;
+ static STR = 9;
+ static STR_BACKSLASH = 10;
+ static DQUOTE_DQUOTE = 11;
+ static DQUOTE_DQUOTE_DQUOTE = 12;
+ static TRIPS = 13;
+ static TRIPS_DQUOTE = 14;
+
constructor(delegate) {
super(delegate);
+ this.trips1 = 0;
+ this.trips2 = 0;
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightCsharp.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '#') {
+ this.state = HighlightCsharp.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightCsharp.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightCsharp.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightCsharp.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightCsharp.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '$' || c == '#') {
+ this.word += c;
+ } else {
+ if (CSHARP_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (CSHARP_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightCsharp.NORMAL);
+ }
+ break;
+
+ case HighlightCsharp.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightCsharp.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightCsharp.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightCsharp.NORMAL);
+ }
+ break;
+
+ case HighlightCsharp.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ }
+ break;
+
+ case HighlightCsharp.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightCsharp.SLASH_STAR_STAR;
+ break;
+
+ case HighlightCsharp.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightCsharp.SLASH_STAR;
+ }
+ break;
+
+ case HighlightCsharp.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightCsharp.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightCsharp.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightCsharp.QUOTE;
+ break;
+
+ case HighlightCsharp.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightCsharp.DQUOTE_DQUOTE;
+ } else if (c == '\\') {
+ this.state = HighlightCsharp.STR_BACKSLASH;
+ } else {
+ this.state = HighlightCsharp.STR;
+ }
+ break;
+
+ case HighlightCsharp.STR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightCsharp.STR_BACKSLASH;
+ }
+ break;
+
+ case HighlightCsharp.STR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightCsharp.STR;
+ break;
+
+ case HighlightCsharp.DQUOTE_DQUOTE:
+ if (c == '"') {
+ this.append(c);
+ this.state = HighlightCsharp.DQUOTE_DQUOTE_DQUOTE;
+ this.trips1 = 3;
+ this.trips2 = 0;
+ } else {
+ this.pop();
+ this.epsilon(HighlightCsharp.NORMAL);
+ }
+ break;
+
+ case HighlightCsharp.DQUOTE_DQUOTE_DQUOTE:
+ if (c == '"') {
+ this.append(c);
+ ++this.trips1;
+ if (++this.trips2 == 3) {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ }
+ break;
+ } else {
+ this.trips2 = 0;
+ this.state = HighlightCsharp.TRIPS;
+ }
+ // fallthrough
+
+ case HighlightCsharp.TRIPS:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightCsharp.TRIPS_DQUOTE;
+ this.trips2 = 1;
+ }
+ break;
+
+ case HighlightCsharp.TRIPS_DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ if (++this.trips2 == this.trips1) {
+ this.pop();
+ this.state = HighlightCsharp.NORMAL;
+ }
+ } else {
+ this.state = HighlightCsharp.TRIPS;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightCsharp.WORD:
+ if (CSHARP_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (CSHARP_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightCsharp.SLASH:
+ this.append('/');
+ break;
+ case HighlightCsharp.QUOTE:
+ case HighlightCsharp.QUOTE_BACKSLASH:
+ case HighlightCsharp.DQUOTE:
+ case HighlightCsharp.STR:
+ case HighlightCsharp.STR_BACKSLASH:
+ case HighlightCsharp.DQUOTE_DQUOTE:
+ case HighlightCsharp.DQUOTE_DQUOTE_DQUOTE:
+ case HighlightCsharp.TRIPS:
+ case HighlightCsharp.TRIPS_DQUOTE:
+ case HighlightCsharp.SLASH_SLASH:
+ case HighlightCsharp.SLASH_STAR:
+ case HighlightCsharp.SLASH_STAR_STAR:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightCsharp.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_css.js b/llamafile/server/www/highlight_css.js
index b0bc65a0..4939061b 100644
--- a/llamafile/server/www/highlight_css.js
+++ b/llamafile/server/www/highlight_css.js
@@ -14,15 +14,221 @@
class HighlightCss extends Highlighter {
+ static NORMAL = 0;
+ static SELECTOR = 1;
+ static PROPERTY = 2;
+ static VALUE = 3;
+ static QUOTE = 4;
+ static QUOTE_BACKSLASH = 5;
+ static DQUOTE = 6;
+ static DQUOTE_BACKSLASH = 7;
+ static SLASH = 8;
+ static SLASH_STAR = 9;
+ static SLASH_STAR_STAR = 10;
+
constructor(delegate) {
super(delegate);
+ this.pushed = 0;
+ }
+
+ push(tagName, className) {
+ while (this.pushed)
+ this.pop();
+ ++this.pushed;
+ return this.delegate.push(tagName, className);
+ }
+
+ pop() {
+ if (!this.pushed)
+ throw new Error('bad pop');
+ --this.pushed;
+ this.delegate.pop();
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+
+ switch (this.state & 255) {
+
+ case HighlightCss.NORMAL:
+ this.push("span", "selector");
+ this.state = HighlightCss.SELECTOR;
+ // fallthrough
+
+ case HighlightCss.SELECTOR:
+ if (c == '{') {
+ this.state = HighlightCss.PROPERTY;
+ this.pop();
+ this.append(c);
+ this.push("span", "property");
+ } else if (c == ',') {
+ this.pop();
+ this.append(c);
+ this.push("span", "selector");
+ } else if (c == '/') {
+ this.state = HighlightCss.SELECTOR << 8 | HighlightCss.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightCss.SELECTOR << 8 | HighlightCss.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightCss.SELECTOR << 8 | HighlightCss.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightCss.PROPERTY:
+ if (c == '/') {
+ this.state = HighlightCss.PROPERTY << 8 | HighlightCss.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightCss.PROPERTY << 8 | HighlightCss.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightCss.PROPERTY << 8 | HighlightCss.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == ':') {
+ this.state = HighlightCss.VALUE;
+ this.pop();
+ this.append(c);
+ } else if (c == '}') {
+ this.state = HighlightCss.SELECTOR;
+ this.pop();
+ this.append(c);
+ this.push("span", "selector");
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightCss.VALUE:
+ if (c == '/') {
+ this.state = HighlightCss.VALUE << 8 | HighlightCss.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightCss.VALUE << 8 | HighlightCss.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightCss.VALUE << 8 | HighlightCss.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == ';') {
+ this.state = HighlightCss.PROPERTY;
+ this.append(c);
+ this.push("span", "property");
+ } else if (c == '}') {
+ this.state = HighlightCss.SELECTOR;
+ this.append(c);
+ this.push("span", "selector");
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightCss.SLASH:
+ if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state &= -256;
+ this.state |= HighlightCss.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.state >>= 8;
+ this.epsilon(this.state);
+ }
+ break;
+
+ case HighlightCss.SLASH_STAR:
+ this.append(c);
+ if (c == '*') {
+ this.state &= -256;
+ this.state |= HighlightCss.SLASH_STAR_STAR;
+ }
+ break;
+
+ case HighlightCss.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state >>= 8;
+ if (this.state == HighlightCss.SELECTOR)
+ this.push("span", "selector");
+ if (this.state == HighlightCss.PROPERTY)
+ this.push("span", "property");
+ } else if (c != '*') {
+ this.state &= -256;
+ this.state |= HighlightCss.SLASH_STAR;
+ }
+ break;
+
+ case HighlightCss.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state >>= 8;
+ if (this.state == HighlightCss.SELECTOR)
+ this.push("span", "selector");
+ if (this.state == HighlightCss.PROPERTY)
+ this.push("span", "property");
+ } else if (c == '\\') {
+ this.state &= -256;
+ this.state |= HighlightCss.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightCss.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state &= -256;
+ this.state |= HighlightCss.QUOTE;
+ break;
+
+ case HighlightCss.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state >>= 8;
+ if (this.state == HighlightCss.SELECTOR)
+ this.push("span", "selector");
+ if (this.state == HighlightCss.PROPERTY)
+ this.push("span", "property");
+ } else if (c == '\\') {
+ this.state &= -256;
+ this.state |= HighlightCss.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightCss.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state &= -256;
+ this.state |= HighlightCss.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state & 255) {
+ case HighlightCss.SLASH:
+ this.append('/');
+ break;
+ default:
+ break;
+ }
+ while (this.pushed)
+ this.pop();
+ this.state = HighlightCss.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
+
+Highlighter.REGISTRY['css'] = HighlightCss;
diff --git a/llamafile/server/www/highlight_cxx.js b/llamafile/server/www/highlight_cxx.js
index 87718b32..401ac6d9 100644
--- a/llamafile/server/www/highlight_cxx.js
+++ b/llamafile/server/www/highlight_cxx.js
@@ -196,8 +196,8 @@ class HighlightCxx extends HighlightC {
}
Highlighter.REGISTRY['c++'] = HighlightCxx;
-Highlighter.REGISTRY['cxx'] = HighlightCxx;
-Highlighter.REGISTRY['cpp'] = HighlightCxx;
-Highlighter.REGISTRY['hpp'] = HighlightCxx;
Highlighter.REGISTRY['cc'] = HighlightCxx;
+Highlighter.REGISTRY['cpp'] = HighlightCxx;
Highlighter.REGISTRY['cu'] = HighlightCxx;
+Highlighter.REGISTRY['cxx'] = HighlightCxx;
+Highlighter.REGISTRY['hpp'] = HighlightCxx;
diff --git a/llamafile/server/www/highlight_forth.js b/llamafile/server/www/highlight_forth.js
index dd64f4ae..76b1ae2d 100644
--- a/llamafile/server/www/highlight_forth.js
+++ b/llamafile/server/www/highlight_forth.js
@@ -73,16 +73,122 @@ const FORTH_DEFS = new Set([
class HighlightForth extends Highlighter {
+ static NORMAL = 0;
+ static SYNTAX = 1;
+
constructor(delegate) {
super(delegate);
+ this.is_label = false;
+ this.closer = '';
+ this.word = '';
+ this.pushed = 0;
+ }
+
+ push(tagName, className) {
+ while (this.pushed)
+ this.pop();
+ ++this.pushed;
+ return this.delegate.push(tagName, className);
+ }
+
+ pop() {
+ if (!this.pushed)
+ throw new Error('bad pop');
+ --this.pushed;
+ this.delegate.pop();
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightForth.NORMAL:
+ if (!isspace(c)) {
+ this.word += c;
+ } else if (this.word != "") {
+ if (this.is_label) {
+ this.append(this.word);
+ this.pop();
+ this.is_label = false;
+ } else if (this.word == "\\") { // line comment
+ this.push("span", "comment");
+ this.append(this.word);
+ this.state = HighlightForth.SYNTAX;
+ this.closer = '\n';
+ } else if (this.word == "(" || // inline comment, e.g. ( arg1 arg2 -- res1 )
+ this.word == ".(") { // printed comment, e.g. .( compiling... )
+ this.push("span", "comment");
+ this.append(this.word);
+ this.state = HighlightForth.SYNTAX;
+ this.closer = ')';
+ } else if (this.word == ".\"" || // string
+ this.word == "s\"" || // stack string
+ this.word == "S\"" || // stack string
+ this.word == "c\"" || // counted string
+ this.word == "C\"") { // counted string
+ this.push("span", "string");
+ this.append(this.word);
+ this.state = HighlightForth.SYNTAX;
+ this.closer = '"';
+ } else if (FORTH_DEFS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.push("span", "def");
+ this.is_label = true;
+ } else if (FORTH_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.append(c);
+ break;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightForth.SYNTAX:
+ this.append(c);
+ if (c == this.closer) {
+ this.pop();
+ this.state = HighlightForth.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightForth.NORMAL:
+ if (this.is_label) {
+ this.append(this.word);
+ } else if (FORTH_KEYWORDS.has(this.word.toLowerCase()) ||
+ FORTH_DEFS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ default:
+ break;
+ }
+ while (this.pushed)
+ this.pop();
+ this.is_label = false;
+ this.state = HighlightForth.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_fortran.js b/llamafile/server/www/highlight_fortran.js
index 4a0beec4..83046282 100644
--- a/llamafile/server/www/highlight_fortran.js
+++ b/llamafile/server/www/highlight_fortran.js
@@ -253,16 +253,181 @@ const FORTRAN_TYPES = new Set([
class HighlightFortran extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static COMMENT = 6;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.col = -1;
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+
+ ++this.col;
+ if (c == '\n')
+ this.col = -1;
+
+ if (this.state == HighlightFortran.NORMAL) {
+ if (this.col == 0 && (c == '*' || c == 'c' || c == 'C')) {
+ this.state = HighlightFortran.COMMENT;
+ this.push("span", "comment");
+ } else if (this.col == 5 && c != ' ') {
+ this.push("span", "contin");
+ this.append(c);
+ this.pop();
+ continue;
+ } else if (this.col <= 4 && isdigit(c)) {
+ this.push("span", "label");
+ this.append(c);
+ this.pop();
+ continue;
+ }
+ }
+
+ switch (this.state) {
+
+ case HighlightFortran.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '.') {
+ this.word += c;
+ this.state = HighlightFortran.WORD;
+ } else if (c == '!') {
+ this.state = HighlightFortran.COMMENT;
+ this.push("span", "comment");
+ this.append(c);
+ } else if (c == '\'') {
+ this.state = HighlightFortran.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightFortran.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightFortran.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else if (c == '.' && this.word[0] == '.') {
+ this.word += c;
+ if (FORTRAN_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.state = HighlightFortran.NORMAL;
+ } else {
+ if (FORTRAN_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (FORTRAN_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (FORTRAN_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightFortran.NORMAL);
+ }
+ break;
+
+ case HighlightFortran.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightFortran.NORMAL;
+ }
+ break;
+
+ case HighlightFortran.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightFortran.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightFortran.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightFortran.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightFortran.QUOTE;
+ break;
+
+ case HighlightFortran.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightFortran.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightFortran.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightFortran.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightFortran.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightFortran.WORD:
+ if (FORTRAN_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (FORTRAN_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (FORTRAN_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightFortran.QUOTE:
+ case HighlightFortran.QUOTE_BACKSLASH:
+ case HighlightFortran.DQUOTE:
+ case HighlightFortran.DQUOTE_BACKSLASH:
+ case HighlightFortran.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightFortran.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_go.js b/llamafile/server/www/highlight_go.js
index 1b3ecaa2..ea34186a 100644
--- a/llamafile/server/www/highlight_go.js
+++ b/llamafile/server/www/highlight_go.js
@@ -63,16 +63,199 @@ const GO_TYPES = new Set([
class HighlightGo extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static TICK = 10;
+ static TICK_BACKSLASH = 11;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightGo.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightGo.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightGo.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightGo.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightGo.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '`') {
+ this.state = HighlightGo.TICK;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightGo.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (GO_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (GO_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightGo.NORMAL);
+ }
+ break;
+
+ case HighlightGo.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightGo.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightGo.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightGo.NORMAL);
+ }
+ break;
+
+ case HighlightGo.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightGo.NORMAL;
+ }
+ break;
+
+ case HighlightGo.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightGo.SLASH_STAR_STAR;
+ break;
+
+ case HighlightGo.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightGo.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightGo.SLASH_STAR;
+ }
+ break;
+
+ case HighlightGo.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightGo.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightGo.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightGo.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightGo.QUOTE;
+ break;
+
+ case HighlightGo.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightGo.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightGo.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightGo.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightGo.DQUOTE;
+ break;
+
+ case HighlightGo.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightGo.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightGo.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightGo.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightGo.TICK;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightGo.WORD:
+ if (GO_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (GO_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightGo.SLASH:
+ this.append('/');
+ break;
+ case HighlightGo.QUOTE:
+ case HighlightGo.QUOTE_BACKSLASH:
+ case HighlightGo.DQUOTE:
+ case HighlightGo.DQUOTE_BACKSLASH:
+ case HighlightGo.SLASH_SLASH:
+ case HighlightGo.SLASH_STAR:
+ case HighlightGo.SLASH_STAR_STAR:
+ case HighlightGo.TICK:
+ case HighlightGo.TICK_BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightGo.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_haskell.js b/llamafile/server/www/highlight_haskell.js
index 820d1427..a7896668 100644
--- a/llamafile/server/www/highlight_haskell.js
+++ b/llamafile/server/www/highlight_haskell.js
@@ -47,16 +47,295 @@ const HASKELL_KEYWORDS = new Set([
class HighlightHaskell extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static DQUOTE = 2;
+ static DQUOTE_BACKSLASH = 3;
+ static TICK = 4;
+ static TICK_BACKSLASH = 5;
+ static CURL = 6;
+ static CURL_HYPHEN = 7;
+ static CURL_HYPHEN_HYPHEN = 8;
+ static CURL_HYPHEN_CURL = 9;
+ static HYPHEN = 10;
+ static HYPHEN_HYPHEN = 11;
+ static HYPHEN_LT = 12;
+ static EQUAL = 13;
+ static COLON = 14;
+ static LT = 15;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.level = 0;
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightHaskell.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.epsilon(HighlightHaskell.WORD);
+ } else if (c == '-') {
+ this.state = HighlightHaskell.HYPHEN;
+ } else if (c == '{') {
+ this.state = HighlightHaskell.CURL;
+ } else if (c == '"') {
+ this.state = HighlightHaskell.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '`') {
+ this.state = HighlightHaskell.TICK;
+ this.push("span", "operator");
+ this.append(c);
+ } else if (c == '!' || //
+ c == '#' || //
+ c == '$' || //
+ c == '*' || //
+ c == ',' || //
+ c == '>' || //
+ c == '?' || //
+ c == '@' || //
+ c == '|' || //
+ c == '~') {
+ this.push("span", "operator");
+ this.append(c);
+ this.pop();
+ } else if (c == '=') {
+ this.state = HighlightHaskell.EQUAL;
+ } else if (c == ':') {
+ this.state = HighlightHaskell.COLON;
+ } else if (c == '<') {
+ this.state = HighlightHaskell.LT;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightHaskell.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (HASKELL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.LT:
+ if (c == '-') {
+ this.push("span", "operator");
+ this.append("<-");
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else {
+ this.append('<');
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.COLON:
+ if (c == ':') {
+ this.push("span", "operator");
+ this.append("::");
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else {
+ this.append(':');
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.EQUAL:
+ if (c == '>') {
+ this.push("span", "operator");
+ this.append("=>");
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else {
+ this.push("span", "operator");
+ this.append('=');
+ this.pop();
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.HYPHEN:
+ if (c == '-') {
+ this.push("span", "comment");
+ this.append("--");
+ this.state = HighlightHaskell.HYPHEN_HYPHEN;
+ } else if (c == '<') {
+ this.state = HighlightHaskell.HYPHEN_LT;
+ } else if (c == '>') {
+ this.push("span", "operator");
+ this.append("->");
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else {
+ this.append('-');
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.HYPHEN_LT:
+ if (c == '<') {
+ this.push("span", "operator");
+ this.append("-<<");
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else {
+ this.push("span", "operator");
+ this.append("-<");
+ this.pop();
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.HYPHEN_HYPHEN:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ }
+ break;
+
+ case HighlightHaskell.CURL:
+ if (c == '-') {
+ this.push("span", "comment");
+ this.append("{-");
+ this.state = HighlightHaskell.CURL_HYPHEN;
+ this.level = 1;
+ } else {
+ this.append('{');
+ this.epsilon(HighlightHaskell.NORMAL);
+ }
+ break;
+
+ case HighlightHaskell.CURL_HYPHEN:
+ this.append(c);
+ if (c == '-') {
+ this.state = HighlightHaskell.CURL_HYPHEN_HYPHEN;
+ } else if (c == '{') {
+ this.state = HighlightHaskell.CURL_HYPHEN_CURL;
+ }
+ break;
+
+ case HighlightHaskell.CURL_HYPHEN_CURL:
+ this.append(c);
+ if (c == '-') {
+ this.state = HighlightHaskell.CURL_HYPHEN;
+ ++this.level;
+ } else if (c != '{') {
+ this.state = HighlightHaskell.CURL_HYPHEN;
+ }
+ break;
+
+ case HighlightHaskell.CURL_HYPHEN_HYPHEN:
+ this.append(c);
+ if (c == '}') {
+ if (!--this.level) {
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ }
+ } else if (c != '-') {
+ this.state = HighlightHaskell.CURL_HYPHEN;
+ }
+ break;
+
+ case HighlightHaskell.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightHaskell.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightHaskell.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightHaskell.DQUOTE;
+ break;
+
+ case HighlightHaskell.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightHaskell.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightHaskell.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightHaskell.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightHaskell.TICK;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightHaskell.WORD:
+ if (HASKELL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightHaskell.CURL:
+ this.append('{');
+ break;
+ case HighlightHaskell.HYPHEN:
+ this.append('-');
+ break;
+ case HighlightHaskell.EQUAL:
+ this.append('=');
+ break;
+ case HighlightHaskell.COLON:
+ this.append(':');
+ break;
+ case HighlightHaskell.LT:
+ this.append('<');
+ break;
+ case HighlightHaskell.HYPHEN_LT:
+ this.push("span", "operator");
+ this.append("-<");
+ this.pop();
+ break;
+ case HighlightHaskell.TICK:
+ case HighlightHaskell.TICK_BACKSLASH:
+ case HighlightHaskell.DQUOTE:
+ case HighlightHaskell.DQUOTE_BACKSLASH:
+ case HighlightHaskell.HYPHEN_HYPHEN:
+ case HighlightHaskell.CURL_HYPHEN:
+ case HighlightHaskell.CURL_HYPHEN_HYPHEN:
+ case HighlightHaskell.CURL_HYPHEN_CURL:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightHaskell.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_html.js b/llamafile/server/www/highlight_html.js
index 178f05b0..da6a150f 100644
--- a/llamafile/server/www/highlight_html.js
+++ b/llamafile/server/www/highlight_html.js
@@ -14,17 +14,338 @@
class HighlightHtml extends Highlighter {
+ static NORMAL = 0;
+ static TAG = 1;
+ static TAG2 = 2;
+ static TAG_EXCLAIM = 3;
+ static TAG_EXCLAIM_HYPHEN = 4;
+ static KEY = 5;
+ static VAL = 6;
+ static QUOTE = 7;
+ static DQUOTE = 8;
+ static COMMENT = 9;
+ static COMMENT_HYPHEN = 10;
+ static COMMENT_HYPHEN_HYPHEN = 11;
+ static RELAY = 12;
+ static TAG_QUESTION = 13;
+ static TAG_QUESTION_P = 14;
+ static TAG_QUESTION_P_H = 15;
+ static ENTITY = 16;
+
constructor(delegate) {
super(delegate);
+ this.i = 0;
+ this.name = '';
+ this.closer = '';
+ this.pending = '';
+ this.highlighter = null;
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightHtml.NORMAL:
+ if (c == '<') {
+ this.state = HighlightHtml.TAG;
+ this.name = '';
+ } else if (c == '&') {
+ this.state = HighlightHtml.ENTITY;
+ this.push("span", "entity");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightHtml.ENTITY:
+ this.append(c);
+ if (c == ';') {
+ this.pop();
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ case HighlightHtml.TAG:
+ if (c == '!') {
+ this.state = HighlightHtml.TAG_EXCLAIM;
+ } else if (c == '?') {
+ this.state = HighlightHtml.TAG_QUESTION;
+ } else if (c == '>' || isspace(c)) {
+ this.append('<');
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ } else {
+ this.append('<');
+ this.push("span", "tag");
+ this.append(c);
+ this.name += c.toLowerCase();
+ this.state = HighlightHtml.TAG2;
+ }
+ break;
+
+ case HighlightHtml.TAG2:
+ if (c == '>') {
+ this.pop();
+ this.append(c);
+ this.ontag();
+ } else if (isspace(c)) {
+ this.append(c);
+ this.state = HighlightHtml.KEY;
+ this.pop();
+ this.push("span", "attrib");
+ } else {
+ this.append(c);
+ this.name += c.toLowerCase();
+ }
+ break;
+
+ case HighlightHtml.TAG_EXCLAIM:
+ if (c == '-') {
+ this.state = HighlightHtml.TAG_EXCLAIM_HYPHEN;
+ } else {
+ this.append("<!");
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ case HighlightHtml.TAG_EXCLAIM_HYPHEN:
+ if (c == '-') {
+ this.push("span", "comment");
+ this.append("<!--");
+ this.state = HighlightHtml.COMMENT;
+ } else {
+ this.append("<!-");
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ case HighlightHtml.COMMENT:
+ this.append(c);
+ if (c == '-')
+ this.state = HighlightHtml.COMMENT_HYPHEN;
+ break;
+
+ case HighlightHtml.COMMENT_HYPHEN:
+ this.append(c);
+ if (c == '-') {
+ this.state = HighlightHtml.COMMENT_HYPHEN_HYPHEN;
+ } else {
+ this.state = HighlightHtml.COMMENT;
+ }
+ break;
+
+ case HighlightHtml.COMMENT_HYPHEN_HYPHEN:
+ this.append(c);
+ if (c == '>') {
+ this.pop();
+ this.state = HighlightHtml.NORMAL;
+ } else if (c != '-') {
+ this.state = HighlightHtml.COMMENT;
+ }
+ break;
+
+ case HighlightHtml.KEY:
+ if (c == '=') {
+ this.pop();
+ this.append(c);
+ this.state = HighlightHtml.VAL;
+ } else if (c == '>') {
+ this.pop();
+ this.append(c);
+ this.ontag();
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightHtml.VAL:
+ if (isspace(c)) {
+ this.append(c);
+ this.state = HighlightHtml.KEY;
+ this.push("span", "attrib");
+ } else if (c == '\'') {
+ this.push("span", "string");
+ this.append(c);
+ this.state = HighlightHtml.QUOTE;
+ } else if (c == '"') {
+ this.push("span", "string");
+ this.append(c);
+ this.state = HighlightHtml.DQUOTE;
+ } else if (c == '>') {
+ this.append(c);
+ this.ontag();
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightHtml.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightHtml.VAL;
+ }
+ break;
+
+ case HighlightHtml.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightHtml.VAL;
+ }
+ break;
+
+ case HighlightHtml.RELAY:
+ if (this.closer[this.i] == c.toLowerCase()) {
+ this.pending += c;
+ if (++this.i == this.closer.length) {
+ this.highlighter.flush();
+ delete this.highlighter;
+ this.highlighter = null;
+ this.pop();
+ if (this.closer == "</style>") {
+ this.pending = "</style>";
+ } else if (this.closer == "</script>") {
+ this.pending = "</script>";
+ }
+ this.append(this.pending);
+ this.state = HighlightHtml.NORMAL;
+ this.i = 0;
+ }
+ } else {
+ this.pending += c;
+ this.highlighter.feed(this.pending);
+ this.pending = '';
+ this.i = 0;
+ }
+ break;
+
+ case HighlightHtml.TAG_QUESTION:
+ if (c == 'p') {
+ this.state = HighlightHtml.TAG_QUESTION_P;
+ } else if (c == '=') {
+ this.push("span", "tag");
+ this.append("<?=");
+ this.pop();
+ this.pending = '';
+ this.closer = "?>";
+ let hdom = new HighlightDom(this.push('div', ''));
+ this.highlighter = new HighlightPhp(hdom);
+ this.state = HighlightHtml.RELAY;
+ this.i = 0;
+ } else {
+ this.append("<?");
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ case HighlightHtml.TAG_QUESTION_P:
+ if (c == 'h') {
+ this.state = HighlightHtml.TAG_QUESTION_P_H;
+ } else {
+ this.append("<?p");
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ case HighlightHtml.TAG_QUESTION_P_H:
+ if (c == 'p') {
+ this.append("<");
+ this.push("span", "tag");
+ this.append("?php");
+ this.pop();
+ this.pending = '';
+ this.closer = "?>";
+ let hdom = new HighlightDom(this.push('div', ''));
+ this.highlighter = new HighlightPhp(hdom);
+ this.state = HighlightHtml.RELAY;
+ this.i = 0;
+ } else {
+ this.append("<?ph");
+ this.append(c);
+ this.state = HighlightHtml.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightHtml.TAG:
+ this.append('<');
+ break;
+ case HighlightHtml.TAG_EXCLAIM:
+ this.append("<!");
+ break;
+ case HighlightHtml.TAG_EXCLAIM_HYPHEN:
+ this.append("<!-");
+ break;
+ case HighlightHtml.TAG_QUESTION:
+ this.append("<?");
+ break;
+ case HighlightHtml.TAG_QUESTION_P:
+ this.append("<?p");
+ break;
+ case HighlightHtml.TAG_QUESTION_P_H:
+ this.append("<?ph");
+ break;
+ case HighlightHtml.COMMENT_HYPHEN_HYPHEN:
+ case HighlightHtml.COMMENT_HYPHEN:
+ case HighlightHtml.COMMENT:
+ case HighlightHtml.ENTITY:
+ case HighlightHtml.DQUOTE:
+ case HighlightHtml.QUOTE:
+ case HighlightHtml.TAG2:
+ case HighlightHtml.KEY:
+ this.pop();
+ break;
+ case HighlightHtml.RELAY:
+ this.highlighter.feed(this.pending);
+ this.highlighter.flush();
+ delete this.highlighter;
+ this.highlighter = null;
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.pending = '';
+ this.closer = '';
+ this.name = '';
+ this.state = HighlightHtml.NORMAL;
this.delegate.flush();
}
+
+ ontag() {
+ this.state = HighlightHtml.NORMAL;
+ if (this.name == "script") {
+ this.pending = '';
+ this.closer = "</script>";
+ let hdom = new HighlightDom(this.push('div', ''));
+ this.highlighter = new HighlightJs(hdom);
+ this.state = HighlightHtml.RELAY;
+ this.i = 0;
+ } else if (this.name == "style") {
+ this.pending = '';
+ this.closer = "</style>";
+ let hdom = new HighlightDom(this.push('div', ''));
+ this.highlighter = new HighlightCss(hdom);
+ this.state = HighlightHtml.RELAY;
+ this.i = 0;
+ }
+ this.delta = 1;
+ }
}
Highlighter.REGISTRY['html'] = HighlightHtml;
diff --git a/llamafile/server/www/highlight_java.js b/llamafile/server/www/highlight_java.js
index a025d17a..5189e0ad 100644
--- a/llamafile/server/www/highlight_java.js
+++ b/llamafile/server/www/highlight_java.js
@@ -73,16 +73,277 @@ const JAVA_CONSTANTS = new Set([
class HighlightJava extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static ANNOTATION = 4;
+ static ANNOTATION2 = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static DQUOTE = 10;
+ static DQUOTESTR = 11;
+ static DQUOTESTR_BACKSLASH = 12;
+ static DQUOTE2 = 13;
+ static DQUOTE3 = 14;
+ static DQUOTE3_BACKSLASH = 15;
+ static DQUOTE31 = 16;
+ static DQUOTE32 = 17;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightJava.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightJava.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightJava.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightJava.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightJava.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '@') {
+ this.state = HighlightJava.ANNOTATION;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightJava.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (JAVA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightJava.NORMAL);
+ }
+ break;
+
+ case HighlightJava.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightJava.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightJava.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightJava.NORMAL);
+ }
+ break;
+
+ case HighlightJava.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightJava.NORMAL;
+ }
+ break;
+
+ case HighlightJava.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightJava.SLASH_STAR_STAR;
+ break;
+
+ case HighlightJava.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightJava.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightJava.SLASH_STAR;
+ }
+ break;
+
+ case HighlightJava.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightJava.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJava.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightJava.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJava.QUOTE;
+ break;
+
+ // handle "string"
+ case HighlightJava.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJava.DQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightJava.DQUOTESTR_BACKSLASH;
+ } else {
+ this.state = HighlightJava.DQUOTESTR;
+ }
+ break;
+
+ case HighlightJava.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightJava.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJava.DQUOTESTR_BACKSLASH;
+ }
+ break;
+
+ case HighlightJava.DQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJava.DQUOTESTR;
+ break;
+
+ // handle """string""" from java 15+
+ case HighlightJava.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightJava.DQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightJava.NORMAL);
+ }
+ break;
+
+ case HighlightJava.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJava.DQUOTE31;
+ } else if (c == '\\') {
+ this.state = HighlightJava.DQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightJava.DQUOTE31:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJava.DQUOTE32;
+ } else if (c == '\\') {
+ this.state = HighlightJava.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightJava.DQUOTE3;
+ }
+ break;
+
+ case HighlightJava.DQUOTE32:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightJava.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJava.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightJava.DQUOTE3;
+ }
+ break;
+
+ case HighlightJava.DQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJava.DQUOTE3;
+ break;
+
+ case HighlightJava.ANNOTATION:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "attrib");
+ this.append('@');
+ this.append(c);
+ this.state = HighlightJava.ANNOTATION2;
+ } else {
+ this.append('@');
+ this.epsilon(HighlightJava.NORMAL);
+ }
+ break;
+
+ case HighlightJava.ANNOTATION2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightJava.NORMAL);
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightJava.WORD:
+ if (JAVA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightJava.SLASH:
+ this.append('/');
+ break;
+ case HighlightJava.ANNOTATION:
+ this.append('@');
+ break;
+ case HighlightJava.QUOTE:
+ case HighlightJava.QUOTE_BACKSLASH:
+ case HighlightJava.SLASH_SLASH:
+ case HighlightJava.SLASH_STAR:
+ case HighlightJava.SLASH_STAR_STAR:
+ case HighlightJava.DQUOTE:
+ case HighlightJava.DQUOTESTR:
+ case HighlightJava.DQUOTESTR_BACKSLASH:
+ case HighlightJava.DQUOTE2:
+ case HighlightJava.DQUOTE3:
+ case HighlightJava.DQUOTE3_BACKSLASH:
+ case HighlightJava.DQUOTE31:
+ case HighlightJava.DQUOTE32:
+ case HighlightJava.ANNOTATION2:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightJava.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_js.js b/llamafile/server/www/highlight_js.js
index 00c74557..28d5d6ce 100644
--- a/llamafile/server/www/highlight_js.js
+++ b/llamafile/server/www/highlight_js.js
@@ -150,16 +150,337 @@ const JS_CONSTANTS = new Set([
class HighlightJs extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static TICK = 10;
+ static TICK_BACKSLASH = 11;
+ static TICK_DOLLAR = 12;
+ static REGEX = 13;
+ static REGEX_BACKSLASH = 14;
+ static REGEX_SQUARE = 15;
+ static REGEX_SQUARE_BACKSLASH = 16;
+
+ static EXPECT_VALUE = 0;
+ static EXPECT_OPERATOR = 1;
+
+ // https://262.ecma-international.org/12.0/#sec-line-terminators
+ static is_line_terminator(c) {
+ switch (c) {
+ case '\r':
+ case '\n':
+ case '\u2028': // LINE SEPARATOR
+ case '\u2029': // PARAGRAPH SEPARATOR
+ return true;
+ default:
+ return false;
+ }
+ }
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.expect = HighlightJs.EXPECT_VALUE;
+ this.nest = [];
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ if (c == '\r')
+ continue;
+ if (c == 0xFEFF)
+ continue; // utf-8 bom
+ switch (this.state) {
+
+ case HighlightJs.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightJs.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightJs.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightJs.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ } else if (c == '"') {
+ this.state = HighlightJs.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ } else if (c == '`') {
+ this.state = HighlightJs.TICK;
+ this.push("span", "string");
+ this.append('`');
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ } else if (c == '{' && this.nest) {
+ this.expect = HighlightJs.EXPECT_VALUE;
+ this.append('{');
+ this.nest.push(HighlightJs.NORMAL);
+ } else if (c == '}' && this.nest) {
+ if ((this.state = this.nest.pop()) != HighlightJs.NORMAL)
+ this.push("span", "string");
+ this.append('}');
+ } else if (c == ')' || c == '}' || c == ']') {
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ this.append(c);
+ } else if (isdigit(c) || c == '.') {
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ this.append(c);
+ } else if (ispunct(c)) {
+ this.expect = HighlightJs.EXPECT_VALUE;
+ this.append(c);
+ } else if (isdigit(c)) {
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightJs.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (JS_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightJs.EXPECT_VALUE;
+ } else if (JS_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ } else if (JS_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ } else {
+ this.append(this.word);
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ }
+ this.word = '';
+ this.epsilon(HighlightJs.NORMAL);
+ }
+ break;
+
+ case HighlightJs.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightJs.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightJs.SLASH_STAR;
+ } else if (this.expect == HighlightJs.EXPECT_VALUE) {
+ this.expect = HighlightJs.EXPECT_OPERATOR;
+ this.push("span", "string");
+ this.append('/');
+ this.append(c);
+ if (c == '\\') {
+ this.state = HighlightJs.REGEX_BACKSLASH;
+ } else if (c == '[') {
+ this.state = HighlightJs.REGEX_SQUARE;
+ } else {
+ this.state = HighlightJs.REGEX;
+ }
+ } else {
+ this.append('/');
+ this.epsilon(HighlightJs.NORMAL);
+ }
+ break;
+
+ case HighlightJs.SLASH_SLASH:
+ this.append(c);
+ if (HighlightJs.is_line_terminator(c)) {
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ }
+ break;
+
+ case HighlightJs.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightJs.SLASH_STAR_STAR;
+ break;
+
+ case HighlightJs.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightJs.SLASH_STAR;
+ }
+ break;
+
+ case HighlightJs.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJs.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightJs.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJs.QUOTE;
+ break;
+
+ case HighlightJs.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJs.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightJs.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJs.DQUOTE;
+ break;
+
+ case HighlightJs.TICK:
+ if (c == '`') {
+ this.append('`');
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ } else if (c == '$') {
+ this.state = HighlightJs.TICK_DOLLAR;
+ } else if (c == '\\') {
+ this.append('\\');
+ this.state = HighlightJs.TICK_BACKSLASH;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightJs.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJs.TICK;
+ break;
+
+ case HighlightJs.TICK_DOLLAR:
+ if (c == '{') {
+ this.push("span", "bold");
+ this.append('$');
+ this.pop();
+ this.append('{');
+ this.pop();
+ this.expect = HighlightJs.EXPECT_VALUE;
+ this.nest.push(HighlightJs.TICK);
+ this.state = HighlightJs.NORMAL;
+ } else {
+ this.push("span", "warning");
+ this.append('$');
+ this.pop();
+ this.epsilon(HighlightJs.TICK);
+ }
+ break;
+
+ case HighlightJs.REGEX:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightJs.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJs.REGEX_BACKSLASH;
+ } else if (c == '[') {
+ this.state = HighlightJs.REGEX_SQUARE;
+ }
+ break;
+
+ case HighlightJs.REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJs.REGEX;
+ break;
+
+ case HighlightJs.REGEX_SQUARE:
+ // because /[/]/g is valid code
+ this.append(c);
+ if (c == '\\') {
+ this.state = HighlightJs.REGEX_SQUARE_BACKSLASH;
+ } else if (c == ']') {
+ this.state = HighlightJs.REGEX;
+ }
+ break;
+
+ case HighlightJs.REGEX_SQUARE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJs.REGEX_SQUARE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightJs.WORD:
+ if (JS_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JS_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (JS_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightJs.SLASH:
+ this.append('/');
+ break;
+ case HighlightJs.TICK_DOLLAR:
+ this.append('$');
+ this.pop();
+ break;
+ case HighlightJs.TICK:
+ case HighlightJs.TICK_BACKSLASH:
+ case HighlightJs.QUOTE:
+ case HighlightJs.QUOTE_BACKSLASH:
+ case HighlightJs.DQUOTE:
+ case HighlightJs.DQUOTE_BACKSLASH:
+ case HighlightJs.SLASH_SLASH:
+ case HighlightJs.SLASH_STAR:
+ case HighlightJs.SLASH_STAR_STAR:
+ case HighlightJs.REGEX:
+ case HighlightJs.REGEX_BACKSLASH:
+ case HighlightJs.REGEX_SQUARE:
+ case HighlightJs.REGEX_SQUARE_BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.nest = [];
+ this.state = HighlightJs.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_julia.js b/llamafile/server/www/highlight_julia.js
index 242370b2..c74ea2e1 100644
--- a/llamafile/server/www/highlight_julia.js
+++ b/llamafile/server/www/highlight_julia.js
@@ -46,16 +46,231 @@ const JULIA_KEYWORDS = new Set([
class HighlightJulia extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static QUOTE = 3;
+ static QUOTE_BACKSLASH = 4;
+ static ANNOTATION = 5;
+ static ANNOTATION2 = 6;
+ static DQUOTE = 7;
+ static DQUOTE_VAR = 8;
+ static DQUOTESTR = 9;
+ static DQUOTESTR_BACKSLASH = 10;
+ static DQUOTE2 = 11;
+ static DQUOTE3 = 12;
+ static DQUOTE3_BACKSLASH = 13;
+ static DQUOTE31 = 14;
+ static DQUOTE32 = 15;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightJulia.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightJulia.WORD;
+ this.word += c;
+ } else if (c == '#') {
+ this.state = HighlightJulia.COMMENT;
+ this.push("span", "comment");
+ this.append('#');
+ } else if (c == '\'') {
+ this.state = HighlightJulia.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightJulia.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '@') {
+ this.state = HighlightJulia.ANNOTATION;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightJulia.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (JULIA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightJulia.NORMAL);
+ }
+ break;
+
+ case HighlightJulia.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightJulia.NORMAL;
+ }
+ break;
+
+ case HighlightJulia.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightJulia.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightJulia.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJulia.QUOTE;
+ break;
+
+ case HighlightJulia.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJulia.DQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.DQUOTESTR_BACKSLASH;
+ } else {
+ this.state = HighlightJulia.DQUOTESTR;
+ }
+ break;
+
+ case HighlightJulia.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightJulia.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.DQUOTESTR_BACKSLASH;
+ }
+ break;
+
+ case HighlightJulia.DQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJulia.DQUOTESTR;
+ break;
+
+ case HighlightJulia.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightJulia.DQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightJulia.NORMAL);
+ }
+ break;
+
+ case HighlightJulia.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJulia.DQUOTE31;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.DQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightJulia.DQUOTE31:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightJulia.DQUOTE32;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightJulia.DQUOTE3;
+ }
+ break;
+
+ case HighlightJulia.DQUOTE32:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightJulia.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightJulia.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightJulia.DQUOTE3;
+ }
+ break;
+
+ case HighlightJulia.DQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightJulia.DQUOTE3;
+ break;
+
+ case HighlightJulia.ANNOTATION:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "attrib");
+ this.append('@');
+ this.append(c);
+ this.state = HighlightJulia.ANNOTATION2;
+ } else {
+ this.append('@');
+ this.epsilon(HighlightJulia.NORMAL);
+ }
+ break;
+
+ case HighlightJulia.ANNOTATION2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightJulia.NORMAL);
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightJulia.WORD:
+ if (JULIA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightJulia.ANNOTATION:
+ this.append('@');
+ break;
+ case HighlightJulia.COMMENT:
+ case HighlightJulia.QUOTE:
+ case HighlightJulia.QUOTE_BACKSLASH:
+ case HighlightJulia.DQUOTE:
+ case HighlightJulia.DQUOTESTR:
+ case HighlightJulia.DQUOTESTR_BACKSLASH:
+ case HighlightJulia.DQUOTE2:
+ case HighlightJulia.DQUOTE3:
+ case HighlightJulia.DQUOTE3_BACKSLASH:
+ case HighlightJulia.DQUOTE31:
+ case HighlightJulia.DQUOTE32:
+ case HighlightJulia.ANNOTATION2:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightJulia.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_kotlin.js b/llamafile/server/www/highlight_kotlin.js
index c0f0dae5..32cc6f3f 100644
--- a/llamafile/server/www/highlight_kotlin.js
+++ b/llamafile/server/www/highlight_kotlin.js
@@ -89,16 +89,361 @@ const KOTLIN_KEYWORDS = new Set([
class HighlightKotlin extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static ANNOTATION = 4;
+ static ANNOTATION2 = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static DQUOTE = 10;
+ static DQUOTE_DOLLAR = 11;
+ static DQUOTE_VAR = 12;
+ static DQUOTESTR = 13;
+ static DQUOTESTR_BACKSLASH = 14;
+ static DQUOTESTR_DOLLAR = 15;
+ static DQUOTESTR_VAR = 16;
+ static DQUOTE2 = 17;
+ static DQUOTE3 = 18;
+ static DQUOTE3_BACKSLASH = 19;
+ static DQUOTE3_DOLLAR = 20;
+ static DQUOTE3_VAR = 21;
+ static DQUOTE31 = 22;
+ static DQUOTE32 = 23;
+
constructor(delegate) {
super(delegate);
+ this.nest = [];
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightKotlin.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightKotlin.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightKotlin.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightKotlin.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightKotlin.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '@') {
+ this.state = HighlightKotlin.ANNOTATION;
+ } else if (c == '{' && this.nest) {
+ this.nest.push(HighlightKotlin.NORMAL);
+ this.append('{');
+ } else if (c == '}' && this.nest) {
+ if ((this.state = this.nest.pop()) != HighlightKotlin.NORMAL)
+ this.push("span", "string");
+ this.append('}');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightKotlin.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (KOTLIN_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && this.word[0] == this.word[0].toUpperCase()) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightKotlin.NORMAL);
+ }
+ break;
+
+ case HighlightKotlin.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightKotlin.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightKotlin.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightKotlin.NORMAL);
+ }
+ break;
+
+ case HighlightKotlin.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightKotlin.NORMAL;
+ }
+ break;
+
+ case HighlightKotlin.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightKotlin.SLASH_STAR_STAR;
+ break;
+
+ case HighlightKotlin.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightKotlin.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightKotlin.SLASH_STAR;
+ }
+ break;
+
+ case HighlightKotlin.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightKotlin.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightKotlin.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightKotlin.QUOTE;
+ break;
+
+ case HighlightKotlin.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightKotlin.DQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.DQUOTESTR_BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightKotlin.DQUOTESTR_DOLLAR;
+ } else {
+ this.state = HighlightKotlin.DQUOTESTR;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightKotlin.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.DQUOTESTR_BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightKotlin.DQUOTESTR_DOLLAR;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightKotlin.DQUOTESTR;
+ break;
+
+ case HighlightKotlin.DQUOTESTR_DOLLAR:
+ if (c == '{') {
+ this.append(c);
+ this.pop();
+ this.nest.push(HighlightKotlin.DQUOTESTR);
+ this.state = HighlightKotlin.NORMAL;
+ } else if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "bold");
+ this.append(c);
+ this.state = HighlightKotlin.DQUOTESTR_VAR;
+ } else {
+ this.epsilon(HighlightKotlin.DQUOTESTR);
+ }
+ break;
+
+ case HighlightKotlin.DQUOTESTR_VAR:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightKotlin.DQUOTESTR);
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightKotlin.DQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightKotlin.NORMAL);
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightKotlin.DQUOTE31;
+ } else if (c == '$') {
+ this.state = HighlightKotlin.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.DQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE31:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightKotlin.DQUOTE32;
+ } else if (c == '$') {
+ this.state = HighlightKotlin.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightKotlin.DQUOTE3;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE32:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightKotlin.NORMAL;
+ } else if (c == '$') {
+ this.state = HighlightKotlin.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightKotlin.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightKotlin.DQUOTE3;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightKotlin.DQUOTE3;
+ break;
+
+ case HighlightKotlin.DQUOTE3_DOLLAR:
+ if (c == '{') {
+ this.append(c);
+ this.pop();
+ this.nest.push(HighlightKotlin.DQUOTE3);
+ this.state = HighlightKotlin.NORMAL;
+ } else if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "bold");
+ this.append(c);
+ this.state = HighlightKotlin.DQUOTE3_VAR;
+ } else {
+ this.append(c);
+ this.state = HighlightKotlin.DQUOTE3;
+ }
+ break;
+
+ case HighlightKotlin.DQUOTE3_VAR:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightKotlin.DQUOTE3);
+ }
+ break;
+
+ case HighlightKotlin.ANNOTATION:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "attrib");
+ this.append('@');
+ this.append(c);
+ this.state = HighlightKotlin.ANNOTATION2;
+ } else {
+ this.append('@');
+ this.epsilon(HighlightKotlin.NORMAL);
+ }
+ break;
+
+ case HighlightKotlin.ANNOTATION2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightKotlin.NORMAL);
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightKotlin.WORD:
+ if (KOTLIN_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && isupper(this.word[0])) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightKotlin.SLASH:
+ this.append('/');
+ break;
+ case HighlightKotlin.ANNOTATION:
+ this.append('@');
+ break;
+ case HighlightKotlin.QUOTE:
+ case HighlightKotlin.QUOTE_BACKSLASH:
+ case HighlightKotlin.SLASH_SLASH:
+ case HighlightKotlin.SLASH_STAR:
+ case HighlightKotlin.SLASH_STAR_STAR:
+ case HighlightKotlin.DQUOTE:
+ case HighlightKotlin.DQUOTESTR:
+ case HighlightKotlin.DQUOTESTR_BACKSLASH:
+ case HighlightKotlin.DQUOTESTR_DOLLAR:
+ case HighlightKotlin.DQUOTESTR_VAR:
+ case HighlightKotlin.DQUOTE2:
+ case HighlightKotlin.DQUOTE3:
+ case HighlightKotlin.DQUOTE3_BACKSLASH:
+ case HighlightKotlin.DQUOTE31:
+ case HighlightKotlin.DQUOTE32:
+ case HighlightKotlin.ANNOTATION2:
+ case HighlightKotlin.DQUOTE3_DOLLAR:
+ case HighlightKotlin.DQUOTE3_VAR:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.nest = [];
+ this.state = HighlightKotlin.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_ld.js b/llamafile/server/www/highlight_ld.js
index f9c2d4eb..81a8d632 100644
--- a/llamafile/server/www/highlight_ld.js
+++ b/llamafile/server/www/highlight_ld.js
@@ -122,16 +122,234 @@ const LD_WARNINGS = new Set([
class HighlightLd extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static DQUOTE = 2;
+ static DQUOTE_BACKSLASH = 3;
+ static SLASH = 4;
+ static SLASH_SLASH = 5;
+ static SLASH_STAR = 6;
+ static SLASH_STAR_STAR = 7;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.is_bol = true;
+ this.is_cpp = false;
+ this.is_cpp_builtin = false;
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightLd.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '.' || c == ':') {
+ this.state = HighlightLd.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightLd.SLASH;
+ } else if (c == '"') {
+ this.state = HighlightLd.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '#' && this.is_bol) {
+ this.is_cpp = true;
+ this.push("span", "builtin");
+ this.append(c);
+ } else if (c == '\n') {
+ this.append(c);
+ if (this.is_cpp) {
+ if (this.is_cpp_builtin) {
+ this.pop();
+ }
+ this.is_cpp = false;
+ }
+ this.is_cpp_builtin = false;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightLd.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '.' || c == '/' || c == ':') {
+ this.word += c;
+ } else {
+ if (this.is_cpp) {
+ if (CPP_KEYWORDS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ if (this.is_cpp_builtin) {
+ this.pop();
+ this.is_cpp_builtin = false;
+ }
+ } else if (C_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ } else {
+ if (LD_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (LD_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (LD_WARNINGS.has(this.word)) {
+ this.push("span", "warning");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ }
+ this.word = '';
+ this.epsilon(HighlightLd.NORMAL);
+ }
+ break;
+
+ case HighlightLd.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightLd.SLASH_SLASH;
+ } else if (c == 'D') {
+ // for /DISCARD/ warning keyword
+ this.word += "/D";
+ this.state = HighlightLd.WORD;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightLd.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightLd.NORMAL);
+ }
+ break;
+
+ case HighlightLd.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightLd.NORMAL;
+ this.is_cpp = false;
+ }
+ break;
+
+ case HighlightLd.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightLd.SLASH_STAR_STAR;
+ break;
+
+ case HighlightLd.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightLd.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightLd.SLASH_STAR;
+ }
+ break;
+
+ case HighlightLd.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightLd.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightLd.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightLd.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightLd.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ if (this.is_bol) {
+ if (!isspace(c))
+ this.is_bol = false;
+ } else {
+ if (c == '\n')
+ this.is_bol = true;
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightLd.WORD:
+ if (this.is_cpp) {
+ if (CPP_KEYWORDS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (C_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ this.pop();
+ }
+ } else {
+ if (LD_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (LD_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (LD_WARNINGS.has(this.word)) {
+ this.push("span", "warning");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ }
+ this.word = '';
+ break;
+ case HighlightLd.SLASH:
+ this.append('/');
+ if (this.is_cpp)
+ this.pop();
+ break;
+ case HighlightLd.DQUOTE:
+ case HighlightLd.DQUOTE_BACKSLASH:
+ case HighlightLd.SLASH_SLASH:
+ case HighlightLd.SLASH_STAR:
+ case HighlightLd.SLASH_STAR_STAR:
+ this.pop();
+ break;
+ default:
+ if (this.is_cpp)
+ this.pop();
+ break;
+ }
+ if (this.is_cpp) {
+ if (this.is_cpp_builtin) {
+ this.pop();
+ }
+ this.is_cpp = false;
+ }
+ this.is_cpp_builtin = false;
+ this.is_bol = true;
+ this.state = HighlightLd.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_lisp.js b/llamafile/server/www/highlight_lisp.js
index 731f4054..8fff4265 100644
--- a/llamafile/server/www/highlight_lisp.js
+++ b/llamafile/server/www/highlight_lisp.js
@@ -26,6 +26,7 @@ const LISP_KEYWORDS = new Set([
'ctypecase',
'declaim',
'declare',
+ 'def',
'defadvice',
'defalias',
'defconst',
@@ -74,6 +75,7 @@ const LISP_KEYWORDS = new Set([
'eval-when',
'flet',
'flet*',
+ 'fn',
'go',
'handler-bind',
'handler-case',
@@ -85,9 +87,12 @@ const LISP_KEYWORDS = new Set([
'let',
'let*',
'letf',
+ 'letfn',
'locally',
'loop',
'macrolet',
+ 'monitor-enter',
+ 'monitor-exit',
'multiple-value-bind',
'multiple-value-prog1',
'proclaim',
@@ -97,16 +102,22 @@ const LISP_KEYWORDS = new Set([
'prog2',
'progn',
'progv',
+ 'quote',
+ 'recur',
'restart-bind',
'restart-case',
'return',
'return-from',
+ 'set!',
'symbol-macrolet',
'tagbody',
'the',
+ 'throw',
+ 'try',
'typecase',
'unless',
'unwind-protect',
+ 'var',
'when',
'while',
'with-accessors',
@@ -125,16 +136,142 @@ const LISP_KEYWORDS = new Set([
class HighlightLisp extends Highlighter {
+ static NORMAL = 0;
+ static SYMBOL = 1;
+ static DQUOTE = 2;
+ static DQUOTE_BACKSLASH = 3;
+ static COMMENT = 4;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.is_first = false;
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightLisp.NORMAL:
+ if (c == '(') {
+ this.append(c);
+ this.is_first = true;
+ } else if (c == ';') {
+ this.push("span", "comment");
+ this.append(c);
+ this.state = HighlightLisp.COMMENT;
+ } else if (c == '[') {
+ this.append(c);
+ this.state = HighlightLisp.SYMBOL;
+ this.is_first = false;
+ } else if (c == ')' || c == ']') {
+ this.append(c);
+ this.is_first = false;
+ } else if (c == '\'' || c == '#' || c == '`' || c == ',') {
+ this.append(c);
+ this.is_first = false;
+ } else if (c == '"') {
+ this.push("span", "string");
+ this.append(c);
+ this.state = HighlightLisp.DQUOTE;
+ this.is_first = false;
+ } else if (isspace(c)) {
+ this.append(c);
+ } else {
+ this.word += c;
+ this.state = HighlightLisp.SYMBOL;
+ }
+ break;
+
+ case HighlightLisp.SYMBOL:
+ if (isspace(c) || //
+ c == '(' || //
+ c == ')' || //
+ c == '[' || //
+ c == ']' || //
+ c == ',' || //
+ c == '#' || //
+ c == '`' || //
+ c == '"' || //
+ c == '\'') {
+ if (this.is_first && LISP_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word.length > 1 && this.word[0] == ':') {
+ this.push("span", "lispkw");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.is_first = false;
+ this.word = '';
+ this.epsilon(HighlightLisp.NORMAL);
+ } else {
+ this.word += c;
+ }
+ break;
+
+ case HighlightLisp.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightLisp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightLisp.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightLisp.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightLisp.DQUOTE;
+ break;
+
+ case HighlightLisp.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightLisp.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightLisp.SYMBOL:
+ if (this.is_first && LISP_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word.length > 1 && this.word[0] == ':') {
+ this.push("span", "lispkw");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightLisp.DQUOTE:
+ case HighlightLisp.DQUOTE_BACKSLASH:
+ case HighlightLisp.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.is_first = false;
+ this.state = HighlightLisp.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_lua.js b/llamafile/server/www/highlight_lua.js
index bf9b974b..7768c8ee 100644
--- a/llamafile/server/www/highlight_lua.js
+++ b/llamafile/server/www/highlight_lua.js
@@ -107,16 +107,244 @@ const LUA_CONSTANTS = new Set([
class HighlightLua extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static HYPHEN = 6;
+ static HYPHEN_HYPHEN = 7;
+ static HYPHEN_HYPHEN_LSB = 8;
+ static COMMENT = 9;
+ static TICK = 10;
+ static LSB = 11;
+ static LITERAL = 12;
+ static LITERAL_RSB = 13;
+
constructor(delegate) {
super(delegate);
+ this.level1 = 0;
+ this.level2 = 0;
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightLua.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightLua.WORD;
+ this.word += c;
+ } else if (c == '-') {
+ this.state = HighlightLua.HYPHEN;
+ } else if (c == '\'') {
+ this.state = HighlightLua.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightLua.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '[') {
+ this.state = HighlightLua.LSB;
+ this.level1 = 0;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightLua.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (LUA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (LUA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (LUA_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightLua.NORMAL);
+ }
+ break;
+
+ case HighlightLua.HYPHEN:
+ if (c == '-') {
+ this.push("span", "comment");
+ this.append("--");
+ this.state = HighlightLua.HYPHEN_HYPHEN;
+ } else {
+ this.append('-');
+ this.epsilon(HighlightLua.NORMAL);
+ }
+ break;
+
+ case HighlightLua.HYPHEN_HYPHEN:
+ if (c == '[') {
+ this.append('[');
+ this.state = HighlightLua.HYPHEN_HYPHEN_LSB;
+ this.level1 = 0;
+ } else {
+ this.epsilon(HighlightLua.COMMENT);
+ }
+ break;
+
+ case HighlightLua.HYPHEN_HYPHEN_LSB:
+ if (c == '=') {
+ this.append('=');
+ ++this.level1;
+ } else if (c == '[') {
+ this.append('[');
+ this.state = HighlightLua.LITERAL;
+ } else {
+ this.epsilon(HighlightLua.COMMENT);
+ }
+ break;
+
+ case HighlightLua.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightLua.NORMAL;
+ }
+ break;
+
+ case HighlightLua.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightLua.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightLua.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightLua.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightLua.QUOTE;
+ break;
+
+ case HighlightLua.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightLua.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightLua.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightLua.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightLua.DQUOTE;
+ break;
+
+ case HighlightLua.LSB:
+ if (c == '=') {
+ ++this.level1;
+ } else if (c == '[') {
+ this.push("span", "string");
+ this.append('[');
+ for (let i = 0; i < this.level1; ++i)
+ this.append('=');
+ this.append('[');
+ this.state = HighlightLua.LITERAL;
+ } else {
+ this.append('[');
+ for (let i = 0; i < this.level1; ++i)
+ this.append('=');
+ this.epsilon(HighlightLua.NORMAL);
+ }
+ break;
+
+ case HighlightLua.LITERAL:
+ this.append(c);
+ if (c == ']') {
+ this.state = HighlightLua.LITERAL_RSB;
+ this.level2 = 0;
+ }
+ break;
+
+ case HighlightLua.LITERAL_RSB:
+ this.append(c);
+ if (c == '=') {
+ ++this.level2;
+ } else if (c == ']') {
+ if (this.level2 == this.level1) {
+ this.pop();
+ this.state = HighlightLua.NORMAL;
+ } else {
+ this.level2 = 0;
+ }
+ } else {
+ this.state = HighlightLua.LITERAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightLua.WORD:
+ if (LUA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (LUA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (LUA_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightLua.LSB:
+ this.append('[');
+ for (let i = 0; i < this.level1; ++i)
+ this.append('=');
+ break;
+ case HighlightLua.HYPHEN:
+ this.append('-');
+ break;
+ case HighlightLua.QUOTE:
+ case HighlightLua.QUOTE_BACKSLASH:
+ case HighlightLua.DQUOTE:
+ case HighlightLua.DQUOTE_BACKSLASH:
+ case HighlightLua.COMMENT:
+ case HighlightLua.LITERAL:
+ case HighlightLua.LITERAL_RSB:
+ case HighlightLua.HYPHEN_HYPHEN:
+ case HighlightLua.HYPHEN_HYPHEN_LSB:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightLua.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_m4.js b/llamafile/server/www/highlight_m4.js
index b0074e74..3c424e19 100644
--- a/llamafile/server/www/highlight_m4.js
+++ b/llamafile/server/www/highlight_m4.js
@@ -183,16 +183,109 @@ const M4_KEYWORDS = new Set([
class HighlightM4 extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static DOLLAR = 3;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightM4.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightM4.WORD;
+ this.word += c;
+ } else if (c == '#') {
+ this.state = HighlightM4.COMMENT;
+ this.push("span", "comment");
+ this.append('#');
+ } else if (c == '$') {
+ this.state = HighlightM4.DOLLAR;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightM4.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (M4_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word == "dnl" || this.word == "m4_dnl") {
+ this.push("span", "comment");
+ this.append(this.word);
+ this.word = '';
+ this.epsilon(HighlightM4.COMMENT);
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightM4.NORMAL);
+ }
+ break;
+
+ case HighlightM4.DOLLAR:
+ if (isdigit(c) || c == '*' || c == '#' || c == '@') {
+ this.append('$');
+ this.push("span", "var");
+ this.append(c);
+ this.pop();
+ this.state = HighlightM4.NORMAL;
+ } else {
+ this.append('$');
+ this.epsilon(HighlightM4.NORMAL);
+ }
+ break;
+
+ case HighlightM4.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightM4.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightM4.WORD:
+ if (M4_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightM4.DOLLAR:
+ this.append('$');
+ break;
+ case HighlightM4.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightM4.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_make.js b/llamafile/server/www/highlight_make.js
index 7e8fa505..01747b01 100644
--- a/llamafile/server/www/highlight_make.js
+++ b/llamafile/server/www/highlight_make.js
@@ -72,18 +72,203 @@ const MAKE_BUILTINS = new Set([
'words',
]);
+function is_automatic_variable(c) {
+ switch (c) {
+ case '@':
+ case '%':
+ case '<':
+ case '?':
+ case '^':
+ case '+':
+ case '|':
+ case '*':
+ return true;
+ default:
+ return false;
+ }
+}
+
class HighlightMake extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static COMMENT_BACKSLASH = 3;
+ static DOLLAR = 4;
+ static DOLLAR2 = 5;
+ static VARIABLE = 6;
+ static BACKSLASH = 7;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightMake.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '-' || c == '.') {
+ this.state = HighlightMake.WORD;
+ this.word += c;
+ break;
+ } else if (c == '#') {
+ this.state = HighlightMake.COMMENT;
+ this.push("span", "comment");
+ this.append('#');
+ } else if (c == '$') {
+ this.state = HighlightMake.DOLLAR;
+ this.append('$');
+ } else if (c == '\\') {
+ this.state = HighlightMake.BACKSLASH;
+ this.push("span", "escape");
+ this.append('\\');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightMake.BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightMake.NORMAL;
+ break;
+
+ case HighlightMake.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '-' || c == '.') {
+ this.word += c;
+ } else {
+ if (MAKE_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightMake.NORMAL);
+ }
+ break;
+
+ case HighlightMake.DOLLAR:
+ if (isdigit(c) || is_automatic_variable(c)) {
+ this.push("span", "var");
+ this.append(c);
+ this.pop();
+ this.state = HighlightMake.NORMAL;
+ } else if (c == '$') {
+ this.state = HighlightMake.DOLLAR2;
+ this.append('$');
+ } else if (c == '(') {
+ this.state = HighlightMake.VARIABLE;
+ this.append('(');
+ } else {
+ this.epsilon(HighlightMake.NORMAL);
+ }
+ break;
+
+ case HighlightMake.DOLLAR2:
+ if (c == '(') {
+ this.state = HighlightMake.VARIABLE;
+ this.append('(');
+ } else {
+ this.epsilon(HighlightMake.NORMAL);
+ }
+ break;
+
+ case HighlightMake.VARIABLE:
+ if (isalnum(c) || //
+ c == '%' || //
+ c == '*' || //
+ c == '+' || //
+ c == '-' || //
+ c == '.' || //
+ c == '<' || //
+ c == '?' || //
+ c == '@' || //
+ c == '_') {
+ this.word += c;
+ } else if (c == '$' && this.word.empty()) {
+ this.state = HighlightMake.DOLLAR;
+ this.append('$');
+ } else if (c == ')' || //
+ c == ':') {
+ this.push("span", "var");
+ this.append(this.word);
+ this.pop();
+ this.word = '';
+ this.append(c);
+ this.state = HighlightMake.NORMAL;
+ } else {
+ if (MAKE_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightMake.NORMAL);
+ }
+ break;
+
+ case HighlightMake.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightMake.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightMake.COMMENT_BACKSLASH;
+ }
+ break;
+
+ case HighlightMake.COMMENT_BACKSLASH:
+ this.append(c);
+ this.state = HighlightMake.COMMENT;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightMake.WORD:
+ if (MAKE_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightMake.VARIABLE:
+ if (MAKE_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightMake.COMMENT:
+ case HighlightMake.COMMENT_BACKSLASH:
+ case HighlightMake.BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightMake.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_markdown.js b/llamafile/server/www/highlight_markdown.js
index 6160c265..c0682f80 100644
--- a/llamafile/server/www/highlight_markdown.js
+++ b/llamafile/server/www/highlight_markdown.js
@@ -34,6 +34,7 @@ class HighlightMarkdown extends Highlighter {
static LANG2 = 17;
static NEWLINE = 18;
static EAT_NEWLINE = 19;
+ static INCODE2_TICK2 = 20;
constructor(delegate) {
super(delegate);
@@ -60,6 +61,7 @@ class HighlightMarkdown extends Highlighter {
} else if (c == '\\') {
// handle \*\*not bold\*\* etc.
this.state = HighlightMarkdown.BACKSLASH;
+ this.bol = false;
} else if (c == '\n') {
this.bol = true;
this.tail = false;
@@ -111,6 +113,7 @@ class HighlightMarkdown extends Highlighter {
if (c == '\\')
this.state = HighlightMarkdown.EMPHASIS_BACKSLASH;
}
+ this.bol = false;
break;
case HighlightMarkdown.EMPHASIS:
@@ -166,12 +169,18 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.TICK:
if (c == '`') {
- this.state = HighlightMarkdown.TICK_TICK;
+ if (this.bol) {
+ this.state = HighlightMarkdown.TICK_TICK;
+ } else {
+ this.push("span", "incode");
+ this.state = HighlightMarkdown.INCODE2;
+ }
} else {
this.push('code', '');
this.append(c);
this.state = HighlightMarkdown.INCODE;
}
+ this.bol = false;
break;
case HighlightMarkdown.INCODE:
@@ -197,14 +206,23 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.INCODE2_TICK:
if (c == '`') {
- this.pop();
- this.state = HighlightMarkdown.NORMAL;
+ this.state = HighlightMarkdown.INCODE2_TICK2;
} else {
+ this.append('`');
this.append(c);
this.state = HighlightMarkdown.INCODE2;
}
break;
+ case HighlightMarkdown.INCODE2_TICK2:
+ if (c == '`') {
+ this.append('`');
+ } else {
+ this.pop();
+ this.epsilon(HighlightMarkdown.NORMAL);
+ }
+ break;
+
case HighlightMarkdown.TICK_TICK:
if (c == '`') {
this.state = HighlightMarkdown.LANG;
@@ -227,7 +245,6 @@ class HighlightMarkdown extends Highlighter {
if (c == "\n") {
this.flush();
let hdom = new HighlightDom(this.push('pre', ''));
- console.log(this.lang);
if (!(this.highlighter = Highlighter.create(this.lang, hdom)))
this.highlighter = Highlighter.create('txt', hdom);
this.state = HighlightMarkdown.CODE;
@@ -293,6 +310,7 @@ class HighlightMarkdown extends Highlighter {
case HighlightMarkdown.INCODE:
case HighlightMarkdown.INCODE2:
case HighlightMarkdown.INCODE2_TICK:
+ case HighlightMarkdown.INCODE2_TICK2:
case HighlightMarkdown.STRONG:
case HighlightMarkdown.STRONG_BACKSLASH:
case HighlightMarkdown.STRONG_STAR:
@@ -325,6 +343,7 @@ class HighlightMarkdown extends Highlighter {
this.tail = false;
this.newlines = 0;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_matlab.js b/llamafile/server/www/highlight_matlab.js
index 024142bb..6a27e3ac 100644
--- a/llamafile/server/www/highlight_matlab.js
+++ b/llamafile/server/www/highlight_matlab.js
@@ -516,16 +516,147 @@ const MATLAB_CONSTANTS = new Set([
class HighlightMatlab extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static QUOTE = 3;
+ static QUOTE_BACKSLASH = 4;
+ static DQUOTE = 5;
+ static DQUOTE_BACKSLASH = 6;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightMatlab.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightMatlab.WORD;
+ this.word += c;
+ } else if (c == '%') {
+ this.state = HighlightMatlab.COMMENT;
+ this.push("span", "comment");
+ this.append('%');
+ } else if (c == '\'') {
+ this.state = HighlightMatlab.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightMatlab.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightMatlab.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (MATLAB_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (MATLAB_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (MATLAB_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightMatlab.NORMAL);
+ }
+ break;
+
+ case HighlightMatlab.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightMatlab.NORMAL;
+ }
+ break;
+
+ case HighlightMatlab.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightMatlab.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightMatlab.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightMatlab.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightMatlab.QUOTE;
+ break;
+
+ case HighlightMatlab.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightMatlab.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightMatlab.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightMatlab.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightMatlab.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightMatlab.WORD:
+ if (MATLAB_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (MATLAB_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (MATLAB_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightMatlab.QUOTE:
+ case HighlightMatlab.QUOTE_BACKSLASH:
+ case HighlightMatlab.DQUOTE:
+ case HighlightMatlab.DQUOTE_BACKSLASH:
+ case HighlightMatlab.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightMatlab.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_ocaml.js b/llamafile/server/www/highlight_ocaml.js
index 672f0ea0..f37559ea 100644
--- a/llamafile/server/www/highlight_ocaml.js
+++ b/llamafile/server/www/highlight_ocaml.js
@@ -101,16 +101,255 @@ const OCAML_CONSTANTS = new Set([
class HighlightOcaml extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static LPAREN = 6;
+ static COMMENT = 7;
+ static COMMENT_STAR = 8;
+ static COMMENT_LPAREN = 9;
+ static LCURLY = 10;
+ static RAWSTR = 11;
+ static RAWSTR_PIPE = 12;
+
constructor(delegate) {
super(delegate);
+ this.nest = 0;
+ this.word = '';
+ this.word2 = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+
+ switch (this.state) {
+
+ case HighlightOcaml.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '~') {
+ this.epsilon(HighlightOcaml.WORD);
+ } else if (c == '(') {
+ this.state = HighlightOcaml.LPAREN;
+ } else if (c == '\'') {
+ this.state = HighlightOcaml.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightOcaml.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '{') {
+ this.state = HighlightOcaml.LCURLY;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightOcaml.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '\'' || c == '~') {
+ this.word += c;
+ } else {
+ if (OCAML_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (OCAML_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (OCAML_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word.length > 1 && this.word[0] == '~') {
+ this.push("span", "property");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightOcaml.NORMAL);
+ }
+ break;
+
+ case HighlightOcaml.LPAREN:
+ if (c == '*') {
+ this.push("span", "comment");
+ this.append("(*");
+ this.state = HighlightOcaml.COMMENT;
+ this.nest = 1;
+ } else {
+ this.append('(');
+ this.epsilon(HighlightOcaml.NORMAL);
+ }
+ break;
+
+ case HighlightOcaml.COMMENT:
+ this.append(c);
+ if (c == '*') {
+ this.state = HighlightOcaml.COMMENT_STAR;
+ } else if (c == '(') {
+ this.state = HighlightOcaml.COMMENT_LPAREN;
+ }
+ break;
+
+ case HighlightOcaml.COMMENT_STAR:
+ this.append(c);
+ if (c == ')') {
+ if (!--this.nest) {
+ this.pop();
+ this.state = HighlightOcaml.NORMAL;
+ }
+ } else if (c == '(') {
+ this.state = HighlightOcaml.COMMENT_LPAREN;
+ } else if (c != '*') {
+ this.state = HighlightOcaml.COMMENT;
+ }
+ break;
+
+ case HighlightOcaml.COMMENT_LPAREN:
+ this.append(c);
+ if (c == '*') {
+ ++this.nest;
+ this.state = HighlightOcaml.COMMENT;
+ } else if (c != '(') {
+ this.state = HighlightOcaml.COMMENT;
+ }
+ break;
+
+ case HighlightOcaml.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightOcaml.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightOcaml.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightOcaml.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightOcaml.QUOTE;
+ break;
+
+ case HighlightOcaml.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightOcaml.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightOcaml.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightOcaml.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightOcaml.DQUOTE;
+ break;
+
+ case HighlightOcaml.LCURLY:
+ if (c == '|') {
+ this.push("span", "string");
+ this.append('{');
+ this.append(this.word);
+ this.append('|');
+ this.state = HighlightOcaml.RAWSTR;
+ } else if (isalpha(c) || c == '_') {
+ this.word += c;
+ } else {
+ this.append('{');
+ if (this.word.empty()) {
+ this.epsilon(HighlightOcaml.NORMAL);
+ } else {
+ this.epsilon(HighlightOcaml.WORD);
+ }
+ }
+ break;
+
+ case HighlightOcaml.RAWSTR:
+ this.append(c);
+ if (c == '|') {
+ this.state = HighlightOcaml.RAWSTR_PIPE;
+ }
+ break;
+
+ case HighlightOcaml.RAWSTR_PIPE:
+ this.append(c);
+ if (c == '}' && this.word2 == this.word) {
+ this.pop();
+ this.word2 = '';
+ this.word = '';
+ this.state = HighlightOcaml.NORMAL;
+ } else if (c == '|') {
+ this.word2 = '';
+ } else if (isalpha(c) || c == '_') {
+ this.word2 += c;
+ } else {
+ this.word2 = '';
+ this.state = HighlightOcaml.RAWSTR;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightOcaml.WORD:
+ if (OCAML_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (OCAML_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (OCAML_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightOcaml.LPAREN:
+ this.append('(');
+ break;
+ case HighlightOcaml.LCURLY:
+ this.append('{');
+ this.append(this.word);
+ this.word = '';
+ break;
+ case HighlightOcaml.RAWSTR:
+ case HighlightOcaml.RAWSTR_PIPE:
+ this.word = '';
+ this.word2 = '';
+ this.pop();
+ break;
+ case HighlightOcaml.QUOTE:
+ case HighlightOcaml.QUOTE_BACKSLASH:
+ case HighlightOcaml.DQUOTE:
+ case HighlightOcaml.DQUOTE_BACKSLASH:
+ case HighlightOcaml.COMMENT:
+ case HighlightOcaml.COMMENT_STAR:
+ case HighlightOcaml.COMMENT_LPAREN:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightOcaml.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_pascal.js b/llamafile/server/www/highlight_pascal.js
index b29e32cc..7c384aee 100644
--- a/llamafile/server/www/highlight_pascal.js
+++ b/llamafile/server/www/highlight_pascal.js
@@ -718,16 +718,193 @@ const PASCAL_TYPES = new Set([
class HighlightPascal extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static DQUOTE = 3;
+ static SLASH = 4;
+ static SLASH_SLASH = 5;
+ static CURLY = 6;
+ static PAREN = 7;
+ static PAREN_STAR = 8;
+ static PAREN_STAR_STAR = 9;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+
+ switch (this.state) {
+
+ case HighlightPascal.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.epsilon(HighlightPascal.WORD);
+ } else if (c == '/') {
+ this.state = HighlightPascal.SLASH;
+ } else if (c == '(') {
+ this.state = HighlightPascal.PAREN;
+ } else if (c == '\'') {
+ this.state = HighlightPascal.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightPascal.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '{') {
+ this.state = HighlightPascal.CURLY;
+ this.push("span", "comment");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightPascal.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (PASCAL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PASCAL_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (PASCAL_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightPascal.NORMAL);
+ }
+ break;
+
+ case HighlightPascal.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightPascal.SLASH_SLASH;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightPascal.NORMAL);
+ }
+ break;
+
+ case HighlightPascal.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightPascal.NORMAL;
+ }
+ break;
+
+ case HighlightPascal.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightPascal.NORMAL;
+ }
+ break;
+
+ case HighlightPascal.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightPascal.NORMAL;
+ }
+ break;
+
+ case HighlightPascal.CURLY:
+ this.append(c);
+ if (c == '}') {
+ this.pop();
+ this.state = HighlightPascal.NORMAL;
+ }
+ break;
+
+ case HighlightPascal.PAREN:
+ if (c == '*') {
+ this.push("span", "comment");
+ this.append("(*");
+ this.state = HighlightPascal.PAREN_STAR;
+ } else {
+ this.append('(');
+ this.epsilon(HighlightPascal.NORMAL);
+ }
+ break;
+
+ case HighlightPascal.PAREN_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightPascal.PAREN_STAR_STAR;
+ break;
+
+ case HighlightPascal.PAREN_STAR_STAR:
+ this.append(c);
+ if (c == ')') {
+ this.pop();
+ this.state = HighlightPascal.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightPascal.PAREN_STAR;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightPascal.WORD:
+ if (PASCAL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PASCAL_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (PASCAL_BUILTINS.has(this.word.toLowerCase())) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightPascal.SLASH:
+ this.append('/');
+ break;
+ case HighlightPascal.PAREN:
+ this.append('(');
+ break;
+ case HighlightPascal.QUOTE:
+ case HighlightPascal.DQUOTE:
+ case HighlightPascal.SLASH_SLASH:
+ case HighlightPascal.CURLY:
+ case HighlightPascal.PAREN_STAR:
+ case HighlightPascal.PAREN_STAR_STAR:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightPascal.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_perl.js b/llamafile/server/www/highlight_perl.js
index 6099d1bc..f72ea54d 100644
--- a/llamafile/server/www/highlight_perl.js
+++ b/llamafile/server/www/highlight_perl.js
@@ -53,16 +53,528 @@ const PERL_KEYWORDS = new Set([
class HighlightPerl extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static TICK = 6;
+ static TICK_BACKSLASH = 7;
+ static VAR = 8;
+ static VAR2 = 9;
+ static COMMENT = 10;
+ static LT = 11;
+ static LT_LT = 12;
+ static LT_LT_NAME = 13;
+ static LT_LT_QNAME = 14;
+ static HEREDOC_BOL = 15;
+ static HEREDOC = 16;
+ static REGEX = 17;
+ static REGEX_BACKSLASH = 18;
+ static S_REGEX = 19;
+ static S_REGEX_BACKSLASH = 20;
+ static S_REGEX_S = 21;
+ static S_REGEX_S_BACKSLASH = 22;
+ static EQUAL = 23;
+ static BACKSLASH = 24;
+
+ static EXPECT_VALUE = 0;
+ static EXPECT_OPERATOR = 1;
+
+ static mirror(c) {
+ switch (c) {
+ case '(':
+ return ')';
+ case '{':
+ return '}';
+ case '[':
+ return ']';
+ case '<':
+ return '>';
+ default:
+ return c;
+ }
+ }
+
+ static is_magic_var(c) {
+ switch (c) {
+ case '!':
+ case '"':
+ case '#':
+ case '&':
+ case '-':
+ case '/':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case '\'':
+ case '\\':
+ case '^':
+ case '_':
+ case '`':
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ static is_regex_punct(c) {
+ switch (c) {
+ case '!':
+ case '"':
+ case '#':
+ case '%':
+ case '&':
+ case '(':
+ case '*':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '@':
+ case '[':
+ case '\'':
+ case '^':
+ case '`':
+ case '{':
+ case '|':
+ case '~':
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ static is_regex_prefix(s) {
+ return s == "m" ||
+ s == "s" ||
+ s == "y" ||
+ s == "q" ||
+ s == "tr" ||
+ s == "qq" ||
+ s == "qw" ||
+ s == "qx" ||
+ s == "qr";
+ }
+
+ static is_double_regex(s) {
+ return s == "s" ||
+ s == "y" ||
+ s == "tr";
+ }
+
constructor(delegate) {
super(delegate);
+ this.i = 0;
+ this.c = 0;
+ this.last = 0;
+ this.expect = 0;
+ this.opener = 0;
+ this.closer = 0;
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ this.word = '';
+ this.heredoc = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ this.last = this.c;
+ this.c = c;
+ switch (this.state) {
+
+ case HighlightPerl.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightPerl.WORD;
+ this.word += c;
+ } else if (c == '\'') {
+ this.state = HighlightPerl.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else if (c == '"') {
+ this.state = HighlightPerl.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else if (c == '=' && (!this.last || this.last == '\n')) {
+ this.state = HighlightPerl.EQUAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.BACKSLASH;
+ this.push("span", "escape");
+ this.append('\\');
+ } else if (c == '`') {
+ this.state = HighlightPerl.TICK;
+ this.push("span", "string");
+ this.append('`');
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else if (c == '$') {
+ this.append('$');
+ this.state = HighlightPerl.VAR;
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else if (c == '@' || c == '%') {
+ this.append(c);
+ this.push("span", "var");
+ this.state = HighlightPerl.VAR2;
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else if (c == '#') {
+ this.push("span", "comment");
+ this.append('#');
+ this.state = HighlightPerl.COMMENT;
+ } else if (c == '<') {
+ this.append('<');
+ this.state = HighlightPerl.LT;
+ this.expect = HighlightPerl.EXPECT_VALUE;
+ } else if (c == '/' && this.expect == HighlightPerl.EXPECT_VALUE && this.last != '/') {
+ this.opener = '/';
+ this.closer = '/';
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ this.push("span", "string");
+ this.append(c);
+ this.state = HighlightPerl.REGEX;
+ } else if (c == '\n') {
+ this.append('\n');
+ if (this.pending_heredoc) {
+ this.push("span", "string");
+ this.pending_heredoc = false;
+ this.state = HighlightPerl.HEREDOC_BOL;
+ this.i = 0;
+ }
+ } else if (c == ')' || c == '}' || c == ']') {
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ this.append(c);
+ } else if (ispunct(c)) {
+ this.expect = HighlightPerl.EXPECT_VALUE;
+ this.append(c);
+ } else if (isdigit(c) || c == '.') {
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightPerl.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (PERL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ if (this.word == "shift") {
+ this.expect = HighlightPerl.EXPECT_OPERATOR;
+ } else {
+ this.expect = HighlightPerl.EXPECT_VALUE;
+ }
+ } else {
+ this.append(this.word);
+ this.expect = HighlightPerl.EXPECT_VALUE;
+ if (HighlightPerl.is_regex_punct(c) &&
+ HighlightPerl.is_regex_prefix(this.word)) {
+ this.opener = c;
+ this.closer = HighlightPerl.mirror(c);
+ this.push("span", "string");
+ this.append(c);
+ if (HighlightPerl.is_double_regex(this.word)) {
+ this.state = HighlightPerl.S_REGEX;
+ } else {
+ this.state = HighlightPerl.REGEX;
+ }
+ this.word = '';
+ break;
+ }
+ }
+ this.word = '';
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ break;
+
+ case HighlightPerl.VAR:
+ if (isdigit(c) || HighlightPerl.is_magic_var(c)) {
+ this.push("span", "var");
+ this.append(c);
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ break;
+ } else if (c == '{') {
+ this.state = HighlightPerl.VAR2;
+ this.append(c);
+ this.push("span", "var");
+ break;
+ } else {
+ this.push("span", "var");
+ this.state = HighlightPerl.VAR2;
+ }
+ // fallthrough
+
+ case HighlightPerl.VAR2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ }
+ break;
+
+ case HighlightPerl.REGEX:
+ this.append(c);
+ if (c == this.closer) {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.REGEX_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.REGEX;
+ break;
+
+ case HighlightPerl.S_REGEX:
+ this.append(c);
+ if (c == this.opener) {
+ this.state = HighlightPerl.S_REGEX_S;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.S_REGEX_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.S_REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.S_REGEX;
+ break;
+
+ case HighlightPerl.S_REGEX_S:
+ this.append(c);
+ if (c == this.closer) {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.S_REGEX_S_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.S_REGEX_S_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.S_REGEX_S;
+ break;
+
+ case HighlightPerl.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.QUOTE;
+ break;
+
+ case HighlightPerl.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.DQUOTE;
+ break;
+
+ case HighlightPerl.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightPerl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPerl.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightPerl.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPerl.TICK;
+ break;
+
+ case HighlightPerl.EQUAL:
+ if (isalpha(c)) {
+ this.push("span", "comment");
+ this.append('=');
+ this.append(c);
+ this.heredoc = "=cut";
+ this.state = HighlightPerl.HEREDOC;
+ this.i = 0;
+ } else {
+ this.append('=');
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.LT:
+ if (c == '<') {
+ this.append(c);
+ this.state = HighlightPerl.LT_LT;
+ this.heredoc = '';
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ } else {
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.LT_LT:
+ if (c == '-') {
+ this.indented_heredoc = true;
+ this.append(c);
+ } else if (c == '"' || c == '\'') {
+ this.closer = c;
+ this.state = HighlightPerl.LT_LT_QNAME;
+ this.push("span", "string");
+ this.append(c);
+ } else if (isalpha(c) || c == '_') {
+ this.state = HighlightPerl.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else if (isascii(c) && isblank(c)) {
+ this.append(c);
+ } else {
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.LT_LT_NAME:
+ if (isalnum(c) || c == '_') {
+ this.state = HighlightPerl.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else if (c == '\n') {
+ this.append(c);
+ this.push("span", "string");
+ this.state = HighlightPerl.HEREDOC_BOL;
+ } else {
+ this.pending_heredoc = true;
+ this.epsilon(HighlightPerl.NORMAL);
+ }
+ break;
+
+ case HighlightPerl.LT_LT_QNAME:
+ this.append(c);
+ if (c == this.closer) {
+ this.pop();
+ this.state = HighlightPerl.HEREDOC_BOL;
+ this.pending_heredoc = true;
+ this.state = HighlightPerl.NORMAL;
+ } else {
+ this.heredoc += c;
+ }
+ break;
+
+ case HighlightPerl.HEREDOC_BOL:
+ this.append(c);
+ if (c == '\n') {
+ if (this.i == this.heredoc.length) {
+ this.state = HighlightPerl.NORMAL;
+ this.pop();
+ }
+ this.i = 0;
+ } else if (c == '\t' && this.indented_heredoc) {
+ // do nothing
+ } else if (this.i < this.heredoc.length && this.heredoc[this.i] == c) {
+ this.i++;
+ } else {
+ this.state = HighlightPerl.HEREDOC;
+ this.i = 0;
+ }
+ break;
+
+ case HighlightPerl.HEREDOC:
+ this.append(c);
+ if (c == '\n') {
+ this.state = HighlightPerl.HEREDOC_BOL;
+ this.i = 0;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightPerl.WORD:
+ if (PERL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightPerl.EQUAL:
+ this.append('=');
+ break;
+ case HighlightPerl.VAR2:
+ case HighlightPerl.TICK:
+ case HighlightPerl.TICK_BACKSLASH:
+ case HighlightPerl.QUOTE:
+ case HighlightPerl.QUOTE_BACKSLASH:
+ case HighlightPerl.DQUOTE:
+ case HighlightPerl.DQUOTE_BACKSLASH:
+ case HighlightPerl.COMMENT:
+ case HighlightPerl.HEREDOC_BOL:
+ case HighlightPerl.HEREDOC:
+ case HighlightPerl.LT_LT_QNAME:
+ case HighlightPerl.REGEX:
+ case HighlightPerl.REGEX_BACKSLASH:
+ case HighlightPerl.S_REGEX:
+ case HighlightPerl.S_REGEX_BACKSLASH:
+ case HighlightPerl.S_REGEX_S:
+ case HighlightPerl.S_REGEX_S_BACKSLASH:
+ case HighlightPerl.BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightPerl.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_php.js b/llamafile/server/www/highlight_php.js
index 63e1f984..c19e9c59 100644
--- a/llamafile/server/www/highlight_php.js
+++ b/llamafile/server/www/highlight_php.js
@@ -100,16 +100,214 @@ const PHP_CONSTANTS = new Set([
class HighlightPhp extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static TICK = 10;
+ static TICK_BACKSLASH = 11;
+ static VAR = 12;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightPhp.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightPhp.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightPhp.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightPhp.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightPhp.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '`') {
+ this.state = HighlightPhp.TICK;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '$') {
+ this.append(c);
+ this.state = HighlightPhp.VAR;
+ this.push("span", "var");
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightPhp.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (PHP_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PHP_CONSTANTS.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightPhp.NORMAL);
+ }
+ break;
+
+ case HighlightPhp.VAR:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightPhp.NORMAL);
+ }
+ break;
+
+ case HighlightPhp.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightPhp.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightPhp.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightPhp.NORMAL);
+ }
+ break;
+
+ case HighlightPhp.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightPhp.NORMAL;
+ }
+ break;
+
+ case HighlightPhp.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightPhp.SLASH_STAR_STAR;
+ break;
+
+ case HighlightPhp.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightPhp.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightPhp.SLASH_STAR;
+ }
+ break;
+
+ case HighlightPhp.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightPhp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPhp.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightPhp.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPhp.QUOTE;
+ break;
+
+ case HighlightPhp.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightPhp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPhp.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightPhp.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPhp.DQUOTE;
+ break;
+
+ case HighlightPhp.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightPhp.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPhp.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightPhp.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPhp.TICK;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightPhp.WORD:
+ if (PHP_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PHP_CONSTANTS.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightPhp.SLASH:
+ this.append('/');
+ break;
+ case HighlightPhp.VAR:
+ case HighlightPhp.TICK:
+ case HighlightPhp.TICK_BACKSLASH:
+ case HighlightPhp.QUOTE:
+ case HighlightPhp.QUOTE_BACKSLASH:
+ case HighlightPhp.DQUOTE:
+ case HighlightPhp.DQUOTE_BACKSLASH:
+ case HighlightPhp.SLASH_SLASH:
+ case HighlightPhp.SLASH_STAR:
+ case HighlightPhp.SLASH_STAR_STAR:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightPhp.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_python.js b/llamafile/server/www/highlight_python.js
index 13087d3c..50091d17 100644
--- a/llamafile/server/www/highlight_python.js
+++ b/llamafile/server/www/highlight_python.js
@@ -55,16 +55,278 @@ const PYTHON_CONSTANTS = new Set([
class HighlightPython extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COM = 2;
+ static SQUOTE = 3;
+ static SQUOTESTR = 4;
+ static SQUOTESTR_BACKSLASH = 5;
+ static SQUOTE2 = 6;
+ static SQUOTE3 = 7;
+ static SQUOTE3_BACKSLASH = 8;
+ static SQUOTE31 = 9;
+ static SQUOTE32 = 10;
+ static DQUOTE = 11;
+ static DQUOTESTR = 12;
+ static DQUOTESTR_BACKSLASH = 13;
+ static DQUOTE2 = 14;
+ static DQUOTE3 = 15;
+ static DQUOTE3_BACKSLASH = 16;
+ static DQUOTE31 = 17;
+ static DQUOTE32 = 18;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightPython.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.epsilon(HighlightPython.WORD);
+ } else if (c == '#') {
+ this.state = HighlightPython.COM;
+ this.push("span", "comment");
+ this.append(c);
+ } else if (c == '\'') {
+ this.state = HighlightPython.SQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightPython.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightPython.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (PYTHON_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PYTHON_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightPython.NORMAL);
+ }
+ break;
+
+ case HighlightPython.COM:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightPython.NORMAL;
+ }
+ break;
+
+ case HighlightPython.SQUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.state = HighlightPython.SQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightPython.SQUOTESTR_BACKSLASH;
+ } else {
+ this.state = HighlightPython.SQUOTESTR;
+ }
+ break;
+
+ case HighlightPython.SQUOTESTR:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightPython.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPython.SQUOTESTR_BACKSLASH;
+ }
+ break;
+
+ case HighlightPython.SQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPython.SQUOTESTR;
+ break;
+
+ case HighlightPython.SQUOTE2:
+ if (c == '\'') {
+ this.append(c);
+ this.state = HighlightPython.SQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightPython.NORMAL);
+ }
+ break;
+
+ case HighlightPython.SQUOTE3:
+ this.append(c);
+ if (c == '\'') {
+ this.state = HighlightPython.SQUOTE31;
+ } else if (c == '\\') {
+ this.state = HighlightPython.SQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightPython.SQUOTE31:
+ this.append(c);
+ if (c == '\'') {
+ this.state = HighlightPython.SQUOTE32;
+ } else if (c == '\\') {
+ this.state = HighlightPython.SQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightPython.SQUOTE3;
+ }
+ break;
+
+ case HighlightPython.SQUOTE32:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightPython.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPython.SQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightPython.SQUOTE3;
+ }
+ break;
+
+ case HighlightPython.SQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPython.SQUOTE3;
+ break;
+
+ case HighlightPython.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightPython.DQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightPython.DQUOTESTR_BACKSLASH;
+ } else {
+ this.state = HighlightPython.DQUOTESTR;
+ }
+ break;
+
+ case HighlightPython.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightPython.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPython.DQUOTESTR_BACKSLASH;
+ }
+ break;
+
+ case HighlightPython.DQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPython.DQUOTESTR;
+ break;
+
+ case HighlightPython.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightPython.DQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightPython.NORMAL);
+ }
+ break;
+
+ case HighlightPython.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightPython.DQUOTE31;
+ } else if (c == '\\') {
+ this.state = HighlightPython.DQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightPython.DQUOTE31:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightPython.DQUOTE32;
+ } else if (c == '\\') {
+ this.state = HighlightPython.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightPython.DQUOTE3;
+ }
+ break;
+
+ case HighlightPython.DQUOTE32:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightPython.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightPython.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightPython.DQUOTE3;
+ }
+ break;
+
+ case HighlightPython.DQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightPython.DQUOTE3;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightPython.WORD:
+ if (PYTHON_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (PYTHON_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightPython.COM:
+ case HighlightPython.SQUOTE:
+ case HighlightPython.SQUOTESTR:
+ case HighlightPython.SQUOTESTR_BACKSLASH:
+ case HighlightPython.SQUOTE2:
+ case HighlightPython.SQUOTE3:
+ case HighlightPython.SQUOTE3_BACKSLASH:
+ case HighlightPython.SQUOTE31:
+ case HighlightPython.SQUOTE32:
+ case HighlightPython.DQUOTE:
+ case HighlightPython.DQUOTESTR:
+ case HighlightPython.DQUOTESTR_BACKSLASH:
+ case HighlightPython.DQUOTE2:
+ case HighlightPython.DQUOTE3:
+ case HighlightPython.DQUOTE3_BACKSLASH:
+ case HighlightPython.DQUOTE31:
+ case HighlightPython.DQUOTE32:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightPython.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_r.js b/llamafile/server/www/highlight_r.js
index 0e8aebcb..44d84ced 100644
--- a/llamafile/server/www/highlight_r.js
+++ b/llamafile/server/www/highlight_r.js
@@ -195,16 +195,238 @@ const R_CONSTANTS = new Set([
class HighlightR extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static COMMENT = 2;
+ static QUOTE = 3;
+ static QUOTE_BACKSLASH = 4;
+ static DQUOTE = 5;
+ static DQUOTE_BACKSLASH = 6;
+ static HYPHEN = 7;
+ static HYPHEN_GT = 8;
+ static LT = 9;
+ static LT_LT = 10;
+ static COLON = 11;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightR.NORMAL:
+ if (!isascii(c) || isalpha(c)) {
+ this.state = HighlightR.WORD;
+ this.word += c;
+ } else if (c == '#') {
+ this.push("span", "comment");
+ this.append('#');
+ this.state = HighlightR.COMMENT;
+ } else if (c == '\'') {
+ this.state = HighlightR.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightR.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '-') {
+ this.state = HighlightR.HYPHEN;
+ } else if (c == '<') {
+ this.state = HighlightR.LT;
+ } else if (c == ':') {
+ this.state = HighlightR.COLON;
+ } else if (c == '$' || c == '@') {
+ this.push("span", "operator");
+ this.append(c);
+ this.pop();
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightR.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '.') {
+ this.word += c;
+ } else {
+ if (R_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (R_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (R_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ case HighlightR.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ }
+ break;
+
+ case HighlightR.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightR.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightR.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightR.QUOTE;
+ break;
+
+ case HighlightR.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightR.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightR.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightR.DQUOTE;
+ break;
+
+ case HighlightR.COLON:
+ if (c == ':') {
+ this.push("span", "operator");
+ this.append("::");
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else {
+ this.append(':');
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ case HighlightR.LT:
+ if (c == '<') {
+ this.state = HighlightR.LT_LT;
+ } else if (c == '-') {
+ this.push("span", "operator");
+ this.append("<-");
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else {
+ this.append('<');
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ case HighlightR.HYPHEN:
+ if (c == '>') {
+ this.state = HighlightR.HYPHEN_GT;
+ } else {
+ this.append('-');
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ case HighlightR.LT_LT:
+ if (c == '-') {
+ this.push("span", "operator");
+ this.append("<<-");
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else {
+ this.append("<<");
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ case HighlightR.HYPHEN_GT:
+ if (c == '>') {
+ this.push("span", "operator");
+ this.append("->>");
+ this.pop();
+ this.state = HighlightR.NORMAL;
+ } else {
+ this.push("span", "operator");
+ this.append("->");
+ this.pop();
+ this.epsilon(HighlightR.NORMAL);
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightR.WORD:
+ if (R_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (R_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (R_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightR.HYPHEN:
+ this.append('-');
+ break;
+ case HighlightR.HYPHEN_GT:
+ this.append("->");
+ break;
+ case HighlightR.LT:
+ this.append('<');
+ break;
+ case HighlightR.LT_LT:
+ this.append("<<");
+ break;
+ case HighlightR.COLON:
+ this.append(':');
+ break;
+ case HighlightR.QUOTE:
+ case HighlightR.QUOTE_BACKSLASH:
+ case HighlightR.DQUOTE:
+ case HighlightR.DQUOTE_BACKSLASH:
+ case HighlightR.COMMENT:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightR.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_ruby.js b/llamafile/server/www/highlight_ruby.js
index 68cfe520..79b87de6 100644
--- a/llamafile/server/www/highlight_ruby.js
+++ b/llamafile/server/www/highlight_ruby.js
@@ -132,16 +132,583 @@ const RUBY_CONSTANTS = new Set([
class HighlightRuby extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static EQUAL = 2;
+ static EQUAL_WORD = 3;
+ static QUOTE = 4;
+ static QUOTE_BACKSLASH = 5;
+ static DQUOTE = 6;
+ static DQUOTE_BACKSLASH = 7;
+ static TICK = 8;
+ static TICK_BACKSLASH = 9;
+ static COMMENT = 10;
+ static LT = 11;
+ static LT_LT = 12;
+ static LT_LT_NAME = 13;
+ static LT_LT_QNAME = 14;
+ static HEREDOC_BOL = 15;
+ static HEREDOC = 16;
+ static COLON = 17;
+ static COLON_WORD = 18;
+ static AT = 19;
+ static AT_WORD = 20;
+ static DOLLAR = 21;
+ static DOLLAR_WORD = 22;
+ static PERCENT = 23;
+ static PERCENT2 = 24;
+ static PERCENT_STRING = 25;
+ static MULTICOM = 26;
+ static MULTICOM_BOL = 27;
+ static REGEX = 28;
+ static REGEX_BACKSLASH = 29;
+
+ static mirror(c) {
+ switch (c) {
+ case '(':
+ return ')';
+ case '{':
+ return '}';
+ case '[':
+ return ']';
+ case '<':
+ return '>';
+ default:
+ return c;
+ }
+ }
+
+ static isident(c) {
+ return !isascii(c) ||
+ isalnum(c) ||
+ c == '!' ||
+ c == '$' ||
+ c == '%' ||
+ c == '&' ||
+ c == '-' ||
+ c == '/' ||
+ c == '=' ||
+ c == '?' ||
+ c == '@' ||
+ c == '^' ||
+ c == '_';
+ }
+
constructor(delegate) {
super(delegate);
+ this.i = 0;
+ this.q = 0;
+ this.last = 0;
+ this.level = 0;
+ this.opener = 0;
+ this.closer = 0;
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ this.heredoc = '';
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+
+ if (!isblank(c) && c != '/' && c != '<')
+ this.last = c;
+
+ switch (this.state) {
+
+ case HighlightRuby.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightRuby.WORD;
+ this.word += c;
+ } else if (c == ':') {
+ this.state = HighlightRuby.COLON;
+ } else if (c == '@') {
+ this.state = HighlightRuby.AT;
+ } else if (c == '=') {
+ this.state = HighlightRuby.EQUAL;
+ } else if (c == '$') {
+ this.state = HighlightRuby.DOLLAR;
+ } else if (c == '%') {
+ this.state = HighlightRuby.PERCENT;
+ this.q = 0;
+ } else if (c == '\'') {
+ this.state = HighlightRuby.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightRuby.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '`') {
+ this.state = HighlightRuby.TICK;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '#') {
+ this.push("span", "comment");
+ this.append(c);
+ this.state = HighlightRuby.COMMENT;
+ } else if (c == '<' && !isalnum(this.last)) {
+ this.append(c);
+ this.state = HighlightRuby.LT;
+ } else if (c == '/' && !isalnum(this.last)) {
+ this.state = HighlightRuby.REGEX;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '\n') {
+ this.append(c);
+ if (this.pending_heredoc) {
+ this.push("span", "string");
+ this.pending_heredoc = false;
+ this.state = HighlightRuby.HEREDOC_BOL;
+ this.i = 0;
+ }
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightRuby.EQUAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightRuby.EQUAL_WORD;
+ this.word += c;
+ } else {
+ this.append('=');
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.EQUAL_WORD:
+ if (HighlightRuby.isident(c)) {
+ this.word += c;
+ break;
+ } else if (this.word == "begin") {
+ this.push("span", "comment");
+ this.append("=begin");
+ this.append(c);
+ if (c == '\n') {
+ this.state = HighlightRuby.MULTICOM_BOL;
+ this.i = 0;
+ } else {
+ this.state = HighlightRuby.MULTICOM;
+ }
+ this.word = '';
+ break;
+ } else {
+ this.append('=');
+ this.state = HighlightRuby.WORD;
+ }
+ // fallthrough
+
+ case HighlightRuby.WORD:
+ if (HighlightRuby.isident(c)) {
+ this.word += c;
+ } else {
+ if (RUBY_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ this.last = 0;
+ } else if (RUBY_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (RUBY_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && this.word[0] == this.word[0].toUpperCase()) {
+ this.push("span", "class");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.REGEX:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRuby.REGEX_BACKSLASH;
+ }
+ break;
+
+ case HighlightRuby.REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRuby.REGEX;
+ break;
+
+ case HighlightRuby.MULTICOM:
+ this.append(c);
+ if (c == '\n') {
+ this.state = HighlightRuby.MULTICOM_BOL;
+ this.i = 0;
+ }
+ break;
+
+ case HighlightRuby.MULTICOM_BOL:
+ this.append(c);
+ if (c == "=end"[this.i]) {
+ if (++this.i == 4) {
+ this.state = HighlightRuby.NORMAL;
+ this.pop();
+ }
+ } else {
+ this.state = HighlightRuby.MULTICOM;
+ }
+ break;
+
+ case HighlightRuby.COLON:
+ if (HighlightRuby.isident(c)) {
+ this.push("span", "lispkw");
+ this.append(':');
+ this.append(c);
+ this.state = HighlightRuby.COLON_WORD;
+ } else {
+ this.append(':');
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.COLON_WORD:
+ if (HighlightRuby.isident(c)) {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.AT:
+ if (HighlightRuby.isident(c)) {
+ this.push("span", "var");
+ this.append('@');
+ this.append(c);
+ this.state = HighlightRuby.AT_WORD;
+ } else {
+ this.append('@');
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.AT_WORD:
+ if (HighlightRuby.isident(c)) {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.PERCENT:
+ if (c == 'q' || c == 'Q') {
+ this.q = c;
+ this.state = HighlightRuby.PERCENT2;
+ } else if (ispunct(c)) {
+ this.level = 1;
+ this.opener = c;
+ this.closer = HighlightRuby.mirror(c);
+ this.push("span", "string");
+ this.append('%');
+ this.append(c);
+ this.state = HighlightRuby.PERCENT_STRING;
+ } else {
+ this.append('%');
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.PERCENT2:
+ if (ispunct(c)) {
+ this.level = 1;
+ this.opener = c;
+ this.closer = HighlightRuby.mirror(c);
+ this.push("span", "string");
+ this.append('%');
+ this.append(this.q);
+ this.append(c);
+ this.state = HighlightRuby.PERCENT_STRING;
+ } else {
+ this.append('%');
+ this.append(this.q);
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.PERCENT_STRING:
+ this.append(c);
+ if (c == this.opener && this.opener != this.closer) {
+ ++this.level;
+ } else if (c == this.closer) {
+ if (!--this.level) {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ }
+ }
+ break;
+
+ case HighlightRuby.DOLLAR:
+ if (isdigit(c) || //
+ c == '!' || //
+ c == '"' || //
+ c == '#' || //
+ c == '$' || //
+ c == '&' || //
+ c == '-' || //
+ c == '/' || //
+ c == '<' || //
+ c == '=' || //
+ c == '>' || //
+ c == '@' || //
+ c == '\'' || //
+ c == '\\' || //
+ c == '^' || //
+ c == '_' || //
+ c == '`') {
+ this.push("span", "var");
+ this.append('$');
+ this.append(c);
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ } else if (isalpha(c)) {
+ this.push("span", "var");
+ this.append('$');
+ this.append(c);
+ this.state = HighlightRuby.DOLLAR_WORD;
+ } else {
+ this.append('$');
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.DOLLAR_WORD:
+ if (HighlightRuby.isident(c)) {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ }
+ break;
+
+ case HighlightRuby.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRuby.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightRuby.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRuby.QUOTE;
+ break;
+
+ case HighlightRuby.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRuby.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightRuby.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRuby.DQUOTE;
+ break;
+
+ case HighlightRuby.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightRuby.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRuby.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightRuby.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRuby.TICK;
+ break;
+
+ case HighlightRuby.LT:
+ if (c == '<') {
+ this.append(c);
+ this.state = HighlightRuby.LT_LT;
+ this.heredoc = '';
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ } else {
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.LT_LT:
+ if (c == '-') {
+ this.indented_heredoc = true;
+ this.append(c);
+ } else if (c == '\'' || c == '`' || c == '"') {
+ this.closer = c;
+ this.state = HighlightRuby.LT_LT_QNAME;
+ this.push("span", "string");
+ this.append(c);
+ } else if (isalpha(c) || c == '_') {
+ this.state = HighlightRuby.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else {
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.LT_LT_NAME:
+ if (isalnum(c) || c == '_') {
+ this.state = HighlightRuby.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else if (c == '\n') {
+ this.append(c);
+ this.push("span", "string");
+ this.state = HighlightRuby.HEREDOC_BOL;
+ } else {
+ this.pending_heredoc = true;
+ this.epsilon(HighlightRuby.NORMAL);
+ }
+ break;
+
+ case HighlightRuby.LT_LT_QNAME:
+ this.append(c);
+ if (c == this.closer) {
+ this.pop();
+ this.state = HighlightRuby.HEREDOC_BOL;
+ this.pending_heredoc = true;
+ this.state = HighlightRuby.NORMAL;
+ } else {
+ this.heredoc += c;
+ }
+ break;
+
+ case HighlightRuby.HEREDOC_BOL:
+ this.append(c);
+ if (c == '\n') {
+ if (this.i == this.heredoc.length) {
+ this.state = HighlightRuby.NORMAL;
+ this.pop();
+ }
+ this.i = 0;
+ } else if (c == '\t' && this.indented_heredoc) {
+ // do nothing
+ } else if (this.i < this.heredoc.length && this.heredoc[this.i] == c) {
+ this.i++;
+ } else {
+ this.state = HighlightRuby.HEREDOC;
+ this.i = 0;
+ }
+ break;
+
+ case HighlightRuby.HEREDOC:
+ this.append(c);
+ if (c == '\n')
+ this.state = HighlightRuby.HEREDOC_BOL;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightRuby.EQUAL_WORD:
+ this.append('=');
+ // fallthrough
+ case HighlightRuby.WORD:
+ if (RUBY_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (RUBY_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (RUBY_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && this.word[0] == this.word[0].toUpperCase()) {
+ this.push("span", "class");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightRuby.AT:
+ this.append('@');
+ break;
+ case HighlightRuby.EQUAL:
+ this.append('=');
+ break;
+ case HighlightRuby.COLON:
+ this.append(':');
+ break;
+ case HighlightRuby.DOLLAR:
+ this.append('$');
+ break;
+ case HighlightRuby.PERCENT:
+ this.append('%');
+ break;
+ case HighlightRuby.PERCENT2:
+ this.append('%');
+ this.append(this.q);
+ break;
+ case HighlightRuby.REGEX:
+ case HighlightRuby.REGEX_BACKSLASH:
+ case HighlightRuby.PERCENT_STRING:
+ case HighlightRuby.AT_WORD:
+ case HighlightRuby.DOLLAR_WORD:
+ case HighlightRuby.TICK:
+ case HighlightRuby.TICK_BACKSLASH:
+ case HighlightRuby.QUOTE:
+ case HighlightRuby.QUOTE_BACKSLASH:
+ case HighlightRuby.DQUOTE:
+ case HighlightRuby.DQUOTE_BACKSLASH:
+ case HighlightRuby.COMMENT:
+ case HighlightRuby.HEREDOC_BOL:
+ case HighlightRuby.HEREDOC:
+ case HighlightRuby.LT_LT_QNAME:
+ case HighlightRuby.COLON_WORD:
+ case HighlightRuby.MULTICOM:
+ case HighlightRuby.MULTICOM_BOL:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightRuby.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_rust.js b/llamafile/server/www/highlight_rust.js
index bd079349..3f1875e5 100644
--- a/llamafile/server/www/highlight_rust.js
+++ b/llamafile/server/www/highlight_rust.js
@@ -93,16 +93,259 @@ const RUST_TYPES = new Set([
class HighlightRust extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static QUOTE2 = 4;
+ static DQUOTE = 5;
+ static DQUOTE_BACKSLASH = 6;
+ static SLASH = 7;
+ static SLASH_SLASH = 8;
+ static SLASH_STAR = 9;
+ static SLASH_STAR_STAR = 10;
+ static HASH = 11;
+ static HASH_EXCLAIM = 12;
+ static ATTRIB = 13;
+
constructor(delegate) {
super(delegate);
+ this.nest = 0;
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightRust.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '!') {
+ this.epsilon(HighlightRust.WORD);
+ } else if (c == '/') {
+ this.state = HighlightRust.SLASH;
+ } else if (c == '#') {
+ this.state = HighlightRust.HASH;
+ } else if (c == '\'') {
+ this.state = HighlightRust.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightRust.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightRust.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '!') {
+ this.word += c;
+ } else {
+ if (RUST_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word.length >= 2 && this.word[this.word.length - 1] == '!') {
+ this.push("span", "macro");
+ this.append(this.word);
+ this.pop();
+ } else if (RUST_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (RUST_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightRust.NORMAL);
+ }
+ break;
+
+ case HighlightRust.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightRust.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightRust.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightRust.NORMAL);
+ }
+ break;
+
+ case HighlightRust.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ }
+ break;
+
+ case HighlightRust.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightRust.SLASH_STAR_STAR;
+ break;
+
+ case HighlightRust.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightRust.SLASH_STAR;
+ }
+ break;
+
+ case HighlightRust.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRust.QUOTE_BACKSLASH;
+ } else {
+ this.state = HighlightRust.QUOTE2;
+ }
+ break;
+
+ case HighlightRust.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRust.QUOTE2;
+ break;
+
+ case HighlightRust.QUOTE2:
+ if (c == '\'') {
+ this.append(c);
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ } else {
+ this.pop();
+ this.append(c);
+ this.state = HighlightRust.NORMAL;
+ }
+ break;
+
+ case HighlightRust.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightRust.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightRust.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightRust.DQUOTE;
+ break;
+
+ case HighlightRust.HASH:
+ if (c == '!') {
+ this.state = HighlightRust.HASH_EXCLAIM;
+ } else if (c == '[') {
+ this.push("span", "attrib");
+ this.append("#[");
+ this.state = HighlightRust.ATTRIB;
+ } else {
+ this.append('#');
+ this.epsilon(HighlightRust.NORMAL);
+ }
+ break;
+
+ case HighlightRust.HASH_EXCLAIM:
+ if (c == '[') {
+ this.push("span", "attrib");
+ this.append("#![");
+ this.state = HighlightRust.ATTRIB;
+ } else {
+ this.append("#!");
+ this.epsilon(HighlightRust.NORMAL);
+ }
+ break;
+
+ case HighlightRust.ATTRIB:
+ this.append(c);
+ if (c == '[') {
+ ++this.nest;
+ } else if (c == ']') {
+ if (this.nest) {
+ --this.nest;
+ } else {
+ this.pop();
+ this.state = HighlightRust.NORMAL;
+ }
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightRust.WORD:
+ if (RUST_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word.length >= 2 && this.word[this.word.length - 1] == '!') {
+ this.push("span", "macro");
+ this.append(this.word);
+ this.pop();
+ } else if (RUST_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (RUST_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightRust.SLASH:
+ this.append('/');
+ break;
+ case HighlightRust.QUOTE:
+ case HighlightRust.QUOTE_BACKSLASH:
+ case HighlightRust.QUOTE2:
+ case HighlightRust.DQUOTE:
+ case HighlightRust.DQUOTE_BACKSLASH:
+ case HighlightRust.ATTRIB:
+ case HighlightRust.SLASH_SLASH:
+ case HighlightRust.SLASH_STAR:
+ case HighlightRust.SLASH_STAR_STAR:
+ this.pop();
+ break;
+ case HighlightRust.HASH:
+ this.append('#');
+ break;
+ case HighlightRust.HASH_EXCLAIM:
+ this.append("#!");
+ break;
+ default:
+ break;
+ }
+ this.nest = 0;
+ this.state = HighlightRust.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_scala.js b/llamafile/server/www/highlight_scala.js
index 8b08d1eb..a25d36f7 100644
--- a/llamafile/server/www/highlight_scala.js
+++ b/llamafile/server/www/highlight_scala.js
@@ -54,16 +54,361 @@ const SCALA_KEYWORDS = new Set([
class HighlightScala extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static ANNOTATION = 4;
+ static ANNOTATION2 = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static DQUOTE = 10;
+ static DQUOTE_DOLLAR = 11;
+ static DQUOTE_VAR = 12;
+ static DQUOTESTR = 13;
+ static DQUOTESTR_BACKSLASH = 14;
+ static DQUOTESTR_DOLLAR = 15;
+ static DQUOTESTR_VAR = 16;
+ static DQUOTE2 = 17;
+ static DQUOTE3 = 18;
+ static DQUOTE3_BACKSLASH = 19;
+ static DQUOTE3_DOLLAR = 20;
+ static DQUOTE3_VAR = 21;
+ static DQUOTE31 = 22;
+ static DQUOTE32 = 23;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
+ this.nest = [];
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightScala.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightScala.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightScala.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightScala.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightScala.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '@') {
+ this.state = HighlightScala.ANNOTATION;
+ } else if (c == '{' && this.nest) {
+ this.nest.push(HighlightScala.NORMAL);
+ this.append('{');
+ } else if (c == '}' && this.nest) {
+ if ((this.state = this.nest.pop()) != HighlightScala.NORMAL)
+ this.push("span", "string");
+ this.append('}');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightScala.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (SCALA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && this.word[0] == this.word[0].toUpperCase()) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightScala.NORMAL);
+ }
+ break;
+
+ case HighlightScala.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightScala.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightScala.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightScala.NORMAL);
+ }
+ break;
+
+ case HighlightScala.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightScala.NORMAL;
+ }
+ break;
+
+ case HighlightScala.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightScala.SLASH_STAR_STAR;
+ break;
+
+ case HighlightScala.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightScala.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightScala.SLASH_STAR;
+ }
+ break;
+
+ case HighlightScala.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightScala.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightScala.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightScala.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightScala.QUOTE;
+ break;
+
+ case HighlightScala.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightScala.DQUOTE2;
+ } else if (c == '\\') {
+ this.state = HighlightScala.DQUOTESTR_BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightScala.DQUOTESTR_DOLLAR;
+ } else {
+ this.state = HighlightScala.DQUOTESTR;
+ }
+ break;
+
+ case HighlightScala.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightScala.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightScala.DQUOTESTR_BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightScala.DQUOTESTR_DOLLAR;
+ }
+ break;
+
+ case HighlightScala.DQUOTESTR_BACKSLASH:
+ this.append(c);
+ this.state = HighlightScala.DQUOTESTR;
+ break;
+
+ case HighlightScala.DQUOTESTR_DOLLAR:
+ if (c == '{') {
+ this.append(c);
+ this.pop();
+ this.nest.push(HighlightScala.DQUOTESTR);
+ this.state = HighlightScala.NORMAL;
+ } else if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "bold");
+ this.append(c);
+ this.state = HighlightScala.DQUOTESTR_VAR;
+ } else {
+ this.epsilon(HighlightScala.DQUOTESTR);
+ }
+ break;
+
+ case HighlightScala.DQUOTESTR_VAR:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightScala.DQUOTESTR);
+ }
+ break;
+
+ case HighlightScala.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightScala.DQUOTE3;
+ } else {
+ this.pop();
+ this.epsilon(HighlightScala.NORMAL);
+ }
+ break;
+
+ case HighlightScala.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightScala.DQUOTE31;
+ } else if (c == '$') {
+ this.state = HighlightScala.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightScala.DQUOTE3_BACKSLASH;
+ }
+ break;
+
+ case HighlightScala.DQUOTE31:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightScala.DQUOTE32;
+ } else if (c == '$') {
+ this.state = HighlightScala.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightScala.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightScala.DQUOTE3;
+ }
+ break;
+
+ case HighlightScala.DQUOTE32:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightScala.NORMAL;
+ } else if (c == '$') {
+ this.state = HighlightScala.DQUOTE3_DOLLAR;
+ } else if (c == '\\') {
+ this.state = HighlightScala.DQUOTE3_BACKSLASH;
+ } else {
+ this.state = HighlightScala.DQUOTE3;
+ }
+ break;
+
+ case HighlightScala.DQUOTE3_BACKSLASH:
+ this.append(c);
+ this.state = HighlightScala.DQUOTE3;
+ break;
+
+ case HighlightScala.DQUOTE3_DOLLAR:
+ if (c == '{') {
+ this.append(c);
+ this.pop();
+ this.nest.push(HighlightScala.DQUOTE3);
+ this.state = HighlightScala.NORMAL;
+ } else if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "bold");
+ this.append(c);
+ this.state = HighlightScala.DQUOTE3_VAR;
+ } else {
+ this.append(c);
+ this.state = HighlightScala.DQUOTE3;
+ }
+ break;
+
+ case HighlightScala.DQUOTE3_VAR:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightScala.DQUOTE3);
+ }
+ break;
+
+ case HighlightScala.ANNOTATION:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.push("span", "attrib");
+ this.append('@');
+ this.append(c);
+ this.state = HighlightScala.ANNOTATION2;
+ } else {
+ this.append('@');
+ this.epsilon(HighlightScala.NORMAL);
+ }
+ break;
+
+ case HighlightScala.ANNOTATION2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightScala.NORMAL);
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightScala.WORD:
+ if (SCALA_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (JAVA_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else if (this.word && this.word[0] == this.word[0].toUpperCase()) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightScala.SLASH:
+ this.append('/');
+ break;
+ case HighlightScala.ANNOTATION:
+ this.append('@');
+ break;
+ case HighlightScala.QUOTE:
+ case HighlightScala.QUOTE_BACKSLASH:
+ case HighlightScala.SLASH_SLASH:
+ case HighlightScala.SLASH_STAR:
+ case HighlightScala.SLASH_STAR_STAR:
+ case HighlightScala.DQUOTE:
+ case HighlightScala.DQUOTESTR:
+ case HighlightScala.DQUOTESTR_BACKSLASH:
+ case HighlightScala.DQUOTESTR_DOLLAR:
+ case HighlightScala.DQUOTESTR_VAR:
+ case HighlightScala.DQUOTE2:
+ case HighlightScala.DQUOTE3:
+ case HighlightScala.DQUOTE3_BACKSLASH:
+ case HighlightScala.DQUOTE31:
+ case HighlightScala.DQUOTE32:
+ case HighlightScala.ANNOTATION2:
+ case HighlightScala.DQUOTE3_DOLLAR:
+ case HighlightScala.DQUOTE3_VAR:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightScala.NORMAL;
+ this.nest = [];
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_shell.js b/llamafile/server/www/highlight_shell.js
index 2a034fa8..47486f4b 100644
--- a/llamafile/server/www/highlight_shell.js
+++ b/llamafile/server/www/highlight_shell.js
@@ -83,16 +83,523 @@ const SHELL_BUILTINS = new Set([
class HighlightShell extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static DQUOTE = 3;
+ static DQUOTE_VAR = 4;
+ static DQUOTE_VAR2 = 5;
+ static DQUOTE_CURL = 6;
+ static DQUOTE_CURL_BACKSLASH = 7;
+ static DQUOTE_BACKSLASH = 8;
+ static TICK = 9;
+ static TICK_BACKSLASH = 10;
+ static VAR = 11;
+ static VAR2 = 12;
+ static CURL = 13;
+ static CURL_BACKSLASH = 14;
+ static COMMENT = 15;
+ static LT = 16;
+ static LT_LT = 17;
+ static LT_LT_NAME = 18;
+ static LT_LT_QNAME = 19;
+ static HEREDOC_BOL = 20;
+ static HEREDOC = 21;
+ static HEREDOC_VAR = 22;
+ static HEREDOC_VAR2 = 23;
+ static HEREDOC_CURL = 24;
+ static HEREDOC_CURL_BACKSLASH = 25;
+ static BACKSLASH = 26;
+
constructor(delegate) {
super(delegate);
+ this.c = 0;
+ this.i = 0;
+ this.curl = 0;
+ this.last = 0;
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ this.no_interpolation = false;
+ this.heredoc = '';
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ this.last = this.c;
+ this.c = c;
+ switch (this.state) {
+
+ case HighlightShell.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightShell.WORD;
+ this.word += c;
+ } else if (c == '\'') {
+ this.state = HighlightShell.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '\\') {
+ this.state = HighlightShell.BACKSLASH;
+ this.push("span", "escape");
+ this.append('\\');
+ } else if (c == '"') {
+ this.state = HighlightShell.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '`') {
+ this.state = HighlightShell.TICK;
+ this.push("span", "string");
+ this.append('`');
+ } else if (c == '$') {
+ this.state = HighlightShell.VAR;
+ this.append('$');
+ } else if (c == '<') {
+ this.state = HighlightShell.LT;
+ this.append('<');
+ } else if (c == '#' && (!this.last || isspace(this.last))) {
+ this.push("span", "comment");
+ this.append('#');
+ this.state = HighlightShell.COMMENT;
+ } else if (c == '\n') {
+ this.append('\n');
+ if (this.pending_heredoc) {
+ this.push("span", "string");
+ this.pending_heredoc = false;
+ this.state = HighlightShell.HEREDOC_BOL;
+ this.i = 0;
+ }
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ break;
+
+ case HighlightShell.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (SHELL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SHELL_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightShell.NORMAL);
+ }
+ break;
+
+ case HighlightShell.VAR:
+ if (c == '!' || //
+ c == '#' || //
+ c == '$' || //
+ c == '*' || //
+ c == '-' || //
+ c == '?' || //
+ c == '@' || //
+ c == '\\' || //
+ c == '^') {
+ this.push("span", "var");
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ break;
+ } else if (c == '{') {
+ this.append('{');
+ this.push("span", "var");
+ this.state = HighlightShell.CURL;
+ this.curl = 1;
+ break;
+ } else {
+ this.push("span", "var");
+ this.state = HighlightShell.VAR2;
+ }
+ // fallthrough
+
+ case HighlightShell.VAR2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightShell.NORMAL);
+ }
+ break;
+
+ case HighlightShell.CURL:
+ if (c == '\\') {
+ this.state = HighlightShell.CURL_BACKSLASH;
+ this.pop();
+ this.push("span", "escape");
+ this.append('\\');
+ } else if (c == '{') {
+ this.pop();
+ this.append('{');
+ this.push("span", "var");
+ ++this.curl;
+ } else if (c == '}') {
+ this.pop();
+ this.append('}');
+ if (!--this.curl) {
+ this.state = HighlightShell.NORMAL;
+ }
+ } else if (ispunct(c)) {
+ this.pop();
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.CURL_BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.CURL;
+ break;
+
+ case HighlightShell.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ }
+ break;
+
+ case HighlightShell.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ }
+ break;
+
+ case HighlightShell.DQUOTE:
+ if (c == '"') {
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ } else if (c == '\\') {
+ this.append(c);
+ this.state = HighlightShell.DQUOTE_BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightShell.DQUOTE_VAR;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightShell.DQUOTE;
+ break;
+
+ case HighlightShell.DQUOTE_VAR:
+ if (c == '!' || //
+ c == '#' || //
+ c == '$' || //
+ c == '*' || //
+ c == '-' || //
+ c == '?' || //
+ c == '@' || //
+ c == '\\' || //
+ c == '^') {
+ this.push("span", "bold");
+ this.append('$');
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.DQUOTE;
+ break;
+ } else if (c == '{') {
+ this.push("span", "bold");
+ this.append("${");
+ this.state = HighlightShell.DQUOTE_CURL;
+ this.curl = 1;
+ break;
+ } else if (c == '(') {
+ this.append('$');
+ this.state = HighlightShell.DQUOTE_VAR2;
+ } else {
+ this.push("span", "bold");
+ this.append('$');
+ this.state = HighlightShell.DQUOTE_VAR2;
+ }
+ // fallthrough
+
+ case HighlightShell.DQUOTE_VAR2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightShell.DQUOTE);
+ }
+ break;
+
+ case HighlightShell.DQUOTE_CURL:
+ if (c == '\\') {
+ this.state = HighlightShell.DQUOTE_CURL_BACKSLASH;
+ this.append('\\');
+ } else if (c == '{') {
+ this.append('{');
+ ++this.curl;
+ } else if (c == '}') {
+ this.append('}');
+ if (!--this.curl) {
+ this.pop();
+ this.state = HighlightShell.DQUOTE;
+ }
+ } else if (ispunct(c)) {
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.DQUOTE_CURL_BACKSLASH:
+ this.append(c);
+ this.state = HighlightShell.DQUOTE_CURL;
+ break;
+
+ case HighlightShell.TICK:
+ this.append(c);
+ if (c == '`') {
+ this.pop();
+ this.state = HighlightShell.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightShell.TICK_BACKSLASH;
+ }
+ break;
+
+ case HighlightShell.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightShell.TICK;
+ break;
+
+ case HighlightShell.LT:
+ if (c == '<') {
+ this.append(c);
+ this.state = HighlightShell.LT_LT;
+ this.heredoc = '';
+ this.pending_heredoc = false;
+ this.indented_heredoc = false;
+ this.no_interpolation = false;
+ } else {
+ this.epsilon(HighlightShell.NORMAL);
+ }
+ break;
+
+ case HighlightShell.LT_LT:
+ if (c == '-') {
+ this.indented_heredoc = true;
+ this.append(c);
+ } else if (c == '\\') {
+ this.append(c);
+ } else if (c == '\'') {
+ this.state = HighlightShell.LT_LT_QNAME;
+ this.push("span", "string");
+ this.append(c);
+ this.no_interpolation = true;
+ } else if (isalpha(c) || c == '_') {
+ this.state = HighlightShell.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else if (isascii(c) && isblank(c)) {
+ this.append(c);
+ } else {
+ this.epsilon(HighlightShell.NORMAL);
+ }
+ break;
+
+ case HighlightShell.LT_LT_NAME:
+ if (isalnum(c) || c == '_') {
+ this.state = HighlightShell.LT_LT_NAME;
+ this.heredoc += c;
+ this.append(c);
+ } else if (c == '\n') {
+ this.append(c);
+ this.push("span", "string");
+ this.state = HighlightShell.HEREDOC_BOL;
+ } else {
+ this.pending_heredoc = true;
+ this.epsilon(HighlightShell.NORMAL);
+ }
+ break;
+
+ case HighlightShell.LT_LT_QNAME:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightShell.HEREDOC_BOL;
+ this.pending_heredoc = true;
+ this.state = HighlightShell.NORMAL;
+ } else {
+ this.heredoc += c;
+ }
+ break;
+
+ case HighlightShell.HEREDOC_BOL:
+ this.append(c);
+ if (c == '\n') {
+ if (this.i == this.heredoc.length) {
+ this.state = HighlightShell.NORMAL;
+ this.pop();
+ }
+ this.i = 0;
+ } else if (c == '\t' && this.indented_heredoc) {
+ // do nothing
+ } else if (this.i < this.heredoc.length && this.heredoc[this.i] == c) {
+ this.i++;
+ } else {
+ this.state = HighlightShell.HEREDOC;
+ this.i = 0;
+ }
+ break;
+
+ case HighlightShell.HEREDOC:
+ if (c == '\n') {
+ this.append('\n');
+ this.state = HighlightShell.HEREDOC_BOL;
+ } else if (c == '$' && !this.no_interpolation) {
+ this.state = HighlightShell.HEREDOC_VAR;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.HEREDOC_VAR:
+ if (c == '!' || //
+ c == '#' || //
+ c == '$' || //
+ c == '*' || //
+ c == '-' || //
+ c == '?' || //
+ c == '@' || //
+ c == '\\' || //
+ c == '^') {
+ this.push("span", "bold");
+ this.append('$');
+ this.append(c);
+ this.pop();
+ this.state = HighlightShell.HEREDOC;
+ break;
+ } else if (c == '{') {
+ this.push("span", "bold");
+ this.append("${");
+ this.state = HighlightShell.HEREDOC_CURL;
+ this.curl = 1;
+ break;
+ } else if (c == '(') {
+ this.append('$');
+ this.state = HighlightShell.HEREDOC_VAR2;
+ } else {
+ this.push("span", "bold");
+ this.append('$');
+ this.state = HighlightShell.HEREDOC_VAR2;
+ }
+ // fallthrough
+
+ case HighlightShell.HEREDOC_VAR2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightShell.HEREDOC);
+ }
+ break;
+
+ case HighlightShell.HEREDOC_CURL:
+ if (c == '\\') {
+ this.state = HighlightShell.HEREDOC_CURL_BACKSLASH;
+ this.append('\\');
+ } else if (c == '{') {
+ this.append('{');
+ ++this.curl;
+ } else if (c == '}') {
+ this.append('}');
+ if (!--this.curl) {
+ this.pop();
+ this.state = HighlightShell.HEREDOC;
+ }
+ } else if (ispunct(c)) {
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightShell.HEREDOC_CURL_BACKSLASH:
+ this.append(c);
+ this.state = HighlightShell.HEREDOC_CURL;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightShell.WORD:
+ if (SHELL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SHELL_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightShell.DQUOTE_VAR:
+ this.append('$');
+ this.pop();
+ break;
+ case HighlightShell.HEREDOC_VAR:
+ this.append('$');
+ this.pop();
+ break;
+ case HighlightShell.VAR2:
+ case HighlightShell.CURL:
+ case HighlightShell.CURL_BACKSLASH:
+ case HighlightShell.TICK:
+ case HighlightShell.TICK_BACKSLASH:
+ case HighlightShell.QUOTE:
+ case HighlightShell.DQUOTE:
+ case HighlightShell.DQUOTE_VAR2:
+ case HighlightShell.DQUOTE_CURL:
+ case HighlightShell.DQUOTE_CURL_BACKSLASH:
+ case HighlightShell.DQUOTE_BACKSLASH:
+ case HighlightShell.COMMENT:
+ case HighlightShell.HEREDOC_BOL:
+ case HighlightShell.HEREDOC:
+ case HighlightShell.HEREDOC_VAR2:
+ case HighlightShell.HEREDOC_CURL:
+ case HighlightShell.HEREDOC_CURL_BACKSLASH:
+ case HighlightShell.LT_LT_QNAME:
+ case HighlightShell.BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightShell.NORMAL;
+ this.last = 0;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_sql.js b/llamafile/server/www/highlight_sql.js
index 64eff041..2152ce38 100644
--- a/llamafile/server/www/highlight_sql.js
+++ b/llamafile/server/www/highlight_sql.js
@@ -954,16 +954,188 @@ const SQL_TYPES = new Set([
class HighlightSql extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static HYPHEN = 6;
+ static HYPHEN_HYPHEN = 7;
+ static SLASH = 8;
+ static SLASH_STAR = 9;
+ static SLASH_STAR_STAR = 10;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightSql.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.epsilon(HighlightSql.WORD);
+ } else if (c == '/') {
+ this.state = HighlightSql.SLASH;
+ } else if (c == '-') {
+ this.state = HighlightSql.HYPHEN;
+ } else if (c == '\'') {
+ this.state = HighlightSql.QUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else if (c == '"') {
+ this.state = HighlightSql.DQUOTE;
+ this.push("span", "string");
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightSql.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_' || c == '-') {
+ this.word += c;
+ } else {
+ if (SQL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SQL_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightSql.NORMAL);
+ }
+ break;
+
+ case HighlightSql.SLASH:
+ if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightSql.SLASH_STAR;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightSql.NORMAL);
+ }
+ break;
+
+ case HighlightSql.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightSql.SLASH_STAR_STAR;
+ break;
+
+ case HighlightSql.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightSql.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightSql.SLASH_STAR;
+ }
+ break;
+
+ case HighlightSql.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightSql.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightSql.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightSql.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightSql.QUOTE;
+ break;
+
+ case HighlightSql.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightSql.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightSql.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightSql.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightSql.DQUOTE;
+ break;
+
+ case HighlightSql.HYPHEN:
+ if (c == '-') {
+ this.push("span", "comment");
+ this.append("--");
+ this.state = HighlightSql.HYPHEN_HYPHEN;
+ } else {
+ this.append('-');
+ this.epsilon(HighlightSql.NORMAL);
+ }
+ break;
+
+ case HighlightSql.HYPHEN_HYPHEN:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightSql.NORMAL;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightSql.WORD:
+ if (SQL_KEYWORDS.has(this.word.toLowerCase())) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SQL_TYPES.has(this.word.toLowerCase())) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightSql.SLASH:
+ this.append('/');
+ break;
+ case HighlightSql.HYPHEN:
+ this.append('-');
+ break;
+ case HighlightSql.QUOTE:
+ case HighlightSql.QUOTE_BACKSLASH:
+ case HighlightSql.DQUOTE:
+ case HighlightSql.DQUOTE_BACKSLASH:
+ case HighlightSql.SLASH_STAR:
+ case HighlightSql.SLASH_STAR_STAR:
+ case HighlightSql.HYPHEN_HYPHEN:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightSql.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_swift.js b/llamafile/server/www/highlight_swift.js
index 153b45eb..96a48b4a 100644
--- a/llamafile/server/www/highlight_swift.js
+++ b/llamafile/server/www/highlight_swift.js
@@ -274,12 +274,9 @@ const SWIFT_TYPES = new Set([
'ASCII',
'AcceptPolicy',
'ActivationType',
- 'ActivityOptions',
- 'AddingOptions',
'AdditiveArithmetic',
'AffineTransform',
'AllCases',
- 'AncestorRepresentation',
'Any',
'AnyBidirectionalCollection',
'AnyClass',
@@ -294,21 +291,12 @@ const SWIFT_TYPES = new Set([
'Array',
'ArrayLiteralElement',
'ArraySlice',
- 'AuthChallengeDisposition',
- 'AutoreleasingUnsafeMutablePointer',
- 'BackgroundTaskCancelledReason',
- 'Base64DecodingOptions',
- 'Base64EncodingOptions',
'Behavior',
- 'BidirectionalCollection',
'BinaryFloatingPoint',
'BinaryInteger',
'BiquadFunctions',
'BlockOperation',
'Body',
- 'BookmarkCreationOptions',
- 'BookmarkFileCreationOptions',
- 'BookmarkResolutionOptions',
'Bool',
'BooleanLiteralType',
'Bound',
@@ -362,7 +350,6 @@ const SWIFT_TYPES = new Set([
'CommandLine',
'Comparable',
'Comparator',
- 'CompareOptions',
'ComparisonResult',
'CompletionHandler',
'Component',
@@ -409,7 +396,6 @@ const SWIFT_TYPES = new Set([
'DictionaryIterator',
'DictionaryLiteral',
'Dimension',
- 'DirectoryEnumerationOptions',
'DirectoryEnumerator',
'DisplayStyle',
'Distance',
@@ -418,7 +404,6 @@ const SWIFT_TYPES = new Set([
'DocumentReadingOptionKey',
'DocumentType',
'Double',
- 'DrawingOptions',
'DropFirstSequence',
'DropWhileSequence',
'Element',
@@ -428,28 +413,16 @@ const SWIFT_TYPES = new Set([
'EncodedScalar',
'Encoder',
'Encoding',
- 'EncodingConversionOptions',
'EncodingError',
'EnergyFormatter',
'EnumeratedIterator',
'EnumeratedSequence',
- 'EnumerationOptions',
'Equatable',
'Error',
'ErrorCode',
'ErrorPointer',
'Event',
'Exponent',
- 'ExpressibleByArrayLiteral',
- 'ExpressibleByBooleanLiteral',
- 'ExpressibleByDictionaryLiteral',
- 'ExpressibleByExtendedGraphemeClusterLiteral',
- 'ExpressibleByFloatLiteral',
- 'ExpressibleByIntegerLiteral',
- 'ExpressibleByNilLiteral',
- 'ExpressibleByStringInterpolation',
- 'ExpressibleByStringLiteral',
- 'ExpressibleByUnicodeScalarLiteral',
'ExpressionType',
'ExtendedGraphemeClusterLiteralType',
'ExtendedGraphemeClusterType',
@@ -504,7 +477,6 @@ const SWIFT_TYPES = new Set([
'Int64',
'Int8',
'IntegerLiteralType',
- 'ItemReplacementOptions',
'Iterator',
'IteratorProtocol',
'IteratorSequence',
@@ -513,14 +485,8 @@ const SWIFT_TYPES = new Set([
'JSONSerialization',
'JoinedSequence',
'Key',
- 'KeyDecodingStrategy',
- 'KeyEncodingStrategy',
'KeyPath',
'KeyValuePairs',
- 'KeyedDecodingContainer',
- 'KeyedDecodingContainerProtocol',
- 'KeyedEncodingContainer',
- 'KeyedEncodingContainerProtocol',
'Keys',
'Kind',
'LanguageDirection',
@@ -549,7 +515,6 @@ const SWIFT_TYPES = new Set([
'MaskStorage',
'MassFormatter',
'MatchingFlags',
- 'MatchingOptions',
'MatchingPolicy',
'Measurement',
'MeasurementFormatter',
@@ -560,39 +525,9 @@ const SWIFT_TYPES = new Set([
'MirrorPath',
'Mode',
'Modifier',
- 'MultipathServiceType',
- 'MutabilityOptions',
- 'MutableCollection',
- 'MutableURLRequest',
- 'NSASCIIStringEncoding',
- 'NSAffineTransform',
- 'NSAffineTransformStruct',
- 'NSAppleEventDescriptor',
- 'NSAppleEventManager',
- 'NSAppleEventTimeOutDefault',
- 'NSAppleEventTimeOutNone',
'NSAppleScript',
'NSArchiver',
- 'NSArgumentEvaluationScriptError',
- 'NSArgumentsWrongScriptError',
'NSArray',
- 'NSAssertionHandler',
- 'NSAssertionHandlerKey',
- 'NSAttributedString',
- 'NSBackgroundActivityScheduler',
- 'NSBinarySearchingOptions',
- 'NSBuddhistCalendar',
- 'NSBundleErrorMaximum',
- 'NSBundleErrorMinimum',
- 'NSBundleExecutableArchitectureI386',
- 'NSBundleExecutableArchitecturePPC',
- 'NSBundleExecutableArchitecturePPC64',
- 'NSBundleExecutableArchitectureX86_64',
- 'NSBundleOnDemandResourceExceededMaximumSizeError',
- 'NSBundleOnDemandResourceInvalidTagError',
- 'NSBundleOnDemandResourceOutOfSpaceError',
- 'NSBundleResourceRequest',
- 'NSBundleResourceRequestLoadingPriorityUrgent',
'NSCache',
'NSCacheDelegate',
'NSCalendar',
@@ -627,7 +562,6 @@ const SWIFT_TYPES = new Set([
'NSCompressionFailedError',
'NSCondition',
'NSConditionLock',
- 'NSContainerSpecifierError',
'NSCopying',
'NSCountCommand',
'NSCountedSet',
@@ -647,11 +581,9 @@ const SWIFT_TYPES = new Set([
'NSDecompressionFailedError',
'NSDeleteCommand',
'NSDictionary',
- 'NSDiscardableContent',
'NSDistributedLock',
'NSEdgeInsets',
'NSEdgeInsetsZero',
- 'NSEnumerationOptions',
'NSEnumerator',
'NSError',
'NSErrorDomain',
@@ -668,58 +600,10 @@ const SWIFT_TYPES = new Set([
'NSExecutableRuntimeMismatchError',
'NSExistsCommand',
'NSExpression',
- 'NSExtensionContext',
- 'NSExtensionItem',
- 'NSExtensionItemAttachmentsKey',
- 'NSExtensionItemAttributedContentTextKey',
- 'NSExtensionItemAttributedTitleKey',
- 'NSExtensionItemsAndErrorsKey',
- 'NSExtensionJavaScriptFinalizeArgumentKey',
- 'NSExtensionJavaScriptPreprocessingResultsKey',
- 'NSExtensionRequestHandling',
'NSFastEnumeration',
'NSFastEnumerationIterator',
'NSFastEnumerationState',
'NSFeatureUnsupportedError',
- 'NSFileAccessIntent',
- 'NSFileCoordinator',
- 'NSFileErrorMaximum',
- 'NSFileErrorMinimum',
- 'NSFileHandleNotificationDataItem',
- 'NSFileHandleNotificationFileHandleItem',
- 'NSFileHandleNotificationMonitorModes',
- 'NSFileLockingError',
- 'NSFileManagerUnmountBusyError',
- 'NSFileManagerUnmountDissentingProcessIdentifierErrorKey',
- 'NSFileManagerUnmountUnknownError',
- 'NSFileNoSuchFileError',
- 'NSFilePathErrorKey',
- 'NSFilePresenter',
- 'NSFileProviderService',
- 'NSFileProviderServiceName',
- 'NSFileReadCorruptFileError',
- 'NSFileReadInapplicableStringEncodingError',
- 'NSFileReadInvalidFileNameError',
- 'NSFileReadNoPermissionError',
- 'NSFileReadNoSuchFileError',
- 'NSFileReadTooLargeError',
- 'NSFileReadUnknownError',
- 'NSFileReadUnknownStringEncodingError',
- 'NSFileReadUnsupportedSchemeError',
- 'NSFileSecurity',
- 'NSFileVersion',
- 'NSFileWriteFileExistsError',
- 'NSFileWriteInapplicableStringEncodingError',
- 'NSFileWriteInvalidFileNameError',
- 'NSFileWriteNoPermissionError',
- 'NSFileWriteOutOfSpaceError',
- 'NSFileWriteUnknownError',
- 'NSFileWriteUnsupportedSchemeError',
- 'NSFileWriteVolumeReadOnlyError',
- 'NSFormattingError',
- 'NSFormattingErrorMaximum',
- 'NSFormattingErrorMinimum',
- 'NSFoundationVersionNumber',
'NSGetCommand',
'NSGrammarCorrections',
'NSGrammarRange',
@@ -728,8 +612,6 @@ const SWIFT_TYPES = new Set([
'NSHPUXOperatingSystem',
'NSHashEnumerator',
'NSHashTable',
- 'NSHashTableCallBacks',
- 'NSHashTableOptions',
'NSHebrewCalendar',
'NSHelpAnchorErrorKey',
'NSISO2022JPStringEncoding',
@@ -738,34 +620,12 @@ const SWIFT_TYPES = new Set([
'NSISOLatin2StringEncoding',
'NSIndexPath',
'NSIndexSet',
- 'NSIndexSetIterator',
- 'NSIndexSpecifier',
- 'NSIndianCalendar',
- 'NSIntMapKeyCallBacks',
- 'NSIntMapValueCallBacks',
- 'NSIntegerHashCallBacks',
- 'NSIntegerMapKeyCallBacks',
- 'NSIntegerMapValueCallBacks',
- 'NSInternalScriptError',
- 'NSInternalSpecifierError',
- 'NSInvalidIndexSpecifierError',
- 'NSIslamicCalendar',
- 'NSIslamicCivilCalendar',
- 'NSItemProvider',
- 'NSItemProviderFileOptions',
- 'NSItemProviderPreferredImageSizeKey',
- 'NSItemProviderReading',
- 'NSItemProviderRepresentationVisibility',
- 'NSItemProviderWriting',
- 'NSJapaneseCalendar',
- 'NSJapaneseEUCStringEncoding',
'NSKeySpecifierEvaluationScriptError',
'NSKeyValueChange',
'NSKeyValueChangeKey',
'NSKeyValueObservation',
'NSKeyValueObservedChange',
'NSKeyValueObservingCustomization',
- 'NSKeyValueObservingOptions',
'NSKeyValueOperator',
'NSKeyValueSetMutationKind',
'NSKeyValueValidationError',
@@ -780,11 +640,6 @@ const SWIFT_TYPES = new Set([
'NSLinguisticTaggerUnit',
'NSLoadedClasses',
'NSLocale',
- 'NSLocalizedDescriptionKey',
- 'NSLocalizedFailureErrorKey',
- 'NSLocalizedFailureReasonErrorKey',
- 'NSLocalizedRecoveryOptionsErrorKey',
- 'NSLocalizedRecoverySuggestionErrorKey',
'NSLock',
'NSLocking',
'NSLogicalTest',
@@ -796,211 +651,10 @@ const SWIFT_TYPES = new Set([
'NSMapEnumerator',
'NSMapTable',
'NSMapTableCopyIn',
- 'NSMapTableKeyCallBacks',
'NSMapTableObjectPointerPersonality',
- 'NSMapTableOptions',
'NSMapTableStrongMemory',
- 'NSMapTableValueCallBacks',
'NSMapTableWeakMemory',
'NSMeasurement',
- 'NSMetadataItem',
- 'NSMetadataItemAcquisitionMakeKey',
- 'NSMetadataItemAcquisitionModelKey',
- 'NSMetadataItemAlbumKey',
- 'NSMetadataItemAltitudeKey',
- 'NSMetadataItemApertureKey',
- 'NSMetadataItemAppleLoopDescriptorsKey',
- 'NSMetadataItemAppleLoopsKeyFilterTypeKey',
- 'NSMetadataItemAppleLoopsLoopModeKey',
- 'NSMetadataItemAppleLoopsRootKeyKey',
- 'NSMetadataItemApplicationCategoriesKey',
- 'NSMetadataItemAttributeChangeDateKey',
- 'NSMetadataItemAudiencesKey',
- 'NSMetadataItemAudioBitRateKey',
- 'NSMetadataItemAudioChannelCountKey',
- 'NSMetadataItemAudioEncodingApplicationKey',
- 'NSMetadataItemAudioSampleRateKey',
- 'NSMetadataItemAudioTrackNumberKey',
- 'NSMetadataItemAuthorAddressesKey',
- 'NSMetadataItemAuthorEmailAddressesKey',
- 'NSMetadataItemAuthorsKey',
- 'NSMetadataItemBitsPerSampleKey',
- 'NSMetadataItemCFBundleIdentifierKey',
- 'NSMetadataItemCameraOwnerKey',
- 'NSMetadataItemCityKey',
- 'NSMetadataItemCodecsKey',
- 'NSMetadataItemColorSpaceKey',
- 'NSMetadataItemCommentKey',
- 'NSMetadataItemComposerKey',
- 'NSMetadataItemContactKeywordsKey',
- 'NSMetadataItemContentCreationDateKey',
- 'NSMetadataItemContentModificationDateKey',
- 'NSMetadataItemContentTypeKey',
- 'NSMetadataItemContentTypeTreeKey',
- 'NSMetadataItemContributorsKey',
- 'NSMetadataItemCopyrightKey',
- 'NSMetadataItemCountryKey',
- 'NSMetadataItemCoverageKey',
- 'NSMetadataItemCreatorKey',
- 'NSMetadataItemDateAddedKey',
- 'NSMetadataItemDeliveryTypeKey',
- 'NSMetadataItemDescriptionKey',
- 'NSMetadataItemDirectorKey',
- 'NSMetadataItemDisplayNameKey',
- 'NSMetadataItemDownloadedDateKey',
- 'NSMetadataItemDueDateKey',
- 'NSMetadataItemDurationSecondsKey',
- 'NSMetadataItemEXIFGPSVersionKey',
- 'NSMetadataItemEXIFVersionKey',
- 'NSMetadataItemEditorsKey',
- 'NSMetadataItemEmailAddressesKey',
- 'NSMetadataItemEncodingApplicationsKey',
- 'NSMetadataItemExecutableArchitecturesKey',
- 'NSMetadataItemExecutablePlatformKey',
- 'NSMetadataItemExposureModeKey',
- 'NSMetadataItemExposureProgramKey',
- 'NSMetadataItemExposureTimeSecondsKey',
- 'NSMetadataItemExposureTimeStringKey',
- 'NSMetadataItemFNumberKey',
- 'NSMetadataItemFSContentChangeDateKey',
- 'NSMetadataItemFSCreationDateKey',
- 'NSMetadataItemFSNameKey',
- 'NSMetadataItemFSSizeKey',
- 'NSMetadataItemFinderCommentKey',
- 'NSMetadataItemFlashOnOffKey',
- 'NSMetadataItemFocalLength35mmKey',
- 'NSMetadataItemFocalLengthKey',
- 'NSMetadataItemFontsKey',
- 'NSMetadataItemGPSAreaInformationKey',
- 'NSMetadataItemGPSDOPKey',
- 'NSMetadataItemGPSDateStampKey',
- 'NSMetadataItemGPSDestBearingKey',
- 'NSMetadataItemGPSDestDistanceKey',
- 'NSMetadataItemGPSDestLatitudeKey',
- 'NSMetadataItemGPSDestLongitudeKey',
- 'NSMetadataItemGPSDifferentalKey',
- 'NSMetadataItemGPSMapDatumKey',
- 'NSMetadataItemGPSMeasureModeKey',
- 'NSMetadataItemGPSProcessingMethodKey',
- 'NSMetadataItemGPSStatusKey',
- 'NSMetadataItemGPSTrackKey',
- 'NSMetadataItemGenreKey',
- 'NSMetadataItemHasAlphaChannelKey',
- 'NSMetadataItemHeadlineKey',
- 'NSMetadataItemISOSpeedKey',
- 'NSMetadataItemIdentifierKey',
- 'NSMetadataItemImageDirectionKey',
- 'NSMetadataItemInformationKey',
- 'NSMetadataItemInstantMessageAddressesKey',
- 'NSMetadataItemInstructionsKey',
- 'NSMetadataItemIsApplicationManagedKey',
- 'NSMetadataItemIsGeneralMIDISequenceKey',
- 'NSMetadataItemIsLikelyJunkKey',
- 'NSMetadataItemIsUbiquitousKey',
- 'NSMetadataItemKeySignatureKey',
- 'NSMetadataItemKeywordsKey',
- 'NSMetadataItemKindKey',
- 'NSMetadataItemLanguagesKey',
- 'NSMetadataItemLastUsedDateKey',
- 'NSMetadataItemLatitudeKey',
- 'NSMetadataItemLayerNamesKey',
- 'NSMetadataItemLensModelKey',
- 'NSMetadataItemLongitudeKey',
- 'NSMetadataItemLyricistKey',
- 'NSMetadataItemMaxApertureKey',
- 'NSMetadataItemMediaTypesKey',
- 'NSMetadataItemMeteringModeKey',
- 'NSMetadataItemMusicalGenreKey',
- 'NSMetadataItemMusicalInstrumentCategoryKey',
- 'NSMetadataItemMusicalInstrumentNameKey',
- 'NSMetadataItemNamedLocationKey',
- 'NSMetadataItemNumberOfPagesKey',
- 'NSMetadataItemOrganizationsKey',
- 'NSMetadataItemOrientationKey',
- 'NSMetadataItemOriginalFormatKey',
- 'NSMetadataItemOriginalSourceKey',
- 'NSMetadataItemPageHeightKey',
- 'NSMetadataItemPageWidthKey',
- 'NSMetadataItemParticipantsKey',
- 'NSMetadataItemPathKey',
- 'NSMetadataItemPerformersKey',
- 'NSMetadataItemPhoneNumbersKey',
- 'NSMetadataItemPixelCountKey',
- 'NSMetadataItemPixelHeightKey',
- 'NSMetadataItemPixelWidthKey',
- 'NSMetadataItemProducerKey',
- 'NSMetadataItemProfileNameKey',
- 'NSMetadataItemProjectsKey',
- 'NSMetadataItemPublishersKey',
- 'NSMetadataItemRecipientAddressesKey',
- 'NSMetadataItemRecipientEmailAddressesKey',
- 'NSMetadataItemRecipientsKey',
- 'NSMetadataItemRecordingDateKey',
- 'NSMetadataItemRecordingYearKey',
- 'NSMetadataItemRedEyeOnOffKey',
- 'NSMetadataItemResolutionHeightDPIKey',
- 'NSMetadataItemResolutionWidthDPIKey',
- 'NSMetadataItemRightsKey',
- 'NSMetadataItemSecurityMethodKey',
- 'NSMetadataItemSpeedKey',
- 'NSMetadataItemStarRatingKey',
- 'NSMetadataItemStateOrProvinceKey',
- 'NSMetadataItemStreamableKey',
- 'NSMetadataItemSubjectKey',
- 'NSMetadataItemTempoKey',
- 'NSMetadataItemTextContentKey',
- 'NSMetadataItemThemeKey',
- 'NSMetadataItemTimeSignatureKey',
- 'NSMetadataItemTimestampKey',
- 'NSMetadataItemTitleKey',
- 'NSMetadataItemTotalBitRateKey',
- 'NSMetadataItemURLKey',
- 'NSMetadataItemVersionKey',
- 'NSMetadataItemVideoBitRateKey',
- 'NSMetadataItemWhereFromsKey',
- 'NSMetadataItemWhiteBalanceKey',
- 'NSMetadataQuery',
- 'NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope',
- 'NSMetadataQueryAttributeValueTuple',
- 'NSMetadataQueryDelegate',
- 'NSMetadataQueryIndexedLocalComputerScope',
- 'NSMetadataQueryIndexedNetworkScope',
- 'NSMetadataQueryLocalComputerScope',
- 'NSMetadataQueryNetworkScope',
- 'NSMetadataQueryResultContentRelevanceAttribute',
- 'NSMetadataQueryResultGroup',
- 'NSMetadataQueryUbiquitousDataScope',
- 'NSMetadataQueryUbiquitousDocumentsScope',
- 'NSMetadataQueryUpdateAddedItemsKey',
- 'NSMetadataQueryUpdateChangedItemsKey',
- 'NSMetadataQueryUpdateRemovedItemsKey',
- 'NSMetadataQueryUserHomeScope',
- 'NSMetadataUbiquitousItemContainerDisplayNameKey',
- 'NSMetadataUbiquitousItemDownloadRequestedKey',
- 'NSMetadataUbiquitousItemDownloadingErrorKey',
- 'NSMetadataUbiquitousItemDownloadingStatusCurrent',
- 'NSMetadataUbiquitousItemDownloadingStatusDownloaded',
- 'NSMetadataUbiquitousItemDownloadingStatusKey',
- 'NSMetadataUbiquitousItemDownloadingStatusNotDownloaded',
- 'NSMetadataUbiquitousItemHasUnresolvedConflictsKey',
- 'NSMetadataUbiquitousItemIsDownloadedKey',
- 'NSMetadataUbiquitousItemIsDownloadingKey',
- 'NSMetadataUbiquitousItemIsExternalDocumentKey',
- 'NSMetadataUbiquitousItemIsSharedKey',
- 'NSMetadataUbiquitousItemIsUploadedKey',
- 'NSMetadataUbiquitousItemIsUploadingKey',
- 'NSMetadataUbiquitousItemPercentDownloadedKey',
- 'NSMetadataUbiquitousItemPercentUploadedKey',
- 'NSMetadataUbiquitousItemURLInLocalContainerKey',
- 'NSMetadataUbiquitousItemUploadingErrorKey',
- 'NSMetadataUbiquitousSharedItemCurrentUserPermissionsKey',
- 'NSMetadataUbiquitousSharedItemCurrentUserRoleKey',
- 'NSMetadataUbiquitousSharedItemMostRecentEditorNameComponentsKey',
- 'NSMetadataUbiquitousSharedItemOwnerNameComponentsKey',
- 'NSMetadataUbiquitousSharedItemPermissionsReadOnly',
- 'NSMetadataUbiquitousSharedItemPermissionsReadWrite',
- 'NSMetadataUbiquitousSharedItemRoleOwner',
- 'NSMetadataUbiquitousSharedItemRoleParticipant',
'NSMiddleSpecifier',
'NSMoveCommand',
'NSMutableArray',
@@ -1018,290 +672,47 @@ const SWIFT_TYPES = new Set([
'NSNameSpecifier',
'NSNoScriptError',
'NSNoSpecifierError',
- 'NSNoTopLevelContainersSpecifierError',
- 'NSNonLossyASCIIStringEncoding',
- 'NSNonOwnedPointerHashCallBacks',
- 'NSNonOwnedPointerMapKeyCallBacks',
- 'NSNonOwnedPointerMapValueCallBacks',
- 'NSNonOwnedPointerOrNullMapKeyCallBacks',
- 'NSNonRetainedObjectHashCallBacks',
- 'NSNonRetainedObjectMapKeyCallBacks',
- 'NSNonRetainedObjectMapValueCallBacks',
'NSNotFound',
'NSNotification',
- 'NSNotificationDeliverImmediately',
- 'NSNotificationPostToAllSessions',
'NSNull',
'NSNumber',
- 'NSOSF1OperatingSystem',
- 'NSOSStatusErrorDomain',
'NSObject',
- 'NSObjectHashCallBacks',
- 'NSObjectMapKeyCallBacks',
- 'NSObjectMapValueCallBacks',
'NSObjectProtocol',
- 'NSOpenStepUnicodeReservedBase',
- 'NSOperationNotSupportedForKeyException',
- 'NSOperationNotSupportedForKeyScriptError',
- 'NSOperationNotSupportedForKeySpecifierError',
'NSOrderedSet',
'NSOrthography',
- 'NSOwnedObjectIdentityHashCallBacks',
- 'NSOwnedPointerHashCallBacks',
- 'NSOwnedPointerMapKeyCallBacks',
- 'NSOwnedPointerMapValueCallBacks',
- 'NSPOSIXErrorDomain',
- 'NSPersianCalendar',
- 'NSPersonNameComponentDelimiter',
- 'NSPersonNameComponentFamilyName',
- 'NSPersonNameComponentGivenName',
- 'NSPersonNameComponentKey',
- 'NSPersonNameComponentMiddleName',
- 'NSPersonNameComponentNickname',
- 'NSPersonNameComponentPrefix',
- 'NSPersonNameComponentSuffix',
- 'NSPersonNameComponents',
'NSPoint',
'NSPointerArray',
'NSPointerFunctions',
- 'NSPointerToStructHashCallBacks',
'NSPositionalSpecifier',
'NSPredicate',
- 'NSPropertyListErrorMaximum',
- 'NSPropertyListErrorMinimum',
- 'NSPropertyListReadCorruptError',
- 'NSPropertyListReadStreamError',
- 'NSPropertyListReadUnknownVersionError',
- 'NSPropertyListWriteInvalidError',
- 'NSPropertyListWriteStreamError',
- 'NSPropertySpecifier',
- 'NSProprietaryStringEncoding',
- 'NSProtocolChecker',
'NSProxy',
'NSPurgeableData',
'NSQuitCommand',
'NSRandomSpecifier',
'NSRange',
- 'NSRangeSpecifier',
- 'NSReceiverEvaluationScriptError',
- 'NSReceiversCantHandleCommandScriptError',
- 'NSRecoveryAttempterErrorKey',
'NSRect',
'NSRecursiveLock',
'NSRegularExpression',
'NSRelativeSpecifier',
- 'NSRepublicOfChinaCalendar',
- 'NSRequiredArgumentsMissingScriptError',
- 'NSSaveOptions',
'NSScannedOption',
- 'NSScriptClassDescription',
- 'NSScriptCoercionHandler',
- 'NSScriptCommand',
- 'NSScriptCommandDescription',
- 'NSScriptExecutionContext',
- 'NSScriptObjectSpecifier',
- 'NSScriptSuiteRegistry',
- 'NSScriptWhoseTest',
'NSSecureCoding',
'NSSet',
'NSSetCommand',
- 'NSShiftJISStringEncoding',
'NSSize',
- 'NSSolarisOperatingSystem',
'NSSortDescriptor',
- 'NSSortOptions',
'NSSpecifierTest',
'NSSpellServer',
'NSSpellServerDelegate',
'NSStreamSOCKSErrorDomain',
'NSStreamSocketSSLErrorDomain',
'NSString',
- 'NSStringEncodingErrorKey',
- 'NSSunOSOperatingSystem',
- 'NSSwappedDouble',
- 'NSSwappedFloat',
- 'NSSymbolStringEncoding',
- 'NSTextCheckingAllCustomTypes',
- 'NSTextCheckingAllSystemTypes',
- 'NSTextCheckingAllTypes',
- 'NSTextCheckingKey',
- 'NSTextCheckingResult',
- 'NSTextCheckingTypes',
- 'NSTextWritingDirection',
- 'NSTimeIntervalSince1970',
'NSTimeZone',
- 'NSTypeIdentifierAddressText',
- 'NSTypeIdentifierDateText',
- 'NSTypeIdentifierPhoneNumberText',
- 'NSTypeIdentifierTransitInformationText',
'NSURL',
- 'NSURLAuthenticationMethodClientCertificate',
- 'NSURLAuthenticationMethodDefault',
- 'NSURLAuthenticationMethodHTMLForm',
- 'NSURLAuthenticationMethodHTTPBasic',
- 'NSURLAuthenticationMethodHTTPDigest',
- 'NSURLAuthenticationMethodNTLM',
- 'NSURLAuthenticationMethodNegotiate',
- 'NSURLAuthenticationMethodServerTrust',
- 'NSURLComponents',
- 'NSURLConnection',
- 'NSURLConnectionDataDelegate',
- 'NSURLConnectionDelegate',
- 'NSURLConnectionDownloadDelegate',
- 'NSURLCredentialStorageRemoveSynchronizableCredentials',
- 'NSURLDownload',
- 'NSURLDownloadDelegate',
- 'NSURLErrorAppTransportSecurityRequiresSecureConnection',
- 'NSURLErrorBackgroundSessionInUseByAnotherProcess',
- 'NSURLErrorBackgroundSessionRequiresSharedContainer',
- 'NSURLErrorBackgroundSessionWasDisconnected',
- 'NSURLErrorBackgroundTaskCancelledReasonKey',
- 'NSURLErrorBadServerResponse',
- 'NSURLErrorBadURL',
- 'NSURLErrorCallIsActive',
- 'NSURLErrorCancelled',
- 'NSURLErrorCancelledReasonBackgroundUpdatesDisabled',
- 'NSURLErrorCancelledReasonInsufficientSystemResources',
- 'NSURLErrorCancelledReasonUserForceQuitApplication',
- 'NSURLErrorCannotCloseFile',
- 'NSURLErrorCannotConnectToHost',
- 'NSURLErrorCannotCreateFile',
- 'NSURLErrorCannotDecodeContentData',
- 'NSURLErrorCannotDecodeRawData',
- 'NSURLErrorCannotFindHost',
- 'NSURLErrorCannotLoadFromNetwork',
- 'NSURLErrorCannotMoveFile',
- 'NSURLErrorCannotOpenFile',
- 'NSURLErrorCannotParseResponse',
- 'NSURLErrorCannotRemoveFile',
- 'NSURLErrorCannotWriteToFile',
- 'NSURLErrorClientCertificateRejected',
- 'NSURLErrorClientCertificateRequired',
- 'NSURLErrorDNSLookupFailed',
- 'NSURLErrorDataLengthExceedsMaximum',
- 'NSURLErrorDataNotAllowed',
- 'NSURLErrorDomain',
- 'NSURLErrorDownloadDecodingFailedMidStream',
- 'NSURLErrorDownloadDecodingFailedToComplete',
- 'NSURLErrorFailingURLErrorKey',
- 'NSURLErrorFailingURLPeerTrustErrorKey',
- 'NSURLErrorFailingURLStringErrorKey',
- 'NSURLErrorFileDoesNotExist',
- 'NSURLErrorFileIsDirectory',
- 'NSURLErrorFileOutsideSafeArea',
- 'NSURLErrorHTTPTooManyRedirects',
- 'NSURLErrorInternationalRoamingOff',
- 'NSURLErrorKey',
- 'NSURLErrorNetworkConnectionLost',
- 'NSURLErrorNetworkUnavailableReasonKey',
- 'NSURLErrorNoPermissionsToReadFile',
- 'NSURLErrorNotConnectedToInternet',
- 'NSURLErrorRedirectToNonExistentLocation',
- 'NSURLErrorRequestBodyStreamExhausted',
- 'NSURLErrorResourceUnavailable',
- 'NSURLErrorSecureConnectionFailed',
- 'NSURLErrorServerCertificateHasBadDate',
- 'NSURLErrorServerCertificateHasUnknownRoot',
- 'NSURLErrorServerCertificateNotYetValid',
- 'NSURLErrorServerCertificateUntrusted',
- 'NSURLErrorTimedOut',
- 'NSURLErrorUnknown',
- 'NSURLErrorUnsupportedURL',
- 'NSURLErrorUserAuthenticationRequired',
- 'NSURLErrorUserCancelledAuthentication',
- 'NSURLErrorZeroByteResource',
- 'NSURLFileScheme',
- 'NSURLHandle',
- 'NSURLProtectionSpaceFTP',
- 'NSURLProtectionSpaceFTPProxy',
- 'NSURLProtectionSpaceHTTP',
- 'NSURLProtectionSpaceHTTPProxy',
- 'NSURLProtectionSpaceHTTPS',
- 'NSURLProtectionSpaceHTTPSProxy',
- 'NSURLProtectionSpaceSOCKSProxy',
- 'NSURLQueryItem',
'NSURLRequest',
- 'NSURLSessionDownloadTaskResumeData',
- 'NSURLSessionTransferSizeUnknown',
- 'NSUTF16BigEndianStringEncoding',
- 'NSUTF16LittleEndianStringEncoding',
- 'NSUTF16StringEncoding',
- 'NSUTF32BigEndianStringEncoding',
- 'NSUTF32LittleEndianStringEncoding',
- 'NSUTF32StringEncoding',
- 'NSUTF8StringEncoding',
'NSUUID',
- 'NSUbiquitousFileErrorMaximum',
- 'NSUbiquitousFileErrorMinimum',
- 'NSUbiquitousFileNotUploadedDueToQuotaError',
- 'NSUbiquitousFileUbiquityServerNotAvailable',
- 'NSUbiquitousFileUnavailableError',
- 'NSUbiquitousKeyValueStore',
- 'NSUbiquitousKeyValueStoreAccountChange',
- 'NSUbiquitousKeyValueStoreChangeReasonKey',
- 'NSUbiquitousKeyValueStoreChangedKeysKey',
- 'NSUbiquitousKeyValueStoreInitialSyncChange',
- 'NSUbiquitousKeyValueStoreQuotaViolationChange',
- 'NSUbiquitousKeyValueStoreServerChange',
'NSUnarchiver',
- 'NSUncaughtExceptionHandler',
- 'NSUndefinedDateComponent',
- 'NSUnderlineStyle',
- 'NSUnderlyingErrorKey',
- 'NSUndoCloseGroupingRunLoopOrdering',
- 'NSUndoManagerGroupIsDiscardableKey',
- 'NSUnicodeStringEncoding',
- 'NSUniqueIDSpecifier',
- 'NSUnknownKeyScriptError',
- 'NSUnknownKeySpecifierError',
- 'NSUserActivity',
- 'NSUserActivityConnectionUnavailableError',
- 'NSUserActivityDelegate',
- 'NSUserActivityErrorMaximum',
- 'NSUserActivityErrorMinimum',
- 'NSUserActivityHandoffFailedError',
- 'NSUserActivityHandoffUserInfoTooLargeError',
- 'NSUserActivityPersistentIdentifier',
- 'NSUserActivityRemoteApplicationTimedOutError',
- 'NSUserActivityTypeBrowsingWeb',
- 'NSUserAppleScriptTask',
- 'NSUserAutomatorTask',
- 'NSUserCancelledError',
- 'NSUserNotification',
- 'NSUserNotificationAction',
- 'NSUserNotificationCenter',
- 'NSUserNotificationCenterDelegate',
- 'NSUserNotificationDefaultSoundName',
- 'NSUserScriptTask',
- 'NSUserUnixTask',
- 'NSValidationErrorMaximum',
- 'NSValidationErrorMinimum',
'NSValue',
- 'NSValueTransformerName',
'NSWhoseSpecifier',
- 'NSWindows95OperatingSystem',
- 'NSWindowsCP1250StringEncoding',
- 'NSWindowsCP1251StringEncoding',
- 'NSWindowsCP1252StringEncoding',
- 'NSWindowsCP1253StringEncoding',
- 'NSWindowsCP1254StringEncoding',
- 'NSWindowsNTOperatingSystem',
- 'NSWrapCalendarComponents',
- 'NSWritingDirectionFormatType',
- 'NSXPCConnection',
- 'NSXPCConnectionErrorMaximum',
- 'NSXPCConnectionErrorMinimum',
- 'NSXPCConnectionInterrupted',
- 'NSXPCConnectionInvalid',
- 'NSXPCConnectionReplyInvalid',
- 'NSXPCInterface',
- 'NSXPCListener',
- 'NSXPCListenerDelegate',
- 'NSXPCListenerEndpoint',
- 'NSXPCProxyCreating',
- 'NS_BigEndian',
- 'NS_LittleEndian',
- 'NS_UnknownByteOrder',
'Name',
'NameStyle',
'NetService',
@@ -1311,8 +722,6 @@ const SWIFT_TYPES = new Set([
'NetworkServiceType',
'NetworkUnavailableReason',
'Never',
- 'NonConformingFloatDecodingStrategy',
- 'NonConformingFloatEncodingStrategy',
'Notification',
'NotificationCenter',
'NotificationCoalescing',
@@ -1328,9 +737,7 @@ const SWIFT_TYPES = new Set([
'Operation',
'OperationQueue',
'Operator',
- 'OptionSet',
'Optional',
- 'Options',
'Output',
'OutputFormatting',
'OutputStream',
@@ -1343,8 +750,6 @@ const SWIFT_TYPES = new Set([
'PartialRangeThrough',
'PartialRangeUpTo',
'Persistence',
- 'PersonNameComponents',
- 'PersonNameComponentsFormatter',
'Pipe',
'PlaygroundQuickLook',
'Pointee',
@@ -1352,7 +757,6 @@ const SWIFT_TYPES = new Set([
'PortDelegate',
'PortMessage',
'PostingStyle',
- 'PreferredPresentationStyle',
'PrefixSequence',
'Process',
'ProcessInfo',
@@ -1362,10 +766,6 @@ const SWIFT_TYPES = new Set([
'ProgressUserInfoKey',
'Properties',
'PropertyKey',
- 'PropertyListDecoder',
- 'PropertyListEncoder',
- 'PropertyListFormat',
- 'PropertyListSerialization',
'Published',
'Publisher',
'PublishingHandler',
@@ -1381,8 +781,6 @@ const SWIFT_TYPES = new Set([
'RawRepresentable',
'RawSignificand',
'RawValue',
- 'ReadOptions',
- 'ReadingOptions',
'RecoverableError',
'ReferenceConvertible',
'ReferenceType',
@@ -1391,7 +789,6 @@ const SWIFT_TYPES = new Set([
'RelativePosition',
'Repeated',
'RepeatedTimePolicy',
- 'ReplacingOptions',
'ResourceFetchType',
'ResponseDisposition',
'Result',
@@ -1419,13 +816,10 @@ const SWIFT_TYPES = new Set([
'SIMDStorage',
'Scalar',
'Scanner',
- 'SchedulerOptions',
'SchedulerTimeType',
'SearchDirection',
- 'SearchOptions',
'SearchPathDirectory',
'SearchPathDomainMask',
- 'SendOptions',
'Sequence',
'Set',
'SetAlgebra',
@@ -1433,8 +827,6 @@ const SWIFT_TYPES = new Set([
'SetIterator',
'SignedInteger',
'SignedNumeric',
- 'SingleValueDecodingContainer',
- 'SingleValueEncodingContainer',
'Slice',
'SocketNativeHandle',
'SocketPort',
@@ -1445,10 +837,6 @@ const SWIFT_TYPES = new Set([
'StoragePolicy',
'Stream',
'StreamDelegate',
- 'StreamNetworkServiceTypeValue',
- 'StreamSOCKSProxyConfiguration',
- 'StreamSOCKSProxyVersion',
- 'StreamSocketSecurityLevel',
'Stride',
'StrideThrough',
'StrideThroughIterator',
@@ -1456,7 +844,6 @@ const SWIFT_TYPES = new Set([
'StrideToIterator',
'Strideable',
'String',
- 'StringEncodingDetectionOptionsKey',
'StringInterpolation',
'StringInterpolationProtocol',
'StringLiteralType',
@@ -1482,15 +869,12 @@ const SWIFT_TYPES = new Set([
'Timer',
'TimerPublisher',
'Tuple',
- 'UIItemProviderPresentationSizeProviding',
'UInt',
'UInt16',
'UInt32',
'UInt64',
'UInt8',
'URL',
- 'URLAuthenticationChallenge',
- 'URLAuthenticationChallengeSender',
'URLCache',
'URLComponents',
'URLCredential',
@@ -1508,25 +892,6 @@ const SWIFT_TYPES = new Set([
'URLResourceValues',
'URLResponse',
'URLSession',
- 'URLSessionConfiguration',
- 'URLSessionDataDelegate',
- 'URLSessionDataTask',
- 'URLSessionDelegate',
- 'URLSessionDownloadDelegate',
- 'URLSessionDownloadTask',
- 'URLSessionStreamDelegate',
- 'URLSessionStreamTask',
- 'URLSessionTask',
- 'URLSessionTaskDelegate',
- 'URLSessionTaskMetrics',
- 'URLSessionTaskTransactionMetrics',
- 'URLSessionUploadTask',
- 'URLSessionWebSocketDelegate',
- 'URLSessionWebSocketTask',
- 'URLThumbnailDictionaryItem',
- 'URLUbiquitousItemDownloadingStatus',
- 'URLUbiquitousSharedItemPermissions',
- 'URLUbiquitousSharedItemRole',
'UTF16',
'UTF16View',
'UTF32',
@@ -1534,7 +899,6 @@ const SWIFT_TYPES = new Set([
'UTF8View',
'UUID',
'UnboundedRange',
- 'UnboundedRange_',
'UndoManager',
'UnfoldFirstSequence',
'UnfoldSequence',
@@ -1555,17 +919,12 @@ const SWIFT_TYPES = new Set([
'UnitConverterLinear',
'UnitDispersion',
'UnitDuration',
- 'UnitElectricCharge',
- 'UnitElectricCurrent',
- 'UnitElectricPotentialDifference',
- 'UnitElectricResistance',
'UnitEnergy',
'UnitFrequency',
'UnitFuelEfficiency',
'UnitIlluminance',
'UnitLength',
'UnitMass',
- 'UnitOptions',
'UnitPower',
'UnitPressure',
'UnitSpeed',
@@ -1574,10 +933,7 @@ const SWIFT_TYPES = new Set([
'UnitVolume',
'Units',
'UnitsStyle',
- 'UnkeyedDecodingContainer',
- 'UnkeyedEncodingContainer',
'Unmanaged',
- 'UnmountOptions',
'UnpublishingHandler',
'UnsafeBufferPointer',
'UnsafeMutableBufferPointer',
@@ -1595,11 +951,7 @@ const SWIFT_TYPES = new Set([
'Values',
'Version',
'Void',
- 'VolumeEnumerationOptions',
'Words',
- 'WritableKeyPath',
- 'WriteOptions',
- 'WritingOptions',
'XMLDTD',
'XMLDTDNode',
'XMLDocument',
@@ -1607,25 +959,391 @@ const SWIFT_TYPES = new Set([
'XMLNode',
'XMLParser',
'XMLParserDelegate',
- 'ZeroFormattingBehavior',
'Zip2Sequence',
- '_AppendKeyPath',
'swift',
'unichar',
]);
class HighlightSwift extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static SLASH = 2;
+ static SLASH_SLASH = 3;
+ static SLASH_STAR = 4;
+ static SLASH_SLASH_BACKSLASH = 5;
+ static SLASH_STAR_STAR = 6;
+ static HASH = 7;
+ static DQUOTE = 8;
+ static DQUOTESTR = 9;
+ static DQUOTESTR_BACKSLASH = 10;
+ static DQUOTESTR_END = 11;
+ static DQUOTE2 = 12;
+ static DQUOTE3 = 13;
+ static DQUOTE3_BACKSLASH = 14;
+ static DQUOTE31 = 15;
+ static DQUOTE32 = 16;
+ static DQUOTE3_END = 17;
+ static REGEX = 18;
+ static REGEX_END = 19;
+ static REGEX_BACKSLASH = 20;
+
constructor(delegate) {
super(delegate);
+ this.hash1 = 0;
+ this.hash2 = 0;
+ this.word = '';
+ this.nest = [];
+ this.hash = [];
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightSwift.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightSwift.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightSwift.SLASH;
+ } else if (c == '"') {
+ this.state = HighlightSwift.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ this.hash1 = 0;
+ } else if (c == '#') {
+ this.state = HighlightSwift.HASH;
+ this.hash1 = 1;
+ } else if (c == '(' && this.nest) {
+ this.append('(');
+ this.nest.push(HighlightSwift.NORMAL);
+ this.hash.push(0);
+ } else if (c == ')' && this.nest) {
+ this.state = this.nest.pop();
+ this.hash1 = this.hash.pop();
+ if (this.state != HighlightSwift.NORMAL)
+ this.push("span", "string");
+ this.append(')');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightSwift.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (SWIFT_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightSwift.NORMAL);
+ }
+ break;
+
+ case HighlightSwift.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightSwift.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightSwift.SLASH_STAR;
+ } else {
+ this.push("span", "string");
+ this.append('/');
+ this.hash1 = 0;
+ this.epsilon(HighlightSwift.REGEX);
+ }
+ break;
+
+ case HighlightSwift.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightSwift.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightSwift.SLASH_SLASH_BACKSLASH;
+ }
+ break;
+
+ case HighlightSwift.SLASH_SLASH_BACKSLASH:
+ this.append(c);
+ this.state = HighlightSwift.SLASH_SLASH;
+ break;
+
+ case HighlightSwift.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightSwift.SLASH_STAR_STAR;
+ break;
+
+ case HighlightSwift.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightSwift.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightSwift.SLASH_STAR;
+ }
+ break;
+
+ case HighlightSwift.HASH:
+ if (c == '#') {
+ ++this.hash1;
+ } else if (c == '"') {
+ this.push("span", "string");
+ for (let i = 0; i < this.hash1; ++i)
+ this.append('#');
+ this.append('"');
+ this.state = HighlightSwift.DQUOTE;
+ } else if (c == '/') {
+ this.push("span", "string");
+ for (let i = 0; i < this.hash1; ++i)
+ this.append('#');
+ this.append('/');
+ this.state = HighlightSwift.REGEX;
+ } else {
+ for (let i = 0; i < this.hash1; ++i)
+ this.append('#');
+ this.epsilon(HighlightSwift.NORMAL);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightSwift.DQUOTE2;
+ this.hash2 = 0;
+ } else if (c == '\\') {
+ this.state = HighlightSwift.DQUOTESTR_BACKSLASH;
+ this.hash2 = 0;
+ } else {
+ this.state = HighlightSwift.DQUOTESTR;
+ }
+ break;
+
+ case HighlightSwift.DQUOTESTR:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightSwift.DQUOTESTR_END;
+ this.hash2 = 0;
+ } else if (c == '\\') {
+ this.state = HighlightSwift.DQUOTESTR_BACKSLASH;
+ this.hash2 = 0;
+ }
+ break;
+
+ case HighlightSwift.DQUOTESTR_END:
+ if (this.hash2 == this.hash1) {
+ this.pop();
+ this.epsilon(HighlightSwift.NORMAL);
+ } else if (c == '#') {
+ this.append('#');
+ ++this.hash2;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTESTR);
+ }
+ break;
+
+ case HighlightSwift.DQUOTESTR_BACKSLASH:
+ if (c == '#' && this.hash2 < this.hash1) {
+ this.append('#');
+ ++this.hash2;
+ } else if (c == '(' && this.hash2 == this.hash1) {
+ this.append('(');
+ this.pop();
+ this.nest.push(HighlightSwift.DQUOTESTR);
+ this.hash.push(this.hash1);
+ this.state = HighlightSwift.NORMAL;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTESTR);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE2:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightSwift.DQUOTE3;
+ } else if (c == '#' && this.hash2 < this.hash1) {
+ this.append('#');
+ ++this.hash2;
+ } else if (this.hash2 == this.hash1) {
+ this.pop();
+ this.epsilon(HighlightSwift.NORMAL);
+ } else {
+ this.epsilon(HighlightSwift.DQUOTESTR);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE3:
+ this.append(c);
+ if (c == '"') {
+ this.state = HighlightSwift.DQUOTE31;
+ } else if (c == '\\') {
+ this.state = HighlightSwift.DQUOTE3_BACKSLASH;
+ this.hash2 = 0;
+ }
+ break;
+
+ case HighlightSwift.DQUOTE31:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightSwift.DQUOTE32;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTE3);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE32:
+ if (c == '"') {
+ this.append('"');
+ this.state = HighlightSwift.DQUOTESTR_END;
+ this.hash2 = 0;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTE3);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE3_BACKSLASH:
+ if (c == '#' && this.hash2 < this.hash1) {
+ this.append('#');
+ ++this.hash2;
+ } else if (c == '(' && this.hash2 == this.hash1) {
+ this.append('(');
+ this.pop();
+ this.nest.push(HighlightSwift.DQUOTE3);
+ this.hash.push(this.hash1);
+ this.state = HighlightSwift.NORMAL;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTE3);
+ }
+ break;
+
+ case HighlightSwift.DQUOTE3_END:
+ if (this.hash2 == this.hash1) {
+ this.pop();
+ this.epsilon(HighlightSwift.NORMAL);
+ } else if (c == '#') {
+ this.append('#');
+ ++this.hash2;
+ } else {
+ this.epsilon(HighlightSwift.DQUOTE3);
+ }
+ break;
+
+ case HighlightSwift.REGEX:
+ this.append(c);
+ if (c == '/') {
+ this.state = HighlightSwift.REGEX_END;
+ this.hash2 = 0;
+ } else if (c == '\\') {
+ this.state = HighlightSwift.REGEX_BACKSLASH;
+ this.hash2 = 0;
+ }
+ break;
+
+ case HighlightSwift.REGEX_END:
+ if (this.hash2 == this.hash1) {
+ this.pop();
+ this.epsilon(HighlightSwift.NORMAL);
+ } else if (c == '#') {
+ this.append('#');
+ ++this.hash2;
+ } else {
+ this.epsilon(HighlightSwift.REGEX);
+ }
+ break;
+
+ case HighlightSwift.REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightSwift.REGEX;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightSwift.WORD:
+ if (SWIFT_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (SWIFT_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightSwift.SLASH:
+ this.append('/');
+ break;
+ case HighlightSwift.HASH:
+ for (let i = 0; i < this.hash1; ++i)
+ this.append('#');
+ break;
+ case HighlightSwift.SLASH_SLASH:
+ case HighlightSwift.SLASH_SLASH_BACKSLASH:
+ case HighlightSwift.SLASH_STAR:
+ case HighlightSwift.SLASH_STAR_STAR:
+ case HighlightSwift.DQUOTE:
+ case HighlightSwift.DQUOTESTR:
+ case HighlightSwift.DQUOTESTR_BACKSLASH:
+ case HighlightSwift.DQUOTESTR_END:
+ case HighlightSwift.DQUOTE2:
+ case HighlightSwift.DQUOTE3:
+ case HighlightSwift.DQUOTE3_BACKSLASH:
+ case HighlightSwift.DQUOTE31:
+ case HighlightSwift.DQUOTE32:
+ case HighlightSwift.DQUOTE3_END:
+ case HighlightSwift.REGEX:
+ case HighlightSwift.REGEX_END:
+ case HighlightSwift.REGEX_BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightSwift.NORMAL;
+ this.nest = [];
+ this.hash = [];
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_tcl.js b/llamafile/server/www/highlight_tcl.js
index 6a05a777..348c967d 100644
--- a/llamafile/server/www/highlight_tcl.js
+++ b/llamafile/server/www/highlight_tcl.js
@@ -127,16 +127,184 @@ const TCL_TYPES = new Set([
class HighlightTcl extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static DQUOTE = 2;
+ static DQUOTE_BACKSLASH = 3;
+ static VAR = 4;
+ static VAR2 = 5;
+ static VAR_CURLY = 6;
+ static COMMENT = 7;
+ static COMMENT_BACKSLASH = 8;
+ static BACKSLASH = 9;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightTcl.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightTcl.WORD;
+ this.word += c;
+ } else if (c == '"') {
+ this.state = HighlightTcl.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else if (c == '$') {
+ this.append('$');
+ this.state = HighlightTcl.VAR;
+ } else if (c == '#') {
+ this.push("span", "comment");
+ this.append('#');
+ this.state = HighlightTcl.COMMENT;
+ } else if (c == '\\') {
+ this.state = HighlightTcl.BACKSLASH;
+ this.push("span", "escape");
+ this.append('\\');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTcl.WORD:
+ if (!(isspace(c) || c == ';')) {
+ this.word += c;
+ } else {
+ if (TCL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (TCL_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (TCL_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightTcl.NORMAL);
+ }
+ break;
+
+ case HighlightTcl.BACKSLASH:
+ this.append(c);
+ this.pop();
+ this.state = HighlightTcl.NORMAL;
+ break;
+
+ case HighlightTcl.VAR:
+ if (c == '{') {
+ this.append('{');
+ this.push("span", "var");
+ this.state = HighlightTcl.VAR_CURLY;
+ break;
+ } else {
+ this.push("span", "var");
+ this.state = HighlightTcl.VAR2;
+ }
+ // fallthrough
+
+ case HighlightTcl.VAR2:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightTcl.NORMAL);
+ }
+ break;
+
+ case HighlightTcl.VAR_CURLY:
+ if (c == '}') {
+ this.pop();
+ this.append('}');
+ this.state = HighlightTcl.NORMAL;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTcl.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightTcl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightTcl.COMMENT_BACKSLASH;
+ }
+ break;
+
+ case HighlightTcl.COMMENT_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTcl.COMMENT;
+ break;
+
+ case HighlightTcl.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightTcl.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightTcl.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightTcl.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTcl.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightTcl.WORD:
+ if (TCL_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (TCL_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (TCL_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightTcl.VAR2:
+ case HighlightTcl.VAR_CURLY:
+ case HighlightTcl.DQUOTE:
+ case HighlightTcl.DQUOTE_BACKSLASH:
+ case HighlightTcl.COMMENT:
+ case HighlightTcl.COMMENT_BACKSLASH:
+ case HighlightTcl.BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightTcl.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_tex.js b/llamafile/server/www/highlight_tex.js
index 7d4398ef..c06e95d4 100644
--- a/llamafile/server/www/highlight_tex.js
+++ b/llamafile/server/www/highlight_tex.js
@@ -14,16 +14,176 @@
class HighlightTex extends Highlighter {
+ static NORMAL = 0;
+ static BACKSLASH = 1;
+ static COMMAND = 2;
+ static COMMENT = 3;
+ static DOLLAR = 4;
+ static MATH = 5;
+ static MATH_BACKSLASH = 6;
+ static BACKTICK = 7;
+ static STRING = 8;
+ static STRING_QUOTE = 9;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightTex.NORMAL:
+ if (c == '\\') {
+ this.state = HighlightTex.BACKSLASH;
+ } else if (c == '$') {
+ this.state = HighlightTex.DOLLAR;
+ } else if (c == '`') {
+ this.state = HighlightTex.BACKTICK;
+ } else if (c == '%') {
+ this.state = HighlightTex.COMMENT;
+ this.push("span", "comment");
+ this.append('%');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTex.BACKSLASH:
+ if (c == '\\') {
+ this.push("span", "warning");
+ this.append("\\\\");
+ this.pop();
+ this.state = HighlightTex.NORMAL;
+ } else if (isspace(c)) {
+ this.append('\\');
+ this.epsilon(HighlightTex.NORMAL);
+ } else if (isalpha(c) || c == '@') {
+ this.push("span", "keyword");
+ this.append('\\');
+ this.append(c);
+ this.state = HighlightTex.COMMAND;
+ } else {
+ this.push("span", "escape");
+ this.append('\\');
+ this.append(c);
+ this.pop();
+ this.state = HighlightTex.NORMAL;
+ }
+ break;
+
+ case HighlightTex.COMMAND:
+ if (isalpha(c) || c == '@') {
+ this.append(c);
+ } else {
+ this.pop();
+ this.epsilon(HighlightTex.NORMAL);
+ }
+ break;
+
+ case HighlightTex.DOLLAR:
+ if (c == '$') {
+ this.append("$$");
+ this.state = HighlightTex.NORMAL;
+ } else if (c == '\\') {
+ this.push("span", "math");
+ this.append("$\\");
+ this.state = HighlightTex.MATH_BACKSLASH;
+ } else {
+ this.push("span", "math");
+ this.append("$");
+ this.append(c);
+ this.state = HighlightTex.MATH;
+ }
+ break;
+
+ case HighlightTex.MATH:
+ if (c == '$') {
+ this.append("$");
+ this.pop();
+ this.state = HighlightTex.NORMAL;
+ } else if (c == '\\') {
+ this.append('\\');
+ this.state = HighlightTex.MATH_BACKSLASH;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTex.MATH_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTex.MATH;
+ break;
+
+ case HighlightTex.COMMENT:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightTex.NORMAL;
+ }
+ break;
+
+ case HighlightTex.BACKTICK:
+ if (c == '`') {
+ this.push("span", "string");
+ this.append("``");
+ this.state = HighlightTex.STRING;
+ } else {
+ this.append('`');
+ this.epsilon(HighlightTex.NORMAL);
+ }
+ break;
+
+ case HighlightTex.STRING:
+ this.append(c);
+ if (c == '\'')
+ this.state = HighlightTex.STRING_QUOTE;
+ break;
+
+ case HighlightTex.STRING_QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightTex.NORMAL;
+ } else {
+ this.state = HighlightTex.STRING;
+ }
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightTex.BACKTICK:
+ this.append('`');
+ break;
+ case HighlightTex.DOLLAR:
+ this.append('$');
+ break;
+ case HighlightTex.BACKSLASH:
+ this.append('\\');
+ break;
+ case HighlightTex.COMMAND:
+ case HighlightTex.COMMENT:
+ case HighlightTex.MATH:
+ case HighlightTex.MATH_BACKSLASH:
+ case HighlightTex.STRING:
+ case HighlightTex.STRING_QUOTE:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightTex.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/highlight_typescript.js b/llamafile/server/www/highlight_typescript.js
index f2fa2cbf..0ba1cdf2 100644
--- a/llamafile/server/www/highlight_typescript.js
+++ b/llamafile/server/www/highlight_typescript.js
@@ -86,16 +86,325 @@ const TYPESCRIPT_TYPES = new Set([
class HighlightTypescript extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static SLASH_STAR = 8;
+ static SLASH_STAR_STAR = 9;
+ static TICK = 10;
+ static TICK_BACKSLASH = 11;
+ static TICK_DOLLAR = 12;
+ static REGEX = 13;
+ static REGEX_BACKSLASH = 14;
+ static REGEX_SQUARE = 15;
+ static REGEX_SQUARE_BACKSLASH = 16;
+
+ static EXPECT_VALUE = 0;
+ static EXPECT_OPERATOR = 1;
+
constructor(delegate) {
super(delegate);
+ this.expect = 0;
+ this.word = '';
+ this.nest = [];
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightTypescript.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ this.state = HighlightTypescript.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightTypescript.SLASH;
+ } else if (c == '\'') {
+ this.state = HighlightTypescript.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ } else if (c == '"') {
+ this.state = HighlightTypescript.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ } else if (c == '`') {
+ this.state = HighlightTypescript.TICK;
+ this.push("span", "string");
+ this.append('`');
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ } else if (c == '{' && this.nest) {
+ this.expect = HighlightTypescript.EXPECT_VALUE;
+ this.append('{');
+ this.nest.push(HighlightTypescript.NORMAL);
+ } else if (c == '}' && this.nest) {
+ if ((this.state = this.nest.pop()) != HighlightTypescript.NORMAL)
+ this.push("span", "string");
+ this.append('}');
+ } else if (c == ')' || c == '}' || c == ']') {
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ this.append(c);
+ } else if (ispunct(c)) {
+ this.expect = HighlightTypescript.EXPECT_VALUE;
+ this.append(c);
+ } else if (isdigit(c) || c == '.') {
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ this.append(c);
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTypescript.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (TYPESCRIPT_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightTypescript.EXPECT_VALUE;
+ } else if (TYPESCRIPT_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightTypescript.EXPECT_VALUE;
+ } else if (JS_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ } else if (JS_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ } else {
+ this.append(this.word);
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ }
+ this.word = '';
+ this.epsilon(HighlightTypescript.NORMAL);
+ }
+ break;
+
+ case HighlightTypescript.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightTypescript.SLASH_SLASH;
+ } else if (c == '*') {
+ this.push("span", "comment");
+ this.append("/*");
+ this.state = HighlightTypescript.SLASH_STAR;
+ } else if (expect_ == HighlightTypescript.EXPECT_VALUE) {
+ this.expect = HighlightTypescript.EXPECT_OPERATOR;
+ this.push("span", "string");
+ this.append('/');
+ this.append(c);
+ if (c == '\\') {
+ this.state = HighlightTypescript.REGEX_BACKSLASH;
+ } else if (c == '[') {
+ this.state = HighlightTypescript.REGEX_SQUARE;
+ } else {
+ this.state = HighlightTypescript.REGEX;
+ }
+ } else {
+ this.append('/');
+ this.epsilon(HighlightTypescript.NORMAL);
+ }
+ break;
+
+ case HighlightTypescript.SLASH_SLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ }
+ break;
+
+ case HighlightTypescript.SLASH_STAR:
+ this.append(c);
+ if (c == '*')
+ this.state = HighlightTypescript.SLASH_STAR_STAR;
+ break;
+
+ case HighlightTypescript.SLASH_STAR_STAR:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ } else if (c != '*') {
+ this.state = HighlightTypescript.SLASH_STAR;
+ }
+ break;
+
+ case HighlightTypescript.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightTypescript.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightTypescript.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTypescript.QUOTE;
+ break;
+
+ case HighlightTypescript.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightTypescript.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightTypescript.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTypescript.DQUOTE;
+ break;
+
+ case HighlightTypescript.TICK:
+ if (c == '`') {
+ this.append('`');
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ } else if (c == '$') {
+ this.state = HighlightTypescript.TICK_DOLLAR;
+ } else if (c == '\\') {
+ this.append('\\');
+ this.state = HighlightTypescript.TICK_BACKSLASH;
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightTypescript.TICK_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTypescript.TICK;
+ break;
+
+ case HighlightTypescript.TICK_DOLLAR:
+ if (c == '{') {
+ this.push("span", "bold");
+ this.append('$');
+ this.pop();
+ this.append('{');
+ this.pop();
+ this.expect = HighlightTypescript.EXPECT_VALUE;
+ this.nest.push(HighlightTypescript.TICK);
+ this.state = HighlightTypescript.NORMAL;
+ } else {
+ this.push("span", "warning");
+ this.append('$');
+ this.pop();
+ this.epsilon(HighlightTypescript.TICK);
+ }
+ break;
+
+ case HighlightTypescript.REGEX:
+ this.append(c);
+ if (c == '/') {
+ this.pop();
+ this.state = HighlightTypescript.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightTypescript.REGEX_BACKSLASH;
+ } else if (c == '[') {
+ this.state = HighlightTypescript.REGEX_SQUARE;
+ }
+ break;
+
+ case HighlightTypescript.REGEX_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTypescript.REGEX;
+ break;
+
+ case HighlightTypescript.REGEX_SQUARE:
+ this.append(c);
+ if (c == '\\') {
+ this.state = HighlightTypescript.REGEX_SQUARE_BACKSLASH;
+ } else if (c == ']') {
+ this.state = HighlightTypescript.REGEX;
+ }
+ break;
+
+ case HighlightTypescript.REGEX_SQUARE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightTypescript.REGEX_SQUARE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightTypescript.WORD:
+ if (TYPESCRIPT_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (TYPESCRIPT_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (JS_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (JS_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightTypescript.SLASH:
+ this.append('/');
+ break;
+ case HighlightTypescript.TICK_DOLLAR:
+ this.append('$');
+ this.pop();
+ break;
+ case HighlightTypescript.TICK:
+ case HighlightTypescript.TICK_BACKSLASH:
+ case HighlightTypescript.QUOTE:
+ case HighlightTypescript.QUOTE_BACKSLASH:
+ case HighlightTypescript.DQUOTE:
+ case HighlightTypescript.DQUOTE_BACKSLASH:
+ case HighlightTypescript.SLASH_SLASH:
+ case HighlightTypescript.SLASH_STAR:
+ case HighlightTypescript.SLASH_STAR_STAR:
+ case HighlightTypescript.REGEX:
+ case HighlightTypescript.REGEX_BACKSLASH:
+ case HighlightTypescript.REGEX_SQUARE:
+ case HighlightTypescript.REGEX_SQUARE_BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightTypescript.NORMAL;
+ this.nest = [];
this.delegate.flush();
+ this.delta = 0;
}
}
diff --git a/llamafile/server/www/highlight_zig.js b/llamafile/server/www/highlight_zig.js
index 91d0abbf..e00bf387 100644
--- a/llamafile/server/www/highlight_zig.js
+++ b/llamafile/server/www/highlight_zig.js
@@ -279,16 +279,188 @@ const ZIG_TYPES = new Set([
class HighlightZig extends Highlighter {
+ static NORMAL = 0;
+ static WORD = 1;
+ static QUOTE = 2;
+ static QUOTE_BACKSLASH = 3;
+ static DQUOTE = 4;
+ static DQUOTE_BACKSLASH = 5;
+ static SLASH = 6;
+ static SLASH_SLASH = 7;
+ static BACKSLASH = 8;
+ static BACKSLASH_BACKSLASH = 9;
+
constructor(delegate) {
super(delegate);
+ this.word = '';
}
feed(input) {
- this.append(input);
+ for (let i = 0; i < input.length; i += this.delta) {
+ this.delta = 1;
+ let c = input[i];
+ switch (this.state) {
+
+ case HighlightZig.NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_' || c == '@') {
+ this.state = HighlightZig.WORD;
+ this.word += c;
+ } else if (c == '/') {
+ this.state = HighlightZig.SLASH;
+ } else if (c == '\\') {
+ this.state = HighlightZig.BACKSLASH;
+ } else if (c == '\'') {
+ this.state = HighlightZig.QUOTE;
+ this.push("span", "string");
+ this.append('\'');
+ } else if (c == '"') {
+ this.state = HighlightZig.DQUOTE;
+ this.push("span", "string");
+ this.append('"');
+ } else {
+ this.append(c);
+ }
+ break;
+
+ case HighlightZig.WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ this.word += c;
+ } else {
+ if (ZIG_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ this.epsilon(HighlightZig.NORMAL);
+ }
+ break;
+
+ case HighlightZig.BACKSLASH:
+ if (c == '\\') {
+ this.push("span", "string");
+ this.append("\\\\");
+ this.state = HighlightZig.BACKSLASH_BACKSLASH;
+ } else {
+ this.append('\\');
+ this.epsilon(HighlightZig.NORMAL);
+ }
+ break;
+
+ case HighlightZig.SLASH_SLASH:
+ case HighlightZig.BACKSLASH_BACKSLASH:
+ this.append(c);
+ if (c == '\n') {
+ this.pop();
+ this.state = HighlightZig.NORMAL;
+ }
+ break;
+
+ case HighlightZig.SLASH:
+ if (c == '/') {
+ this.push("span", "comment");
+ this.append("//");
+ this.state = HighlightZig.SLASH_SLASH;
+ } else {
+ this.append('/');
+ this.epsilon(HighlightZig.NORMAL);
+ }
+ break;
+
+ case HighlightZig.QUOTE:
+ this.append(c);
+ if (c == '\'') {
+ this.pop();
+ this.state = HighlightZig.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightZig.QUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightZig.QUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightZig.QUOTE;
+ break;
+
+ case HighlightZig.DQUOTE:
+ this.append(c);
+ if (c == '"') {
+ this.pop();
+ this.state = HighlightZig.NORMAL;
+ } else if (c == '\\') {
+ this.state = HighlightZig.DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case HighlightZig.DQUOTE_BACKSLASH:
+ this.append(c);
+ this.state = HighlightZig.DQUOTE;
+ break;
+
+ default:
+ throw new Error('Invalid state');
+ }
+ }
}
flush() {
+ switch (this.state) {
+ case HighlightZig.WORD:
+ if (ZIG_KEYWORDS.has(this.word)) {
+ this.push("span", "keyword");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_TYPES.has(this.word)) {
+ this.push("span", "type");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_BUILTINS.has(this.word)) {
+ this.push("span", "builtin");
+ this.append(this.word);
+ this.pop();
+ } else if (ZIG_CONSTANTS.has(this.word)) {
+ this.push("span", "constant");
+ this.append(this.word);
+ this.pop();
+ } else {
+ this.append(this.word);
+ }
+ this.word = '';
+ break;
+ case HighlightZig.SLASH:
+ this.append('/');
+ break;
+ case HighlightZig.BACKSLASH:
+ this.append('\\');
+ break;
+ case HighlightZig.QUOTE:
+ case HighlightZig.QUOTE_BACKSLASH:
+ case HighlightZig.DQUOTE:
+ case HighlightZig.DQUOTE_BACKSLASH:
+ case HighlightZig.SLASH_SLASH:
+ case HighlightZig.BACKSLASH_BACKSLASH:
+ this.pop();
+ break;
+ default:
+ break;
+ }
+ this.state = HighlightZig.NORMAL;
this.delegate.flush();
+ this.delta = 1;
}
}
diff --git a/llamafile/server/www/index.html b/llamafile/server/www/index.html
index 949759b0..c1df1913 100644
--- a/llamafile/server/www/index.html
+++ b/llamafile/server/www/index.html
@@ -8,10 +8,7 @@
<div class="chat-container">
<div class="chat-header">
- <h1>
- <img src="chatbot.png" alt="[logo]">
- llamafile
- </h1>
+ <h1><img src="chatbot.png" alt="[logo]"> llamafile</h1>
</div>
<div class="chat-messages" id="chat-messages">
<div class="message system">
@@ -26,6 +23,7 @@
<div class="chat-input-container">
<input type="text" class="chat-input" id="chat-input" placeholder="Type your message..." autocomplete="off">
<button class="send-button" id="send-button">Send</button>
+ <button class="stop-button" id="stop-button" style="display:none">Stop</button>
</div>
</div>