Module Name: src
Committed By: rillig
Date: Tue Mar 9 19:14:39 UTC 2021
Modified Files:
src/usr.bin/indent: indent.c indent_codes.h lexi.c parse.c
Log Message:
indent: make token names more precise
The previous 'casestmt' was wrong since a case label is not a statement
at all.
The previous 'swstmt' was overly short, and wrong as well, since it
represents only the 'switch (expr)' part, which is not a complete switch
statement. Same for 'ifstmt', 'whilestmt', 'forstmt'.
The previous word 'head' was not precise enough since it didn't specify
exactly where the head ends and the body starts. Especially for
handling the dangling else, this distinction is important.
No functional change.
To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/indent/indent_codes.h
cvs rdiff -u -r1.29 -r1.30 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.16 -r1.17 src/usr.bin/indent/parse.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.37 src/usr.bin/indent/indent.c:1.38
--- src/usr.bin/indent/indent.c:1.37 Tue Mar 9 18:28:10 2021
+++ src/usr.bin/indent/indent.c Tue Mar 9 19:14:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.37 2021/03/09 18:28:10 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.38 2021/03/09 19:14:39 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)indent.c 5.1
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.37 2021/03/09 18:28:10 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.38 2021/03/09 19:14:39 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -786,7 +786,7 @@ check_type:
ps.want_blank = true;
break;
- case casestmt: /* got word 'case' or 'default' */
+ case case_label: /* got word 'case' or 'default' */
scase = true; /* so we can process the later colon properly */
goto copy_id;
@@ -853,7 +853,7 @@ check_type:
* structure declaration, we
* arent any more */
- if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
+ if ((!sp_sw || hd_type != for_exprs) && ps.p_l_follow > 0) {
/*
* This should be true iff there were unbalanced parens in the
@@ -981,23 +981,24 @@ check_type:
}
prefix_blankline_requested = 0;
parse(rbrace); /* let parser know about this */
- ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == ifhead
+ ps.search_brace = opt.cuddle_else
+ && ps.p_stack[ps.tos] == if_expr_stmt
&& ps.il[ps.tos] >= ps.ind_level;
if (ps.tos <= 1 && opt.blanklines_after_procs && ps.dec_nest <= 0)
postfix_blankline_requested = 1;
break;
- case swstmt: /* got keyword "switch" */
+ case switch_expr: /* got keyword "switch" */
sp_sw = true;
- hd_type = swstmt; /* keep this for when we have seen the
+ hd_type = switch_expr; /* keep this for when we have seen the
* expression */
goto copy_id; /* go move the token into buffer */
case keyword_for_if_while:
sp_sw = true; /* the interesting stuff is done after the
* expression is scanned */
- hd_type = (*token == 'i' ? ifstmt :
- (*token == 'w' ? whilestmt : forstmt));
+ hd_type = (*token == 'i' ? if_expr :
+ (*token == 'w' ? while_expr : for_exprs));
/*
* remember the type of header for later use by parser
@@ -1015,7 +1016,7 @@ check_type:
}
force_nl = true;/* also, following stuff must go onto new line */
last_else = 1;
- parse(elselit);
+ parse(keyword_else);
}
else {
if (e_code != s_code) { /* make sure this starts a line */
@@ -1026,7 +1027,7 @@ check_type:
}
force_nl = true;/* also, following stuff must go onto new line */
last_else = 0;
- parse(dolit);
+ parse(keyword_do);
}
goto copy_id; /* move the token into line */
Index: src/usr.bin/indent/indent_codes.h
diff -u src/usr.bin/indent/indent_codes.h:1.9 src/usr.bin/indent/indent_codes.h:1.10
--- src/usr.bin/indent/indent_codes.h:1.9 Tue Mar 9 18:28:10 2021
+++ src/usr.bin/indent/indent_codes.h Tue Mar 9 19:14:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: indent_codes.h,v 1.9 2021/03/09 18:28:10 rillig Exp $ */
+/* $NetBSD: indent_codes.h,v 1.10 2021/03/09 19:14:39 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -49,7 +49,7 @@ typedef enum token_type {
binary_op, /* e.g. '<<' or '+' or '&&' or '/=' */
postop, /* trailing '++' or '--' */
question, /* the '?' from a '?:' expression */
- casestmt,
+ case_label,
colon,
semicolon,
lbrace,
@@ -57,22 +57,22 @@ typedef enum token_type {
ident,
comma,
comment,
- swstmt,
+ switch_expr, /* 'switch' '(' <expr> ')' */
preesc,
form_feed,
decl,
- keyword_for_if_while,
- keyword_do_else,
- ifstmt,
- whilestmt,
- forstmt,
+ keyword_for_if_while, /* 'for', 'if' or 'while' */
+ keyword_do_else, /* 'do' or 'else' */
+ if_expr, /* 'if' '(' <expr> ')' */
+ while_expr, /* 'while' '(' <expr> ')' */
+ for_exprs, /* 'for' '(' ... ')' */
stmt,
- stmtl,
- elselit,
- dolit,
- dohead,
- ifhead,
- elsehead,
+ stmt_list,
+ keyword_else, /* 'else' */
+ keyword_do, /* 'do' */
+ do_stmt, /* 'do' <stmt> */
+ if_expr_stmt, /* 'if' '(' <expr> ')' <stmt> */
+ if_expr_stmt_else, /* 'if' '(' <expr> ')' <stmt> 'else' */
period,
strpfx,
storage,
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.29 src/usr.bin/indent/lexi.c:1.30
--- src/usr.bin/indent/lexi.c:1.29 Tue Mar 9 18:28:10 2021
+++ src/usr.bin/indent/lexi.c Tue Mar 9 19:14:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.29 2021/03/09 18:28:10 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.30 2021/03/09 19:14:39 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (
#include <sys/cdefs.h>
#ifndef lint
#if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.29 2021/03/09 18:28:10 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.30 2021/03/09 19:14:39 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@@ -204,13 +204,13 @@ token_type_name(token_type tk)
{
static const char *const name[] = {
"end_of_file", "newline", "lparen", "rparen", "unary_op",
- "binary_op", "postop", "question", "casestmt", "colon",
+ "binary_op", "postop", "question", "case_label", "colon",
"semicolon", "lbrace", "rbrace", "ident", "comma",
- "comment", "swstmt", "preesc", "form_feed", "decl",
+ "comment", "switch_expr", "preesc", "form_feed", "decl",
"keyword_for_if_while", "keyword_do_else",
- "ifstmt", "whilestmt", "forstmt",
- "stmt", "stmtl", "elselit", "dolit", "dohead",
- "ifhead", "elsehead", "period", "strpfx", "storage",
+ "if_expr", "while_expr", "for_exprs",
+ "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt",
+ "if_expr_stmt", "if_expr_stmt_else", "period", "strpfx", "storage",
"funcname", "type_def", "structure"
};
@@ -367,9 +367,9 @@ lexi(struct parser_state *state)
state->last_u_d = true;
switch (p->rwcode) {
case rw_switch:
- return lexi_end(swstmt);
+ return lexi_end(switch_expr);
case rw_case_or_default:
- return lexi_end(casestmt);
+ return lexi_end(case_label);
case rw_struct_or_union_or_enum:
case rw_type:
found_typename:
Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.16 src/usr.bin/indent/parse.c:1.17
--- src/usr.bin/indent/parse.c:1.16 Tue Mar 9 18:21:01 2021
+++ src/usr.bin/indent/parse.c Tue Mar 9 19:14:39 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.16 2021/03/09 18:21:01 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.17 2021/03/09 19:14:39 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -68,7 +68,7 @@ parse(token_type tk) /* tk: the code fo
printf("parse token: '%s' \"%s\"\n", token_type_name(tk), token);
#endif
- while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
+ while (ps.p_stack[ps.tos] == if_expr_stmt && tk != keyword_else) {
/* true if we have an if without an else */
ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
* reduction */
@@ -101,18 +101,19 @@ parse(token_type tk) /* tk: the code fo
}
break;
- case ifstmt: /* scanned if (...) */
- if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
- /*
- * Note that the stack pointer here is decremented, effectively
- * reducing "else if" to "if". This saves a lot of stack space
- * in case of a long "if-else-if ... else-if" sequence.
- */
- ps.i_l_follow = ps.il[ps.tos--];
- /* the rest is the same as for dolit and forstmt */
+ case if_expr: /* 'if' '(' <expr> ')' */
+ if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
+ /*
+ * Note that the stack pointer here is decremented, effectively
+ * reducing "else if" to "if". This saves a lot of stack space
+ * in case of a long "if-else-if ... else-if" sequence.
+ */
+ ps.i_l_follow = ps.il[ps.tos--];
+ }
+ /* the rest is the same as for keyword_do and for_exprs */
/* FALLTHROUGH */
- case dolit: /* 'do' */
- case forstmt: /* for (...) */
+ case keyword_do: /* 'do' */
+ case for_exprs: /* 'for' (...) */
ps.p_stack[++ps.tos] = tk;
ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
++ps.i_l_follow; /* subsequent statements should be indented 1 */
@@ -122,7 +123,7 @@ parse(token_type tk) /* tk: the code fo
case lbrace: /* scanned { */
break_comma = false; /* don't break comma in an initial list */
if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
- || ps.p_stack[ps.tos] == stmtl)
+ || ps.p_stack[ps.tos] == stmt_list)
++ps.i_l_follow; /* it is a random, isolated stmt group or a
* declaration */
else {
@@ -134,7 +135,7 @@ parse(token_type tk) /* tk: the code fo
/*
* it is a group as part of a while, for, etc.
*/
- if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
+ if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
--ps.ind_level;
/*
* for a switch, brace should be two levels out from the code
@@ -149,15 +150,15 @@ parse(token_type tk) /* tk: the code fo
ps.il[ps.tos] = ps.i_l_follow;
break;
- case whilestmt: /* scanned while (...) */
- if (ps.p_stack[ps.tos] == dohead) {
+ case while_expr: /* 'while' '(' <expr> ')' */
+ if (ps.p_stack[ps.tos] == do_stmt) {
/* it is matched with do stmt */
ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
- ps.p_stack[++ps.tos] = whilestmt;
+ ps.p_stack[++ps.tos] = while_expr;
ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
}
else { /* it is a while loop */
- ps.p_stack[++ps.tos] = whilestmt;
+ ps.p_stack[++ps.tos] = while_expr;
ps.il[ps.tos] = ps.i_l_follow;
++ps.i_l_follow;
ps.search_brace = opt.btype_2;
@@ -165,23 +166,22 @@ parse(token_type tk) /* tk: the code fo
break;
- case elselit: /* scanned an else */
-
- if (ps.p_stack[ps.tos] != ifhead)
+ case keyword_else:
+ if (ps.p_stack[ps.tos] != if_expr_stmt)
diag(1, "Unmatched 'else'");
else {
ps.ind_level = ps.il[ps.tos]; /* indentation for else should
* be same as for if */
ps.i_l_follow = ps.ind_level + 1; /* everything following should
* be in 1 level */
- ps.p_stack[ps.tos] = elsehead;
+ ps.p_stack[ps.tos] = if_expr_stmt_else;
/* remember if with else */
ps.search_brace = opt.btype_2 | opt.else_if;
}
break;
case rbrace: /* scanned a } */
- /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
+ /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
ps.p_stack[ps.tos] = stmt;
@@ -190,8 +190,8 @@ parse(token_type tk) /* tk: the code fo
diag(1, "Statement nesting error");
break;
- case swstmt: /* had switch (...) */
- ps.p_stack[++ps.tos] = swstmt;
+ case switch_expr: /* had switch (...) */
+ ps.p_stack[++ps.tos] = switch_expr;
ps.cstk[ps.tos] = case_ind;
/* save current case indent level */
ps.il[ps.tos] = ps.i_l_follow;
@@ -246,20 +246,20 @@ reduce_stmt(void)
switch (ps.p_stack[ps.tos - 1]) {
case stmt: /* stmt stmt */
- case stmtl: /* stmtl stmt */
- ps.p_stack[--ps.tos] = stmtl;
+ case stmt_list: /* stmt_list stmt */
+ ps.p_stack[--ps.tos] = stmt_list;
return true;
- case dolit: /* do <stmt> */
- ps.p_stack[--ps.tos] = dohead;
+ case keyword_do: /* 'do' <stmt> */
+ ps.p_stack[--ps.tos] = do_stmt;
ps.i_l_follow = ps.il[ps.tos];
return true;
- case ifstmt: /* if (<expr>) <stmt> */
- ps.p_stack[--ps.tos] = ifhead;
+ case if_expr: /* 'if' '(' <expr> ')' <stmt> */
+ ps.p_stack[--ps.tos] = if_expr_stmt;
int i = ps.tos - 1;
while (ps.p_stack[i] != stmt &&
- ps.p_stack[i] != stmtl &&
+ ps.p_stack[i] != stmt_list &&
ps.p_stack[i] != lbrace)
--i;
ps.i_l_follow = ps.il[i];
@@ -270,13 +270,13 @@ reduce_stmt(void)
*/
return true;
- case swstmt: /* switch (<expr>) <stmt> */
+ case switch_expr: /* 'switch' '(' <expr> ')' <stmt> */
case_ind = ps.cstk[ps.tos - 1];
/* FALLTHROUGH */
case decl: /* finish of a declaration */
- case elsehead: /* if (<expr>) <stmt> else <stmt> */
- case forstmt: /* for (<...>) <stmt> */
- case whilestmt: /* while (<expr>) <stmt> */
+ case if_expr_stmt_else: /* 'if' '(' <expr> ')' <stmt> 'else' <stmt> */
+ case for_exprs: /* 'for' '(' ... ')' <stmt> */
+ case while_expr: /* 'while' '(' <expr> ')' <stmt> */
ps.p_stack[--ps.tos] = stmt;
ps.i_l_follow = ps.il[ps.tos];
return true;
@@ -300,8 +300,8 @@ again:
if (ps.p_stack[ps.tos] == stmt) {
if (reduce_stmt())
goto again;
- } else if (ps.p_stack[ps.tos] == whilestmt) {
- if (ps.p_stack[ps.tos - 1] == dohead) {
+ } else if (ps.p_stack[ps.tos] == while_expr) {
+ if (ps.p_stack[ps.tos - 1] == do_stmt) {
ps.tos -= 2;
goto again;
}