From 9e2eb717fe2a146ccd15e66a45d48d48f867e097 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 21 May 2021 15:22:35 +0100 Subject: [PATCH] Correct `newline_limit` logic --- core/odin/ast/ast.odin | 16 ++++++---------- core/odin/format/format.odin | 6 ++++++ core/odin/parser/parser.odin | 32 ++++++++------------------------ core/odin/printer/visit.odin | 25 ++++++------------------- 4 files changed, 26 insertions(+), 53 deletions(-) diff --git a/core/odin/ast/ast.odin b/core/odin/ast/ast.odin index cf2cdeacc..b213c4b74 100644 --- a/core/odin/ast/ast.odin +++ b/core/odin/ast/ast.odin @@ -16,16 +16,12 @@ Proc_Inlining :: enum u32 { No_Inline = 2, } -Proc_Calling_Convention :: enum i32 { - Invalid = 0, - Odin, - Contextless, - C_Decl, - Std_Call, - Fast_Call, - None, - - Foreign_Block_Default = -1, +Proc_Calling_Convention_Extra :: enum i32 { + Foreign_Block_Default, +} +Proc_Calling_Convention :: union { + string, + Proc_Calling_Convention_Extra, } Node_State_Flag :: enum { diff --git a/core/odin/format/format.odin b/core/odin/format/format.odin index c231e0afd..325881db8 100644 --- a/core/odin/format/format.odin +++ b/core/odin/format/format.odin @@ -11,6 +11,8 @@ simplify :: proc(file: ^ast.File) { } format :: proc(filepath: string, source: string, config: printer.Config, parser_flags := parser.Flags{}, allocator := context.allocator) -> (string, bool) { + config := config; + pkg := ast.Package { kind = .Normal, }; @@ -21,6 +23,10 @@ format :: proc(filepath: string, source: string, config: printer.Config, parser_ fullpath = filepath, }; + config.newline_limit = clamp(config.newline_limit, 0, 16); + config.spaces = clamp(config.spaces, 1, 16); + config.align_length_break = clamp(config.align_length_break, 0, 64); + p := parser.default_parser(parser_flags); ok := parser.parse_file(&p, &file); diff --git a/core/odin/parser/parser.odin b/core/odin/parser/parser.odin index efdee41f4..7eee8f5b5 100644 --- a/core/odin/parser/parser.odin +++ b/core/odin/parser/parser.odin @@ -1939,24 +1939,12 @@ parse_results :: proc(p: ^Parser) -> (list: ^ast.Field_List, diverging: bool) { string_to_calling_convention :: proc(s: string) -> ast.Proc_Calling_Convention { if s[0] != '"' && s[0] != '`' { - return .Invalid; + return nil; } - switch s[1:len(s)-1] { - case "odin": - return .Odin; - case "contextless": - return .Contextless; - case "cdecl", "c": - return .C_Decl; - case "stdcall", "std": - return .Std_Call; - case "fast", "fastcall": - return .Fast_Call; - - case "none": - return .None; + if len(s) == 2 { + return nil; } - return .Invalid; + return s; } parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) { @@ -1981,21 +1969,17 @@ parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) { } parse_proc_type :: proc(p: ^Parser, tok: tokenizer.Token) -> ^ast.Proc_Type { - cc := ast.Proc_Calling_Convention.Invalid; + cc: ast.Proc_Calling_Convention; if p.curr_tok.kind == .String { str := expect_token(p, .String); cc = string_to_calling_convention(str.text); - if cc == ast.Proc_Calling_Convention.Invalid { + if cc == nil { error(p, str.pos, "unknown calling convention '%s'", str.text); } } - if cc == ast.Proc_Calling_Convention.Invalid { - if p.in_foreign_block { - cc = ast.Proc_Calling_Convention.Foreign_Block_Default; - } else { - cc = ast.Proc_Calling_Convention.Odin; - } + if cc == nil && p.in_foreign_block { + cc = .Foreign_Block_Default; } expect_token(p, .Open_Paren); diff --git a/core/odin/printer/visit.odin b/core/odin/printer/visit.odin index 0e492af70..3eab3b4b3 100644 --- a/core/odin/printer/visit.odin +++ b/core/odin/printer/visit.odin @@ -161,12 +161,12 @@ push_comments :: proc(p: ^Printer, pos: tokenizer.Pos) { if prev_comment == nil { lines := comment_group.pos.line - p.last_source_position.line; - set_line(p, p.last_line_index + min(p.config.newline_limit, lines)); + set_line(p, p.last_line_index + min(p.config.newline_limit+1, lines)); } for comment, i in comment_group.list { if prev_comment != nil && p.last_source_position.line != comment.pos.line { - newline_position(p, min(p.config.newline_limit, comment.pos.line - prev_comment.pos.line - prev_comment_lines)); + newline_position(p, min(p.config.newline_limit+1, comment.pos.line - prev_comment.pos.line - prev_comment_lines)); } prev_comment_lines = push_comment(p, comment); @@ -177,7 +177,7 @@ push_comments :: proc(p: ^Printer, pos: tokenizer.Pos) { } if prev_comment != nil { - newline_position(p, min(p.config.newline_limit, p.source_position.line - prev_comment.pos.line - prev_comment_lines)); + newline_position(p, min(p.config.newline_limit+1, p.source_position.line - prev_comment.pos.line - prev_comment_lines)); } } @@ -268,7 +268,7 @@ set_source_position :: proc(p: ^Printer, pos: tokenizer.Pos) { @(private) move_line :: proc(p: ^Printer, pos: tokenizer.Pos) { - move_line_limit(p, pos, p.config.newline_limit); + move_line_limit(p, pos, p.config.newline_limit+1); } @(private) @@ -1368,22 +1368,9 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type, is_proc_lit := fa explicit_calling := false; - switch proc_type.calling_convention { - case .Odin: - case .Contextless: - push_string_token(p, "\"contextless\"", 1); + if v, ok := proc_type.calling_convention.(string); ok { explicit_calling = true; - case .C_Decl: - push_string_token(p, "\"c\"", 1); - explicit_calling = true; - case .Std_Call: - push_string_token(p, "\"std\"", 1); - explicit_calling = true; - case .Fast_Call: - push_string_token(p, "\"fast\"", 1); - explicit_calling = true; - case .None, .Invalid, .Foreign_Block_Default: - // nothing + push_string_token(p, v, 1); } if explicit_calling {