Skip to content

Commit 331c5ea

Browse files
committed
progress, format fields, attrs
1 parent ad05c4a commit 331c5ea

File tree

8 files changed

+100
-63
lines changed

8 files changed

+100
-63
lines changed

lib/rivet/src/ast/Decl.ri

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,43 +140,6 @@ public enum Decl {
140140
self_pos: token.Pos;
141141
mut sym: Sym;
142142
mut defer_stmts: []Stmt.Defer;
143-
144-
public func stringify(self) -> string {
145-
mut sb := strings.Builder.new(500);
146-
if self.is_public {
147-
sb.write_string("public ");
148-
}
149-
sb.write_fmt("func {}(", self.name);
150-
if self.is_method {
151-
if self.self_is_ptr {
152-
sb.write_string("&");
153-
}
154-
if self.self_is_mut {
155-
sb.write_string("mut ");
156-
}
157-
sb.write_string("self");
158-
if self.args.len > 0 {
159-
sb.write_string(", ");
160-
}
161-
}
162-
for i, arg in self.args {
163-
if arg.is_mut {
164-
sb.write_string("mut ");
165-
}
166-
sb.write_fmt("{}: {}", arg.name, arg.type);
167-
if arg.has_def_expr {
168-
sb.write_fmt(" := {}", arg.def_expr);
169-
}
170-
if i < self.args.len - 1 {
171-
sb.write_string(", ");
172-
}
173-
}
174-
sb.write_byte(b')');
175-
if self.ret_type !is .Void {
176-
sb.write_fmt(" -> {}", self.ret_type);
177-
}
178-
return sb.to_string();
179-
}
180143
},
181144
Test {
182145
docs: []Comment;

lib/rivet/src/fmt/attrs.ri

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (C) 2023 The Rivet Developers. All rights reserved.
2+
// Use of this source code is governed by an MIT license that can
3+
// be found in the LICENSE file.
4+
5+
import ../ast;
6+
7+
extend Formatter {
8+
func format_attributes(mut self, attrs: ast.Attributes) {
9+
if attrs.is_empty() {
10+
return;
11+
}
12+
self.write("#[");
13+
for i, attr in attrs.attributes {
14+
self.write(attr.name);
15+
if !attr.args.is_empty() {
16+
self.write("(");
17+
for i2, arg in attr.args {
18+
if arg.is_named {
19+
self.write_fmt("{}: ", arg.name);
20+
}
21+
self.format_expr(arg.expr);
22+
if i2 < attr.args.len - 1 {
23+
self.write(", ");
24+
}
25+
}
26+
self.write(")");
27+
}
28+
if i < attrs.attributes.len - 1 {
29+
self.write("; ");
30+
}
31+
}
32+
self.writeln("]");
33+
}
34+
}

lib/rivet/src/fmt/comments.ri

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ extend Formatter {
1717
// - prev_line: the line number of the previous token to save linebreaks
1818
func format_comment(
1919
mut self, comment: ast.Comment, same_line: bool := false,
20-
level: CommentsLevel := .Keep
20+
level: CommentsLevel := .Keep, has_nl: bool := false
2121
) {
2222
if level == .Indent {
2323
self.indent += 1;
2424
}
2525
if !comment.is_multiline() {
2626
is_separate_line := !same_line or comment.text.starts_with("\x01");
2727
s := comment.text.trim_left("\x01").trim_right(" ");
28-
mut out_s := "//";
29-
if comment.is_doc {
30-
out_s = out_s.concat("/");
31-
}
28+
mut out_s := if comment.is_doc { "///" } else { "//" };
3229
if s != "" {
3330
if s[0].is_alnum() {
3431
out_s = out_s.concat(" ");
@@ -39,7 +36,10 @@ extend Formatter {
3936
_ = self.remove_new_line(); // delete the generated \n
4037
self.write(" ");
4138
}
42-
self.writeln(out_s);
39+
self.write(out_s);
40+
if has_nl {
41+
self.writeln();
42+
}
4343
} else {
4444
lines := comment.text.trim_space().split_into_lines();
4545
start_break := comment.text[0].is_alnum() or comment.text[0].is_space();
@@ -88,7 +88,7 @@ extend Formatter {
8888
) {
8989
self.writeln();
9090
}
91-
if i == 0 and self.output.len() > 1 and self.output.last_n(1)[0].is_space() {
91+
if i == 0 and self.output.len() > 1 and !self.output.last_n(1)[0].is_space() {
9292
self.write(" ");
9393
}
9494
self.format_comment(comment, same_line, level);

lib/rivet/src/fmt/decls.ri

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,35 @@ import ../ast;
66
import ../token;
77

88
extend Formatter {
9-
func format_decls(mut self, decls: []ast.Decl) {
9+
func format_decls(mut self, decls: []ast.Decl, is_root: bool := false) {
1010
mut prev_decl := if decls.len > 0 {
1111
decls[0]
1212
} else {
1313
.Empty(token.noPos)
1414
};
15-
self.indent += 1;
15+
if !is_root {
16+
self.indent += 1;
17+
}
1618
for decl in decls {
1719
if self.should_insert_newline_before_node(.Decl(decl), .Decl(prev_decl)) {
1820
self.writeln();
1921
}
2022
self.format_decl(decl);
2123
prev_decl = decl;
2224
}
23-
self.indent -= 1;
25+
if !is_root {
26+
self.indent -= 1;
27+
}
2428
}
2529

2630
func format_decl(mut self, decl: ast.Decl) {
2731
match decl is {
28-
.Comment as comment -> self.format_comment(comment, same_line: true),
32+
.Comment as comment -> self.format_comment(comment, has_nl: true),
2933
.Struct as struct_decl -> self.format_struct(struct_decl),
3034
.Func as func_decl -> self.format_func(func_decl),
3135
.Field as field -> {
3236
self.format_comments(field.docs);
37+
self.format_attributes(field.attributes);
3338
if field.is_public {
3439
self.write("public ");
3540
}
@@ -49,6 +54,7 @@ extend Formatter {
4954

5055
func format_struct(mut self, struct_decl: ast.Decl.Struct) {
5156
self.format_comments(struct_decl.docs);
57+
self.format_attributes(struct_decl.attributes);
5258
if struct_decl.is_public {
5359
self.write("public ");
5460
}
@@ -64,7 +70,40 @@ extend Formatter {
6470

6571
func format_func(mut self, func_decl: ast.Decl.Func) {
6672
self.format_comments(func_decl.docs);
67-
self.write(func_decl.stringify());
73+
self.format_attributes(func_decl.attributes);
74+
if func_decl.is_public {
75+
self.write("public ");
76+
}
77+
self.write_fmt("func {}(", func_decl.name);
78+
if func_decl.is_method {
79+
if func_decl.self_is_ptr {
80+
self.write("&");
81+
}
82+
if func_decl.self_is_mut {
83+
self.write("mut ");
84+
}
85+
self.write("self");
86+
if func_decl.args.len > 0 {
87+
self.write(", ");
88+
}
89+
}
90+
for i, arg in func_decl.args {
91+
if arg.is_mut {
92+
self.write("mut ");
93+
}
94+
self.write_fmt("{}: {}", arg.name, arg.type);
95+
if arg.has_def_expr {
96+
self.write(" := ");
97+
self.format_expr(arg.def_expr);
98+
}
99+
if i < func_decl.args.len - 1 {
100+
self.write(", ");
101+
}
102+
}
103+
self.write(")");
104+
if func_decl.ret_type !is .Void {
105+
self.write_fmt(" -> {}", func_decl.ret_type);
106+
}
68107
if func_decl.is_extern or !func_decl.has_body {
69108
self.writeln(";");
70109
} else {

lib/rivet/src/fmt/exprs.ri

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ extend Formatter {
2020
self.format_stmts(block.stmts);
2121
}
2222
if block.is_expr {
23-
self.writeln()
23+
self.writeln();
24+
self.format_expr(block.expr);
2425
}
25-
self.writeln("}");
26+
self.write("}");
2627
},
2728
else -> {}
2829
}

lib/rivet/src/fmt/mod.ri

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public struct Formatter {
2828

2929
public func format(mut self, source_file: ast.SourceFile) -> string {
3030
self.source_file = source_file;
31-
self.format_decls(source_file.decls);
31+
self.format_decls(source_file.decls, is_root: true);
3232
return self.output.to_string();
3333
}
3434

@@ -87,11 +87,7 @@ public struct Formatter {
8787
// when the removal action actually occurs, the string of the last line after
8888
// the removal is returned
8989
func remove_new_line(mut self, imports_output: bool := false) -> string {
90-
mut buffer := if imports_output {
91-
self.imports_output
92-
} else {
93-
self.output
94-
};
90+
mut buffer := if imports_output { self.imports_output } else { self.output };
9591
buffer_len := if buffer.len() > 0 { buffer.len() - 1 } else { 0 };
9692
mut i := buffer_len;
9793
while i >= 0 : i -= 1 {
@@ -105,7 +101,7 @@ public struct Formatter {
105101
if i == buffer_len {
106102
return "";
107103
}
108-
buffer.go_back(if buffer.len() > 0 { buffer.len() - i - 1 } else { i - 1 });
104+
buffer.go_back(if i > 0 { i - 1 } else { buffer.len() - i - 1 });
109105
self.empty_line = false;
110106
mut line_len: uint := 0;
111107
mut last_line_str := @vec(uint8);
@@ -135,10 +131,15 @@ public struct Formatter {
135131
if node is .Decl as decl and prev_node is .Decl as prev_decl {
136132
match prev_decl is {
137133
// Force a newline after function declarations
138-
// The only exception is inside a block of no_body functions
134+
// The only exception is inside a block of no body functions
139135
.Func as func_decl if decl !is .Func or !func_decl.has_body -> return true,
140-
.Struct -> return true, // // Force a newline after struct declarations
136+
.Struct -> return true, // Force a newline after struct declarations
141137
.Alias -> return prev_decl !is .Alias,
138+
.Field as prev_field_d -> return if decl is .Field as field_d {
139+
field_d.pos.line - prev_field_d.pos.line == 2
140+
} else {
141+
false
142+
},
142143
.Import -> return false,
143144
else -> {}
144145
}

lib/rivet/src/fmt/stmts.ri

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// be found in the LICENSE file.
44

55
import ../ast;
6-
import ../token;
76

87
extend Formatter {
98
func format_stmts(mut self, stmts: []ast.Stmt) {
@@ -16,7 +15,7 @@ extend Formatter {
1615

1716
func format_stmt(mut self, stmt: ast.Stmt) {
1817
match stmt is {
19-
.Comment as comment -> self.format_comment(comment),
18+
.Comment as comment -> self.format_comment(comment, has_nl: true),
2019
.Expr as expr -> self.format_expr(expr),
2120
else -> {}
2221
}

lib/rivet/src/parser/decls.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ../token;
1111
extend Parser {
1212
func parse_doc_comments(mut self) -> []ast.Comment {
1313
mut comments := @vec(ast.Comment);
14-
while self.tok.kind == .DocComment {
14+
while self.tok.kind == .DocComment or (self.tok.kind == .Comment and comments.len > 0) {
1515
comments.push(self.parse_comment());
1616
}
1717
return comments;

0 commit comments

Comments
 (0)