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