-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlextest.c
141 lines (124 loc) · 5.08 KB
/
lextest.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
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
#include "lex.h"
#include <stdio.h>
#include <string.h>
struct lex_test {
char* text;
enum token_type types[6];
};
static struct lex_test tests[] = {
{"", {END_OF_EXPRESSION}},
{"*", {STAR, END_OF_EXPRESSION}},
{"*=", {STAR_EQUAL, END_OF_EXPRESSION}},
{"++", {DOUBLE_PLUS, END_OF_EXPRESSION}},
{"0x17", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"0x17.fp1", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"071", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"09e1", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"09", {BOGUS, END_OF_EXPRESSION}},
{"morx*=17", {LITERAL_OR_ID, STAR_EQUAL, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"sizeof int", {SIZEOF, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"sizeof_int", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"sizeo sizeof", {LITERAL_OR_ID, SIZEOF, END_OF_EXPRESSION}},
{"sizeof", {SIZEOF, END_OF_EXPRESSION}},
{"a>>3", {LITERAL_OR_ID, RIGHT_SHIFT, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"a>>=3", {LITERAL_OR_ID, RIGHT_SHIFT_EQUAL, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"a->r", {LITERAL_OR_ID, ARROW, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"a\001", {LITERAL_OR_ID, BOGUS, END_OF_EXPRESSION}},
{"\"a\\001\"", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"\"a\"/*b*/+c", {LITERAL_OR_ID, PLUS, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"a.b", {LITERAL_OR_ID, DOT, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"(a0.b)", {OPEN_PAREN, LITERAL_OR_ID, DOT, LITERAL_OR_ID, CLOSE_PAREN, END_OF_EXPRESSION}},
{"1.2", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"1.2.3", {BOGUS, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"0x1.2", {BOGUS, END_OF_EXPRESSION}},
{"0x1.2p", {BOGUS, END_OF_EXPRESSION}},
{"0x1.2pf", {BOGUS, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"0x1.2.2p4", {BOGUS, LITERAL_OR_ID, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"1.0e4", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"1.0e-4", {LITERAL_OR_ID, END_OF_EXPRESSION}},
{"1.0e-4e", {BOGUS, LITERAL_OR_ID, END_OF_EXPRESSION}},
{"1.0e-4.", {BOGUS, DOT, END_OF_EXPRESSION}},
{"\"", {BOGUS, END_OF_EXPRESSION}},
{0, {0}}
};
static int assert_token(const char* test, lex_buf* buf,
enum token_type expected_type,
const char* expected_value) {
struct token token = get_next_token(buf);
if (token.token_type != expected_type) {
printf("Literal test %s failed: expected type %s but got %s\n",
test, token_names[expected_type], token_names[token.token_type]);
return 1;
}
if (expected_value) {
if (strcmp(expected_value, token.token_value) != 0) {
printf("Literal test failed: expected value %s but got %s\n",
expected_value, token.token_value);
return 1;
}
} else if (token.token_value) {
printf("Literal test failed: unexpected value %s\n",
token.token_value);
return 1;
}
return 0;
}
static int test_literals() {
lex_buf buf;
int failures = 0;
const char* teststr = "\"a\001\\x\\\"\"";
buf = start_lex(teststr);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "\"a\001\\x\\\"\"");
failures += assert_token(teststr, &buf, END_OF_EXPRESSION, 0);
teststr = "\"a\001\\x\\\"\"*";
done_lex(buf);
buf = start_lex(teststr);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "\"a\001\\x\\\"\"");
failures += assert_token(teststr, &buf, STAR, 0);
failures += assert_token(teststr, &buf, END_OF_EXPRESSION, 0);
teststr = "\"a\001\\x\\\"\"*''";
done_lex(buf);
buf = start_lex(teststr);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "\"a\001\\x\\\"\"");
failures += assert_token(teststr, &buf, STAR, 0);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "''");
failures += assert_token(teststr, &buf, END_OF_EXPRESSION, 0);
teststr = "\"a\001\\x\\\"\"*'\\''";
done_lex(buf);
buf = start_lex(teststr);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "\"a\001\\x\\\"\"");
failures += assert_token(teststr, &buf, STAR, 0);
failures += assert_token(teststr, &buf, LITERAL_OR_ID, "'\\''");
failures += assert_token(teststr, &buf, END_OF_EXPRESSION, 0);
done_lex(buf);
return failures;
}
int main() {
int i = 0;
int failures = test_literals();
while (tests[i].text) {
struct lex_test test = tests[i];
lex_buf buf = start_lex(test.text);
struct token token;
int token_no = 0;
while (1) {
token = get_next_token(&buf);
if (token.token_type != test.types[token_no]) {
printf("Test failed: expected %s but got %s in token %d of test %s\n",
token_names[test.types[token_no]],
token_names[token.token_type],
token_no,
test.text);
++failures;
break;
}
token_no += 1;
if (token.token_type == END_OF_EXPRESSION) {
break;
}
}
done_lex(buf);
++i;
}
return failures > 0 ? 1 : 0;
}