Skip to content

Commit 9841f02

Browse files
committed
feat(rivet.tokenizer): tokenize comments
1 parent fccbc37 commit 9841f02

File tree

5 files changed

+51
-21
lines changed

5 files changed

+51
-21
lines changed

lib/rivet/src/ast/Decl.ri

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public enum Decl {
2121
pos: token.Pos;
2222
},
2323
Extern {
24-
docs: DocComment;
24+
docs: []Comment;
2525
attributes: Attributes;
2626
abi: ABI;
2727
decls: []Decl;
2828
pos: token.Pos;
2929
},
3030
Alias {
31-
docs: DocComment;
31+
docs: []Comment;
3232
attributes: Attributes;
3333
is_public: bool;
3434
name: string;
@@ -39,7 +39,7 @@ public enum Decl {
3939
mut sym: Sym;
4040
},
4141
Trait {
42-
docs: DocComment;
42+
docs: []Comment;
4343
attributes: Attributes;
4444
is_public: bool;
4545
name: string;
@@ -49,7 +49,7 @@ public enum Decl {
4949
mut sym: Sym;
5050
},
5151
Enum {
52-
docs: DocComment;
52+
docs: []Comment;
5353
attributes: Attributes;
5454
is_public: bool;
5555
name: string;
@@ -62,7 +62,7 @@ public enum Decl {
6262
mut sym: Sym;
6363
},
6464
Struct {
65-
docs: DocComment;
65+
docs: []Comment;
6666
attributes: Attributes;
6767
is_public: bool;
6868
name: string;
@@ -73,7 +73,7 @@ public enum Decl {
7373
mut sym: Sym;
7474
},
7575
Field {
76-
docs: DocComment;
76+
docs: []Comment;
7777
attributes: Attributes;
7878
is_public: bool;
7979
is_mut: bool;
@@ -91,7 +91,7 @@ public enum Decl {
9191
pos: token.Pos;
9292
},
9393
Const {
94-
docs: DocComment;
94+
docs: []Comment;
9595
attributes: Attributes;
9696
is_public: bool;
9797
name: string;
@@ -102,7 +102,7 @@ public enum Decl {
102102
mut sym: Sym;
103103
},
104104
Var {
105-
docs: DocComment;
105+
docs: []Comment;
106106
attributes: Attributes;
107107
is_public: bool;
108108
is_extern: bool;
@@ -114,7 +114,7 @@ public enum Decl {
114114
mut scope: Scope;
115115
},
116116
Func {
117-
docs: DocComment;
117+
docs: []Comment;
118118
attributes: Attributes;
119119
is_public: bool;
120120
is_extern: bool;
@@ -141,7 +141,7 @@ public enum Decl {
141141
mut defer_stmts: []Stmt.Defer;
142142
},
143143
Test {
144-
docs: DocComment;
144+
docs: []Comment;
145145
attributes: Attributes;
146146
name: string;
147147
stmts: []mut Stmt;
@@ -174,10 +174,16 @@ public struct EnumVariantDecl {
174174
}
175175

176176
#[boxed]
177-
public struct DocComment {
177+
public struct Comment {
178+
mut is_doc: bool;
178179
mut lines: []string;
179180
pos: token.Pos;
180181

182+
#[inline]
183+
func is_multiline(self) -> bool {
184+
return self.lines.len > 1;
185+
}
186+
181187
#[inline]
182188
func is_empty(self) -> bool {
183189
return self.lines.is_empty();

lib/rivet/src/parser/decls.ri

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import ../report;
99
import ../token;
1010

1111
extend Parser {
12-
func parse_doc_comment(mut self) -> ast.DocComment {
13-
mut pos := self.tok.pos;
14-
mut lines := @vec(string);
15-
while self.accept(.DocComment) {
16-
lines.push(self.prev_tok.lit);
17-
pos += self.prev_tok.pos;
12+
func parse_doc_comment(mut self) -> []ast.Comment {
13+
mut comments := @vec(ast.Comment);
14+
while self.accept(.DocComment) or self.accept(.Comment) {
15+
comments.push(
16+
ast.Comment(
17+
self.prev_tok.kind == .DocComment,
18+
self.prev_tok.lit.split_into_lines(),
19+
self.prev_tok.pos
20+
)
21+
);
1822
}
19-
return ast.DocComment(lines, pos);
23+
return comments;
2024
}
2125

2226
public func parse_attributes(
@@ -527,7 +531,7 @@ extend Parser {
527531
}
528532

529533
func parse_func_decl(
530-
mut self, docs: ast.DocComment, attributes: ast.Attributes,
534+
mut self, docs: []ast.Comment, attributes: ast.Attributes,
531535
is_public: bool, is_unsafe: bool, abi: ast.ABI
532536
) -> ast.Decl {
533537
mut is_operator := false;

lib/rivet/src/prefs/mod.ri

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public struct Prefs {
5959
public mut mod_dir: string;
6060
public mut mod_output: string;
6161

62+
public mut is_fmt: bool;
63+
public mut is_docs: bool;
64+
public mut is_test: bool;
65+
6266
public mut library_path: []string := [rivetcLibDir, libDir];
6367
public mut libraries_to_link: []string;
6468
public mut objects_to_link: []string;
@@ -71,12 +75,10 @@ public struct Prefs {
7175
public mut all_warns_are_errors: bool;
7276
public mut hide_all_warns: bool;
7377
public mut is_verbose: bool;
74-
public mut is_fmt: bool;
7578

7679
// Output info
7780
public mut output_mode: OutputMode;
7881
public mut optimize_mode: OptimizeMode;
79-
public mut is_test: bool;
8082
public mut run_output: bool;
8183
public mut run_output_args: []string;
8284

lib/rivet/src/token/Kind.ri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static kindStrings := [
1414
KindMap(.Unknown, "unknown"),
1515
KindMap(.EndOfFile, "end of file"),
1616
KindMap(.DocComment, "doc-comment"),
17+
KindMap(.Comment, "comment"),
1718
KindMap(.Name, "name"),
1819
KindMap(.Number, "number"),
1920
KindMap(.Char, "character"),
@@ -111,6 +112,7 @@ public enum Kind < traits.Stringable {
111112
Unknown, // unknown
112113
EndOfFile, // end of file
113114
DocComment, // doc-comment
115+
Comment, // comment
114116
Name, // name
115117
Number, // number
116118
Char, // character

lib/rivet/src/tokenizer/next.ri

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,23 @@ extend Tokenizer {
8585
self.pos += 1;
8686
return self.new_token(.DivAssign, len: 2);
8787
} else if nextc == b'/' {
88+
is_doc_comment := self.look_ahead(2) == b'/';
89+
start := self.pos + if is_doc_comment { 3 } else { 0 };
8890
self.ignore_line();
91+
end := self.pos;
8992
if self.text[self.pos - 1] != CR {
9093
self.pos -= 1;
9194
self.line -= 1;
9295
}
96+
if (is_doc_comment and self.prefs.is_docs) or self.prefs.is_fmt {
97+
line := self.text[start..end].trim_space();
98+
return self.new_token(.Comment, line, line.len);
99+
}
93100
continue;
94101
} else if nextc == b'*' {
95102
start_pos := self.pos;
103+
start_line := self.line;
104+
start_col := utils.max(1, self.current_column()) - 1;
96105
self.pos += 1;
97106
mut nest_count := 1;
98107
while nest_count > 0 and self.pos < self.text.len - 1 {
@@ -109,12 +118,19 @@ extend Tokenizer {
109118
nest_count -= 1;
110119
}
111120
}
121+
end_pos := self.pos;
112122
self.pos += 1;
113123
if self.pos >= self.text.len {
114124
self.pos = start_pos;
115125
self.error("comment not terminated");
116126
break;
117127
}
128+
if self.prefs.is_fmt {
129+
line := self.text[start_pos..end_pos].trim_space();
130+
return self.new_multiline_token(
131+
.Comment, line, line.len, start_line, self.line, start_col
132+
);
133+
}
118134
continue;
119135
}
120136
return self.new_token(.Div);

0 commit comments

Comments
 (0)