lexi.c revision 1.164 1 /* $NetBSD: lexi.c,v 1.164 2021/11/25 18:48:37 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.164 2021/11/25 18:48:37 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 <stdlib.h>
52 #include <string.h>
53
54 #include "indent.h"
55
56 /*
57 * While inside lexi_alnum, this constant just marks a type, independently of
58 * the parentheses level.
59 */
60 #define lsym_type lsym_type_outside_parentheses
61
62 /* must be sorted alphabetically, is used in binary search */
63 static const struct keyword {
64 const char *name;
65 lexer_symbol lsym;
66 } keywords[] = {
67 {"_Bool", lsym_type},
68 {"_Complex", lsym_type},
69 {"_Imaginary", lsym_type},
70 {"auto", lsym_storage_class},
71 {"bool", lsym_type},
72 {"break", lsym_word},
73 {"case", lsym_case_label},
74 {"char", lsym_type},
75 {"complex", lsym_type},
76 {"const", lsym_type},
77 {"continue", lsym_word},
78 {"default", lsym_case_label},
79 {"do", lsym_do},
80 {"double", lsym_type},
81 {"else", lsym_else},
82 {"enum", lsym_tag},
83 {"extern", lsym_storage_class},
84 {"float", lsym_type},
85 {"for", lsym_for},
86 {"goto", lsym_word},
87 {"if", lsym_if},
88 {"imaginary", lsym_type},
89 {"inline", lsym_word},
90 {"int", lsym_type},
91 {"long", lsym_type},
92 {"offsetof", lsym_offsetof},
93 {"register", lsym_storage_class},
94 {"restrict", lsym_word},
95 {"return", lsym_return},
96 {"short", lsym_type},
97 {"signed", lsym_type},
98 {"sizeof", lsym_sizeof},
99 {"static", lsym_storage_class},
100 {"struct", lsym_tag},
101 {"switch", lsym_switch},
102 {"typedef", lsym_typedef},
103 {"union", lsym_tag},
104 {"unsigned", lsym_type},
105 {"void", lsym_type},
106 {"volatile", lsym_type},
107 {"while", lsym_while}
108 };
109
110 static struct {
111 const char **items;
112 unsigned int len;
113 unsigned int cap;
114 } typenames;
115
116 /*
117 * The transition table below was rewritten by hand from lx's output, given
118 * the following definitions. lx is Katherine Flavel's lexer generator.
119 *
120 * O = /[0-7]/; D = /[0-9]/; NZ = /[1-9]/;
121 * H = /[a-f0-9]/i; B = /[0-1]/; HP = /0x/i;
122 * BP = /0b/i; E = /e[+\-]?/i D+; P = /p[+\-]?/i D+;
123 * FS = /[fl]/i; IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
124 *
125 * D+ E FS? -> $float;
126 * D* "." D+ E? FS? -> $float;
127 * D+ "." E? FS? -> $float; HP H+ IS? -> $int;
128 * HP H+ P FS? -> $float; NZ D* IS? -> $int;
129 * HP H* "." H+ P FS? -> $float; "0" O* IS? -> $int;
130 * HP H+ "." P FS -> $float; BP B+ IS? -> $int;
131 */
132 /* INDENT OFF */
133 static const unsigned char lex_number_state[][26] = {
134 /* examples:
135 00
136 s 0xx
137 t 00xaa
138 a 11 101100xxa..
139 r 11ee0001101lbuuxx.a.pp
140 t.01.e+008bLuxll0Ll.aa.p+0
141 states: ABCDEFGHIJKLMNOPQRSTUVWXYZ */
142 [0] = "uuiifuufiuuiiuiiiiiuiuuuuu", /* (other) */
143 [1] = "CEIDEHHHIJQ U Q VUVVZZZ", /* 0 */
144 [2] = "DEIDEHHHIJQ U Q VUVVZZZ", /* 1 */
145 [3] = "DEIDEHHHIJ U VUVVZZZ", /* 2 3 4 5 6 7 */
146 [4] = "DEJDEHHHJJ U VUVVZZZ", /* 8 9 */
147 [5] = " U VUVV ", /* A a C c D d */
148 [6] = " K U VUVV ", /* B b */
149 [7] = " FFF FF U VUVV ", /* E e */
150 [8] = " f f U VUVV f", /* F f */
151 [9] = " LLf fL PR Li L f", /* L */
152 [10] = " OOf fO S P O i O f", /* l */
153 [11] = " FFX ", /* P p */
154 [12] = " MM M i iiM M ", /* U u */
155 [13] = " N ", /* X x */
156 [14] = " G Y ", /* + - */
157 [15] = "B EE EE T W ", /* . */
158 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
159 };
160 /* INDENT ON */
161
162 static const unsigned char lex_number_row[] = {
163 ['0'] = 1,
164 ['1'] = 2,
165 ['2'] = 3, ['3'] = 3, ['4'] = 3, ['5'] = 3, ['6'] = 3, ['7'] = 3,
166 ['8'] = 4, ['9'] = 4,
167 ['A'] = 5, ['a'] = 5, ['C'] = 5, ['c'] = 5, ['D'] = 5, ['d'] = 5,
168 ['B'] = 6, ['b'] = 6,
169 ['E'] = 7, ['e'] = 7,
170 ['F'] = 8, ['f'] = 8,
171 ['L'] = 9,
172 ['l'] = 10,
173 ['P'] = 11, ['p'] = 11,
174 ['U'] = 12, ['u'] = 12,
175 ['X'] = 13, ['x'] = 13,
176 ['+'] = 14, ['-'] = 14,
177 ['.'] = 15,
178 };
179
180 static void
181 check_size_token(size_t desired_size)
182 {
183 if (token.e + desired_size >= token.l)
184 buf_expand(&token, desired_size);
185 }
186
187 static void
188 token_add_char(char ch)
189 {
190 check_size_token(1);
191 *token.e++ = ch;
192 }
193
194 #ifdef debug
195 static const char *
196 lsym_name(lexer_symbol sym)
197 {
198 static const char *const name[] = {
199 "eof",
200 "preprocessing",
201 "newline",
202 "form_feed",
203 "comment",
204 "lparen_or_lbracket",
205 "rparen_or_rbracket",
206 "lbrace",
207 "rbrace",
208 "period",
209 "unary_op",
210 "binary_op",
211 "postfix_op",
212 "question",
213 "colon",
214 "comma",
215 "semicolon",
216 "typedef",
217 "storage_class",
218 "type_outside_parentheses",
219 "type_in_parentheses",
220 "tag",
221 "case_label",
222 "string_prefix",
223 "sizeof",
224 "offsetof",
225 "word",
226 "funcname",
227 "do",
228 "else",
229 "for",
230 "if",
231 "switch",
232 "while",
233 "return",
234 };
235
236 return name[sym];
237 }
238
239 static void
240 debug_print_buf(const char *name, const struct buffer *buf)
241 {
242 if (buf->s < buf->e) {
243 debug_printf("%s ", name);
244 debug_vis_range("\"", buf->s, buf->e, "\"\n");
245 }
246 }
247
248 #define debug_ps_bool(name) \
249 if (ps.name != prev_ps.name) \
250 debug_println("[%c] ps." #name, ps.name ? 'x' : ' ')
251 #define debug_ps_int(name) \
252 if (ps.name != prev_ps.name) \
253 debug_println("%3d ps." #name, ps.name)
254
255 static void
256 debug_lexi(lexer_symbol lsym)
257 {
258 /*
259 * Watch out for 'rolled back parser state' in the debug output; the
260 * differences around these are unreliable.
261 */
262 static struct parser_state prev_ps;
263
264 debug_println("");
265 debug_printf("line %d: %s", line_no, lsym_name(lsym));
266 debug_vis_range(" \"", token.s, token.e, "\"\n");
267
268 debug_print_buf("label", &lab);
269 debug_print_buf("code", &code);
270 debug_print_buf("comment", &com);
271
272 debug_println(" ps.prev_token = %s", lsym_name(ps.prev_token));
273 debug_ps_bool(next_col_1);
274 debug_ps_bool(curr_col_1);
275 debug_ps_bool(next_unary);
276 debug_ps_bool(is_function_definition);
277 debug_ps_bool(want_blank);
278 debug_ps_int(paren_level);
279 debug_ps_int(p_l_follow);
280 if (ps.paren_level != prev_ps.paren_level) {
281 debug_printf(" ps.paren_indents:");
282 for (int i = 0; i < ps.paren_level; i++)
283 debug_printf(" %d", ps.paren_indents[i]);
284 debug_println("");
285 }
286 debug_ps_int(cast_mask);
287 debug_ps_int(not_cast_mask);
288
289 debug_ps_int(comment_delta);
290 debug_ps_int(n_comment_delta);
291 debug_ps_int(com_ind);
292
293 debug_ps_bool(block_init);
294 debug_ps_int(block_init_level);
295 debug_ps_bool(init_or_struct);
296
297 debug_ps_int(ind_level);
298 debug_ps_int(ind_level_follow);
299
300 debug_ps_int(decl_level);
301 debug_ps_bool(decl_on_line);
302 debug_ps_bool(in_decl);
303 debug_ps_int(just_saw_decl);
304 debug_ps_bool(in_func_def_params);
305 debug_ps_bool(decl_indent_done);
306
307 debug_ps_bool(in_stmt_or_decl);
308 debug_ps_bool(in_stmt_cont);
309 debug_ps_bool(is_case_label);
310
311 debug_ps_bool(search_stmt);
312
313 prev_ps = ps;
314 }
315 #endif
316
317 static lexer_symbol
318 lexi_end(lexer_symbol lsym)
319 {
320 #ifdef debug
321 debug_lexi(lsym);
322 #endif
323 return lsym;
324 }
325
326 static void
327 lex_number(void)
328 {
329 for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
330 unsigned char ch = (unsigned char)inp_peek();
331 if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
332 break;
333
334 unsigned char row = lex_number_row[ch];
335 if (lex_number_state[row][s - 'A'] == ' ') {
336 /*-
337 * lex_number_state[0][s - 'A'] now indicates the type:
338 * f = floating, i = integer, u = unknown
339 */
340 return;
341 }
342
343 s = lex_number_state[row][s - 'A'];
344 token_add_char(inp_next());
345 }
346 }
347
348 static bool
349 is_identifier_start(char ch)
350 {
351 return ch_isalpha(ch) || ch == '_' || ch == '$';
352 }
353
354 static bool
355 is_identifier_part(char ch)
356 {
357 return ch_isalnum(ch) || ch == '_' || ch == '$';
358 }
359
360 static void
361 lex_word(void)
362 {
363 for (;;) {
364 if (is_identifier_part(inp_peek()))
365 token_add_char(inp_next());
366 else if (inp_peek() == '\\' && inp_lookahead(1) == '\n') {
367 inp_skip();
368 inp_skip();
369 } else
370 return;
371 }
372 }
373
374 static void
375 lex_char_or_string(void)
376 {
377 for (char delim = token.e[-1];;) {
378 if (inp_peek() == '\n') {
379 diag(1, "Unterminated literal");
380 return;
381 }
382
383 token_add_char(inp_next());
384 if (token.e[-1] == delim)
385 return;
386
387 if (token.e[-1] == '\\') {
388 if (inp_peek() == '\n')
389 ++line_no;
390 token_add_char(inp_next());
391 }
392 }
393 }
394
395 /* Guess whether the current token is a declared type. */
396 static bool
397 probably_typename(void)
398 {
399 if (ps.prev_token == lsym_storage_class)
400 return true;
401 if (ps.block_init)
402 return false;
403 if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */
404 return false;
405 if (inp_peek() == '*' && inp_lookahead(1) != '=')
406 goto maybe;
407 /* XXX: is_identifier_start */
408 if (ch_isalpha(inp_peek()))
409 goto maybe;
410 return false;
411 maybe:
412 return ps.prev_token == lsym_semicolon ||
413 ps.prev_token == lsym_lbrace ||
414 ps.prev_token == lsym_rbrace;
415 }
416
417 static int
418 bsearch_typenames(const char *key)
419 {
420 const char **arr = typenames.items;
421 int lo = 0;
422 int hi = (int)typenames.len - 1;
423
424 while (lo <= hi) {
425 int mid = (int)((unsigned)(lo + hi) >> 1);
426 int cmp = strcmp(arr[mid], key);
427 if (cmp < 0)
428 lo = mid + 1;
429 else if (cmp > 0)
430 hi = mid - 1;
431 else
432 return mid;
433 }
434 return -(lo + 1);
435 }
436
437 static bool
438 is_typename(void)
439 {
440 if (opt.auto_typedefs &&
441 token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
442 return true;
443
444 return bsearch_typenames(token.s) >= 0;
445 }
446
447 static int
448 cmp_keyword_by_name(const void *key, const void *elem)
449 {
450 return strcmp(key, ((const struct keyword *)elem)->name);
451 }
452
453 static bool
454 probably_looking_at_definition(void)
455 {
456 int paren_level = 0;
457 for (const char *p = inp_p(), *e = inp_line_end(); p < e; p++) {
458 proceed:
459 if (*p == '(')
460 paren_level++;
461 if (*p == ')' && --paren_level == 0) {
462 p++;
463 while (p < e && (ch_isspace(*p) || is_identifier_part(*p)))
464 p++;
465 if (*p == '(')
466 goto proceed;
467 return !(*p == ';' || *p == ',');
468 }
469 }
470
471 /*
472 * To further reduce the cases where indent wrongly treats an incomplete
473 * function declaration as a function definition, thus adding a newline
474 * before the function name, it may be worth looking for parameter names,
475 * as these are often omitted in function declarations and only included
476 * in function definitions. Or just increase the lookahead to more than
477 * just the current line of input, until the next '{'.
478 */
479 return true;
480 }
481
482 /* Read an alphanumeric token into 'token', or return lsym_eof. */
483 static lexer_symbol
484 lexi_alnum(void)
485 {
486 if (ch_isdigit(inp_peek()) ||
487 (inp_peek() == '.' && ch_isdigit(inp_lookahead(1)))) {
488 lex_number();
489 } else if (is_identifier_part(inp_peek())) {
490 lex_word();
491 } else
492 return lsym_eof; /* just as a placeholder */
493
494 *token.e = '\0';
495
496 if (token.s[0] == 'L' && token.s[1] == '\0' &&
497 (inp_peek() == '"' || inp_peek() == '\''))
498 return lsym_string_prefix;
499
500 while (ch_isblank(inp_peek()))
501 inp_skip();
502
503 ps.next_unary = ps.prev_token == lsym_tag; /* for 'struct s *' */
504
505 if (ps.prev_token == lsym_tag && ps.p_l_follow == 0)
506 return lsym_type_outside_parentheses;
507
508 const struct keyword *kw = bsearch(token.s, keywords,
509 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
510 bool is_type = false;
511 if (kw == NULL) {
512 if (is_typename()) {
513 is_type = true;
514 ps.next_unary = true;
515 goto found_typename;
516 }
517
518 } else { /* we have a keyword */
519 is_type = kw->lsym == lsym_type;
520 ps.next_unary = true;
521 if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
522 return kw->lsym;
523
524 found_typename:
525 if (ps.p_l_follow > 0) {
526 /* inside parentheses: cast, param list, offsetof or sizeof */
527 ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask;
528 }
529 if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
530 if (kw != NULL && kw->lsym == lsym_tag)
531 return lsym_tag;
532 if (ps.p_l_follow == 0)
533 return lsym_type_outside_parentheses;
534 }
535 }
536
537 if (inp_peek() == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
538 !ps.in_func_def_params && !ps.block_init) {
539
540 if (ps.p_l_follow == 0 && probably_looking_at_definition()) {
541 ps.is_function_definition = true;
542 if (ps.in_decl)
543 ps.in_func_def_params = true;
544 return lsym_funcname;
545 }
546
547 } else if (ps.p_l_follow == 0 && probably_typename()) {
548 ps.next_unary = true;
549 return lsym_type_outside_parentheses;
550 }
551
552 return is_type ? lsym_type_in_parentheses : lsym_word;
553 }
554
555 static bool
556 is_asterisk_unary(void)
557 {
558 if (ps.next_unary || ps.in_func_def_params)
559 return true;
560 if (ps.prev_token == lsym_word ||
561 ps.prev_token == lsym_rparen_or_rbracket)
562 return false;
563 return ps.in_decl && ps.p_l_follow > 0;
564 }
565
566 static void
567 lex_asterisk_unary(void)
568 {
569 while (inp_peek() == '*' || ch_isspace(inp_peek())) {
570 if (inp_peek() == '*')
571 token_add_char('*');
572 inp_skip();
573 }
574
575 if (ps.in_decl) {
576 const char *tp = inp_p(), *e = inp_line_end();
577
578 while (tp < e) {
579 if (ch_isspace(*tp))
580 tp++;
581 else if (is_identifier_start(*tp)) {
582 tp++;
583 while (tp < e && is_identifier_part(*tp))
584 tp++;
585 } else
586 break;
587 }
588
589 if (tp < e && *tp == '(')
590 ps.is_function_definition = true;
591 }
592 }
593
594 /* Reads the next token, placing it in the global variable "token". */
595 lexer_symbol
596 lexi(void)
597 {
598 token.e = token.s;
599 ps.curr_col_1 = ps.next_col_1;
600 ps.next_col_1 = false;
601
602 while (ch_isblank(inp_peek())) {
603 ps.curr_col_1 = false;
604 inp_skip();
605 }
606
607 lexer_symbol alnum_lsym = lexi_alnum();
608 if (alnum_lsym != lsym_eof)
609 return lexi_end(alnum_lsym);
610
611 /* Scan a non-alphanumeric token */
612
613 check_size_token(3); /* for things like "<<=" */
614 *token.e++ = inp_next();
615 *token.e = '\0';
616
617 lexer_symbol lsym;
618 bool next_unary;
619
620 switch (token.e[-1]) {
621
622 /* INDENT OFF */
623 case '(':
624 case '[': lsym = lsym_lparen_or_lbracket; next_unary = true; break;
625 case ')':
626 case ']': lsym = lsym_rparen_or_rbracket; next_unary = false; break;
627 case '?': lsym = lsym_question; next_unary = true; break;
628 case ':': lsym = lsym_colon; next_unary = true; break;
629 case ';': lsym = lsym_semicolon; next_unary = true; break;
630 case '{': lsym = lsym_lbrace; next_unary = true; break;
631 case '}': lsym = lsym_rbrace; next_unary = true; break;
632 case ',': lsym = lsym_comma; next_unary = true; break;
633 case '.': lsym = lsym_period; next_unary = false; break;
634 /* INDENT ON */
635
636 case '\n':
637 /* if data has been exhausted, the '\n' is a dummy. */
638 lsym = had_eof ? lsym_eof : lsym_newline;
639 next_unary = ps.next_unary;
640 ps.next_col_1 = true;
641 break;
642
643 case '\f':
644 lsym = lsym_form_feed;
645 next_unary = ps.next_unary;
646 ps.next_col_1 = true;
647 break;
648
649 case '#':
650 lsym = lsym_preprocessing;
651 next_unary = ps.next_unary;
652 break;
653
654 case '\'':
655 case '"':
656 lex_char_or_string();
657 lsym = lsym_word;
658 next_unary = false;
659 break;
660
661 case '-':
662 case '+':
663 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
664 next_unary = true;
665
666 if (inp_peek() == token.e[-1]) { /* '++' or '--' */
667 *token.e++ = inp_next();
668 if (ps.prev_token == lsym_word ||
669 ps.prev_token == lsym_rparen_or_rbracket) {
670 lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
671 next_unary = false;
672 }
673
674 } else if (inp_peek() == '=') { /* '+=' or '-=' */
675 *token.e++ = inp_next();
676
677 } else if (inp_peek() == '>') { /* '->' */
678 *token.e++ = inp_next();
679 lsym = lsym_unary_op;
680 next_unary = false;
681 ps.want_blank = false;
682 }
683 break;
684
685 case '=':
686 if (ps.init_or_struct)
687 ps.block_init = true;
688 if (inp_peek() == '=') { /* == */
689 *token.e++ = inp_next();
690 *token.e = '\0';
691 }
692 lsym = lsym_binary_op;
693 next_unary = true;
694 break;
695
696 case '>':
697 case '<':
698 case '!': /* ops like <, <<, <=, !=, etc */
699 if (inp_peek() == '>' || inp_peek() == '<' || inp_peek() == '=')
700 *token.e++ = inp_next();
701 if (inp_peek() == '=')
702 *token.e++ = inp_next();
703 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
704 next_unary = true;
705 break;
706
707 case '*':
708 if (is_asterisk_unary()) {
709 lex_asterisk_unary();
710 lsym = lsym_unary_op;
711 next_unary = true;
712 } else {
713 if (inp_peek() == '=')
714 *token.e++ = inp_next();
715 lsym = lsym_binary_op;
716 next_unary = true;
717 }
718 break;
719
720 default:
721 if (token.e[-1] == '/' && (inp_peek() == '*' || inp_peek() == '/')) {
722 *token.e++ = inp_next();
723 lsym = lsym_comment;
724 next_unary = ps.next_unary;
725 break;
726 }
727
728 /* handle '||', '&&', etc., and also things as in 'int *****i' */
729 while (token.e[-1] == inp_peek() || inp_peek() == '=')
730 token_add_char(inp_next());
731
732 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
733 next_unary = true;
734 }
735
736 ps.next_unary = next_unary;
737
738 check_size_token(1);
739 *token.e = '\0';
740
741 return lexi_end(lsym);
742 }
743
744 void
745 register_typename(const char *name)
746 {
747 if (typenames.len >= typenames.cap) {
748 typenames.cap = 16 + 2 * typenames.cap;
749 typenames.items = xrealloc(typenames.items,
750 sizeof(typenames.items[0]) * typenames.cap);
751 }
752
753 int pos = bsearch_typenames(name);
754 if (pos >= 0)
755 return; /* already in the list */
756
757 pos = -(pos + 1);
758 memmove(typenames.items + pos + 1, typenames.items + pos,
759 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
760 typenames.items[pos] = xstrdup(name);
761 }
762