Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Add Perl syntax highlighting
Justine Tunney9c4069310345
diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cppindex 8df5d60e..babfe378 100644--- a/llamafile/highlight.cpp+++ b/llamafile/highlight.cpp@@ -48,6 +48,8 @@ Highlight *Highlight::create(const std::string_view &lang) {return new HighlightHtml;} else if (lang == "php") {return new HighlightHtml; // sic+ } else if (lang == "php!") {+ return new HighlightPhp;} else if (lang == "csharp" || lang == "cs" || lang == "c#") {return new HighlightC(is_keyword_csharp);} else if (lang == "kotlin") {@@ -64,6 +66,8 @@ Highlight *Highlight::create(const std::string_view &lang) {return new HighlightAda;} else if (lang == "haskell" || lang == "hs") {return new HighlightHaskell;+ } else if (lang == "perl" || lang == "pl") {+ return new HighlightPerl;} else {return new HighlightPlain;}diff --git a/llamafile/highlight.h b/llamafile/highlight.hindex a57f2a1c..7bc3fc7a 100644--- a/llamafile/highlight.h+++ b/llamafile/highlight.h@@ -66,6 +66,7 @@ is_keyword_f is_keyword_lua_builtin;is_keyword_f is_keyword_lisp;is_keyword_f is_keyword_ada;is_keyword_f is_keyword_haskell;+is_keyword_f is_keyword_perl;}class Highlight {@@ -275,3 +276,15 @@ class HighlightHaskell : public Highlight {int t_ = 0;std::string symbol_;};++class HighlightPerl : public Highlight {+ public:+ HighlightPerl();+ ~HighlightPerl() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;++ private:+ int t_ = 0;+ std::string word_;+};diff --git a/llamafile/highlight_perl.cpp b/llamafile/highlight_perl.cppnew file mode 100644index 00000000..1a382328--- /dev/null+++ b/llamafile/highlight_perl.cpp@@ -0,0 +1,205 @@+// -*- 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 QUOTE_BACKSLASH 3+#define DQUOTE 4+#define DQUOTE_BACKSLASH 5+#define TICK 6+#define TICK_BACKSLASH 7+#define VAR 8+#define VAR2 9+#define COMMENT 10++HighlightPerl::HighlightPerl() {+}++HighlightPerl::~HighlightPerl() {+}++void HighlightPerl::feed(std::string *r, std::string_view input) {+ int c;+ for (size_t i = 0; i < input.size(); ++i) {+ c = input[i] & 255;+ switch (t_) {++ Normal:+ case NORMAL:+ if (!isascii(c) || isalpha(c) || c == '_') {+ t_ = WORD;+ word_ += c;+ } else if (c == '\'') {+ t_ = QUOTE;+ *r += HI_STRING;+ *r += c;+ } else if (c == '"') {+ t_ = DQUOTE;+ *r += HI_STRING;+ *r += c;+ } else if (c == '`') {+ t_ = TICK;+ *r += HI_STRING;+ *r += c;+ } else if (c == '$') {+ *r += c;+ t_ = VAR;+ } else if (c == '@' || c == '%') {+ *r += c;+ *r += HI_VAR;+ t_ = VAR2;+ } else if (c == '#') {+ *r += HI_COMMENT;+ *r += c;+ t_ = COMMENT;+ } else {+ *r += c;+ }+ break;++ Word:+ case WORD:+ if (!isascii(c) || isalnum(c) || c == '_') {+ word_ += c;+ } else {+ if (is_keyword_perl(word_.data(), word_.size())) {+ *r += HI_KEYWORD;+ *r += word_;+ *r += HI_RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case VAR:+ t_ = VAR2;+ if (c == '{') {+ *r += c;+ *r += HI_VAR;+ break;+ } else {+ *r += HI_VAR;+ }+ // fallthrough++ case VAR2:+ if (!isascii(c) || isalpha(c) || isdigit(c) || c == '_') {+ *r += c;+ } else {+ *r += HI_RESET;+ t_ = NORMAL;+ goto Normal;+ }+ break;++ case COMMENT:+ if (c == '\n') {+ *r += HI_RESET;+ *r += c;+ t_ = NORMAL;+ } else {+ *r += c;+ }+ break;++ case QUOTE:+ *r += c;+ if (c == '\'') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c == '\\') {+ t_ = QUOTE_BACKSLASH;+ }+ break;++ case QUOTE_BACKSLASH:+ *r += c;+ t_ = QUOTE;+ break;++ case DQUOTE:+ *r += c;+ if (c == '"') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c == '\\') {+ t_ = DQUOTE_BACKSLASH;+ }+ break;++ case DQUOTE_BACKSLASH:+ *r += c;+ t_ = DQUOTE;+ break;++ case TICK:+ *r += c;+ if (c == '`') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else if (c == '\\') {+ t_ = TICK_BACKSLASH;+ }+ break;++ case TICK_BACKSLASH:+ *r += c;+ t_ = TICK;+ break;++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightPerl::flush(std::string *r) {+ switch (t_) {+ case WORD:+ if (is_keyword_perl(word_.data(), word_.size())) {+ *r += HI_KEYWORD;+ *r += word_;+ *r += HI_RESET;+ } else {+ *r += word_;+ }+ word_.clear();+ break;+ case VAR2:+ case TICK:+ case TICK_BACKSLASH:+ case QUOTE:+ case QUOTE_BACKSLASH:+ case DQUOTE:+ case DQUOTE_BACKSLASH:+ case COMMENT:+ *r += HI_RESET;+ break;+ default:+ break;+ }+ t_ = NORMAL;+}diff --git a/llamafile/highlight_php.cpp b/llamafile/highlight_php.cppindex 507dea38..9e0bc221 100644--- a/llamafile/highlight_php.cpp+++ b/llamafile/highlight_php.cpp@@ -47,9 +47,9 @@ void HighlightPhp::feed(std::string *r, std::string_view input) {Normal:case NORMAL:- if (!isascii(c) || isalpha(c) || c == '_' || c == '#') {+ if (!isascii(c) || isalpha(c) || c == '_') {t_ = WORD;- goto Word;+ word_ += c;} else if (c == '/') {t_ = SLASH;} else if (c == '\'') {@@ -75,7 +75,7 @@ void HighlightPhp::feed(std::string *r, std::string_view input) {Word:case WORD:- if (!isascii(c) || isalpha(c) || isdigit(c) || c == '_') {+ if (!isascii(c) || isalnum(c) || c == '_') {word_ += c;} else {if (is_keyword_php(word_.data(), word_.size())) {@@ -211,8 +211,11 @@ void HighlightPhp::flush(std::string *r) {break;case VAR:case TICK:+ case TICK_BACKSLASH:case QUOTE:+ case QUOTE_BACKSLASH:case DQUOTE:+ case DQUOTE_BACKSLASH:case SLASH_SLASH:case SLASH_STAR:case SLASH_STAR_STAR:diff --git a/llamafile/highlight_test.cpp b/llamafile/highlight_test.cppindex f925b373..4781e7de 100644--- a/llamafile/highlight_test.cpp+++ b/llamafile/highlight_test.cpp@@ -47,7 +47,8 @@ const char *const kLanguages[] = {"lua", //"markdown", //"pascal", //- "php", //+ "perl", //+ "php!", //"plain", //"python", //"rust", //diff --git a/llamafile/is_keyword_perl.c b/llamafile/is_keyword_perl.cnew file mode 100644index 00000000..0a02077c--- /dev/null+++ b/llamafile/is_keyword_perl.c@@ -0,0 +1,163 @@+/* ANSI-C code produced by gperf version 3.1 */+/* Command-line: gperf is_keyword_perl.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_perl.gperf"++#include <string.h>++#define TOTAL_KEYWORDS 36+#define MIN_WORD_LENGTH 2+#define MAX_WORD_LENGTH 8+#define MIN_HASH_VALUE 3+#define MAX_HASH_VALUE 49+/* maximum key range = 47, 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[] =+ {+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 5, 50, 15, 15,+ 50, 0, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 30, 15, 0,+ 5, 5, 5, 0, 10, 20, 50, 50, 20, 35,+ 30, 0, 20, 35, 0, 10, 0, 10, 0, 15,+ 15, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,+ 50, 50, 50, 50, 50, 50+ };+ register unsigned int hval = len;++ switch (hval)+ {+ default:+ hval += asso_values[(unsigned char)str[2]];+ /*FALLTHROUGH*/+ case 2:+ case 1:+ hval += asso_values[(unsigned char)str[0]];+ break;+ }+ return hval;+}++const char *+is_keyword_perl (register const char *str, register size_t len)+{+ static const char * const wordlist[] =+ {+ "", "", "",+ "our",+ "goto",+ "given",+ "return",+ "do",+ "for",+ "redo",+ "BEGIN",+ "",+ "foreach",+ "die",+ "exec",+ "until",+ "",+ "default",+ "use",+ "else",+ "elsif",+ "",+ "if",+ "has",+ "when",+ "local",+ "",+ "package",+ "sub",+ "exit",+ "", "",+ "no",+ "END",+ "last",+ "",+ "unless",+ "my",+ "continue",+ "eval",+ "while",+ "",+ "require",+ "",+ "dump",+ "state",+ "import",+ "", "",+ "next"+ };++ 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_perl.gperf b/llamafile/is_keyword_perl.gperfnew file mode 100644index 00000000..22515a00--- /dev/null+++ b/llamafile/is_keyword_perl.gperf@@ -0,0 +1,44 @@+%{+#include <string.h>+%}+%compare-strncmp+%language=ANSI-C+%readonly-tables+%define lookup-function-name is_keyword_perl+%%+BEGIN+END+continue+default+die+do+dump+else+elsif+eval+exec+exit+for+foreach+given+goto+has+if+import+last+local+my+next+no+our+package+redo+require+return+state+sub+unless+until+use+when+while