Home | History | Annotate | Line # | Download | only in indent
lexi.c revision 1.198
      1 /*	$NetBSD: lexi.c,v 1.198 2023/05/18 04:23:03 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 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: lexi.c,v 1.198 2023/05/18 04:23:03 rillig Exp $");
     42 
     43 #include <stdlib.h>
     44 #include <string.h>
     45 
     46 #include "indent.h"
     47 
     48 /* In lexi_alnum, this constant marks a type, independent of parentheses. */
     49 #define lsym_type lsym_type_outside_parentheses
     50 #define lsym_type_modifier lsym_storage_class
     51 
     52 /* must be sorted alphabetically, is used in binary search */
     53 static const struct keyword {
     54 	const char name[12];
     55 	lexer_symbol lsym;
     56 } keywords[] = {
     57 	{"_Bool", lsym_type},
     58 	{"_Complex", lsym_type},
     59 	{"_Imaginary", lsym_type},
     60 	{"auto", lsym_storage_class},
     61 	{"bool", lsym_type},
     62 	{"break", lsym_word},
     63 	{"case", lsym_case_label},
     64 	{"char", lsym_type},
     65 	{"complex", lsym_type},
     66 	{"const", lsym_type_modifier},
     67 	{"continue", lsym_word},
     68 	{"default", lsym_case_label},
     69 	{"do", lsym_do},
     70 	{"double", lsym_type},
     71 	{"else", lsym_else},
     72 	{"enum", lsym_tag},
     73 	{"extern", lsym_storage_class},
     74 	{"float", lsym_type},
     75 	{"for", lsym_for},
     76 	{"goto", lsym_word},
     77 	{"if", lsym_if},
     78 	{"imaginary", lsym_type},
     79 	{"inline", lsym_word},
     80 	{"int", lsym_type},
     81 	{"long", lsym_type},
     82 	{"offsetof", lsym_offsetof},
     83 	{"register", lsym_storage_class},
     84 	{"restrict", lsym_word},
     85 	{"return", lsym_return},
     86 	{"short", lsym_type},
     87 	{"signed", lsym_type},
     88 	{"sizeof", lsym_sizeof},
     89 	{"static", lsym_storage_class},
     90 	{"struct", lsym_tag},
     91 	{"switch", lsym_switch},
     92 	{"typedef", lsym_typedef},
     93 	{"union", lsym_tag},
     94 	{"unsigned", lsym_type},
     95 	{"void", lsym_type},
     96 	{"volatile", lsym_type_modifier},
     97 	{"while", lsym_while}
     98 };
     99 
    100 static struct {
    101 	const char **items;
    102 	unsigned int len;
    103 	unsigned int cap;
    104 } typenames;
    105 
    106 /*-
    107  * The transition table below was rewritten by hand from lx's output, given
    108  * the following definitions. lx is Katherine Flavel's lexer generator.
    109  *
    110  * O  = /[0-7]/;        D  = /[0-9]/;          NZ = /[1-9]/;
    111  * H  = /[a-f0-9]/i;    B  = /[0-1]/;          HP = /0x/i;
    112  * BP = /0b/i;          E  = /e[+\-]?/i D+;    P  = /p[+\-]?/i D+;
    113  * FS = /[fl]/i;        IS = /u/i /(l|L|ll|LL)/? | /(l|L|ll|LL)/ /u/i?;
    114  *
    115  * D+           E  FS? -> $float;
    116  * D*    "." D+ E? FS? -> $float;
    117  * D+    "."    E? FS? -> $float;    HP H+           IS? -> $int;
    118  * HP H+        P  FS? -> $float;    NZ D*           IS? -> $int;
    119  * HP H* "." H+ P  FS? -> $float;    "0" O*          IS? -> $int;
    120  * HP H+ "."    P  FS  -> $float;    BP B+           IS? -> $int;
    121  */
    122 /* INDENT OFF */
    123 static const unsigned char lex_number_state[][26] = {
    124     /*                examples:
    125                                      00
    126              s                      0xx
    127              t                    00xaa
    128              a     11       101100xxa..
    129              r   11ee0001101lbuuxx.a.pp
    130              t.01.e+008bLuxll0Ll.aa.p+0
    131     states:  ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    132     [0] =   "uuiifuufiuuiiuiiiiiuiuuuuu",	/* (other) */
    133     [1] =   "CEIDEHHHIJQ  U  Q  VUVVZZZ",	/* 0 */
    134     [2] =   "DEIDEHHHIJQ  U  Q  VUVVZZZ",	/* 1 */
    135     [3] =   "DEIDEHHHIJ   U     VUVVZZZ",	/* 2 3 4 5 6 7 */
    136     [4] =   "DEJDEHHHJJ   U     VUVVZZZ",	/* 8 9 */
    137     [5] =   "             U     VUVV   ",	/* A a C c D d */
    138     [6] =   "  K          U     VUVV   ",	/* B b */
    139     [7] =   "  FFF   FF   U     VUVV   ",	/* E e */
    140     [8] =   "    f  f     U     VUVV  f",	/* F f */
    141     [9] =   "  LLf  fL  PR   Li  L    f",	/* L */
    142     [10] =  "  OOf  fO   S P O i O    f",	/* l */
    143     [11] =  "                    FFX   ",	/* P p */
    144     [12] =  "  MM    M  i  iiM   M     ",	/* U u */
    145     [13] =  "  N                       ",	/* X x */
    146     [14] =  "     G                 Y  ",	/* + - */
    147     [15] =  "B EE    EE   T      W     ",	/* . */
    148     /*       ABCDEFGHIJKLMNOPQRSTUVWXYZ */
    149 };
    150 /* INDENT ON */
    151 
    152 static const unsigned char lex_number_row[] = {
    153 	['0'] = 1,
    154 	['1'] = 2,
    155 	['2'] = 3, ['3'] = 3, ['4'] = 3, ['5'] = 3, ['6'] = 3, ['7'] = 3,
    156 	['8'] = 4, ['9'] = 4,
    157 	['A'] = 5, ['a'] = 5, ['C'] = 5, ['c'] = 5, ['D'] = 5, ['d'] = 5,
    158 	['B'] = 6, ['b'] = 6,
    159 	['E'] = 7, ['e'] = 7,
    160 	['F'] = 8, ['f'] = 8,
    161 	['L'] = 9,
    162 	['l'] = 10,
    163 	['P'] = 11, ['p'] = 11,
    164 	['U'] = 12, ['u'] = 12,
    165 	['X'] = 13, ['x'] = 13,
    166 	['+'] = 14, ['-'] = 14,
    167 	['.'] = 15,
    168 };
    169 
    170 static void
    171 token_add_char(char ch)
    172 {
    173 	buf_add_char(&token, ch);
    174 }
    175 
    176 
    177 static void
    178 lex_number(void)
    179 {
    180 	for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
    181 		unsigned char ch = (unsigned char)inp.st[0];
    182 		if (ch == '\\' && inp.st[1] == '\n') {
    183 			inp.st++;
    184 			inp_skip();
    185 			line_no++;
    186 			continue;
    187 		}
    188 		if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
    189 			break;
    190 
    191 		unsigned char row = lex_number_row[ch];
    192 		if (lex_number_state[row][s - 'A'] == ' ') {
    193 			/*-
    194 		         * lex_number_state[0][s - 'A'] now indicates the type:
    195 		         * f = floating, i = integer, u = unknown
    196 		         */
    197 			return;
    198 		}
    199 
    200 		s = lex_number_state[row][s - 'A'];
    201 		token_add_char(inp_next());
    202 	}
    203 }
    204 
    205 static bool
    206 is_identifier_start(char ch)
    207 {
    208 	return ch_isalpha(ch) || ch == '_' || ch == '$';
    209 }
    210 
    211 static bool
    212 is_identifier_part(char ch)
    213 {
    214 	return ch_isalnum(ch) || ch == '_' || ch == '$';
    215 }
    216 
    217 static void
    218 lex_word(void)
    219 {
    220 	for (;;) {
    221 		if (is_identifier_part(inp.st[0]))
    222 			token_add_char(*inp.st++);
    223 		else if (inp.st[0] == '\\' && inp.st[1] == '\n') {
    224 			inp.st++;
    225 			inp_skip();
    226 			line_no++;
    227 		} else
    228 			return;
    229 	}
    230 }
    231 
    232 static void
    233 lex_char_or_string(void)
    234 {
    235 	for (char delim = token.mem[token.len - 1];;) {
    236 		if (inp.st[0] == '\n') {
    237 			diag(1, "Unterminated literal");
    238 			return;
    239 		}
    240 
    241 		token_add_char(*inp.st++);
    242 		if (token.mem[token.len - 1] == delim)
    243 			return;
    244 
    245 		if (token.mem[token.len - 1] == '\\') {
    246 			if (inp.st[0] == '\n')
    247 				++line_no;
    248 			token_add_char(inp_next());
    249 		}
    250 	}
    251 }
    252 
    253 /* Guess whether the current token is a declared type. */
    254 static bool
    255 probably_typename(void)
    256 {
    257 	if (ps.prev_token == lsym_storage_class)
    258 		return true;
    259 	if (ps.block_init)
    260 		return false;
    261 	if (ps.in_stmt_or_decl)	/* XXX: this condition looks incorrect */
    262 		return false;
    263 	if (inp.st[0] == '*' && inp.st[1] != '=')
    264 		goto maybe;
    265 	/* XXX: is_identifier_start */
    266 	if (ch_isalpha(inp.st[0]))
    267 		goto maybe;
    268 	return false;
    269 maybe:
    270 	return ps.prev_token == lsym_semicolon ||
    271 	    ps.prev_token == lsym_lbrace ||
    272 	    ps.prev_token == lsym_rbrace;
    273 }
    274 
    275 static int
    276 bsearch_typenames(const char *key)
    277 {
    278 	const char **arr = typenames.items;
    279 	int lo = 0;
    280 	int hi = (int)typenames.len - 1;
    281 
    282 	while (lo <= hi) {
    283 		int mid = (int)((unsigned)(lo + hi) >> 1);
    284 		int cmp = strcmp(arr[mid], key);
    285 		if (cmp < 0)
    286 			lo = mid + 1;
    287 		else if (cmp > 0)
    288 			hi = mid - 1;
    289 		else
    290 			return mid;
    291 	}
    292 	return -(lo + 1);
    293 }
    294 
    295 static bool
    296 is_typename(void)
    297 {
    298 	if (opt.auto_typedefs &&
    299 	    token.len >= 2 && memcmp(token.mem + token.len - 2, "_t", 2) == 0)
    300 		return true;
    301 
    302 	return bsearch_typenames(token.st) >= 0;
    303 }
    304 
    305 static int
    306 cmp_keyword_by_name(const void *key, const void *elem)
    307 {
    308 	return strcmp(key, ((const struct keyword *)elem)->name);
    309 }
    310 
    311 /*
    312  * Looking at something like 'function_name(...)' in a line, guess whether
    313  * this starts a function definition or a declaration.
    314  */
    315 static bool
    316 probably_looking_at_definition(void)
    317 {
    318 	int paren_level = 0;
    319 	for (const char *p = inp.st; *p != '\n'; p++) {
    320 		if (*p == '(')
    321 			paren_level++;
    322 		if (*p == ')' && --paren_level == 0) {
    323 			p++;
    324 
    325 			while (*p != '\n' && (ch_isspace(*p) || is_identifier_part(*p)))
    326 				p++;	/* '__dead' or '__unused' */
    327 
    328 			if (*p == '\n')	/* func(...) */
    329 				break;
    330 			if (*p == ';')	/* func(...); */
    331 				return false;
    332 			if (*p == ',')	/* double abs(), pi; */
    333 				return false;
    334 			if (*p == '(')	/* func(...) __attribute__((...)) */
    335 				paren_level++;	/* func(...) __printflike(...)
    336 						 */
    337 			else
    338 				break;	/* func(...) { ... */
    339 		}
    340 	}
    341 
    342 	/* To further reduce the cases where indent wrongly treats an
    343 	 * incomplete function declaration as a function definition, thus
    344 	 * adding a newline before the function name, it may be worth looking
    345 	 * for parameter names, as these are often omitted in function
    346 	 * declarations and only included in function definitions. Or just
    347 	 * increase the lookahead to more than just the current line of input,
    348 	 * until the next '{'. */
    349 	return true;
    350 }
    351 
    352 /* Read an alphanumeric token into 'token', or return lsym_eof. */
    353 static lexer_symbol
    354 lexi_alnum(void)
    355 {
    356 	if (ch_isdigit(inp.st[0]) ||
    357 	    (inp.st[0] == '.' && ch_isdigit(inp.st[1]))) {
    358 		lex_number();
    359 	} else if (is_identifier_start(inp.st[0])) {
    360 		lex_word();
    361 
    362 		if (token.len == 1 && token.st[0] == 'L' &&
    363 		    (inp.st[0] == '"' || inp.st[0] == '\'')) {
    364 			token_add_char(*inp.st++);
    365 			lex_char_or_string();
    366 			ps.next_unary = false;
    367 			return lsym_word;
    368 		}
    369 	} else
    370 		return lsym_eof;	/* just as a placeholder */
    371 
    372 	while (ch_isblank(inp.st[0]))
    373 		inp.st++;
    374 
    375 	ps.next_unary = ps.prev_token == lsym_tag
    376 	    || ps.prev_token == lsym_typedef;
    377 
    378 	if (ps.prev_token == lsym_tag && ps.nparen == 0)
    379 		return lsym_type_outside_parentheses;
    380 
    381 	token_add_char('\0');
    382 	token.len--;
    383 	const struct keyword *kw = bsearch(token.st, keywords,
    384 	    array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
    385 	bool is_type = false;
    386 	if (kw == NULL) {
    387 		if (is_typename()) {
    388 			is_type = true;
    389 			ps.next_unary = true;
    390 			if (ps.in_enum == in_enum_enum)
    391 				ps.in_enum = in_enum_type;
    392 			goto found_typename;
    393 		}
    394 
    395 	} else {		/* we have a keyword */
    396 		is_type = kw->lsym == lsym_type;
    397 		ps.next_unary = true;
    398 		if (kw->lsym != lsym_tag && kw->lsym != lsym_type)
    399 			return kw->lsym;
    400 
    401 found_typename:
    402 		if (ps.nparen > 0) {
    403 			/* inside parentheses: cast, param list, offsetof or
    404 			 * sizeof */
    405 			if (ps.paren[ps.nparen - 1].cast == cast_unknown)
    406 				ps.paren[ps.nparen - 1].cast = cast_maybe;
    407 		}
    408 		if (ps.prev_token != lsym_period && ps.prev_token != lsym_unary_op) {
    409 			if (kw != NULL && kw->lsym == lsym_tag) {
    410 				if (token.st[0] == 'e' /* enum */)
    411 					ps.in_enum = in_enum_enum;
    412 				return lsym_tag;
    413 			}
    414 			if (ps.nparen == 0)
    415 				return lsym_type_outside_parentheses;
    416 		}
    417 	}
    418 
    419 	if (inp.st[0] == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
    420 	    !ps.in_func_def_params && !ps.block_init) {
    421 
    422 		if (ps.nparen == 0 && probably_looking_at_definition()) {
    423 			ps.is_function_definition = true;
    424 			if (ps.in_decl)
    425 				ps.in_func_def_params = true;
    426 			return lsym_funcname;
    427 		}
    428 
    429 	} else if (ps.nparen == 0 && probably_typename()) {
    430 		ps.next_unary = true;
    431 		return lsym_type_outside_parentheses;
    432 	}
    433 
    434 	return is_type ? lsym_type_in_parentheses : lsym_word;
    435 }
    436 
    437 static bool
    438 is_asterisk_unary(void)
    439 {
    440 	if (ps.next_unary || ps.in_func_def_params)
    441 		return true;
    442 	if (ps.prev_token == lsym_word ||
    443 	    ps.prev_token == lsym_rparen_or_rbracket)
    444 		return false;
    445 	return ps.in_decl && ps.nparen > 0;
    446 }
    447 
    448 static void
    449 lex_asterisk_unary(void)
    450 {
    451 	while (inp.st[0] == '*' || ch_isspace(inp.st[0])) {
    452 		if (inp.st[0] == '*')
    453 			token_add_char('*');
    454 		inp_skip();
    455 	}
    456 
    457 	if (ps.in_decl) {
    458 		for (const char *tp = inp.st; *tp != '\n';) {
    459 			if (ch_isspace(*tp))
    460 				tp++;
    461 			else if (is_identifier_start(*tp)) {
    462 				tp++;
    463 				while (is_identifier_part(*tp))
    464 					tp++;
    465 			} else {
    466 				if (*tp == '(')
    467 					ps.is_function_definition = true;
    468 				break;
    469 			}
    470 		}
    471 	}
    472 }
    473 
    474 static void
    475 skip_blank(const char **pp)
    476 {
    477 	while (ch_isblank(**pp))
    478 		(*pp)++;
    479 }
    480 
    481 static bool
    482 skip_string(const char **pp, const char *s)
    483 {
    484 	size_t len = strlen(s);
    485 	if (strncmp(*pp, s, len) == 0) {
    486 		*pp += len;
    487 		return true;
    488 	}
    489 	return false;
    490 }
    491 
    492 static void
    493 lex_indent_comment(void)
    494 {
    495 	const char *p = inp.mem;
    496 
    497 	skip_blank(&p);
    498 	if (!skip_string(&p, "/*"))
    499 		return;
    500 	skip_blank(&p);
    501 	if (!skip_string(&p, "INDENT"))
    502 		return;
    503 
    504 	enum indent_enabled enabled;
    505 	skip_blank(&p);
    506 	if (*p == '*' || skip_string(&p, "ON"))
    507 		enabled = indent_last_off_line;
    508 	else if (skip_string(&p, "OFF"))
    509 		enabled = indent_off;
    510 	else
    511 		return;
    512 
    513 	skip_blank(&p);
    514 	if (!skip_string(&p, "*/\n"))
    515 		return;
    516 
    517 	if (lab.len > 0 || code.len > 0 || com.len > 0)
    518 		output_line();
    519 
    520 	indent_enabled = enabled;
    521 }
    522 
    523 /* Reads the next token, placing it in the global variable "token". */
    524 lexer_symbol
    525 lexi(void)
    526 {
    527 	token.len = 0;
    528 	ps.curr_col_1 = ps.next_col_1;
    529 	ps.next_col_1 = false;
    530 
    531 	for (;;) {
    532 		if (ch_isblank(inp.st[0])) {
    533 			ps.curr_col_1 = false;
    534 			inp.st++;
    535 		} else if (inp.st[0] == '\\' && inp.st[1] == '\n') {
    536 			inp.st++;
    537 			inp_skip();
    538 			line_no++;
    539 		} else
    540 			break;
    541 	}
    542 
    543 	lexer_symbol alnum_lsym = lexi_alnum();
    544 	if (alnum_lsym != lsym_eof) {
    545 		debug_parser_state(alnum_lsym);
    546 		return alnum_lsym;
    547 	}
    548 
    549 	/* Scan a non-alphanumeric token */
    550 
    551 	token_add_char(inp_next());
    552 
    553 	lexer_symbol lsym;
    554 	bool next_unary;
    555 
    556 	switch (token.mem[token.len - 1]) {
    557 
    558     /* INDENT OFF */
    559     case '(':
    560     case '[':	lsym = lsym_lparen_or_lbracket;	next_unary = true;	break;
    561     case ')':
    562     case ']':	lsym = lsym_rparen_or_rbracket;	next_unary = false;	break;
    563     case '?':	lsym = lsym_question;		next_unary = true;	break;
    564     case ':':	lsym = lsym_colon;		next_unary = true;	break;
    565     case ';':	lsym = lsym_semicolon;		next_unary = true;	break;
    566     case '{':	lsym = lsym_lbrace;		next_unary = true;	break;
    567     case '}':	lsym = lsym_rbrace;		next_unary = true;	break;
    568     case ',':	lsym = lsym_comma;		next_unary = true;	break;
    569     case '.':	lsym = lsym_period;		next_unary = false;	break;
    570     /* INDENT ON */
    571 
    572 	case '\n':
    573 		/* if data has been exhausted, the '\n' is a dummy. */
    574 		lsym = had_eof ? lsym_eof : lsym_newline;
    575 		next_unary = ps.next_unary;
    576 		ps.next_col_1 = true;
    577 		break;
    578 
    579 	case '#':
    580 		lsym = lsym_preprocessing;
    581 		next_unary = ps.next_unary;
    582 		break;
    583 
    584 	case '\'':
    585 	case '"':
    586 		lex_char_or_string();
    587 		lsym = lsym_word;
    588 		next_unary = false;
    589 		break;
    590 
    591 	case '-':
    592 	case '+':
    593 		lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    594 		next_unary = true;
    595 
    596 		if (inp.st[0] == token.mem[token.len - 1]) {	/* '++' or '--' */
    597 			token_add_char(*inp.st++);
    598 			if (ps.prev_token == lsym_word ||
    599 			    ps.prev_token == lsym_rparen_or_rbracket) {
    600 				lsym = ps.next_unary ? lsym_unary_op : lsym_postfix_op;
    601 				next_unary = false;
    602 			}
    603 
    604 		} else if (inp.st[0] == '=') {	/* '+=' or '-=' */
    605 			token_add_char(*inp.st++);
    606 
    607 		} else if (inp.st[0] == '>') {	/* '->' */
    608 			token_add_char(*inp.st++);
    609 			lsym = lsym_unary_op;
    610 			next_unary = false;
    611 			ps.want_blank = false;
    612 		}
    613 		break;
    614 
    615 	case '=':
    616 		if (ps.init_or_struct)
    617 			ps.block_init = true;
    618 		if (inp.st[0] == '=')
    619 			token_add_char(*inp.st++);
    620 		lsym = lsym_binary_op;
    621 		next_unary = true;
    622 		break;
    623 
    624 	case '>':
    625 	case '<':
    626 	case '!':		/* ops like <, <<, <=, !=, etc */
    627 		if (inp.st[0] == '>' || inp.st[0] == '<' || inp.st[0] == '=')
    628 			token_add_char(*inp.st++);
    629 		if (inp.st[0] == '=')
    630 			token_add_char(*inp.st++);
    631 		lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    632 		next_unary = true;
    633 		break;
    634 
    635 	case '*':
    636 		if (is_asterisk_unary()) {
    637 			lex_asterisk_unary();
    638 			lsym = lsym_unary_op;
    639 			next_unary = true;
    640 		} else {
    641 			if (inp.st[0] == '=')
    642 				token_add_char(*inp.st++);
    643 			lsym = lsym_binary_op;
    644 			next_unary = true;
    645 		}
    646 		break;
    647 
    648 	default:
    649 		if (token.mem[token.len - 1] == '/'
    650 		    && (inp.st[0] == '*' || inp.st[0] == '/')) {
    651 			enum indent_enabled prev = indent_enabled;
    652 			lex_indent_comment();
    653 			if (prev == indent_on && indent_enabled == indent_off)
    654 				clear_indent_off_text();
    655 			token_add_char(*inp.st++);
    656 			lsym = lsym_comment;
    657 			next_unary = ps.next_unary;
    658 			break;
    659 		}
    660 
    661 		/* handle '||', '&&', etc., and also things as in 'int *****i'
    662 		 */
    663 		while (inp.st[0] == token.mem[token.len - 1] || inp.st[0] == '=')
    664 			token_add_char(*inp.st++);
    665 
    666 		lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
    667 		next_unary = true;
    668 	}
    669 
    670 	if (ps.in_enum == in_enum_enum || ps.in_enum == in_enum_type)
    671 		ps.in_enum = lsym == lsym_lbrace ? in_enum_brace : in_enum_no;
    672 	if (lsym == lsym_rbrace)
    673 		ps.in_enum = in_enum_no;
    674 
    675 	ps.next_unary = next_unary;
    676 
    677 	debug_parser_state(lsym);
    678 	return lsym;
    679 }
    680 
    681 void
    682 register_typename(const char *name)
    683 {
    684 	if (typenames.len >= typenames.cap) {
    685 		typenames.cap = 16 + 2 * typenames.cap;
    686 		typenames.items = nonnull(realloc(typenames.items,
    687 		    sizeof(typenames.items[0]) * typenames.cap));
    688 	}
    689 
    690 	int pos = bsearch_typenames(name);
    691 	if (pos >= 0)
    692 		return;		/* already in the list */
    693 
    694 	pos = -(pos + 1);
    695 	memmove(typenames.items + pos + 1, typenames.items + pos,
    696 	    sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
    697 	typenames.items[pos] = nonnull(strdup(name));
    698 }
    699