xref: /netbsd-src/tests/usr.bin/indent/psym_else.c (revision 0d2fd2321819c1ec4a18a6d5d8aaa19b4bb1be90)
1 /* $NetBSD: psym_else.c,v 1.8 2025/01/07 02:55:31 rillig Exp $ */
2 
3 /*
4  * Tests for the parser symbol psym_else, which represents the keyword 'else'
5  * that is being shifted on the parser stack.
6  *
7  * This parser symbol never ends up on the stack itself.
8  */
9 
10 /*
11  * When parsing nested incomplete 'if' statements, the problem of the
12  * 'dangling else' occurs.  It is resolved by binding the 'else' to the
13  * innermost incomplete 'if' statement.
14  *
15  * In 'parse', an if_expr_stmt is reduced to a simple statement, unless the
16  * next token is 'else'. The comment does not influence this since it never
17  * reaches 'parse'.
18  */
19 //indent input
20 void
21 example(bool cond)
22 {
23 	if (cond)
24 	if (cond)
25 	if (cond)
26 	stmt();
27 	else
28 	stmt();
29 	/* comment */
30 	else
31 	stmt();
32 }
33 //indent end
34 
35 //indent run
36 void
37 example(bool cond)
38 {
39 	if (cond)
40 		if (cond)
41 			if (cond)
42 				stmt();
43 			else
44 				stmt();
45 		/* comment */
46 		else
47 			stmt();
48 }
49 //indent end
50 
51 
52 /*
53  * The keyword 'else' is followed by an expression, as opposed to 'if', which
54  * is followed by a parenthesized expression.
55  */
56 //indent input
57 void
58 function(void)
59 {
60 	if(var>0)var=0;else(var=3);
61 }
62 //indent end
63 
64 //indent run
65 void
66 function(void)
67 {
68 	if (var > 0)
69 		var = 0;
70 	else
71 		(var = 3);
72 }
73 //indent end
74 
75 
76 //indent input
77 {
78 	else
79 }
80 //indent end
81 
82 //indent run
83 {
84 	else
85 }
86 // exit 1
87 // error: Standard Input:2: Unmatched 'else'
88 //indent end
89