lexi.c revision 1.96 1 /* $NetBSD: lexi.c,v 1.96 2021/10/24 20:47:00 rillig Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #if 0
41 static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/cdefs.h>
45 #if defined(__NetBSD__)
46 __RCSID("$NetBSD: lexi.c,v 1.96 2021/10/24 20:47:00 rillig Exp $");
47 #elif defined(__FreeBSD__)
48 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
49 #endif
50
51 #include <sys/param.h>
52 #include <assert.h>
53 #include <stdio.h>
54 #include <ctype.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "indent.h"
59
60 /* must be sorted alphabetically, is used in binary search */
61 static const struct keyword {
62 const char *name;
63 enum keyword_kind kind;
64 } keywords[] = {
65 {"_Bool", kw_type},
66 {"_Complex", kw_type},
67 {"_Imaginary", kw_type},
68 {"auto", kw_storage_class},
69 {"bool", kw_type},
70 {"break", kw_jump},
71 {"case", kw_case_or_default},
72 {"char", kw_type},
73 {"complex", kw_type},
74 {"const", kw_type},
75 {"continue", kw_jump},
76 {"default", kw_case_or_default},
77 {"do", kw_do_or_else},
78 {"double", kw_type},
79 {"else", kw_do_or_else},
80 {"enum", kw_struct_or_union_or_enum},
81 {"extern", kw_storage_class},
82 {"float", kw_type},
83 {"for", kw_for_or_if_or_while},
84 {"goto", kw_jump},
85 {"if", kw_for_or_if_or_while},
86 {"imaginary", kw_type},
87 {"inline", kw_inline_or_restrict},
88 {"int", kw_type},
89 {"long", kw_type},
90 {"offsetof", kw_offsetof},
91 {"register", kw_storage_class},
92 {"restrict", kw_inline_or_restrict},
93 {"return", kw_jump},
94 {"short", kw_type},
95 {"signed", kw_type},
96 {"sizeof", kw_sizeof},
97 {"static", kw_storage_class},
98 {"struct", kw_struct_or_union_or_enum},
99 {"switch", kw_switch},
100 {"typedef", kw_typedef},
101 {"union", kw_struct_or_union_or_enum},
102 {"unsigned", kw_type},
103 {"void", kw_type},
104 {"volatile", kw_type},
105 {"while", kw_for_or_if_or_while}
106 };
107
108 static struct {
109 const char **items;
110 unsigned int len;
111 unsigned int cap;
112 } typenames;
113
114 /*
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.
117 *
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?;
122 *
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;
129 */
130 /* INDENT OFF */
131 static const unsigned char lex_number_state[][26] = {
132 /* examples:
133 00
134 s 0xx
135 t 00xaa
136 a 11 101100xxa..
137 r 11ee0001101lbuuxx.a.pp
138 t.01.e+008bLuxll0Ll.aa.p+0
139 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
140 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu", /* (other) */
141 [1] = "CEIDEHHHIJQ U Q VUVVZZZ", /* 0 */
142 [2] = "DEIDEHHHIJQ U Q VUVVZZZ", /* 1 */
143 [3] = "DEIDEHHHIJ U VUVVZZZ", /* 2 3 4 5 6 7 */
144 [4] = "DEJDEHHHJJ U VUVVZZZ", /* 8 9 */
145 [5] = " U VUVV ", /* A a C c D d */
146 [6] = " K U VUVV ", /* B b */
147 [7] = " FFF FF U VUVV ", /* E e */
148 [8] = " f f U VUVV f", /* F f */
149 [9] = " LLf fL PR Li L f", /* L */
150 [10] = " OOf fO S P O i O f", /* l */
151 [11] = " FFX ", /* P p */
152 [12] = " MM M i iiM M ", /* U u */
153 [13] = " N ", /* X x */
154 [14] = " G Y ", /* + - */
155 [15] = "B EE EE T W ", /* . */
156 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
157 };
158 /* INDENT ON */
159
160 static const uint8_t lex_number_row[] = {
161 ['0'] = 1,
162 ['1'] = 2,
163 ['2'] = 3, ['3'] = 3, ['4'] = 3, ['5'] = 3, ['6'] = 3, ['7'] = 3,
164 ['8'] = 4, ['9'] = 4,
165 ['A'] = 5, ['a'] = 5, ['C'] = 5, ['c'] = 5, ['D'] = 5, ['d'] = 5,
166 ['B'] = 6, ['b'] = 6,
167 ['E'] = 7, ['e'] = 7,
168 ['F'] = 8, ['f'] = 8,
169 ['L'] = 9,
170 ['l'] = 10,
171 ['P'] = 11, ['p'] = 11,
172 ['U'] = 12, ['u'] = 12,
173 ['X'] = 13, ['x'] = 13,
174 ['+'] = 14, ['-'] = 14,
175 ['.'] = 15,
176 };
177
178 static char
179 inbuf_peek(void)
180 {
181 return *inp.s;
182 }
183
184 void
185 inbuf_skip(void)
186 {
187 inp.s++;
188 if (inp.s >= inp.e)
189 inbuf_read_line();
190 }
191
192 char
193 inbuf_next(void)
194 {
195 char ch = inbuf_peek();
196 inbuf_skip();
197 return ch;
198 }
199
200 static void
201 check_size_token(size_t desired_size)
202 {
203 if (token.e + desired_size >= token.l)
204 buf_expand(&token, desired_size);
205 }
206
207 static void
208 token_add_char(char ch)
209 {
210 check_size_token(1);
211 *token.e++ = ch;
212 }
213
214 static int
215 cmp_keyword_by_name(const void *key, const void *elem)
216 {
217 return strcmp(key, ((const struct keyword *)elem)->name);
218 }
219
220 #ifdef debug
221 const char *
222 token_type_name(token_type ttype)
223 {
224 static const char *const name[] = {
225 "end_of_file", "newline", "lparen_or_lbracket", "rparen_or_rbracket",
226 "unary_op", "binary_op", "postfix_op", "question",
227 "case_label", "colon",
228 "semicolon", "lbrace", "rbrace", "ident", "comma",
229 "comment", "switch_expr", "preprocessing", "form_feed", "decl",
230 "keyword_for_if_while", "keyword_do_else",
231 "if_expr", "while_expr", "for_exprs",
232 "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt",
233 "if_expr_stmt", "if_expr_stmt_else", "period", "string_prefix",
234 "storage_class", "funcname", "type_def", "keyword_struct_union_enum"
235 };
236
237 assert(0 <= ttype && ttype < array_length(name));
238
239 return name[ttype];
240 }
241
242 static void
243 debug_print_buf(const char *name, const struct buffer *buf)
244 {
245 if (buf->s < buf->e) {
246 debug_printf(" %s ", name);
247 debug_vis_range("\"", buf->s, buf->e, "\"");
248 }
249 }
250 #endif
251
252 static token_type
253 lexi_end(token_type ttype)
254 {
255 #ifdef debug
256 debug_printf("in line %d, lexi returns '%s'",
257 line_no, token_type_name(ttype));
258 debug_print_buf("token", &token);
259 debug_print_buf("label", &lab);
260 debug_print_buf("code", &code);
261 debug_print_buf("comment", &com);
262 debug_printf("\n");
263 #endif
264
265 return ttype;
266 }
267
268 static void
269 lex_number(void)
270 {
271 for (uint8_t s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
272 uint8_t ch = (uint8_t)*inp.s;
273 if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
274 break;
275
276 uint8_t row = lex_number_row[ch];
277 if (lex_number_state[row][s - 'A'] == ' ') {
278 /*-
279 * lex_number_state[0][s - 'A'] now indicates the type:
280 * f = floating, i = integer, u = unknown
281 */
282 break;
283 }
284
285 s = lex_number_state[row][s - 'A'];
286 token_add_char(inbuf_next());
287 }
288 }
289
290 static void
291 lex_word(void)
292 {
293 while (isalnum((unsigned char)*inp.s) ||
294 *inp.s == '\\' ||
295 *inp.s == '_' || *inp.s == '$') {
296
297 if (*inp.s == '\\') {
298 if (inp.s[1] == '\n') {
299 inp.s += 2;
300 if (inp.s >= inp.e)
301 inbuf_read_line();
302 } else
303 break;
304 }
305
306 token_add_char(inbuf_next());
307 }
308 }
309
310 static void
311 lex_char_or_string(void)
312 {
313 for (char delim = *token.s;;) {
314 if (*inp.s == '\n') {
315 diag(1, "Unterminated literal");
316 return;
317 }
318
319 token_add_char(inbuf_next());
320 if (token.e[-1] == delim)
321 return;
322
323 if (token.e[-1] == '\\') {
324 if (*inp.s == '\n')
325 ++line_no;
326 token_add_char(inbuf_next());
327 }
328 }
329 }
330
331 /* Guess whether the current token is a declared type. */
332 static bool
333 probably_typename(const struct parser_state *state)
334 {
335 if (state->p_l_follow != 0)
336 return false;
337 if (state->block_init || state->in_stmt)
338 return false;
339 if (inp.s[0] == '*' && inp.s[1] != '=')
340 goto maybe;
341 if (isalpha((unsigned char)*inp.s))
342 goto maybe;
343 return false;
344 maybe:
345 return state->last_token == semicolon ||
346 state->last_token == lbrace ||
347 state->last_token == rbrace;
348 }
349
350 static int
351 bsearch_typenames(const char *key)
352 {
353 const char **arr = typenames.items;
354 int lo = 0;
355 int hi = (int)typenames.len - 1;
356
357 while (lo <= hi) {
358 int mid = (int)((unsigned)(lo + hi) >> 1);
359 int cmp = strcmp(arr[mid], key);
360 if (cmp < 0)
361 lo = mid + 1;
362 else if (cmp > 0)
363 hi = mid - 1;
364 else
365 return mid;
366 }
367 return -(lo + 1);
368 }
369
370 static bool
371 is_typename(void)
372 {
373 if (opt.auto_typedefs &&
374 token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
375 return true;
376
377 return bsearch_typenames(token.s) >= 0;
378 }
379
380 /* Read an alphanumeric token into 'token', or return end_of_file. */
381 static token_type
382 lexi_alnum(struct parser_state *state)
383 {
384 if (!(isalnum((unsigned char)*inp.s) ||
385 *inp.s == '_' || *inp.s == '$' ||
386 (inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))))
387 return end_of_file; /* just as a placeholder */
388
389 if (isdigit((unsigned char)*inp.s) ||
390 (inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))) {
391 lex_number();
392 } else {
393 lex_word();
394 }
395 *token.e = '\0';
396
397 if (token.s[0] == 'L' && token.s[1] == '\0' &&
398 (*inp.s == '"' || *inp.s == '\''))
399 return string_prefix;
400
401 while (is_hspace(inbuf_peek()))
402 inbuf_skip();
403 state->keyword = kw_0;
404
405 if (state->last_token == keyword_struct_union_enum &&
406 state->p_l_follow == 0) {
407 state->next_unary = true;
408 return decl;
409 }
410
411 /* Operator after identifier is binary unless last token was 'struct'. */
412 state->next_unary = state->last_token == keyword_struct_union_enum;
413
414 const struct keyword *kw = bsearch(token.s, keywords,
415 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
416 if (kw == NULL) {
417 if (is_typename()) {
418 state->keyword = kw_type;
419 state->next_unary = true;
420 goto found_typename;
421 }
422
423 } else { /* we have a keyword */
424 state->keyword = kw->kind;
425 state->next_unary = true;
426
427 switch (kw->kind) {
428 case kw_switch:
429 return switch_expr;
430
431 case kw_case_or_default:
432 return case_label;
433
434 case kw_struct_or_union_or_enum:
435 case kw_type:
436 found_typename:
437 if (state->p_l_follow != 0) {
438 /* inside parens: cast, param list, offsetof or sizeof */
439 state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
440 }
441 if (state->last_token == period || state->last_token == unary_op) {
442 state->keyword = kw_0;
443 break;
444 }
445 if (kw != NULL && kw->kind == kw_struct_or_union_or_enum)
446 return keyword_struct_union_enum;
447 if (state->p_l_follow != 0)
448 break;
449 return decl;
450
451 case kw_for_or_if_or_while:
452 return keyword_for_if_while;
453
454 case kw_do_or_else:
455 return keyword_do_else;
456
457 case kw_storage_class:
458 return storage_class;
459
460 case kw_typedef:
461 return type_def;
462
463 default: /* all others are treated like any other
464 * identifier */
465 return ident;
466 }
467 }
468
469 if (*inp.s == '(' && state->tos <= 1 && state->ind_level == 0 &&
470 !state->in_parameter_declaration && !state->block_init) {
471
472 for (const char *p = inp.s; p < inp.e;)
473 if (*p++ == ')' && (*p == ';' || *p == ','))
474 goto not_proc;
475
476 strncpy(state->procname, token.s, sizeof state->procname - 1);
477 if (state->in_decl)
478 state->in_parameter_declaration = true;
479 return funcname;
480 not_proc:;
481
482 } else if (probably_typename(state)) {
483 state->keyword = kw_type;
484 state->next_unary = true;
485 return decl;
486 }
487
488 if (state->last_token == decl) /* if this is a declared variable,
489 * then following sign is unary */
490 state->next_unary = true; /* will make "int a -1" work */
491
492 return ident; /* the ident is not in the list */
493 }
494
495 /* Reads the next token, placing it in the global variable "token". */
496 token_type
497 lexi(struct parser_state *state)
498 {
499 token.e = token.s;
500 state->col_1 = state->last_nl;
501 state->last_nl = false;
502
503 while (is_hspace(*inp.s)) {
504 state->col_1 = false;
505 inbuf_skip();
506 }
507
508 token_type alnum_ttype = lexi_alnum(state);
509 if (alnum_ttype != end_of_file)
510 return lexi_end(alnum_ttype);
511
512 /* Scan a non-alphanumeric token */
513
514 check_size_token(3); /* for things like "<<=" */
515 *token.e++ = inbuf_next();
516 *token.e = '\0';
517
518 token_type ttype;
519 bool unary_delim = false; /* whether the current token forces a
520 * following operator to be unary */
521
522 switch (*token.s) {
523 case '\n':
524 unary_delim = state->next_unary;
525 state->last_nl = true; /* remember that we just had a newline */
526 /* if data has been exhausted, the newline is a dummy. */
527 ttype = had_eof ? end_of_file : newline;
528 break;
529
530 case '\'':
531 case '"':
532 lex_char_or_string();
533 ttype = ident;
534 break;
535
536 case '(':
537 case '[':
538 unary_delim = true;
539 ttype = lparen_or_lbracket;
540 break;
541
542 case ')':
543 case ']':
544 ttype = rparen_or_rbracket;
545 break;
546
547 case '#':
548 unary_delim = state->next_unary;
549 ttype = preprocessing;
550 break;
551
552 case '?':
553 unary_delim = true;
554 ttype = question;
555 break;
556
557 case ':':
558 ttype = colon;
559 unary_delim = true;
560 break;
561
562 case ';':
563 unary_delim = true;
564 ttype = semicolon;
565 break;
566
567 case '{':
568 unary_delim = true;
569 ttype = lbrace;
570 break;
571
572 case '}':
573 unary_delim = true;
574 ttype = rbrace;
575 break;
576
577 case '\f':
578 unary_delim = state->next_unary;
579 state->last_nl = true; /* remember this, so we can set 'state->col_1'
580 * right */
581 ttype = form_feed;
582 break;
583
584 case ',':
585 unary_delim = true;
586 ttype = comma;
587 break;
588
589 case '.':
590 unary_delim = false;
591 ttype = period;
592 break;
593
594 case '-':
595 case '+':
596 ttype = state->next_unary ? unary_op : binary_op;
597 unary_delim = true;
598
599 if (*inp.s == token.s[0]) { /* ++, -- */
600 *token.e++ = *inp.s++;
601 if (state->last_token == ident ||
602 state->last_token == rparen_or_rbracket) {
603 ttype = state->next_unary ? unary_op : postfix_op;
604 unary_delim = false;
605 }
606
607 } else if (*inp.s == '=') { /* += */
608 *token.e++ = *inp.s++;
609
610 } else if (*inp.s == '>') { /* -> */
611 *token.e++ = *inp.s++;
612 unary_delim = false;
613 ttype = unary_op;
614 state->want_blank = false;
615 }
616 break;
617
618 case '=':
619 if (state->init_or_struct)
620 state->block_init = true;
621 if (*inp.s == '=') { /* == */
622 *token.e++ = *inp.s++;
623 *token.e = '\0';
624 }
625 ttype = binary_op;
626 unary_delim = true;
627 break;
628
629 case '>':
630 case '<':
631 case '!': /* ops like <, <<, <=, !=, etc */
632 if (*inp.s == '>' || *inp.s == '<' || *inp.s == '=')
633 *token.e++ = inbuf_next();
634 if (*inp.s == '=')
635 *token.e++ = *inp.s++;
636 ttype = state->next_unary ? unary_op : binary_op;
637 unary_delim = true;
638 break;
639
640 case '*':
641 unary_delim = true;
642 if (!state->next_unary) {
643 if (*inp.s == '=')
644 *token.e++ = *inp.s++;
645 ttype = binary_op;
646 break;
647 }
648
649 while (*inp.s == '*' || isspace((unsigned char)*inp.s)) {
650 if (*inp.s == '*')
651 token_add_char('*');
652 inbuf_skip();
653 }
654
655 if (ps.in_decl) {
656 char *tp = inp.s;
657
658 while (isalpha((unsigned char)*tp) ||
659 isspace((unsigned char)*tp)) {
660 if (++tp >= inp.e)
661 inbuf_read_line();
662 }
663 if (*tp == '(')
664 ps.procname[0] = ' ';
665 }
666
667 ttype = unary_op;
668 break;
669
670 default:
671 if (token.s[0] == '/' && (*inp.s == '*' || *inp.s == '/')) {
672 /* it is start of comment */
673 *token.e++ = inbuf_next();
674
675 ttype = comment;
676 unary_delim = state->next_unary;
677 break;
678 }
679
680 while (token.e[-1] == *inp.s || *inp.s == '=') {
681 /* handle '||', '&&', etc., and also things as in 'int *****i' */
682 token_add_char(inbuf_next());
683 }
684
685 ttype = state->next_unary ? unary_op : binary_op;
686 unary_delim = true;
687 }
688
689 if (inp.s >= inp.e) /* check for input buffer empty */
690 inbuf_read_line();
691
692 state->next_unary = unary_delim;
693
694 check_size_token(1);
695 *token.e = '\0';
696
697 return lexi_end(ttype);
698 }
699
700 void
701 add_typename(const char *name)
702 {
703 if (typenames.len >= typenames.cap) {
704 typenames.cap = 16 + 2 * typenames.cap;
705 typenames.items = xrealloc(typenames.items,
706 sizeof(typenames.items[0]) * typenames.cap);
707 }
708
709 int pos = bsearch_typenames(name);
710 if (pos >= 0)
711 return; /* already in the list */
712
713 pos = -(pos + 1);
714 memmove(typenames.items + pos + 1, typenames.items + pos,
715 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
716 typenames.items[pos] = xstrdup(name);
717 }
718