lexi.c revision 1.14.16.2 1 1.14.16.2 martin /* $NetBSD: lexi.c,v 1.14.16.2 2020/04/13 08:05:43 martin Exp $ */
2 1.3 tls
3 1.14.16.1 christos /*-
4 1.14.16.1 christos * SPDX-License-Identifier: BSD-4-Clause
5 1.12 agc *
6 1.1 cgd * Copyright (c) 1985 Sun Microsystems, Inc.
7 1.14.16.1 christos * Copyright (c) 1980, 1993
8 1.14.16.1 christos * The Regents of the University of California. All rights reserved.
9 1.1 cgd * All rights reserved.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.1 cgd * 3. All advertising materials mentioning features or use of this software
20 1.1 cgd * must display the following acknowledgement:
21 1.1 cgd * This product includes software developed by the University of
22 1.1 cgd * California, Berkeley and its contributors.
23 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.1 cgd * may be used to endorse or promote products derived from this software
25 1.1 cgd * without specific prior written permission.
26 1.1 cgd *
27 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 cgd * SUCH DAMAGE.
38 1.1 cgd */
39 1.1 cgd
40 1.5 mrg #if 0
41 1.14.16.1 christos #ifndef lint
42 1.5 mrg static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
43 1.14.16.1 christos #endif /* not lint */
44 1.14.16.1 christos #endif
45 1.14.16.1 christos
46 1.14.16.1 christos #include <sys/cdefs.h>
47 1.14.16.1 christos #ifndef lint
48 1.14.16.1 christos #if defined(__NetBSD__)
49 1.14.16.2 martin __RCSID("$NetBSD: lexi.c,v 1.14.16.2 2020/04/13 08:05:43 martin Exp $");
50 1.14.16.1 christos #elif defined(__FreeBSD__)
51 1.14.16.1 christos __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
52 1.14.16.1 christos #endif
53 1.5 mrg #endif
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * Here we have the token scanner for indent. It scans off one token and puts
57 1.1 cgd * it in the global variable "token". It returns a code, indicating the type
58 1.1 cgd * of token scanned.
59 1.1 cgd */
60 1.1 cgd
61 1.14.16.1 christos #include <err.h>
62 1.1 cgd #include <stdio.h>
63 1.1 cgd #include <ctype.h>
64 1.1 cgd #include <stdlib.h>
65 1.1 cgd #include <string.h>
66 1.14.16.1 christos #include <sys/param.h>
67 1.14.16.1 christos
68 1.1 cgd #include "indent_globs.h"
69 1.1 cgd #include "indent_codes.h"
70 1.14.16.1 christos #include "indent.h"
71 1.1 cgd
72 1.1 cgd struct templ {
73 1.14.16.1 christos const char *rwd;
74 1.14.16.1 christos int rwcode;
75 1.1 cgd };
76 1.1 cgd
77 1.14.16.1 christos /*
78 1.14.16.1 christos * This table has to be sorted alphabetically, because it'll be used in binary
79 1.14.16.1 christos * search. For the same reason, string must be the first thing in struct templ.
80 1.14.16.1 christos */
81 1.14.16.1 christos struct templ specials[] =
82 1.1 cgd {
83 1.14.16.1 christos {"_Bool", 4},
84 1.14.16.1 christos {"_Complex", 4},
85 1.14.16.1 christos {"_Imaginary", 4},
86 1.14.16.1 christos {"auto", 10},
87 1.14.16.1 christos {"bool", 4},
88 1.14.16.1 christos {"break", 9},
89 1.14.16.1 christos {"case", 8},
90 1.14.16.1 christos {"char", 4},
91 1.14.16.1 christos {"complex", 4},
92 1.14.16.1 christos {"const", 4},
93 1.14.16.1 christos {"continue", 12},
94 1.14.16.1 christos {"default", 8},
95 1.14.16.1 christos {"do", 6},
96 1.14.16.1 christos {"double", 4},
97 1.14.16.1 christos {"else", 6},
98 1.14.16.1 christos {"enum", 3},
99 1.14.16.1 christos {"extern", 10},
100 1.14.16.1 christos {"float", 4},
101 1.14.16.1 christos {"for", 5},
102 1.14.16.1 christos {"global", 4},
103 1.14.16.1 christos {"goto", 9},
104 1.14.16.1 christos {"if", 5},
105 1.14.16.1 christos {"imaginary", 4},
106 1.14.16.1 christos {"inline", 12},
107 1.14.16.1 christos {"int", 4},
108 1.14.16.1 christos {"long", 4},
109 1.14.16.1 christos {"offsetof", 1},
110 1.14.16.1 christos {"register", 10},
111 1.14.16.1 christos {"restrict", 12},
112 1.14.16.1 christos {"return", 9},
113 1.14.16.1 christos {"short", 4},
114 1.14.16.1 christos {"signed", 4},
115 1.14.16.1 christos {"sizeof", 2},
116 1.14.16.1 christos {"static", 10},
117 1.14.16.1 christos {"struct", 3},
118 1.14.16.1 christos {"switch", 7},
119 1.14.16.1 christos {"typedef", 11},
120 1.14.16.1 christos {"union", 3},
121 1.14.16.1 christos {"unsigned", 4},
122 1.14.16.1 christos {"void", 4},
123 1.14.16.1 christos {"volatile", 4},
124 1.14.16.1 christos {"while", 5}
125 1.1 cgd };
126 1.1 cgd
127 1.14.16.1 christos const char **typenames;
128 1.14.16.1 christos int typename_count;
129 1.14.16.1 christos int typename_top = -1;
130 1.1 cgd
131 1.14.16.1 christos /*
132 1.14.16.1 christos * The transition table below was rewritten by hand from lx's output, given
133 1.14.16.1 christos * the following definitions. lx is Katherine Flavel's lexer generator.
134 1.14.16.1 christos *
135 1.14.16.1 christos * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
136 1.14.16.1 christos * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
137 1.14.16.1 christos * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
138 1.14.16.1 christos * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
139 1.14.16.1 christos *
140 1.14.16.1 christos * D+ E FS? -> $float;
141 1.14.16.1 christos * D* "." D+ E? FS? -> $float;
142 1.14.16.1 christos * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
143 1.14.16.1 christos * HP H+ P FS? -> $float; NZ D* IS? -> $int;
144 1.14.16.1 christos * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
145 1.14.16.1 christos * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
146 1.14.16.1 christos */
147 1.14.16.1 christos static char const *table[] = {
148 1.14.16.1 christos /* examples:
149 1.14.16.1 christos 00
150 1.14.16.1 christos s 0xx
151 1.14.16.1 christos t 00xaa
152 1.14.16.1 christos a 11 101100xxa..
153 1.14.16.1 christos r 11ee0001101lbuuxx.a.pp
154 1.14.16.1 christos t.01.e+008bLuxll0Ll.aa.p+0
155 1.14.16.1 christos states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
156 1.14.16.1 christos ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
157 1.14.16.1 christos ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
158 1.14.16.1 christos ['7'] = "DEIDEHHHIJ U VUVVZZZ",
159 1.14.16.1 christos ['9'] = "DEJDEHHHJJ U VUVVZZZ",
160 1.14.16.1 christos ['a'] = " U VUVV ",
161 1.14.16.1 christos ['b'] = " K U VUVV ",
162 1.14.16.1 christos ['e'] = " FFF FF U VUVV ",
163 1.14.16.1 christos ['f'] = " f f U VUVV f",
164 1.14.16.1 christos ['u'] = " MM M i iiM M ",
165 1.14.16.1 christos ['x'] = " N ",
166 1.14.16.1 christos ['p'] = " FFX ",
167 1.14.16.1 christos ['L'] = " LLf fL PR Li L f",
168 1.14.16.1 christos ['l'] = " OOf fO S P O i O f",
169 1.14.16.1 christos ['+'] = " G Y ",
170 1.14.16.1 christos ['.'] = "B EE EE T W ",
171 1.14.16.1 christos /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
172 1.14.16.1 christos [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
173 1.14.16.1 christos };
174 1.1 cgd
175 1.14.16.1 christos static int
176 1.14.16.1 christos strcmp_type(const void *e1, const void *e2)
177 1.14.16.1 christos {
178 1.14.16.1 christos return (strcmp(e1, *(const char * const *)e2));
179 1.14.16.1 christos }
180 1.1 cgd
181 1.1 cgd int
182 1.14.16.1 christos lexi(struct parser_state *state)
183 1.1 cgd {
184 1.14.16.1 christos int unary_delim; /* this is set to 1 if the current token
185 1.1 cgd * forces a following operator to be unary */
186 1.14.16.1 christos int code; /* internal code to be returned */
187 1.14.16.1 christos char qchar; /* the delimiter character for a string */
188 1.6 lukem
189 1.14.16.1 christos e_token = s_token; /* point to start of place to save token */
190 1.14.16.1 christos unary_delim = false;
191 1.14.16.1 christos state->col_1 = state->last_nl; /* tell world that this token started
192 1.14.16.1 christos * in column 1 iff the last thing
193 1.14.16.1 christos * scanned was a newline */
194 1.14.16.1 christos state->last_nl = false;
195 1.14.16.1 christos
196 1.14.16.1 christos while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
197 1.14.16.1 christos state->col_1 = false; /* leading blanks imply token is not in column
198 1.14.16.1 christos * 1 */
199 1.14.16.1 christos if (++buf_ptr >= buf_end)
200 1.14.16.1 christos fill_buffer();
201 1.14.16.1 christos }
202 1.1 cgd
203 1.14.16.1 christos /* Scan an alphanumeric token */
204 1.14.16.1 christos if (isalnum((unsigned char)*buf_ptr) ||
205 1.14.16.1 christos *buf_ptr == '_' || *buf_ptr == '$' ||
206 1.14.16.1 christos (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
207 1.14.16.1 christos /*
208 1.14.16.1 christos * we have a character or number
209 1.14.16.1 christos */
210 1.14.16.1 christos struct templ *p;
211 1.1 cgd
212 1.14.16.1 christos if (isdigit((unsigned char)*buf_ptr) ||
213 1.8 christos (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
214 1.14.16.1 christos char s;
215 1.14.16.1 christos unsigned char i;
216 1.14.16.1 christos
217 1.14.16.1 christos for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
218 1.14.16.1 christos i = (unsigned char)*buf_ptr;
219 1.14.16.1 christos if (i >= nitems(table) || table[i] == NULL ||
220 1.14.16.1 christos table[i][s - 'A'] == ' ') {
221 1.14.16.1 christos s = table[0][s - 'A'];
222 1.14.16.1 christos break;
223 1.14.16.1 christos }
224 1.14.16.1 christos s = table[i][s - 'A'];
225 1.14.16.1 christos CHECK_SIZE_TOKEN(1);
226 1.14.16.1 christos *e_token++ = *buf_ptr++;
227 1.14.16.1 christos if (buf_ptr >= buf_end)
228 1.14.16.1 christos fill_buffer();
229 1.14.16.1 christos }
230 1.14.16.1 christos /* s now indicates the type: f(loating), i(integer), u(nknown) */
231 1.14.16.1 christos }
232 1.14.16.1 christos else
233 1.14.16.1 christos while (isalnum((unsigned char)*buf_ptr) ||
234 1.14.16.1 christos *buf_ptr == BACKSLASH ||
235 1.14.16.1 christos *buf_ptr == '_' || *buf_ptr == '$') {
236 1.14.16.1 christos /* fill_buffer() terminates buffer with newline */
237 1.14.16.1 christos if (*buf_ptr == BACKSLASH) {
238 1.14.16.1 christos if (*(buf_ptr + 1) == '\n') {
239 1.14.16.1 christos buf_ptr += 2;
240 1.14.16.1 christos if (buf_ptr >= buf_end)
241 1.14.16.1 christos fill_buffer();
242 1.14.16.1 christos } else
243 1.14.16.1 christos break;
244 1.14.16.1 christos }
245 1.14.16.1 christos CHECK_SIZE_TOKEN(1);
246 1.14.16.1 christos /* copy it over */
247 1.14.16.1 christos *e_token++ = *buf_ptr++;
248 1.14.16.1 christos if (buf_ptr >= buf_end)
249 1.14.16.1 christos fill_buffer();
250 1.14.16.1 christos }
251 1.6 lukem *e_token = '\0';
252 1.14.16.1 christos
253 1.14.16.1 christos if (s_token[0] == 'L' && s_token[1] == '\0' &&
254 1.14.16.1 christos (*buf_ptr == '"' || *buf_ptr == '\''))
255 1.14.16.1 christos return (strpfx);
256 1.14.16.1 christos
257 1.14.16.1 christos while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
258 1.14.16.1 christos if (++buf_ptr >= buf_end)
259 1.6 lukem fill_buffer();
260 1.14.16.1 christos }
261 1.14.16.1 christos state->keyword = 0;
262 1.14.16.1 christos if (state->last_token == structure && !state->p_l_follow) {
263 1.14.16.1 christos /* if last token was 'struct' and we're not
264 1.14.16.1 christos * in parentheses, then this token
265 1.14.16.1 christos * should be treated as a declaration */
266 1.14.16.1 christos state->last_u_d = true;
267 1.14.16.1 christos return (decl);
268 1.14.16.1 christos }
269 1.14.16.1 christos /*
270 1.14.16.1 christos * Operator after identifier is binary unless last token was 'struct'
271 1.14.16.1 christos */
272 1.14.16.1 christos state->last_u_d = (state->last_token == structure);
273 1.14.16.1 christos
274 1.14.16.1 christos p = bsearch(s_token,
275 1.14.16.1 christos specials,
276 1.14.16.1 christos sizeof(specials) / sizeof(specials[0]),
277 1.14.16.1 christos sizeof(specials[0]),
278 1.14.16.1 christos strcmp_type);
279 1.14.16.1 christos if (p == NULL) { /* not a special keyword... */
280 1.14.16.1 christos char *u;
281 1.14.16.1 christos
282 1.14.16.1 christos /* ... so maybe a type_t or a typedef */
283 1.14.16.1 christos if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
284 1.14.16.1 christos strcmp(u, "_t") == 0) || (typename_top >= 0 &&
285 1.14.16.1 christos bsearch(s_token, typenames, typename_top + 1,
286 1.14.16.1 christos sizeof(typenames[0]), strcmp_type))) {
287 1.14.16.1 christos state->keyword = 4; /* a type name */
288 1.14.16.1 christos state->last_u_d = true;
289 1.14.16.1 christos goto found_typename;
290 1.14.16.1 christos }
291 1.14.16.1 christos } else { /* we have a keyword */
292 1.14.16.1 christos state->keyword = p->rwcode;
293 1.14.16.1 christos state->last_u_d = true;
294 1.14.16.1 christos switch (p->rwcode) {
295 1.14.16.1 christos case 7: /* it is a switch */
296 1.14.16.1 christos return (swstmt);
297 1.14.16.1 christos case 8: /* a case or default */
298 1.14.16.1 christos return (casestmt);
299 1.14.16.1 christos
300 1.14.16.1 christos case 3: /* a "struct" */
301 1.14.16.1 christos /* FALLTHROUGH */
302 1.14.16.1 christos case 4: /* one of the declaration keywords */
303 1.14.16.1 christos found_typename:
304 1.14.16.1 christos if (state->p_l_follow) {
305 1.14.16.1 christos /* inside parens: cast, param list, offsetof or sizeof */
306 1.14.16.1 christos state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
307 1.14.16.1 christos }
308 1.14.16.1 christos if (state->last_token == period || state->last_token == unary_op) {
309 1.14.16.1 christos state->keyword = 0;
310 1.14.16.1 christos break;
311 1.14.16.1 christos }
312 1.14.16.1 christos if (p != NULL && p->rwcode == 3)
313 1.14.16.1 christos return (structure);
314 1.14.16.1 christos if (state->p_l_follow)
315 1.14.16.1 christos break;
316 1.14.16.1 christos return (decl);
317 1.14.16.1 christos
318 1.14.16.1 christos case 5: /* if, while, for */
319 1.14.16.1 christos return (sp_paren);
320 1.14.16.1 christos
321 1.14.16.1 christos case 6: /* do, else */
322 1.14.16.1 christos return (sp_nparen);
323 1.14.16.1 christos
324 1.14.16.1 christos case 10: /* storage class specifier */
325 1.14.16.1 christos return (storage);
326 1.14.16.1 christos
327 1.14.16.1 christos case 11: /* typedef */
328 1.14.16.1 christos return (type_def);
329 1.14.16.1 christos
330 1.14.16.1 christos default: /* all others are treated like any other
331 1.14.16.1 christos * identifier */
332 1.14.16.1 christos return (ident);
333 1.14.16.1 christos } /* end of switch */
334 1.14.16.1 christos } /* end of if (found_it) */
335 1.14.16.1 christos if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
336 1.14.16.1 christos state->in_parameter_declaration == 0 && state->block_init == 0) {
337 1.14.16.1 christos char *tp = buf_ptr;
338 1.14.16.1 christos while (tp < buf_end)
339 1.14.16.1 christos if (*tp++ == ')' && (*tp == ';' || *tp == ','))
340 1.14.16.1 christos goto not_proc;
341 1.14.16.1 christos strncpy(state->procname, token, sizeof state->procname - 1);
342 1.14.16.1 christos if (state->in_decl)
343 1.14.16.1 christos state->in_parameter_declaration = 1;
344 1.14.16.1 christos return (funcname);
345 1.14.16.1 christos not_proc:;
346 1.14.16.1 christos }
347 1.14.16.1 christos /*
348 1.14.16.1 christos * The following hack attempts to guess whether or not the current
349 1.14.16.1 christos * token is in fact a declaration keyword -- one that has been
350 1.14.16.1 christos * typedefd
351 1.14.16.1 christos */
352 1.14.16.1 christos else if (!state->p_l_follow && !state->block_init &&
353 1.14.16.1 christos !state->in_stmt &&
354 1.14.16.1 christos ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
355 1.14.16.1 christos isalpha((unsigned char)*buf_ptr)) &&
356 1.14.16.1 christos (state->last_token == semicolon || state->last_token == lbrace ||
357 1.14.16.1 christos state->last_token == rbrace)) {
358 1.14.16.1 christos state->keyword = 4; /* a type name */
359 1.14.16.1 christos state->last_u_d = true;
360 1.14.16.1 christos return decl;
361 1.14.16.1 christos }
362 1.14.16.1 christos if (state->last_token == decl) /* if this is a declared variable,
363 1.14.16.1 christos * then following sign is unary */
364 1.14.16.1 christos state->last_u_d = true; /* will make "int a -1" work */
365 1.14.16.1 christos return (ident); /* the ident is not in the list */
366 1.14.16.1 christos } /* end of procesing for alpanum character */
367 1.6 lukem
368 1.14.16.1 christos /* Scan a non-alphanumeric token */
369 1.14.16.1 christos
370 1.14.16.1 christos CHECK_SIZE_TOKEN(3); /* things like "<<=" */
371 1.14.16.1 christos *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
372 1.14.16.1 christos * moved here */
373 1.14.16.1 christos *e_token = '\0';
374 1.14.16.1 christos if (++buf_ptr >= buf_end)
375 1.14.16.1 christos fill_buffer();
376 1.14.16.1 christos
377 1.14.16.1 christos switch (*token) {
378 1.14.16.1 christos case '\n':
379 1.14.16.1 christos unary_delim = state->last_u_d;
380 1.14.16.1 christos state->last_nl = true; /* remember that we just had a newline */
381 1.14.16.1 christos code = (had_eof ? 0 : newline);
382 1.14.16.1 christos
383 1.14.16.1 christos /*
384 1.14.16.1 christos * if data has been exhausted, the newline is a dummy, and we should
385 1.14.16.1 christos * return code to stop
386 1.14.16.1 christos */
387 1.14.16.1 christos break;
388 1.14.16.1 christos
389 1.14.16.1 christos case '\'': /* start of quoted character */
390 1.14.16.1 christos case '"': /* start of string */
391 1.14.16.1 christos qchar = *token;
392 1.14.16.1 christos do { /* copy the string */
393 1.14.16.1 christos while (1) { /* move one character or [/<char>]<char> */
394 1.14.16.1 christos if (*buf_ptr == '\n') {
395 1.14.16.2 martin diag(1, "Unterminated literal");
396 1.14.16.1 christos goto stop_lit;
397 1.14.16.1 christos }
398 1.14.16.1 christos CHECK_SIZE_TOKEN(2);
399 1.14.16.1 christos *e_token = *buf_ptr++;
400 1.14.16.1 christos if (buf_ptr >= buf_end)
401 1.14.16.1 christos fill_buffer();
402 1.14.16.1 christos if (*e_token == BACKSLASH) { /* if escape, copy extra char */
403 1.14.16.1 christos if (*buf_ptr == '\n') /* check for escaped newline */
404 1.14.16.1 christos ++line_no;
405 1.14.16.1 christos *++e_token = *buf_ptr++;
406 1.14.16.1 christos ++e_token; /* we must increment this again because we
407 1.14.16.1 christos * copied two chars */
408 1.14.16.1 christos if (buf_ptr >= buf_end)
409 1.14.16.1 christos fill_buffer();
410 1.1 cgd }
411 1.14.16.1 christos else
412 1.14.16.1 christos break; /* we copied one character */
413 1.14.16.1 christos } /* end of while (1) */
414 1.14.16.1 christos } while (*e_token++ != qchar);
415 1.1 cgd stop_lit:
416 1.14.16.1 christos code = ident;
417 1.14.16.1 christos break;
418 1.6 lukem
419 1.14.16.1 christos case ('('):
420 1.14.16.1 christos case ('['):
421 1.14.16.1 christos unary_delim = true;
422 1.14.16.1 christos code = lparen;
423 1.14.16.1 christos break;
424 1.14.16.1 christos
425 1.14.16.1 christos case (')'):
426 1.14.16.1 christos case (']'):
427 1.14.16.1 christos code = rparen;
428 1.14.16.1 christos break;
429 1.14.16.1 christos
430 1.14.16.1 christos case '#':
431 1.14.16.1 christos unary_delim = state->last_u_d;
432 1.14.16.1 christos code = preesc;
433 1.14.16.1 christos break;
434 1.14.16.1 christos
435 1.14.16.1 christos case '?':
436 1.14.16.1 christos unary_delim = true;
437 1.14.16.1 christos code = question;
438 1.14.16.1 christos break;
439 1.14.16.1 christos
440 1.14.16.1 christos case (':'):
441 1.14.16.1 christos code = colon;
442 1.14.16.1 christos unary_delim = true;
443 1.14.16.1 christos break;
444 1.14.16.1 christos
445 1.14.16.1 christos case (';'):
446 1.14.16.1 christos unary_delim = true;
447 1.14.16.1 christos code = semicolon;
448 1.14.16.1 christos break;
449 1.14.16.1 christos
450 1.14.16.1 christos case ('{'):
451 1.14.16.1 christos unary_delim = true;
452 1.14.16.1 christos
453 1.14.16.1 christos /*
454 1.14.16.1 christos * if (state->in_or_st) state->block_init = 1;
455 1.14.16.1 christos */
456 1.14.16.1 christos /* ? code = state->block_init ? lparen : lbrace; */
457 1.14.16.1 christos code = lbrace;
458 1.14.16.1 christos break;
459 1.14.16.1 christos
460 1.14.16.1 christos case ('}'):
461 1.14.16.1 christos unary_delim = true;
462 1.14.16.1 christos /* ? code = state->block_init ? rparen : rbrace; */
463 1.14.16.1 christos code = rbrace;
464 1.14.16.1 christos break;
465 1.14.16.1 christos
466 1.14.16.1 christos case 014: /* a form feed */
467 1.14.16.1 christos unary_delim = state->last_u_d;
468 1.14.16.1 christos state->last_nl = true; /* remember this so we can set 'state->col_1'
469 1.14.16.1 christos * right */
470 1.14.16.1 christos code = form_feed;
471 1.14.16.1 christos break;
472 1.14.16.1 christos
473 1.14.16.1 christos case (','):
474 1.14.16.1 christos unary_delim = true;
475 1.14.16.1 christos code = comma;
476 1.14.16.1 christos break;
477 1.1 cgd
478 1.14.16.1 christos case '.':
479 1.14.16.1 christos unary_delim = false;
480 1.14.16.1 christos code = period;
481 1.14.16.1 christos break;
482 1.6 lukem
483 1.14.16.1 christos case '-':
484 1.14.16.1 christos case '+': /* check for -, +, --, ++ */
485 1.14.16.1 christos code = (state->last_u_d ? unary_op : binary_op);
486 1.14.16.1 christos unary_delim = true;
487 1.14.16.1 christos
488 1.14.16.1 christos if (*buf_ptr == token[0]) {
489 1.14.16.1 christos /* check for doubled character */
490 1.14.16.1 christos *e_token++ = *buf_ptr++;
491 1.14.16.1 christos /* buffer overflow will be checked at end of loop */
492 1.14.16.1 christos if (state->last_token == ident || state->last_token == rparen) {
493 1.14.16.1 christos code = (state->last_u_d ? unary_op : postop);
494 1.14.16.1 christos /* check for following ++ or -- */
495 1.14.16.1 christos unary_delim = false;
496 1.14.16.1 christos }
497 1.14.16.1 christos }
498 1.14.16.1 christos else if (*buf_ptr == '=')
499 1.14.16.1 christos /* check for operator += */
500 1.14.16.1 christos *e_token++ = *buf_ptr++;
501 1.14.16.1 christos else if (*buf_ptr == '>') {
502 1.14.16.1 christos /* check for operator -> */
503 1.14.16.1 christos *e_token++ = *buf_ptr++;
504 1.14.16.1 christos unary_delim = false;
505 1.14.16.1 christos code = unary_op;
506 1.14.16.1 christos state->want_blank = false;
507 1.14.16.1 christos }
508 1.14.16.1 christos break; /* buffer overflow will be checked at end of
509 1.1 cgd * switch */
510 1.1 cgd
511 1.14.16.1 christos case '=':
512 1.14.16.1 christos if (state->in_or_st)
513 1.14.16.1 christos state->block_init = 1;
514 1.14.16.1 christos if (*buf_ptr == '=') {/* == */
515 1.14.16.1 christos *e_token++ = '='; /* Flip =+ to += */
516 1.14.16.1 christos buf_ptr++;
517 1.14.16.1 christos *e_token = 0;
518 1.14.16.1 christos }
519 1.14.16.1 christos code = binary_op;
520 1.14.16.1 christos unary_delim = true;
521 1.14.16.1 christos break;
522 1.14.16.1 christos /* can drop thru!!! */
523 1.14.16.1 christos
524 1.14.16.1 christos case '>':
525 1.14.16.1 christos case '<':
526 1.14.16.1 christos case '!': /* ops like <, <<, <=, !=, etc */
527 1.14.16.1 christos if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
528 1.14.16.1 christos *e_token++ = *buf_ptr;
529 1.14.16.1 christos if (++buf_ptr >= buf_end)
530 1.14.16.1 christos fill_buffer();
531 1.14.16.1 christos }
532 1.14.16.1 christos if (*buf_ptr == '=')
533 1.14.16.1 christos *e_token++ = *buf_ptr++;
534 1.14.16.1 christos code = (state->last_u_d ? unary_op : binary_op);
535 1.14.16.1 christos unary_delim = true;
536 1.14.16.1 christos break;
537 1.14.16.1 christos
538 1.14.16.1 christos case '*':
539 1.14.16.1 christos unary_delim = true;
540 1.14.16.1 christos if (!state->last_u_d) {
541 1.14.16.1 christos if (*buf_ptr == '=')
542 1.14.16.1 christos *e_token++ = *buf_ptr++;
543 1.14.16.1 christos code = binary_op;
544 1.14.16.1 christos break;
545 1.14.16.1 christos }
546 1.14.16.1 christos while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
547 1.14.16.1 christos if (*buf_ptr == '*') {
548 1.14.16.1 christos CHECK_SIZE_TOKEN(1);
549 1.14.16.1 christos *e_token++ = *buf_ptr;
550 1.14.16.1 christos }
551 1.14.16.1 christos if (++buf_ptr >= buf_end)
552 1.14.16.1 christos fill_buffer();
553 1.14.16.1 christos }
554 1.14.16.1 christos if (ps.in_decl) {
555 1.14.16.1 christos char *tp = buf_ptr;
556 1.1 cgd
557 1.14.16.1 christos while (isalpha((unsigned char)*tp) ||
558 1.14.16.1 christos isspace((unsigned char)*tp)) {
559 1.14.16.1 christos if (++tp >= buf_end)
560 1.14.16.1 christos fill_buffer();
561 1.14.16.1 christos }
562 1.14.16.1 christos if (*tp == '(')
563 1.14.16.1 christos ps.procname[0] = ' ';
564 1.14.16.1 christos }
565 1.14.16.1 christos code = unary_op;
566 1.14.16.1 christos break;
567 1.14.16.1 christos
568 1.14.16.1 christos default:
569 1.14.16.1 christos if (token[0] == '/' && *buf_ptr == '*') {
570 1.14.16.1 christos /* it is start of comment */
571 1.14.16.1 christos *e_token++ = '*';
572 1.14.16.1 christos
573 1.14.16.1 christos if (++buf_ptr >= buf_end)
574 1.14.16.1 christos fill_buffer();
575 1.1 cgd
576 1.14.16.1 christos code = comment;
577 1.14.16.1 christos unary_delim = state->last_u_d;
578 1.14.16.1 christos break;
579 1.1 cgd }
580 1.14.16.1 christos while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
581 1.14.16.1 christos /*
582 1.14.16.1 christos * handle ||, &&, etc, and also things as in int *****i
583 1.14.16.1 christos */
584 1.14.16.1 christos CHECK_SIZE_TOKEN(1);
585 1.14.16.1 christos *e_token++ = *buf_ptr;
586 1.14.16.1 christos if (++buf_ptr >= buf_end)
587 1.1 cgd fill_buffer();
588 1.14.16.1 christos }
589 1.14.16.1 christos code = (state->last_u_d ? unary_op : binary_op);
590 1.14.16.1 christos unary_delim = true;
591 1.14.16.1 christos
592 1.14.16.1 christos
593 1.14.16.1 christos } /* end of switch */
594 1.14.16.1 christos if (buf_ptr >= buf_end) /* check for input buffer empty */
595 1.14.16.1 christos fill_buffer();
596 1.14.16.1 christos state->last_u_d = unary_delim;
597 1.14.16.1 christos CHECK_SIZE_TOKEN(1);
598 1.14.16.1 christos *e_token = '\0'; /* null terminate the token */
599 1.14.16.1 christos return (code);
600 1.1 cgd }
601 1.14.16.1 christos
602 1.14.16.1 christos /* Initialize constant transition table */
603 1.6 lukem void
604 1.14.16.1 christos init_constant_tt(void)
605 1.1 cgd {
606 1.14.16.1 christos table['-'] = table['+'];
607 1.14.16.1 christos table['8'] = table['9'];
608 1.14.16.1 christos table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
609 1.14.16.1 christos table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
610 1.14.16.1 christos table['B'] = table['b'];
611 1.14.16.1 christos table['E'] = table['e'];
612 1.14.16.1 christos table['U'] = table['u'];
613 1.14.16.1 christos table['X'] = table['x'];
614 1.14.16.1 christos table['P'] = table['p'];
615 1.14.16.1 christos table['F'] = table['f'];
616 1.14.16.1 christos }
617 1.14.16.1 christos
618 1.14.16.1 christos void
619 1.14.16.1 christos alloc_typenames(void)
620 1.14.16.1 christos {
621 1.14.16.1 christos
622 1.14.16.1 christos typenames = (const char **)malloc(sizeof(typenames[0]) *
623 1.14.16.1 christos (typename_count = 16));
624 1.14.16.1 christos if (typenames == NULL)
625 1.14.16.1 christos err(1, NULL);
626 1.14.16.1 christos }
627 1.14.16.1 christos
628 1.14.16.1 christos void
629 1.14.16.1 christos add_typename(const char *key)
630 1.14.16.1 christos {
631 1.14.16.1 christos int comparison;
632 1.14.16.1 christos const char *copy;
633 1.14.16.1 christos
634 1.14.16.1 christos if (typename_top + 1 >= typename_count) {
635 1.14.16.1 christos typenames = realloc((void *)typenames,
636 1.14.16.1 christos sizeof(typenames[0]) * (typename_count *= 2));
637 1.14.16.1 christos if (typenames == NULL)
638 1.14.16.1 christos err(1, NULL);
639 1.14.16.1 christos }
640 1.14.16.1 christos if (typename_top == -1)
641 1.14.16.1 christos typenames[++typename_top] = copy = strdup(key);
642 1.14.16.1 christos else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
643 1.14.16.1 christos /* take advantage of sorted input */
644 1.14.16.1 christos if (comparison == 0) /* remove duplicates */
645 1.14.16.1 christos return;
646 1.14.16.1 christos typenames[++typename_top] = copy = strdup(key);
647 1.14.16.1 christos }
648 1.14.16.1 christos else {
649 1.14.16.1 christos int p;
650 1.14.16.1 christos
651 1.14.16.1 christos for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
652 1.14.16.1 christos /* find place for the new key */;
653 1.14.16.1 christos if (comparison == 0) /* remove duplicates */
654 1.14.16.1 christos return;
655 1.14.16.1 christos memmove(&typenames[p + 1], &typenames[p],
656 1.14.16.1 christos sizeof(typenames[0]) * (++typename_top - p));
657 1.14.16.1 christos typenames[p] = copy = strdup(key);
658 1.14.16.1 christos }
659 1.14.16.1 christos
660 1.14.16.1 christos if (copy == NULL)
661 1.14.16.1 christos err(1, NULL);
662 1.1 cgd }
663