Skip to content

Commit 7890688

Browse files
committed
use externals for comments
done for the sake of differentiating the next character after '//', which requires forward lookup, and could not be achieved via regular expressions
1 parent b715ba3 commit 7890688

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

corpus/source_files.txt

+8-9
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,19 @@ Line comments
5959
Doc comments
6060
============================================
6161

62-
/// Foo
63-
///
64-
/// # Description
65-
///
66-
/// * Bar
62+
/// Multi
63+
/// Line
64+
/// Doc
65+
/// Comment
66+
// / Now a line comment (note the space separating the third slash)
67+
// Line comment
6768

6869
----
6970

7071
(source_file
7172
(doc_comment)
72-
(doc_comment)
73-
(doc_comment)
74-
(doc_comment)
75-
(doc_comment))
73+
(line_comment)
74+
(line_comment))
7675

7776
=====================================
7877
Greek letters in identifiers

grammar.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ module.exports = grammar({
4545
$.raw_string_literal,
4646
$.float_literal,
4747
$.block_comment,
48+
$.line_comment,
49+
$.doc_comment
4850
],
4951

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

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

1428-
doc_comment: $ => token(seq(
1429-
'///', /.*/
1430-
)),
1431-
1432-
line_comment: $ => token(seq(
1433-
new RegExp('\/\/[^\/]'), /.*/
1434-
)),
1435-
14361430
_path: $ => choice(
14371431
$.self,
14381432
alias(choice(...primitive_types), $.identifier),

src/scanner.c

+44
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,49 @@ 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+
if (lexer->lookahead == '/') {
152+
lexer->result_symbol = DOC_COMMENT;
153+
while (true) {
154+
while (lexer->lookahead != '\n') {
155+
advance(lexer);
156+
}
157+
if (lexer->lookahead == 0) {
158+
break;
159+
}
160+
161+
lexer->mark_end(lexer);
162+
advance(lexer);
163+
if (lexer->lookahead == '/') {
164+
advance(lexer);
165+
if (lexer->lookahead == '/') {
166+
advance(lexer);
167+
if (lexer->lookahead == '/') {
168+
advance(lexer);
169+
} else {
170+
break;
171+
}
172+
} else {
173+
break;
174+
}
175+
} else {
176+
break;
177+
}
178+
}
179+
} else {
180+
lexer->result_symbol = LINE_COMMENT;
181+
while (lexer->lookahead != '\n' && lexer->lookahead != 0) {
182+
advance(lexer);
183+
}
184+
}
185+
186+
return true;
187+
}
188+
146189
if (lexer->lookahead != '*') return false;
190+
147191
advance(lexer);
148192

149193
bool after_star = false;

0 commit comments

Comments
 (0)