9 static struct mem tok_mem
; /* the data read via cpp_read() so far */
10 static struct mem tok
; /* the previous token */
12 static long off
, off_pre
; /* current and previous positions */
14 static int tok_set
; /* the current token was read */
16 static char *tok3
[] = {
17 "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=",
18 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->"
21 static char *find_tok3(char *r
)
24 for (i
= 0; i
< LEN(tok3
); i
++) {
26 if (s
[0] == r
[0] && s
[1] == r
[1] && (!s
[2] || s
[2] == r
[2]))
32 static char *esc_code
= "abefnrtv";
33 static char *esc
= "\a\b\e\f\n\r\t\v";
34 static char *digs
= "0123456789abcdef";
36 static int esc_char(int *c
, char *s
)
39 *c
= (unsigned char) *s
;
42 if (strchr(esc_code
, (unsigned char) s
[1])) {
43 *c
= esc
[strchr(esc_code
, (unsigned char) s
[1]) - esc_code
];
46 if (isdigit(s
[1]) || s
[1] == 'x') {
55 while ((d
= memchr(digs
, s
[i
], base
))) {
63 *c
= (unsigned char) s
[1];
67 long tok_num(char *tok
, long *num
)
70 long num_bt
= 4 | T_MSIGN
;
71 if (tok
[0] == '0' && tolower(tok
[1]) == 'x') {
76 if (strchr(digs
, tolower((unsigned char) tok
[0]))) {
79 if (base
== 10 && tok
[0] == '0')
81 while (tok
[0] && (c
= strchr(digs
, tolower((unsigned char) tok
[0])))) {
88 int c
= tolower((unsigned char) tok
[0]);
89 if (c
!= 'u' && c
!= 'l')
94 num_bt
= (num_bt
& T_MSIGN
) | LONGSZ
;
101 esc_char(&ret
, tok
+ 1);
108 static int id_char(int c
)
110 return isalnum(c
) || c
== '_';
113 static int skipws(void)
121 if (cpp_read(&cbuf
, &clen
))
123 mem_put(&tok_mem
, cbuf
, clen
);
124 buf
= mem_buf(&tok_mem
);
125 len
= mem_len(&tok_mem
);
127 while (off
< len
&& isspace(buf
[off
]))
131 if (buf
[off
] == '\\' && buf
[off
+ 1] == '\n') {
135 if (buf
[off
] == '/' && buf
[off
+ 1] == '/') {
136 while (++off
< len
&& buf
[off
] != '\n')
137 if (buf
[off
] == '\\')
141 if (buf
[off
] == '/' && buf
[off
+ 1] == '*') {
142 while (++off
< len
) {
143 if (buf
[off
] == '*' && buf
[off
+ 1] == '/') {
155 static int tok_read(void)
163 if (buf
[off
] == '"') {
165 while (buf
[off
] == '"') {
167 while (off
< len
&& buf
[off
] != '"') {
168 if (buf
[off
] == '\\') {
169 off
+= esc_char(&c
, buf
+ off
);
172 mem_putc(&tok
, (unsigned char) buf
[off
++]);
175 if (off
>= len
|| buf
[off
++] != '"')
183 if (isdigit((unsigned char) buf
[off
])) {
184 if (buf
[off
] == '0' && (buf
[off
+ 1] == 'x' || buf
[off
+ 1] == 'X')) {
185 mem_putc(&tok
, (unsigned char) buf
[off
++]);
186 mem_putc(&tok
, (unsigned char) buf
[off
++]);
188 while (off
< len
&& strchr(digs
, tolower((unsigned char) buf
[off
])))
189 mem_putc(&tok
, (unsigned char) buf
[off
++]);
190 while (off
< len
&& strchr("uUlL", (unsigned char) buf
[off
]))
191 mem_putc(&tok
, (unsigned char) buf
[off
++]);
194 if (buf
[off
] == '\'') {
196 int n
= esc_char(&c
, buf
+ off
+ 1) + 1 + 1;
197 for (i
= 0; i
< n
; i
++)
198 mem_putc(&tok
, (unsigned char) buf
[off
++]);
201 if (id_char((unsigned char) buf
[off
])) {
202 while (off
< len
&& id_char((unsigned char) buf
[off
]))
203 mem_putc(&tok
, (unsigned char) buf
[off
++]);
206 if (off
+ 2 <= len
&& (t3
= find_tok3(buf
+ off
))) {
208 mem_put(&tok
, t3
, strlen(t3
));
211 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", (unsigned char) buf
[off
])) {
212 mem_putc(&tok
, (unsigned char) buf
[off
++]);
224 return mem_buf(&tok
);
233 return mem_buf(&tok
);
238 return mem_len(&tok
);
243 return tok_set
? off_pre
: off
;
246 void tok_jump(long addr
)