Mercurial > emacs
comparison src/syntax.c @ 29835:5dec8ee96c0c
(back_comment): Simplify string-parity counting (with
the added benefit of handling multiple string-styles as long as
they are not nested).
Jump to the slow code as soon as a comment starter is found in
a "string_lossage" position. Fixes the case: " /* " /* " */.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Wed, 21 Jun 2000 14:56:24 +0000 |
parents | 5a736b07dbf2 |
children | f59021679260 |
comparison
equal
deleted
inserted
replaced
29834:fb20d35ffa9a | 29835:5dec8ee96c0c |
---|---|
465 an even number of string quotes away from the safe place. | 465 an even number of string quotes away from the safe place. |
466 | 466 |
467 OFROM[I] is position of the earliest comment-starter seen | 467 OFROM[I] is position of the earliest comment-starter seen |
468 which is I+2X quotes from the comment-end. | 468 which is I+2X quotes from the comment-end. |
469 PARITY is current parity of quotes from the comment end. */ | 469 PARITY is current parity of quotes from the comment end. */ |
470 int parity = 0; | 470 int string_style = -1; /* Presumed outside of any string. */ |
471 int my_stringend = 0; | |
472 int string_lossage = 0; | 471 int string_lossage = 0; |
473 int comment_end = from; | 472 int comment_end = from; |
474 int comment_end_byte = from_byte; | 473 int comment_end_byte = from_byte; |
475 int comstart_pos = 0; | 474 int comstart_pos = 0; |
476 int comstart_byte; | 475 int comstart_byte; |
477 /* Value that PARITY had, when we reached the position | 476 /* Value that PARITY had, when we reached the position |
478 in COMSTART_POS. */ | 477 in COMSTART_POS. */ |
479 int comstart_parity = 0; | |
480 int scanstart = from - 1; | 478 int scanstart = from - 1; |
481 /* Place where the containing defun starts, | 479 /* Place where the containing defun starts, |
482 or 0 if we didn't come across it yet. */ | 480 or 0 if we didn't come across it yet. */ |
483 int defun_start = 0; | 481 int defun_start = 0; |
484 int defun_start_byte = 0; | 482 int defun_start_byte = 0; |
528 /* Ignore escaped characters, except comment-enders. */ | 526 /* Ignore escaped characters, except comment-enders. */ |
529 if (code != Sendcomment && char_quoted (from, from_byte)) | 527 if (code != Sendcomment && char_quoted (from, from_byte)) |
530 continue; | 528 continue; |
531 | 529 |
532 /* Track parity of quotes. */ | 530 /* Track parity of quotes. */ |
533 if (code == Sstring) | 531 switch (code) |
534 { | 532 { |
535 parity ^= 1; | 533 case Sstring_fence: |
536 if (my_stringend == 0) | 534 case Scomment_fence: |
537 my_stringend = c; | 535 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE); |
538 /* If we have two kinds of string delimiters. | 536 case Sstring: |
539 There's no way to grok this scanning backwards. */ | 537 /* Track parity of quotes. */ |
540 else if (my_stringend != c) | 538 if (string_style == -1) |
539 /* Entering a string. */ | |
540 string_style = c; | |
541 else if (string_style == c) | |
542 /* Leaving the string. */ | |
543 string_style = -1; | |
544 else | |
545 /* If we have two kinds of string delimiters. | |
546 There's no way to grok this scanning backwards. */ | |
541 string_lossage = 1; | 547 string_lossage = 1; |
542 } | 548 break; |
543 | 549 |
544 if (code == Sstring_fence || code == Scomment_fence) | 550 case Scomment: |
545 { | 551 /* We've already checked that it is the relevant comstyle. */ |
546 parity ^= 1; | 552 if (string_style != -1 || string_lossage) |
547 if (my_stringend == 0) | 553 /* There are odd string quotes involved, so let's be careful. |
548 my_stringend | 554 Test case in Pascal: " { " a { " } */ |
549 = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE; | 555 goto lossage; |
550 /* If we have two kinds of string delimiters. | 556 |
551 There's no way to grok this scanning backwards. */ | 557 if (comnested && --nesting <= 0) |
552 else if (my_stringend != (code == Sstring_fence | |
553 ? ST_STRING_STYLE : ST_COMMENT_STYLE)) | |
554 string_lossage = 1; | |
555 } | |
556 | |
557 if (code == Scomment) | |
558 /* We've already checked that it is the relevant comstyle. */ | |
559 { | |
560 if (comnested && --nesting <= 0 && parity == 0 && !string_lossage) | |
561 /* nested comments have to be balanced, so we don't need to | 558 /* nested comments have to be balanced, so we don't need to |
562 keep looking for earlier ones. We use here the same (slightly | 559 keep looking for earlier ones. We use here the same (slightly |
563 incorrect) reasoning as below: since it is followed by uniform | 560 incorrect) reasoning as below: since it is followed by uniform |
564 paired string quotes, this comment-start has to be outside of | 561 paired string quotes, this comment-start has to be outside of |
565 strings, else the comment-end itself would be inside a string. */ | 562 strings, else the comment-end itself would be inside a string. */ |
566 goto done; | 563 goto done; |
567 | 564 |
568 /* Record comment-starters according to that | 565 /* Record comment-starters according to that |
569 quote-parity to the comment-end. */ | 566 quote-parity to the comment-end. */ |
570 comstart_parity = parity; | |
571 comstart_pos = from; | 567 comstart_pos = from; |
572 comstart_byte = from_byte; | 568 comstart_byte = from_byte; |
569 break; | |
570 | |
571 default: | |
572 ; | |
573 } | 573 } |
574 | 574 |
575 /* If we find another earlier comment-ender, | 575 /* If we find another earlier comment-ender, |
576 any comment-starts earlier than that don't count | 576 any comment-starts earlier than that don't count |
577 (because they go with the earlier comment-ender). */ | 577 (because they go with the earlier comment-ender). */ |
605 /* If the earliest comment starter | 605 /* If the earliest comment starter |
606 is followed by uniform paired string quotes or none, | 606 is followed by uniform paired string quotes or none, |
607 we know it can't be inside a string | 607 we know it can't be inside a string |
608 since if it were then the comment ender would be inside one. | 608 since if it were then the comment ender would be inside one. |
609 So it does start a comment. Skip back to it. */ | 609 So it does start a comment. Skip back to it. */ |
610 else if (!comnested && comstart_parity == 0 && !string_lossage) | 610 else if (!comnested) |
611 { | 611 { |
612 from = comstart_pos; | 612 from = comstart_pos; |
613 from_byte = comstart_byte; | 613 from_byte = comstart_byte; |
614 /* Globals are correct now. */ | 614 /* Globals are correct now. */ |
615 } | 615 } |
616 else | 616 else |
617 { | 617 { |
618 struct lisp_parse_state state; | |
619 lossage: | |
618 /* We had two kinds of string delimiters mixed up | 620 /* We had two kinds of string delimiters mixed up |
619 together. Decode this going forwards. | 621 together. Decode this going forwards. |
620 Scan fwd from the previous comment ender | 622 Scan fwd from the previous comment ender |
621 to the one in question; this records where we | 623 to the one in question; this records where we |
622 last passed a comment starter. */ | 624 last passed a comment starter. */ |
623 struct lisp_parse_state state; | |
624 /* If we did not already find the defun start, find it now. */ | 625 /* If we did not already find the defun start, find it now. */ |
625 if (defun_start == 0) | 626 if (defun_start == 0) |
626 { | 627 { |
627 defun_start = find_defun_start (comment_end, comment_end_byte); | 628 defun_start = find_defun_start (comment_end, comment_end_byte); |
628 defun_start_byte = find_start_value_byte; | 629 defun_start_byte = find_start_value_byte; |