Home | History | Annotate | Line # | Download | only in indent
lexi.c revision 1.134
      1 /*	$NetBSD: lexi.c,v 1.134 2021/11/07 07:35:06 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.134 2021/11/07 07:35:06 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_word},
     74     {"case", lsym_case_label},
     75     {"char", lsym_type},
     76     {"complex", lsym_type},
     77     {"const", lsym_type},
     78     {"continue", lsym_word},
     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_word},
     88     {"if", lsym_if},
     89     {"imaginary", lsym_type},
     90     {"inline", lsym_word},
     91     {"int", lsym_type},
     92     {"long", lsym_type},
     93     {"offsetof", lsym_offsetof},
     94     {"register", lsym_storage_class},
     95     {"restrict", lsym_word},
     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 inp_peek(void)
    183 {
    184     return *inp.s;
    185 }
    186 
    187 void
    188 inp_skip(void)
    189 {
    190     inp.s++;
    191     if (inp.s >= inp.e)
    192 	inp_read_line();
    193 }
    194 
    195 char
    196 inp_next(void)
    197 {
    198     char ch = inp_peek();
    199     inp_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 	"type_in_parentheses",
    243 	"tag",
    244 	"case_label",
    245 	"string_prefix",
    246 	"sizeof",
    247 	"offsetof",
    248 	"word",
    249 	"funcname",
    250 	"do",
    251 	"else",
    252 	"for",
    253 	"if",
    254 	"switch",
    255 	"while",
    256 	"return",
    257     };
    258 
    259     return name[sym];
    260 }
    261 
    262 static void
    263 debug_print_buf(const char *name, const struct buffer *buf)
    264 {
    265     if (buf->s < buf->e) {
    266 	debug_printf("%s ", name);
    267 	debug_vis_range("\"", buf->s, buf->e, "\"\n");
    268     }
    269 }
    270 
    271 #define debug_ps_bool(name) \
    272         if (ps.name != prev_ps.name) \
    273 	    debug_println("[%c] ps." #name, ps.name ? 'x' : ' ')
    274 #define debug_ps_int(name) \
    275 	if (ps.name != prev_ps.name) \
    276 	    debug_println("%3d ps." #name, ps.name)
    277 
    278 static void
    279 debug_lexi(lexer_symbol lsym)
    280 {
    281     /*
    282      * Watch out for 'rolled back parser state' in the debug output; the
    283      * differences around these are unreliable.
    284      */
    285     static struct parser_state prev_ps;
    286 
    287     debug_println("");
    288     debug_printf("line %d: %s", line_no, lsym_name(lsym));
    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(next_col_1);
    297     debug_ps_bool(curr_col_1);
    298     debug_ps_bool(next_unary);
    299     if (strcmp(ps.procname, prev_ps.procname) != 0)
    300 	debug_println("    ps.procname = '%s'", ps.procname);
    301     debug_ps_bool(want_blank);
    302     debug_ps_int(paren_level);
    303     debug_ps_int(p_l_follow);
    304     if (ps.paren_level != prev_ps.paren_level) {
    305 	debug_printf("    ps.paren_indents:");
    306 	for (int i = 0; i < ps.paren_level; i++)
    307 	    debug_printf(" %d", ps.paren_indents[i]);
    308 	debug_println("");
    309     }
    310     debug_ps_int(cast_mask);
    311     debug_ps_int(not_cast_mask);
    312 
    313     debug_ps_int(comment_delta);
    314     debug_ps_int(n_comment_delta);
    315     debug_ps_int(com_ind);
    316 
    317     debug_ps_bool(block_init);
    318     debug_ps_int(block_init_level);
    319     debug_ps_bool(init_or_struct);
    320 
    321     debug_ps_int(ind_level);
    322     debug_ps_int(ind_level_follow);
    323 
    324     debug_ps_int(decl_nest);
    325     debug_ps_bool(decl_on_line);
    326     debug_ps_bool(in_decl);
    327     debug_ps_int(just_saw_decl);
    328     debug_ps_bool(in_parameter_declaration);
    329     debug_ps_bool(decl_indent_done);
    330 
    331     debug_ps_bool(in_stmt);
    332     debug_ps_bool(ind_stmt);
    333     debug_ps_bool(is_case_label);
    334 
    335     debug_ps_bool(search_stmt);
    336 
    337     prev_ps = ps;
    338 }
    339 #endif
    340 
    341 /* ARGSUSED */
    342 static lexer_symbol
    343 lexi_end(lexer_symbol lsym)
    344 {
    345 #ifdef debug
    346     debug_lexi(lsym);
    347 #endif
    348     return lsym;
    349 }
    350 
    351 static void
    352 lex_number(void)
    353 {
    354     for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
    355 	unsigned char ch = (unsigned char)*inp.s;
    356 	if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
    357 	    break;
    358 
    359 	unsigned char row = lex_number_row[ch];
    360 	if (lex_number_state[row][s - 'A'] == ' ') {
    361 	    /*-
    362 	     * lex_number_state[0][s - 'A'] now indicates the type:
    363 	     * f = floating, i = integer, u = unknown
    364 	     */
    365 	    break;
    366 	}
    367 
    368 	s = lex_number_state[row][s - 'A'];
    369 	token_add_char(inp_next());
    370     }
    371 }
    372 
    373 static void
    374 lex_word(void)
    375 {
    376     while (isalnum((unsigned char)*inp.s) ||
    377 	    *inp.s == '\\' ||
    378 	    *inp.s == '_' || *inp.s == '$') {
    379 
    380 	if (*inp.s == '\\') {
    381 	    if (inp.s[1] == '\n') {
    382 		inp.s += 2;
    383 		if (inp.s >= inp.e)
    384 		    inp_read_line();
    385 	    } else
    386 		break;
    387 	}
    388 
    389 	token_add_char(inp_next());
    390     }
    391 }
    392 
    393 static void
    394 lex_char_or_string(void)
    395 {
    396     for (char delim = token.e[-1];;) {
    397 	if (*inp.s == '\n') {
    398 	    diag(1, "Unterminated literal");
    399 	    return;
    400 	}
    401 
    402 	token_add_char(inp_next());
    403 	if (token.e[-1] == delim)
    404 	    return;
    405 
    406 	if (token.e[-1] == '\\') {
    407 	    if (*inp.s == '\n')
    408 		++line_no;
    409 	    token_add_char(inp_next());
    410 	}
    411     }
    412 }
    413 
    414 /* Guess whether the current token is a declared type. */
    415 static bool
    416 probably_typename(void)
    417 {
    418     if (ps.p_l_follow > 0)
    419 	return false;
    420     if (ps.block_init || ps.in_stmt)
    421 	return false;
    422     if (inp.s[0] == '*' && inp.s[1] != '=')
    423 	goto maybe;
    424     if (isalpha((unsigned char)*inp.s))
    425 	goto maybe;
    426     return false;
    427 maybe:
    428     return ps.prev_token == lsym_semicolon ||
    429 	ps.prev_token == lsym_lbrace ||
    430 	ps.prev_token == lsym_rbrace;
    431 }
    432 
    433 static int
    434 bsearch_typenames(const char *key)
    435 {
    436     const char **arr = typenames.items;
    437     int lo = 0;
    438     int hi = (int)typenames.len - 1;
    439 
    440     while (lo <= hi) {
    441 	int mid = (int)((unsigned)(lo + hi) >> 1);
    442 	int cmp = strcmp(arr[mid], key);
    443 	if (cmp < 0)
    444 	    lo = mid + 1;
    445 	else if (cmp > 0)
    446 	    hi = mid - 1;
    447 	else
    448 	    return mid;
    449     }
    450     return -(lo + 1);
    451 }
    452 
    453 static bool
    454 is_typename(void)
    455 {
    456     if (opt.auto_typedefs &&
    457 	token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
    458 	return true;
    459 
    460     return bsearch_typenames(token.s) >= 0;
    461 }
    462 
    463 static int
    464 cmp_keyword_by_name(const void *key, const void *elem)
    465 {
    466     return strcmp(key, ((const struct keyword *)elem)->name);
    467 }
    468 
    469 /* Read an alphanumeric token into 'token', or return end_of_file. */
    470 static lexer_symbol
    471 lexi_alnum(void)
    472 {
    473     if (isdigit((unsigned char)*inp.s) ||
    474 	(inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))) {
    475 	lex_number();
    476     } else if (isalnum((unsigned char)*inp.s) ||
    477 	    *inp.s == '_' || *inp.s == '$') {
    478 	lex_word();
    479     } else
    480 	return lsym_eof;	/* just as a placeholder */
    481 
    482     *token.e = '\0';
    483 
    484     if (token.s[0] == 'L' && token.s[1] == '\0' &&
    485 	(*inp.s == '"' || *inp.s == '\''))
    486 	return lsym_string_prefix;
    487 
    488     while (ch_isblank(inp_peek()))
    489 	inp_skip();
    490 
    491     if (ps.prev_token == lsym_tag && ps.p_l_follow == 0) {
    492 	ps.next_unary = true;
    493 	return lsym_type_at_paren_level_0;
    494     }
    495 
    496     /* Operator after identifier is binary unless last token was 'struct'. */
    497     ps.next_unary = ps.prev_token == lsym_tag;
    498 
    499     const struct keyword *kw = bsearch(token.s, keywords,
    500 	array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
    501     bool is_type = false;
    502     if (kw == NULL) {
    503 	if (is_typename()) {
    504 	    is_type = true;
    505 	    ps.next_unary = true;
    506 	    goto found_typename;
    507 	}
    508 
    509     } else {			/* we have a keyword */
    510 	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.next_unary = true;
    543 	return lsym_type_at_paren_level_0;
    544     }
    545 
    546     return is_type ? lsym_type_in_parentheses : lsym_word;
    547 }
    548 
    549 /* Reads the next token, placing it in the global variable "token". */
    550 lexer_symbol
    551 lexi(void)
    552 {
    553     token.e = token.s;
    554     ps.curr_col_1 = ps.next_col_1;
    555     ps.next_col_1 = false;
    556 
    557     while (ch_isblank(*inp.s)) {
    558 	ps.curr_col_1 = false;
    559 	inp_skip();
    560     }
    561 
    562     lexer_symbol alnum_lsym = lexi_alnum();
    563     if (alnum_lsym != lsym_eof)
    564 	return lexi_end(alnum_lsym);
    565 
    566     /* Scan a non-alphanumeric token */
    567 
    568     check_size_token(3);	/* for things like "<<=" */
    569     *token.e++ = inp_next();
    570     *token.e = '\0';
    571 
    572     lexer_symbol lsym;
    573     bool unary_delim = false;	/* whether the current token forces a
    574 				 * following operator to be unary */
    575 
    576     switch (token.e[-1]) {
    577     case '\n':
    578 	unary_delim = ps.next_unary;
    579 	ps.next_col_1 = true;
    580 	/* if data has been exhausted, the newline is a dummy. */
    581 	lsym = had_eof ? lsym_eof : lsym_newline;
    582 	break;
    583 
    584     case '\'':
    585     case '"':
    586 	lex_char_or_string();
    587 	lsym = lsym_word;
    588 	break;
    589 
    590     case '(':
    591     case '[':
    592 	unary_delim = true;
    593 	lsym = lsym_lparen_or_lbracket;
    594 	break;
    595 
    596     case ')':
    597     case ']':
    598 	lsym = lsym_rparen_or_rbracket;
    599 	break;
    600 
    601     case '#':
    602 	unary_delim = ps.next_unary;
    603 	lsym = lsym_preprocessing;
    604 	break;
    605 
    606     case '?':
    607 	unary_delim = true;
    608 	lsym = lsym_question;
    609 	break;
    610 
    611     case ':':
    612 	lsym = lsym_colon;
    613 	unary_delim = true;
    614 	break;
    615 
    616     case ';':
    617 	unary_delim = true;
    618 	lsym = lsym_semicolon;
    619 	break;
    620 
    621     case '{':
    622 	unary_delim = true;
    623 	lsym = lsym_lbrace;
    624 	break;
    625 
    626     case '}':
    627 	unary_delim = true;
    628 	lsym = lsym_rbrace;
    629 	break;
    630 
    631     case '\f':
    632 	unary_delim = ps.next_unary;
    633 	ps.next_col_1 = true;
    634 	lsym = lsym_form_feed;
    635 	break;
    636 
    637     case ',':
    638 	unary_delim = true;
    639 	lsym = lsym_comma;
    640 	break;
    641 
    642     case '.':
    643 	unary_delim = false;
    644 	lsym = lsym_period;
    645 	break;
    646 
    647     case '-':
    648     case '+':
    649 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    650 	unary_delim = true;
    651 
    652 	if (*inp.s == token.e[-1]) {	/* ++, -- */
    653 	    *token.e++ = *inp.s++;
    654 	    if (ps.prev_token == lsym_word ||
    655 		    ps.prev_token == lsym_rparen_or_rbracket) {
    656 		lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
    657 		unary_delim = false;
    658 	    }
    659 
    660 	} else if (*inp.s == '=') {	/* += */
    661 	    *token.e++ = *inp.s++;
    662 
    663 	} else if (*inp.s == '>') {	/* -> */
    664 	    *token.e++ = *inp.s++;
    665 	    unary_delim = false;
    666 	    lsym = lsym_unary_op;
    667 	    ps.want_blank = false;
    668 	}
    669 	break;
    670 
    671     case '=':
    672 	if (ps.init_or_struct)
    673 	    ps.block_init = true;
    674 	if (*inp.s == '=') {	/* == */
    675 	    *token.e++ = *inp.s++;
    676 	    *token.e = '\0';
    677 	}
    678 	lsym = lsym_binary_op;
    679 	unary_delim = true;
    680 	break;
    681 
    682     case '>':
    683     case '<':
    684     case '!':			/* ops like <, <<, <=, !=, etc */
    685 	if (*inp.s == '>' || *inp.s == '<' || *inp.s == '=')
    686 	    *token.e++ = inp_next();
    687 	if (*inp.s == '=')
    688 	    *token.e++ = *inp.s++;
    689 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    690 	unary_delim = true;
    691 	break;
    692 
    693     case '*':
    694 	unary_delim = true;
    695 	if (!ps.next_unary) {
    696 	    if (*inp.s == '=')
    697 		*token.e++ = *inp.s++;
    698 	    lsym = lsym_binary_op;
    699 	    break;
    700 	}
    701 
    702 	while (*inp.s == '*' || isspace((unsigned char)*inp.s)) {
    703 	    if (*inp.s == '*')
    704 		token_add_char('*');
    705 	    inp_skip();
    706 	}
    707 
    708 	if (ps.in_decl) {
    709 	    char *tp = inp.s;
    710 
    711 	    while (isalpha((unsigned char)*tp) ||
    712 		    isspace((unsigned char)*tp)) {
    713 		if (++tp >= inp.e)
    714 		    inp_read_line();
    715 	    }
    716 	    if (*tp == '(')
    717 		ps.procname[0] = ' ';
    718 	}
    719 
    720 	lsym = lsym_unary_op;
    721 	break;
    722 
    723     default:
    724 	if (token.e[-1] == '/' && (*inp.s == '*' || *inp.s == '/')) {
    725 	    *token.e++ = inp_next();
    726 	    lsym = lsym_comment;
    727 	    unary_delim = ps.next_unary;
    728 	    break;
    729 	}
    730 
    731 	/* handle '||', '&&', etc., and also things as in 'int *****i' */
    732 	while (token.e[-1] == *inp.s || *inp.s == '=')
    733 	    token_add_char(inp_next());
    734 
    735 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    736 	unary_delim = true;
    737     }
    738 
    739     if (inp.s >= inp.e)
    740 	inp_read_line();
    741 
    742     ps.next_unary = unary_delim;
    743 
    744     check_size_token(1);
    745     *token.e = '\0';
    746 
    747     return lexi_end(lsym);
    748 }
    749 
    750 void
    751 register_typename(const char *name)
    752 {
    753     if (typenames.len >= typenames.cap) {
    754 	typenames.cap = 16 + 2 * typenames.cap;
    755 	typenames.items = xrealloc(typenames.items,
    756 	    sizeof(typenames.items[0]) * typenames.cap);
    757     }
    758 
    759     int pos = bsearch_typenames(name);
    760     if (pos >= 0)
    761 	return;			/* already in the list */
    762 
    763     pos = -(pos + 1);
    764     memmove(typenames.items + pos + 1, typenames.items + pos,
    765 	sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
    766     typenames.items[pos] = xstrdup(name);
    767 }
    768