Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Support more highlighting corner cases
Justine Tunney457cefbe3585
diff --git a/llamafile/highlight.h b/llamafile/highlight.hindex 41b5fab0..54d2e286 100644--- a/llamafile/highlight.h+++ b/llamafile/highlight.h@@ -188,6 +188,7 @@ class HighlightC : public Highlight {bool is_pod_ = 0;bool is_bol_ = true;bool is_cpp_ = false;+ bool is_define_ = false;bool is_include_ = false;std::string word_;std::string heredoc_;@@ -229,6 +230,8 @@ class HighlightJs : public Highlight {void flush(std::string *result) override;private:+ int c_ = 0;+ int u_ = 0;int t_ = 0;int expect_;std::string word_;diff --git a/llamafile/highlight_asm.cpp b/llamafile/highlight_asm.cppindex 8e46f58d..be7482e5 100644--- a/llamafile/highlight_asm.cpp+++ b/llamafile/highlight_asm.cpp@@ -105,6 +105,8 @@ void HighlightAsm::feed(std::string *r, std::string_view input) {u_ = 0;c_ = c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_c.cpp b/llamafile/highlight_c.cppindex a769d837..43bcb6da 100644--- a/llamafile/highlight_c.cpp+++ b/llamafile/highlight_c.cpp@@ -38,6 +38,7 @@ enum {TRIGRAPH,CPP_LT,BACKSLASH,+ UNIVERSAL,};HighlightC::HighlightC(is_keyword_f *is_keyword, //@@ -68,7 +69,7 @@ void HighlightC::feed(std::string *r, std::string_view input) {} else if (!isascii(c) || isalpha(c) || c == '_') {t_ = WORD;word_ += c;- } else if (c == '#' && is_bol_) {+ } else if (c == '#' && is_bol_ && !is_cpp_ && !is_define_) {is_cpp_ = true;*r += HI_BUILTIN;*r += '#';@@ -76,9 +77,8 @@ void HighlightC::feed(std::string *r, std::string_view input) {*r += HI_STRING;*r += '<';t_ = CPP_LT;- } else if (c == '\\' && is_cpp_) {+ } else if (c == '\\') {t_ = BACKSLASH;- *r += '\\';} else if (c == '/') {t_ = SLASH;} else if (c == '?') {@@ -98,6 +98,7 @@ void HighlightC::feed(std::string *r, std::string_view input) {is_include_ = false;is_cpp_ = false;}+ is_define_ = false;} else {*r += c;}@@ -115,6 +116,10 @@ void HighlightC::feed(std::string *r, std::string_view input) {*r += HI_RESET;if (word_ == "include" || word_ == "include_next")is_include_ = true;+ if (word_ == "define") {+ is_cpp_ = false;+ is_define_ = true;+ }} else if (is_keyword_c_constant(word_.data(), word_.size())) {*r += HI_CONSTANT;*r += word_;@@ -156,8 +161,35 @@ void HighlightC::feed(std::string *r, std::string_view input) {break;case BACKSLASH:- *r += c;- t_ = NORMAL;+ if (c == 'u') {+ *r += HI_ESCAPE;+ *r += "\\u";+ t_ = UNIVERSAL;+ i_ = 4;+ } else if (c == 'U') {+ *r += HI_ESCAPE;+ *r += "\\U";+ t_ = UNIVERSAL;+ i_ = 8;+ } else {+ *r += '\\';+ *r += c;+ t_ = NORMAL;+ }+ break;++ case UNIVERSAL:+ if (isascii(c) && isxdigit(c)) {+ *r += c;+ if (!--i_) {+ *r += HI_RESET;+ t_ = NORMAL;+ }+ } else {+ *r += HI_RESET;+ t_ = NORMAL;+ goto Normal;+ }break;case QUESTION:@@ -386,6 +418,11 @@ void HighlightC::flush(std::string *r) {if (is_cpp_)*r += HI_RESET;break;+ case BACKSLASH:+ *r += '\\';+ if (is_cpp_)+ *r += HI_RESET;+ break;case QUOTE:case QUOTE_BACKSLASH:case DQUOTE:@@ -404,8 +441,9 @@ void HighlightC::flush(std::string *r) {break;}is_include_ = false;- is_pod_ = false;+ is_define_ = false;is_cpp_ = false;+ is_pod_ = false;is_bol_ = true;t_ = NORMAL;}diff --git a/llamafile/highlight_js.cpp b/llamafile/highlight_js.cppindex 0d6246e4..f9ce7a85 100644--- a/llamafile/highlight_js.cpp+++ b/llamafile/highlight_js.cpp@@ -16,7 +16,8 @@// limitations under the License.#include "highlight.h"-+#include "string.h"+#include <cosmo.h>#include <ctype.h>enum {@@ -49,52 +50,85 @@ HighlightJs::HighlightJs() {HighlightJs::~HighlightJs() {}+// https://262.ecma-international.org/12.0/#sec-line-terminators+static bool is_line_terminator(wchar_t c) {+ switch (c) {+ case '\r':+ case '\n':+ case 0x2028: // LINE SEPARATOR+ case 0x2029: // PARAGRAPH SEPARATOR+ return true;+ default:+ return false;+ }+}+void HighlightJs::feed(std::string *r, std::string_view input) {- int c;for (size_t i = 0; i < input.size(); ++i) {- c = input[i] & 255;+ 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;+ }+ if (c == '\r')+ continue;+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:case NORMAL:if (!isascii(c) || isalpha(c) || c == '_') {t_ = WORD;- word_ += c;+ append_wchar(&word_, c);} else if (c == '/') {t_ = SLASH;} else if (c == '\'') {t_ = QUOTE;*r += HI_STRING;- *r += c;+ append_wchar(r, c);expect_ = EXPECT_OPERATOR;} else if (c == '"') {t_ = DQUOTE;*r += HI_STRING;- *r += c;+ append_wchar(r, c);expect_ = EXPECT_OPERATOR;} else if (c == '`') {t_ = TICK;*r += HI_STRING;- *r += c;+ append_wchar(r, c);expect_ = EXPECT_OPERATOR;} else if (c == ')' || c == '}' || c == ']') {expect_ = EXPECT_OPERATOR;- *r += c;+ append_wchar(r, c);} else if (ispunct(c)) {expect_ = EXPECT_VALUE;- *r += c;+ append_wchar(r, c);} else if (isdigit(c)) {expect_ = EXPECT_OPERATOR;- *r += c;+ append_wchar(r, c);} else {- *r += c;+ append_wchar(r, c);}break;Word:case WORD:if (!isascii(c) || isalnum(c) || c == '_') {- word_ += c;+ append_wchar(&word_, c);} else {if (is_keyword_js(word_.data(), word_.size())) {*r += HI_KEYWORD;@@ -134,7 +168,7 @@ void HighlightJs::feed(std::string *r, std::string_view input) {expect_ = EXPECT_OPERATOR;*r += HI_STRING;*r += '/';- *r += c;+ append_wchar(r, c);if (c == '\\') {t_ = REGEX_BACKSLASH;} else if (c == '[') {@@ -150,21 +184,21 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case SLASH_SLASH:- *r += c;- if (c == '\n') {+ append_wchar(r, c);+ if (is_line_terminator(c)) {*r += HI_RESET;t_ = NORMAL;}break;case SLASH_STAR:- *r += c;+ append_wchar(r, c);if (c == '*')t_ = SLASH_STAR_STAR;break;case SLASH_STAR_STAR:- *r += c;+ append_wchar(r, c);if (c == '/') {*r += HI_RESET;t_ = NORMAL;@@ -174,7 +208,7 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case QUOTE:- *r += c;+ append_wchar(r, c);if (c == '\'') {*r += HI_RESET;t_ = NORMAL;@@ -184,12 +218,12 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case QUOTE_BACKSLASH:- *r += c;+ append_wchar(r, c);t_ = QUOTE;break;case DQUOTE:- *r += c;+ append_wchar(r, c);if (c == '"') {*r += HI_RESET;t_ = NORMAL;@@ -199,12 +233,12 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case DQUOTE_BACKSLASH:- *r += c;+ append_wchar(r, c);t_ = DQUOTE;break;case TICK:- *r += c;+ append_wchar(r, c);if (c == '`') {*r += HI_RESET;t_ = NORMAL;@@ -214,12 +248,12 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case TICK_BACKSLASH:- *r += c;+ append_wchar(r, c);t_ = TICK;break;case REGEX:- *r += c;+ append_wchar(r, c);if (c == '/') {*r += HI_RESET;t_ = NORMAL;@@ -231,13 +265,13 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case REGEX_BACKSLASH:- *r += c;+ append_wchar(r, c);t_ = REGEX;break;case REGEX_SQUARE:// because /[/]/g is valid code- *r += c;+ append_wchar(r, c);if (c == '\\') {t_ = REGEX_SQUARE_BACKSLASH;} else if (c == ']') {@@ -246,7 +280,7 @@ void HighlightJs::feed(std::string *r, std::string_view input) {break;case REGEX_SQUARE_BACKSLASH:- *r += c;+ append_wchar(r, c);t_ = REGEX_SQUARE;break;@@ -297,5 +331,7 @@ void HighlightJs::flush(std::string *r) {default:break;}+ c_ = 0;+ u_ = 0;t_ = NORMAL;}diff --git a/llamafile/highlight_make.cpp b/llamafile/highlight_make.cppindex a1811827..ecca27ae 100644--- a/llamafile/highlight_make.cpp+++ b/llamafile/highlight_make.cpp@@ -72,6 +72,8 @@ void HighlightMake::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_markdown.cpp b/llamafile/highlight_markdown.cppindex dd93dc8d..edcccd06 100644--- a/llamafile/highlight_markdown.cpp+++ b/llamafile/highlight_markdown.cpp@@ -65,6 +65,8 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_perl.cpp b/llamafile/highlight_perl.cppindex ddb7f17f..3c8a88c3 100644--- a/llamafile/highlight_perl.cpp+++ b/llamafile/highlight_perl.cpp@@ -168,6 +168,8 @@ void HighlightPerl::feed(std::string *r, std::string_view input) {u_ = 0;c_ = c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_shell.cpp b/llamafile/highlight_shell.cppindex f640107a..79ec29fc 100644--- a/llamafile/highlight_shell.cpp+++ b/llamafile/highlight_shell.cpp@@ -66,6 +66,8 @@ void HighlightShell::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_swift.cpp b/llamafile/highlight_swift.cppindex 035aff7c..4eb5ee55 100644--- a/llamafile/highlight_swift.cpp+++ b/llamafile/highlight_swift.cpp@@ -84,6 +84,8 @@ void HighlightSwift::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_tcl.cpp b/llamafile/highlight_tcl.cppindex dc83d36a..642c9d9e 100644--- a/llamafile/highlight_tcl.cpp+++ b/llamafile/highlight_tcl.cpp@@ -58,6 +58,8 @@ void HighlightTcl::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_tex.cpp b/llamafile/highlight_tex.cppindex 3efa06db..a7557d1f 100644--- a/llamafile/highlight_tex.cpp+++ b/llamafile/highlight_tex.cpp@@ -59,6 +59,8 @@ void HighlightTex::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_typescript.cpp b/llamafile/highlight_typescript.cppindex 5add1641..5680e76b 100644--- a/llamafile/highlight_typescript.cpp+++ b/llamafile/highlight_typescript.cpp@@ -71,6 +71,8 @@ void HighlightTypescript::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal:diff --git a/llamafile/highlight_zig.cpp b/llamafile/highlight_zig.cppindex 9a3bb042..db23815b 100644--- a/llamafile/highlight_zig.cpp+++ b/llamafile/highlight_zig.cpp@@ -59,6 +59,8 @@ void HighlightZig::feed(std::string *r, std::string_view input) {u_ = 0;c = b;}+ if (c == 0xFEFF)+ continue; // utf-8 bomswitch (t_) {Normal: