-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrongsyntax.h
More file actions
157 lines (155 loc) · 4.33 KB
/
wrongsyntax.h
File metadata and controls
157 lines (155 loc) · 4.33 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
#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"}; // 12
char *keywords2[] = {"int", "long", "double", "float", "char", "unsigned", "signed", "void", "#define"};// 8
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
if(isdigit(ch)) {
curr_node->color=MAGENTA;
}
int j;
for(j=0; j<12; 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!=curr_node) {
started->color=CYAN;
started=started->next;
} started->color=CYAN;
break;
}
else if(is_separator(search->next->ch)) {
while(started!=curr_node) {
started->color=CYAN;
started=started->next;
} started->color=CYAN;
break;
}
}
}
for(j=0; j<8; j++){
int slen=strlen(keywords2[j]), count=0;
dNode *search=curr_node, *started=curr_node;
while(search->next!=NULL && search->ch==keywords2[j][count]) {
search=search->next;
count++;
}
if(keywords2[j][count]=='\0') {
if(search->next==NULL) {
while(started!=curr_node) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
else if(is_separator(search->next->ch)) {
while(started!=curr_node) {
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) in_string=0;
}
else if(ch=='"'||ch=='\'') {
in_string=ch;
curr_node->color=GREEN;
}
else if(curr_node->prev!=NULL) {
if(is_separator(curr_node->prev->ch)) {
int j;
for(j=0; j<12; 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=CYAN;
started=started->next;
} started->color=CYAN;
break;
}
else if(is_separator(search->ch)) {
while(started!=search) {
started->color=CYAN;
started=started->next;
} started->color=CYAN;
break;
}
}
}
for(j=0; j<8; j++){
int slen=strlen(keywords2[j]), count=0;
dNode *search=curr_node, *started=curr_node;
while(search->next!=NULL && search->ch==keywords2[j][count]) {
search=search->next;
count++;
}
if(keywords2[j][count]=='\0') {
if(search->next==NULL) {
while(started!=curr_node) {
started->color=YELLOW;
started=started->next;
} started->color=YELLOW;
break;
}
else if(is_separator(search->next->ch)) {
while(started!=curr_node) {
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;
}
}