Skip to content

Commit

Permalink
Improve JS/HTML syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 4, 2024
1 parent 14713b5 commit 9b96502
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
2 changes: 2 additions & 0 deletions llamafile/highlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ class HighlightJs : public Highlight {
int u_ = 0;
int t_ = 0;
int expect_;
int nesti_ = 0;
std::string word_;
unsigned char nest_[16];
};

class HighlightTypescript : public Highlight {
Expand Down
2 changes: 1 addition & 1 deletion llamafile/highlight_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void HighlightHtml::feed(std::string *r, std::string_view input) {
if (name_ == "script") {
pending_.clear();
closer_ = "</script>";
highlighter_ = new HighlightC(is_keyword_js);
highlighter_ = new HighlightJs;
t_ = RELAY;
i_ = 0;
} else if (name_ == "style") {
Expand Down
49 changes: 45 additions & 4 deletions llamafile/highlight_js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum {
SLASH_STAR_STAR,
TICK,
TICK_BACKSLASH,
TICK_DOLLAR,
REGEX,
REGEX_BACKSLASH,
REGEX_SQUARE,
Expand Down Expand Up @@ -99,18 +100,26 @@ void HighlightJs::feed(std::string *r, std::string_view input) {
} else if (c == '\'') {
t_ = QUOTE;
*r += HI_STRING;
lf::append_wchar(r, c);
*r += '\'';
expect_ = EXPECT_OPERATOR;
} else if (c == '"') {
t_ = DQUOTE;
*r += HI_STRING;
lf::append_wchar(r, c);
*r += '"';
expect_ = EXPECT_OPERATOR;
} else if (c == '`') {
t_ = TICK;
*r += HI_STRING;
lf::append_wchar(r, c);
*r += '`';
expect_ = EXPECT_OPERATOR;
} else if (c == '{' && nesti_ && nesti_ < sizeof(nest_)) {
expect_ = EXPECT_VALUE;
*r += '{';
nest_[nesti_++] = NORMAL;
} else if (c == '}' && nesti_) {
if ((t_ = nest_[--nesti_]) != NORMAL)
*r += HI_STRING;
*r += '}';
} else if (c == ')' || c == '}' || c == ']') {
expect_ = EXPECT_OPERATOR;
lf::append_wchar(r, c);
Expand Down Expand Up @@ -240,13 +249,19 @@ void HighlightJs::feed(std::string *r, std::string_view input) {
t_ = DQUOTE;
break;

Tick:
case TICK:
lf::append_wchar(r, c);
if (c == '`') {
*r += '`';
*r += HI_RESET;
t_ = NORMAL;
} else if (c == '$') {
t_ = TICK_DOLLAR;
} else if (c == '\\') {
*r += '\\';
t_ = TICK_BACKSLASH;
} else {
lf::append_wchar(r, c);
}
break;

Expand All @@ -255,6 +270,27 @@ void HighlightJs::feed(std::string *r, std::string_view input) {
t_ = TICK;
break;

case TICK_DOLLAR:
if (c == '{' && nesti_ < sizeof(nest_)) {
*r += HI_BOLD;
*r += '$';
*r += HI_UNBOLD;
*r += HI_STRING;
*r += '{';
*r += HI_RESET;
expect_ = EXPECT_VALUE;
nest_[nesti_++] = TICK;
t_ = NORMAL;
} else {
*r += HI_WARNING;
*r += '$';
*r += HI_UNBOLD;
*r += HI_STRING;
t_ = TICK;
goto Tick;
}
break;

case REGEX:
lf::append_wchar(r, c);
if (c == '/') {
Expand Down Expand Up @@ -316,6 +352,10 @@ void HighlightJs::flush(std::string *r) {
case SLASH:
*r += '/';
break;
case TICK_DOLLAR:
*r += '$';
*r += HI_RESET;
break;
case TICK:
case TICK_BACKSLASH:
case QUOTE:
Expand All @@ -337,4 +377,5 @@ void HighlightJs::flush(std::string *r) {
c_ = 0;
u_ = 0;
t_ = NORMAL;
nesti_ = 0;
}
27 changes: 14 additions & 13 deletions llamafile/highlight_kotlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ enum {
SLASH_SLASH,
SLASH_STAR,
SLASH_STAR_STAR,
DQUOTE, // "
DQUOTE_DOLLAR, // "
DQUOTE_VAR, // "
DQUOTESTR, // "...
DQUOTESTR_BACKSLASH, // "...
DQUOTESTR_DOLLAR, // "...
DQUOTESTR_VAR, // "...
DQUOTE2, // ""
DQUOTE3, // """...
DQUOTE,
DQUOTE_DOLLAR,
DQUOTE_VAR,
DQUOTESTR,
DQUOTESTR_BACKSLASH,
DQUOTESTR_DOLLAR,
DQUOTESTR_VAR,
DQUOTE2,
DQUOTE3,
DQUOTE3_BACKSLASH,
DQUOTE3_DOLLAR, // """...
DQUOTE3_VAR, // """...
DQUOTE31, // """..."
DQUOTE32, // """...""
DQUOTE3_DOLLAR,
DQUOTE3_VAR,
DQUOTE31,
DQUOTE32,
};

HighlightKotlin::HighlightKotlin() {
Expand Down Expand Up @@ -384,4 +384,5 @@ void HighlightKotlin::flush(std::string *r) {
break;
}
t_ = NORMAL;
nesti_ = 0;
}
1 change: 1 addition & 0 deletions llamafile/highlight_scala.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,5 @@ void HighlightScala::flush(std::string *r) {
break;
}
t_ = NORMAL;
nesti_ = 0;
}
1 change: 1 addition & 0 deletions llamafile/highlight_swift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,5 @@ void HighlightSwift::flush(std::string *r) {
c_ = 0;
u_ = 0;
t_ = NORMAL;
nesti_ = 0;
}
1 change: 1 addition & 0 deletions llamafile/highlight_typescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,5 @@ void HighlightTypescript::flush(std::string *r) {
c_ = 0;
u_ = 0;
t_ = NORMAL;
nesti_ = 0;
}

0 comments on commit 9b96502

Please sign in to comment.