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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! These highlighters will return the following tokens names:
//!
//! keyword - a keyword for that language
//! boolean - a boolean
//! comment - a comment (both multiline and single line)
//! string - a string data type
//! number - a number
//! function - a function identifier
//! macro - a macro identifier
//! struct - a class / struct / enum / trait name
//! operator - operators within that language e.g. == or != or >= or +
//! namespace - a namespace for modules
//! character - a character data type
//! attribute - an attribute within the language
//! reference - for references within the language e.g. &self or &mut
//! symbol - a symbol data type (mainly for the Ruby language)
//! global - for global variable identifiers
//! regex - for regex datatypes in languages
//! header - for headers (mainly for the C language)
//!
//! These syntax highlighters are quite advanced and tend to do a decent job of syntax highlighting
//! with detail of which wouldn't be out of place in a popular text editor.
//! there may be an edge case where something goes a bit wrong, in that case, please open an issue

use crate::Highlighter;

/// Obtain the rust syntax highlighter
#[allow(clippy::missing_panics_doc, clippy::too_many_lines)]
#[must_use]
pub fn rust() -> Highlighter {
    let mut h = Highlighter::new();
    let keywords: Vec<&str> = vec![
        r"\b(as)\b",
        r"\b(break)\b",
        r"\b(char)\b",
        r"\b(const)\b",
        r"\b(continue)\b",
        r"\b(crate)\b",
        r"\b(else)\b",
        r"\b(enum)\b",
        r"\b(extern)\b",
        r"\b(fn)\b",
        r"\b(for)\b",
        r"\b(if)\b",
        r"\b(impl)\b",
        r"\b(in)\b",
        r"\b(let)\b",
        r"\b(loop)\b",
        r"\b(match)\b",
        r"\b(mod)\b",
        r"\b(move)\b",
        r"\b(mut)\b",
        r"\b(pub)\b",
        r"\b(ref)\b",
        r"\b(return)\b",
        r"\b(self)\b",
        r"\b(static)\b",
        r"\b(struct)\b",
        r"\b(super)\b",
        r"\b(trait)\b",
        r"\b(type)\b",
        r"\b(unsafe)\b",
        r"\b(use)\b",
        r"\b(where)\b",
        r"\b(while)\b",
        r"\b(async)\b",
        r"\b(await)\b",
        r"\b(dyn)\b",
        r"\b(abstract)\b",
        r"\b(become)\b",
        r"\b(box)\b",
        r"\b(do)\b",
        r"\b(final)\b",
        r"\b(macro)\b",
        r"\b(override)\b",
        r"\b(priv)\b",
        r"\b(typeof)\b",
        r"\b(unsized)\b",
        r"\b(virtual)\b",
        r"\b(yield)\b",
        r"\b(try)\b",
        r"\b('static)\b",
        r"\b(u8)\b",
        r"\b(u16)\b",
        r"\b(u32)\b",
        r"\b(u64)\b",
        r"\b(u128)\b",
        r"\b(usize)\b",
        r"\b(i8)\b",
        r"\b(i16)\b",
        r"\b(i32)\b",
        r"\b(i64)\b",
        r"\b(i128)\b",
        r"\b(isize)\b",
        r"\b(f32)\b",
        r"\b(f64)\b",
        r"\b(String)\b",
        r"\b(Vec)\b",
        r"\b(str)\b",
        r"\b(Some)\b",
        r"\b(bool)\b",
        r"\b(None)\b",
        r"\b(Box)\b",
        r"\b(Result)\b",
        r"\b(Option)\b",
        r"\b(Ok)\b",
        r"\b(Err)\b",
        r"\b(Self)\b",
        r"\b(std)\b",
    ];
    // Keywords
    h.join(keywords.as_slice(), "keyword").unwrap();
    h.join(&[r"\b(true)\b", r"\b(false)\b"], "boolean").unwrap();
    // Add comment definitions
    h.add(r"(?m)(//.*)$", "comment").unwrap();
    h.add_bounded("/*", "*/", false, "comment");
    // Add numbers definition
    h.join(&[r"\b(\d+.\d+|\d+)", r"\b(\d+.\d+(?:f32|f64))"], "number")
        .unwrap();
    // Add string definition
    h.add_bounded("\"", "\"", true, "string");
    // Add identifier definition
    h.add(r"([a-z_][A-Za-z0-9_]*)\s*\(", "function").unwrap();
    // Add macro definition
    h.add(r"([a-z_][A-Za-z0-9_]*!)\s*", "macro").unwrap();
    // Structs
    h.join(
        &[
            "(?:trait|enum|struct|impl)\\s+([A-Z][A-Za-z0-9_]*)\\s*",
            "impl(?:<.*?>|)\\s+([A-Z][A-Za-z0-9_]*)",
            "([A-Z][A-Za-z0-9_]*)::",
            r"([A-Z][A-Za-z0-9_]*)\s*\(",
            "impl.*for\\s+([A-Z][A-Za-z0-9_]*)",
            r"::\s*([A-Z_][A-Za-z0-9_]*)\s*\(",
        ],
        "struct",
    )
    .unwrap();
    // Operators
    h.join(
        &[
            r"(=)",
            r"(\+)",
            r"(\-)",
            r"(\*)",
            r"[^/](/)[^/]",
            r"(\+=)",
            r"(\-=)",
            r"(\*=)",
            r"(\\=)",
            r"(\?)",
            r"(==)",
            r"(!=)",
            r"(>=)",
            r"(<=)",
            r"(<)",
            r"(>)",
        ],
        "operator",
    )
    .unwrap();
    // Namespaces
    h.add(r"([a-z_][A-Za-z0-9_]*)::", "namespace").unwrap();
    // Characters
    h.join(&["('.')", r"('\\.')"], "character").unwrap();
    // Attributes
    h.add("(?ms)^\\s*(#(?:!|)\\[.*?\\])", "attribute").unwrap();
    // References
    h.join(
        &[
            "(&)", "&str", "&mut", "&self", "&i8", "&i16", "&i32", "&i64", "&i128", "&isize",
            "&u8", "&u16", "&u32", "&u64", "&u128", "&usize", "&f32", "&f64",
        ],
        "reference",
    )
    .unwrap();
    h
}

/// Obtain the python syntax highlighter
#[allow(clippy::missing_panics_doc)]
#[must_use]
pub fn python() -> Highlighter {
    let mut h = Highlighter::new();
    let keywords: Vec<&str> = vec![
        r"\b(and)\b",
        r"\b(as)\b",
        r"\b(assert)\b",
        r"\b(break)\b",
        r"\b(class)\b",
        r"\b(continue)\b",
        r"\b(def)\b",
        r"\b(del)\b",
        r"\b(elif)\b",
        r"\b(else)\b",
        r"\b(except)\b",
        r"\b(exec)\b",
        r"\b(finally)\b",
        r"\b(for)\b",
        r"\b(from)\b",
        r"\b(global)\b",
        r"\b(if)\b",
        r"\b(import)\b",
        r"\b(in)\b",
        r"\b(is)\b",
        r"\b(lambda)\b",
        r"\b(not)\b",
        r"\b(or)\b",
        r"\b(pass)\b",
        r"\b(raise)\b",
        r"\b(return)\b",
        r"\b(try)\b",
        r"\b(while)\b",
        r"\b(with)\b",
        r"\b(yield)\b",
        r"\b(str)\b",
        r"\b(bool)\b",
        r"\b(int)\b",
        r"\b(tuple)\b",
        r"\b(list)\b",
        r"\b(dict)\b",
        r"\b(tuple)\b",
        r"\b(len)\b",
        r"\b(None)\b",
        r"\b(input)\b",
        r"\b(type)\b",
        r"\b(set)\b",
        r"\b(range)\b",
        r"\b(enumerate)\b",
        r"\b(open)\b",
        r"\b(iter)\b",
        r"\b(min)\b",
        r"\b(max)\b",
        r"\b(dir)\b",
        r"\b(self)\b",
        r"\b(isinstance)\b",
        r"\b(help)\b",
        r"\b(next)\b",
        r"\b(super)\b",
    ];
    // Keywords
    h.join(keywords.as_slice(), "keyword").unwrap();
    h.join(&[r"\b(True)\b", r"\b(False)\b"], "boolean").unwrap();
    // Add comment definitions
    h.add(r"(?m)(#.*)$", "comment").unwrap();
    // Add numbers definition
    h.add(r"\b(\d+.\d+|\d+)", "number").unwrap();
    // Add string definition
    h.add_bounded("\"\"\"", "\"\"\"", false, "string");
    h.add_bounded("\"", "\"", true, "string");
    // Add identifier definition
    h.add(r"([a-z_][A-Za-z0-9_]*)\s*\(", "function").unwrap();
    // Struct definition
    h.add(r"class\s+([A-Za-z0-9_]*)", "struct").unwrap();
    // Operators
    h.join(
        &[
            r"(=)",
            r"(\+)",
            r"(\-)",
            r"(\*)",
            r"(\s/\s)",
            r"(\s//\s)",
            r"(%)",
            r"(\+=)",
            r"(\-=)",
            r"(\*=)",
            r"(\\=)",
            r"(==)",
            r"(!=)",
            r"(>=)",
            r"(<=)",
            r"(<)",
            r"(>)",
        ],
        "operator",
    )
    .unwrap();
    // Attributes
    h.add("@.*$", "attribute").unwrap();
    h
}