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
39 * Here we have the token scanner for indent. It scans off one token and puts
40 * it in the global variable "token". It returns a code, indicating the type
49 #include <sys/param.h>
51 #include "indent_globs.h"
52 #include "indent_codes.h"
61 * This table has to be sorted alphabetically, because it'll be used in binary
62 * search. For the same reason, string must be the first thing in struct templ.
64 struct templ specials[] =
110 const char **typenames;
112 int typename_top = -1;
115 * The transition table below was rewritten by hand from lx's output, given
116 * the following definitions. lx is Katherine Flavel's lexer generator.
118 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
119 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
120 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
121 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
123 * D+ E FS? -> $float;
124 * D* "." D+ E? FS? -> $float;
125 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
126 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
127 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
128 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
130 static char const *table[] = {
136 r 11ee0001101lbuuxx.a.pp
137 t.01.e+008bLuxll0Ll.aa.p+0
138 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
139 ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
140 ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
141 ['7'] = "DEIDEHHHIJ U VUVVZZZ",
142 ['9'] = "DEJDEHHHJJ U VUVVZZZ",
144 ['b'] = " K U VUVV ",
145 ['e'] = " FFF FF U VUVV ",
146 ['f'] = " f f U VUVV f",
147 ['u'] = " MM M i iiM M ",
150 ['L'] = " LLf fL PR Li L f",
151 ['l'] = " OOf fO S P O i O f",
153 ['.'] = "B EE EE T W ",
154 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
155 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
159 strcmp_type(const void *e1, const void *e2)
161 return (strcmp(e1, *(const char * const *)e2));
165 lexi(struct parser_state *state)
167 int unary_delim; /* this is set to 1 if the current token
168 * forces a following operator to be unary */
169 int code; /* internal code to be returned */
170 char qchar; /* the delimiter character for a string */
172 e_token = s_token; /* point to start of place to save token */
174 state->col_1 = state->last_nl; /* tell world that this token started
175 * in column 1 iff the last thing
176 * scanned was a newline */
177 state->last_nl = false;
179 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
180 state->col_1 = false; /* leading blanks imply token is not in column
182 if (++buf_ptr >= buf_end)
186 /* Scan an alphanumeric token */
187 if (isalnum((unsigned char)*buf_ptr) ||
188 *buf_ptr == '_' || *buf_ptr == '$' ||
189 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
191 * we have a character or number
195 if (isdigit((unsigned char)*buf_ptr) ||
196 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
200 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
201 i = (unsigned char)*buf_ptr;
202 if (i >= nitems(table) || table[i] == NULL ||
203 table[i][s - 'A'] == ' ') {
204 s = table[0][s - 'A'];
207 s = table[i][s - 'A'];
209 *e_token++ = *buf_ptr++;
210 if (buf_ptr >= buf_end)
213 /* s now indicates the type: f(loating), i(integer), u(nknown) */
216 while (isalnum((unsigned char)*buf_ptr) ||
217 *buf_ptr == BACKSLASH ||
218 *buf_ptr == '_' || *buf_ptr == '$') {
219 /* fill_buffer() terminates buffer with newline */
220 if (*buf_ptr == BACKSLASH) {
221 if (*(buf_ptr + 1) == '\n') {
223 if (buf_ptr >= buf_end)
230 *e_token++ = *buf_ptr++;
231 if (buf_ptr >= buf_end)
236 if (s_token[0] == 'L' && s_token[1] == '\0' &&
237 (*buf_ptr == '"' || *buf_ptr == '\''))
240 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
241 if (++buf_ptr >= buf_end)
245 if (state->last_token == structure && !state->p_l_follow) {
246 /* if last token was 'struct' and we're not
247 * in parentheses, then this token
248 * should be treated as a declaration */
249 state->last_u_d = true;
253 * Operator after identifier is binary unless last token was 'struct'
255 state->last_u_d = (state->last_token == structure);
259 sizeof(specials) / sizeof(specials[0]),
262 if (p == NULL) { /* not a special keyword... */
265 /* ... so maybe a type_t or a typedef */
266 if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
267 strcmp(u, "_t") == 0) || (typename_top >= 0 &&
268 bsearch(s_token, typenames, typename_top + 1,
269 sizeof(typenames[0]), strcmp_type))) {
270 state->keyword = 4; /* a type name */
271 state->last_u_d = true;
274 } else { /* we have a keyword */
275 state->keyword = p->rwcode;
276 state->last_u_d = true;
278 case 7: /* it is a switch */
280 case 8: /* a case or default */
283 case 3: /* a "struct" */
285 case 4: /* one of the declaration keywords */
287 if (state->p_l_follow) {
288 /* inside parens: cast, param list, offsetof or sizeof */
289 state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
291 if (state->last_token == period || state->last_token == unary_op) {
295 if (p != NULL && p->rwcode == 3)
297 if (state->p_l_follow)
301 case 5: /* if, while, for */
304 case 6: /* do, else */
307 case 10: /* storage class specifier */
310 case 11: /* typedef */
313 default: /* all others are treated like any other
316 } /* end of switch */
317 } /* end of if (found_it) */
318 if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
319 state->in_parameter_declaration == 0 && state->block_init == 0) {
322 if (*tp++ == ')' && (*tp == ';' || *tp == ','))
324 strncpy(state->procname, token, sizeof state->procname - 1);
326 state->in_parameter_declaration = 1;
331 * The following hack attempts to guess whether or not the current
332 * token is in fact a declaration keyword -- one that has been
335 else if (!state->p_l_follow && !state->block_init &&
337 ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
338 isalpha((unsigned char)*buf_ptr)) &&
339 (state->last_token == semicolon || state->last_token == lbrace ||
340 state->last_token == rbrace)) {
341 state->keyword = 4; /* a type name */
342 state->last_u_d = true;
345 if (state->last_token == decl) /* if this is a declared variable,
346 * then following sign is unary */
347 state->last_u_d = true; /* will make "int a -1" work */
348 return (ident); /* the ident is not in the list */
349 } /* end of processing for alpanum character */
351 /* Scan a non-alphanumeric token */
353 CHECK_SIZE_TOKEN(3); /* things like "<<=" */
354 *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
357 if (++buf_ptr >= buf_end)
362 unary_delim = state->last_u_d;
363 state->last_nl = true; /* remember that we just had a newline */
364 code = (had_eof ? 0 : newline);
367 * if data has been exhausted, the newline is a dummy, and we should
368 * return code to stop
372 case '\'': /* start of quoted character */
373 case '"': /* start of string */
375 do { /* copy the string */
376 while (1) { /* move one character or [/<char>]<char> */
377 if (*buf_ptr == '\n') {
378 diag2(1, "Unterminated literal");
382 *e_token = *buf_ptr++;
383 if (buf_ptr >= buf_end)
385 if (*e_token == BACKSLASH) { /* if escape, copy extra char */
386 if (*buf_ptr == '\n') /* check for escaped newline */
388 *++e_token = *buf_ptr++;
389 ++e_token; /* we must increment this again because we
390 * copied two chars */
391 if (buf_ptr >= buf_end)
395 break; /* we copied one character */
396 } /* end of while (1) */
397 } while (*e_token++ != qchar);
414 unary_delim = state->last_u_d;
437 * if (state->in_or_st) state->block_init = 1;
439 /* ? code = state->block_init ? lparen : lbrace; */
445 /* ? code = state->block_init ? rparen : rbrace; */
449 case 014: /* a form feed */
450 unary_delim = state->last_u_d;
451 state->last_nl = true; /* remember this so we can set 'state->col_1'
467 case '+': /* check for -, +, --, ++ */
468 code = (state->last_u_d ? unary_op : binary_op);
471 if (*buf_ptr == token[0]) {
472 /* check for doubled character */
473 *e_token++ = *buf_ptr++;
474 /* buffer overflow will be checked at end of loop */
475 if (state->last_token == ident || state->last_token == rparen) {
476 code = (state->last_u_d ? unary_op : postop);
477 /* check for following ++ or -- */
481 else if (*buf_ptr == '=')
482 /* check for operator += */
483 *e_token++ = *buf_ptr++;
484 else if (*buf_ptr == '>') {
485 /* check for operator -> */
486 *e_token++ = *buf_ptr++;
487 if (!opt.pointer_as_binop) {
490 state->want_blank = false;
493 break; /* buffer overflow will be checked at end of
498 state->block_init = 1;
499 if (*buf_ptr == '=') {/* == */
500 *e_token++ = '='; /* Flip =+ to += */
507 /* can drop thru!!! */
511 case '!': /* ops like <, <<, <=, !=, etc */
512 if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
513 *e_token++ = *buf_ptr;
514 if (++buf_ptr >= buf_end)
518 *e_token++ = *buf_ptr++;
519 code = (state->last_u_d ? unary_op : binary_op);
525 if (!state->last_u_d) {
527 *e_token++ = *buf_ptr++;
531 while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
532 if (*buf_ptr == '*') {
534 *e_token++ = *buf_ptr;
536 if (++buf_ptr >= buf_end)
542 while (isalpha((unsigned char)*tp) ||
543 isspace((unsigned char)*tp)) {
548 ps.procname[0] = ' ';
554 if (token[0] == '/' && *buf_ptr == '*') {
555 /* it is start of comment */
558 if (++buf_ptr >= buf_end)
562 unary_delim = state->last_u_d;
565 while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
567 * handle ||, &&, etc, and also things as in int *****i
570 *e_token++ = *buf_ptr;
571 if (++buf_ptr >= buf_end)
574 code = (state->last_u_d ? unary_op : binary_op);
578 } /* end of switch */
579 if (buf_ptr >= buf_end) /* check for input buffer empty */
581 state->last_u_d = unary_delim;
583 *e_token = '\0'; /* null terminate the token */
587 /* Initialize constant transition table */
589 init_constant_tt(void)
591 table['-'] = table['+'];
592 table['8'] = table['9'];
593 table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
594 table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
595 table['B'] = table['b'];
596 table['E'] = table['e'];
597 table['U'] = table['u'];
598 table['X'] = table['x'];
599 table['P'] = table['p'];
600 table['F'] = table['f'];
604 alloc_typenames(void)
607 typenames = (const char **)malloc(sizeof(typenames[0]) *
608 (typename_count = 16));
609 if (typenames == NULL)
614 add_typename(const char *key)
619 if (typename_top + 1 >= typename_count) {
620 typenames = realloc((void *)typenames,
621 sizeof(typenames[0]) * (typename_count *= 2));
622 if (typenames == NULL)
625 if (typename_top == -1)
626 typenames[++typename_top] = copy = strdup(key);
627 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
628 /* take advantage of sorted input */
629 if (comparison == 0) /* remove duplicates */
631 typenames[++typename_top] = copy = strdup(key);
636 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
637 /* find place for the new key */;
638 if (comparison == 0) /* remove duplicates */
640 memmove(&typenames[p + 1], &typenames[p],
641 sizeof(typenames[0]) * (++typename_top - p));
642 typenames[p] = copy = strdup(key);