Skip to content

Distribute and run LLMs with a single file.

Improve Swift and TypeScript syntax highlighting

Justine Tunneyc6e603955065
diff --git a/llamafile/chatbot.cpp b/llamafile/chatbot.cpp
index 7fcbc47d..0a03f07e 100644
--- a/llamafile/chatbot.cpp
+++ b/llamafile/chatbot.cpp
@@ -200,14 +200,11 @@ static void on_help(const std::vector<std::string> &args) {
if (args.size() == 1) {
fprintf(stderr, "\
" BOLD "available commands" RESET "\n\
- /clear restart conversation\n\
/context print context window usage\n\
/dump [FILE] print or save context window to file\n\
/exit end program\n\
/help [COMMAND] show help\n\
/manual [on|off] toggle manual role mode\n\
- /pop restore context window size\n\
- /push push context window size to stack\n\
/stats print performance metrics\n\
");
} else if (args[1] == "context") {
@@ -309,23 +306,23 @@ static void on_context(const std::vector<std::string> &args) {
printf("use the `-c %d` flag at startup for maximum context\n", max_context);
}
-static void on_clear(const std::vector<std::string> &args) {
+static void print_stack(void) {
+ for (size_t i = g_stack.size(); i--;)
+ printf("%8d\n", g_stack[i]);
+}
+
+static void on_clear(const std::vector<std::string> &args) { // [experimental]
g_past = g_clear;
g_stack.clear();
g_history.resize(g_past);
}
-static void print_stack(void) {
- for (size_t i = g_stack.size(); i--;)
- printf("%12d\n", g_stack[i]);
-}
-
-static void on_push(const std::vector<std::string> &args) {
+static void on_push(const std::vector<std::string> &args) { // [experimental]
g_stack.push_back(g_past);
print_stack();
}
-static void on_pop(const std::vector<std::string> &args) {
+static void on_pop(const std::vector<std::string> &args) { // [experimental]
if (g_stack.empty()) {
fprintf(stderr, BRIGHT_RED "error: context length stack is empty" RESET "\n");
return;
@@ -373,14 +370,11 @@ static char *on_hint(const char *line, const char **ansi1, const char **ansi2) {
static void on_completion(const char *line, int pos, bestlineCompletions *comp) {
static const char *const kCompletions[] = {
- "/clear", // usage: /clear
"/context", // usage: /context
"/dump", // usage: /dump [FILE]
"/exit", // usage: /exit
"/help", // usage: /help [COMMAND]
"/manual", // usage: /manual [on|off]
- "/pop", // usage: /pop
- "/push", // usage: /push
"/stats", // usage: /stats
};
for (int i = 0; i < sizeof(kCompletions) / sizeof(*kCompletions); ++i)
diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cpp
index ef5e191d..cf266800 100644
--- a/llamafile/highlight.cpp
+++ b/llamafile/highlight.cpp
@@ -62,11 +62,11 @@ Highlight *Highlight::create(const std::string_view &lang) {
if (lang == "js" || //
lang == "json" || //
lang == "javascript")
- return new HighlightJs(is_keyword_js, nullptr);
+ return new HighlightJs;
if (lang == "ts" || //
lang == "typescript")
- return new HighlightJs(is_keyword_typescript, is_keyword_typescript_type);
+ return new HighlightTypescript;
if (lang == "java")
return new HighlightJava;
@@ -153,7 +153,7 @@ Highlight *Highlight::create(const std::string_view &lang) {
return new HighlightShell;
if (lang == "swift")
- return new HighlightC(is_keyword_swift, is_keyword_swift_type);
+ return new HighlightSwift;
if (lang == "d")
return new HighlightC(is_keyword_d);
diff --git a/llamafile/highlight.h b/llamafile/highlight.h
index 7ac97bfc..41b5fab0 100644
--- a/llamafile/highlight.h
+++ b/llamafile/highlight.h
@@ -100,6 +100,8 @@ is_keyword_f is_keyword_shell;
is_keyword_f is_keyword_shell_builtin;
is_keyword_f is_keyword_swift;
is_keyword_f is_keyword_swift_type;
+is_keyword_f is_keyword_swift_builtin;
+is_keyword_f is_keyword_swift_constant;
is_keyword_f is_keyword_d;
is_keyword_f is_keyword_zig;
is_keyword_f is_keyword_zig_type;
@@ -221,7 +223,7 @@ class HighlightGo : public Highlight {
class HighlightJs : public Highlight {
public:
- HighlightJs(is_keyword_f *is_keyword, is_keyword_f *is_type);
+ HighlightJs();
~HighlightJs() override;
void feed(std::string *result, std::string_view input) override;
void flush(std::string *result) override;
@@ -230,8 +232,23 @@ class HighlightJs : public Highlight {
int t_ = 0;
int expect_;
std::string word_;
- is_keyword_f *is_type_;
- is_keyword_f *is_keyword_;
+};
+
+class HighlightTypescript : public Highlight {
+ public:
+ HighlightTypescript();
+ ~HighlightTypescript() override;
+ void feed(std::string *result, std::string_view input) override;
+ void flush(std::string *result) override;
+
+ private:
+ int c_ = 0;
+ int u_ = 0;
+ int t_ = 0;
+ int expect_;
+ int nesti_ = 0;
+ std::string word_;
+ unsigned char nest_[16];
};
class HighlightPython : public Highlight {
@@ -656,3 +673,22 @@ class HighlightR : public Highlight {
int t_ = 0;
std::string word_;
};
+
+class HighlightSwift : public Highlight {
+ public:
+ HighlightSwift();
+ ~HighlightSwift() override;
+ void feed(std::string *result, std::string_view input) override;
+ void flush(std::string *result) override;
+
+ private:
+ int c_ = 0;
+ int u_ = 0;
+ int t_ = 0;
+ int hash1_ = 0;
+ int hash2_ = 0;
+ int nesti_ = 0;
+ std::string word_;
+ unsigned char nest_[16];
+ unsigned char hash_[16];
+};
diff --git a/llamafile/highlight_asm.cpp b/llamafile/highlight_asm.cpp
index 0775268a..8e46f58d 100644
--- a/llamafile/highlight_asm.cpp
+++ b/llamafile/highlight_asm.cpp
@@ -439,5 +439,7 @@ void HighlightAsm::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_js.cpp b/llamafile/highlight_js.cpp
index 9c8a2764..0d6246e4 100644
--- a/llamafile/highlight_js.cpp
+++ b/llamafile/highlight_js.cpp
@@ -43,8 +43,7 @@ enum {
EXPECT_OPERATOR,
};
-HighlightJs::HighlightJs(is_keyword_f *is_keyword, is_keyword_f *is_type)
- : expect_(EXPECT_VALUE), is_keyword_(is_keyword), is_type_(is_type) {
+HighlightJs::HighlightJs() {
}
HighlightJs::~HighlightJs() {
@@ -97,16 +96,11 @@ void HighlightJs::feed(std::string *r, std::string_view input) {
if (!isascii(c) || isalnum(c) || c == '_') {
word_ += c;
} else {
- if (is_keyword_(word_.data(), word_.size())) {
+ if (is_keyword_js(word_.data(), word_.size())) {
*r += HI_KEYWORD;
*r += word_;
*r += HI_RESET;
expect_ = EXPECT_VALUE;
- } else if (is_type_ && is_type_(word_.data(), word_.size())) {
- *r += HI_TYPE;
- *r += word_;
- *r += HI_RESET;
- expect_ = EXPECT_VALUE;
} else if (is_keyword_js_builtin(word_.data(), word_.size())) {
*r += HI_BUILTIN;
*r += word_;
@@ -265,14 +259,10 @@ void HighlightJs::feed(std::string *r, std::string_view input) {
void HighlightJs::flush(std::string *r) {
switch (t_) {
case WORD:
- if (is_keyword_(word_.data(), word_.size())) {
+ if (is_keyword_js(word_.data(), word_.size())) {
*r += HI_KEYWORD;
*r += word_;
*r += HI_RESET;
- } else if (is_type_ && is_type_(word_.data(), word_.size())) {
- *r += HI_TYPE;
- *r += word_;
- *r += HI_RESET;
} else if (is_keyword_js_builtin(word_.data(), word_.size())) {
*r += HI_BUILTIN;
*r += word_;
diff --git a/llamafile/highlight_kotlin.cpp b/llamafile/highlight_kotlin.cpp
index 8cbc4533..fb6be988 100644
--- a/llamafile/highlight_kotlin.cpp
+++ b/llamafile/highlight_kotlin.cpp
@@ -167,7 +167,6 @@ void HighlightKotlin::feed(std::string *r, std::string_view input) {
t_ = QUOTE;
break;
- // handle "string"
case DQUOTE:
*r += c;
if (c == '"') {
@@ -210,8 +209,8 @@ void HighlightKotlin::feed(std::string *r, std::string_view input) {
*r += c;
t_ = DQUOTESTR_VAR;
} else {
- *r += c;
t_ = DQUOTESTR;
+ goto Dquotestr;
}
break;
@@ -225,7 +224,6 @@ void HighlightKotlin::feed(std::string *r, std::string_view input) {
}
break;
- // handle """string"""
case DQUOTE2:
if (c == '"') {
*r += '"';
diff --git a/llamafile/highlight_make.cpp b/llamafile/highlight_make.cpp
index 3f3129be..a1811827 100644
--- a/llamafile/highlight_make.cpp
+++ b/llamafile/highlight_make.cpp
@@ -227,5 +227,7 @@ void HighlightMake::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_perl.cpp b/llamafile/highlight_perl.cpp
index a33fa8b5..ddb7f17f 100644
--- a/llamafile/highlight_perl.cpp
+++ b/llamafile/highlight_perl.cpp
@@ -555,5 +555,7 @@ void HighlightPerl::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_shell.cpp b/llamafile/highlight_shell.cpp
index a25b9ce4..f640107a 100644
--- a/llamafile/highlight_shell.cpp
+++ b/llamafile/highlight_shell.cpp
@@ -344,5 +344,7 @@ void HighlightShell::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_swift.cpp b/llamafile/highlight_swift.cpp
new file mode 100644
index 00000000..035aff7c
--- /dev/null
+++ b/llamafile/highlight_swift.cpp
@@ -0,0 +1,453 @@
+// -*- 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 "highlight.h"
+#include "string.h"
+#include <cosmo.h>
+#include <ctype.h>
+
+// apple swift
+//
+// - string interpolation
+// "hi \(2 * (2 + 2)) there"
+//
+// - hash strings
+// #"hi \#(2 * (2 + 2)) there"#
+// ##"hi \##(2 * (2 + 2)) there"##
+//
+// - hash multiline strings
+// #"""hi \#(2 * (2 + 2)) there"""#
+// ##"""hi \##(2 * (2 + 2)) there"""##
+//
+
+enum {
+ NORMAL,
+ WORD,
+ SLASH,
+ SLASH_SLASH,
+ SLASH_STAR,
+ SLASH_SLASH_BACKSLASH,
+ SLASH_STAR_STAR,
+ HASH,
+ DQUOTE,
+ DQUOTESTR,
+ DQUOTESTR_BACKSLASH,
+ DQUOTESTR_END,
+ DQUOTE2,
+ DQUOTE3,
+ DQUOTE3_BACKSLASH,
+ DQUOTE31,
+ DQUOTE32,
+ DQUOTE3_END,
+ REGEX,
+ REGEX_END,
+ REGEX_BACKSLASH,
+};
+
+HighlightSwift::HighlightSwift() {
+}
+
+HighlightSwift::~HighlightSwift() {
+}
+
+void HighlightSwift::feed(std::string *r, std::string_view input) {
+ for (size_t i = 0; i < input.size(); ++i) {
+ wchar_t c;
+ int b = input[i] & 255;
+ if (!u_) {
+ if (b < 0300) {
+ c = b;
+ } else {
+ c_ = ThomPikeByte(b);
+ u_ = ThomPikeLen(b) - 1;
+ continue;
+ }
+ } else if (ThomPikeCont(b)) {
+ c = c_ = ThomPikeMerge(c_, b);
+ if (--u_)
+ continue;
+ } else {
+ u_ = 0;
+ c = b;
+ }
+ switch (t_) {
+
+ Normal:
+ case NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ t_ = WORD;
+ append_wchar(&word_, c);
+ } else if (c == '/') {
+ t_ = SLASH;
+ } else if (c == '"') {
+ t_ = DQUOTE;
+ *r += HI_STRING;
+ *r += '"';
+ hash1_ = 0;
+ } else if (c == '#') {
+ t_ = HASH;
+ hash1_ = 1;
+ } else if (c == '(' && nesti_ && nesti_ < sizeof(nest_)) {
+ *r += '(';
+ nest_[nesti_] = NORMAL;
+ hash_[nesti_] = 0;
+ nesti_++;
+ } else if (c == ')' && nesti_) {
+ --nesti_;
+ t_ = nest_[nesti_];
+ hash1_ = hash_[nesti_];
+ if (t_ != NORMAL)
+ *r += HI_STRING;
+ *r += ')';
+ } else {
+ append_wchar(r, c);
+ }
+ break;
+
+ Word:
+ case WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ append_wchar(&word_, c);
+ } else {
+ if (is_keyword_swift(word_.data(), word_.size())) {
+ *r += HI_KEYWORD;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_type(word_.data(), word_.size())) {
+ *r += HI_TYPE;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_builtin(word_.data(), word_.size())) {
+ *r += HI_BUILTIN;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_constant(word_.data(), word_.size())) {
+ *r += HI_CONSTANT;
+ *r += word_;
+ *r += HI_RESET;
+ } else {
+ *r += word_;
+ }
+ word_.clear();
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case SLASH:
+ if (c == '/') {
+ *r += HI_COMMENT;
+ *r += "//";
+ t_ = SLASH_SLASH;
+ } else if (c == '*') {
+ *r += HI_COMMENT;
+ *r += "/*";
+ t_ = SLASH_STAR;
+ } else {
+ *r += HI_STRING;
+ *r += '/';
+ hash1_ = 0;
+ t_ = REGEX;
+ goto Regex;
+ }
+ break;
+
+ case SLASH_SLASH:
+ append_wchar(r, c);
+ if (c == '\n') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c == '\\') {
+ t_ = SLASH_SLASH_BACKSLASH;
+ }
+ break;
+
+ case SLASH_SLASH_BACKSLASH:
+ append_wchar(r, c);
+ t_ = SLASH_SLASH;
+ break;
+
+ case SLASH_STAR:
+ append_wchar(r, c);
+ if (c == '*')
+ t_ = SLASH_STAR_STAR;
+ break;
+
+ case SLASH_STAR_STAR:
+ append_wchar(r, c);
+ if (c == '/') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c != '*') {
+ t_ = SLASH_STAR;
+ }
+ break;
+
+ case HASH:
+ if (c == '#') {
+ ++hash1_;
+ } else if (c == '"') {
+ *r += HI_STRING;
+ for (int i = 0; i < hash1_; ++i)
+ *r += '#';
+ *r += '"';
+ t_ = DQUOTE;
+ } else if (c == '/') {
+ *r += HI_STRING;
+ for (int i = 0; i < hash1_; ++i)
+ *r += '#';
+ *r += '/';
+ t_ = REGEX;
+ } else {
+ for (int i = 0; i < hash1_; ++i)
+ *r += '#';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ Dquote:
+ case DQUOTE:
+ append_wchar(r, c);
+ if (c == '"') {
+ t_ = DQUOTE2;
+ hash2_ = 0;
+ } else if (c == '\\') {
+ t_ = DQUOTESTR_BACKSLASH;
+ hash2_ = 0;
+ } else {
+ t_ = DQUOTESTR;
+ }
+ break;
+
+ Dquotestr:
+ case DQUOTESTR:
+ append_wchar(r, c);
+ if (c == '"') {
+ t_ = DQUOTESTR_END;
+ hash2_ = 0;
+ } else if (c == '\\') {
+ t_ = DQUOTESTR_BACKSLASH;
+ hash2_ = 0;
+ }
+ break;
+
+ case DQUOTESTR_END:
+ if (hash2_ == hash1_) {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ } else if (c == '#') {
+ *r += '#';
+ ++hash2_;
+ } else {
+ t_ = DQUOTESTR;
+ goto Dquotestr;
+ }
+ break;
+
+ case DQUOTESTR_BACKSLASH:
+ if (c == '#' && hash2_ < hash1_) {
+ *r += '#';
+ ++hash2_;
+ } else if (c == '(' && hash2_ == hash1_ && nesti_ < sizeof(nest_)) {
+ *r += '(';
+ *r += HI_RESET;
+ nest_[nesti_] = DQUOTESTR;
+ hash_[nesti_] = hash1_;
+ ++nesti_;
+ t_ = NORMAL;
+ } else {
+ t_ = DQUOTESTR;
+ goto Dquotestr;
+ }
+ break;
+
+ case DQUOTE2:
+ if (c == '"') {
+ *r += '"';
+ t_ = DQUOTE3;
+ } else if (c == '#' && hash2_ < hash1_) {
+ *r += '#';
+ ++hash2_;
+ } else if (hash2_ == hash1_) {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ } else {
+ t_ = DQUOTESTR;
+ goto Dquotestr;
+ }
+ break;
+
+ Dquote3:
+ case DQUOTE3:
+ append_wchar(r, c);
+ if (c == '"') {
+ t_ = DQUOTE31;
+ } else if (c == '\\') {
+ t_ = DQUOTE3_BACKSLASH;
+ hash2_ = 0;
+ }
+ break;
+
+ case DQUOTE31:
+ if (c == '"') {
+ *r += '"';
+ t_ = DQUOTE32;
+ } else {
+ t_ = DQUOTE3;
+ goto Dquote3;
+ }
+ break;
+
+ case DQUOTE32:
+ if (c == '"') {
+ *r += '"';
+ t_ = DQUOTESTR_END;
+ hash2_ = 0;
+ } else {
+ t_ = DQUOTE3;
+ goto Dquote3;
+ }
+ break;
+
+ case DQUOTE3_BACKSLASH:
+ if (c == '#' && hash2_ < hash1_) {
+ *r += '#';
+ ++hash2_;
+ } else if (c == '(' && hash2_ == hash1_ && nesti_ < sizeof(nest_)) {
+ *r += '(';
+ *r += HI_RESET;
+ nest_[nesti_] = DQUOTE3;
+ hash_[nesti_] = hash1_;
+ ++nesti_;
+ t_ = NORMAL;
+ } else {
+ t_ = DQUOTE3;
+ goto Dquote3;
+ }
+ break;
+
+ case DQUOTE3_END:
+ if (hash2_ == hash1_) {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ } else if (c == '#') {
+ *r += '#';
+ ++hash2_;
+ } else {
+ t_ = DQUOTE3;
+ goto Dquote3;
+ }
+ break;
+
+ Regex:
+ case REGEX:
+ append_wchar(r, c);
+ if (c == '/') {
+ t_ = REGEX_END;
+ hash2_ = 0;
+ } else if (c == '\\') {
+ t_ = REGEX_BACKSLASH;
+ hash2_ = 0;
+ }
+ break;
+
+ case REGEX_END:
+ if (hash2_ == hash1_) {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ } else if (c == '#') {
+ *r += '#';
+ ++hash2_;
+ } else {
+ t_ = REGEX;
+ goto Regex;
+ }
+ break;
+
+ case REGEX_BACKSLASH:
+ append_wchar(r, c);
+ t_ = REGEX;
+ break;
+
+ default:
+ __builtin_unreachable();
+ }
+ }
+}
+
+void HighlightSwift::flush(std::string *r) {
+ switch (t_) {
+ case WORD:
+ if (is_keyword_swift(word_.data(), word_.size())) {
+ *r += HI_KEYWORD;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_type(word_.data(), word_.size())) {
+ *r += HI_TYPE;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_builtin(word_.data(), word_.size())) {
+ *r += HI_BUILTIN;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_swift_constant(word_.data(), word_.size())) {
+ *r += HI_CONSTANT;
+ *r += word_;
+ *r += HI_RESET;
+ } else {
+ *r += word_;
+ }
+ word_.clear();
+ break;
+ case SLASH:
+ *r += '/';
+ break;
+ case HASH:
+ for (int i = 0; i < hash1_; ++i)
+ *r += '#';
+ break;
+ case SLASH_SLASH:
+ case SLASH_SLASH_BACKSLASH:
+ case SLASH_STAR:
+ case SLASH_STAR_STAR:
+ case DQUOTE:
+ case DQUOTESTR:
+ case DQUOTESTR_BACKSLASH:
+ case DQUOTESTR_END:
+ case DQUOTE2:
+ case DQUOTE3:
+ case DQUOTE3_BACKSLASH:
+ case DQUOTE31:
+ case DQUOTE32:
+ case DQUOTE3_END:
+ case REGEX:
+ case REGEX_END:
+ case REGEX_BACKSLASH:
+ *r += HI_RESET;
+ break;
+ default:
+ break;
+ }
+ c_ = 0;
+ u_ = 0;
+ t_ = NORMAL;
+}
diff --git a/llamafile/highlight_tcl.cpp b/llamafile/highlight_tcl.cpp
index cd412e17..dc83d36a 100644
--- a/llamafile/highlight_tcl.cpp
+++ b/llamafile/highlight_tcl.cpp
@@ -208,5 +208,7 @@ void HighlightTcl::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_tex.cpp b/llamafile/highlight_tex.cpp
index d4ed71bb..3efa06db 100644
--- a/llamafile/highlight_tex.cpp
+++ b/llamafile/highlight_tex.cpp
@@ -210,5 +210,7 @@ void HighlightTex::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/highlight_typescript.cpp b/llamafile/highlight_typescript.cpp
new file mode 100644
index 00000000..5add1641
--- /dev/null
+++ b/llamafile/highlight_typescript.cpp
@@ -0,0 +1,369 @@
+// -*- 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 "highlight.h"
+#include "string.h"
+#include <cosmo.h>
+#include <ctype.h>
+
+enum {
+ NORMAL,
+ WORD,
+ QUOTE,
+ QUOTE_BACKSLASH,
+ DQUOTE,
+ DQUOTE_BACKSLASH,
+ SLASH,
+ SLASH_SLASH,
+ SLASH_STAR,
+ SLASH_STAR_STAR,
+ TICK,
+ TICK_BACKSLASH,
+ TICK_DOLLAR,
+ REGEX,
+ REGEX_BACKSLASH,
+ REGEX_SQUARE,
+ REGEX_SQUARE_BACKSLASH,
+};
+
+enum {
+ EXPECT_VALUE,
+ EXPECT_OPERATOR,
+};
+
+HighlightTypescript::HighlightTypescript() {
+}
+
+HighlightTypescript::~HighlightTypescript() {
+}
+
+void HighlightTypescript::feed(std::string *r, std::string_view input) {
+ for (size_t i = 0; i < input.size(); ++i) {
+ wchar_t c;
+ int b = input[i] & 255;
+ if (!u_) {
+ if (b < 0300) {
+ c = b;
+ } else {
+ c_ = ThomPikeByte(b);
+ u_ = ThomPikeLen(b) - 1;
+ continue;
+ }
+ } else if (ThomPikeCont(b)) {
+ c = c_ = ThomPikeMerge(c_, b);
+ if (--u_)
+ continue;
+ } else {
+ u_ = 0;
+ c = b;
+ }
+ switch (t_) {
+
+ Normal:
+ case NORMAL:
+ if (!isascii(c) || isalpha(c) || c == '_') {
+ t_ = WORD;
+ append_wchar(&word_, c);
+ } else if (c == '/') {
+ t_ = SLASH;
+ } else if (c == '\'') {
+ t_ = QUOTE;
+ *r += HI_STRING;
+ *r += '\'';
+ expect_ = EXPECT_OPERATOR;
+ } else if (c == '"') {
+ t_ = DQUOTE;
+ *r += HI_STRING;
+ *r += '"';
+ expect_ = EXPECT_OPERATOR;
+ } else if (c == '`') {
+ t_ = TICK;
+ *r += HI_STRING;
+ *r += '`';
+ expect_ = EXPECT_OPERATOR;
+ } else if (c == '{' && nesti_ && nesti_ < sizeof(nest_)) {
+ expect_ = EXPECT_VALUE;
+ *r += '{';
+ nest_[nesti_++] = NORMAL;
+ } else if (c == '}' && nesti_) {
+ if ((t_ = nest_[--nesti_]) != NORMAL)
+ *r += HI_STRING;
+ *r += '}';
+ } else if (c == ')' || c == '}' || c == ']') {
+ expect_ = EXPECT_OPERATOR;
+ append_wchar(r, c);
+ } else if (ispunct(c)) {
+ expect_ = EXPECT_VALUE;
+ append_wchar(r, c);
+ } else if (isdigit(c)) {
+ expect_ = EXPECT_OPERATOR;
+ append_wchar(r, c);
+ } else {
+ append_wchar(r, c);
+ }
+ break;
+
+ Word:
+ case WORD:
+ if (!isascii(c) || isalnum(c) || c == '_') {
+ append_wchar(&word_, c);
+ } else {
+ if (is_keyword_typescript(word_.data(), word_.size())) {
+ *r += HI_KEYWORD;
+ *r += word_;
+ *r += HI_RESET;
+ expect_ = EXPECT_VALUE;
+ } else if (is_keyword_typescript_type(word_.data(), word_.size())) {
+ *r += HI_TYPE;
+ *r += word_;
+ *r += HI_RESET;
+ expect_ = EXPECT_VALUE;
+ } else if (is_keyword_js_builtin(word_.data(), word_.size())) {
+ *r += HI_BUILTIN;
+ *r += word_;
+ *r += HI_RESET;
+ expect_ = EXPECT_OPERATOR;
+ } else if (is_keyword_js_constant(word_.data(), word_.size())) {
+ *r += HI_CONSTANT;
+ *r += word_;
+ *r += HI_RESET;
+ expect_ = EXPECT_OPERATOR;
+ } else {
+ *r += word_;
+ expect_ = EXPECT_OPERATOR;
+ }
+ word_.clear();
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case SLASH:
+ if (c == '/') {
+ *r += HI_COMMENT;
+ *r += "//";
+ t_ = SLASH_SLASH;
+ } else if (c == '*') {
+ *r += HI_COMMENT;
+ *r += "/*";
+ t_ = SLASH_STAR;
+ } else if (expect_ == EXPECT_VALUE) {
+ expect_ = EXPECT_OPERATOR;
+ *r += HI_STRING;
+ *r += '/';
+ append_wchar(r, c);
+ if (c == '\\') {
+ t_ = REGEX_BACKSLASH;
+ } else if (c == '[') {
+ t_ = REGEX_SQUARE;
+ } else {
+ t_ = REGEX;
+ }
+ } else {
+ *r += '/';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case SLASH_SLASH:
+ append_wchar(r, c);
+ if (c == '\n') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ }
+ break;
+
+ case SLASH_STAR:
+ append_wchar(r, c);
+ if (c == '*')
+ t_ = SLASH_STAR_STAR;
+ break;
+
+ case SLASH_STAR_STAR:
+ append_wchar(r, c);
+ if (c == '/') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c != '*') {
+ t_ = SLASH_STAR;
+ }
+ break;
+
+ case QUOTE:
+ append_wchar(r, c);
+ if (c == '\'') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c == '\\') {
+ t_ = QUOTE_BACKSLASH;
+ }
+ break;
+
+ case QUOTE_BACKSLASH:
+ append_wchar(r, c);
+ t_ = QUOTE;
+ break;
+
+ case DQUOTE:
+ append_wchar(r, c);
+ if (c == '"') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c == '\\') {
+ t_ = DQUOTE_BACKSLASH;
+ }
+ break;
+
+ case DQUOTE_BACKSLASH:
+ append_wchar(r, c);
+ t_ = DQUOTE;
+ break;
+
+ Tick:
+ case TICK:
+ if (c == '`') {
+ *r += '`';
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c == '$') {
+ t_ = TICK_DOLLAR;
+ } else if (c == '\\') {
+ *r += '\\';
+ t_ = TICK_BACKSLASH;
+ } else {
+ append_wchar(r, c);
+ }
+ break;
+
+ case TICK_BACKSLASH:
+ append_wchar(r, c);
+ t_ = TICK;
+ break;
+
+ case TICK_DOLLAR:
+ if (c == '{' && nesti_ < sizeof(nest_)) {
+ // this is how the typescript playground highlights
+ *r += HI_RESET;
+ *r += '$';
+ *r += HI_STRING;
+ *r += '{';
+ *r += HI_RESET;
+ expect_ = EXPECT_VALUE;
+ nest_[nesti_++] = TICK;
+ t_ = NORMAL;
+ } else {
+ *r += HI_WARNING;
+ *r += '$';
+ *r += HI_UNBOLD;
+ *r += HI_STRING;
+ t_ = TICK;
+ goto Tick;
+ }
+ break;
+
+ case REGEX:
+ append_wchar(r, c);
+ if (c == '/') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c == '\\') {
+ t_ = REGEX_BACKSLASH;
+ } else if (c == '[') {
+ t_ = REGEX_SQUARE;
+ }
+ break;
+
+ case REGEX_BACKSLASH:
+ append_wchar(r, c);
+ t_ = REGEX;
+ break;
+
+ case REGEX_SQUARE:
+ // because /[/]/g is valid code
+ append_wchar(r, c);
+ if (c == '\\') {
+ t_ = REGEX_SQUARE_BACKSLASH;
+ } else if (c == ']') {
+ t_ = REGEX;
+ }
+ break;
+
+ case REGEX_SQUARE_BACKSLASH:
+ append_wchar(r, c);
+ t_ = REGEX_SQUARE;
+ break;
+
+ default:
+ __builtin_unreachable();
+ }
+ }
+}
+
+void HighlightTypescript::flush(std::string *r) {
+ switch (t_) {
+ case WORD:
+ if (is_keyword_typescript(word_.data(), word_.size())) {
+ *r += HI_KEYWORD;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_typescript_type(word_.data(), word_.size())) {
+ *r += HI_TYPE;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_js_builtin(word_.data(), word_.size())) {
+ *r += HI_BUILTIN;
+ *r += word_;
+ *r += HI_RESET;
+ } else if (is_keyword_js_constant(word_.data(), word_.size())) {
+ *r += HI_CONSTANT;
+ *r += word_;
+ *r += HI_RESET;
+ } else {
+ *r += word_;
+ }
+ word_.clear();
+ break;
+ case SLASH:
+ *r += '/';
+ break;
+ case TICK_DOLLAR:
+ *r += '$';
+ *r += HI_RESET;
+ break;
+ case TICK:
+ case TICK_BACKSLASH:
+ case QUOTE:
+ case QUOTE_BACKSLASH:
+ case DQUOTE:
+ case DQUOTE_BACKSLASH:
+ case SLASH_SLASH:
+ case SLASH_STAR:
+ case SLASH_STAR_STAR:
+ case REGEX:
+ case REGEX_BACKSLASH:
+ case REGEX_SQUARE:
+ case REGEX_SQUARE_BACKSLASH:
+ *r += HI_RESET;
+ break;
+ default:
+ break;
+ }
+ c_ = 0;
+ u_ = 0;
+ t_ = NORMAL;
+}
diff --git a/llamafile/highlight_zig.cpp b/llamafile/highlight_zig.cpp
index 4c91a3b7..9a3bb042 100644
--- a/llamafile/highlight_zig.cpp
+++ b/llamafile/highlight_zig.cpp
@@ -222,5 +222,7 @@ void HighlightZig::flush(std::string *r) {
default:
break;
}
+ c_ = 0;
+ u_ = 0;
t_ = NORMAL;
}
diff --git a/llamafile/is_keyword_js_builtin.gperf b/llamafile/is_keyword_js_builtin.gperf
index 3d273221..10355dfc 100644
--- a/llamafile/is_keyword_js_builtin.gperf
+++ b/llamafile/is_keyword_js_builtin.gperf
@@ -61,6 +61,7 @@ Uint8ClampedArray
WeakMap
WeakRef
WeakSet
+console
decodeURI
decodeURIComponent
encodeURI
diff --git a/llamafile/is_keyword_swift.gperf b/llamafile/is_keyword_swift.gperf
index 2ee5729d..57066f7f 100644
--- a/llamafile/is_keyword_swift.gperf
+++ b/llamafile/is_keyword_swift.gperf
@@ -116,7 +116,6 @@ exclusivity
exported
extension
fallthrough
-false
file
fileprivate
final
@@ -155,7 +154,6 @@ module
mutableAddressWithNativeOwner
mutableAddressWithOwner
mutating
-nil
noDerivative
noasync
noescape
@@ -207,7 +205,6 @@ then
throw
throws
transpose
-true
try
typealias
unavailable
diff --git a/llamafile/is_keyword_swift_builtin.gperf b/llamafile/is_keyword_swift_builtin.gperf
new file mode 100644
index 00000000..00ab8578
--- /dev/null
+++ b/llamafile/is_keyword_swift_builtin.gperf
@@ -0,0 +1,40 @@
+%{
+#include <string.h>
+%}
+%pic
+%compare-strncmp
+%language=ANSI-C
+%readonly-tables
+%define lookup-function-name is_keyword_swift_builtin
+%%
+abs
+assert
+assertionFailure
+debugPrint
+dump
+fatalError
+getVaList
+isKnownUniquelyReferenced
+max
+min
+numericCast
+precondition
+preconditionFailure
+print
+readLine
+repeatElement
+sequence
+stride
+swap
+transcode
+type
+unsafeBitCast
+unsafeDowncast
+withExtendedLifetime
+withUnsafeBytes
+withUnsafeMutableBytes
+withUnsafeMutablePointer
+withUnsafePointer
+withVaList
+withoutActuallyEscaping
+zip
diff --git a/llamafile/is_keyword_swift_constant.gperf b/llamafile/is_keyword_swift_constant.gperf
new file mode 100644
index 00000000..37dcbafb
--- /dev/null
+++ b/llamafile/is_keyword_swift_constant.gperf
@@ -0,0 +1,12 @@
+%{
+#include <string.h>
+%}
+%pic
+%compare-strncmp
+%language=ANSI-C
+%readonly-tables
+%define lookup-function-name is_keyword_swift_constant
+%%
+false
+nil
+true
diff --git a/llamafile/is_keyword_zig_builtin.gperf b/llamafile/is_keyword_zig_builtin.gperf
index 1e0016c6..67421187 100644
--- a/llamafile/is_keyword_zig_builtin.gperf
+++ b/llamafile/is_keyword_zig_builtin.gperf
@@ -7,37 +7,33 @@
%readonly-tables
%define lookup-function-name is_keyword_zig_builtin
%%
-@FieldType
-@This
-@Type
-@TypeOf
-@Vector
@abs
-@addWithOverflow
@addrSpaceCast
+@addWithOverflow
@alignCast
@alignOf
+@ArgType
@as
@atomicLoad
@atomicRmw
@atomicStore
@bitCast
@bitOffsetOf
+@bitreverse
@bitReverse
@bitSizeOf
+@boolToInt
@branchHint
@breakpoint
+@bswap
+@byteOffsetOf
+@bytesToSlice
@byteSwap
+@call
@cDefine
+@ceil
@cImport
@cInclude
-@cUndef
-@cVaArg
-@cVaCopy
-@cVaEnd
-@cVaStart
-@call
-@ceil
@clz
@cmpxchgStrong
@cmpxchgWeak
@@ -46,59 +42,86 @@
@constCast
@cos
@ctz
+@cUndef
+@cVaArg
+@cVaCopy
+@cVaEnd
+@cVaStart
@divExact
@divFloor
@divTrunc
@embedFile
@enumFromInt
+@enumToInt
@errorCast
@errorFromInt
@errorName
@errorReturnTrace
+@errorToInt
+@errSetCast
@exp
@exp2
@export
@extern
+@fence
@field
@fieldParentPtr
+@FieldType
@floatCast
@floatFromInt
+@floatToInt
@floor
@frameAddress
+@handle
@hasDecl
@hasField
@import
@inComptime
+@inlineCall
@intCast
@intFromBool
@intFromEnum
@intFromError
@intFromFloat
@intFromPtr
+@intToEnum
+@intToError
+@intToFloat
+@intToPtr
+@IntType
@log
@log10
@log2
@max
+@memberCount
+@memberName
+@memberType
@memcpy
@memset
@min
@mod
@mulAdd
@mulWithOverflow
+@newStackCall
+@noInlineCall
@offsetOf
+@OpaqueType
@panic
@popCount
@prefetch
@ptrCast
@ptrFromInt
+@ptrToInt
@reduce
@rem
@returnAddress
@round
@select
@setAlignStack
+@setCold
@setEvalBranchQuota
@setFloatMode
+@setGlobalLinkage
@setRuntimeSafety
@shlExact
@shlWithOverflow
@@ -106,18 +129,26 @@
@shuffle
@sin
@sizeOf
+@sliceToBytes
@splat
@sqrt
@src
@subWithOverflow
@tagName
+@TagType
@tan
+@This
@trap
@trunc
@truncate
+@Type
+@typeId
@typeInfo
@typeName
+@typeOf
+@TypeOf
@unionInit
+@Vector
@volatileCast
@wasmMemoryGrow
@wasmMemorySize