summaryrefslogtreecommitdiff
path: root/syntax.c
blob: ffaae4a562a72e66a8b0e77f7a6a91e196d8037a (plain)
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
#include "syntax.h"

#include <string.h>
#include "editor.h"

char* c_highlight_extensions[] = { ".c", ".h", ".cpp", ".cc", NULL };

char* c_highlight_keywords[] = {
	"switch", "if", "while", "for", "break", "continue", "return", "else",
	"struct", "union", "typedef", "static", "enum", "class", "case",
	"#include", "#define", "#ifdef", "#ifndef",

	"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
	"void|", NULL
};

struct editor_syntax highlight_database[] = {
	{
		"c",
		c_highlight_extensions,
		c_highlight_keywords,
		"//", "/*", "*/",
		HIGHLIGHT_FLAG_NUMBERS | HIGHLIGHT_FLAG_STRINGS
	}
};

int is_separator(int c)
{
	return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
}

void editor_update_syntax(struct editor_state* editor, struct editor_row* row)
{
	row->highlight = realloc(row->highlight, row->render_size);
	memset(row->highlight, HIGHLIGHT_NORMAL, row->render_size);

	if (editor->syntax == NULL)
		return;

	char** keywords = editor->syntax->keywords;

	// TODO: Remove
	char* single_line_comment_start = editor->syntax->single_line_comment_start;
	char* multi_line_comment_start = editor->syntax->multi_line_comment_start;
	char* multi_line_comment_end = editor->syntax->multi_line_comment_end;
	
	int single_line_comment_start_length = single_line_comment_start ? strlen(single_line_comment_start) : 0;
	int multi_line_comment_start_length = multi_line_comment_start ? strlen(multi_line_comment_start) : 0;
	int multi_line_comment_end_length = multi_line_comment_end ? strlen(multi_line_comment_end) : 0;

	int previous_separator = 1;
	int in_string = 0;
	int in_comment = (row->index > 0 && editor->rows[row->index - 1].highlight_open_comment);

	int i = 0;
	while (i < row->render_size) {
		char c = row->render[i];
		unsigned char previous_highlight = (i > 0) ? row->highlight[i - 1] : HIGHLIGHT_NORMAL;

		if (single_line_comment_start_length && !in_string && !in_comment) {
			if (!strncmp(&row->render[i], single_line_comment_start, single_line_comment_start_length)) {
				memset(&row->highlight[i], HIGHLIGHT_COMMENT, row->render_size - i);
				break;
			}			
		}

		if (multi_line_comment_start_length && multi_line_comment_end_length && !in_string) {
			if (in_comment) {
				row->highlight[i] = HIGHLIGHT_MULTILINE_COMMENT;
				if (!strncmp(&row->render[i], multi_line_comment_end, multi_line_comment_end_length)) {
					memset(&row->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_end_length);

					i += multi_line_comment_end_length;
					in_comment = 0;
					previous_separator = 1;
					continue;
				} else {
					i++;
					continue;
				}
			} else if (!strncmp(&row->render[i], multi_line_comment_start, multi_line_comment_start_length)) {
				memset(&row->highlight[i], HIGHLIGHT_MULTILINE_COMMENT, multi_line_comment_start_length);
				i += multi_line_comment_start_length;
				in_comment = 1;
				continue;
			}
		}

		if (editor->syntax->flags & HIGHLIGHT_FLAG_STRINGS) {
			if (in_string) {
				row->highlight[i] = HIGHLIGHT_STRING;

				if (c == '\\' && i + 1 < row->render_size) {
					row->highlight[i + 1] = HIGHLIGHT_STRING;
					i += 2;
					continue;
				}

				if (c == in_string)
					in_string = 0;

				i++;
				previous_separator = 1;
				continue;
			} else {
				if (c == '"' || c == '\'') {
					in_string = c;
					row->highlight[i] = HIGHLIGHT_STRING;
					i++;
					continue;
				}
			}
		}

		if (editor->syntax->flags & HIGHLIGHT_FLAG_NUMBERS) {
			if ((isdigit(c) && (previous_separator || previous_highlight == HIGHLIGHT_NUMBER)) || (c == '.' && previous_highlight == HIGHLIGHT_NUMBER)) {
				row->highlight[i] = HIGHLIGHT_NUMBER;
				i++;
				previous_separator = 0;
				continue;
			}
		}

		if (previous_separator) {
			int j;
			for (j = 0; keywords[j]; j++) {
				int keyword_length = strlen(keywords[j]);
				int is_secondary = keywords[j][keyword_length - 1] == '|';

				if (is_secondary)
					keyword_length--;

				if (!strncmp(&row->render[i], keywords[j], keyword_length) && is_separator(row->render[i + keyword_length])) {
					memset(&row->highlight[i], is_secondary ? HIGHLIGHT_KEYWORD2 : HIGHLIGHT_KEYWORD1, keyword_length);
					i += keyword_length;
					break;
				}
			}
			if (keywords[j] != NULL) {
				previous_separator = 0;
				continue;
			}
		}

		previous_separator = is_separator(c);
		i++;
	}

	int changed = (row->highlight_open_comment != in_comment);
	row->highlight_open_comment = in_comment;
	if (changed && row->index + 1 < editor->row_count)
		editor_update_syntax(editor, &editor->rows[row->index + 1]);
}

int editor_syntax_to_colour(int highlight)
{
	switch (highlight) {
		case HIGHLIGHT_MULTILINE_COMMENT:
		case HIGHLIGHT_COMMENT: return 36;
		case HIGHLIGHT_KEYWORD1: return 33;
		case HIGHLIGHT_KEYWORD2: return 32;
		case HIGHLIGHT_STRING: return 35;
		case HIGHLIGHT_NUMBER: return 31;
		case HIGHLIGHT_MATCH: return 34;
		default: return 37;
	}
}

void editor_select_syntax_highlight(struct editor_state* editor)
{
	editor->syntax = NULL;
	
	if (editor->filename == NULL)
		return;

	char* extension = strrchr(editor->filename, '.');

	for (unsigned int j = 0; j < HIGHLIGHT_DATABASE_ENTRY_COUNT; j++) {
		struct editor_syntax* syntax = &highlight_database[j];
		unsigned int i = 0;

		while (syntax->filetype_match[i]) {
			int is_extension = (syntax->filetype_match[i][0] == '.');
			if ((is_extension && extension && !strcmp(extension, syntax->filetype_match[i])) || (!is_extension && strstr(editor->filename, syntax->filetype_match[i]))) {
				editor->syntax = syntax;

				int file_row;
				for (file_row = 0; file_row < editor->row_count; file_row++) {
					editor_update_syntax(editor, &editor->rows[file_row]);
				}
				
				return;
			}
			i++;
		}
	}
}