-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
95 lines (79 loc) · 1.67 KB
/
parser.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
#include "parser.h"
#include <stdlib.h>
#include <wctype.h>
#ifdef DEBUG
static int depth = 0;
#endif // DEBUG
static int col = 0;
static int row = 1;
bool noexpect(wchar_t *c) {
bool success = false;
while (next != WEOF && *c && *c != next) {
c++;
}
if (*c == '\0') {
success = true;
scan();
}
return success;
}
bool expecti(wchar_t c) {
bool success = false;
if ((success = next == c))
scani();
return success;
}
bool expect(wchar_t c) {
bool success = false;
if ((success = next == c))
scan();
return success;
}
void scan() {
if (next != WEOF) {
next = getwchar();
col++;
if (iswspace(next) && !iswblank(next)) {
col = 0;
row++;
}
}
}
void scani() {
if (next != WEOF) {
do {
scan();
} while (next != WEOF && iswspace(next));
}
}
wchar_t *next_to_string() {
static wchar_t string[2];
if (next == WEOF) {
return L"EOF";
} else {
swprintf(string, 2, L"%lc", next);
return string;
}
}
void error(char *err) {
wprintf(L"ERROR(%d:%d): %s (next: '%ls')\n", row, col, err, next_to_string());
exit(EXIT_FAILURE);
}
#ifdef DEBUG
void enter(char *what) {
int i;
for (i = 0; i < depth; i++) {
wprintf(L"| ");
}
wprintf(L"+-%s: enter\tnext: '%ls' (%d:%d)\n", what, next_to_string(), row, col);
depth++;
}
void leave(char *what) {
int i;
depth--;
for (i = 0; i < depth; i++) {
wprintf(L"| ");
}
wprintf(L"+-%s: leave\tnext: '%ls' (%d:%d)\n", what, next_to_string(), row, col);
}
#endif // DEBUG