Skip to content

Distribute and run LLMs with a single file.

Add Haskell syntax highlighting

Justine Tunney232a9f6b38be
diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cpp
index fd8b6405..8df5d60e 100644
--- a/llamafile/highlight.cpp
+++ b/llamafile/highlight.cpp
@@ -62,6 +62,8 @@ Highlight *Highlight::create(const std::string_view &lang) {
return new HighlightLisp;
} else if (lang == "ada") {
return new HighlightAda;
+ } else if (lang == "haskell" || lang == "hs") {
+ return new HighlightHaskell;
} else {
return new HighlightPlain;
}
diff --git a/llamafile/highlight.h b/llamafile/highlight.h
index 433f616c..a57f2a1c 100644
--- a/llamafile/highlight.h
+++ b/llamafile/highlight.h
@@ -38,6 +38,7 @@
#define HI_BUILTIN "\033[35m" // magenta
#define HI_LISPKW "\033[35m" // magenta
#define HI_ENTITY "\033[36m" // cyan
+#define HI_OPERATOR "\033[36m" // cyan
typedef const char *is_keyword_f(const char *, size_t);
@@ -64,6 +65,7 @@ is_keyword_f is_keyword_lua;
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;
}
class Highlight {
@@ -261,3 +263,15 @@ class HighlightAda : public Highlight {
int t_ = 0;
std::string symbol_;
};
+
+class HighlightHaskell : public Highlight {
+ public:
+ HighlightHaskell();
+ ~HighlightHaskell() override;
+ void feed(std::string *result, std::string_view input) override;
+ void flush(std::string *result) override;
+
+ private:
+ int t_ = 0;
+ std::string symbol_;
+};
diff --git a/llamafile/highlight_c.cpp b/llamafile/highlight_c.cpp
index 34b5ab1a..862531e4 100644
--- a/llamafile/highlight_c.cpp
+++ b/llamafile/highlight_c.cpp
@@ -137,7 +137,7 @@ void HighlightC::feed(std::string *r, std::string_view input) {
if (c == '/') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '*') {
t_ = SLASH_STAR;
}
break;
@@ -192,6 +192,7 @@ void HighlightC::flush(std::string *r) {
case SLASH:
*r += '/';
break;
+ case TICK:
case QUOTE:
case DQUOTE:
case SLASH_SLASH:
diff --git a/llamafile/highlight_css.cpp b/llamafile/highlight_css.cpp
index 077fadd0..6bd2e72e 100644
--- a/llamafile/highlight_css.cpp
+++ b/llamafile/highlight_css.cpp
@@ -159,7 +159,7 @@ void HighlightCss::feed(std::string *r, std::string_view input) {
if (c == '/') {
*r += HI_RESET;
goto Pop;
- } else {
+ } else if (c != '*') {
t_ &= -256;
t_ |= SLASH_STAR;
}
diff --git a/llamafile/highlight_haskell.cpp b/llamafile/highlight_haskell.cpp
new file mode 100644
index 00000000..bdbfb5a1
--- /dev/null
+++ b/llamafile/highlight_haskell.cpp
@@ -0,0 +1,312 @@
+// -*- 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 TICK 4
+#define CURL 5
+#define CURL_HYPHEN 6
+#define CURL_HYPHEN_HYPHEN 7
+#define HYPHEN 8
+#define HYPHEN_HYPHEN 9
+#define HYPHEN_LT 10
+#define EQUAL 11
+#define COLON 12
+#define LT 13
+#define BACKSLASH 64
+
+HighlightHaskell::HighlightHaskell() {
+}
+
+HighlightHaskell::~HighlightHaskell() {
+}
+
+void HighlightHaskell::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_ = HYPHEN;
+ } else if (c == '{') {
+ t_ = CURL;
+ } 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_OPERATOR;
+ *r += c;
+ } else if (c == '!' || //
+ c == '#' || //
+ c == '$' || //
+ c == '*' || //
+ c == ',' || //
+ c == '>' || //
+ c == '?' || //
+ c == '@' || //
+ c == '|' || //
+ c == '~') {
+ *r += HI_OPERATOR;
+ *r += c;
+ *r += HI_RESET;
+ } else if (c == '=') {
+ t_ = EQUAL;
+ } else if (c == ':') {
+ t_ = COLON;
+ } else if (c == '<') {
+ t_ = LT;
+ } else {
+ *r += c;
+ }
+ break;
+
+ Word:
+ case WORD:
+ if (!isascii(c) || isalpha(c) || isdigit(c) || c == '_') {
+ symbol_ += c;
+ } else {
+ if (is_keyword_haskell(symbol_.data(), symbol_.size())) {
+ *r += HI_KEYWORD;
+ *r += symbol_;
+ *r += HI_RESET;
+ } else {
+ *r += symbol_;
+ }
+ symbol_.clear();
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case LT:
+ if (c == '-') {
+ *r += HI_OPERATOR;
+ *r += "<-";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else {
+ *r += '<';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case COLON:
+ if (c == ':') {
+ *r += HI_OPERATOR;
+ *r += "::";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else {
+ *r += ':';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case EQUAL:
+ if (c == '>') {
+ *r += HI_OPERATOR;
+ *r += "=>";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else {
+ *r += HI_OPERATOR;
+ *r += '=';
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case HYPHEN:
+ if (c == '-') {
+ *r += HI_COMMENT;
+ *r += "--";
+ t_ = HYPHEN_HYPHEN;
+ } else if (c == '<') {
+ t_ = HYPHEN_LT;
+ } else if (c == '>') {
+ *r += HI_OPERATOR;
+ *r += "->";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else {
+ *r += '-';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case HYPHEN_LT:
+ if (c == '<') {
+ *r += HI_OPERATOR;
+ *r += "-<<";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else {
+ *r += HI_OPERATOR;
+ *r += "-<";
+ *r += HI_RESET;
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case HYPHEN_HYPHEN:
+ if (c == '\n') {
+ *r += HI_RESET;
+ *r += c;
+ t_ = NORMAL;
+ } else {
+ *r += c;
+ }
+ break;
+
+ case CURL:
+ if (c == '-') {
+ *r += HI_COMMENT;
+ *r += "{-";
+ t_ = CURL_HYPHEN;
+ } else {
+ *r += '{';
+ t_ = NORMAL;
+ goto Normal;
+ }
+ break;
+
+ case CURL_HYPHEN:
+ *r += c;
+ if (c == '-')
+ t_ = CURL_HYPHEN_HYPHEN;
+ break;
+
+ case CURL_HYPHEN_HYPHEN:
+ *r += c;
+ if (c == '}') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ } else if (c != '-') {
+ t_ = CURL_HYPHEN;
+ }
+ break;
+
+ case QUOTE:
+ *r += c;
+ if (c == '\'') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ }
+ break;
+
+ case DQUOTE:
+ *r += c;
+ if (c == '"') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ }
+ break;
+
+ case TICK:
+ *r += c;
+ if (c == '`') {
+ *r += HI_RESET;
+ t_ = NORMAL;
+ }
+ break;
+
+ default:
+ __builtin_unreachable();
+ }
+ }
+}
+
+void HighlightHaskell::flush(std::string *r) {
+ t_ &= ~BACKSLASH;
+ switch (t_) {
+ case WORD:
+ if (is_keyword_haskell(symbol_.data(), symbol_.size())) {
+ *r += HI_KEYWORD;
+ *r += symbol_;
+ *r += HI_RESET;
+ } else {
+ *r += symbol_;
+ }
+ symbol_.clear();
+ break;
+ case CURL:
+ *r += '{';
+ break;
+ case HYPHEN:
+ *r += '-';
+ break;
+ case EQUAL:
+ *r += '=';
+ break;
+ case COLON:
+ *r += ':';
+ break;
+ case LT:
+ *r += '<';
+ break;
+ case HYPHEN_LT:
+ *r += HI_OPERATOR;
+ *r += "-<";
+ *r += HI_RESET;
+ break;
+ case TICK:
+ case QUOTE:
+ case DQUOTE:
+ case HYPHEN_HYPHEN:
+ case CURL_HYPHEN:
+ case CURL_HYPHEN_HYPHEN:
+ *r += HI_RESET;
+ break;
+ default:
+ break;
+ }
+ t_ = NORMAL;
+}
diff --git a/llamafile/highlight_html.cpp b/llamafile/highlight_html.cpp
index f0bed187..ad931398 100644
--- a/llamafile/highlight_html.cpp
+++ b/llamafile/highlight_html.cpp
@@ -143,7 +143,7 @@ void HighlightHtml::feed(std::string *r, std::string_view input) {
if (c == '>') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '-') {
t_ = COMMENT;
}
break;
diff --git a/llamafile/highlight_pascal.cpp b/llamafile/highlight_pascal.cpp
index 866d013e..0b95f433 100644
--- a/llamafile/highlight_pascal.cpp
+++ b/llamafile/highlight_pascal.cpp
@@ -160,7 +160,7 @@ void HighlightPascal::feed(std::string *r, std::string_view input) {
if (c == ')') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '*') {
t_ = PAREN_STAR;
}
break;
diff --git a/llamafile/highlight_php.cpp b/llamafile/highlight_php.cpp
index 51b9d6be..5a13cd13 100644
--- a/llamafile/highlight_php.cpp
+++ b/llamafile/highlight_php.cpp
@@ -147,7 +147,7 @@ void HighlightPhp::feed(std::string *r, std::string_view input) {
if (c == '/') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '*') {
t_ = SLASH_STAR;
}
break;
@@ -199,6 +199,7 @@ void HighlightPhp::flush(std::string *r) {
*r += '/';
break;
case VAR:
+ case TICK:
case QUOTE:
case DQUOTE:
case SLASH_SLASH:
diff --git a/llamafile/highlight_rust.cpp b/llamafile/highlight_rust.cpp
index 990cd5f8..d0b1d1c0 100644
--- a/llamafile/highlight_rust.cpp
+++ b/llamafile/highlight_rust.cpp
@@ -139,7 +139,7 @@ void HighlightRust::feed(std::string *r, std::string_view input) {
if (c == '/') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '*') {
t_ = SLASH_STAR;
}
break;
diff --git a/llamafile/highlight_sql.cpp b/llamafile/highlight_sql.cpp
index c86845d8..80b246e3 100644
--- a/llamafile/highlight_sql.cpp
+++ b/llamafile/highlight_sql.cpp
@@ -116,7 +116,7 @@ void HighlightSql::feed(std::string *r, std::string_view input) {
if (c == '/') {
*r += HI_RESET;
t_ = NORMAL;
- } else {
+ } else if (c != '*') {
t_ = SLASH_STAR;
}
break;
diff --git a/llamafile/is_keyword_haskell.c b/llamafile/is_keyword_haskell.c
new file mode 100644
index 00000000..b764422f
--- /dev/null
+++ b/llamafile/is_keyword_haskell.c
@@ -0,0 +1,143 @@
+/* ANSI-C code produced by gperf version 3.1 */
+/* Command-line: gperf llamafile/is_keyword_haskell.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 "llamafile/is_keyword_haskell.gperf"
+
+#include <string.h>
+
+#define TOTAL_KEYWORDS 30
+#define MIN_WORD_LENGTH 2
+#define MAX_WORD_LENGTH 9
+#define MIN_HASH_VALUE 2
+#define MAX_HASH_VALUE 39
+/* maximum key range = 38, 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[] =
+ {
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 5, 40, 10,
+ 25, 5, 15, 0, 30, 0, 40, 40, 5, 5,
+ 0, 10, 10, 5, 15, 0, 0, 40, 40, 0,
+ 0, 10, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+ 40, 40, 40, 40, 40, 40
+ };
+ return len + asso_values[(unsigned char)str[len - 1]] + asso_values[(unsigned char)str[0]];
+}
+
+const char *
+is_keyword_haskell (register const char *str, register size_t len)
+{
+ static const char * const wordlist[] =
+ {
+ "", "",
+ "in",
+ "",
+ "then",
+ "infix",
+ "import",
+ "as",
+ "let",
+ "type",
+ "where",
+ "infixl",
+ "newtype",
+ "instance",
+ "else",
+ "class",
+ "module",
+ "if",
+ "mdo",
+ "case",
+ "",
+ "infixr",
+ "foreign",
+ "",
+ "proc",
+ "",
+ "forall",
+ "of",
+ "rec",
+ "", "",
+ "family",
+ "default",
+ "deriving",
+ "data",
+ "",
+ "hiding",
+ "do",
+ "",
+ "qualified"
+ };
+
+ 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_haskell.gperf b/llamafile/is_keyword_haskell.gperf
new file mode 100644
index 00000000..bb9b04e8
--- /dev/null
+++ b/llamafile/is_keyword_haskell.gperf
@@ -0,0 +1,38 @@
+%{
+#include <string.h>
+%}
+%compare-strncmp
+%language=ANSI-C
+%readonly-tables
+%define lookup-function-name is_keyword_haskell
+%%
+as
+case
+class
+data
+default
+deriving
+do
+else
+family
+forall
+foreign
+hiding
+if
+import
+in
+infix
+infixl
+infixr
+instance
+let
+mdo
+module
+newtype
+of
+proc
+qualified
+rec
+then
+type
+where