indent: for the token types, use enum instead of #define

This makes it easier to step through the code in a debugger.

No functional change.
This commit is contained in:
rillig 2021-03-07 10:56:18 +00:00
parent 3658a7adc6
commit bf0defb337
7 changed files with 68 additions and 60 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.7 2019/04/04 15:22:13 kamil Exp $
# $NetBSD: Makefile,v 1.8 2021/03/07 10:56:18 rillig Exp $
# from: @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= indent
@ -6,4 +6,6 @@ SRCS= indent.c io.c lexi.c parse.c pr_comment.c args.c
COPTS.io.c += -Wno-error=format-nonliteral
LINTFLAGS+= -e
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.c,v 1.29 2021/03/07 10:42:48 rillig Exp $ */
/* $NetBSD: indent.c,v 1.30 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -46,7 +46,7 @@ static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
__RCSID("$NetBSD: indent.c,v 1.29 2021/03/07 10:42:48 rillig Exp $");
__RCSID("$NetBSD: indent.c,v 1.30 2021/03/07 10:56:18 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@ -143,8 +143,8 @@ main(int argc, char **argv)
int dec_ind; /* current indentation for declarations */
int di_stack[20]; /* a stack of structure indentation levels */
int force_nl; /* when true, code must be broken */
int hd_type = 0; /* used to store type of stmt for if (...),
* for (...), etc */
token_type hd_type = end_of_file; /* used to store type of stmt
* for if (...), for (...), etc */
int i; /* local loop counter */
int scase; /* set to true when we see a case, so we will
* know what to do with the following colon */
@ -155,7 +155,7 @@ main(int argc, char **argv)
* construct */
const char *t_ptr; /* used for copying tokens */
int tabs_to_var; /* true if using tabs to indent to var name */
int type_code; /* the type of token, returned by lexi */
token_type type_code; /* returned by lexi */
int last_else = 0; /* true iff last keyword was an else */
const char *profile_name = NULL;
@ -505,7 +505,7 @@ main(int argc, char **argv)
* We must make this check, just in case there was an unexpected
* EOF.
*/
if (type_code != 0) {
if (type_code != end_of_file) {
/*
* The only intended purpose of calling lexi() below is to
* categorize the next token in order to decide whether to
@ -546,7 +546,7 @@ main(int argc, char **argv)
} /* end of while (search_brace) */
last_else = 0;
check_type:
if (type_code == 0) { /* we got eof */
if (type_code == end_of_file) { /* we got eof */
if (s_lab != e_lab || s_code != e_code
|| s_com != e_com) /* must dump end of line */
dump_line();
@ -1274,6 +1274,9 @@ check_type:
case comment: /* we have gotten a / followed by * this is a biggie */
pr_comment();
break;
default:
break;
} /* end of big switch stmt */
*e_code = '\0'; /* make sure code section is null terminated */

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.h,v 1.3 2021/03/07 10:42:48 rillig Exp $ */
/* $NetBSD: indent.h,v 1.4 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@ -30,7 +30,7 @@
#if 0
#if defined(__NetBSD__)
__RCSID("$NetBSD: indent.h,v 1.3 2021/03/07 10:42:48 rillig Exp $");
__RCSID("$NetBSD: indent.h,v 1.4 2021/03/07 10:56:18 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
#endif
@ -50,11 +50,11 @@ int compute_label_target(void);
int count_spaces(int, char *);
int count_spaces_until(int, char *, char *);
void init_constant_tt(void);
int lexi(struct parser_state *);
token_type lexi(struct parser_state *);
void diag(int, const char *, ...) __printflike(2, 3);
void dump_line(void);
void fill_buffer(void);
void parse(int);
void parse(token_type);
void pr_comment(void);
void set_defaults(void);
void set_option(char *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent_codes.h,v 1.6 2019/04/04 15:22:13 kamil Exp $ */
/* $NetBSD: indent_codes.h,v 1.7 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -40,40 +40,43 @@
* $FreeBSD: head/usr.bin/indent/indent_codes.h 334564 2018-06-03 16:21:15Z pstef $
*/
#define newline 1
#define lparen 2
#define rparen 3
#define unary_op 4
#define binary_op 5
#define postop 6
#define question 7
#define casestmt 8
#define colon 9
#define semicolon 10
#define lbrace 11
#define rbrace 12
#define ident 13
#define comma 14
#define comment 15
#define swstmt 16
#define preesc 17
#define form_feed 18
#define decl 19
#define sp_paren 20
#define sp_nparen 21
#define ifstmt 22
#define whilestmt 23
#define forstmt 24
#define stmt 25
#define stmtl 26
#define elselit 27
#define dolit 28
#define dohead 29
#define ifhead 30
#define elsehead 31
#define period 32
#define strpfx 33
#define storage 34
#define funcname 35
#define type_def 36
#define structure 37
typedef enum token_type {
end_of_file,
newline,
lparen,
rparen,
unary_op,
binary_op,
postop,
question,
casestmt,
colon,
semicolon,
lbrace,
rbrace,
ident,
comma,
comment,
swstmt,
preesc,
form_feed,
decl,
sp_paren,
sp_nparen,
ifstmt,
whilestmt,
forstmt,
stmt,
stmtl,
elselit,
dolit,
dohead,
ifhead,
elsehead,
period,
strpfx,
storage,
funcname,
type_def,
structure
} token_type;

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent_globs.h,v 1.12 2020/04/23 00:17:34 joerg Exp $ */
/* $NetBSD: indent_globs.h,v 1.13 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -239,8 +239,8 @@ extern int suppress_blanklines;/* set iff following blanklines should be
#define STACKSIZE 256
extern struct parser_state {
int last_token;
int p_stack[STACKSIZE]; /* this is the parsers stack */
token_type last_token;
token_type p_stack[STACKSIZE]; /* this is the parsers stack */
int il[STACKSIZE]; /* this stack stores indentation levels */
float cstk[STACKSIZE];/* used to store case stmt indentation levels */
int box_com; /* set to true when we are in a "boxed"

View File

@ -1,4 +1,4 @@
/* $NetBSD: lexi.c,v 1.18 2021/03/07 10:42:48 rillig Exp $ */
/* $NetBSD: lexi.c,v 1.19 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
__RCSID("$NetBSD: lexi.c,v 1.18 2021/03/07 10:42:48 rillig Exp $");
__RCSID("$NetBSD: lexi.c,v 1.19 2021/03/07 10:56:18 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@ -176,12 +176,12 @@ strcmp_type(const void *e1, const void *e2)
return (strcmp(e1, *(const char * const *)e2));
}
int
token_type
lexi(struct parser_state *state)
{
int unary_delim; /* this is set to 1 if the current token
* forces a following operator to be unary */
int code; /* internal code to be returned */
token_type code; /* internal code to be returned */
char qchar; /* the delimiter character for a string */
e_token = s_token; /* point to start of place to save token */
@ -376,7 +376,7 @@ lexi(struct parser_state *state)
case '\n':
unary_delim = state->last_u_d;
state->last_nl = true; /* remember that we just had a newline */
code = (had_eof ? 0 : newline);
code = (had_eof ? end_of_file : newline);
/*
* if data has been exhausted, the newline is a dummy, and we should

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.12 2021/03/07 10:42:48 rillig Exp $ */
/* $NetBSD: parse.c,v 1.13 2021/03/07 10:56:18 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -60,7 +60,7 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pste
static void reduce(void);
void
parse(int tk) /* tk: the code for the construct scanned */
parse(token_type tk) /* tk: the code for the construct scanned */
{
int i;