Correct newline_limit logic

This commit is contained in:
gingerBill
2021-05-21 15:22:35 +01:00
parent f0c9f82e1b
commit 9e2eb717fe
4 changed files with 26 additions and 53 deletions
+6 -10
View File
@@ -16,16 +16,12 @@ Proc_Inlining :: enum u32 {
No_Inline = 2, No_Inline = 2,
} }
Proc_Calling_Convention :: enum i32 { Proc_Calling_Convention_Extra :: enum i32 {
Invalid = 0, Foreign_Block_Default,
Odin, }
Contextless, Proc_Calling_Convention :: union {
C_Decl, string,
Std_Call, Proc_Calling_Convention_Extra,
Fast_Call,
None,
Foreign_Block_Default = -1,
} }
Node_State_Flag :: enum { Node_State_Flag :: enum {
+6
View File
@@ -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) { format :: proc(filepath: string, source: string, config: printer.Config, parser_flags := parser.Flags{}, allocator := context.allocator) -> (string, bool) {
config := config;
pkg := ast.Package { pkg := ast.Package {
kind = .Normal, kind = .Normal,
}; };
@@ -21,6 +23,10 @@ format :: proc(filepath: string, source: string, config: printer.Config, parser_
fullpath = filepath, 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); p := parser.default_parser(parser_flags);
ok := parser.parse_file(&p, &file); ok := parser.parse_file(&p, &file);
+8 -24
View File
@@ -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 { string_to_calling_convention :: proc(s: string) -> ast.Proc_Calling_Convention {
if s[0] != '"' && s[0] != '`' { if s[0] != '"' && s[0] != '`' {
return .Invalid; return nil;
} }
switch s[1:len(s)-1] { if len(s) == 2 {
case "odin": return nil;
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;
} }
return .Invalid; return s;
} }
parse_proc_tags :: proc(p: ^Parser) -> (tags: ast.Proc_Tags) { 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 { 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 { if p.curr_tok.kind == .String {
str := expect_token(p, .String); str := expect_token(p, .String);
cc = string_to_calling_convention(str.text); 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); error(p, str.pos, "unknown calling convention '%s'", str.text);
} }
} }
if cc == ast.Proc_Calling_Convention.Invalid { if cc == nil && p.in_foreign_block {
if p.in_foreign_block { cc = .Foreign_Block_Default;
cc = ast.Proc_Calling_Convention.Foreign_Block_Default;
} else {
cc = ast.Proc_Calling_Convention.Odin;
}
} }
expect_token(p, .Open_Paren); expect_token(p, .Open_Paren);
+6 -19
View File
@@ -161,12 +161,12 @@ push_comments :: proc(p: ^Printer, pos: tokenizer.Pos) {
if prev_comment == nil { if prev_comment == nil {
lines := comment_group.pos.line - p.last_source_position.line; 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 { for comment, i in comment_group.list {
if prev_comment != nil && p.last_source_position.line != comment.pos.line { 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); prev_comment_lines = push_comment(p, comment);
@@ -177,7 +177,7 @@ push_comments :: proc(p: ^Printer, pos: tokenizer.Pos) {
} }
if prev_comment != nil { 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) @(private)
move_line :: proc(p: ^Printer, pos: tokenizer.Pos) { 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) @(private)
@@ -1368,22 +1368,9 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type, is_proc_lit := fa
explicit_calling := false; explicit_calling := false;
switch proc_type.calling_convention { if v, ok := proc_type.calling_convention.(string); ok {
case .Odin:
case .Contextless:
push_string_token(p, "\"contextless\"", 1);
explicit_calling = true; explicit_calling = true;
case .C_Decl: push_string_token(p, v, 1);
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
} }
if explicit_calling { if explicit_calling {