Skip to content

Commit d38b7d8

Browse files
committed
feat(rivet/parser): parse comments
1 parent 9841f02 commit d38b7d8

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

lib/rivet/src/ast/Stmt.ri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ../token;
66

77
public enum Stmt {
88
Empty: token.Pos,
9+
Comment: Comment,
910
Expr: Expr,
1011
VarDecl {
1112
lefts: []ObjectData;
@@ -46,6 +47,7 @@ public enum Stmt {
4647
public func position(self) -> token.Pos {
4748
return match self is {
4849
.Empty as empty_pos -> empty_pos,
50+
.Comment as comment -> comment.pos,
4951
.Expr as expr -> expr.position(),
5052
.VarDecl as var_decl -> var_decl.pos,
5153
.While as while_stmt -> while_stmt.pos,

lib/rivet/src/parser/decls.ri

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@ import ../report;
99
import ../token;
1010

1111
extend Parser {
12-
func parse_doc_comment(mut self) -> []ast.Comment {
12+
func parse_doc_comments(mut self) -> []ast.Comment {
1313
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-
);
14+
while self.tok.kind == .DocComment {
15+
comments.push(self.parse_comment(true));
2216
}
2317
return comments;
2418
}
2519

26-
public func parse_attributes(
20+
func parse_attributes(
2721
mut self, parse_mod_attributes: bool := false
2822
) -> ast.Attributes {
2923
mut attributes := ast.Attributes();
@@ -97,7 +91,7 @@ extend Parser {
9791
}
9892

9993
func parse_decl(mut self) -> ast.Decl {
100-
doc_comment := self.parse_doc_comment();
94+
doc_comment := self.parse_doc_comments();
10195
attributes := self.parse_attributes(
10296
self.tok.kind == .Hash and self.peek_tok.kind == .Bang
10397
);

lib/rivet/src/parser/mod.ri

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,35 @@ public struct Parser {
155155
}
156156
return false;
157157
}
158+
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();
162+
}
163+
return ast.Comment(
164+
self.prev_tok.kind == .DocComment,
165+
self.prev_tok.lit.split_into_lines(),
166+
self.prev_tok.pos
167+
);
168+
}
169+
170+
// NOTE:
171+
// same_line: only eat comments on the same line as the previous token.
172+
// follow_up: comments directly below the previous token as long as there
173+
// is no empty line.
174+
func eat_comments(mut self, same_line: bool := false, follow_up: bool := false) -> []ast.Comment {
175+
mut comments := @vec(ast.Comment);
176+
mut line := self.prev_tok.pos.line;
177+
while {
178+
if self.tok.kind != .Comment or (same_line and self.tok.pos.line > line)
179+
or (follow_up and (self.tok.pos.line > line + 1 or self.tok.lit.contains("\n"))) {
180+
break;
181+
}
182+
comments.push(self.parse_comment());
183+
if follow_up {
184+
line = self.prev_tok.pos.line;
185+
}
186+
}
187+
return comments;
188+
}
158189
}

lib/rivet/src/parser/stmts.ri

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import ../ast;
66
import ../report;
77

88
extend Parser {
9+
#[inline]
10+
func parse_comment_stmt(mut self) -> ast.Stmt {
11+
return .Comment(self.parse_comment());
12+
}
13+
914
func parse_stmt(mut self) -> ast.Stmt {
1015
return match {
16+
self.tok.kind == .Comment -> self.parse_comment_stmt(),
1117
self.accept(.KwWhile) -> {
1218
pos := self.prev_tok.pos;
1319
mut is_inf := false;

lib/rivet/src/tokenizer/next.ri

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extend Tokenizer {
9494
self.line -= 1;
9595
}
9696
if (is_doc_comment and self.prefs.is_docs) or self.prefs.is_fmt {
97-
line := self.text[start..end].trim_space();
97+
line := self.text[start..end];
9898
return self.new_token(.Comment, line, line.len);
9999
}
100100
continue;
@@ -126,7 +126,7 @@ extend Tokenizer {
126126
break;
127127
}
128128
if self.prefs.is_fmt {
129-
line := self.text[start_pos..end_pos].trim_space();
129+
line := self.text[start_pos..end_pos];
130130
return self.new_multiline_token(
131131
.Comment, line, line.len, start_line, self.line, start_col
132132
);

0 commit comments

Comments
 (0)