Home | History | Annotate | Line # | Download | only in indent
lexi.c revision 1.137
      1 /*	$NetBSD: lexi.c,v 1.137 2021/11/07 15:18:25 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.137 2021/11/07 15:18:25 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_outside_parentheses
     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_outside_parentheses",
    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_level);
    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.block_init || ps.in_stmt)
    419 	return false;
    420     if (inp.s[0] == '*' && inp.s[1] != '=')
    421 	goto maybe;
    422     if (isalpha((unsigned char)*inp.s))
    423 	goto maybe;
    424     return false;
    425 maybe:
    426     return ps.prev_token == lsym_semicolon ||
    427 	ps.prev_token == lsym_lbrace ||
    428 	ps.prev_token == lsym_rbrace;
    429 }
    430 
    431 static int
    432 bsearch_typenames(const char *key)
    433 {
    434     const char **arr = typenames.items;
    435     int lo = 0;
    436     int hi = (int)typenames.len - 1;
    437 
    438     while (lo <= hi) {
    439 	int mid = (int)((unsigned)(lo + hi) >> 1);
    440 	int cmp = strcmp(arr[mid], key);
    441 	if (cmp < 0)
    442 	    lo = mid + 1;
    443 	else if (cmp > 0)
    444 	    hi = mid - 1;
    445 	else
    446 	    return mid;
    447     }
    448     return -(lo + 1);
    449 }
    450 
    451 static bool
    452 is_typename(void)
    453 {
    454     if (opt.auto_typedefs &&
    455 	token.e - token.s >= 2 && memcmp(token.e - 2, "_t", 2) == 0)
    456 	return true;
    457 
    458     return bsearch_typenames(token.s) >= 0;
    459 }
    460 
    461 static int
    462 cmp_keyword_by_name(const void *key, const void *elem)
    463 {
    464     return strcmp(key, ((const struct keyword *)elem)->name);
    465 }
    466 
    467 /* Read an alphanumeric token into 'token', or return end_of_file. */
    468 static lexer_symbol
    469 lexi_alnum(void)
    470 {
    471     if (isdigit((unsigned char)*inp.s) ||
    472 	(inp.s[0] == '.' && isdigit((unsigned char)inp.s[1]))) {
    473 	lex_number();
    474     } else if (isalnum((unsigned char)*inp.s) ||
    475 	    *inp.s == '_' || *inp.s == '$') {
    476 	lex_word();
    477     } else
    478 	return lsym_eof;	/* just as a placeholder */
    479 
    480     *token.e = '\0';
    481 
    482     if (token.s[0] == 'L' && token.s[1] == '\0' &&
    483 	(*inp.s == '"' || *inp.s == '\''))
    484 	return lsym_string_prefix;
    485 
    486     while (ch_isblank(inp_peek()))
    487 	inp_skip();
    488 
    489     if (ps.prev_token == lsym_tag && ps.p_l_follow == 0) {
    490 	ps.next_unary = true;
    491 	return lsym_type_outside_parentheses;
    492     }
    493 
    494     /* Operator after identifier is binary unless last token was 'struct'. */
    495     ps.next_unary = ps.prev_token == lsym_tag;
    496 
    497     const struct keyword *kw = bsearch(token.s, keywords,
    498 	array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
    499     bool is_type = false;
    500     if (kw == NULL) {
    501 	if (is_typename()) {
    502 	    is_type = true;
    503 	    ps.next_unary = true;
    504 	    goto found_typename;
    505 	}
    506 
    507     } else {			/* we have a keyword */
    508 	is_type = kw->lsym == lsym_type;
    509 	ps.next_unary = true;
    510 	if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
    511 	    return kw->lsym;
    512 
    513 found_typename:
    514 	if (ps.p_l_follow > 0) {
    515 	    /* inside parentheses: cast, param list, offsetof or sizeof */
    516 	    ps.cast_mask |= (1 << ps.p_l_follow) & ~ps.not_cast_mask;
    517 	}
    518 	if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
    519 	    if (kw != NULL && kw->lsym == lsym_tag)
    520 		return lsym_tag;
    521 	    if (ps.p_l_follow == 0)
    522 		return lsym_type_outside_parentheses;
    523 	}
    524     }
    525 
    526     if (*inp.s == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
    527 	!ps.in_parameter_declaration && !ps.block_init) {
    528 
    529 	for (const char *p = inp.s; p < inp.e;)
    530 	    if (*p++ == ')' && (*p == ';' || *p == ','))
    531 		goto no_function_definition;
    532 
    533 	strncpy(ps.procname, token.s, sizeof ps.procname - 1);
    534 	if (ps.in_decl)
    535 	    ps.in_parameter_declaration = true;
    536 	return lsym_funcname;
    537 no_function_definition:;
    538 
    539     } else if (ps.p_l_follow == 0 && probably_typename()) {
    540 	ps.next_unary = true;
    541 	return lsym_type_outside_parentheses;
    542     }
    543 
    544     return is_type ? lsym_type_in_parentheses : lsym_word;
    545 }
    546 
    547 /* Reads the next token, placing it in the global variable "token". */
    548 lexer_symbol
    549 lexi(void)
    550 {
    551     token.e = token.s;
    552     ps.curr_col_1 = ps.next_col_1;
    553     ps.next_col_1 = false;
    554 
    555     while (ch_isblank(*inp.s)) {
    556 	ps.curr_col_1 = false;
    557 	inp_skip();
    558     }
    559 
    560     lexer_symbol alnum_lsym = lexi_alnum();
    561     if (alnum_lsym != lsym_eof)
    562 	return lexi_end(alnum_lsym);
    563 
    564     /* Scan a non-alphanumeric token */
    565 
    566     check_size_token(3);	/* for things like "<<=" */
    567     *token.e++ = inp_next();
    568     *token.e = '\0';
    569 
    570     lexer_symbol lsym;
    571     bool unary_delim = false;	/* whether the current token forces a
    572 				 * following operator to be unary */
    573 
    574     switch (token.e[-1]) {
    575     case '\n':
    576 	unary_delim = ps.next_unary;
    577 	ps.next_col_1 = true;
    578 	/* if data has been exhausted, the newline is a dummy. */
    579 	lsym = had_eof ? lsym_eof : lsym_newline;
    580 	break;
    581 
    582     case '\'':
    583     case '"':
    584 	lex_char_or_string();
    585 	lsym = lsym_word;
    586 	break;
    587 
    588     case '(':
    589     case '[':
    590 	unary_delim = true;
    591 	lsym = lsym_lparen_or_lbracket;
    592 	break;
    593 
    594     case ')':
    595     case ']':
    596 	lsym = lsym_rparen_or_rbracket;
    597 	break;
    598 
    599     case '#':
    600 	unary_delim = ps.next_unary;
    601 	lsym = lsym_preprocessing;
    602 	break;
    603 
    604     case '?':
    605 	unary_delim = true;
    606 	lsym = lsym_question;
    607 	break;
    608 
    609     case ':':
    610 	lsym = lsym_colon;
    611 	unary_delim = true;
    612 	break;
    613 
    614     case ';':
    615 	unary_delim = true;
    616 	lsym = lsym_semicolon;
    617 	break;
    618 
    619     case '{':
    620 	unary_delim = true;
    621 	lsym = lsym_lbrace;
    622 	break;
    623 
    624     case '}':
    625 	unary_delim = true;
    626 	lsym = lsym_rbrace;
    627 	break;
    628 
    629     case '\f':
    630 	unary_delim = ps.next_unary;
    631 	ps.next_col_1 = true;
    632 	lsym = lsym_form_feed;
    633 	break;
    634 
    635     case ',':
    636 	unary_delim = true;
    637 	lsym = lsym_comma;
    638 	break;
    639 
    640     case '.':
    641 	unary_delim = false;
    642 	lsym = lsym_period;
    643 	break;
    644 
    645     case '-':
    646     case '+':
    647 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    648 	unary_delim = true;
    649 
    650 	if (*inp.s == token.e[-1]) {	/* ++, -- */
    651 	    *token.e++ = *inp.s++;
    652 	    if (ps.prev_token == lsym_word ||
    653 		    ps.prev_token == lsym_rparen_or_rbracket) {
    654 		lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
    655 		unary_delim = false;
    656 	    }
    657 
    658 	} else if (*inp.s == '=') {	/* += */
    659 	    *token.e++ = *inp.s++;
    660 
    661 	} else if (*inp.s == '>') {	/* -> */
    662 	    *token.e++ = *inp.s++;
    663 	    unary_delim = false;
    664 	    lsym = lsym_unary_op;
    665 	    ps.want_blank = false;
    666 	}
    667 	break;
    668 
    669     case '=':
    670 	if (ps.init_or_struct)
    671 	    ps.block_init = true;
    672 	if (*inp.s == '=') {	/* == */
    673 	    *token.e++ = *inp.s++;
    674 	    *token.e = '\0';
    675 	}
    676 	lsym = lsym_binary_op;
    677 	unary_delim = true;
    678 	break;
    679 
    680     case '>':
    681     case '<':
    682     case '!':			/* ops like <, <<, <=, !=, etc */
    683 	if (*inp.s == '>' || *inp.s == '<' || *inp.s == '=')
    684 	    *token.e++ = inp_next();
    685 	if (*inp.s == '=')
    686 	    *token.e++ = *inp.s++;
    687 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    688 	unary_delim = true;
    689 	break;
    690 
    691     case '*':
    692 	unary_delim = true;
    693 	if (!ps.next_unary) {
    694 	    if (*inp.s == '=')
    695 		*token.e++ = *inp.s++;
    696 	    lsym = lsym_binary_op;
    697 	    break;
    698 	}
    699 
    700 	while (*inp.s == '*' || isspace((unsigned char)*inp.s)) {
    701 	    if (*inp.s == '*')
    702 		token_add_char('*');
    703 	    inp_skip();
    704 	}
    705 
    706 	if (ps.in_decl) {
    707 	    char *tp = inp.s;
    708 
    709 	    while (isalpha((unsigned char)*tp) ||
    710 		    isspace((unsigned char)*tp)) {
    711 		if (++tp >= inp.e)
    712 		    inp_read_line();
    713 	    }
    714 	    if (*tp == '(')
    715 		ps.procname[0] = ' ';
    716 	}
    717 
    718 	lsym = lsym_unary_op;
    719 	break;
    720 
    721     default:
    722 	if (token.e[-1] == '/' && (*inp.s == '*' || *inp.s == '/')) {
    723 	    *token.e++ = inp_next();
    724 	    lsym = lsym_comment;
    725 	    unary_delim = ps.next_unary;
    726 	    break;
    727 	}
    728 
    729 	/* handle '||', '&&', etc., and also things as in 'int *****i' */
    730 	while (token.e[-1] == *inp.s || *inp.s == '=')
    731 	    token_add_char(inp_next());
    732 
    733 	lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    734 	unary_delim = true;
    735     }
    736 
    737     if (inp.s >= inp.e)
    738 	inp_read_line();
    739 
    740     ps.next_unary = unary_delim;
    741 
    742     check_size_token(1);
    743     *token.e = '\0';
    744 
    745     return lexi_end(lsym);
    746 }
    747 
    748 void
    749 register_typename(const char *name)
    750 {
    751     if (typenames.len >= typenames.cap) {
    752 	typenames.cap = 16 + 2 * typenames.cap;
    753 	typenames.items = xrealloc(typenames.items,
    754 	    sizeof(typenames.items[0]) * typenames.cap);
    755     }
    756 
    757     int pos = bsearch_typenames(name);
    758     if (pos >= 0)
    759 	return;			/* already in the list */
    760 
    761     pos = -(pos + 1);
    762     memmove(typenames.items + pos + 1, typenames.items + pos,
    763 	sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
    764     typenames.items[pos] = xstrdup(name);
    765 }
    766