-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.c
73 lines (67 loc) · 1.62 KB
/
token.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
// All the logic for reading tokens from stdin and file pointers goes here
void add_to_buf(char ch) {
if((bufused < IBUFSIZE - 1) && ch != EOF) {
buf[bufused++] = ch;
}
}
char *buf2str() {
buf[bufused++] = '\0';
return dclang_strdup(buf);
}
void setinput(FILE *fp) {
file_stack[fsp++] = ifp;
ifp = fp;
}
void revertinput() {
if (fsp == 0) {
exit(0);
}
ifp = file_stack[--fsp];
}
char *get_token() {
DCLANG_LONG ch;
bufused = 0;
// skip leading spaces and comments
while (1) {
// skip leading space
do {
if((ch = fgetc(ifp)) == EOF) {
revertinput();
return "EOF";
}
} while(isspace(ch));
// if we are starting a comment:
if (strchr("#", ch)) {
// go to the end of the line
do {
if((ch = fgetc(ifp)) == EOF) {
revertinput();
return "EOF";
}
} while(! strchr("\n", ch));
continue;
}
// is this a string?
if (strchr("\"", ch)) {
// call the sub-routine to deal with the string:
stringfunc();
continue;
} else {
add_to_buf(ch);
break;
}
}
// grab all the next non-whitespace characters
while (1) {
// check again for EOF
if ((ch = fgetc(ifp)) == EOF) {
revertinput();
return "EOF";
}
if (isspace(ch)) {
ungetc(ch, ifp);
return buf2str();
}
add_to_buf(ch);
}
}