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