-
Notifications
You must be signed in to change notification settings - Fork 25
/
hilite.c
105 lines (80 loc) · 2.2 KB
/
hilite.c
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
/* hlite.c, generic syntax highlighting, Atto Emacs, Hugh Barney, Public Domain, 2016 */
#include "header.h"
int state = ID_DEFAULT;
int next_state = ID_DEFAULT;
int skip_count = 0;
char_t get_at(buffer_t *bp, point_t pt)
{
return (*ptr(bp, pt));
}
static char_t symbols[] = "{}[]()!'£$%^&*-+=:;@~#<>,.?/\\|";
int is_symbol(char_t c)
{
register char_t *p = symbols;
for (p = symbols; *p != '\0'; p++)
if (*p == c) return 1;
return 0;
}
void set_parse_state(buffer_t *bp, point_t pt)
{
register point_t po;
state = ID_DEFAULT;
next_state = ID_DEFAULT;
skip_count = 0;
for (po =0; po < pt; po++)
parse_text(bp, po);
}
int parse_text(buffer_t *bp, point_t pt)
{
if (skip_count-- > 0)
return state;
char_t c_now = get_at(bp, pt);
char_t c_next = get_at(bp, pt + 1);
state = next_state;
if (state == ID_DEFAULT && c_now == '/' && c_next == '*') {
skip_count = 1;
return (next_state = state = ID_BLOCK_COMMENT);
}
if (state == ID_BLOCK_COMMENT && c_now == '*' && c_next == '/') {
skip_count = 1;
next_state = ID_DEFAULT;
return ID_BLOCK_COMMENT;
}
if (state == ID_DEFAULT && c_now == '/' && c_next == '/') {
skip_count = 1;
return (next_state = state = ID_LINE_COMMENT);
}
if (state == ID_LINE_COMMENT && c_now == '\n')
return (next_state = ID_DEFAULT);
if (state == ID_DEFAULT && c_now == '"')
return (next_state = ID_DOUBLE_STRING);
if (state == ID_DOUBLE_STRING && c_now == '\\') {
skip_count = 1;
return (next_state = ID_DOUBLE_STRING);
}
if (state == ID_DOUBLE_STRING && c_now == '"') {
next_state = ID_DEFAULT;
return ID_DOUBLE_STRING;
}
if (state == ID_DEFAULT && c_now == '\'')
return (next_state = ID_SINGLE_STRING);
if (state == ID_SINGLE_STRING && c_now == '\\') {
skip_count = 1;
return (next_state = ID_SINGLE_STRING);
}
if (state == ID_SINGLE_STRING && c_now == '\'') {
next_state = ID_DEFAULT;
return ID_SINGLE_STRING;
}
if (state != ID_DEFAULT)
return (next_state = state);
if (state == ID_DEFAULT && c_now >= '0' && c_now <= '9') {
next_state = ID_DEFAULT;
return (state = ID_DIGITS);
}
if (state == ID_DEFAULT && 1 == is_symbol(c_now)) {
next_state = ID_DEFAULT;
return (state = ID_SYMBOL);
}
return (next_state = state);
}