1 /* $NetBSD: psym_if_expr_stmt_else.c,v 1.7 2025/01/03 23:37:18 rillig Exp $ */ 2 3 /* 4 * Tests for the parser symbol psym_if_expr_stmt_else, which represents the 5 * parser state after reading the keyword 'if', the controlling expression, 6 * the statement of the 'then' branch and the keyword 'else'. 7 * 8 * If the next token is an 'if', the formatting depends on the option '-ei' or 9 * '-nei'. Any other lookahead token completes the 'if' statement. 10 */ 11 12 //indent input 13 void 14 example(_Bool cond) 15 { 16 if (cond) {} 17 else if (cond) {} 18 else if (cond) i++; 19 else {} 20 } 21 //indent end 22 23 //indent run 24 void 25 example(_Bool cond) 26 { 27 if (cond) { 28 } 29 else if (cond) { 30 } 31 else if (cond) 32 i++; 33 else { 34 } 35 } 36 //indent end 37 38 /* 39 * Combining the options '-bl' (place brace on the left margin) and '-ce' 40 * (cuddle else) looks strange, but is technically correct. 41 */ 42 //indent run -bl 43 void 44 example(_Bool cond) 45 { 46 if (cond) 47 { 48 } 49 else if (cond) 50 { 51 } 52 else if (cond) 53 i++; 54 else 55 { 56 } 57 } 58 //indent end 59 60 //indent run-equals-prev-output -bl -nce 61 62 /* 63 * Adding the option '-nei' (do not join 'else if') expands the code even 64 * more. 65 */ 66 //indent run -bl -nce -nei 67 void 68 example(_Bool cond) 69 { 70 if (cond) 71 { 72 } 73 else 74 if (cond) 75 { 76 } 77 else 78 if (cond) 79 i++; 80 else 81 { 82 } 83 } 84 //indent end 85 86 87 /* 88 * In many cases, an else-if sequence is written in a single line. 89 * Occasionally, there are cases in which the code is clearer when the 90 * 'else' and 'if' are in separate lines. In such a case, the inner 'if' 91 * statement should be indented like any other statement. 92 */ 93 //indent input 94 void 95 example(void) 96 { 97 if (cond) 98 stmt(); 99 else 100 if (cond) 101 stmt(); 102 } 103 //indent end 104 105 //indent run 106 void 107 example(void) 108 { 109 if (cond) 110 stmt(); 111 else if (cond) 112 stmt(); 113 } 114 //indent end 115