Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Syntax highlight D properly
Justine Tunneyd30da30e3cf8
diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cppindex 97c85030..c4d3b85e 100644--- a/llamafile/highlight.cpp+++ b/llamafile/highlight.cpp@@ -17,219 +17,198 @@#include "highlight.h"-Highlight *Highlight::create(const std::string_view &lang) {+struct HighlightEntry {+ char *name;+ Highlight *(*ctor)(void);+};++extern "C" Highlight *highlight_create_ada_(void) {+ return new HighlightAda;+}++extern "C" Highlight *highlight_create_asm_(void) {+ return new HighlightAsm;+}++extern "C" Highlight *highlight_create_basic_(void) {+ return new HighlightBasic;+}++extern "C" Highlight *highlight_create_bnf_(void) {+ return new HighlightBnf;+}++extern "C" Highlight *highlight_create_c_(void) {+ return new HighlightC(is_keyword_c, //+ is_keyword_c_type, //+ is_keyword_c_builtin, //+ is_keyword_c_constant);+}++extern "C" Highlight *highlight_create_cxx_(void) {+ return new HighlightC(is_keyword_cxx, //+ is_keyword_c_type, //+ is_keyword_c_builtin, //+ is_keyword_c_constant);+}++extern "C" Highlight *highlight_create_cmake_(void) {+ return new HighlightCmake;+}++extern "C" Highlight *highlight_create_cobol_(void) {+ return new HighlightCobol;+}++extern "C" Highlight *highlight_create_csharp_(void) {+ return new HighlightCsharp;+}++extern "C" Highlight *highlight_create_css_(void) {+ return new HighlightCss;+}++extern "C" Highlight *highlight_create_d_(void) {+ return new HighlightD;+}++extern "C" Highlight *highlight_create_forth_(void) {+ return new HighlightForth;+}++extern "C" Highlight *highlight_create_fortran_(void) {+ return new HighlightFortran;+}++extern "C" Highlight *highlight_create_go_(void) {+ return new HighlightGo;+}++extern "C" Highlight *highlight_create_haskell_(void) {+ return new HighlightHaskell;+}++extern "C" Highlight *highlight_create_html_(void) {+ return new HighlightHtml;+}++extern "C" Highlight *highlight_create_java_(void) {+ return new HighlightJava;+}++extern "C" Highlight *highlight_create_js_(void) {+ return new HighlightJs;+}++extern "C" Highlight *highlight_create_julia_(void) {+ return new HighlightJulia;+}++extern "C" Highlight *highlight_create_kotlin_(void) {+ return new HighlightKotlin;+}++extern "C" Highlight *highlight_create_ld_(void) {+ return new HighlightLd;+}++extern "C" Highlight *highlight_create_lisp_(void) {+ return new HighlightLisp;+}- if (lang == "txt")- return new HighlightTxt;-- if (lang == "md" || //- lang == "markdown")- return new HighlightMarkdown;-- if (lang == "c" || //- lang == "h" || //- lang == "m")- return new HighlightC(is_keyword_c, //- is_keyword_c_type, //- is_keyword_c_builtin, //- is_keyword_c_constant);-- if (lang == "c++" || //- lang == "cxx" || //- lang == "cpp" || //- lang == "hpp" || //- lang == "cc" || //- lang == "cu")- return new HighlightC(is_keyword_cxx, //- is_keyword_c_type, //- is_keyword_c_builtin, //- is_keyword_c_constant);-- if (lang == "s" || //- lang == "asm" || //- lang == "nasm" || //- lang == "yasm" || //- lang == "fasm" || //- lang == "assembly" || //- lang == "assembler")- return new HighlightAsm;-- if (lang == "ld" || //- lang == "lds" || //- lang == "ld-script")- return new HighlightLd;-- if (lang == "js" || //- lang == "json" || //- lang == "javascript")- return new HighlightJs;-- if (lang == "ts" || //- lang == "typescript")- return new HighlightTypescript;-- if (lang == "java")- return new HighlightJava;-- if (lang == "py" || //- lang == "python")- return new HighlightPython;-- if (lang == "rs" || //- lang == "rust")- return new HighlightRust;-- if (lang == "f" || //- lang == "fortran")- return new HighlightFortran;-- if (lang == "cob" || //- lang == "cbl" || //- lang == "cobol")- return new HighlightCobol;-- if (lang == "pas" || //- lang == "pascal" || //- lang == "delphi")- return new HighlightPascal;-- if (lang == "go")- return new HighlightGo;-- if (lang == "sql")- return new HighlightSql;-- if (lang == "css")- return new HighlightCss;-- if (lang == "html" || //- lang == "htm" || //- lang == "xhtml" || //- lang == "xml")- return new HighlightHtml;-- if (lang == "php")- return new HighlightHtml; // sic-- if (lang == "php!")- return new HighlightPhp;-- if (lang == "csharp" || //- lang == "cs" || //- lang == "c#")- return new HighlightCsharp;-- if (lang == "kt" || //- lang == "kts" || //- lang == "kotlin")- return new HighlightKotlin;-- if (lang == "sc" || //- lang == "sbt" || //- lang == "scala")- return new HighlightScala;-- if (lang == "lua")- return new HighlightLua;-- if (lang == "lisp" || //- lang == "el" || //- lang == "elisp" || //- lang == "cl" || //- lang == "clisp" || //- lang == "scheme" || //- lang == "racket" || //- lang == "clojure")- return new HighlightLisp;-- if (lang == "ada" || //- lang == "adb")- return new HighlightAda;-- if (lang == "haskell" || //- lang == "hs")- return new HighlightHaskell;-- if (lang == "perl" || //- lang == "pl")- return new HighlightPerl;-- if (lang == "shell" || //- lang == "bash" || //- lang == "sh" || //- lang == "ksh")- return new HighlightShell;-- if (lang == "swift")- return new HighlightSwift;-- if (lang == "d")- return new HighlightC(is_keyword_d, nullptr, nullptr, is_keyword_d_constant);-- if (lang == "r")- return new HighlightR;-- if (lang == "zig")- return new HighlightZig;-- if (lang == "tcl")- return new HighlightTcl;-- if (lang == "m4" || //- lang == "ac")- return new HighlightM4;-- if (lang == "ruby" || //- lang == "rb")- return new HighlightRuby;-- if (lang == "tex" || //- lang == "latex")- return new HighlightTex;-- if (lang == "fs" || //- lang == "4th" || //- lang == "frt" || //- lang == "fth" || //- lang == "forth")- return new HighlightForth;-- if (lang == "mk" || //- lang == "make" || //- lang == "gmake" || //- lang == "makefile" || //- lang == "gmakefile")- return new HighlightMake;-- if (lang == "vb" || //- lang == "vba" || //- lang == "vbs" || //- lang == "bas" || //- lang == "basic" || //- lang == "vb.net" || //- lang == "qbasic" || //- lang == "freebasic")- return new HighlightBasic;-- if (lang == "matlab")- return new HighlightMatlab;-- if (lang == "jl" || //- lang == "julia")- return new HighlightJulia;-- if (lang == "ml" || //- lang == "mli" || //- lang == "ocaml")- return new HighlightOcaml;-- if (lang == "cmake")- return new HighlightCmake;-- if (lang == "bnf" || //- lang == "abnf" || //- lang == "grammar")- return new HighlightBnf;+extern "C" Highlight *highlight_create_lua_(void) {+ return new HighlightLua;+}++extern "C" Highlight *highlight_create_m4_(void) {+ return new HighlightM4;+}++extern "C" Highlight *highlight_create_make_(void) {+ return new HighlightMake;+}++extern "C" Highlight *highlight_create_markdown_(void) {+ return new HighlightMarkdown;+}++extern "C" Highlight *highlight_create_matlab_(void) {+ return new HighlightMatlab;+}++extern "C" Highlight *highlight_create_ocaml_(void) {+ return new HighlightOcaml;+}++extern "C" Highlight *highlight_create_pascal_(void) {+ return new HighlightPascal;+}++extern "C" Highlight *highlight_create_perl_(void) {+ return new HighlightPerl;+}++extern "C" Highlight *highlight_create_php_(void) {+ return new HighlightPhp;+}++extern "C" Highlight *highlight_create_python_(void) {+ return new HighlightPython;+}++extern "C" Highlight *highlight_create_r_(void) {+ return new HighlightR;+}++extern "C" Highlight *highlight_create_ruby_(void) {+ return new HighlightRuby;+}+extern "C" Highlight *highlight_create_rust_(void) {+ return new HighlightRust;+}++extern "C" Highlight *highlight_create_scala_(void) {+ return new HighlightScala;+}++extern "C" Highlight *highlight_create_shell_(void) {+ return new HighlightShell;+}++extern "C" Highlight *highlight_create_sql_(void) {+ return new HighlightSql;+}++extern "C" Highlight *highlight_create_swift_(void) {+ return new HighlightSwift;+}++extern "C" Highlight *highlight_create_tcl_(void) {+ return new HighlightTcl;+}++extern "C" Highlight *highlight_create_tex_(void) {+ return new HighlightTex;+}++extern "C" Highlight *highlight_create_txt_(void) {+ return new HighlightTxt;+}++extern "C" Highlight *highlight_create_typescript_(void) {+ return new HighlightTypescript;+}++extern "C" Highlight *highlight_create_zig_(void) {+ return new HighlightZig;+}++extern "C" const HighlightEntry *highlight_lookup_(const char *str, size_t len);++Highlight *Highlight::create(const std::string_view &lang) {+ const HighlightEntry *slot;+ if ((slot = highlight_lookup_(lang.data(), lang.size())))+ return slot->ctor();return nullptr;}diff --git a/llamafile/highlight.h b/llamafile/highlight.hindex fa50092b..9d52f64f 100644--- a/llamafile/highlight.h+++ b/llamafile/highlight.h@@ -210,6 +210,25 @@ class HighlightC : public Highlight {is_keyword_f *is_constant_;};+class HighlightD : public Highlight {+ public:+ HighlightD();+ ~HighlightD() 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 depth_ = 0;+ unsigned char opener_ = 0;+ unsigned char closer_ = 0;+ std::string heredoc_;+ std::string heredoc2_;+ std::string word_;+};+class HighlightJava : public Highlight {public:HighlightJava();diff --git a/llamafile/highlight_create.gperf b/llamafile/highlight_create.gperfnew file mode 100644index 00000000..2ba52745--- /dev/null+++ b/llamafile/highlight_create.gperf@@ -0,0 +1,175 @@+%{+#include <string.h>+#include <libc/str/tab.h>+#define GPERF_DOWNCASE+struct Highlight;+struct Highlight *highlight_create_ada_(void);+struct Highlight *highlight_create_asm_(void);+struct Highlight *highlight_create_basic_(void);+struct Highlight *highlight_create_bnf_(void);+struct Highlight *highlight_create_c_(void);+struct Highlight *highlight_create_cmake_(void);+struct Highlight *highlight_create_cobol_(void);+struct Highlight *highlight_create_csharp_(void);+struct Highlight *highlight_create_css_(void);+struct Highlight *highlight_create_cxx_(void);+struct Highlight *highlight_create_d_(void);+struct Highlight *highlight_create_forth_(void);+struct Highlight *highlight_create_fortran_(void);+struct Highlight *highlight_create_go_(void);+struct Highlight *highlight_create_haskell_(void);+struct Highlight *highlight_create_html_(void);+struct Highlight *highlight_create_java_(void);+struct Highlight *highlight_create_js_(void);+struct Highlight *highlight_create_julia_(void);+struct Highlight *highlight_create_kotlin_(void);+struct Highlight *highlight_create_ld_(void);+struct Highlight *highlight_create_lisp_(void);+struct Highlight *highlight_create_lua_(void);+struct Highlight *highlight_create_m4_(void);+struct Highlight *highlight_create_make_(void);+struct Highlight *highlight_create_markdown_(void);+struct Highlight *highlight_create_matlab_(void);+struct Highlight *highlight_create_ocaml_(void);+struct Highlight *highlight_create_pascal_(void);+struct Highlight *highlight_create_perl_(void);+struct Highlight *highlight_create_php_(void);+struct Highlight *highlight_create_python_(void);+struct Highlight *highlight_create_r_(void);+struct Highlight *highlight_create_ruby_(void);+struct Highlight *highlight_create_rust_(void);+struct Highlight *highlight_create_scala_(void);+struct Highlight *highlight_create_shell_(void);+struct Highlight *highlight_create_sql_(void);+struct Highlight *highlight_create_swift_(void);+struct Highlight *highlight_create_tcl_(void);+struct Highlight *highlight_create_tex_(void);+struct Highlight *highlight_create_txt_(void);+struct Highlight *highlight_create_typescript_(void);+struct Highlight *highlight_create_zig_(void);+%}+%compare-strncmp+%ignore-case+%language=ANSI-C+%readonly-tables+%struct-type+%define lookup-function-name highlight_lookup_+struct HighlightEntry { char *name; struct Highlight* (*ctor)(void); };+%%+txt, highlight_create_txt_+md, highlight_create_markdown_+markdown, highlight_create_markdown_+c, highlight_create_c_+h, highlight_create_c_+m, highlight_create_c_+c++, highlight_create_cxx_+cxx, highlight_create_cxx_+cpp, highlight_create_cxx_+hpp, highlight_create_cxx_+cc, highlight_create_cxx_+cu, highlight_create_cxx_+s, highlight_create_asm_+asm, highlight_create_asm_+nasm, highlight_create_asm_+yasm, highlight_create_asm_+fasm, highlight_create_asm_+assembly, highlight_create_asm_+assembler, highlight_create_asm_+ld, highlight_create_ld_+lds, highlight_create_ld_+ld-script, highlight_create_ld_+js, highlight_create_js_+json, highlight_create_js_+javascript, highlight_create_js_+ts, highlight_create_typescript_+typescript, highlight_create_typescript_+java, highlight_create_java_+py, highlight_create_python_+python, highlight_create_python_+rs, highlight_create_rust_+rust, highlight_create_rust_+f, highlight_create_fortran_+fortran, highlight_create_fortran_+cob, highlight_create_cobol_+cbl, highlight_create_cobol_+cobol, highlight_create_cobol_+pas, highlight_create_pascal_+pascal, highlight_create_pascal_+delphi, highlight_create_pascal_+go, highlight_create_go_+sql, highlight_create_sql_+css, highlight_create_css_+html, highlight_create_html_+htm, highlight_create_html_+xhtml, highlight_create_html_+xml, highlight_create_html_+php, highlight_create_html_+php!, highlight_create_php_+csharp, highlight_create_csharp_+cs, highlight_create_csharp_+c#, highlight_create_csharp_+kt, highlight_create_kotlin_+kts, highlight_create_kotlin_+kotlin, highlight_create_kotlin_+sc, highlight_create_scala_+sbt, highlight_create_scala_+scala, highlight_create_scala_+lua, highlight_create_lua_+lisp, highlight_create_lisp_+el, highlight_create_lisp_+elisp, highlight_create_lisp_+cl, highlight_create_lisp_+clisp, highlight_create_lisp_+scheme, highlight_create_lisp_+racket, highlight_create_lisp_+clojure, highlight_create_lisp_+ada, highlight_create_ada_+adb, highlight_create_ada_+hs, highlight_create_haskell_+haskell, highlight_create_haskell_+pl, highlight_create_perl_+perl, highlight_create_perl_+shell, highlight_create_shell_+bash, highlight_create_shell_+sh, highlight_create_shell_+ksh, highlight_create_shell_+swift, highlight_create_swift_+d, highlight_create_d_+r, highlight_create_r_+zig, highlight_create_zig_+tcl, highlight_create_tcl_+m4, highlight_create_m4_+ac, highlight_create_m4_+rb, highlight_create_ruby_+ruby, highlight_create_ruby_+tex, highlight_create_tex_+latex, highlight_create_tex_+fs, highlight_create_forth_+4th, highlight_create_forth_+frt, highlight_create_forth_+fth, highlight_create_forth_+forth, highlight_create_forth_+mk, highlight_create_make_+make, highlight_create_make_+gmake, highlight_create_make_+makefile, highlight_create_make_+gmakefile, highlight_create_make_+vb, highlight_create_basic_+vba, highlight_create_basic_+vbs, highlight_create_basic_+bas, highlight_create_basic_+basic, highlight_create_basic_+vb.net, highlight_create_basic_+qbasic, highlight_create_basic_+freebasic, highlight_create_basic_+matlab, highlight_create_matlab_+jl, highlight_create_julia_+julia, highlight_create_julia_+ml, highlight_create_ocaml_+mli, highlight_create_ocaml_+ocaml, highlight_create_ocaml_+cmake, highlight_create_cmake_+bnf, highlight_create_bnf_+abnf, highlight_create_bnf_+gbnf, highlight_create_bnf_+grammar, highlight_create_bnf_diff --git a/llamafile/highlight_d.cpp b/llamafile/highlight_d.cppnew file mode 100644index 00000000..0e038529--- /dev/null+++ b/llamafile/highlight_d.cpp@@ -0,0 +1,510 @@+// -*- 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>++// D probably has the best lexical syntax documentation of any language.+// https://dlang.org/spec/lex.html++enum {+ NORMAL,+ WORD,+ QUOTE,+ QUOTE_BACKSLASH,+ DQUOTE,+ DQUOTE_BACKSLASH,+ SLASH,+ SLASH_SLASH,+ SLASH_STAR,+ SLASH_STAR_STAR,+ SLASH_PLUS,+ SLASH_PLUS_PLUS,+ SLASH_PLUS_SLASH,+ BACKTICK,+ R,+ R_DQUOTE,+ Q,+ Q_DQUOTE,+ Q_DQUOTE_STRING,+ Q_DQUOTE_STRING_END,+ Q_DQUOTE_IDENT,+ Q_DQUOTE_HEREDOC,+ Q_DQUOTE_HEREDOC_BOL,+ Q_DQUOTE_HEREDOC_END,+ X,+ X_DQUOTE,+};++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;+ }+}++static int mirror(int c) {+ switch (c) {+ case '(':+ return ')';+ case '{':+ return '}';+ case '[':+ return ']';+ case '<':+ return '>';+ default:+ return c;+ }+}++HighlightD::HighlightD() {+}++HighlightD::~HighlightD() {+}++void HighlightD::feed(std::string *r, std::string_view input) {+ int c;+ 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;+ }+ if (c == '\r')+ continue;+ if (c == 0xFEFF)+ continue; // utf-8 bom+ switch (t_) {++ Normal:+ case NORMAL:+ if (c == 'r') {+ t_ = R;+ } else if (c == 'q') {+ t_ = Q;+ } else if (c == 'x') {+ t_ = X;+ } else if (!isascii(c) || isalpha(c) || c == '_') {+ t_ = WORD;+ lf::append_wchar(&word_, c);+ } else if (c == '`') {+ t_ = BACKTICK;+ *r += HI_STRING;+ *r += '`';+ } else if (c == '/') {+ t_ = SLASH;+ } else if (c == '\'') {+ t_ = QUOTE;+ *r += HI_STRING;+ *r += '\'';+ } else if (c == '"') {+ t_ = DQUOTE;+ *r += HI_STRING;+ *r += '"';+ } else {+ lf::append_wchar(r, c);+ }+ break;++ Word:+ case WORD:+ if (!isascii(c) || isalnum(c) || c == '_') {+ lf::append_wchar(&word_, c);+ } else {+ if (is_keyword_d(word_.data(), word_.size())) {+ *r += HI_KEYWORD;+ *r += word_;+ *r += HI_RESET;+ } else if (is_keyword_d_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 if (c == '+') {+ *r += HI_COMMENT;+ *r += "/+";+ t_ = SLASH_PLUS;+ depth_ = 1;+ } else {+ *r += '/';+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case SLASH_SLASH:+ lf::append_wchar(r, c);+ if (is_line_terminator(c)) {+ *r += HI_RESET;+ t_ = NORMAL;+ }+ break;++ case SLASH_STAR:+ lf::append_wchar(r, c);+ if (c == '*')+ t_ = SLASH_STAR_STAR;+ break;++ case SLASH_STAR_STAR:+ lf::append_wchar(r, c);+ if (c == '/') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c != '*') {+ t_ = SLASH_STAR;+ }+ break;++ case SLASH_PLUS:+ lf::append_wchar(r, c);+ if (c == '+') {+ t_ = SLASH_PLUS_PLUS;+ } else if (c == '/') {+ t_ = SLASH_PLUS_SLASH;+ }+ break;++ case SLASH_PLUS_PLUS:+ lf::append_wchar(r, c);+ if (c == '/') {+ if (!--depth_) {+ *r += HI_RESET;+ t_ = NORMAL;+ } else {+ t_ = SLASH_PLUS;+ }+ } else if (c != '+') {+ t_ = SLASH_PLUS;+ }+ break;++ case SLASH_PLUS_SLASH:+ lf::append_wchar(r, c);+ if (c == '+') {+ ++depth_;+ t_ = SLASH_PLUS;+ } else if (c != '/') {+ t_ = SLASH_PLUS;+ }+ break;++ case QUOTE:+ lf::append_wchar(r, c);+ if (c == '\'') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c == '\\') {+ t_ = QUOTE_BACKSLASH;+ }+ break;++ case QUOTE_BACKSLASH:+ lf::append_wchar(r, c);+ t_ = QUOTE;+ break;++ case DQUOTE:+ lf::append_wchar(r, c);+ if (c == '"') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c == '\\') {+ t_ = DQUOTE_BACKSLASH;+ }+ break;++ case DQUOTE_BACKSLASH:+ lf::append_wchar(r, c);+ t_ = DQUOTE;+ break;++ case R:+ if (c == '"') {+ t_ = R_DQUOTE;+ *r += 'r';+ *r += HI_STRING;+ *r += '"';+ } else {+ word_ += 'r';+ t_ = WORD;+ goto Word;+ }+ break;++ case R_DQUOTE:+ lf::append_wchar(r, c);+ if (c == '"') {+ *r += HI_RESET;+ t_ = NORMAL;+ }+ break;++ case BACKTICK:+ lf::append_wchar(r, c);+ if (c == '`') {+ *r += HI_RESET;+ t_ = NORMAL;+ }+ break;++ case Q:+ if (c == '"') {+ t_ = Q_DQUOTE;+ *r += 'q';+ *r += HI_STRING;+ *r += '"';+ } else {+ word_ += 'q';+ t_ = WORD;+ goto Word;+ }+ break;++ case Q_DQUOTE:+ lf::append_wchar(r, c);+ if (!isascii(c) || isalpha(c) || c == '_') {+ heredoc_.clear();+ lf::append_wchar(&heredoc_, c);+ t_ = Q_DQUOTE_IDENT;+ } else {+ opener_ = c;+ closer_ = mirror(c);+ depth_ = 1;+ t_ = Q_DQUOTE_STRING;+ }+ break;++ QDquoteString:+ case Q_DQUOTE_STRING:+ if (c == closer_) {+ if (closer_ != opener_) {+ if (depth_) {+ --depth_;+ } else {+ *r += HI_RESET;+ *r += HI_WARNING;+ }+ if (!depth_) {+ t_ = Q_DQUOTE_STRING_END;+ }+ } else {+ t_ = Q_DQUOTE_STRING_END;+ }+ } else if (c == opener_ && closer_ != opener_) {+ ++depth_;+ }+ lf::append_wchar(r, c);+ break;++ case Q_DQUOTE_STRING_END:+ if (c == '"') {+ lf::append_wchar(r, c);+ *r += HI_RESET;+ t_ = NORMAL;+ } else {+ *r += HI_RESET;+ *r += HI_WARNING;+ t_ = Q_DQUOTE_STRING;+ goto QDquoteString;+ }+ break;++ case Q_DQUOTE_IDENT:+ if (is_line_terminator(c)) {+ t_ = Q_DQUOTE_HEREDOC_BOL;+ heredoc2_.clear();+ } else if (!isascii(c) || isalpha(c) || c == '_') {+ lf::append_wchar(&heredoc_, c);+ } else {+ *r += HI_RESET;+ *r += HI_WARNING;+ t_ = Q_DQUOTE_HEREDOC_BOL;+ heredoc2_.clear();+ }+ lf::append_wchar(r, c);+ break;++ QDquoteHeredoc:+ case Q_DQUOTE_HEREDOC:+ lf::append_wchar(r, c);+ if (is_line_terminator(c)) {+ t_ = Q_DQUOTE_HEREDOC_BOL;+ heredoc2_.clear();+ }+ break;++ case Q_DQUOTE_HEREDOC_BOL:+ lf::append_wchar(r, c);+ if (is_line_terminator(c)) {+ t_ = Q_DQUOTE_HEREDOC_BOL;+ heredoc2_.clear();+ } else {+ lf::append_wchar(&heredoc2_, c);+ if (heredoc_.starts_with(heredoc2_)) {+ if (heredoc_ == heredoc2_) {+ t_ = Q_DQUOTE_HEREDOC_END;+ }+ } else {+ t_ = Q_DQUOTE_HEREDOC;+ }+ }+ break;++ case Q_DQUOTE_HEREDOC_END:+ if (c == '"') {+ lf::append_wchar(r, c);+ *r += HI_RESET;+ t_ = NORMAL;+ } else {+ *r += HI_RESET;+ *r += HI_WARNING;+ t_ = Q_DQUOTE_HEREDOC;+ goto QDquoteHeredoc;+ }+ break;++ case X:+ if (c == '"') {+ *r += 'x';+ *r += HI_STRING;+ *r += '"';+ t_ = X_DQUOTE;+ } else {+ word_ += 'x';+ t_ = WORD;+ goto Word;+ }+ break;++ case X_DQUOTE:+ if (is_line_terminator(c) || isspace(c) || isxdigit(c)) {+ lf::append_wchar(r, c);+ } else if (c == '"') {+ *r += '"';+ *r += HI_RESET;+ t_ = NORMAL;+ } else {+ *r += HI_RESET;+ *r += HI_WARNING;+ lf::append_wchar(r, c);+ *r += HI_RESET;+ *r += HI_STRING;+ }+ break;++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightD::flush(std::string *r) {+ switch (t_) {+ case WORD:+ if (is_keyword_d(word_.data(), word_.size())) {+ *r += HI_KEYWORD;+ *r += word_;+ *r += HI_RESET;+ } else if (is_keyword_d_constant(word_.data(), word_.size())) {+ *r += HI_CONSTANT;+ *r += word_;+ *r += HI_RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ break;+ case SLASH:+ *r += '/';+ break;+ case R:+ *r += 'r';+ break;+ case Q:+ *r += 'q';+ break;+ case X:+ *r += 'x';+ break;+ case QUOTE:+ case QUOTE_BACKSLASH:+ case DQUOTE:+ case DQUOTE_BACKSLASH:+ case SLASH_SLASH:+ case SLASH_STAR:+ case SLASH_STAR_STAR:+ case SLASH_PLUS:+ case SLASH_PLUS_PLUS:+ case SLASH_PLUS_SLASH:+ case BACKTICK:+ case R_DQUOTE:+ case Q_DQUOTE:+ case Q_DQUOTE_STRING:+ case Q_DQUOTE_STRING_END:+ case Q_DQUOTE_IDENT:+ case Q_DQUOTE_HEREDOC:+ case Q_DQUOTE_HEREDOC_BOL:+ case Q_DQUOTE_HEREDOC_END:+ *r += HI_RESET;+ break;+ default:+ break;+ }+ c_ = 0;+ u_ = 0;+ t_ = NORMAL;+}diff --git a/llamafile/highlight_test.cpp b/llamafile/highlight_test.cppindex 4e36671c..7100585b 100644--- a/llamafile/highlight_test.cpp+++ b/llamafile/highlight_test.cpp@@ -25,7 +25,7 @@#define LENGTH 10#define ITERATIONS 200000-#define CHARSET " aQq123{}[]!@#$%^*().\"'`\\/\n-_=&;:<>,"+#define CHARSET " raQq123{}[]!@#$%^*().\"'`\\/\n-_=&;:<>,"const char *const kLanguages[] = {"ada", //@@ -144,6 +144,10 @@ bool is_color_reset(const std::string &input) {int main(int argc, char *argv[]) {for (int l = 0; l < ARRAYLEN(kLanguages); ++l) {Highlight *h = Highlight::create(kLanguages[l]);+ if (!h) {+ fprintf(stderr, "Highlight::create(%`'s) failed\n", kLanguages[l]);+ exit(1);+ }for (int i = 0; i < ITERATIONS; ++i) {std::string sauce = generate_random_string(LENGTH);std::string colorized;diff --git a/llamafile/server/www/ctype.js b/llamafile/server/www/ctype.jsindex 5856956f..e6f7beac 100644--- a/llamafile/server/www/ctype.js+++ b/llamafile/server/www/ctype.js@@ -102,3 +102,27 @@ function ispunct(c) {return false;}}++function isxdigit(c) {+ switch (c.toLowerCase()) {+ case '0':+ case '1':+ case '2':+ case '3':+ case '4':+ case '5':+ case '6':+ case '7':+ case '8':+ case '9':+ case 'a':+ case 'b':+ case 'c':+ case 'd':+ case 'e':+ case 'f':+ return true;+ default:+ return false;+ }+}diff --git a/llamafile/server/www/highlight_d.js b/llamafile/server/www/highlight_d.jsindex 4793338c..f25201b4 100644--- a/llamafile/server/www/highlight_d.js+++ b/llamafile/server/www/highlight_d.js@@ -128,13 +128,466 @@ const D_CONSTANTS = new Set(['true',]);-class HighlightD extends HighlightC {+class HighlightD extends Highlighter {++ static NORMAL = 0;+ static WORD = 1;+ static QUOTE = 2;+ static QUOTE_BACKSLASH = 3;+ static DQUOTE = 4;+ static DQUOTE_BACKSLASH = 5;+ static SLASH = 6;+ static SLASH_SLASH = 7;+ static SLASH_STAR = 8;+ static SLASH_STAR_STAR = 9;+ static SLASH_PLUS = 10;+ static SLASH_PLUS_PLUS = 11;+ static SLASH_PLUS_SLASH = 12;+ static BACKTICK = 13;+ static R = 14;+ static R_DQUOTE = 15;+ static Q = 16;+ static Q_DQUOTE = 17;+ static Q_DQUOTE_STRING = 18;+ static Q_DQUOTE_STRING_END = 19;+ static Q_DQUOTE_IDENT = 20;+ static Q_DQUOTE_HEREDOC = 21;+ static Q_DQUOTE_HEREDOC_BOL = 22;+ static Q_DQUOTE_HEREDOC_END = 23;+ static X = 24;+ static X_DQUOTE = 25;++ static is_line_terminator(c) {+ switch (c) {+ case '\r':+ case '\n':+ case '\u2028': // LINE SEPARATOR+ case '\u2029': // PARAGRAPH SEPARATOR+ return true;+ default:+ return false;+ }+ }++ static mirror(c) {+ switch (c) {+ case '(':+ return ')';+ case '{':+ return '}';+ case '[':+ return ']';+ case '<':+ return '>';+ default:+ return c;+ }+ }+constructor(delegate) {super(delegate);- this.keywords = D_KEYWORDS;- this.builtins = null;- this.constants = D_CONSTANTS;- this.types = null;+ this.depth = 0;+ this.opener = '';+ this.closer = '';+ this.word = '';+ this.heredoc = '';+ this.heredoc2 = '';+ }++ feed(input) {+ for (let i = 0; i < input.length; i += this.delta) {+ this.delta = 1;+ let c = input[i];+ if (c == '\r')+ continue;+ if (c == 0xFEFF)+ continue; // utf-8 bom+ switch (this.state) {++ case HighlightD.NORMAL:+ if (c == 'r') {+ this.state = HighlightD.R;+ } else if (c == 'q') {+ this.state = HighlightD.Q;+ } else if (c == 'x') {+ this.state = HighlightD.X;+ } else if (!isascii(c) || isalpha(c) || c == '_') {+ this.state = HighlightD.WORD;+ this.word += c;+ } else if (c == '`') {+ this.state = HighlightD.BACKTICK;+ this.push("span", "string");+ this.append('`');+ } else if (c == '/') {+ this.state = HighlightD.SLASH;+ } else if (c == '\'') {+ this.state = HighlightD.QUOTE;+ this.push("span", "string");+ this.append('\'');+ } else if (c == '"') {+ this.state = HighlightD.DQUOTE;+ this.push("span", "string");+ this.append('"');+ } else {+ this.append(c);+ }+ break;++ case HighlightD.WORD:+ if (!isascii(c) || isalnum(c) || c == '_') {+ this.word += c;+ } else {+ if (D_KEYWORDS.has(this.word)) {+ this.push("span", "keyword");+ this.append(this.word);+ this.pop();+ } else if (D_CONSTANTS.has(this.word)) {+ this.push("span", "constant");+ this.append(this.word);+ this.pop();+ } else {+ this.append(this.word);+ }+ this.word = '';+ this.epsilon(HighlightD.NORMAL);+ }+ break;++ case HighlightD.SLASH:+ if (c == '/') {+ this.push("span", "comment");+ this.append("//");+ this.state = HighlightD.SLASH_SLASH;+ } else if (c == '*') {+ this.push("span", "comment");+ this.append("/*");+ this.state = HighlightD.SLASH_STAR;+ } else if (c == '+') {+ this.push("span", "comment");+ this.append("/+");+ this.state = HighlightD.SLASH_PLUS;+ this.depth = 1;+ } else {+ this.append('/');+ this.epsilon(HighlightD.NORMAL);+ }+ break;++ case HighlightD.SLASH_SLASH:+ this.append(c);+ if (HighlightD.is_line_terminator(c)) {+ this.pop();+ this.state = HighlightD.NORMAL;+ }+ break;++ case HighlightD.SLASH_STAR:+ this.append(c);+ if (c == '*')+ this.state = HighlightD.SLASH_STAR_STAR;+ break;++ case HighlightD.SLASH_STAR_STAR:+ this.append(c);+ if (c == '/') {+ this.pop();+ this.state = HighlightD.NORMAL;+ } else if (c != '*') {+ this.state = HighlightD.SLASH_STAR;+ }+ break;++ case HighlightD.SLASH_PLUS:+ this.append(c);+ if (c == '+') {+ this.state = HighlightD.SLASH_PLUS_PLUS;+ } else if (c == '/') {+ this.state = HighlightD.SLASH_PLUS_SLASH;+ }+ break;++ case HighlightD.SLASH_PLUS_PLUS:+ this.append(c);+ if (c == '/') {+ if (!--this.depth) {+ this.pop();+ this.state = HighlightD.NORMAL;+ } else {+ this.state = HighlightD.SLASH_PLUS;+ }+ } else if (c != '+') {+ this.state = HighlightD.SLASH_PLUS;+ }+ break;++ case HighlightD.SLASH_PLUS_SLASH:+ this.append(c);+ if (c == '+') {+ ++this.depth;+ this.state = HighlightD.SLASH_PLUS;+ } else if (c != '/') {+ this.state = HighlightD.SLASH_PLUS;+ }+ break;++ case HighlightD.QUOTE:+ this.append(c);+ if (c == '\'') {+ this.pop();+ this.state = HighlightD.NORMAL;+ } else if (c == '\\') {+ this.state = HighlightD.QUOTE_BACKSLASH;+ }+ break;++ case HighlightD.QUOTE_BACKSLASH:+ this.append(c);+ this.state = HighlightD.QUOTE;+ break;++ case HighlightD.DQUOTE:+ this.append(c);+ if (c == '"') {+ this.pop();+ this.state = HighlightD.NORMAL;+ } else if (c == '\\') {+ this.state = HighlightD.DQUOTE_BACKSLASH;+ }+ break;++ case HighlightD.DQUOTE_BACKSLASH:+ this.append(c);+ this.state = HighlightD.DQUOTE;+ break;++ case HighlightD.R:+ if (c == '"') {+ this.state = HighlightD.R_DQUOTE;+ this.append('r');+ this.push("span", "string");+ this.append('"');+ } else {+ this.word += 'r';+ this.epsilon(HighlightD.WORD);+ }+ break;++ case HighlightD.R_DQUOTE:+ this.append(c);+ if (c == '"') {+ this.pop();+ this.state = HighlightD.NORMAL;+ }+ break;++ case HighlightD.BACKTICK:+ this.append(c);+ if (c == '`') {+ this.pop();+ this.state = HighlightD.NORMAL;+ }+ break;++ case HighlightD.Q:+ if (c == '"') {+ this.state = HighlightD.Q_DQUOTE;+ this.append('q');+ this.push("span", "string");+ this.append('"');+ } else {+ this.word += 'q';+ this.epsilon(HighlightD.WORD);+ }+ break;++ case HighlightD.Q_DQUOTE:+ this.append(c);+ if (!isascii(c) || isalpha(c) || c == '_') {+ this.heredoc = c;+ this.state = HighlightD.Q_DQUOTE_IDENT;+ } else {+ this.opener = c;+ this.closer = HighlightD.mirror(c);+ this.depth = 1;+ this.state = HighlightD.Q_DQUOTE_STRING;+ }+ break;++ case HighlightD.Q_DQUOTE_STRING:+ if (c == this.closer) {+ if (this.closer != this.opener) {+ if (this.depth) {+ --this.depth;+ } else {+ this.pop();+ this.push("span", "warning");+ }+ if (!this.depth) {+ this.state = HighlightD.Q_DQUOTE_STRING_END;+ }+ } else {+ this.state = HighlightD.Q_DQUOTE_STRING_END;+ }+ } else if (c == this.opener && this.closer != this.opener) {+ ++this.depth;+ }+ this.append(c);+ break;++ case HighlightD.Q_DQUOTE_STRING_END:+ if (c == '"') {+ this.append(c);+ this.pop();+ this.state = HighlightD.NORMAL;+ } else {+ this.pop();+ this.push("span", "warning");+ this.epsilon(HighlightD.Q_DQUOTE_STRING);+ }+ break;++ case HighlightD.Q_DQUOTE_IDENT:+ if (HighlightD.is_line_terminator(c)) {+ this.state = HighlightD.Q_DQUOTE_HEREDOC_BOL;+ this.heredoc2 = '';+ } else if (!isascii(c) || isalpha(c) || c == '_') {+ this.heredoc += c;+ } else {+ this.pop();+ this.push("span", "warning");+ this.state = HighlightD.Q_DQUOTE_HEREDOC_BOL;+ this.heredoc2 = '';+ }+ this.append(c);+ break;++ case HighlightD.Q_DQUOTE_HEREDOC:+ this.append(c);+ if (HighlightD.is_line_terminator(c)) {+ this.state = HighlightD.Q_DQUOTE_HEREDOC_BOL;+ this.heredoc2 = '';+ }+ break;++ case HighlightD.Q_DQUOTE_HEREDOC_BOL:+ this.append(c);+ if (HighlightD.is_line_terminator(c)) {+ this.state = HighlightD.Q_DQUOTE_HEREDOC_BOL;+ this.heredoc2 = '';+ } else {+ this.heredoc2 += c;+ if (this.heredoc.startsWith(this.heredoc2)) {+ if (this.heredoc == this.heredoc2) {+ this.state = HighlightD.Q_DQUOTE_HEREDOC_END;+ }+ } else {+ this.state = HighlightD.Q_DQUOTE_HEREDOC;+ }+ }+ break;++ case HighlightD.Q_DQUOTE_HEREDOC_END:+ if (c == '"') {+ this.append(c);+ this.pop();+ this.state = HighlightD.NORMAL;+ } else {+ this.pop();+ this.push("span", "warning");+ this.epsilon(HighlightD.Q_DQUOTE_HEREDOC);+ }+ break;++ case HighlightD.X:+ if (c == '"') {+ this.append('x');+ this.push("span", "string");+ this.append('"');+ this.state = HighlightD.X_DQUOTE;+ } else {+ this.word += 'x';+ this.epsilon(HighlightD.WORD);+ }+ break;++ case HighlightD.X_DQUOTE:+ if (HighlightD.is_line_terminator(c) || isspace(c) || isxdigit(c)) {+ this.append(c);+ } else if (c == '"') {+ this.append('"');+ this.pop();+ this.state = HighlightD.NORMAL;+ } else {+ this.pop();+ this.push("span", "warning");+ this.append(c);+ this.pop();+ this.push("span", "string");+ }+ break;++ default:+ throw new Error('Invalid state');+ }+ }+ }++ flush() {+ switch (this.state) {+ case HighlightD.WORD:+ if (D_KEYWORDS.has(this.word)) {+ this.push("span", "keyword");+ this.append(this.word);+ this.pop();+ } else if (D_CONSTANTS.has(this.word)) {+ this.push("span", "constant");+ this.append(this.word);+ this.pop();+ } else {+ this.append(this.word);+ }+ this.word = '';+ break;+ case HighlightD.SLASH:+ this.append('/');+ break;+ case HighlightD.R:+ this.append('r');+ break;+ case HighlightD.Q:+ this.append('q');+ break;+ case HighlightD.X:+ this.append('x');+ break;+ case HighlightD.QUOTE:+ case HighlightD.QUOTE_BACKSLASH:+ case HighlightD.DQUOTE:+ case HighlightD.DQUOTE_BACKSLASH:+ case HighlightD.SLASH_SLASH:+ case HighlightD.SLASH_STAR:+ case HighlightD.SLASH_STAR_STAR:+ case HighlightD.SLASH_PLUS:+ case HighlightD.SLASH_PLUS_PLUS:+ case HighlightD.SLASH_PLUS_SLASH:+ case HighlightD.BACKTICK:+ case HighlightD.R_DQUOTE:+ case HighlightD.Q_DQUOTE:+ case HighlightD.Q_DQUOTE_STRING:+ case HighlightD.Q_DQUOTE_STRING_END:+ case HighlightD.Q_DQUOTE_IDENT:+ case HighlightD.Q_DQUOTE_HEREDOC:+ case HighlightD.Q_DQUOTE_HEREDOC_BOL:+ case HighlightD.Q_DQUOTE_HEREDOC_END:+ this.pop();+ break;+ default:+ break;+ }+ this.c = 0;+ this.u = 0;+ this.state = HighlightD.NORMAL;+ this.delegate.flush();}}diff --git a/llamafile/server/www/test/test.md b/llamafile/server/www/test/test.mdindex 37823964..662a44e1 100644--- a/llamafile/server/www/test/test.md+++ b/llamafile/server/www/test/test.md@@ -382,3 +382,37 @@ endendend```++# D++```d+// d stands for dead, for+// what is dead may never die+// but rises again, harder and stronger+/* com /* com */ plain+/+ com /+ com +/ com +/ plain+/+/++/+/ plain+/+/++/++/ plain+/+//++/++/ plain+/+//++/+++/ plain+"str\"str" plain+`str"\str` plain+r"str\str" plain+q"/str/warn/" plain+q"<str>warn>" plain+q"(str"(str)"str)" plain+q"[str"[str]"str]" plain+q"eos+hello+eos" plain+q"eos!warn+warn+eos" plain+q"eos+warn+eos+warn+eos" plain+x"dead beef" plain+x"dead warn" plain+```