11 static struct mem tok_mem
; /* the data read via cpp_read() so far */
12 static struct mem str
; /* the last tok_str() string */
16 static char name
[NAMELEN
];
25 {"static", TOK_STATIC
},
26 {"extern", TOK_EXTERN
},
27 {"return", TOK_RETURN
},
28 {"unsigned", TOK_UNSIGNED
},
29 {"signed", TOK_SIGNED
},
34 {"struct", TOK_STRUCT
},
37 {"typedef", TOK_TYPEDEF
},
43 {"switch", TOK_SWITCH
},
45 {"sizeof", TOK_SIZEOF
},
47 {"continue", TOK_CONTINUE
},
48 {"default", TOK_DEFAULT
},
52 static char *tok3
[] = {
53 "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=",
54 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->"
57 static int get_tok3(int num
)
60 for (i
= 0; i
< LEN(tok3
); i
++)
61 if (num
== TOK3(tok3
[i
]))
66 static char *esc_code
= "abefnrtv";
67 static char *esc
= "\a\b\e\f\n\r\t\v";
68 static char *digs
= "0123456789abcdef";
70 static int esc_char(int *c
, char *s
)
73 *c
= (unsigned char) *s
;
76 if (strchr(esc_code
, s
[1])) {
77 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
80 if (isdigit(s
[1]) || s
[1] == 'x') {
89 while ((d
= memchr(digs
, s
[i
], base
))) {
97 *c
= (unsigned char) s
[1];
110 static void readnum(void)
113 num_bt
= 4 | BT_SIGNED
;
114 if (buf
[cur
] == '0' && tolower(buf
[cur
+ 1]) == 'x') {
115 num_bt
&= ~BT_SIGNED
;
119 if (strchr(digs
, tolower(buf
[cur
]))) {
122 if (base
== 10 && buf
[cur
] == '0')
124 while (cur
< len
&& (c
= strchr(digs
, tolower(buf
[cur
])))) {
131 int c
= tolower(buf
[cur
]);
132 if (c
!= 'u' && c
!= 'l')
135 num_bt
&= ~BT_SIGNED
;
137 num_bt
= (num_bt
& BT_SIGNED
) | LONGSZ
;
142 if (buf
[cur
] == '\'') {
144 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
151 void tok_str(char **buf
, int *len
)
154 *len
= mem_len(&str
) + 1;
156 *buf
= mem_buf(&str
);
159 static void readstr(struct mem
*mem
)
165 while (s
< e
&& *s
!= '"') {
167 s
+= esc_char(&c
, s
);
170 mem_putc(mem
, (unsigned char) *s
++);
176 static int id_char(int c
)
178 return isalnum(c
) || c
== '_';
181 static int skipws(void)
189 if (cpp_read(&cbuf
, &clen
))
191 mem_put(&tok_mem
, cbuf
, clen
);
192 buf
= mem_buf(&tok_mem
);
193 len
= mem_len(&tok_mem
);
195 while (cur
< len
&& isspace(buf
[cur
]))
199 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n') {
203 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
204 while (++cur
< len
&& buf
[cur
] != '\n')
205 if (buf
[cur
] == '\\')
209 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
210 while (++cur
< len
) {
211 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
234 if (buf
[cur
] == '"') {
236 while (buf
[cur
] == '"') {
243 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'') {
247 if (id_char(buf
[cur
])) {
250 while (cur
< len
&& id_char(buf
[cur
]))
253 for (i
= 0; i
< LEN(kwds
); i
++)
254 if (!strcmp(kwds
[i
].name
, name
))
258 if (cur
+ 3 <= len
&& (num
= get_tok3(TOK3(buf
+ cur
)))) {
262 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
266 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
285 return next
== -1 ? cur
: pre
;
288 void tok_jump(long addr
)