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