📄 indent.c
字号:
*e_code++ = ' '; embedded_comment_on_line = 2; } else embedded_comment_on_line = 1; for (t_ptr = s_com; *t_ptr; ++t_ptr) { CHECK_CODE_SIZE; *e_code++ = *t_ptr; } *e_code++ = ' '; *e_code = '\0'; /* null terminate code sect */ parser_state_tos->want_blank = false; e_com = s_com; } } else if (type_code != comment && type_code != cplus_comment) /* preserve force_nl thru a comment but cancel forced newline after newline, form feed, etc */ force_nl = false; /*-----------------------------------------------------*\ | do switch on type of token scanned | \*-----------------------------------------------------*/ CHECK_CODE_SIZE; switch (type_code) { /* now, decide what to do with the token */ case form_feed: /* found a form feed in line */ parser_state_tos->use_ff = true; /* a form feed is treated much like a newline */ dump_line (); parser_state_tos->want_blank = false; break; case newline: if ((parser_state_tos->last_token != comma || !leave_comma || !break_comma || parser_state_tos->p_l_follow > 0 || parser_state_tos->block_init || s_com != e_com) && (parser_state_tos->last_token != rbrace || !btype_2) || (compute_code_target () + (e_code - s_code) > max_col - tabsize)) { dump_line (); parser_state_tos->want_blank = false; } /* If we were on the line with a #else or a #endif, we aren't anymore. */ else_or_endif = false; ++line_no; /* keep track of input line number */ break; case lparen: /* Braces in initializer lists should be put on new lines. This is necessary so that -gnu does not cause things like char *this_is_a_string_array[] = { "foo", "this_string_does_not_fit", "nor_does_this_rather_long_string" } which is what happens because we are trying to line the strings up with the parentheses, and those that are too long are moved to the right an ugly amount. However, if the current line is empty, the left brace is already on a new line, so don't molest it. */ if (token[0] == '{' && (s_code != e_code || s_com != e_com || s_lab != e_lab)) { dump_line (); /* Do not put a space before the '{'. */ parser_state_tos->want_blank = false; } /* Count parens so we know how deep we are. */ if (++parser_state_tos->p_l_follow >= parser_state_tos->paren_indents_size) { parser_state_tos->paren_indents_size *= 2; parser_state_tos->paren_indents = (short *) xrealloc ((char *) parser_state_tos->paren_indents, parser_state_tos->paren_indents_size * sizeof (short)); } parser_state_tos->paren_depth++; if (parser_state_tos->want_blank && *token != '[' && (parser_state_tos->last_token != ident || proc_calls_space || (parser_state_tos->its_a_keyword && (!parser_state_tos->sizeof_keyword || blank_after_sizeof)))) *e_code++ = ' '; if (parser_state_tos->in_decl && !parser_state_tos->block_init) { if (troff && !parser_state_tos->dumped_decl_indent && !is_procname && parser_state_tos->last_token == decl) { parser_state_tos->dumped_decl_indent = 1; sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n", (int) (dec_ind * 7), token_end - token, token); e_code += strlen (e_code); } else if (*token != '[') { while ((e_code - s_code) < dec_ind) { CHECK_CODE_SIZE; *e_code++ = ' '; } *e_code++ = token[0]; } else *e_code++ = token[0]; } else *e_code++ = token[0]; parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1] = e_code - s_code; if (sp_sw && parser_state_tos->p_l_follow == 1 && extra_expression_indent && parser_state_tos->paren_indents[0] < 2 * ind_size) parser_state_tos->paren_indents[0] = 2 * ind_size; parser_state_tos->want_blank = false; if (parser_state_tos->in_or_st && *token == '(' && parser_state_tos->tos <= 2) { /* this is a kluge to make sure that declarations will be aligned right if proc decl has an explicit type on it, i.e. "int a(x) {..." */ parse_lparen_in_decl (); /* Turn off flag for structure decl or initialization. */ parser_state_tos->in_or_st = false; } if (parser_state_tos->sizeof_keyword) parser_state_tos->sizeof_mask |= 1 << parser_state_tos->p_l_follow; /* The '(' that starts a cast can never be preceeded by an indentifier or decl. */ if (parser_state_tos->last_token == decl || (parser_state_tos->last_token == ident && parser_state_tos->last_rw != rw_return)) parser_state_tos->noncast_mask |= 1 << parser_state_tos->p_l_follow; else parser_state_tos->noncast_mask &= ~(1 << parser_state_tos->p_l_follow); break; case rparen: parser_state_tos->paren_depth--; if (parser_state_tos->cast_mask & (1 << parser_state_tos->p_l_follow) & ~parser_state_tos->sizeof_mask) { parser_state_tos->last_u_d = true; parser_state_tos->cast_mask &= (1 << parser_state_tos->p_l_follow) - 1; if (!parser_state_tos->cast_mask && cast_space) parser_state_tos->want_blank = true; else parser_state_tos->want_blank = false; } else if (parser_state_tos->in_decl && ! parser_state_tos->block_init && parser_state_tos->paren_depth == 0) parser_state_tos->want_blank = true; parser_state_tos->sizeof_mask &= (1 << parser_state_tos->p_l_follow) - 1; if (--parser_state_tos->p_l_follow < 0) { parser_state_tos->p_l_follow = 0; diag (0, "Extra %c", (int) *token, 0); } /* if the paren starts the line, then indent it */ if (e_code == s_code) { int level = parser_state_tos->p_l_follow; parser_state_tos->paren_level = level; if (level > 0) paren_target = -parser_state_tos->paren_indents[level - 1]; else paren_target = 0; } *e_code++ = token[0];#if 0 if (!parser_state_tos->cast_mask || cast_space) parser_state_tos->want_blank = true;#endif /* check for end of if (...), or some such */ if (sp_sw && (parser_state_tos->p_l_follow == 0)) { /* Indicate that we have just left the parenthesized expression of a while, if, or for, unless we are getting out of the parenthesized expression of the while of a do-while loop. (do-while is different because a semicolon immediately following this will not indicate a null loop body). */ if (parser_state_tos->p_stack[parser_state_tos->tos] != dohead) last_token_ends_sp = 2; sp_sw = false; force_nl = true; /* must force newline after if */ parser_state_tos->last_u_d = true; /* inform lexi that a following operator is unary */ parser_state_tos->in_stmt = false; /* dont use stmt continuation indentation */ parse (hd_type); /* let parser worry about if, or whatever */ } parser_state_tos->search_brace = btype_2; /* this should insure that constructs such as main(){...} and int[]{...} have their braces put in the right place */ break; case unary_op: /* this could be any unary operation */ if (parser_state_tos->want_blank) *e_code++ = ' '; if (troff && !parser_state_tos->dumped_decl_indent && parser_state_tos->in_decl && !is_procname) { sprintf (e_code, "\n.Du %dp+\200p \"%.*s\"\n", (int) (dec_ind * 7), token_end - token, token); parser_state_tos->dumped_decl_indent = 1; e_code += strlen (e_code); } else { char *res = token; char *res_end = token_end; /* if this is a unary op in a declaration, we should indent this token */ if (parser_state_tos->paren_depth == 0 && parser_state_tos->in_decl && !parser_state_tos->block_init) { while ((e_code - s_code) < (dec_ind - (token_end - token))) { CHECK_CODE_SIZE; *e_code++ = ' '; } } if (troff && token[0] == '-' && token[1] == '>') { static char resval[] = "\\(->"; res = resval; res_end = res + sizeof (resval); } for (t_ptr = res; t_ptr < res_end; ++t_ptr) { CHECK_CODE_SIZE; *e_code++ = *t_ptr; } } parser_state_tos->want_blank = false; break; case binary_op: /* any binary operation */ if (parser_state_tos->want_blank || (e_code > s_code && *e_code != ' ')) *e_code++ = ' '; { char *res = token; char *res_end = token_end;#define set_res(str) \ {\ static char resval[] = str;\ res = resval;\ res_end = res + sizeof(resval);\ } if (troff) switch (token[0]) { case '<': if (token[1] == '=') set_res ("\\(<="); break; case '>': if (token[1] == '=') set_res ("\\(>="); break; case '!': if (token[1] == '=') set_res ("\\(!="); break; case '|': if (token[1] == '|') { set_res ("\\(br\\(br"); } else if (token[1] == 0) set_res ("\\(br"); break; } for (t_ptr = res; t_ptr < res_end; ++t_ptr) { CHECK_CODE_SIZE; *e_code++ = *t_ptr; /* move the operator */ } } parser_state_tos->want_blank = true; break; case postop: /* got a trailing ++ or -- */ *e_code++ = token[0]; *e_code++ = token[1]; parser_state_tos->want_blank = true; break; case question: /* got a ? */ squest++; /* this will be used when a later colon appears so we can distinguish the <c>?<n>:<n> construct */ if (parser_state_tos->want_blank) *e_code++ = ' '; *e_code++ = '?'; parser_state_tos->want_blank = true; break; case casestmt: /* got word 'case' or 'default' */ scase = true; /* so we can process the later colon properly */ goto copy_id; case colon: /* got a ':' */ if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */ --squest; if (parser_state_tos->want_blank) *e_code++ = ' '; *e_code++ = ':'; parser_state_tos->want_blank = true; break; } if (parser_state_tos->in_decl) { *e_code++ = ':'; parser_state_tos->want_blank = false; break; } parser_state_tos->in_stmt = false; /* seeing a label does not imply we are in a stmt */ for (t_ptr = s_code; *t_ptr; ++t_ptr) *e_lab++ = *t_ptr; /* turn everything so far into a label */ e_code = s_code; *e_lab++ = ':'; *e_lab++ = ' '; *e_lab = '\0'; /* parser_state_tos->pcas e will be used by dump_line to decide how to indent the label. force_nl will force a case n: to be on a line by itself */ force_nl = parser_state_tos->pcase = scase; scase = false; parser_state_tos->want_blank = false; break; case semicolon: /* we are not in an initialization or structure declaration */ parser_state_tos->in_or_st = false; scase = false; squest = 0; /* The following code doesn't seem to do much good. Just because we've found something like extern int foo(); or int (*foo)(); doesn't mean we are out of a declaration. Now if it was serving some purpose we'll have to address that.... if (parser_state_tos->last_token == rparen) parser_state_tos->in_parameter_declaration = 0; */ parser_state_tos->cast_mask = 0; parser_state_tos->sizeof_mask = 0; parser_state_tos->block_init = 0; parser_state_tos->block_init_level = 0; parser_state_tos->just_saw_decl--; if (parser_state_tos->in_decl && s_code == e_code && !parser_state_tos->block_init) while ((e_code - s_code) < (dec_ind - 1)) { CHECK_CODE_SIZE; *e_code++ = ' '; } /* if we were in a first level structure declaration, we aren't any more */ parser_state_tos->in_decl = (parser_state_tos->dec_nest > 0);#if 0 /* Changed to allow "{}" inside parens, as when passed to a macro. -jla */ if ((!sp_sw || hd_type != forstmt) && parser_state_tos->p_l_follow > 0) { /* This should be true iff there were unbalanced parens in the stmt. It is a bit complicated, because the semicolon might be in a for stmt */ diag (1, "Unbalanced parens", 0, 0); parser_state_tos->p_l_follow = 0; if (sp_sw) { /* this is a check for a if, while, etc. with unbalanced parens */ sp_sw = false; parse (hd_type); /* dont lose the if, or whatever */ } }#endif /* If we have a semicolon following an if, while, or for, and the user wants us to, we should insert a space (to show that there is a null statement there). */ if (last_token_ends_sp && space_sp_semicolon) { *e_code++ = ' '; } *e_code++ = ';'; parser_state_tos->want_blank = true; /* we are no longer in the middle of a stmt */ parser_state_tos->in_stmt = (parser_state_tos->p_l_follow > 0); if (!sp_sw) { /* if not if for (;;) */ parse (semicolon);/* let parser know about end of stmt */ force_nl = true; /* force newline after a end of stmt */ } break; case lbrace: /* got a '{' */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -