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