Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: clean up 'parse', add test for dangli...



details:   https://anonhg.NetBSD.org/src/rev/b3e49abd7fd1
branches:  trunk
changeset: 988665:b3e49abd7fd1
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Oct 08 21:48:33 2021 +0000

description:
indent: clean up 'parse', add test for dangling else

No functional change.

diffstat:

 tests/usr.bin/indent/token-keyword_else.0        |  21 +++++++++++++++++++--
 tests/usr.bin/indent/token-keyword_else.0.stdout |  21 +++++++++++++++++++--
 usr.bin/indent/parse.c                           |  18 ++++++++----------
 3 files changed, 46 insertions(+), 14 deletions(-)

diffs (97 lines):

diff -r b7347971f313 -r b3e49abd7fd1 tests/usr.bin/indent/token-keyword_else.0
--- a/tests/usr.bin/indent/token-keyword_else.0 Fri Oct 08 21:41:29 2021 +0000
+++ b/tests/usr.bin/indent/token-keyword_else.0 Fri Oct 08 21:48:33 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: token-keyword_else.0,v 1.1 2021/03/12 00:13:06 rillig Exp $ */
+/* $NetBSD: token-keyword_else.0,v 1.2 2021/10/08 21:48:33 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -9,4 +9,21 @@
  * innermost incomplete 'if' statement.
  */
 
-/* TODO: Add some code to be formatted. */
+/*
+ * In 'parse', an if_expr_stmt is reduced to a simple statement, unless the
+ * next token is 'else'. The comment does not influence this since it never
+ * reaches 'parse'.
+ */
+void
+example(bool cond)
+{
+       if (cond)
+       if (cond)
+       if (cond)
+       stmt();
+       else
+       stmt();
+       /* comment */
+       else
+       stmt();
+}
diff -r b7347971f313 -r b3e49abd7fd1 tests/usr.bin/indent/token-keyword_else.0.stdout
--- a/tests/usr.bin/indent/token-keyword_else.0.stdout  Fri Oct 08 21:41:29 2021 +0000
+++ b/tests/usr.bin/indent/token-keyword_else.0.stdout  Fri Oct 08 21:48:33 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: token-keyword_else.0.stdout,v 1.1 2021/03/12 00:13:06 rillig Exp $ */
+/* $NetBSD: token-keyword_else.0.stdout,v 1.2 2021/10/08 21:48:33 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -9,4 +9,21 @@
  * innermost incomplete 'if' statement.
  */
 
-/* TODO: Add some code to be formatted. */
+/*
+ * In 'parse', an if_expr_stmt is reduced to a simple statement, unless the
+ * next token is 'else'. The comment does not influence this since it never
+ * reaches 'parse'.
+ */
+void
+example(bool cond)
+{
+       if (cond)
+               if (cond)
+                       if (cond)
+                               stmt();
+                       else
+                               stmt();
+       /* comment */
+               else
+                       stmt();
+}
diff -r b7347971f313 -r b3e49abd7fd1 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Fri Oct 08 21:41:29 2021 +0000
+++ b/usr.bin/indent/parse.c    Fri Oct 08 21:48:33 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.33 2021/10/07 22:56:49 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.34 2021/10/08 21:48:33 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -62,16 +62,14 @@
 void
 parse(token_type ttype)
 {
-
-#ifdef debug
-    printf("parse token: '%s' \"%s\"\n", token_type_name(ttype), token.s);
-#endif
+    debug_println("parse token: '%s' \"%s\"",
+       token_type_name(ttype), token.s);
 
-    while (ps.p_stack[ps.tos] == if_expr_stmt && ttype != keyword_else) {
-       /* true if we have an if without an else */
-       ps.p_stack[ps.tos] = stmt;      /* apply the if(..) stmt ::= stmt
-                                        * reduction */
-       reduce();               /* see if this allows any reduction */
+    if (ttype != keyword_else) {
+       while (ps.p_stack[ps.tos] == if_expr_stmt) {
+           ps.p_stack[ps.tos] = stmt;
+           reduce();
+       }
     }
 
     switch (ttype) {



Home | Main Index | Thread Index | Old Index