From 9c6ab059810624043b0ebab9cc1b87a110457e7a Mon Sep 17 00:00:00 2001 From: Daniel Gavin Date: Thu, 29 Apr 2021 00:51:24 +0200 Subject: [PATCH] fix tokenizer for ~= and better struct aligning --- core/odin/printer/printer.odin | 19 ++++++++++++------- core/odin/printer/visit.odin | 9 ++++++++- core/odin/tokenizer/tokenizer.odin | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/core/odin/printer/printer.odin b/core/odin/printer/printer.odin index 63c0c5fc7..407e5a415 100644 --- a/core/odin/printer/printer.odin +++ b/core/odin/printer/printer.odin @@ -350,8 +350,8 @@ format_keyword_to_brace :: proc(p: ^Printer, line_index: int, format_index: int, keyword_found := false; keyword_token: Format_Token; keyword_line: int; - largest := 0; + largest := 0; brace_count := 0; done := false; @@ -385,6 +385,10 @@ format_keyword_to_brace :: proc(p: ^Printer, line_index: int, format_index: int, break; } + if format_token.kind == .Undef || format_token.kind == .Comma { + return; + } + if line_index == 0 && i <= format_index { continue; } @@ -489,18 +493,20 @@ align_var_decls :: proc(p: ^Printer) { not_mutable = true; } - if line.format_tokens[i].kind == .Proc || - line.format_tokens[i].kind == .Union || + if line.format_tokens[i].kind == .Union || line.format_tokens[i].kind == .Enum || line.format_tokens[i].kind == .Struct || line.format_tokens[i].kind == .For || - line.format_tokens[i].kind == .If { + line.format_tokens[i].kind == .If || + line.format_tokens[i].kind == .Comment { continue_flag = true; } - if line.format_tokens[i].kind == .Comment { + //enforced undef is always on the last line, if it exists + if line.format_tokens[i].kind == .Proc && line.format_tokens[len(line.format_tokens)-1].kind != .Undef { continue_flag = true; } + } if continue_flag { @@ -601,7 +607,6 @@ align_var_decls :: proc(p: ^Printer) { } align_switch_stmt :: proc(p: ^Printer, index: int) { - switch_found := false; brace_token: Format_Token; brace_line: int; @@ -739,7 +744,6 @@ align_enum :: proc(p: ^Printer, index: int) { } align_struct :: proc(p: ^Printer, index: int) { - struct_found := false; brace_token: Format_Token; brace_line: int; @@ -795,6 +799,7 @@ align_struct :: proc(p: ^Printer, index: int) { format_tokens[colon_count] = {format_token = &line.format_tokens[i + 1], length = length}; colon_count += 1; largest = max(length, largest); + break; } length += len(format_token.text) + format_token.spaces_before; diff --git a/core/odin/printer/visit.odin b/core/odin/printer/visit.odin index 003bb8332..a8f7068c8 100644 --- a/core/odin/printer/visit.odin +++ b/core/odin/printer/visit.odin @@ -461,8 +461,10 @@ visit_decl :: proc(p: ^Printer, decl: ^ast.Decl, called_in_stmt := false) { for value in v.values { switch a in value.derived { - case Proc_Lit, Union_Type, Enum_Type, Struct_Type: + case Union_Type, Enum_Type, Struct_Type: add_semicolon = false || called_in_stmt; + case Proc_Lit: + add_semicolon = false; } } @@ -532,6 +534,9 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener } switch v in stmt.derived { + case Import_Decl: + visit_decl(p, cast(^Decl)stmt, true); + return; case Value_Decl: visit_decl(p, cast(^Decl)stmt, true); return; @@ -693,6 +698,8 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener case Type_Switch_Stmt: move_line(p, v.pos); + hint_current_line(p, {.Switch_Stmt}); + if v.label != nil { visit_expr(p, v.label); push_generic_token(p, .Colon, 0); diff --git a/core/odin/tokenizer/tokenizer.odin b/core/odin/tokenizer/tokenizer.odin index 58f546191..b1b446192 100644 --- a/core/odin/tokenizer/tokenizer.odin +++ b/core/odin/tokenizer/tokenizer.odin @@ -608,7 +608,7 @@ scan :: proc(t: ^Tokenizer) -> Token { kind = switch3(t, .And, .And_Eq, '&', .Cmp_And); } case '|': kind = switch3(t, .Or, .Or_Eq, '|', .Cmp_Or); - case '~': kind = .Xor; + case '~': kind = switch2(t, .Xor, .Xor_Eq); case '<': kind = switch4(t, .Lt, .Lt_Eq, '<', .Shl, .Shl_Eq); case '>': kind = switch4(t, .Gt, .Gt_Eq, '>', .Shr,.Shr_Eq);