-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.h
More file actions
133 lines (131 loc) · 3.71 KB
/
syntax.h
File metadata and controls
133 lines (131 loc) · 3.71 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
#include <ctype.h>
#define MAGENTA 2
#define YELLOW 3
#define CYAN 4
#define GREEN 5
#define WHITE 6
int is_separator(char ch) { // function for checking if char is separator
return isspace(ch) || ch=='\n' || strchr(",.()+-/*=~%<>[];", ch)!=NULL; // strchr checks if ch is any of the chars in string given
}
void update_syntax(buffer *buff) {
line *curr_line=buff->top;
dNode *curr_node=curr_line->start;
char ch;
int in_string=0;
char *keywords1[] = {"switch", "if", "while", "for", "break", "continue", "return", "else", "struct", "typedef", "case", "int", "long", "double", "float", "char", "unsigned", "signed", "void"}; // 19 keywords
while(curr_line!=NULL) { //reset
while(curr_node!=NULL) {
curr_node->color=WHITE;
curr_node=curr_node->next;
}
curr_line=curr_line->next_line;
if(curr_line==NULL) break;
curr_node=curr_line->start;
}
curr_line=buff->top;
curr_node=curr_line->start;
while(curr_line!=NULL) {
while(curr_node!=NULL) {
ch=curr_node->ch;
if(curr_node->prev==NULL) { // for digits and keywords at the beginning of lines
if(isdigit(ch)) {
curr_node->color=MAGENTA;
}
if(ch=='#') {
dNode *temp=curr_node;
while(temp->next!=NULL) {
temp->color=MAGENTA;
temp=temp->next;
}
}
else if(ch=='/' && curr_node->next->ch=='/') { //for single line comments
dNode *curr=curr_node;
while(curr->next!=NULL) {
curr->color=CYAN;
curr=curr->next;
}
break;
}
int j;
for(j=0; j<19; j++){
int slen=strlen(keywords1[j]), count=0;
dNode *search=curr_node, *started=curr_node;
while(search->next!=NULL && search->ch==keywords1[j][count]) {
search=search->next;
count++;
}
if(keywords1[j][count]=='\0') {
if(search->next==NULL) {
while(started!=search) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
else if(is_separator(search->ch)) {
search=search->prev;
while(started!=search) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
}
}
}
else if(isdigit(ch) && (curr_node->prev->color==MAGENTA || is_separator(curr_node->prev->ch))) { //if current char is a digit
curr_node->color=MAGENTA;
}
else if(in_string) { //for strings
curr_node->color=GREEN;
if(ch==in_string||ch=='>') in_string=0;
}
else if(ch=='"'||ch=='\''||ch=='<') { // for checking if strings are closed
in_string=ch;
curr_node->color=GREEN;
}
else if(ch=='/' && curr_node->next->ch=='/') { //for single line comments
dNode *curr=curr_node;
while(curr->next!=NULL) {
curr->color=CYAN;
curr=curr->next;
}
break;
}
else if(curr_node->prev!=NULL) { //for keywords in body of line
if(is_separator(curr_node->prev->ch)) {
int j;
for(j=0; j<19; j++){
int slen=strlen(keywords1[j]), count=0;
dNode *search=curr_node, *started=curr_node;
while(search->next!=NULL && search->ch==keywords1[j][count]) {
search=search->next;
count++;
}
if(keywords1[j][count]=='\0') {
if(search->next==NULL) {
while(started!=search) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
else if(is_separator(search->ch)) {
search=search->prev;
while(started!=search) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
}
}
}
}
curr_node=curr_node->next;
}
curr_line=curr_line->next_line;
if(curr_line==NULL) break;
curr_node=curr_line->start;
}
}