| 365 | |
| 366 | |
| 367 | static bool scan_multiline_comment(TSLexer *lexer, bool *newline, uint32_t *bytes) { |
| 368 | if (lexer->lookahead != '/') return false; |
| 369 | advance_counted(lexer, bytes, false); |
| 370 | if (lexer->lookahead != '*') return false; |
| 371 | advance_counted(lexer, bytes, false); |
| 372 | |
| 373 | bool after_star = false; |
| 374 | unsigned nesting_depth = 1; |
| 375 | for (;;) { |
| 376 | if (newline && (lexer->lookahead == '\n' || lexer->lookahead == '\r')) |
| 377 | *newline = true; |
| 378 | switch (lexer->lookahead) { |
| 379 | case '*': |
| 380 | advance_counted(lexer, bytes, false); |
| 381 | after_star = true; |
| 382 | break; |
| 383 | case '/': |
| 384 | advance_counted(lexer, bytes, false); |
| 385 | if (after_star) { |
| 386 | after_star = false; |
| 387 | nesting_depth -= 1; |
| 388 | if (nesting_depth == 0) { |
| 389 | lexer->result_symbol = MULTILINE_COMMENT; |
| 390 | lexer->mark_end(lexer); |
| 391 | return true; |
| 392 | } |
| 393 | } else { |
| 394 | after_star = false; |
| 395 | if (lexer->lookahead == '*') { |
| 396 | nesting_depth += 1; |
| 397 | advance_counted(lexer, bytes, false); |
| 398 | } |
| 399 | } |
| 400 | break; |
| 401 | case '\0': |
| 402 | // Accept unterminated block comments at EOF rather than rejecting them. |
| 403 | // This matches JetBrains PSI behavior which recognizes unclosed /* as a |
| 404 | // BLOCK_COMMENT token (plus an error element). Without this, the scanner |
| 405 | // returns false and tree-sitter tries to parse the comment delimiters |
| 406 | // as operators/expressions. |
| 407 | if (lexer->eof(lexer)) { |
| 408 | lexer->result_symbol = MULTILINE_COMMENT; |
| 409 | lexer->mark_end(lexer); |
| 410 | return true; |
| 411 | } |
| 412 | // A literal NUL byte inside the comment (lookahead is '\0' but not EOF) |
| 413 | // must be consumed like any other comment content. Returning here without |
| 414 | // advancing leaves the lexer at the same offset, so tree-sitter re-invokes |
| 415 | // the scanner forever on inputs such as "/*\0" -> a parse-time hang (DoS). |
| 416 | // fallthrough |
| 417 | default: |
| 418 | advance_counted(lexer, bytes, false); |
| 419 | after_star = false; |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 |
no test coverage detected