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