@@ -6,6 +6,8 @@ enum TokenType {
66 RAW_STRING_LITERAL ,
77 FLOAT_LITERAL ,
88 BLOCK_COMMENT ,
9+ LINE_COMMENT ,
10+ DOC_COMMENT ,
911};
1012
1113void * tree_sitter_rust_external_scanner_create () { return NULL ; }
@@ -143,7 +145,95 @@ 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+ bool started_with_slash = lexer -> lookahead == '/' ;
153+ switch (lexer -> lookahead ) {
154+ case '!' :
155+ case '/' : {
156+ advance (lexer );
157+
158+ // If three consecutive slashes were seen and this is the fourth one,
159+ // the line turns back to a normal comment.
160+ // The above rule does not apply for "//!" which is also a doc
161+ // comment, hence why it is relevant to track started_with_slash.
162+ if (started_with_slash == false || lexer -> lookahead != '/' ) {
163+ lexer -> result_symbol = DOC_COMMENT ;
164+
165+ while (true) {
166+ while (true) {
167+ switch (lexer -> lookahead ) {
168+ case '\n' : {
169+ lexer -> mark_end (lexer );
170+ advance (lexer );
171+ goto finished_doc_comment_line ;
172+ }
173+ case 0 : {
174+ goto doc_comment_exit ;
175+ }
176+ default : {
177+ advance (lexer );
178+ }
179+ }
180+ }
181+
182+ finished_doc_comment_line :
183+
184+ while (isblank (lexer -> lookahead )) lexer -> advance (lexer , false);
185+
186+ if (lexer -> lookahead == '\n' ) {
187+ // Even if there's another comment ahead, it'll be part of a
188+ // separate node. Break here.
189+ break ;
190+ }
191+
192+ if (lexer -> lookahead == '/' ) {
193+ advance (lexer );
194+ if (lexer -> lookahead == '/' ) {
195+ advance (lexer );
196+ if (started_with_slash ) {
197+ if (lexer -> lookahead == '/' ) {
198+ advance (lexer );
199+ // If a fourth slash is found, the line turns back to a normal comment
200+ if (lexer -> lookahead == '/' ) {
201+ break ;
202+ }
203+ } else {
204+ break ;
205+ }
206+ } else if (lexer -> lookahead != '!' ) {
207+ break ;
208+ }
209+ } else {
210+ break ;
211+ }
212+ } else {
213+ break ;
214+ }
215+ }
216+ }
217+
218+ break ;
219+ }
220+ }
221+
222+ doc_comment_exit :
223+
224+ // Might have already processed a doc comment in the loop above
225+ if (lexer -> result_symbol != DOC_COMMENT ) {
226+ lexer -> result_symbol = LINE_COMMENT ;
227+ while (lexer -> lookahead != '\n' && lexer -> lookahead != 0 ) {
228+ advance (lexer );
229+ }
230+ }
231+
232+ return true;
233+ }
234+
146235 if (lexer -> lookahead != '*' ) return false;
236+
147237 advance (lexer );
148238
149239 bool after_star = false;
0 commit comments