|
| 1 | +#include "formatvisit.h" |
| 2 | +#include <assert.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +void ajust_line_and_identation(struct token* token, struct format_visit_ctx* ctx) |
| 8 | +{ |
| 9 | + /* |
| 10 | + * Before this token we must have a identation and before identation a new line. |
| 11 | + * If we don't have it we need to insert. |
| 12 | + */ |
| 13 | + |
| 14 | + if (token && token->level == 0) |
| 15 | + { |
| 16 | + struct token* previous_token = token->prev; |
| 17 | + if (previous_token) |
| 18 | + { |
| 19 | + if (previous_token->type == TK_BLANKS) |
| 20 | + { |
| 21 | + char blanks[50] = {0}; |
| 22 | + if (ctx->identation > 0) |
| 23 | + snprintf(blanks, sizeof blanks, "%*c", (ctx->identation * 4), ' '); |
| 24 | + |
| 25 | + /*only adjust the number of spaces*/ |
| 26 | + free(previous_token->lexeme); |
| 27 | + previous_token->lexeme = strdup(blanks); |
| 28 | + |
| 29 | + struct token* previous_previous_token = |
| 30 | + previous_token->prev; |
| 31 | + |
| 32 | + if (previous_previous_token->type != TK_NEWLINE) |
| 33 | + { |
| 34 | + struct error error = { 0 }; |
| 35 | + struct token_list list = tokenizer("\n", NULL, 0, TK_FLAG_NONE, &error); |
| 36 | + token_list_insert_after(&ctx->ast.token_list, previous_previous_token, &list); |
| 37 | + } |
| 38 | + } |
| 39 | + else if (previous_token->type != TK_NEWLINE) |
| 40 | + { |
| 41 | + char blanks[50]; |
| 42 | + if (ctx->identation > 0) |
| 43 | + { |
| 44 | + snprintf(blanks, sizeof blanks, "\n%*c", (ctx->identation * 4), ' '); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + snprintf(blanks, sizeof blanks, "\n"); |
| 49 | + } |
| 50 | + |
| 51 | + struct error error = { 0 }; |
| 52 | + struct token_list list = tokenizer(blanks, NULL, 0, TK_FLAG_NONE, &error); |
| 53 | + token_list_insert_after(&ctx->ast.token_list, previous_token, &list); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static void format_visit_unlabeled_statement(struct format_visit_ctx* ctx, struct unlabeled_statement* p_unlabeled_statement, struct error* error); |
| 60 | + |
| 61 | +static void format_visit_statement(struct format_visit_ctx* ctx, struct statement* p_statement, struct error* error) |
| 62 | +{ |
| 63 | + if (p_statement->labeled_statement) |
| 64 | + { |
| 65 | + //format_visit_labeled_statement(ctx, p_statement->labeled_statement, error); |
| 66 | + } |
| 67 | + else if (p_statement->unlabeled_statement) |
| 68 | + { |
| 69 | + format_visit_unlabeled_statement(ctx, p_statement->unlabeled_statement, error); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +static void format_visit_selection_statement(struct format_visit_ctx* ctx, struct selection_statement* p_selection_statement, struct error* error) |
| 76 | +{ |
| 77 | + if (p_selection_statement->secondary_block) |
| 78 | + { |
| 79 | + |
| 80 | + ajust_line_and_identation(p_selection_statement->secondary_block->first, ctx); |
| 81 | + |
| 82 | + if (p_selection_statement->secondary_block && |
| 83 | + p_selection_statement->secondary_block->statement && |
| 84 | + p_selection_statement->secondary_block->statement->unlabeled_statement && |
| 85 | + p_selection_statement->secondary_block->statement->unlabeled_statement->primary_block && |
| 86 | + p_selection_statement->secondary_block->statement->unlabeled_statement->primary_block->compound_statement) |
| 87 | + { |
| 88 | + format_visit_statement(ctx, p_selection_statement->secondary_block->statement, error); |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + ctx->identation++; |
| 93 | + //ajust_line_and_identation(p_selection_statement->secondary_block->first, ctx); |
| 94 | + |
| 95 | + format_visit_statement(ctx, p_selection_statement->secondary_block->statement, error); |
| 96 | + ctx->identation--; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + //ajust_line_and_identation(p_selection_statement->secondary_block->last, ctx); |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +static void format_visit_jump_statement(struct format_visit_ctx* ctx, struct jump_statement* p_jump_statement, struct error* error) |
| 106 | +{ |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + if (p_jump_statement->token->type == TK_KEYWORD_THROW || |
| 111 | + p_jump_statement->token->type == TK_KEYWORD_RETURN || |
| 112 | + p_jump_statement->token->type == TK_KEYWORD_BREAK || |
| 113 | + p_jump_statement->token->type == TK_KEYWORD_CONTINUE || |
| 114 | + p_jump_statement->token->type == TK_KEYWORD_GOTO) |
| 115 | + { |
| 116 | + ajust_line_and_identation(p_jump_statement->token, ctx); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + assert(false); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +static void format_visit_compound_statement(struct format_visit_ctx* ctx, struct compound_statement* p_compound_statement, struct error* error); |
| 125 | + |
| 126 | + |
| 127 | +static void format_visit_secondary_block(struct format_visit_ctx* ctx, struct secondary_block* p_secondary_block, struct error* error) |
| 128 | +{ |
| 129 | + format_visit_statement(ctx, p_secondary_block->statement, error); |
| 130 | +} |
| 131 | + |
| 132 | +static void format_visit_iteration_statement(struct format_visit_ctx* ctx, struct iteration_statement* p_iteration_statement, struct error* error) |
| 133 | +{ |
| 134 | + |
| 135 | + if (p_iteration_statement->expression1) |
| 136 | + { |
| 137 | + //format_visit_expression(ctx, p_iteration_statement->expression1, error); |
| 138 | + } |
| 139 | + |
| 140 | + if (p_iteration_statement->expression2) |
| 141 | + { |
| 142 | + //format_visit_expression(ctx, p_iteration_statement->expression2, error); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + if (p_iteration_statement->secondary_block) |
| 148 | + { |
| 149 | + format_visit_secondary_block(ctx, p_iteration_statement->secondary_block, error); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +static void format_visit_primary_block(struct format_visit_ctx* ctx, struct primary_block* p_primary_block, struct error* error) |
| 156 | +{ |
| 157 | + if (p_primary_block->defer_statement) |
| 158 | + { |
| 159 | + //visit_defer_statement(ctx, p_primary_block->defer_statement, error); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + if (p_primary_block->compound_statement) |
| 164 | + { |
| 165 | + format_visit_compound_statement(ctx, p_primary_block->compound_statement, error); |
| 166 | + } |
| 167 | + else if (p_primary_block->iteration_statement) |
| 168 | + { |
| 169 | + format_visit_iteration_statement(ctx, p_primary_block->iteration_statement, error); |
| 170 | + } |
| 171 | + else if (p_primary_block->selection_statement) |
| 172 | + { |
| 173 | + format_visit_selection_statement(ctx, p_primary_block->selection_statement, error); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +static void format_visit_expression_statement(struct format_visit_ctx* ctx, struct expression_statement* p_expression_statement, struct error* error) |
| 181 | +{ |
| 182 | + if (p_expression_statement->expression) |
| 183 | + { |
| 184 | + //ajust_line_and_identation(p_expression_statement->first_token, ctx); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +static void format_visit_unlabeled_statement(struct format_visit_ctx* ctx, struct unlabeled_statement* p_unlabeled_statement, struct error* error) |
| 189 | +{ |
| 190 | + if (p_unlabeled_statement->primary_block) |
| 191 | + { |
| 192 | + format_visit_primary_block(ctx, p_unlabeled_statement->primary_block, error); |
| 193 | + } |
| 194 | + else if (p_unlabeled_statement->expression_statement) |
| 195 | + { |
| 196 | + format_visit_expression_statement(ctx, p_unlabeled_statement->expression_statement, error); |
| 197 | + } |
| 198 | + else if (p_unlabeled_statement->jump_statement) |
| 199 | + { |
| 200 | + format_visit_jump_statement(ctx, p_unlabeled_statement->jump_statement, error); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + assert(false); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +static void format_visit_block_item(struct format_visit_ctx* ctx, struct block_item* p_block_item, struct error* error) |
| 209 | +{ |
| 210 | + ajust_line_and_identation(p_block_item->first_token, ctx); |
| 211 | + |
| 212 | + if (p_block_item->declaration) |
| 213 | + { |
| 214 | + //visit_declaration(ctx, p_block_item->declaration, error); |
| 215 | + } |
| 216 | + else if (p_block_item->unlabeled_statement) |
| 217 | + { |
| 218 | + format_visit_unlabeled_statement(ctx, p_block_item->unlabeled_statement, error); |
| 219 | + } |
| 220 | + else if (p_block_item->labeled_statement) |
| 221 | + { |
| 222 | + //visit_labeled_statement(ctx, p_block_item->labeled_statement, error); |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +static void format_visit_block_item_list(struct format_visit_ctx* ctx, struct block_item_list* p_block_item_list, struct error* error) |
| 227 | +{ |
| 228 | + struct block_item* p_block_item = p_block_item_list->head; |
| 229 | + while (error->code == 0 && p_block_item) |
| 230 | + { |
| 231 | + format_visit_block_item(ctx, p_block_item, error); |
| 232 | + p_block_item = p_block_item->next; |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +static void format_visit_compound_statement(struct format_visit_ctx* ctx, struct compound_statement* p_compound_statement, struct error* error) |
| 237 | +{ |
| 238 | + ajust_line_and_identation(p_compound_statement->first, ctx); |
| 239 | + |
| 240 | + ctx->identation++; |
| 241 | + format_visit_block_item_list(ctx, &p_compound_statement->block_item_list, error); |
| 242 | + |
| 243 | + ctx->identation--; |
| 244 | + |
| 245 | + ajust_line_and_identation(p_compound_statement->last, ctx); |
| 246 | +} |
| 247 | + |
| 248 | +static void format_visit_declaration(struct format_visit_ctx* ctx, struct declaration* p_declaration, struct error* error) |
| 249 | +{ |
| 250 | + if (p_declaration->static_assert_declaration) |
| 251 | + { |
| 252 | + //format_visit_static_assert_declaration(ctx, p_declaration->static_assert_declaration, error); |
| 253 | + } |
| 254 | + |
| 255 | + if (p_declaration->declaration_specifiers) |
| 256 | + { |
| 257 | + //format_visit_declaration_specifiers(ctx, p_declaration->declaration_specifiers, error); |
| 258 | + |
| 259 | + } |
| 260 | + |
| 261 | + if (p_declaration->init_declarator_list.head) |
| 262 | + { |
| 263 | + //format_visit_init_declarator_list(ctx, &p_declaration->init_declarator_list, error); |
| 264 | + } |
| 265 | + |
| 266 | + if (p_declaration->function_body) |
| 267 | + { |
| 268 | + format_visit_compound_statement(ctx, p_declaration->function_body, error); |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +void format_visit(struct format_visit_ctx* ctx, struct error* error) |
| 273 | +{ |
| 274 | + struct declaration* p_declaration = ctx->ast.declaration_list.head; |
| 275 | + while (error->code == 0 && p_declaration) |
| 276 | + { |
| 277 | + format_visit_declaration(ctx, p_declaration, error); |
| 278 | + p_declaration = p_declaration->next; |
| 279 | + } |
| 280 | +} |
0 commit comments