Skip to content

Commit be26083

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 be26083

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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: 18 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,23 @@ 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+
} else {
154+
lexer->result_symbol = LINE_COMMENT;
155+
}
156+
157+
while (lexer->lookahead != '\n' && lexer->lookahead != 0) {
158+
advance(lexer);
159+
}
160+
return true;
161+
}
162+
146163
if (lexer->lookahead != '*') return false;
164+
147165
advance(lexer);
148166

149167
bool after_star = false;

0 commit comments

Comments
 (0)