Skip to content

Commit 2260ea8

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 a60f92b commit 2260ea8

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

corpus/source_files.txt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,17 @@ Line comments
5959
Doc comments
6060
============================================
6161

62-
/// Foo
63-
///
64-
/// # Description
65-
///
66-
/// * Bar
62+
/// Multi
63+
/// Line
64+
/// Doc
65+
/// Comment
66+
// Line comment
6767

6868
----
6969

7070
(source_file
7171
(doc_comment)
72-
(doc_comment)
73-
(doc_comment)
74-
(doc_comment)
75-
(doc_comment))
72+
(line_comment))
7673

7774
=====================================
7875
Greek letters in identifiers

grammar.js

Lines changed: 2 additions & 8 deletions
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: $ => [
@@ -1410,14 +1412,6 @@ module.exports = grammar({
14101412

14111413
boolean_literal: $ => choice('true', 'false'),
14121414

1413-
doc_comment: $ => token(seq(
1414-
'///', /.*/
1415-
)),
1416-
1417-
line_comment: $ => token(seq(
1418-
new RegExp('\/\/[^\/]'), /.*/
1419-
)),
1420-
14211415
_path: $ => choice(
14221416
$.self,
14231417
alias(choice(...primitive_types), $.identifier),

src/scanner.c

Lines changed: 44 additions & 0 deletions
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)