lexi.c revision 1.174 1 /* $NetBSD: lexi.c,v 1.174 2023/05/11 10:51:34 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.174 2023/05/11 10:51:34 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 "sizeof",
223 "offsetof",
224 "word",
225 "funcname",
226 "do",
227 "else",
228 "for",
229 "if",
230 "switch",
231 "while",
232 "return",
233 };
234
235 return name[sym];
236 }
237
238 static void
239 debug_print_buf(const char *name, const struct buffer *buf)
240 {
241 if (buf->s < buf->e) {
242 debug_printf("%s ", name);
243 debug_vis_range("\"", buf->s, buf->e, "\"\n");
244 }
245 }
246
247 static bool
248 debug_full_parser_state(void)
249 {
250 return true;
251 }
252
253 #define debug_ps_bool(name) \
254 if (ps.name != prev_ps.name) \
255 debug_println("[%c] -> [%c] ps." #name, \
256 prev_ps.name ? 'x' : ' ', ps.name ? 'x' : ' '); \
257 else if (debug_full_parser_state()) \
258 debug_println(" [%c] ps." #name, ps.name ? 'x' : ' ')
259 #define debug_ps_int(name) \
260 if (ps.name != prev_ps.name) \
261 debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
262 else if (debug_full_parser_state()) \
263 debug_println(" %3d ps." #name, ps.name)
264
265 static bool
266 ps_paren_has_changed(const struct parser_state *prev_ps)
267 {
268 const paren_level_props *prev = prev_ps->paren, *curr = ps.paren;
269
270 if (prev_ps->nparen != ps.nparen)
271 return true;
272
273 for (int i = 0; i < ps.nparen; i++) {
274 if (curr[i].indent != prev[i].indent ||
275 curr[i].maybe_cast != prev[i].maybe_cast ||
276 curr[i].no_cast != prev[i].no_cast)
277 return true;
278 }
279 return false;
280 }
281
282 static void
283 debug_ps_paren(const struct parser_state *prev_ps)
284 {
285 if (!debug_full_parser_state() && !ps_paren_has_changed(prev_ps))
286 return;
287
288 debug_printf(" ps.paren:");
289 for (int i = 0; i < ps.nparen; i++) {
290 const paren_level_props *props = ps.paren + i;
291 const char *cast = props->no_cast ? "(no cast)"
292 : props->maybe_cast ? "(cast)"
293 : "";
294 debug_printf(" %s%d", cast, props->indent);
295 }
296 if (ps.nparen == 0)
297 debug_printf(" none");
298 debug_println("");
299 }
300
301 static void
302 debug_lexi(lexer_symbol lsym)
303 {
304 /*
305 * Watch out for 'rolled back parser state' in the debug output; the
306 * differences around these are unreliable.
307 */
308 static struct parser_state prev_ps;
309
310 debug_println("");
311 debug_printf("line %d: %s", line_no, lsym_name(lsym));
312 debug_vis_range(" \"", token.s, token.e, "\"\n");
313
314 debug_print_buf("label", &lab);
315 debug_print_buf("code", &code);
316 debug_print_buf("comment", &com);
317
318 debug_println(" ps.prev_token = %s", lsym_name(ps.prev_token));
319 debug_ps_bool(next_col_1);
320 debug_ps_bool(curr_col_1);
321 debug_ps_bool(next_unary);
322 debug_ps_bool(is_function_definition);
323 debug_ps_bool(want_blank);
324 debug_ps_bool(force_nl);
325 debug_ps_int(line_start_nparen);
326 debug_ps_int(nparen);
327 debug_ps_paren(&prev_ps);
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_level);
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_func_def_params);
345 debug_ps_bool(decl_indent_done);
346
347 debug_ps_bool(in_stmt_or_decl);
348 debug_ps_bool(in_stmt_cont);
349 debug_ps_bool(is_case_label);
350
351 prev_ps = ps;
352 }
353 #endif
354
355 static lexer_symbol
356 lexi_end(lexer_symbol lsym)
357 {
358 #ifdef debug
359 debug_lexi(lsym);
360 #endif
361 return lsym;
362 }
363
364 static void
365 lex_number(void)
366 {
367 for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
368 unsigned char ch = (unsigned char)inp_peek();
369 if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
370 break;
371
372 unsigned char row = lex_number_row[ch];
373 if (lex_number_state[row][s - 'A'] == ' ') {
374 /*-
375 * lex_number_state[0][s - 'A'] now indicates the type:
376 * f = floating, i = integer, u = unknown
377 */
378 return;
379 }
380
381 s = lex_number_state[row][s - 'A'];
382 token_add_char(inp_next());
383 }
384 }
385
386 static bool
387 is_identifier_start(char ch)
388 {
389 return ch_isalpha(ch) || ch == '_' || ch == '$';
390 }
391
392 static bool
393 is_identifier_part(char ch)
394 {
395 return ch_isalnum(ch) || ch == '_' || ch == '$';
396 }
397
398 static void
399 lex_word(void)
400 {
401 for (;;) {
402 if (is_identifier_part(inp_peek()))
403 token_add_char(inp_next());
404 else if (inp_peek() == '\\' && inp_lookahead(1) == '\n') {
405 inp_skip();
406 inp_skip();
407 } else
408 return;
409 }
410 }
411
412 static void
413 lex_char_or_string(void)
414 {
415 for (char delim = token.e[-1];;) {
416 if (inp_peek() == '\n') {
417 diag(1, "Unterminated literal");
418 return;
419 }
420
421 token_add_char(inp_next());
422 if (token.e[-1] == delim)
423 return;
424
425 if (token.e[-1] == '\\') {
426 if (inp_peek() == '\n')
427 ++line_no;
428 token_add_char(inp_next());
429 }
430 }
431 }
432
433 /* Guess whether the current token is a declared type. */
434 static bool
435 probably_typename(void)
436 {
437 if (ps.prev_token == lsym_storage_class)
438 return true;
439 if (ps.block_init)
440 return false;
441 if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */
442 return false;
443 if (inp_peek() == '*' && inp_lookahead(1) != '=')
444 goto maybe;
445 /* XXX: is_identifier_start */
446 if (ch_isalpha(inp_peek()))
447 goto maybe;
448 return false;
449 maybe:
450 return ps.prev_token == lsym_semicolon ||
451 ps.prev_token == lsym_lbrace ||
452 ps.prev_token == lsym_rbrace;
453 }
454
455 static int
456 bsearch_typenames(const char *key)
457 {
458 const char **arr = typenames.items;
459 int lo = 0;
460 int hi = (int)typenames.len - 1;
461
462 while (lo <= hi) {
463 int mid = (int)((unsigned)(lo + hi) >> 1);
464 int cmp = strcmp(arr[mid], key);
465 if (cmp < 0)
466 lo = mid + 1;
467 else if (cmp > 0)
468 hi = mid - 1;
469 else
470 return mid;
471 }
472 return -(lo + 1);
473 }
474
475 static bool
476 is_typename(void)
477 {
478 if (opt.auto_typedefs &&
479 token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
480 return true;
481
482 return bsearch_typenames(token.s) >= 0;
483 }
484
485 static int
486 cmp_keyword_by_name(const void *key, const void *elem)
487 {
488 return strcmp(key, ((const struct keyword *)elem)->name);
489 }
490
491 /*
492 * Looking at something like 'function_name(...)' in a line, guess whether
493 * this starts a function definition or a declaration.
494 */
495 static bool
496 probably_looking_at_definition(void)
497 {
498 int paren_level = 0;
499 for (const char *p = inp_p(), *e = inp_line_end(); p < e; p++) {
500 if (*p == '(')
501 paren_level++;
502 if (*p == ')' && --paren_level == 0) {
503 p++;
504
505 while (p < e && (ch_isspace(*p) || is_identifier_part(*p)))
506 p++; /* '__dead' or '__unused' */
507
508 if (p == e) /* func(...) */
509 break;
510 if (*p == ';') /* func(...); */
511 return false;
512 if (*p == ',') /* double abs(), pi; */
513 return false;
514 if (*p == '(') /* func(...) __attribute__((...)) */
515 paren_level++; /* func(...) __printflike(...) */
516 else
517 break; /* func(...) { ... */
518 }
519 }
520
521 /*
522 * To further reduce the cases where indent wrongly treats an incomplete
523 * function declaration as a function definition, thus adding a newline
524 * before the function name, it may be worth looking for parameter names,
525 * as these are often omitted in function declarations and only included
526 * in function definitions. Or just increase the lookahead to more than
527 * just the current line of input, until the next '{'.
528 */
529 return true;
530 }
531
532 /* Read an alphanumeric token into 'token', or return lsym_eof. */
533 static lexer_symbol
534 lexi_alnum(void)
535 {
536 if (ch_isdigit(inp_peek()) ||
537 (inp_peek() == '.' && ch_isdigit(inp_lookahead(1)))) {
538 lex_number();
539 } else if (is_identifier_start(inp_peek())) {
540 lex_word();
541
542 if (token.s[0] == 'L' && token.e - token.s == 1 &&
543 (inp_peek() == '"' || inp_peek() == '\'')) {
544 token_add_char(inp_next());
545 lex_char_or_string();
546 ps.next_unary = false;
547
548 check_size_token(1);
549 *token.e = '\0';
550
551 return lsym_word;
552 }
553 } else
554 return lsym_eof; /* just as a placeholder */
555
556 *token.e = '\0';
557
558 while (ch_isblank(inp_peek()))
559 inp_skip();
560
561 ps.next_unary = ps.prev_token == lsym_tag; /* for 'struct s *' */
562
563 if (ps.prev_token == lsym_tag && ps.nparen == 0)
564 return lsym_type_outside_parentheses;
565
566 const struct keyword *kw = bsearch(token.s, keywords,
567 array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
568 bool is_type = false;
569 if (kw == NULL) {
570 if (is_typename()) {
571 is_type = true;
572 ps.next_unary = true;
573 if (ps.in_enum == in_enum_enum)
574 ps.in_enum = in_enum_type;
575 goto found_typename;
576 }
577
578 } else { /* we have a keyword */
579 is_type = kw->lsym == lsym_type;
580 ps.next_unary = true;
581 if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
582 return kw->lsym;
583
584 found_typename:
585 if (ps.nparen > 0) {
586 /* inside parentheses: cast, param list, offsetof or sizeof */
587 if (!ps.paren[ps.nparen - 1].no_cast)
588 ps.paren[ps.nparen - 1].maybe_cast = true;
589 }
590 if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
591 if (kw != NULL && kw->lsym == lsym_tag) {
592 if (token.s[0] == 'e' /* enum */)
593 ps.in_enum = in_enum_enum;
594 return lsym_tag;
595 }
596 if (ps.nparen == 0)
597 return lsym_type_outside_parentheses;
598 }
599 }
600
601 if (inp_peek() == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
602 !ps.in_func_def_params && !ps.block_init) {
603
604 if (ps.nparen == 0 && probably_looking_at_definition()) {
605 ps.is_function_definition = true;
606 if (ps.in_decl)
607 ps.in_func_def_params = true;
608 return lsym_funcname;
609 }
610
611 } else if (ps.nparen == 0 && probably_typename()) {
612 ps.next_unary = true;
613 return lsym_type_outside_parentheses;
614 }
615
616 return is_type ? lsym_type_in_parentheses : lsym_word;
617 }
618
619 static bool
620 is_asterisk_unary(void)
621 {
622 if (ps.next_unary || ps.in_func_def_params)
623 return true;
624 if (ps.prev_token == lsym_word ||
625 ps.prev_token == lsym_rparen_or_rbracket)
626 return false;
627 return ps.in_decl && ps.nparen > 0;
628 }
629
630 static void
631 lex_asterisk_unary(void)
632 {
633 while (inp_peek() == '*' || ch_isspace(inp_peek())) {
634 if (inp_peek() == '*')
635 token_add_char('*');
636 inp_skip();
637 }
638
639 if (ps.in_decl) {
640 const char *tp = inp_p(), *e = inp_line_end();
641
642 while (tp < e) {
643 if (ch_isspace(*tp))
644 tp++;
645 else if (is_identifier_start(*tp)) {
646 tp++;
647 while (tp < e && is_identifier_part(*tp))
648 tp++;
649 } else
650 break;
651 }
652
653 if (tp < e && *tp == '(')
654 ps.is_function_definition = true;
655 }
656 }
657
658 /* Reads the next token, placing it in the global variable "token". */
659 lexer_symbol
660 lexi(void)
661 {
662 token.e = token.s;
663 ps.curr_col_1 = ps.next_col_1;
664 ps.next_col_1 = false;
665
666 while (ch_isblank(inp_peek())) {
667 ps.curr_col_1 = false;
668 inp_skip();
669 }
670
671 lexer_symbol alnum_lsym = lexi_alnum();
672 if (alnum_lsym != lsym_eof)
673 return lexi_end(alnum_lsym);
674
675 /* Scan a non-alphanumeric token */
676
677 check_size_token(3); /* for things like "<<=" */
678 *token.e++ = inp_next();
679 *token.e = '\0';
680
681 lexer_symbol lsym;
682 bool next_unary;
683
684 switch (token.e[-1]) {
685
686 /* INDENT OFF */
687 case '(':
688 case '[': lsym = lsym_lparen_or_lbracket; next_unary = true; break;
689 case ')':
690 case ']': lsym = lsym_rparen_or_rbracket; next_unary = false; break;
691 case '?': lsym = lsym_question; next_unary = true; break;
692 case ':': lsym = lsym_colon; next_unary = true; break;
693 case ';': lsym = lsym_semicolon; next_unary = true; break;
694 case '{': lsym = lsym_lbrace; next_unary = true; break;
695 case '}': lsym = lsym_rbrace; next_unary = true; break;
696 case ',': lsym = lsym_comma; next_unary = true; break;
697 case '.': lsym = lsym_period; next_unary = false; break;
698 /* INDENT ON */
699
700 case '\n':
701 /* if data has been exhausted, the '\n' is a dummy. */
702 lsym = had_eof ? lsym_eof : lsym_newline;
703 next_unary = ps.next_unary;
704 ps.next_col_1 = true;
705 break;
706
707 case '\f':
708 lsym = lsym_form_feed;
709 next_unary = ps.next_unary;
710 ps.next_col_1 = true;
711 break;
712
713 case '#':
714 lsym = lsym_preprocessing;
715 next_unary = ps.next_unary;
716 break;
717
718 case '\'':
719 case '"':
720 lex_char_or_string();
721 lsym = lsym_word;
722 next_unary = false;
723 break;
724
725 case '-':
726 case '+':
727 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
728 next_unary = true;
729
730 if (inp_peek() == token.e[-1]) { /* '++' or '--' */
731 *token.e++ = inp_next();
732 if (ps.prev_token == lsym_word ||
733 ps.prev_token == lsym_rparen_or_rbracket) {
734 lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
735 next_unary = false;
736 }
737
738 } else if (inp_peek() == '=') { /* '+=' or '-=' */
739 *token.e++ = inp_next();
740
741 } else if (inp_peek() == '>') { /* '->' */
742 *token.e++ = inp_next();
743 lsym = lsym_unary_op;
744 next_unary = false;
745 ps.want_blank = false;
746 }
747 break;
748
749 case '=':
750 if (ps.init_or_struct)
751 ps.block_init = true;
752 if (inp_peek() == '=') { /* == */
753 *token.e++ = inp_next();
754 *token.e = '\0';
755 }
756 lsym = lsym_binary_op;
757 next_unary = true;
758 break;
759
760 case '>':
761 case '<':
762 case '!': /* ops like <, <<, <=, !=, etc */
763 if (inp_peek() == '>' || inp_peek() == '<' || inp_peek() == '=')
764 *token.e++ = inp_next();
765 if (inp_peek() == '=')
766 *token.e++ = inp_next();
767 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
768 next_unary = true;
769 break;
770
771 case '*':
772 if (is_asterisk_unary()) {
773 lex_asterisk_unary();
774 lsym = lsym_unary_op;
775 next_unary = true;
776 } else {
777 if (inp_peek() == '=')
778 *token.e++ = inp_next();
779 lsym = lsym_binary_op;
780 next_unary = true;
781 }
782 break;
783
784 default:
785 if (token.e[-1] == '/' && (inp_peek() == '*' || inp_peek() == '/')) {
786 *token.e++ = inp_next();
787 lsym = lsym_comment;
788 next_unary = ps.next_unary;
789 break;
790 }
791
792 /* handle '||', '&&', etc., and also things as in 'int *****i' */
793 while (token.e[-1] == inp_peek() || inp_peek() == '=')
794 token_add_char(inp_next());
795
796 lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
797 next_unary = true;
798 }
799
800 if (ps.in_enum == in_enum_enum || ps.in_enum == in_enum_type)
801 ps.in_enum = lsym == lsym_lbrace ? in_enum_brace : in_enum_no;
802 if (lsym == lsym_rbrace)
803 ps.in_enum = in_enum_no;
804
805 ps.next_unary = next_unary;
806
807 check_size_token(1);
808 *token.e = '\0';
809
810 return lexi_end(lsym);
811 }
812
813 void
814 register_typename(const char *name)
815 {
816 if (typenames.len >= typenames.cap) {
817 typenames.cap = 16 + 2 * typenames.cap;
818 typenames.items = xrealloc(typenames.items,
819 sizeof(typenames.items[0]) * typenames.cap);
820 }
821
822 int pos = bsearch_typenames(name);
823 if (pos >= 0)
824 return; /* already in the list */
825
826 pos = -(pos + 1);
827 memmove(typenames.items + pos + 1, typenames.items + pos,
828 sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
829 typenames.items[pos] = xstrdup(name);
830 }
831