Repository navigation
Expand file tree
/
Copy pathSyntaxHighlighter.cs
More file actions
170 lines (144 loc) · 6.65 KB
/
Copy pathSyntaxHighlighter.cs
File metadata and controls
170 lines (144 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Spectre.Console;
using System.Text;
namespace GTerm
{
/// <summary>
/// Colours one line of GLua or of a console command for GTerm's own output, in Monokai.
///
/// Deliberately a lexer and not a parser: it runs on whatever an agent decided to send, it must
/// never throw on half-written or malformed code, and being wrong about an edge case costs nothing
/// worse than a mis-coloured token. It also works strictly line by line, because that is the unit
/// the console writes in.
/// </summary>
internal static class SyntaxHighlighter
{
private const string Fg = "#f8f8f2"; // plain text
private const string Comment = "#75715e"; // comments
private const string Pink = "#f92672"; // keywords, operators
private const string Purple = "#ae81ff"; // numbers, nil/true/false
private const string Yellow = "#e6db74"; // strings
private const string Cyan = "#66d9ef"; // known globals and libraries
private const string Green = "#a6e22e"; // anything being called
private static readonly HashSet<string> Keywords = new(StringComparer.Ordinal)
{
"and", "break", "continue", "do", "else", "elseif", "end", "for", "function", "goto",
"if", "in", "local", "not", "or", "repeat", "return", "then", "until", "while",
};
/// <summary>Monokai paints constants like numbers, not like keywords.</summary>
private static readonly HashSet<string> Constants = new(StringComparer.Ordinal) { "nil", "true", "false" };
private static readonly HashSet<string> Globals = new(StringComparer.Ordinal)
{
"hook", "timer", "net", "file", "player", "ents", "util", "string", "table", "math", "os",
"render", "surface", "draw", "cam", "vgui", "concommand", "cvars", "team", "umsg", "sound",
"self", "_G", "GAMEMODE", "GM", "LocalPlayer", "Entity", "Vector", "Angle", "Color", "Material",
};
/// <summary>
/// Colour for a realm badge, using the Garry's Mod wiki's own values taken from its stylesheet,
/// so they read the way anyone who has used the wiki already expects: client amber, server blue,
/// menu green. See https://wiki.facepunch.com/gmod/States
/// </summary>
internal static string RealmColour(string? realm) => realm?.Trim().ToLowerInvariant() switch
{
"client" => "#dea909",
"server" => "#03a9f4",
"menu" => "#4caf50",
_ => Fg,
};
internal static string Lua(string line)
{
StringBuilder sb = new(line.Length * 3);
int i = 0;
while (i < line.Length)
{
char c = line[i];
// Comment to end of line. Long-bracket comments still colour correctly per line.
if (c == '-' && i + 1 < line.Length && line[i + 1] == '-')
{
Paint(sb, Comment, line[i..]);
return sb.ToString();
}
if (c is '"' or '\'')
{
int start = i;
char quote = c;
i++;
while (i < line.Length)
{
if (line[i] == '\\') { i += 2; continue; }
if (line[i] == quote) { i++; break; }
i++;
}
if (i > line.Length) i = line.Length;
Paint(sb, Yellow, line[start..i]);
continue;
}
if (char.IsDigit(c))
{
int start = i;
while (i < line.Length && (char.IsLetterOrDigit(line[i]) || line[i] == '.')) i++;
Paint(sb, Purple, line[start..i]);
continue;
}
if (char.IsLetter(c) || c == '_')
{
int start = i;
while (i < line.Length && (char.IsLetterOrDigit(line[i]) || line[i] == '_')) i++;
string word = line[start..i];
if (Keywords.Contains(word)) Paint(sb, Pink, word);
else if (Constants.Contains(word)) Paint(sb, Purple, word);
else if (Globals.Contains(word)) Paint(sb, Cyan, word);
else if (IsCalled(line, i)) Paint(sb, Green, word);
else Paint(sb, Fg, word);
continue;
}
// Operators read as structure; brackets and separators stay quiet.
Paint(sb, "+-*/%^#=<>~.:".Contains(c) ? Pink : Fg, c.ToString());
i++;
}
return sb.ToString();
}
/// <summary>A console command: the command itself, then its arguments.</summary>
internal static string ConsoleCommand(string line)
{
StringBuilder sb = new(line.Length * 3);
int i = 0;
bool first = true;
while (i < line.Length)
{
if (char.IsWhiteSpace(line[i]))
{
int ws = i;
while (i < line.Length && char.IsWhiteSpace(line[i])) i++;
sb.Append(Markup.Escape(line[ws..i]));
continue;
}
if (line[i] == '"')
{
int start = i;
i++;
while (i < line.Length && line[i] != '"') i++;
if (i < line.Length) i++;
Paint(sb, Yellow, line[start..i]);
first = false;
continue;
}
int tokenStart = i;
while (i < line.Length && !char.IsWhiteSpace(line[i])) i++;
string token = line[tokenStart..i];
if (first) Paint(sb, Green, token);
else if (double.TryParse(token, out _)) Paint(sb, Purple, token);
else Paint(sb, Fg, token);
first = false;
}
return sb.ToString();
}
/// <summary>True when the next non-space character opens a call or a string/table argument.</summary>
private static bool IsCalled(string line, int after)
{
while (after < line.Length && line[after] == ' ') after++;
return after < line.Length && line[after] is '(' or '"' or '\'' or '{';
}
private static void Paint(StringBuilder sb, string colour, string text)
=> sb.Append('[').Append(colour).Append(']').Append(Markup.Escape(text)).Append("[/]");
}
}