2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1985 Sun Microsystems, Inc.
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
45 * Here we have the token scanner for indent. It scans off one token and puts
46 * it in the global variable "token". It returns a code, indicating the type
55 #include <sys/param.h>
57 #include "indent_globs.h"
58 #include "indent_codes.h"
67 * This table has to be sorted alphabetically, because it'll be used in binary
68 * search. For the same reason, string must be the first thing in struct templ.
70 struct templ specials[] =
116 const char **typenames;
118 int typename_top = -1;
121 * The transition table below was rewritten by hand from lx's output, given
122 * the following definitions. lx is Katherine Flavel's lexer generator.
124 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
125 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
126 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
127 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
129 * D+ E FS? -> $float;
130 * D* "." D+ E? FS? -> $float;
131 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
132 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
133 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
134 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
136 static char const *table[] = {
142 r 11ee0001101lbuuxx.a.pp
143 t.01.e+008bLuxll0Ll.aa.p+0
144 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
145 ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
146 ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
147 ['7'] = "DEIDEHHHIJ U VUVVZZZ",
148 ['9'] = "DEJDEHHHJJ U VUVVZZZ",
150 ['b'] = " K U VUVV ",
151 ['e'] = " FFF FF U VUVV ",
152 ['f'] = " f f U VUVV f",
153 ['u'] = " MM M i iiM M ",
156 ['L'] = " LLf fL PR Li L f",
157 ['l'] = " OOf fO S P O i O f",
159 ['.'] = "B EE EE T W ",
160 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
161 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
165 strcmp_type(const void *e1, const void *e2)
167 return (strcmp(e1, *(const char * const *)e2));
171 lexi(struct parser_state *state)
173 int unary_delim; /* this is set to 1 if the current token
174 * forces a following operator to be unary */
175 int code; /* internal code to be returned */
176 char qchar; /* the delimiter character for a string */
178 e_token = s_token; /* point to start of place to save token */
180 state->col_1 = state->last_nl; /* tell world that this token started
181 * in column 1 iff the last thing
182 * scanned was a newline */
183 state->last_nl = false;
185 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
186 state->col_1 = false; /* leading blanks imply token is not in column
188 if (++buf_ptr >= buf_end)
192 /* Scan an alphanumeric token */
193 if (isalnum((unsigned char)*buf_ptr) ||
194 *buf_ptr == '_' || *buf_ptr == '$' ||
195 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
197 * we have a character or number
201 if (isdigit((unsigned char)*buf_ptr) ||
202 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
206 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
207 i = (unsigned char)*buf_ptr;
208 if (i >= nitems(table) || table[i] == NULL ||
209 table[i][s - 'A'] == ' ') {
210 s = table[0][s - 'A'];
213 s = table[i][s - 'A'];
215 *e_token++ = *buf_ptr++;
216 if (buf_ptr >= buf_end)
219 /* s now indicates the type: f(loating), i(integer), u(nknown) */
222 while (isalnum((unsigned char)*buf_ptr) ||
223 *buf_ptr == BACKSLASH ||
224 *buf_ptr == '_' || *buf_ptr == '$') {
225 /* fill_buffer() terminates buffer with newline */
226 if (*buf_ptr == BACKSLASH) {
227 if (*(buf_ptr + 1) == '\n') {
229 if (buf_ptr >= buf_end)
236 *e_token++ = *buf_ptr++;
237 if (buf_ptr >= buf_end)
242 if (s_token[0] == 'L' && s_token[1] == '\0' &&
243 (*buf_ptr == '"' || *buf_ptr == '\''))
246 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
247 if (++buf_ptr >= buf_end)
251 if (state->last_token == structure && !state->p_l_follow) {
252 /* if last token was 'struct' and we're not
253 * in parentheses, then this token
254 * should be treated as a declaration */
255 state->last_u_d = true;
259 * Operator after identifier is binary unless last token was 'struct'
261 state->last_u_d = (state->last_token == structure);
265 sizeof(specials) / sizeof(specials[0]),
268 if (p == NULL) { /* not a special keyword... */
271 /* ... so maybe a type_t or a typedef */
272 if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
273 strcmp(u, "_t") == 0) || (typename_top >= 0 &&
274 bsearch(s_token, typenames, typename_top + 1,
275 sizeof(typenames[0]), strcmp_type))) {
276 state->keyword = 4; /* a type name */
277 state->last_u_d = true;
280 } else { /* we have a keyword */
281 state->keyword = p->rwcode;
282 state->last_u_d = true;
284 case 7: /* it is a switch */
286 case 8: /* a case or default */
289 case 3: /* a "struct" */
291 case 4: /* one of the declaration keywords */
293 if (state->p_l_follow) {
294 /* inside parens: cast, param list, offsetof or sizeof */
295 state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
297 if (state->last_token == period || state->last_token == unary_op) {
301 if (p != NULL && p->rwcode == 3)
303 if (state->p_l_follow)
307 case 5: /* if, while, for */
310 case 6: /* do, else */
313 case 10: /* storage class specifier */
316 case 11: /* typedef */
319 default: /* all others are treated like any other
322 } /* end of switch */
323 } /* end of if (found_it) */
324 if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
325 state->in_parameter_declaration == 0 && state->block_init == 0) {
328 if (*tp++ == ')' && (*tp == ';' || *tp == ','))
330 strncpy(state->procname, token, sizeof state->procname - 1);
332 state->in_parameter_declaration = 1;
337 * The following hack attempts to guess whether or not the current
338 * token is in fact a declaration keyword -- one that has been
341 else if (!state->p_l_follow && !state->block_init &&
343 ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
344 isalpha((unsigned char)*buf_ptr)) &&
345 (state->last_token == semicolon || state->last_token == lbrace ||
346 state->last_token == rbrace)) {
347 state->keyword = 4; /* a type name */
348 state->last_u_d = true;
351 if (state->last_token == decl) /* if this is a declared variable,
352 * then following sign is unary */
353 state->last_u_d = true; /* will make "int a -1" work */
354 return (ident); /* the ident is not in the list */
355 } /* end of processing for alpanum character */
357 /* Scan a non-alphanumeric token */
359 CHECK_SIZE_TOKEN(3); /* things like "<<=" */
360 *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
363 if (++buf_ptr >= buf_end)
368 unary_delim = state->last_u_d;
369 state->last_nl = true; /* remember that we just had a newline */
370 code = (had_eof ? 0 : newline);
373 * if data has been exhausted, the newline is a dummy, and we should
374 * return code to stop
378 case '\'': /* start of quoted character */
379 case '"': /* start of string */
381 do { /* copy the string */
382 while (1) { /* move one character or [/<char>]<char> */
383 if (*buf_ptr == '\n') {
384 diag2(1, "Unterminated literal");
388 *e_token = *buf_ptr++;
389 if (buf_ptr >= buf_end)
391 if (*e_token == BACKSLASH) { /* if escape, copy extra char */
392 if (*buf_ptr == '\n') /* check for escaped newline */
394 *++e_token = *buf_ptr++;
395 ++e_token; /* we must increment this again because we
396 * copied two chars */
397 if (buf_ptr >= buf_end)
401 break; /* we copied one character */
402 } /* end of while (1) */
403 } while (*e_token++ != qchar);
420 unary_delim = state->last_u_d;
443 * if (state->in_or_st) state->block_init = 1;
445 /* ? code = state->block_init ? lparen : lbrace; */
451 /* ? code = state->block_init ? rparen : rbrace; */
455 case 014: /* a form feed */
456 unary_delim = state->last_u_d;
457 state->last_nl = true; /* remember this so we can set 'state->col_1'
473 case '+': /* check for -, +, --, ++ */
474 code = (state->last_u_d ? unary_op : binary_op);
477 if (*buf_ptr == token[0]) {
478 /* check for doubled character */
479 *e_token++ = *buf_ptr++;
480 /* buffer overflow will be checked at end of loop */
481 if (state->last_token == ident || state->last_token == rparen) {
482 code = (state->last_u_d ? unary_op : postop);
483 /* check for following ++ or -- */
487 else if (*buf_ptr == '=')
488 /* check for operator += */
489 *e_token++ = *buf_ptr++;
490 else if (*buf_ptr == '>') {
491 /* check for operator -> */
492 *e_token++ = *buf_ptr++;
493 if (!opt.pointer_as_binop) {
496 state->want_blank = false;
499 break; /* buffer overflow will be checked at end of
504 state->block_init = 1;
505 if (*buf_ptr == '=') {/* == */
506 *e_token++ = '='; /* Flip =+ to += */
513 /* can drop thru!!! */
517 case '!': /* ops like <, <<, <=, !=, etc */
518 if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
519 *e_token++ = *buf_ptr;
520 if (++buf_ptr >= buf_end)
524 *e_token++ = *buf_ptr++;
525 code = (state->last_u_d ? unary_op : binary_op);
531 if (!state->last_u_d) {
533 *e_token++ = *buf_ptr++;
537 while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
538 if (*buf_ptr == '*') {
540 *e_token++ = *buf_ptr;
542 if (++buf_ptr >= buf_end)
548 while (isalpha((unsigned char)*tp) ||
549 isspace((unsigned char)*tp)) {
554 ps.procname[0] = ' ';
560 if (token[0] == '/' && *buf_ptr == '*') {
561 /* it is start of comment */
564 if (++buf_ptr >= buf_end)
568 unary_delim = state->last_u_d;
571 while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
573 * handle ||, &&, etc, and also things as in int *****i
576 *e_token++ = *buf_ptr;
577 if (++buf_ptr >= buf_end)
580 code = (state->last_u_d ? unary_op : binary_op);
584 } /* end of switch */
585 if (buf_ptr >= buf_end) /* check for input buffer empty */
587 state->last_u_d = unary_delim;
589 *e_token = '\0'; /* null terminate the token */
593 /* Initialize constant transition table */
595 init_constant_tt(void)
597 table['-'] = table['+'];
598 table['8'] = table['9'];
599 table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
600 table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
601 table['B'] = table['b'];
602 table['E'] = table['e'];
603 table['U'] = table['u'];
604 table['X'] = table['x'];
605 table['P'] = table['p'];
606 table['F'] = table['f'];
610 alloc_typenames(void)
613 typenames = (const char **)malloc(sizeof(typenames[0]) *
614 (typename_count = 16));
615 if (typenames == NULL)
620 add_typename(const char *key)
625 if (typename_top + 1 >= typename_count) {
626 typenames = realloc((void *)typenames,
627 sizeof(typenames[0]) * (typename_count *= 2));
628 if (typenames == NULL)
631 if (typename_top == -1)
632 typenames[++typename_top] = copy = strdup(key);
633 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
634 /* take advantage of sorted input */
635 if (comparison == 0) /* remove duplicates */
637 typenames[++typename_top] = copy = strdup(key);
642 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
643 /* find place for the new key */;
644 if (comparison == 0) /* remove duplicates */
646 memmove(&typenames[p + 1], &typenames[p],
647 sizeof(typenames[0]) * (++typename_top - p));
648 typenames[p] = copy = strdup(key);