IMPORT openssh-9.8p1
[dragonfly.git] / usr.bin / indent / lexi.c
blobb608ae5261fed4a84b7a8c81b029173a1447a0b0
1 /*-
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.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)lexi.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $
38 * Here we have the token scanner for indent. It scans off one token and puts
39 * it in the global variable "token". It returns a code, indicating the type
40 * of token scanned.
43 #include <err.h>
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/param.h>
50 #include "indent_globs.h"
51 #include "indent_codes.h"
52 #include "indent.h"
54 struct templ {
55 const char *rwd;
56 int rwcode;
60 * This table has to be sorted alphabetically, because it'll be used in binary
61 * search. For the same reason, string must be the first thing in struct templ.
63 struct templ specials[] =
65 {"_Bool", 4},
66 {"_Complex", 4},
67 {"_Imaginary", 4},
68 {"auto", 10},
69 {"bool", 4},
70 {"break", 9},
71 {"case", 8},
72 {"char", 4},
73 {"complex", 4},
74 {"const", 4},
75 {"continue", 12},
76 {"default", 8},
77 {"do", 6},
78 {"double", 4},
79 {"else", 6},
80 {"enum", 3},
81 {"extern", 10},
82 {"float", 4},
83 {"for", 5},
84 {"global", 4},
85 {"goto", 9},
86 {"if", 5},
87 {"imaginary", 4},
88 {"inline", 12},
89 {"int", 4},
90 {"long", 4},
91 {"offsetof", 1},
92 {"register", 10},
93 {"restrict", 12},
94 {"return", 9},
95 {"short", 4},
96 {"signed", 4},
97 {"sizeof", 2},
98 {"static", 10},
99 {"struct", 3},
100 {"switch", 7},
101 {"typedef", 11},
102 {"union", 3},
103 {"unsigned", 4},
104 {"void", 4},
105 {"volatile", 4},
106 {"while", 5}
109 const char **typenames;
110 int typename_count;
111 int typename_top = -1;
114 * The transition table below was rewritten by hand from lx's output, given
115 * the following definitions. lx is Katherine Flavel's lexer generator.
117 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
118 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
119 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
120 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
122 * D+ E FS? -> $float;
123 * D* "." D+ E? FS? -> $float;
124 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
125 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
126 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
127 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
129 static char const *table[] = {
130 /* examples:
132 s 0xx
133 t 00xaa
134 a 11 101100xxa..
135 r 11ee0001101lbuuxx.a.pp
136 t.01.e+008bLuxll0Ll.aa.p+0
137 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
138 ['0'] = "CEIDEHHHIJQ U Q VUVVZZZ",
139 ['1'] = "DEIDEHHHIJQ U Q VUVVZZZ",
140 ['7'] = "DEIDEHHHIJ U VUVVZZZ",
141 ['9'] = "DEJDEHHHJJ U VUVVZZZ",
142 ['a'] = " U VUVV ",
143 ['b'] = " K U VUVV ",
144 ['e'] = " FFF FF U VUVV ",
145 ['f'] = " f f U VUVV f",
146 ['u'] = " MM M i iiM M ",
147 ['x'] = " N ",
148 ['p'] = " FFX ",
149 ['L'] = " LLf fL PR Li L f",
150 ['l'] = " OOf fO S P O i O f",
151 ['+'] = " G Y ",
152 ['.'] = "B EE EE T W ",
153 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
154 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu",
157 static int
158 strcmp_type(const void *e1, const void *e2)
160 return (strcmp(e1, *(const char * const *)e2));
164 lexi(struct parser_state *state)
166 int unary_delim; /* this is set to 1 if the current token
167 * forces a following operator to be unary */
168 int code; /* internal code to be returned */
169 char qchar; /* the delimiter character for a string */
171 e_token = s_token; /* point to start of place to save token */
172 unary_delim = false;
173 state->col_1 = state->last_nl; /* tell world that this token started
174 * in column 1 iff the last thing
175 * scanned was a newline */
176 state->last_nl = false;
178 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
179 state->col_1 = false; /* leading blanks imply token is not in column
180 * 1 */
181 if (++buf_ptr >= buf_end)
182 fill_buffer();
185 /* Scan an alphanumeric token */
186 if (isalnum((unsigned char)*buf_ptr) ||
187 *buf_ptr == '_' || *buf_ptr == '$' ||
188 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
190 * we have a character or number
192 struct templ *p;
194 if (isdigit((unsigned char)*buf_ptr) ||
195 (buf_ptr[0] == '.' && isdigit((unsigned char)buf_ptr[1]))) {
196 char s;
197 unsigned char i;
199 for (s = 'A'; s != 'f' && s != 'i' && s != 'u'; ) {
200 i = (unsigned char)*buf_ptr;
201 if (i >= nitems(table) || table[i] == NULL ||
202 table[i][s - 'A'] == ' ') {
203 s = table[0][s - 'A'];
204 break;
206 s = table[i][s - 'A'];
207 CHECK_SIZE_TOKEN(1);
208 *e_token++ = *buf_ptr++;
209 if (buf_ptr >= buf_end)
210 fill_buffer();
212 /* s now indicates the type: f(loating), i(integer), u(nknown) */
214 else
215 while (isalnum((unsigned char)*buf_ptr) ||
216 *buf_ptr == BACKSLASH ||
217 *buf_ptr == '_' || *buf_ptr == '$') {
218 /* fill_buffer() terminates buffer with newline */
219 if (*buf_ptr == BACKSLASH) {
220 if (*(buf_ptr + 1) == '\n') {
221 buf_ptr += 2;
222 if (buf_ptr >= buf_end)
223 fill_buffer();
224 } else
225 break;
227 CHECK_SIZE_TOKEN(1);
228 /* copy it over */
229 *e_token++ = *buf_ptr++;
230 if (buf_ptr >= buf_end)
231 fill_buffer();
233 *e_token = '\0';
235 if (s_token[0] == 'L' && s_token[1] == '\0' &&
236 (*buf_ptr == '"' || *buf_ptr == '\''))
237 return (strpfx);
239 while (*buf_ptr == ' ' || *buf_ptr == '\t') { /* get rid of blanks */
240 if (++buf_ptr >= buf_end)
241 fill_buffer();
243 state->keyword = 0;
244 if (state->last_token == structure && !state->p_l_follow) {
245 /* if last token was 'struct' and we're not
246 * in parentheses, then this token
247 * should be treated as a declaration */
248 state->last_u_d = true;
249 return (decl);
252 * Operator after identifier is binary unless last token was 'struct'
254 state->last_u_d = (state->last_token == structure);
256 p = bsearch(s_token,
257 specials,
258 sizeof(specials) / sizeof(specials[0]),
259 sizeof(specials[0]),
260 strcmp_type);
261 if (p == NULL) { /* not a special keyword... */
262 char *u;
264 /* ... so maybe a type_t or a typedef */
265 if ((opt.auto_typedefs && ((u = strrchr(s_token, '_')) != NULL) &&
266 strcmp(u, "_t") == 0) || (typename_top >= 0 &&
267 bsearch(s_token, typenames, typename_top + 1,
268 sizeof(typenames[0]), strcmp_type))) {
269 state->keyword = 4; /* a type name */
270 state->last_u_d = true;
271 goto found_typename;
273 } else { /* we have a keyword */
274 state->keyword = p->rwcode;
275 state->last_u_d = true;
276 switch (p->rwcode) {
277 case 7: /* it is a switch */
278 return (swstmt);
279 case 8: /* a case or default */
280 return (casestmt);
282 case 3: /* a "struct" */
283 /* FALLTHROUGH */
284 case 4: /* one of the declaration keywords */
285 found_typename:
286 if (state->p_l_follow) {
287 /* inside parens: cast, param list, offsetof or sizeof */
288 state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
290 if (state->last_token == period || state->last_token == unary_op) {
291 state->keyword = 0;
292 break;
294 if (p != NULL && p->rwcode == 3)
295 return (structure);
296 if (state->p_l_follow)
297 break;
298 return (decl);
300 case 5: /* if, while, for */
301 return (sp_paren);
303 case 6: /* do, else */
304 return (sp_nparen);
306 case 10: /* storage class specifier */
307 return (storage);
309 case 11: /* typedef */
310 return (type_def);
312 /* FALLTHROUGH */
313 default: /* all others are treated like any other
314 * identifier */
315 return (ident);
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) {
320 char *tp = buf_ptr;
321 while (tp < buf_end)
322 if (*tp++ == ')' && (*tp == ';' || *tp == ','))
323 goto not_proc;
324 strncpy(state->procname, token, sizeof state->procname - 1);
325 if (state->in_decl)
326 state->in_parameter_declaration = 1;
327 return (funcname);
328 not_proc:;
331 * The following hack attempts to guess whether or not the current
332 * token is in fact a declaration keyword -- one that has been
333 * typedefd
335 else if (!state->p_l_follow && !state->block_init &&
336 !state->in_stmt &&
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;
343 return decl;
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 procesing 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
355 * moved here */
356 *e_token = '\0';
357 if (++buf_ptr >= buf_end)
358 fill_buffer();
360 switch (*token) {
361 case '\n':
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
370 break;
372 case '\'': /* start of quoted character */
373 case '"': /* start of string */
374 qchar = *token;
375 do { /* copy the string */
376 while (1) { /* move one character or [/<char>]<char> */
377 if (*buf_ptr == '\n') {
378 diag2(1, "Unterminated literal");
379 goto stop_lit;
381 CHECK_SIZE_TOKEN(2);
382 *e_token = *buf_ptr++;
383 if (buf_ptr >= buf_end)
384 fill_buffer();
385 if (*e_token == BACKSLASH) { /* if escape, copy extra char */
386 if (*buf_ptr == '\n') /* check for escaped newline */
387 ++line_no;
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)
392 fill_buffer();
394 else
395 break; /* we copied one character */
396 } /* end of while (1) */
397 } while (*e_token++ != qchar);
398 stop_lit:
399 code = ident;
400 break;
402 case ('('):
403 case ('['):
404 unary_delim = true;
405 code = lparen;
406 break;
408 case (')'):
409 case (']'):
410 code = rparen;
411 break;
413 case '#':
414 unary_delim = state->last_u_d;
415 code = preesc;
416 break;
418 case '?':
419 unary_delim = true;
420 code = question;
421 break;
423 case (':'):
424 code = colon;
425 unary_delim = true;
426 break;
428 case (';'):
429 unary_delim = true;
430 code = semicolon;
431 break;
433 case ('{'):
434 unary_delim = true;
437 * if (state->in_or_st) state->block_init = 1;
439 /* ? code = state->block_init ? lparen : lbrace; */
440 code = lbrace;
441 break;
443 case ('}'):
444 unary_delim = true;
445 /* ? code = state->block_init ? rparen : rbrace; */
446 code = rbrace;
447 break;
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'
452 * right */
453 code = form_feed;
454 break;
456 case (','):
457 unary_delim = true;
458 code = comma;
459 break;
461 case '.':
462 unary_delim = false;
463 code = period;
464 break;
466 case '-':
467 case '+': /* check for -, +, --, ++ */
468 code = (state->last_u_d ? unary_op : binary_op);
469 unary_delim = true;
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 -- */
478 unary_delim = false;
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 unary_delim = false;
488 code = unary_op;
489 state->want_blank = false;
491 break; /* buffer overflow will be checked at end of
492 * switch */
494 case '=':
495 if (state->in_or_st)
496 state->block_init = 1;
497 if (*buf_ptr == '=') {/* == */
498 *e_token++ = '='; /* Flip =+ to += */
499 buf_ptr++;
500 *e_token = 0;
502 code = binary_op;
503 unary_delim = true;
504 break;
505 /* can drop thru!!! */
507 case '>':
508 case '<':
509 case '!': /* ops like <, <<, <=, !=, etc */
510 if (*buf_ptr == '>' || *buf_ptr == '<' || *buf_ptr == '=') {
511 *e_token++ = *buf_ptr;
512 if (++buf_ptr >= buf_end)
513 fill_buffer();
515 if (*buf_ptr == '=')
516 *e_token++ = *buf_ptr++;
517 code = (state->last_u_d ? unary_op : binary_op);
518 unary_delim = true;
519 break;
521 case '*':
522 unary_delim = true;
523 if (!state->last_u_d) {
524 if (*buf_ptr == '=')
525 *e_token++ = *buf_ptr++;
526 code = binary_op;
527 break;
529 while (*buf_ptr == '*' || isspace((unsigned char)*buf_ptr)) {
530 if (*buf_ptr == '*') {
531 CHECK_SIZE_TOKEN(1);
532 *e_token++ = *buf_ptr;
534 if (++buf_ptr >= buf_end)
535 fill_buffer();
537 if (ps.in_decl) {
538 char *tp = buf_ptr;
540 while (isalpha((unsigned char)*tp) ||
541 isspace((unsigned char)*tp)) {
542 if (++tp >= buf_end)
543 fill_buffer();
545 if (*tp == '(')
546 ps.procname[0] = ' ';
548 code = unary_op;
549 break;
551 default:
552 if (token[0] == '/' && *buf_ptr == '*') {
553 /* it is start of comment */
554 *e_token++ = '*';
556 if (++buf_ptr >= buf_end)
557 fill_buffer();
559 code = comment;
560 unary_delim = state->last_u_d;
561 break;
563 while (*(e_token - 1) == *buf_ptr || *buf_ptr == '=') {
565 * handle ||, &&, etc, and also things as in int *****i
567 CHECK_SIZE_TOKEN(1);
568 *e_token++ = *buf_ptr;
569 if (++buf_ptr >= buf_end)
570 fill_buffer();
572 code = (state->last_u_d ? unary_op : binary_op);
573 unary_delim = true;
576 } /* end of switch */
577 if (buf_ptr >= buf_end) /* check for input buffer empty */
578 fill_buffer();
579 state->last_u_d = unary_delim;
580 CHECK_SIZE_TOKEN(1);
581 *e_token = '\0'; /* null terminate the token */
582 return (code);
585 /* Initialize constant transition table */
586 void
587 init_constant_tt(void)
589 table['-'] = table['+'];
590 table['8'] = table['9'];
591 table['2'] = table['3'] = table['4'] = table['5'] = table['6'] = table['7'];
592 table['A'] = table['C'] = table['D'] = table['c'] = table['d'] = table['a'];
593 table['B'] = table['b'];
594 table['E'] = table['e'];
595 table['U'] = table['u'];
596 table['X'] = table['x'];
597 table['P'] = table['p'];
598 table['F'] = table['f'];
601 void
602 alloc_typenames(void)
605 typenames = (const char **)malloc(sizeof(typenames[0]) *
606 (typename_count = 16));
607 if (typenames == NULL)
608 err(1, NULL);
611 void
612 add_typename(const char *key)
614 int comparison;
615 const char *copy;
617 if (typename_top + 1 >= typename_count) {
618 typenames = realloc((void *)typenames,
619 sizeof(typenames[0]) * (typename_count *= 2));
620 if (typenames == NULL)
621 err(1, NULL);
623 if (typename_top == -1)
624 typenames[++typename_top] = copy = strdup(key);
625 else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) {
626 /* take advantage of sorted input */
627 if (comparison == 0) /* remove duplicates */
628 return;
629 typenames[++typename_top] = copy = strdup(key);
631 else {
632 int p;
634 for (p = 0; (comparison = strcmp(key, typenames[p])) > 0; p++)
635 /* find place for the new key */;
636 if (comparison == 0) /* remove duplicates */
637 return;
638 memmove(&typenames[p + 1], &typenames[p],
639 sizeof(typenames[0]) * (++typename_top - p));
640 typenames[p] = copy = strdup(key);
643 if (copy == NULL)
644 err(1, NULL);