Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Add syntax highlighting to chatbot
Justine Tunney63205ee5b7c4
diff --git a/build/rules.mk b/build/rules.mkindex 1b0a840a..f8863817 100644--- a/build/rules.mk+++ b/build/rules.mk@@ -5,6 +5,9 @@ LINK.o = $(CXX) $(CCFLAGS) $(LDFLAGS)COMPILE.c = $(CC) $(CCFLAGS) $(CFLAGS) $(CPPFLAGS_) $(CPPFLAGS) $(TARGET_ARCH) -cCOMPILE.cc = $(CXX) $(CCFLAGS) $(CXXFLAGS) $(CPPFLAGS_) $(CPPFLAGS) $(TARGET_ARCH) -c+%.c: %.gperf+ gperf $< >$@+o/$(MODE)/%.o: %.c $(COSMOCC)@mkdir -p $(@D)$(COMPILE.c) -o $@ $<diff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mkindex 3d260a15..832403e8 100644--- a/llamafile/BUILD.mk+++ b/llamafile/BUILD.mk@@ -52,6 +52,9 @@ o/$(MODE)/llamafile: \o/$(MODE)/llamafile/zipcheck \o/$(MODE)/llamafile/tokenize \o/$(MODE)/llamafile/addnl \+ o/$(MODE)/llamafile/high \+ o/$(MODE)/llamafile/highlight_c_test.runs \+ o/$(MODE)/llamafile/highlight_python_test.runs \o/$(MODE)/llamafile/pool_test.runs \o/$(MODE)/llamafile/pool_cancel_test.runs \o/$(MODE)/llamafile/parse_cidr_test.runs \@@ -162,6 +165,24 @@ o/$(MODE)/llamafile/pool_test: \o/$(MODE)/llamafile/crash.o \o/$(MODE)/llamafile/pool.o \+o/$(MODE)/llamafile/highlight_c_test: \+ o/$(MODE)/llamafile/highlight_c_test.o \+ o/$(MODE)/llamafile/highlight_c.o \+ o/$(MODE)/llamafile/is_keyword_c.o \++o/$(MODE)/llamafile/highlight_python_test: \+ o/$(MODE)/llamafile/highlight_python_test.o \+ o/$(MODE)/llamafile/highlight_python.o \+ o/$(MODE)/llamafile/is_keyword_python.o \++o/$(MODE)/llamafile/high: \+ o/$(MODE)/llamafile/high.o \+ o/$(MODE)/llama.cpp/llama.cpp.a \++o/$(MODE)/llamafile/hex2xterm: \+ o/$(MODE)/llamafile/hex2xterm.o \+ o/$(MODE)/llamafile/xterm.o \+o/$(MODE)/llamafile/pool_cancel_test: \o/$(MODE)/llamafile/pool_cancel_test.o \o/$(MODE)/llamafile/crash.o \diff --git a/llamafile/chatbot.cpp b/llamafile/chatbot.cppindex fe108266..44932c8a 100644--- a/llamafile/chatbot.cpp+++ b/llamafile/chatbot.cpp@@ -15,6 +15,7 @@// See the License for the specific language governing permissions and// limitations under the License.+#include "llamafile/highlight.h"#include <assert.h>#include <cosmo.h>#include <ctype.h>@@ -227,6 +228,7 @@ int chatbot_main(int argc, char **argv) {printf("%s\n", params.special ? msg.c_str() : params.prompt.c_str());// perform important setup+ HighlightMarkdown highlighter;struct llama_sampling_context *sampler = llama_sampling_init(params.sparams);signal(SIGINT, on_sigint);@@ -258,13 +260,17 @@ int chatbot_main(int argc, char **argv) {llama_sampling_accept(sampler, g_ctx, id, true);if (llama_token_is_eog(g_model, id))break;- printf("%s", llama_token_to_piece(g_ctx, id, params.special).c_str());+ std::string s;+ highlighter.feed(&s, llama_token_to_piece(g_ctx, id, params.special));+ printf("%s", s.c_str());fflush(stdout);eval_id(id);}g_got_sigint = 0;- printf("\n");free(line);+ std::string s;+ highlighter.flush(&s);+ printf("%s\n", s.c_str());}print_ephemeral("freeing context...");diff --git a/llamafile/hex2xterm.cpp b/llamafile/hex2xterm.cppnew file mode 100644index 00000000..7de0ae8c--- /dev/null+++ b/llamafile/hex2xterm.cpp@@ -0,0 +1,40 @@+// -*- 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 "xterm.h"++#include <stdio.h>+#include <stdlib.h>++int main(int argc, char *argv[]) {++ if (argc < 2) {+ fprintf(stderr, "%s: missing operand\n", argv[0]);+ exit(1);+ }++ // process RRGGBB hex arguments+ for (int i = 1; i < argc; ++i) {+ int rgb = strtol(argv[i], 0, 16);+ int xterm = rgb2xterm256(rgb);+ printf("\n");+ printf("xterm code %d\n", xterm);+ printf("html5 code #%06x\n", rgb);+ printf("foreground \033[38;5;%dm\\033[38;5;%dm\033[0m\n", xterm, xterm);+ printf("background \033[48;5;%dm\\033[48;5;%dm\033[0m\n", xterm, xterm);+ }+}diff --git a/llamafile/high.cpp b/llamafile/high.cppnew file mode 100644index 00000000..eaf77775--- /dev/null+++ b/llamafile/high.cpp@@ -0,0 +1,100 @@+// -*- 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 <fcntl.h>+#include <stdio.h>+#include <stdlib.h>+#include <string>+#include <string_view>+#include <unistd.h>++// syntax highlighter demo++int main(int argc, char *argv[]) {++ // process flags+ int opt;+ int infd = 0;+ int outfd = 1;+ const char *lang = nullptr;+ const char *inpath = nullptr;+ while ((opt = getopt(argc, argv, "hl:o:")) != -1) {+ switch (opt) {+ case 'h':+ printf("usage: %s [-l LANG] [-o OUTFILE] [INFILE]\n", argv[0]);+ exit(0);+ case 'l':+ lang = optarg;+ break;+ case 'o':+ if ((outfd = creat(optarg, 0644)) == -1) {+ perror(optarg);+ exit(1);+ }+ break;+ default:+ exit(1);+ }+ }+ if (optind < argc) {+ inpath = argv[optind];+ if ((infd = open(inpath, O_RDONLY)) == -1) {+ perror(inpath);+ exit(1);+ }+ }++ // create syntax highlighter+ Highlight *h;+ const char *ext;+ if (lang) {+ h = Highlight::create(lang);+ } else if (inpath && (ext = strrchr(inpath, '.'))) {+ h = Highlight::create(ext + 1);+ } else {+ h = Highlight::create("markdown");+ }++ // process input+ std::string res;+ for (;;) {++ // read input chunk+ char buf[256];+ ssize_t rc = read(infd, buf, sizeof(buf));+ if (rc == -1) {+ perror("read");+ exit(1);+ }+ size_t got = rc;+ if (!got)+ break;++ // highlight chunk+ res.clear();+ h->feed(&res, std::string_view(buf, got));++ // write highlighted output chunk+ write(outfd, res.data(), res.size());+ }++ // flush highlighter+ res.clear();+ h->flush(&res);+ write(outfd, res.data(), res.size());+}diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cppnew file mode 100644index 00000000..93f9546a--- /dev/null+++ b/llamafile/highlight.cpp@@ -0,0 +1,36 @@+// -*- 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"++Highlight *Highlight::create(const std::string_view &lang) {+ if (lang == "markdown" || lang == "md") {+ return new HighlightMarkdown;+ } else if (lang == "c") {+ return new HighlightC(is_keyword_c);+ } else if (lang == "c++" || lang == "cxx" || lang == "cpp") {+ return new HighlightC(is_keyword_cxx);+ } else if (lang == "js" || lang == "javascript") {+ return new HighlightC(is_keyword_js);+ } else if (lang == "java") {+ return new HighlightC(is_keyword_java);+ } else if (lang == "py" || lang == "python") {+ return new HighlightPython();+ } else {+ return new HighlightPlain;+ }+}diff --git a/llamafile/highlight.h b/llamafile/highlight.hnew file mode 100644index 00000000..f71210d1--- /dev/null+++ b/llamafile/highlight.h@@ -0,0 +1,90 @@+// -*- 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.++#pragma once+#include <string>+#include <string_view>++#define RESET "\033[0m"+#define BOLDE "\033[1m"+#define KEYWORD "\033[1;34m" // bold blue+#define STRING "\033[0;32m" // green+#define COMMENT "\033[0;31m" // red++typedef const char *is_keyword_f(const char *, size_t);++extern "C" {+is_keyword_f is_keyword_c;+is_keyword_f is_keyword_cxx;+is_keyword_f is_keyword_js;+is_keyword_f is_keyword_java;+is_keyword_f is_keyword_python;+}++class Highlight {+ public:+ static Highlight *create(const std::string_view &lang);+ virtual ~Highlight() = default;+ virtual void feed(std::string *result, std::string_view input) = 0;+ virtual void flush(std::string *result) = 0;+};++class HighlightPlain : public Highlight {+ public:+ HighlightPlain();+ ~HighlightPlain() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;+};++class HighlightC : public Highlight {+ public:+ explicit HighlightC(is_keyword_f is_keyword = is_keyword_c);+ ~HighlightC() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;++ private:+ int t_ = 0;+ std::string word_;+ is_keyword_f *is_keyword_;+};++class HighlightPython : public Highlight {+ public:+ HighlightPython();+ ~HighlightPython() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;++ private:+ int t_ = 0;+ std::string word_;+};++class HighlightMarkdown : public Highlight {+ public:+ HighlightMarkdown();+ ~HighlightMarkdown() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;++ private:+ int t_ = 0;+ std::string lang_;+ Highlight *highlighter_ = nullptr;+};diff --git a/llamafile/highlight_c.cpp b/llamafile/highlight_c.cppnew file mode 100644index 00000000..f0c02108--- /dev/null+++ b/llamafile/highlight_c.cpp@@ -0,0 +1,183 @@+// -*- 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 <ctype.h>++#define NORMAL 0+#define WORD 1+#define QUOTE 2+#define DQUOTE 3+#define SLASH 4+#define SLASH_SLASH 5+#define SLASH_STAR 6+#define SLASH_STAR_STAR 7+#define BACKSLASH 64++HighlightC::HighlightC(is_keyword_f *is_keyword) : is_keyword_(is_keyword) {+}++HighlightC::~HighlightC() {+}++void HighlightC::feed(std::string *r, std::string_view input) {+ int c;+ for (size_t i = 0; i < input.size(); ++i) {+ c = input[i] & 255;++ if (t_ & BACKSLASH) {+ t_ &= ~BACKSLASH;+ *r += c;+ continue;+ } else if (c == '\\') {+ *r += c;+ t_ |= BACKSLASH;+ continue;+ }++ switch (t_) {++ Normal:+ case NORMAL:+ if (!isascii(c) || isalpha(c) || c == '_' || c == '#') {+ t_ = WORD;+ goto Word;+ } else if (c == '/') {+ t_ = SLASH;+ } else if (c == '\'') {+ t_ = QUOTE;+ *r += STRING;+ *r += c;+ } else if (c == '"') {+ t_ = DQUOTE;+ *r += STRING;+ *r += c;+ } else {+ *r += c;+ }+ break;++ Word:+ case WORD:+ if (!isascii(c) || isalpha(c) || isdigit(c) || c == '_' || c == '$' || c == '#') {+ word_ += c;+ } else {+ if (is_keyword_(word_.data(), word_.size())) {+ *r += KEYWORD;+ *r += word_;+ *r += RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case SLASH:+ if (c == '/') {+ *r += COMMENT;+ *r += "//";+ t_ = SLASH_SLASH;+ } else if (c == '*') {+ *r += COMMENT;+ *r += "/*";+ t_ = SLASH_STAR;+ } else {+ *r += '/';+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case SLASH_SLASH:+ if (c == '\n') {+ *r += RESET;+ *r += c;+ t_ = NORMAL;+ } else {+ *r += c;+ }+ break;++ case SLASH_STAR:+ *r += c;+ if (c == '*')+ t_ = SLASH_STAR_STAR;+ break;++ case SLASH_STAR_STAR:+ *r += c;+ if (c == '/') {+ *r += RESET;+ t_ = NORMAL;+ } else {+ t_ = SLASH_STAR;+ }+ break;++ case QUOTE:+ *r += c;+ if (c == '\'') {+ *r += RESET;+ t_ = NORMAL;+ }+ break;++ case DQUOTE:+ *r += c;+ if (c == '"') {+ *r += RESET;+ t_ = NORMAL;+ }+ break;++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightC::flush(std::string *r) {+ switch (t_) {+ case WORD:+ if (is_keyword_(word_.data(), word_.size())) {+ *r += KEYWORD;+ *r += word_;+ *r += RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ break;+ case SLASH:+ *r += '/';+ break;+ case QUOTE:+ case DQUOTE:+ case SLASH_SLASH:+ case SLASH_STAR:+ case SLASH_STAR_STAR:+ *r += RESET;+ break;+ default:+ break;+ }+ t_ = NORMAL;+}diff --git a/llamafile/highlight_c_test.cpp b/llamafile/highlight_c_test.cppnew file mode 100644index 00000000..3b336dbe--- /dev/null+++ b/llamafile/highlight_c_test.cpp@@ -0,0 +1,73 @@+// -*- 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 <cstdio>+#include <cstdlib>++using namespace std::string_literals;++HighlightC g_highlight;++void test(std::string input, std::string expect) {+ std::string got;+ g_highlight.feed(&got, input);+ if (got != expect) {+ fprintf(stderr,+ "error: highlighting failed\n"+ "\t input: %`'s\n"+ "\texpected: %`'s\n"+ "\t got: %`'s\n",+ input.c_str(), expect.c_str(), got.c_str());+ exit(1);+ }+}++void testf(std::string input, std::string expect) {+ std::string got;+ g_highlight.feed(&got, input);+ g_highlight.flush(&got);+ if (got != expect) {+ fprintf(stderr,+ "error: highlighting failed\n"+ "\t input: %`'s\n"+ "\texpected: %`'s\n"+ "\t got: %`'s\n",+ input.c_str(), expect.c_str(), got.c_str());+ exit(1);+ }+}++int main(int argc, char *argv[]) {+ test("", "");+ test("vola", "");+ test("tile ", ""s + KEYWORD + "volatile" + RESET + " ");+ test("yo // hi\nthere ", "yo "s + COMMENT + "// hi" + RESET + "\nthere ");+ testf("// sup", ""s + COMMENT + "// sup" + RESET);+ test("yo /* hi */ there ", "yo "s + COMMENT + "/* hi */" + RESET + " there ");+ testf("yo /* hi */ there", "yo "s + COMMENT + "/* hi */" + RESET + " there");+ testf("yo /* h*i */ there", "yo "s + COMMENT + "/* h*i */" + RESET + " there");+ testf("vola", "vola");+ testf("volatile", ""s + KEYWORD + "volatile" + RESET);+ testf("/hi/", "/hi/");+ testf("x '' y", "x "s + STRING + "''" + RESET + " y");+ testf("x 'c' y", "x "s + STRING + "'c'" + RESET + " y");+ testf("x '\\'' y", "x "s + STRING + "'\\''" + RESET + " y");+ testf("x \"c\" y", "x "s + STRING + "\"c\"" + RESET + " y");+ testf("x \"\\\"\" y", "x "s + STRING + "\"\\\"\"" + RESET + " y");+}diff --git a/llamafile/highlight_markdown.cpp b/llamafile/highlight_markdown.cppnew file mode 100644index 00000000..9fb6d321--- /dev/null+++ b/llamafile/highlight_markdown.cpp@@ -0,0 +1,177 @@+// -*- 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"++#define NORMAL 0+#define TICK 1+#define TICK_TICK 2+#define LANG 3+#define CODE 4+#define CODE_TICK 5+#define CODE_TICK_TICK 6+#define STAR 7+#define STRONG 8+#define STRONG_STAR 9+#define BACKSLASH 10++HighlightMarkdown::HighlightMarkdown() {+}++HighlightMarkdown::~HighlightMarkdown() {+}++void HighlightMarkdown::feed(std::string *r, std::string_view input) {+ char c;+ for (size_t i = 0; i < input.size(); ++i) {+ c = input[i];+ switch (t_) {++ Normal:+ case NORMAL:+ if (c == '`') {+ t_ = TICK;+ *r += c;+ } else if (c == '*') {+ t_ = STAR;+ } else if (c == '\\') {+ t_ = BACKSLASH;+ *r += c;+ } else {+ *r += c;+ }+ break;++ case BACKSLASH:+ *r += c;+ t_ = NORMAL;+ break;++ case STAR:+ if (c == '*') {+ t_ = STRONG;+ *r += BOLDE;+ } else {+ t_ = NORMAL;+ }+ *r += '*';+ *r += c;+ break;++ case STRONG:+ *r += c;+ if (c == '*')+ t_ = STRONG_STAR;+ break;++ case STRONG_STAR:+ *r += c;+ if (c == '*') {+ t_ = NORMAL;+ *r += RESET;+ }+ break;++ case TICK:+ if (c == '`') {+ t_ = TICK_TICK;+ *r += c;+ } else {+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case TICK_TICK:+ if (c == '`') {+ t_ = LANG;+ *r += c;+ } else {+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case LANG:+ if (!isascii(c) || !isspace(c)) {+ *r += c;+ lang_ += c;+ } else {+ highlighter_ = Highlight::create(lang_);+ lang_.clear();+ t_ = CODE;+ goto Code;+ }+ break;++ Code:+ case CODE: {+ char cs[2] = {c};+ highlighter_->feed(r, cs);+ if (c == '`')+ t_ = CODE_TICK;+ break;+ }++ case CODE_TICK: {+ char cs[2] = {c};+ highlighter_->feed(r, cs);+ if (c == '`') {+ t_ = CODE_TICK_TICK;+ } else {+ t_ = CODE;+ }+ break;+ }++ case CODE_TICK_TICK: {+ char cs[2] = {c};+ highlighter_->feed(r, cs);+ if (c == '`') {+ t_ = NORMAL;+ highlighter_->flush(r);+ delete highlighter_;+ highlighter_ = nullptr;+ } else {+ t_ = CODE;+ }+ break;+ }++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightMarkdown::flush(std::string *r) {+ switch (t_) {+ case LANG:+ lang_.clear();+ break;+ case CODE:+ case CODE_TICK:+ case CODE_TICK_TICK:+ highlighter_->flush(r);+ delete highlighter_;+ highlighter_ = nullptr;+ break;+ default:+ break;+ }+ t_ = NORMAL;+}diff --git a/llamafile/highlight_plain.cpp b/llamafile/highlight_plain.cppnew file mode 100644index 00000000..b0401633--- /dev/null+++ b/llamafile/highlight_plain.cpp@@ -0,0 +1,32 @@+// -*- 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"++HighlightPlain::HighlightPlain() {+}++HighlightPlain::~HighlightPlain() {+}++void HighlightPlain::feed(std::string *r, std::string_view input) {+ for (size_t i = 0; i < input.size(); ++i)+ *r += input[i] & 255;+}++void HighlightPlain::flush(std::string *r) {+}diff --git a/llamafile/highlight_python.cpp b/llamafile/highlight_python.cppnew file mode 100644index 00000000..ec0f4058--- /dev/null+++ b/llamafile/highlight_python.cpp@@ -0,0 +1,254 @@+// -*- 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 <ctype.h>++#define NORMAL 0+#define WORD 1+#define COM 2++#define SQUOTE 3 // '+#define SQUOTESTR 4 // '...+#define SQUOTE2 5 // ''+#define SQUOTE3 6 // '''...+#define SQUOTE31 7 // '''...'+#define SQUOTE32 8 // '''...''++#define DQUOTE 9 // "+#define DQUOTESTR 10 // "...+#define DQUOTE2 11 // ""+#define DQUOTE3 12 // """...+#define DQUOTE31 13 // """..."+#define DQUOTE32 14 // """...""++#define BACKSLASH 64++HighlightPython::HighlightPython() {+}++HighlightPython::~HighlightPython() {+}++void HighlightPython::feed(std::string *r, std::string_view input) {+ int c;+ for (size_t i = 0; i < input.size(); ++i) {+ c = input[i] & 255;++ if (t_ & BACKSLASH) {+ t_ &= ~BACKSLASH;+ *r += c;+ continue;+ } else if (c == '\\') {+ *r += c;+ t_ |= BACKSLASH;+ continue;+ }++ switch (t_) {++ Normal:+ case NORMAL:+ if (!isascii(c) || isalpha(c) || c == '_') {+ t_ = WORD;+ goto Word;+ } else if (c == '#') {+ t_ = COM;+ *r += COMMENT;+ *r += c;+ } else if (c == '\'') {+ t_ = SQUOTE;+ *r += STRING;+ *r += c;+ } else if (c == '"') {+ t_ = DQUOTE;+ *r += STRING;+ *r += c;+ } else {+ *r += c;+ }+ break;++ Word:+ case WORD:+ if (!isascii(c) || isalpha(c) || isdigit(c) || c == '_') {+ word_ += c;+ } else {+ if (is_keyword_python(word_.data(), word_.size())) {+ *r += KEYWORD;+ *r += word_;+ *r += RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case COM:+ if (c == '\n') {+ *r += RESET;+ *r += c;+ t_ = NORMAL;+ } else {+ *r += c;+ }+ break;++ // handle 'string'+ case SQUOTE:+ *r += c;+ if (c == '\'') {+ t_ = SQUOTE2;+ } else {+ t_ = SQUOTESTR;+ }+ break;+ case SQUOTESTR:+ *r += c;+ if (c == '\'') {+ *r += RESET;+ t_ = NORMAL;+ }+ break;++ // handle '''string'''+ case SQUOTE2:+ if (c == '\'') {+ *r += c;+ t_ = SQUOTE3;+ } else {+ *r += RESET;+ t_ = NORMAL;+ goto Normal;+ }+ break;+ case SQUOTE3:+ *r += c;+ if (c == '\'')+ t_ = SQUOTE31;+ break;+ case SQUOTE31:+ *r += c;+ if (c == '\'') {+ t_ = SQUOTE32;+ } else {+ t_ = SQUOTE3;+ }+ break;+ case SQUOTE32:+ *r += c;+ if (c == '\'') {+ *r += RESET;+ t_ = NORMAL;+ } else {+ t_ = SQUOTE3;+ }+ break;++ // handle "string"+ case DQUOTE:+ *r += c;+ if (c == '"') {+ t_ = DQUOTE2;+ } else {+ t_ = DQUOTESTR;+ }+ break;+ case DQUOTESTR:+ *r += c;+ if (c == '"') {+ *r += RESET;+ t_ = NORMAL;+ }+ break;++ // handle """string"""+ case DQUOTE2:+ if (c == '"') {+ *r += c;+ t_ = DQUOTE3;+ } else {+ *r += RESET;+ t_ = NORMAL;+ goto Normal;+ }+ break;+ case DQUOTE3:+ *r += c;+ if (c == '"')+ t_ = DQUOTE31;+ break;+ case DQUOTE31:+ *r += c;+ if (c == '"') {+ t_ = DQUOTE32;+ } else {+ t_ = DQUOTE3;+ }+ break;+ case DQUOTE32:+ *r += c;+ if (c == '"') {+ *r += RESET;+ t_ = NORMAL;+ } else {+ t_ = DQUOTE3;+ }+ break;++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightPython::flush(std::string *r) {+ switch (t_) {+ case WORD:+ if (is_keyword_python(word_.data(), word_.size())) {+ *r += KEYWORD;+ *r += word_;+ *r += RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ break;+ case COM:+ case SQUOTE:+ case SQUOTESTR:+ case SQUOTE2:+ case SQUOTE3:+ case SQUOTE31:+ case SQUOTE32:+ case DQUOTE:+ case DQUOTESTR:+ case DQUOTE2:+ case DQUOTE3:+ case DQUOTE31:+ case DQUOTE32:+ *r += RESET;+ break;+ default:+ break;+ }+ t_ = NORMAL;+}diff --git a/llamafile/highlight_python_test.cpp b/llamafile/highlight_python_test.cppnew file mode 100644index 00000000..7510ec46--- /dev/null+++ b/llamafile/highlight_python_test.cpp@@ -0,0 +1,77 @@+// -*- 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 <cstdio>+#include <cstdlib>++using namespace std::string_literals;++HighlightPython g_highlight;++void test(std::string input, std::string expect) {+ std::string got;+ g_highlight.feed(&got, input);+ if (got != expect) {+ fprintf(stderr,+ "error: highlighting failed\n"+ "\t input: %`'s\n"+ "\texpected: %`'s\n"+ "\t got: %`'s\n",+ input.c_str(), expect.c_str(), got.c_str());+ exit(1);+ }+}++void testf(std::string input, std::string expect) {+ std::string got;+ g_highlight.feed(&got, input);+ g_highlight.flush(&got);+ if (got != expect) {+ fprintf(stderr,+ "error: highlighting failed\n"+ "\t input: %`'s\n"+ "\texpected: %`'s\n"+ "\t got: %`'s\n",+ input.c_str(), expect.c_str(), got.c_str());+ exit(1);+ }+}++int main(int argc, char *argv[]) {+ test("", "");+ test("d", "");+ test("ef ", ""s + KEYWORD + "def" + RESET + " ");+ test("yo # hi\nthere ", "yo "s + COMMENT + "# hi" + RESET + "\nthere ");+ testf("# sup", ""s + COMMENT + "# sup" + RESET);+ testf("de", "de");+ testf("def", ""s + KEYWORD + "def" + RESET);+ testf("/hi/", "/hi/");+ testf("x '' y", "x "s + STRING + "''" + RESET + " y");+ testf("x '''''' y", "x "s + STRING + "''''''" + RESET + " y");+ testf("x '''xxx''' y", "x "s + STRING + "'''xxx'''" + RESET + " y");+ testf("x '''xx'x''' y", "x "s + STRING + "'''xx'x'''" + RESET + " y");+ testf("x '''xx''x''' y", "x "s + STRING + "'''xx''x'''" + RESET + " y");+ testf("x \"\" y", "x "s + STRING + "\"\"" + RESET + " y");+ testf("x \"\"\"\"\"\" y", "x "s + STRING + "\"\"\"\"\"\"" + RESET + " y");+ testf("x \"\"\"xxx\"\"\" y", "x "s + STRING + "\"\"\"xxx\"\"\"" + RESET + " y");+ testf("x 'c' y", "x "s + STRING + "'c'" + RESET + " y");+ testf("x '\\'' y", "x "s + STRING + "'\\''" + RESET + " y");+ testf("x \"c\" y", "x "s + STRING + "\"c\"" + RESET + " y");+ testf("x \"\\\"\" y", "x "s + STRING + "\"\\\"\"" + RESET + " y");+}diff --git a/llamafile/is_keyword_c.c b/llamafile/is_keyword_c.cnew file mode 100644index 00000000..97cd3c73--- /dev/null+++ b/llamafile/is_keyword_c.c@@ -0,0 +1,252 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf llamafile/is_keyword_c.gperf */+/* Computed positions: -k'2-3,$' */++#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \+ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \+ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \+ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \+ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \+ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \+ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \+ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \+ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \+ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \+ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \+ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \+ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \+ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \+ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \+ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \+ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \+ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \+ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \+ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \+ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \+ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \+ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))+/* The character set is not based on ISO-646. */+#error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>."+#endif++#line 1 "llamafile/is_keyword_c.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 104+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 19+#define MIN_HASH_VALUE 10+#define MAX_HASH_VALUE 191+/* maximum key range = 182, duplicates = 0 */++#ifdef __GNUC__+__inline+#else+#ifdef __cplusplus+inline+#endif+#endif+static unsigned int+hash (register const char *str, register size_t len)+{+ static const unsigned char asso_values[] =+ {+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 0, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 30, 192, 10, 192, 192, 192, 0, 192, 192, 192,+ 192, 192, 192, 192, 192, 70, 35, 70, 0, 192,+ 0, 60, 192, 0, 192, 192, 192, 192, 0, 192,+ 5, 192, 192, 5, 70, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 5, 192, 60, 65, 70,+ 75, 0, 15, 85, 0, 25, 192, 15, 5, 55,+ 10, 60, 40, 192, 40, 35, 20, 40, 0, 10,+ 10, 35, 5, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192, 192, 192, 192, 192,+ 192, 192, 192, 192, 192, 192+ };+ register unsigned int hval = len;++ switch (hval)+ {+ default:+ hval += asso_values[(unsigned char)str[2]];+ /*FALLTHROUGH*/+ case 2:+ hval += asso_values[(unsigned char)str[1]];+ break;+ }+ return hval + asso_values[(unsigned char)str[len - 1]];+}++const char *+is_keyword_c (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "", "", "", "", "", "", "", "",+ "",+ "#else",+ "_Decimal128",+ "", "", "",+ "__volatile",+ "", "",+ "__volatile__ ",+ "",+ "_Decimal64",+ "inline",+ "__FUNCTION__",+ "__extension__",+ "__label__",+ "#elif",+ "__null",+ "",+ "#elifdef",+ "#elifndef",+ "while",+ "#endif",+ "if",+ "__func__",+ "__PRETTY_FUNCTION__",+ "#line",+ "return",+ "",+ "__inline",+ "__imag__ ",+ "_Decimal32",+ "switch",+ "default",+ "#include",+ "else",+ "__inline__",+ "extern",+ "",+ "__typeof",+ "",+ "union",+ "sizeof",+ "alignof",+ "int",+ "__real__ ",+ "__signed__",+ "__real",+ "thread_local",+ "#if",+ "_Static_assert",+ "break",+ "#ifdef",+ "__restrict__",+ "restrict",+ "", "", "", "",+ "#include_next",+ "",+ "false",+ "#undef",+ "alignas",+ "volatile",+ "",+ "__restrict",+ "__attribute",+ "__asm__",+ "continue",+ "_Noreturn",+ "",+ "__alignof__",+ "#define",+ "__attribute__",+ "true",+ "short",+ "struct",+ "_BitInt",+ "_Thread_local",+ "__alignof",+ "float",+ "__complex__",+ "nullptr",+ "typeof_unqual",+ "__complex",+ "const",+ "typeof",+ "typedef",+ "_Alignof",+ "case",+ "_Imaginary",+ "",+ "__const",+ "__builtin_offsetof",+ "char",+ "_Bool",+ "double",+ "",+ "__thread",+ "enum",+ "", "", "",+ "static_assert",+ "", "", "", "",+ "_Alignas",+ "constexpr",+ "",+ "__imag",+ "do",+ "__signed",+ "auto",+ "__asm",+ "", "",+ "unsigned",+ "bool",+ "", "", "",+ "register",+ "", "",+ "#embed",+ "",+ "_Generic",+ "", "", "", "",+ "for",+ "goto",+ "", "", "",+ "_Complex",+ "", "", "", "", "", "", "",+ "static",+ "", "",+ "long",+ "", "", "",+ "#warning",+ "void",+ "", "",+ "_Atomic",+ "", "", "",+ "__builtin_va_arg",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "",+ "signed"+ };++ if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)+ {+ register unsigned int key = hash (str, len);++ if (key <= MAX_HASH_VALUE)+ {+ register const char *s = wordlist[key];++ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')+ return s;+ }+ }+ return 0;+}diff --git a/llamafile/is_keyword_c.gperf b/llamafile/is_keyword_c.gperfnew file mode 100644index 00000000..4df62c56--- /dev/null+++ b/llamafile/is_keyword_c.gperf@@ -0,0 +1,112 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_c+%%+"#define"+"#elif"+"#elifdef"+"#elifndef"+"#else"+"#embed"+"#endif"+"#if"+"#ifdef"+"#include"+"#include_next"+"#line"+"#undef"+"#warning"+_Alignas+_Alignof+_Atomic+_BitInt+_Bool+_Complex+_Decimal128+_Decimal32+_Decimal64+_Generic+_Imaginary+_Noreturn+_Static_assert+_Thread_local+__FUNCTION__+__PRETTY_FUNCTION__+__alignof+__alignof__+__asm+__asm__+__attribute+__attribute__+__builtin_offsetof+__builtin_va_arg+__complex+__complex__+__const+__extension__+__func__+__imag+__imag__+__inline+__inline__+__label__+__null+__real+__real__+__restrict+__restrict__+__signed+__signed__+__thread+__typeof+__volatile+__volatile__+alignas+alignof+auto+bool+break+case+char+const+constexpr+continue+default+do+double+else+enum+extern+false+float+for+goto+if+inline+int+long+nullptr+register+restrict+return+short+signed+sizeof+static+static_assert+struct+switch+thread_local+true+typedef+typeof+typeof_unqual+union+unsigned+void+volatile+whilediff --git a/llamafile/is_keyword_cxx.c b/llamafile/is_keyword_cxx.cnew file mode 100644index 00000000..506e1f52--- /dev/null+++ b/llamafile/is_keyword_cxx.c@@ -0,0 +1,341 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf llamafile/is_keyword_cxx.gperf */+/* Computed positions: -k'1,3,5,$' */++#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \+ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \+ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \+ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \+ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \+ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \+ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \+ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \+ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \+ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \+ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \+ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \+ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \+ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \+ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \+ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \+ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \+ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \+ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \+ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \+ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \+ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \+ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))+/* The character set is not based on ISO-646. */+#error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>."+#endif++#line 1 "llamafile/is_keyword_cxx.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 142+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 19+#define MIN_HASH_VALUE 3+#define MAX_HASH_VALUE 361+/* maximum key range = 359, duplicates = 0 */++#ifdef __GNUC__+__inline+#else+#ifdef __cplusplus+inline+#endif+#endif+static unsigned int+hash (register const char *str, register size_t len)+{+ static const unsigned short asso_values[] =+ {+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 95, 362, 362, 15, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 70,+ 362, 40, 362, 362, 362, 362, 10, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 15,+ 0, 362, 362, 362, 362, 362, 362, 362, 0, 362,+ 10, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 35, 362, 10, 175, 30,+ 100, 0, 5, 25, 20, 10, 362, 0, 5, 55,+ 15, 110, 140, 40, 50, 95, 0, 20, 20, 30,+ 50, 0, 0, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362, 362, 362, 362, 362,+ 362, 362, 362, 362, 362, 362+ };+ register unsigned int hval = len;++ switch (hval)+ {+ default:+ hval += asso_values[(unsigned char)str[4]];+ /*FALLTHROUGH*/+ case 4:+ case 3:+ hval += asso_values[(unsigned char)str[2]];+ /*FALLTHROUGH*/+ case 2:+ case 1:+ hval += asso_values[(unsigned char)str[0]];+ break;+ }+ return hval + asso_values[(unsigned char)str[len - 1]];+}++const char *+is_keyword_cxx (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "", "",+ "try",+ "", "", "", "", "", "", "", "", "",+ "int",+ "",+ "false",+ "",+ "if",+ "not",+ "", "", "", "", "",+ "true",+ "#else",+ "", "",+ "#if",+ "",+ "#line",+ "#ifdef",+ "#define",+ "volatile",+ "",+ "#elif",+ "inline",+ "",+ "#elifdef",+ "#elifndef",+ "",+ "#undef",+ "",+ "#include",+ "",+ "while",+ "",+ "alignof",+ "#include_next",+ "long",+ "const",+ "#endif",+ "concept",+ "noexcept",+ "constinit",+ "const_cast",+ "__attribute",+ "char8_t",+ "__inline",+ "consteval",+ "",+ "not_eq",+ "",+ "continue",+ "",+ "union",+ "__null",+ "",+ "template",+ "__alignof",+ "__volatile",+ "extern",+ "",+ "#warning",+ "",+ "catch",+ "reinterpret_cast",+ "thread_local",+ "new",+ "enum",+ "", "",+ "__FUNCTION__",+ "__extension__",+ "",+ "using",+ "__imag",+ "__const",+ "char32_t",+ "co_return",+ "", "", "",+ "__attribute__",+ "char",+ "__inline__",+ "", "",+ "__func__",+ "else",+ "compl",+ "__alignof__",+ "virtual",+ "co_await",+ "constexpr",+ "",+ "__real",+ "wchar_t",+ "for",+ "this",+ "",+ "delete",+ "",+ "reflexpr",+ "__PRETTY_FUNCTION__",+ "throw",+ "", "",+ "char16_t",+ "",+ "float",+ "return",+ "",+ "asm",+ "auto",+ "",+ "static_cast",+ "",+ "static_assert",+ "case",+ "",+ "double",+ "default",+ "",+ "void",+ "",+ "friend",+ "alignas",+ "decltype",+ "goto",+ "", "",+ "__asm__",+ "atomic_commit",+ "",+ "atomic_noexcept",+ "xor_eq",+ "",+ "atomic_cancel",+ "", "",+ "static",+ "",+ "xor",+ "", "",+ "and_eq",+ "",+ "explicit",+ "__imag__ ",+ "__asm",+ "switch",+ "or",+ "typename",+ "", "",+ "__complex__",+ "private",+ "__volatile__ ",+ "", "", "", "", "",+ "namespace",+ "",+ "#embed",+ "",+ "operator",+ "__complex",+ "break",+ "struct",+ "dynamic_cast",+ "co_yield",+ "", "", "", "",+ "__typeof",+ "",+ "__restrict",+ "", "",+ "__thread",+ "", "",+ "export",+ "", "",+ "__real__ ",+ "__signed__",+ "", "",+ "requires",+ "", "", "", "", "", "",+ "short",+ "",+ "do",+ "and",+ "", "",+ "sizeof",+ "nullptr",+ "", "", "", "", "", "", "", "",+ "signed",+ "__restrict__",+ "register",+ "",+ "or_eq",+ "", "", "", "",+ "class",+ "",+ "mutable",+ "", "", "", "",+ "synchronized",+ "__builtin_offsetof",+ "", "", "", "",+ "unsigned",+ "", "", "",+ "typedef",+ "", "", "",+ "typeid",+ "", "",+ "__label__",+ "",+ "__builtin_va_arg",+ "",+ "__signed",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "",+ "bitor",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "",+ "bool",+ "",+ "bitand",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "", "",+ "", "", "", "", "", "", "", "",+ "protected",+ "",+ "public"+ };++ if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)+ {+ register unsigned int key = hash (str, len);++ if (key <= MAX_HASH_VALUE)+ {+ register const char *s = wordlist[key];++ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')+ return s;+ }+ }+ return 0;+}diff --git a/llamafile/is_keyword_cxx.gperf b/llamafile/is_keyword_cxx.gperfnew file mode 100644index 00000000..6dbbe7f7--- /dev/null+++ b/llamafile/is_keyword_cxx.gperf@@ -0,0 +1,150 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_cxx+%%+"#define"+"#elif"+"#elifdef"+"#elifndef"+"#else"+"#embed"+"#endif"+"#if"+"#ifdef"+"#include"+"#include_next"+"#line"+"#undef"+"#warning"+__FUNCTION__+__PRETTY_FUNCTION__+__alignof+__alignof__+__asm+__asm__+__attribute+__attribute__+__builtin_offsetof+__builtin_va_arg+__complex+__complex__+__const+__extension__+__func__+__imag+__imag__+__inline+__inline__+__label__+__null+__real+__real__+__restrict+__restrict__+__signed+__signed__+__thread+__typeof+__volatile+__volatile__+alignas+alignof+and+and_eq+asm+atomic_cancel+atomic_commit+atomic_noexcept+auto+bitand+bitor+bool+break+case+catch+char+char16_t+char32_t+char8_t+class+co_await+co_return+co_yield+compl+concept+const+const_cast+consteval+constexpr+constinit+continue+decltype+default+delete+do+double+dynamic_cast+else+enum+explicit+export+extern+false+float+for+friend+goto+if+inline+int+long+mutable+namespace+new+noexcept+not+not_eq+nullptr+operator+or+or_eq+private+protected+public+reflexpr+register+reinterpret_cast+requires+return+short+signed+sizeof+static+static_assert+static_cast+struct+switch+synchronized+template+this+thread_local+throw+true+try+typedef+typeid+typename+union+unsigned+using+virtual+void+volatile+wchar_t+while+xor+xor_eqdiff --git a/llamafile/is_keyword_java.c b/llamafile/is_keyword_java.cnew file mode 100644index 00000000..c8358b80--- /dev/null+++ b/llamafile/is_keyword_java.c@@ -0,0 +1,183 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf is_keyword_java.gperf */+/* Computed positions: -k'1,3' */++#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \+ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \+ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \+ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \+ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \+ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \+ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \+ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \+ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \+ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \+ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \+ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \+ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \+ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \+ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \+ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \+ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \+ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \+ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \+ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \+ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \+ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \+ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))+/* The character set is not based on ISO-646. */+#error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>."+#endif++#line 1 "is_keyword_java.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 50+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 12+#define MIN_HASH_VALUE 3+#define MAX_HASH_VALUE 80+/* maximum key range = 78, duplicates = 0 */++#ifdef __GNUC__+__inline+#else+#ifdef __cplusplus+inline+#endif+#endif+static unsigned int+hash (register const char *str, register size_t len)+{+ static const unsigned char asso_values[] =+ {+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 5, 5, 15,+ 25, 25, 35, 15, 81, 15, 0, 81, 0, 5,+ 0, 10, 0, 55, 5, 0, 30, 45, 0, 5,+ 0, 81, 15, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81, 81, 81, 81,+ 81, 81, 81, 81, 81, 81, 81+ };+ register unsigned int hval = len;++ switch (hval)+ {+ default:+ hval += asso_values[(unsigned char)str[2]+1];+ /*FALLTHROUGH*/+ case 2:+ case 1:+ hval += asso_values[(unsigned char)str[0]];+ break;+ }+ return hval;+}++const char *+is_keyword_java (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "", "",+ "new",+ "void",+ "short",+ "switch",+ "private",+ "strictfp",+ "protected",+ "while",+ "static",+ "boolean",+ "volatile",+ "long",+ "", "",+ "if",+ "", "", "",+ "public",+ "synchronized",+ "",+ "char",+ "class",+ "",+ "do",+ "",+ "enum",+ "const",+ "double",+ "package",+ "continue",+ "this",+ "throw",+ "throws",+ "",+ "for",+ "",+ "float",+ "assert",+ "",+ "abstract",+ "transient",+ "break",+ "",+ "default",+ "try",+ "case",+ "final",+ "native",+ "finally",+ "",+ "byte",+ "instanceof",+ "return",+ "", "",+ "else",+ "super",+ "", "",+ "int",+ "goto",+ "catch",+ "", "", "",+ "interface",+ "", "", "", "", "", "",+ "import",+ "extends",+ "", "",+ "implements"+ };++ if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)+ {+ register unsigned int key = hash (str, len);++ if (key <= MAX_HASH_VALUE)+ {+ register const char *s = wordlist[key];++ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')+ return s;+ }+ }+ return 0;+}diff --git a/llamafile/is_keyword_java.gperf b/llamafile/is_keyword_java.gperfnew file mode 100644index 00000000..f6a57f5b--- /dev/null+++ b/llamafile/is_keyword_java.gperf@@ -0,0 +1,58 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_java+%%+abstract+assert+boolean+break+byte+case+catch+char+class+const+continue+default+do+double+else+enum+extends+final+finally+float+for+goto+if+implements+import+instanceof+int+interface+long+native+new+package+private+protected+public+return+short+static+strictfp+super+switch+synchronized+this+throw+throws+transient+try+void+volatile+whilediff --git a/llamafile/is_keyword_js.c b/llamafile/is_keyword_js.cnew file mode 100644index 00000000..d8979815--- /dev/null+++ b/llamafile/is_keyword_js.c@@ -0,0 +1,152 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf is_keyword_js.gperf */+/* Computed positions: -k'1-2' */++#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \+ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \+ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \+ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \+ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \+ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \+ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \+ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \+ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \+ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \+ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \+ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \+ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \+ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \+ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \+ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \+ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \+ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \+ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \+ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \+ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \+ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \+ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))+/* The character set is not based on ISO-646. */+#error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>."+#endif++#line 1 "is_keyword_js.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 39+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 10+#define MIN_HASH_VALUE 3+#define MAX_HASH_VALUE 55+/* maximum key range = 53, duplicates = 0 */++#ifdef __GNUC__+__inline+#else+#ifdef __cplusplus+inline+#endif+#endif+static unsigned int+hash (register const char *str, register size_t len)+{+ static const unsigned char asso_values[] =+ {+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 5, 45, 0,+ 5, 0, 15, 56, 40, 0, 56, 56, 35, 15,+ 25, 0, 56, 56, 0, 10, 0, 0, 45, 10,+ 30, 25, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56, 56, 56, 56, 56,+ 56, 56, 56, 56, 56, 56+ };+ return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];+}++const char *+is_keyword_js (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "", "",+ "try",+ "true",+ "const",+ "return",+ "do",+ "continue",+ "case",+ "catch",+ "delete",+ "default",+ "debugger",+ "with",+ "super",+ "static",+ "if",+ "for",+ "",+ "await",+ "import",+ "finally",+ "function",+ "",+ "false",+ "switch",+ "in",+ "new",+ "null",+ "yield",+ "typeof",+ "", "", "",+ "instanceof",+ "export",+ "extends",+ "let",+ "else",+ "class",+ "", "", "",+ "this",+ "throw",+ "", "", "",+ "void",+ "break",+ "", "",+ "var",+ "",+ "while"+ };++ if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)+ {+ register unsigned int key = hash (str, len);++ if (key <= MAX_HASH_VALUE)+ {+ register const char *s = wordlist[key];++ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')+ return s;+ }+ }+ return 0;+}diff --git a/llamafile/is_keyword_js.gperf b/llamafile/is_keyword_js.gperfnew file mode 100644index 00000000..d53617fb--- /dev/null+++ b/llamafile/is_keyword_js.gperf@@ -0,0 +1,47 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_js+%%+await+break+case+catch+class+const+continue+debugger+default+delete+do+else+export+extends+false+finally+for+function+if+import+in+instanceof+let+new+null+return+static+super+switch+this+throw+true+try+typeof+var+void+while+with+yielddiff --git a/llamafile/is_keyword_python.c b/llamafile/is_keyword_python.cnew file mode 100644index 00000000..33f8e19d--- /dev/null+++ b/llamafile/is_keyword_python.c@@ -0,0 +1,152 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf is_keyword_python.gperf */+/* Computed positions: -k'1,$' */++#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \+ && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \+ && (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \+ && ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \+ && ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \+ && ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \+ && ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \+ && ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \+ && ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \+ && ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \+ && ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \+ && ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \+ && ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \+ && ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \+ && ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \+ && ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \+ && ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \+ && ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \+ && ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \+ && ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \+ && ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \+ && ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \+ && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))+/* The character set is not based on ISO-646. */+#error "gperf generated tables don't work with this execution character set. Please report a bug to <[email protected]>."+#endif++#line 1 "is_keyword_python.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 35+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 8+#define MIN_HASH_VALUE 2+#define MAX_HASH_VALUE 53+/* maximum key range = 52, duplicates = 0 */++#ifdef __GNUC__+__inline+#else+#ifdef __cplusplus+inline+#endif+#endif+static unsigned int+hash (register const char *str, register size_t len)+{+ static const unsigned char asso_values[] =+ {+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 15, 54, 54, 54, 54, 54, 54, 54, 25, 54,+ 54, 54, 54, 54, 20, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 10, 0, 25,+ 0, 5, 0, 0, 5, 0, 54, 0, 30, 0,+ 15, 5, 0, 54, 20, 20, 0, 54, 54, 10,+ 54, 5, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,+ 54, 54, 54, 54, 54, 54+ };+ return len + asso_values[(unsigned char)str[len - 1]] + asso_values[(unsigned char)str[0]];+}++const char *+is_keyword_python (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "",+ "if",+ "def",+ "from",+ "break",+ "import",+ "",+ "try",+ "elif",+ "yield",+ "except",+ "finally",+ "and",+ "else",+ "await",+ "assert",+ "in",+ "not",+ "with",+ "while",+ "",+ "is",+ "for",+ "pass",+ "False",+ "",+ "or",+ "",+ "True",+ "raise",+ "",+ "as",+ "del",+ "None",+ "",+ "global",+ "",+ "continue",+ "",+ "async",+ "return",+ "", "", "", "",+ "lambda",+ "", "", "",+ "class",+ "", "",+ "nonlocal"+ };++ if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)+ {+ register unsigned int key = hash (str, len);++ if (key <= MAX_HASH_VALUE)+ {+ register const char *s = wordlist[key];++ if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')+ return s;+ }+ }+ return 0;+}diff --git a/llamafile/is_keyword_python.gperf b/llamafile/is_keyword_python.gperfnew file mode 100644index 00000000..28f3fa5c--- /dev/null+++ b/llamafile/is_keyword_python.gperf@@ -0,0 +1,43 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_python+%%+False+None+True+and+as+assert+async+await+break+class+continue+def+del+elif+else+except+finally+for+from+global+if+import+in+is+lambda+nonlocal+not+or+pass+raise+return+try+while+with+yielddiff --git a/llamafile/xterm.cpp b/llamafile/xterm.cppnew file mode 100644index 00000000..8f2250ae--- /dev/null+++ b/llamafile/xterm.cpp@@ -0,0 +1,41 @@+// -*- 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 "xterm.h"++#define SQR(X) ((X) * (X))+#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40++/**+ * Quantizes 24-bit RGB to xterm256 code range [16,256).+ */+int rgb2xterm256(int rgb) {+ int av, ir, ig, ib, il, qr, qg, qb, ql, r, g, b;+ unsigned char cube[] = {0, 0137, 0207, 0257, 0327, 0377};+ r = (rgb & 0xff0000) >> 020;+ g = (rgb & 0x00ff00) >> 010;+ b = (rgb & 0x0000ff) >> 000;+ av = r * .299 + g * .587 + b * .114 + .5;+ ql = (il = av > 238 ? 23 : (av - 3) / 10) * 10 + 8;+ qr = cube[(ir = UNCUBE(r))];+ qg = cube[(ig = UNCUBE(g))];+ qb = cube[(ib = UNCUBE(b))];+ if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <= //+ SQR(ql - r) + SQR(ql - g) + SQR(ql - b))+ return ir * 36 + ig * 6 + ib + 020;+ return il + 0350;+}diff --git a/llamafile/xterm.h b/llamafile/xterm.hnew file mode 100644index 00000000..e3f02f84--- /dev/null+++ b/llamafile/xterm.h@@ -0,0 +1,20 @@+// -*- 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.++#pragma once++int rgb2xterm256(int);diff --git a/whisper.cpp/common.h b/whisper.cpp/common.hindex c4dd2887..5c2321ba 100644--- a/whisper.cpp/common.h+++ b/whisper.cpp/common.h@@ -10,6 +10,8 @@#include <thread>#include <fstream>+#include "llamafile/xterm.h"+#define COMMON_SAMPLE_RATE 16000//@@ -157,43 +159,27 @@ void sam_print_usage(int argc, char ** argv, const sam_params & params);// Terminal utils//-#define SQR(X) ((X) * (X))-#define UNCUBE(x) x < 48 ? 0 : x < 115 ? 1 : (x - 35) / 40--/**- * Quantizes 24-bit RGB to xterm256 code range [16,256).- */-static int rgb2xterm256(int r, int g, int b) {- unsigned char cube[] = {0, 0137, 0207, 0257, 0327, 0377};- int av, ir, ig, ib, il, qr, qg, qb, ql;- av = r * .299 + g * .587 + b * .114 + .5;- ql = (il = av > 238 ? 23 : (av - 3) / 10) * 10 + 8;- qr = cube[(ir = UNCUBE(r))];- qg = cube[(ig = UNCUBE(g))];- qb = cube[(ib = UNCUBE(b))];- if (SQR(qr - r) + SQR(qg - g) + SQR(qb - b) <=- SQR(ql - r) + SQR(ql - g) + SQR(ql - b))- return ir * 36 + ig * 6 + ib + 020;- return il + 0350;-}--static std::string set_xterm256_foreground(int r, int g, int b) {- int x = rgb2xterm256(r, g, b);+static std::string set_xterm256_foreground(int rgb) {+ int x = rgb2xterm256(rgb);std::ostringstream oss;oss << "\033[38;5;" << x << "m";return oss.str();}+static inline int rgb(int red, int green, int blue) {+ return (red & 255) << 16 | (green & 255) << 8 | (blue & 255);+}+// Lowest is red, middle is yellow, highest is green. Color scheme from// Paul Tol; it is colorblind friendly https://personal.sron.nl/~pault/const std::vector<std::string> k_colors = {- set_xterm256_foreground(220, 5, 12),- set_xterm256_foreground(232, 96, 28),- set_xterm256_foreground(241, 147, 45),- set_xterm256_foreground(246, 193, 65),- set_xterm256_foreground(247, 240, 86),- set_xterm256_foreground(144, 201, 135),- set_xterm256_foreground( 78, 178, 101),+ set_xterm256_foreground(rgb(220, 5, 12)),+ set_xterm256_foreground(rgb(232, 96, 28)),+ set_xterm256_foreground(rgb(241, 147, 45)),+ set_xterm256_foreground(rgb(246, 193, 65)),+ set_xterm256_foreground(rgb(247, 240, 86)),+ set_xterm256_foreground(rgb(144, 201, 135)),+ set_xterm256_foreground(rgb( 78, 178, 101)),};//