forked from Hydra0xetc/screenCODE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_highlighting_python.c
More file actions
220 lines (207 loc) · 10.2 KB
/
syntax_highlighting_python.c
File metadata and controls
220 lines (207 loc) · 10.2 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
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
#include "screenshot.h"
#include "syntax_highlighting.h" // New header for declarations
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <glib.h> // For GHashTable and GString
// Global hash tables for faster lookups
extern GHashTable *keywords_ht;
extern GHashTable *preprocessor_directives_ht;
extern GHashTable *standard_functions_ht;
// Helper function to append highlighted text
static void append_and_highlight(
GString* highlighted_code,
const char* start_of_plain_text,
const char* current_token_start,
const char* color,
size_t len
) {
if (start_of_plain_text != current_token_start) {
char *plain_text = g_strndup(start_of_plain_text, current_token_start - start_of_plain_text);
char *escaped_plain_text = g_markup_escape_text(plain_text, -1);
g_string_append(highlighted_code, escaped_plain_text);
g_free(escaped_plain_text);
g_free(plain_text);
}
char *token_text = g_strndup(current_token_start, len);
char *escaped_token = g_markup_escape_text(token_text, -1);
g_string_append_printf(highlighted_code, "<span foreground='%s'>%s</span>", color, escaped_token);
g_free(escaped_token);
g_free(token_text);
}
// Python Language Syntax Highlighting
void init_syntax_tables_python() {
keywords_ht = g_hash_table_new(g_str_hash, g_str_equal);
const char* keywords[] = {
"False", "None", "True", "and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else", "except", "finally",
"for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal",
"not", "or", "pass", "raise", "return", "try", "while", "with", "yield",
"match", "case",
NULL
};
for (int i = 0; keywords[i] != NULL; i++) {
g_hash_table_insert(keywords_ht, (gpointer)keywords[i], GINT_TO_POINTER(1));
}
// Python doesn't have preprocessor directives like C
// preprocessor_directives_ht is not used for Python, so no need to initialize it here.
standard_functions_ht = g_hash_table_new(g_str_hash, g_str_equal);
const char* standard_functions[] = {
"print", "input", "len", "range", "sum", "max", "min", "abs", "round",
"open", "close", "read", "write", "append", "strip", "split", "join",
"int", "float", "str", "list", "tuple", "dict", "set", "bool", "type",
"id", "dir", "help", "isinstance", "issubclass", "super", "hasattr", "getattr",
"setattr", "delattr", "callable", "frozenset", "complex", "divmod", "enumerate",
"filter", "map", "next", "iter", "pow", "reversed", "slice", "sorted",
"zip", "__import__", "any", "all", "chr", "ord", "hex", "oct", "bin",
"classmethod", "staticmethod", "property", "bytearray", "bytes", "memoryview",
NULL
};
for (int i = 0; standard_functions[i] != NULL; i++) {
g_hash_table_insert(standard_functions_ht, (gpointer)standard_functions[i], GINT_TO_POINTER(1));
}
}
void free_syntax_tables_python() {
if (keywords_ht) {
g_hash_table_unref(keywords_ht);
keywords_ht = NULL;
}
if (standard_functions_ht) {
g_hash_table_unref(standard_functions_ht);
standard_functions_ht = NULL;
}
// preprocessor_directives_ht is not used for Python, so no need to free it here.
}
// Sorted operators for longest match first (Python)
static const char* python_sorted_operators[] = {
"**=", "//=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", ">>=", "<<=", // 3 chars
"==", "!=", ">=", "<=", "**", "//", "or", "and", "not", "is", "in", // 2 chars
"+", "-", "*", "/", "%", "=", ">", "<", "&", "|", "^", "~", ".", ":", "[", "]", "{", "}", "(", ")", // 1 char
NULL
};
char* highlight_python_syntax(const char* code) {
GString* highlighted_code = g_string_new("");
const char *ptr = code;
const char *start_of_plain_text = code;
while (*ptr != '\0') {
const char *current_token_start = ptr;
gboolean token_processed = FALSE; // Flag to indicate if a token was processed
// 1. Try to match triple-quoted strings first (Python specific)
if ((*ptr == '"' && *(ptr + 1) == '"' && *(ptr + 2) == '"') ||
(*ptr == '\'' && *(ptr + 1) == '\'' && *(ptr + 2) == '\'')) {
char quote_char = *ptr;
const char *scan_ptr = ptr + 3;
while (*scan_ptr != '\0') {
if (*scan_ptr == '\\' && *(scan_ptr + 1) != '\0') {
scan_ptr += 2; // Skip escaped char
} else if (*scan_ptr == quote_char && *(scan_ptr + 1) == quote_char && *(scan_ptr + 2) == quote_char) {
scan_ptr += 3; // Include closing triple quotes
break;
}
scan_ptr++;
}
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, "#9ece6a", scan_ptr - ptr);
ptr = scan_ptr; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
} else if (*ptr == '"' || *ptr == '\'') { // 2. Match single/double quoted strings
char quote_char = *ptr;
const char *scan_ptr = ptr + 1;
while (*scan_ptr != '\0' && *scan_ptr != '\n') { // Single line strings should stop at newline
if (*scan_ptr == '\\' && *(scan_ptr + 1) != '\0') {
scan_ptr += 2; // Skip backslash and escaped char
} else if (*scan_ptr == quote_char) {
scan_ptr++; // Include closing quote
break;
}
scan_ptr++;
}
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, "#9ece6a", scan_ptr - ptr);
ptr = scan_ptr; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
} else if (*ptr == '#') { // 3. Match comments
const char *scan_ptr = ptr + 1;
while (*scan_ptr != '\0' && *scan_ptr != '\n') {
scan_ptr++;
}
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, "#545c7e", scan_ptr - ptr);
ptr = scan_ptr; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
} else if (g_ascii_isdigit(*ptr) || (*ptr == '.' && g_ascii_isdigit(*(ptr + 1)))) { // 4. Match numbers
const char* num_scan_ptr = ptr;
if (*num_scan_ptr == '0' && (*(num_scan_ptr+1) == 'x' || *(num_scan_ptr+1) == 'X')) { // Hex
num_scan_ptr += 2;
while (g_ascii_isxdigit(*num_scan_ptr)) num_scan_ptr++;
} else if (*num_scan_ptr == '0' && (*(num_scan_ptr+1) == 'o' || *(num_scan_ptr+1) == 'O')) { // Octal
num_scan_ptr += 2;
while (*num_scan_ptr >= '0' && *num_scan_ptr <= '7') num_scan_ptr++;
} else if (*num_scan_ptr == '0' && (*(num_scan_ptr+1) == 'b' || *(num_scan_ptr+1) == 'B')) { // Binary
num_scan_ptr += 2;
while (*num_scan_ptr == '0' || *num_scan_ptr == '1') num_scan_ptr++;
} else { // Decimal or float
while (g_ascii_isdigit(*num_scan_ptr)) num_scan_ptr++;
if (*num_scan_ptr == '.') {
num_scan_ptr++;
while (g_ascii_isdigit(*num_scan_ptr)) num_scan_ptr++;
}
if (*num_scan_ptr == 'e' || *num_scan_ptr == 'E') { // Exponent
num_scan_ptr++;
if (*num_scan_ptr == '+' || *num_scan_ptr == '-') num_scan_ptr++;
while (g_ascii_isdigit(*num_scan_ptr)) num_scan_ptr++;
}
}
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, "#ff9e64", num_scan_ptr - ptr);
ptr = num_scan_ptr; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
} else if (g_ascii_isalpha(*ptr) || *ptr == '_') { // 5. Match keywords or functions
const char *word_scan_ptr = ptr;
while (g_ascii_isalnum(*word_scan_ptr) || *word_scan_ptr == '_') word_scan_ptr++;
char *word = g_strndup(ptr, word_scan_ptr - ptr);
const char* color = NULL;
if (g_hash_table_lookup(keywords_ht, word)) {
color = "#f7768e"; // Red for keywords
} else {
const char* lookahead = word_scan_ptr;
while (*lookahead != '\0' && isspace(*lookahead)) lookahead++;
if (*lookahead == '(' && g_hash_table_lookup(standard_functions_ht, word)) {
color = "#7aa2f7"; // Blue for functions
}
}
if (color) { // Only highlight if it's a keyword or function
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, color, word_scan_ptr - ptr);
ptr = word_scan_ptr; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
}
g_free(word);
} else { // 6. Try to match operators
for (int i = 0; python_sorted_operators[i] != NULL; i++) {
size_t op_len = strlen(python_sorted_operators[i]);
if (strncmp(ptr, python_sorted_operators[i], op_len) == 0) {
append_and_highlight(highlighted_code, start_of_plain_text, current_token_start, "#bb9af7", op_len);
ptr += op_len; // Advance ptr
start_of_plain_text = ptr;
token_processed = TRUE;
break;
}
}
}
if (!token_processed) {
// If no special token was found, it's plain text. Just advance ptr.
ptr++;
}
}
// After the loop, append any remaining plain text
if (start_of_plain_text != ptr) {
char *plain_text = g_strndup(start_of_plain_text, ptr - start_of_plain_text);
char *escaped_plain_text = g_markup_escape_text(plain_text, -1);
g_string_append(highlighted_code, escaped_plain_text);
g_free(escaped_plain_text);
g_free(plain_text);
}
return g_string_free(highlighted_code, FALSE);
}