support #undef NAME
[neatcc.git] / tok.c
blob34a39033b01af2de072a96c17bce14b79fd3f312
1 #include <ctype.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "tok.h"
7 extern void cpp_init(int fd);
8 extern int cpp_read(char *s);
10 static char buf[BUFSIZE];
11 static int len;
12 static int cur;
13 static char name[NAMELEN];
14 static int next;
15 static int pre;
17 static struct {
18 char *name;
19 unsigned id;
20 } kwds[] = {
21 {"void", TOK_VOID},
22 {"static", TOK_STATIC},
23 {"extern", TOK_EXTERN},
24 {"return", TOK_RETURN},
25 {"unsigned", TOK_UNSIGNED},
26 {"signed", TOK_SIGNED},
27 {"short", TOK_SHORT},
28 {"long", TOK_LONG},
29 {"int", TOK_INT},
30 {"char", TOK_CHAR},
31 {"struct", TOK_STRUCT},
32 {"union", TOK_UNION},
33 {"enum", TOK_ENUM},
34 {"typedef", TOK_TYPEDEF},
35 {"if", TOK_IF},
36 {"else", TOK_ELSE},
37 {"for", TOK_FOR},
38 {"while", TOK_WHILE},
39 {"do", TOK_DO},
40 {"switch", TOK_SWITCH},
41 {"case", TOK_CASE},
42 {"sizeof", TOK_SIZEOF},
43 {"break", TOK_BREAK},
44 {"continue", TOK_CONTINUE},
45 {"default", TOK_DEFAULT},
46 {"goto", TOK_GOTO},
49 static char *tok3[] = {
50 "<<", ">>", "++", "--", "<<=", ">>=", "...", "+=", "-=", "*=", "/=",
51 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->", "/*"
54 static int get_tok3(int num)
56 int i;
57 for (i = 0; i < ARRAY_SIZE(tok3); i++)
58 if (num == TOK3(tok3[i]))
59 return num;
60 return 0;
63 static char *esc_code = "abefnrtv";
64 static char *esc = "\a\b\e\f\n\r\t\v";
65 static char *digs = "0123456789abcdef";
67 static int esc_char(int *c, char *s)
69 if (*s != '\\') {
70 *c = *s;
71 return 1;
73 if (strchr(esc_code, s[1])) {
74 *c = esc[strchr(esc_code, s[1]) - esc_code];
75 return 2;
77 if (isdigit(s[1]) || s[1] == 'x') {
78 int ret = 0;
79 int base = 8;
80 int i = 1;
81 char *d;
82 if (s[1] == 'x') {
83 base = 16;
84 i++;
86 while ((d = strchr(digs, s[i]))) {
87 ret *= base;
88 ret += d - digs;
89 i++;
91 *c = ret;
92 return i;
94 *c = s[1];
95 return 2;
98 static long num;
100 long tok_num(void)
102 return num;
105 static void readnum(void)
107 int base = 10;
108 if (buf[cur] == '0' && buf[cur + 1] == 'x') {
109 base = 16;
110 cur += 2;
112 if (strchr(digs, buf[cur])) {
113 long result = 0;
114 char *c;
115 if (base == 10 && buf[cur] == '0')
116 base = 8;
117 while (cur < len && (c = strchr(digs, buf[cur]))) {
118 result *= base;
119 result += c - digs;
120 cur++;
122 num = result;
123 while (cur < len && tolower(buf[cur]) == 'u' ||
124 tolower(buf[cur]) == 'l')
125 cur++;
126 return;
128 if (buf[cur] == '\'') {
129 int ret;
130 cur += 2 + esc_char(&ret, buf + cur + 1);
131 num = ret;
132 return;
134 num = -1;
137 static char str[BUFSIZE];
138 static int str_len;
140 int tok_str(char *buf)
142 if (buf)
143 memcpy(buf, str, str_len);
144 return str_len;
147 static int readstr(char *out)
149 char *s = out;
150 char *r = buf + cur;
151 char *e = buf + len;
152 r++;
153 while (r < e && *r != '"') {
154 if (*r == '\\') {
155 int c;
156 r += esc_char(&c, r);
157 *s++ = c;
158 } else {
159 *s++ = *r++;
162 *s++ = '\0';
163 cur = r - buf + 1;
164 return s - out - 1;
167 static int id_char(int c)
169 return isalnum(c) || c == '_';
172 static int skipws(void)
174 while (1) {
175 if (cur == len) {
176 int r;
177 while (!(r = cpp_read(buf + cur)))
179 if (r == -1)
180 return 1;
181 len += r;
183 while (cur < len && isspace(buf[cur]))
184 cur++;
185 if (cur == len)
186 continue;
187 if (TOK2(buf + cur) != TOK2("/*"))
188 return 0;
189 while (++cur < len) {
190 if (buf[cur] == '*' && buf[cur + 1] == '/') {
191 cur += 2;
192 break;
196 return 0;
199 int tok_get(void)
201 int num;
202 if (next != -1) {
203 int tok = next;
204 next = -1;
205 return tok;
207 pre = cur;
208 if (skipws())
209 return TOK_EOF;
210 if (buf[cur] == '"') {
211 str_len = 0;
212 while (buf[cur] == '"') {
213 str_len += readstr(str + str_len);
214 if (skipws())
215 return TOK_EOF;
217 str_len++;
218 return TOK_STR;
220 if (isdigit(buf[cur]) || buf[cur] == '\'') {
221 readnum();
222 return TOK_NUM;
224 if (id_char(buf[cur])) {
225 char *s = name;
226 int i;
227 while (cur < len && id_char(buf[cur]))
228 *s++ = buf[cur++];
229 *s = '\0';
230 for (i = 0; i < ARRAY_SIZE(kwds); i++)
231 if (!strcmp(kwds[i].name, name))
232 return kwds[i].id;
233 return TOK_NAME;
235 if ((num = get_tok3(TOK3(buf + cur)))) {
236 cur += 3;
237 return num;
239 if ((num = get_tok3(TOK2(buf + cur)))) {
240 cur += 2;
241 return num;
243 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf[cur]))
244 return buf[cur++];
245 return -1;
248 int tok_see(void)
250 if (next == -1)
251 next = tok_get();
252 return next;
255 void tok_init(int fd)
257 next = -1;
258 cpp_init(fd);
261 char *tok_id(void)
263 return name;
266 long tok_addr(void)
268 return next == -1 ? cur : pre;
271 void tok_jump(long addr)
273 cur = addr;
274 pre = cur - 1;
275 next = -1;