Skip to content

Commit c1b1055

Browse files
committed
basic format func decls
1 parent c1bb312 commit c1b1055

File tree

9 files changed

+121
-16
lines changed

9 files changed

+121
-16
lines changed

lib/rivet/src/ast/Decl.ri

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by an MIT license that can
33
// be found in the LICENSE file.
44

5+
import std/strings;
6+
57
import ../token;
68

79
public enum Decl {
@@ -138,6 +140,43 @@ public enum Decl {
138140
self_pos: token.Pos;
139141
mut sym: Sym;
140142
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+
}
141180
},
142181
Test {
143182
docs: []Comment;

lib/rivet/src/fmt/comments.ri

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extend Formatter {
1313
// Optional arguments defines the way comments are going to be written
1414
// - has_nl: adds an newline at the end of a list of comments
1515
// - same_line: line comments will be on the same line as the last statement
16-
// - level: either .keep (don't indent), or .indent (increment indentation)
16+
// - level: either .Keep (don't indent), or .Indent (increment indentation)
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,
@@ -22,10 +22,13 @@ extend Formatter {
2222
if level == .Indent {
2323
self.indent += 1;
2424
}
25-
if !comment.text.contains("\n") {
25+
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(" ");
2828
mut out_s := "//";
29+
if comment.is_doc {
30+
out_s = out_s.concat("/");
31+
}
2932
if s != "" {
3033
if s[0].is_alnum() {
3134
out_s = out_s.concat(" ");
@@ -36,9 +39,6 @@ extend Formatter {
3639
_ = self.remove_new_line(); // delete the generated \n
3740
self.write(" ");
3841
}
39-
if comment.is_doc {
40-
self.write("/");
41-
}
4242
self.writeln(out_s);
4343
} else {
4444
lines := comment.text.trim_space().split_into_lines();

lib/rivet/src/fmt/decls.ri

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,23 @@ extend Formatter {
1313

1414
func format_decl(mut self, decl: ast.Decl) {
1515
match decl is {
16-
.Comment as comment -> self.format_comment(comment),
16+
.Comment as comment -> self.format_comment(comment, same_line: true),
17+
.Func as func_decl -> self.format_func(func_decl),
1718
else -> {}
1819
}
1920
}
21+
22+
func format_func(mut self, func_decl: ast.Decl.Func) {
23+
self.format_comments(func_decl.docs, same_line: true);
24+
self.write(func_decl.stringify());
25+
if func_decl.is_extern {
26+
self.writeln(";");
27+
} else {
28+
self.writeln(" {");
29+
//self.indent += 1;
30+
self.format_stmts(func_decl.stmts);
31+
//self.indent -= 1;
32+
self.writeln("}");
33+
}
34+
}
2035
}

lib/rivet/src/fmt/mod.ri

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import std/strings;
66

77
import ../ast;
8+
import ../utils;
89
import ../prefs;
910

1011
static maxLen: []uint := [0, 35, 60, 85, 93, 100];
@@ -47,7 +48,7 @@ public struct Formatter {
4748
}
4849

4950
func write_indent(mut self) {
50-
self.output.write_string("\t".repeat(self.indent));
51+
self.output.write_string(utils.tabs(self.indent));
5152
self.line_len += self.indent * 4;
5253
}
5354

lib/rivet/src/fmt/stmts.ri

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_stmts(mut self, stmts: []ast.Stmt) {
9+
for stmt in stmts {
10+
self.format_stmt(stmt);
11+
}
12+
}
13+
14+
func format_stmt(mut self, stmt: ast.Stmt) {
15+
match stmt is {
16+
.Comment as comment -> self.format_comment(comment, same_line: true),
17+
else -> {}
18+
}
19+
}
20+
}

lib/rivet/src/parser/decls.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extend Parser {
1212
func parse_doc_comments(mut self) -> []ast.Comment {
1313
mut comments := @vec(ast.Comment);
1414
while self.tok.kind == .DocComment {
15-
comments.push(self.parse_comment(true));
15+
comments.push(self.parse_comment());
1616
}
1717
return comments;
1818
}

lib/rivet/src/parser/mod.ri

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ public struct Parser {
156156
return false;
157157
}
158158

159-
func parse_comment(mut self, parse_doc_comment: bool := false) -> ast.Comment {
160-
if !((parse_doc_comment and self.accept(.DocComment)) or self.accept(.Comment)) {
161-
return ast.Comment();
159+
func parse_comment(mut self) -> ast.Comment {
160+
if self.prefs.is_fmt or (self.prefs.is_docs and self.tok.kind == .DocComment) {
161+
if self.accept(.Comment) or self.accept(.DocComment) {
162+
return ast.Comment(
163+
self.prev_tok.kind == .DocComment, self.prev_tok.lit, self.prev_tok.pos
164+
);
165+
}
162166
}
163-
return ast.Comment(
164-
self.prev_tok.kind == .DocComment, self.prev_tok.lit, self.prev_tok.pos
165-
);
167+
return ast.Comment();
166168
}
167169

168170
// NOTE:

lib/rivet/src/tokenizer/next.ri

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extend Tokenizer {
9797
if (is_doc_comment and self.prefs.is_docs) or self.prefs.is_fmt {
9898
mut comment := self.text[start_pos..end_pos];
9999
// find out if this comment is on its own line (for rivet format)
100-
mut is_separate_line_comment := false;
100+
mut is_separate_line_comment := true;
101101
mut j := start_pos - bytes_to_skip;
102102
while j >= 0 and self.text[j] != LF : j -= 1 {
103103
if self.text[j] !in [b'\t', b' '] {
@@ -110,7 +110,13 @@ extend Tokenizer {
110110
// comments that are on a separate line
111111
comment = "\x01".concat(comment);
112112
}
113-
return self.new_token(.Comment, comment, comment.len);
113+
return self.new_token(
114+
if is_doc_comment {
115+
.DocComment
116+
} else {
117+
.Comment
118+
}, comment, comment.len
119+
);
114120
}
115121
continue;
116122
} else if nextc == b'*' {

lib/rivet/src/utils/mod.ri

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ public const CR: uint8 := 13;
1212

1313
public static mut stderrSupportStyles := styles.stderr_support_styles();
1414

15+
public static tabsTable := [
16+
"",
17+
"\t",
18+
"\t\t",
19+
"\t\t\t",
20+
"\t\t\t\t",
21+
"\t\t\t\t\t",
22+
"\t\t\t\t\t\t",
23+
"\t\t\t\t\t\t\t",
24+
"\t\t\t\t\t\t\t\t",
25+
"\t\t\t\t\t\t\t\t\t",
26+
"\t\t\t\t\t\t\t\t\t\t"
27+
];
28+
29+
public func tabs(n: uint) -> string {
30+
return if n < tabsTable.len {
31+
tabsTable[n]
32+
} else {
33+
"\t".repeat(n)
34+
};
35+
}
36+
1537
#[inline]
1638
public func min(a: uint, b: uint) -> uint {
1739
return if a < b { a } else { b };

0 commit comments

Comments
 (0)