10 static struct mem tok_mem
; /* the data read via cpp_read() so far */
11 static struct mem str
; /* the last tok_str() string */
15 static char name
[NAMELEN
];
24 {"static", TOK_STATIC
},
25 {"extern", TOK_EXTERN
},
26 {"return", TOK_RETURN
},
27 {"unsigned", TOK_UNSIGNED
},
28 {"signed", TOK_SIGNED
},
33 {"struct", TOK_STRUCT
},
36 {"typedef", TOK_TYPEDEF
},
42 {"switch", TOK_SWITCH
},
44 {"sizeof", TOK_SIZEOF
},
46 {"continue", TOK_CONTINUE
},
47 {"default", TOK_DEFAULT
},
51 static char *tok3
[] = {
52 "<<=", ">>=", "...", "<<", ">>", "++", "--", "+=", "-=", "*=", "/=",
53 "%=", "|=", "&=", "^=", "&&", "||", "==", "!=", "<=", ">=", "->"
56 static int get_tok3(int num
)
59 for (i
= 0; i
< LEN(tok3
); i
++)
60 if (num
== TOK3(tok3
[i
]))
65 static char *esc_code
= "abefnrtv";
66 static char *esc
= "\a\b\e\f\n\r\t\v";
67 static char *digs
= "0123456789abcdef";
69 static int esc_char(int *c
, char *s
)
72 *c
= (unsigned char) *s
;
75 if (strchr(esc_code
, s
[1])) {
76 *c
= esc
[strchr(esc_code
, s
[1]) - esc_code
];
79 if (isdigit(s
[1]) || s
[1] == 'x') {
88 while ((d
= memchr(digs
, s
[i
], base
))) {
96 *c
= (unsigned char) s
[1];
109 static void readnum(void)
112 num_bt
= 4 | BT_SIGNED
;
113 if (buf
[cur
] == '0' && buf
[cur
+ 1] == 'x') {
114 num_bt
&= ~BT_SIGNED
;
118 if (strchr(digs
, tolower(buf
[cur
]))) {
121 if (base
== 10 && buf
[cur
] == '0')
123 while (cur
< len
&& (c
= strchr(digs
, tolower(buf
[cur
])))) {
130 int c
= tolower(buf
[cur
]);
131 if (c
!= 'u' && c
!= 'l')
134 num_bt
&= ~BT_SIGNED
;
136 num_bt
= (num_bt
& BT_SIGNED
) | LONGSZ
;
141 if (buf
[cur
] == '\'') {
143 cur
+= 2 + esc_char(&ret
, buf
+ cur
+ 1);
150 void tok_str(char **buf
, int *len
)
153 *len
= mem_len(&str
) + 1;
155 *buf
= mem_buf(&str
);
158 static void readstr(struct mem
*mem
)
164 while (s
< e
&& *s
!= '"') {
166 s
+= esc_char(&c
, s
);
169 mem_putc(mem
, (unsigned char) *s
++);
175 static int id_char(int c
)
177 return isalnum(c
) || c
== '_';
180 static int skipws(void)
188 if (cpp_read(&cbuf
, &clen
))
190 mem_put(&tok_mem
, cbuf
, clen
);
191 buf
= mem_buf(&tok_mem
);
192 len
= mem_len(&tok_mem
);
194 while (cur
< len
&& isspace(buf
[cur
]))
198 if (buf
[cur
] == '\\' && buf
[cur
+ 1] == '\n') {
202 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '/') {
203 while (cur
< len
&& buf
[cur
] != '\n')
207 if (buf
[cur
] == '/' && buf
[cur
+ 1] == '*') {
208 while (++cur
< len
) {
209 if (buf
[cur
] == '*' && buf
[cur
+ 1] == '/') {
232 if (buf
[cur
] == '"') {
234 while (buf
[cur
] == '"') {
241 if (isdigit(buf
[cur
]) || buf
[cur
] == '\'') {
245 if (id_char(buf
[cur
])) {
248 while (cur
< len
&& id_char(buf
[cur
]))
251 for (i
= 0; i
< LEN(kwds
); i
++)
252 if (!strcmp(kwds
[i
].name
, name
))
256 if (cur
+ 3 <= len
&& (num
= get_tok3(TOK3(buf
+ cur
)))) {
260 if ((num
= get_tok3(TOK2(buf
+ cur
)))) {
264 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", buf
[cur
]))
283 return next
== -1 ? cur
: pre
;
286 void tok_jump(long addr
)