Skip to content

Commit 9787ad9

Browse files
committed
designate doc comments
1 parent c7c50d4 commit 9787ad9

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

corpus/source_files.txt

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ Line comments
5555
(source_file
5656
(line_comment))
5757

58+
============================================
59+
Doc comments
60+
============================================
61+
62+
/// Doc
63+
/// Comment
64+
// / Now a line comment (note the space separating the third slash)
65+
66+
/// Doc
67+
/// Comment
68+
//// Four slashes makes the line a normal comment
69+
/// Doc comment got interrupted by the line above
70+
71+
// Check that this comment does not eat the lines forward
72+
foo;
73+
74+
----
75+
76+
(source_file
77+
(doc_comment)
78+
(line_comment)
79+
(doc_comment)
80+
(line_comment)
81+
(doc_comment)
82+
(line_comment)
83+
(identifier))
84+
5885
=====================================
5986
Greek letters in identifiers
6087
=====================================

grammar.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ const primitive_types = numeric_types.concat(['bool', 'str', 'char'])
3838
module.exports = grammar({
3939
name: 'rust',
4040

41-
extras: $ => [/\s/, $.line_comment, $.block_comment],
41+
extras: $ => [/\s/, $.line_comment, $.block_comment, $.doc_comment],
4242

4343
externals: $ => [
4444
$._string_content,
4545
$.raw_string_literal,
4646
$.float_literal,
4747
$.block_comment,
48+
$.line_comment,
49+
$.doc_comment
4850
],
4951

5052
supertypes: $ => [
@@ -1425,15 +1427,6 @@ module.exports = grammar({
14251427

14261428
boolean_literal: $ => choice('true', 'false'),
14271429

1428-
comment: $ => choice(
1429-
$.line_comment,
1430-
$.block_comment
1431-
),
1432-
1433-
line_comment: $ => token(seq(
1434-
'//', /.*/
1435-
)),
1436-
14371430
_path: $ => choice(
14381431
$.self,
14391432
alias(choice(...primitive_types), $.identifier),

src/scanner.c

+58
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ enum TokenType {
66
RAW_STRING_LITERAL,
77
FLOAT_LITERAL,
88
BLOCK_COMMENT,
9+
LINE_COMMENT,
10+
DOC_COMMENT,
911
};
1012

1113
void *tree_sitter_rust_external_scanner_create() { return NULL; }
@@ -143,7 +145,63 @@ bool tree_sitter_rust_external_scanner_scan(void *payload, TSLexer *lexer,
143145

144146
if (lexer->lookahead == '/') {
145147
advance(lexer);
148+
149+
if ((valid_symbols[LINE_COMMENT] || valid_symbols[DOC_COMMENT]) && lexer->lookahead == '/') {
150+
advance(lexer);
151+
152+
if (lexer->lookahead == '/') {
153+
advance(lexer);
154+
155+
// If a fourth slash is found, the line turns back to a normal comment
156+
if (lexer->lookahead != '/') {
157+
lexer->result_symbol = DOC_COMMENT;
158+
while (true) {
159+
while (lexer->lookahead != '\n') {
160+
advance(lexer);
161+
}
162+
if (lexer->lookahead == 0) {
163+
break;
164+
}
165+
166+
lexer->mark_end(lexer);
167+
advance(lexer);
168+
if (lexer->lookahead == '/') {
169+
advance(lexer);
170+
if (lexer->lookahead == '/') {
171+
advance(lexer);
172+
if (lexer->lookahead == '/') {
173+
advance(lexer);
174+
// If a fourth slash is found, the line turns back to a normal comment
175+
if (lexer->lookahead == '/') {
176+
break;
177+
}
178+
} else {
179+
break;
180+
}
181+
} else {
182+
break;
183+
}
184+
} else {
185+
break;
186+
}
187+
}
188+
}
189+
}
190+
191+
// Might have broken from the loop above having already processed a doc
192+
// comment
193+
if (lexer->result_symbol != DOC_COMMENT) {
194+
lexer->result_symbol = LINE_COMMENT;
195+
while (lexer->lookahead != '\n' && lexer->lookahead != 0) {
196+
advance(lexer);
197+
}
198+
}
199+
200+
return true;
201+
}
202+
146203
if (lexer->lookahead != '*') return false;
204+
147205
advance(lexer);
148206

149207
bool after_star = false;

0 commit comments

Comments
 (0)