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>
44 __FBSDID("$FreeBSD$");
47 * Here we have the token scanner for indent. It scans off one token and puts
48 * it in the global variable "token". It returns a code, indicating the type
57 #include <sys/param.h>
59 #include "indent_globs.h"
60 #include "indent_codes.h"
69 * This table has to be sorted alphabetically, because it'll be used in binary
70 * search. For the same reason, string must be the first thing in struct templ.
72 struct templ specials[] =
118 const char **typenames;
120 int typename_top = -1;
123 * The transition table below was rewritten by hand from lx's output, given
124 * the following definitions. lx is Katherine Flavel's lexer generator.
126 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
127 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
128 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
129 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
131 * D+ E FS? -> $float;
132 * D* "." D+ E? FS? -> $float;
133 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
134 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
135 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
136 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
138 static char const *table[] = {
144 r 11ee0001101lbuuxx.a.pp
145 t.01.e+008bLuxll0Ll.aa.p+0
146 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
147 ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
148 ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
149 ['7'] = "DEIDEHHHIJ U VUVVZZZ",
150 ['9'] = "DEJDEHHHJJ U VUVVZZZ",
152 ['b'] = " K U VUVV ",
153 ['e'] = " FFF FF U VUVV ",
154 ['f'] = " f f U VUVV f",
155 ['u'] = " MM M i iiM M ",
158 ['L'] = " LLf fL PR Li L f",
159 ['l'] = " OOf fO S P O i O f",
161 ['.'] = "B EE EE T W ",
162 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
163 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
167 strcmp_type(const void *e1, const void *e2)
169 return (strcmp(e1, *(const char * const *)e2));
173 lexi(struct parser_state *state)
175 int unary_delim; /* this is set to 1 if the current token
176 * forces a following operator to be unary */
177 int code; /* internal code to be returned */
178 char qchar; /* the delimiter character for a string */
180 e_token = s_token; /* point to start of place to save token */
182 state->col_1 = state->last_nl; /* tell world that this token started
183 * in column 1 iff the last thing
184 * scanned was a newline */
185 state->last_nl = false;
187 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
188 state->col_1 = false; /* leading blanks imply token is not in column
190 if (++buf_ptr >= buf_end)
194 /* Scan an alphanumeric token */
195 if (isalnum((unsigned char)*buf_ptr) ||
196 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
198 * we have a character or number
202 if (isdigit((unsigned char)*buf_ptr) ||
203 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
207 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
208 i = (unsigned char)*buf_ptr;
209 if (i >= nitems(table) || table[i] == NULL ||
210 table[i][s - 'A'] == ' ') {
211 s = table[0][s - 'A'];
214 s = table[i][s - 'A'];
216 *e_token++ = *buf_ptr++;
217 if (buf_ptr >= buf_end)
220 /* s now indicates the type: f(loating), i(integer), u(nknown) */
223 while (isalnum((unsigned char)*buf_ptr) ||
224 *buf_ptr == BACKSLASH ||
226 /* fill_buffer() terminates buffer with newline */
227 if (*buf_ptr == BACKSLASH) {
228 if (*(buf_ptr + 1) == '\n') {
230 if (buf_ptr >= buf_end)
237 *e_token++ = *buf_ptr++;
238 if (buf_ptr >= buf_end)
243 if (s_token[0] == 'L' && s_token[1] == '\0' &&
244 (*buf_ptr == '"' || *buf_ptr == '\''))
247 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
248 if (++buf_ptr >= buf_end)
252 if (state->last_token == structure && !state->p_l_follow) {
253 /* if last token was 'struct' and we're not
254 * in parentheses, then this token
255 * should be treated as a declaration */
256 state->last_u_d = true;
260 * Operator after identifier is binary unless last token was 'struct'
262 state->last_u_d = (state->last_token == structure);
266 sizeof(specials) / sizeof(specials[0]),
269 if (p == NULL) { /* not a special keyword... */
272 /* ... so maybe a type_t or a typedef */
273 if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
274 strcmp(u, "_t") == 0) || (typename_top >= 0 &&
275 bsearch(s_token, typenames, typename_top + 1,
276 sizeof(typenames[0]), strcmp_type))) {
277 state->keyword = 4; /* a type name */
278 state->last_u_d = true;
281 } else { /* we have a keyword */
282 state->keyword = p->rwcode;
283 state->last_u_d = true;
285 case 7: /* it is a switch */
287 case 8: /* a case or default */
290 case 3: /* a "struct" */
292 case 4: /* one of the declaration keywords */
294 if (state->p_l_follow) {
295 /* inside parens: cast, param list, offsetof or sizeof */
296 state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
298 if (state->last_token == period || state->last_token == unary_op) {
302 if (p != NULL && p->rwcode == 3)
304 if (state->p_l_follow)
308 case 5: /* if, while, for */
311 case 6: /* do, else */
314 case 10: /* storage class specifier */
317 case 11: /* typedef */
320 default: /* all others are treated like any other
323 } /* end of switch */
324 } /* end of if (found_it) */
325 if (*buf_ptr == '(' && state->tos <= 1 && state->ind_level == 0 &&
326 state->in_parameter_declaration == 0 && state->block_init == 0) {
329 if (*tp++ == ')' && (*tp == ';' || *tp == ','))
331 strncpy(state->procname, token, sizeof state->procname - 1);
333 state->in_parameter_declaration = 1;
338 * The following hack attempts to guess whether or not the current
339 * token is in fact a declaration keyword -- one that has been
342 else if (!state->p_l_follow && !state->block_init &&
344 ((*buf_ptr == '*' && buf_ptr[1] != '=') ||
345 isalpha((unsigned char)*buf_ptr)) &&
346 (state->last_token == semicolon || state->last_token == lbrace ||
347 state->last_token == rbrace)) {
348 state->keyword = 4; /* a type name */
349 state->last_u_d = true;
352 if (state->last_token == decl) /* if this is a declared variable,
353 * then following sign is unary */
354 state->last_u_d = true; /* will make "int a -1" work */
355 return (ident); /* the ident is not in the list */
356 } /* end of procesing for alpanum character */
358 /* Scan a non-alphanumeric token */
360 CHECK_SIZE_TOKEN(3); /* things like "<<=" */
361 *e_token++ = *buf_ptr; /* if it is only a one-character token, it is
364 if (++buf_ptr >= buf_end)
369 unary_delim = state->last_u_d;
370 state->last_nl = true; /* remember that we just had a newline */
371 code = (had_eof ? 0 : newline);
374 * if data has been exhausted, the newline is a dummy, and we should
375 * return code to stop
379 case '\'': /* start of quoted character */
380 case '"': /* start of string */
382 do { /* copy the string */
383 while (1) { /* move one character or [/<char>]<char> */
384 if (*buf_ptr == '\n') {
385 diag2(1, "Unterminated literal");
389 *e_token = *buf_ptr++;
390 if (buf_ptr >= buf_end)
392 if (*e_token == BACKSLASH) { /* if escape, copy extra char */
393 if (*buf_ptr == '\n') /* check for escaped newline */
395 *++e_token = *buf_ptr++;
396 ++e_token; /* we must increment this again because we
397 * copied two chars */
398 if (buf_ptr >= buf_end)
402 break; /* we copied one character */
403 } /* end of while (1) */
404 } while (*e_token++ != qchar);
421 unary_delim = state->last_u_d;
444 * if (state->in_or_st) state->block_init = 1;
446 /* ? code = state->block_init ? lparen : lbrace; */
452 /* ? code = state->block_init ? rparen : rbrace; */
456 case 014: /* a form feed */
457 unary_delim = state->last_u_d;
458 state->last_nl = true; /* remember this so we can set 'state->col_1'
474 case '+': /* check for -, +, --, ++ */
475 code = (state->last_u_d ? unary_op : binary_op);
478 if (*buf_ptr == token[0]) {
479 /* check for doubled character */
480 *e_token++ = *buf_ptr++;
481 /* buffer overflow will be checked at end of loop */
482 if (state->last_token == ident || state->last_token == rparen) {
483 code = (state->last_u_d ? unary_op : postop);
484 /* check for following ++ or -- */
488 else if (*buf_ptr == '=')
489 /* check for operator += */
490 *e_token++ = *buf_ptr++;
491 else if (*buf_ptr == '>') {
492 /* check for operator -> */
493 *e_token++ = *buf_ptr++;
496 state->want_blank = false;
498 break; /* buffer overflow will be checked at end of
503 state->block_init = 1;
504 if (*buf_ptr == '=') {/* == */
505 *e_token++ = '='; /* Flip =+ to += */
512 /* can drop thru!!! */
516 case '!': /* ops like <, <<, <=, !=, etc */
517 if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
518 *e_token++ = *buf_ptr;
519 if (++buf_ptr >= buf_end)
523 *e_token++ = *buf_ptr++;
524 code = (state->last_u_d ? unary_op : binary_op);
530 if (!state->last_u_d) {
532 *e_token++ = *buf_ptr++;
536 while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
537 if (*buf_ptr == '*') {
539 *e_token++ = *buf_ptr;
541 if (++buf_ptr >= buf_end)
547 while (isalpha((unsigned char)*tp) ||
548 isspace((unsigned char)*tp)) {
553 ps.procname[0] = ' ';
559 if (token[0] == '/' && *buf_ptr == '*') {
560 /* it is start of comment */
563 if (++buf_ptr >= buf_end)
567 unary_delim = state->last_u_d;
570 while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
572 * handle ||, &&, etc, and also things as in int *****i
575 *e_token++ = *buf_ptr;
576 if (++buf_ptr >= buf_end)
579 code = (state->last_u_d ? unary_op : binary_op);
583 } /* end of switch */
584 if (buf_ptr >= buf_end) /* check for input buffer empty */
586 state->last_u_d = unary_delim;
588 *e_token = '\0'; /* null terminate the token */
592 /* Initialize constant transition table */
594 init_constant_tt(void)
596 table['-'] = table['+'];
597 table['8'] = table['9'];
598 table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
599 table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
600 table['B'] = table['b'];
601 table['E'] = table['e'];
602 table['U'] = table['u'];
603 table['X'] = table['x'];
604 table['P'] = table['p'];
605 table['F'] = table['f'];
609 alloc_typenames(void)
612 typenames = (const char **)malloc(sizeof(typenames[0]) *
613 (typename_count = 16));
614 if (typenames == NULL)
619 add_typename(const char *key)
624 if (typename_top + 1 >= typename_count) {
625 typenames = realloc((void *)typenames,
626 sizeof(typenames[0]) * (typename_count *= 2));
627 if (typenames == NULL)
630 if (typename_top == -1)
631 typenames[++typename_top] = copy = strdup(key);
632 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
633 /* take advantage of sorted input */
634 if (comparison == 0) /* remove duplicates */
636 typenames[++typename_top] = copy = strdup(key);
641 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
642 /* find place for the new key */;
643 if (comparison == 0) /* remove duplicates */
645 memmove(&typenames[p + 1], &typenames[p],
646 sizeof(typenames[0]) * (++typename_top - p));
647 typenames[p] = copy = strdup(key);