diff options
| author | Stefan Monnier | 2000-08-27 17:44:59 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2000-08-27 17:44:59 +0000 |
| commit | f7c436c1c8538b9b593f7d7040bcbee2b0ad79d0 (patch) | |
| tree | 2a8c8a9937b9a55120661d7341f4e8224b1d8a7c /src/syntax.c | |
| parent | 5749539688a77bde9dd6fd473e7a53d6d1e0cf0c (diff) | |
| download | emacs-f7c436c1c8538b9b593f7d7040bcbee2b0ad79d0.tar.gz emacs-f7c436c1c8538b9b593f7d7040bcbee2b0ad79d0.zip | |
(back_comment): Detect cases where a comment-starter is
actually inside another comment as in: /* a // b */ c // d \n.
Make it clear that `comstart_pos' is unused for nested comments.
Diffstat (limited to 'src/syntax.c')
| -rw-r--r-- | src/syntax.c | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/syntax.c b/src/syntax.c index 9e5247064ef..189e50d0963 100644 --- a/src/syntax.c +++ b/src/syntax.c | |||
| @@ -469,12 +469,16 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p | |||
| 469 | PARITY is current parity of quotes from the comment end. */ | 469 | PARITY is current parity of quotes from the comment end. */ |
| 470 | int string_style = -1; /* Presumed outside of any string. */ | 470 | int string_style = -1; /* Presumed outside of any string. */ |
| 471 | int string_lossage = 0; | 471 | int string_lossage = 0; |
| 472 | /* Not a real lossage: indicates that we have passed a matching comment | ||
| 473 | starter plus an non-matching comment-ender, meaning that any matching | ||
| 474 | comment-starter we might see later could be a false positive (hidden | ||
| 475 | inside another comment). | ||
| 476 | Test case: { a (* b } c (* d *) */ | ||
| 477 | int comment_lossage = 0; | ||
| 472 | int comment_end = from; | 478 | int comment_end = from; |
| 473 | int comment_end_byte = from_byte; | 479 | int comment_end_byte = from_byte; |
| 474 | int comstart_pos = 0; | 480 | int comstart_pos = 0; |
| 475 | int comstart_byte; | 481 | int comstart_byte; |
| 476 | /* Value that PARITY had, when we reached the position | ||
| 477 | in COMSTART_POS. */ | ||
| 478 | int scanstart = from - 1; | 482 | int scanstart = from - 1; |
| 479 | /* Place where the containing defun starts, | 483 | /* Place where the containing defun starts, |
| 480 | or 0 if we didn't come across it yet. */ | 484 | or 0 if we didn't come across it yet. */ |
| @@ -548,23 +552,24 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p | |||
| 548 | 552 | ||
| 549 | case Scomment: | 553 | case Scomment: |
| 550 | /* We've already checked that it is the relevant comstyle. */ | 554 | /* We've already checked that it is the relevant comstyle. */ |
| 551 | if (string_style != -1 || string_lossage) | 555 | if (string_style != -1 || comment_lossage || string_lossage) |
| 552 | /* There are odd string quotes involved, so let's be careful. | 556 | /* There are odd string quotes involved, so let's be careful. |
| 553 | Test case in Pascal: " { " a { " } */ | 557 | Test case in Pascal: " { " a { " } */ |
| 554 | goto lossage; | 558 | goto lossage; |
| 555 | 559 | ||
| 556 | if (comnested && --nesting <= 0) | 560 | if (!comnested) |
| 561 | { | ||
| 562 | /* Record best comment-starter so far. */ | ||
| 563 | comstart_pos = from; | ||
| 564 | comstart_byte = from_byte; | ||
| 565 | } | ||
| 566 | else if (--nesting <= 0) | ||
| 557 | /* nested comments have to be balanced, so we don't need to | 567 | /* nested comments have to be balanced, so we don't need to |
| 558 | keep looking for earlier ones. We use here the same (slightly | 568 | keep looking for earlier ones. We use here the same (slightly |
| 559 | incorrect) reasoning as below: since it is followed by uniform | 569 | incorrect) reasoning as below: since it is followed by uniform |
| 560 | paired string quotes, this comment-start has to be outside of | 570 | paired string quotes, this comment-start has to be outside of |
| 561 | strings, else the comment-end itself would be inside a string. */ | 571 | strings, else the comment-end itself would be inside a string. */ |
| 562 | goto done; | 572 | goto done; |
| 563 | |||
| 564 | /* Record comment-starters according to that | ||
| 565 | quote-parity to the comment-end. */ | ||
| 566 | comstart_pos = from; | ||
| 567 | comstart_byte = from_byte; | ||
| 568 | break; | 573 | break; |
| 569 | 574 | ||
| 570 | case Sendcomment: | 575 | case Sendcomment: |
| @@ -578,6 +583,15 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p | |||
| 578 | this comment-ender rather than ours. */ | 583 | this comment-ender rather than ours. */ |
| 579 | from = stop; /* Break out of the loop. */ | 584 | from = stop; /* Break out of the loop. */ |
| 580 | } | 585 | } |
| 586 | else if (comstart_pos != 0 || c != '\n') | ||
| 587 | /* We're mixing comment styles here, so we'd better be careful. | ||
| 588 | The (comstart_pos != 0 || c != '\n') check is not quite correct | ||
| 589 | (we should just always set comment_lossage), but removing it | ||
| 590 | would imply that any multiline comment in C would go through | ||
| 591 | lossage, which seems overkill. | ||
| 592 | The failure should only happen in the rare cases such as | ||
| 593 | { (* } *) */ | ||
| 594 | comment_lossage = 1; | ||
| 581 | break; | 595 | break; |
| 582 | 596 | ||
| 583 | case Sopen: | 597 | case Sopen: |
| @@ -594,7 +608,7 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p | |||
| 594 | break; | 608 | break; |
| 595 | 609 | ||
| 596 | default: | 610 | default: |
| 597 | continue; | 611 | break; |
| 598 | } | 612 | } |
| 599 | } | 613 | } |
| 600 | 614 | ||
| @@ -604,12 +618,9 @@ back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_p | |||
| 604 | from_byte = comment_end_byte; | 618 | from_byte = comment_end_byte; |
| 605 | UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1); | 619 | UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1); |
| 606 | } | 620 | } |
| 607 | /* If the earliest comment starter | 621 | /* If comstart_pos is set and we get here (ie. didn't jump to `lossage' |
| 608 | is followed by uniform paired string quotes or none, | 622 | or `done'), then we've found the beginning of the non-nested comment. */ |
| 609 | we know it can't be inside a string | 623 | else if (1) /* !comnested */ |
| 610 | since if it were then the comment ender would be inside one. | ||
| 611 | So it does start a comment. Skip back to it. */ | ||
| 612 | else if (!comnested) | ||
| 613 | { | 624 | { |
| 614 | from = comstart_pos; | 625 | from = comstart_pos; |
| 615 | from_byte = comstart_byte; | 626 | from_byte = comstart_byte; |