Mercurial > emacs
comparison src/syntax.c @ 32087:83ecc6bc4bc2
(prev_char_comstart_first): Remove.
(back_comment): Check two-char comment markers more carefully
to better handle overlapping cases like *//* or /* */* */ ...
Match nestedness of ender/starter.
| author | Stefan Monnier <monnier@iro.umontreal.ca> |
|---|---|
| date | Mon, 02 Oct 2000 22:15:14 +0000 |
| parents | 73d18e25e303 |
| children | 3819d0d851cb |
comparison
equal
deleted
inserted
replaced
| 32086:73d18e25e303 | 32087:83ecc6bc4bc2 |
|---|---|
| 423 return val; | 423 return val; |
| 424 } | 424 } |
| 425 | 425 |
| 426 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */ | 426 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */ |
| 427 | 427 |
| 428 static int | 428 /* static int |
| 429 prev_char_comstart_first (pos, pos_byte) | 429 * prev_char_comstart_first (pos, pos_byte) |
| 430 int pos, pos_byte; | 430 * int pos, pos_byte; |
| 431 { | 431 * { |
| 432 int c, val; | 432 * int c, val; |
| 433 | 433 * |
| 434 DEC_BOTH (pos, pos_byte); | 434 * DEC_BOTH (pos, pos_byte); |
| 435 UPDATE_SYNTAX_TABLE_BACKWARD (pos); | 435 * UPDATE_SYNTAX_TABLE_BACKWARD (pos); |
| 436 c = FETCH_CHAR (pos_byte); | 436 * c = FETCH_CHAR (pos_byte); |
| 437 val = SYNTAX_COMSTART_FIRST (c); | 437 * val = SYNTAX_COMSTART_FIRST (c); |
| 438 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); | 438 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); |
| 439 return val; | 439 * return val; |
| 440 } | 440 * } */ |
| 441 | 441 |
| 442 /* Checks whether charpos FROM is at the end of a comment. | 442 /* Checks whether charpos FROM is at the end of a comment. |
| 443 FROM_BYTE is the bytepos corresponding to FROM. | 443 FROM_BYTE is the bytepos corresponding to FROM. |
| 444 Do not move back before STOP. | 444 Do not move back before STOP. |
| 445 | 445 |
| 477 int comment_lossage = 0; | 477 int comment_lossage = 0; |
| 478 int comment_end = from; | 478 int comment_end = from; |
| 479 int comment_end_byte = from_byte; | 479 int comment_end_byte = from_byte; |
| 480 int comstart_pos = 0; | 480 int comstart_pos = 0; |
| 481 int comstart_byte; | 481 int comstart_byte; |
| 482 int scanstart = from - 1; | |
| 483 /* Place where the containing defun starts, | 482 /* Place where the containing defun starts, |
| 484 or 0 if we didn't come across it yet. */ | 483 or 0 if we didn't come across it yet. */ |
| 485 int defun_start = 0; | 484 int defun_start = 0; |
| 486 int defun_start_byte = 0; | 485 int defun_start_byte = 0; |
| 487 register enum syntaxcode code; | 486 register enum syntaxcode code; |
| 488 int nesting = 1; /* current comment nesting */ | 487 int nesting = 1; /* current comment nesting */ |
| 489 int c; | 488 int c; |
| 489 int syntax = 0; | |
| 490 | |
| 491 /* FIXME: A }} comment-ender style leads to incorrect behavior | |
| 492 in the case of {{ c }}} because we ignore the last two chars which are | |
| 493 assumed to be comment-enders although they aren't. */ | |
| 490 | 494 |
| 491 /* At beginning of range to scan, we're outside of strings; | 495 /* At beginning of range to scan, we're outside of strings; |
| 492 that determines quote parity to the comment-end. */ | 496 that determines quote parity to the comment-end. */ |
| 493 while (from != stop) | 497 while (from != stop) |
| 494 { | 498 { |
| 495 int temp_byte; | 499 int temp_byte, prev_syntax; |
| 500 int com2start, com2end; | |
| 496 | 501 |
| 497 /* Move back and examine a character. */ | 502 /* Move back and examine a character. */ |
| 498 DEC_BOTH (from, from_byte); | 503 DEC_BOTH (from, from_byte); |
| 499 UPDATE_SYNTAX_TABLE_BACKWARD (from); | 504 UPDATE_SYNTAX_TABLE_BACKWARD (from); |
| 500 | 505 |
| 506 prev_syntax = syntax; | |
| 501 c = FETCH_CHAR (from_byte); | 507 c = FETCH_CHAR (from_byte); |
| 508 syntax = SYNTAX_WITH_FLAGS (c); | |
| 502 code = SYNTAX (c); | 509 code = SYNTAX (c); |
| 503 | 510 |
| 504 /* If this char is the second of a 2-char comment end sequence, | 511 /* Check for 2-char comment markers. */ |
| 505 back up and give the pair the appropriate syntax. */ | 512 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax) |
| 506 if (from > stop && SYNTAX_COMEND_SECOND (c) | 513 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax) |
| 507 && prev_char_comend_first (from, from_byte)) | 514 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax) |
| 508 { | 515 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax) |
| 509 code = Sendcomment; | 516 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested); |
| 510 DEC_BOTH (from, from_byte); | 517 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax) |
| 511 UPDATE_SYNTAX_TABLE_BACKWARD (from); | 518 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax)); |
| 512 c = FETCH_CHAR (from_byte); | 519 |
| 520 /* Nasty cases with overlapping 2-char comment markers: | |
| 521 - snmp-mode: -- c -- foo -- c -- | |
| 522 --- c -- | |
| 523 ------ c -- | |
| 524 - c-mode: *||* | |
| 525 |* *|* *| | |
| 526 |*| |* |*| | |
| 527 /// */ | |
| 528 | |
| 529 /* If a 2-char comment sequence partly overlaps with another, | |
| 530 we don't try to be clever. */ | |
| 531 if (from > stop && (com2end || com2start)) | |
| 532 { | |
| 533 int next = from, next_byte = from_byte, next_c, next_syntax; | |
| 534 DEC_BOTH (next, next_byte); | |
| 535 UPDATE_SYNTAX_TABLE_BACKWARD (next); | |
| 536 next_c = FETCH_CHAR (next_byte); | |
| 537 next_syntax = SYNTAX_WITH_FLAGS (next_c); | |
| 538 if (((com2start || comnested) | |
| 539 && SYNTAX_FLAGS_COMEND_SECOND (syntax) | |
| 540 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax)) | |
| 541 || ((com2end || comnested) | |
| 542 && SYNTAX_FLAGS_COMSTART_SECOND (syntax) | |
| 543 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax) | |
| 544 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax))) | |
| 545 goto lossage; | |
| 546 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */ | |
| 513 } | 547 } |
| 514 | 548 |
| 515 /* If this char starts a 2-char comment start sequence, | 549 if (com2start && comstart_pos == 0) |
| 516 treat it like a 1-char comment starter. */ | 550 /* We're looking at a comment starter. But it might be a comment |
| 517 if (from < scanstart && SYNTAX_COMSTART_FIRST (c)) | 551 ender as well (see snmp-mode). The first time we see one, we |
| 518 { | 552 need to consider it as a comment starter, |
| 519 temp_byte = inc_bytepos (from_byte); | 553 and the subsequent times as a comment ender. */ |
| 520 UPDATE_SYNTAX_TABLE_FORWARD (from + 1); | 554 com2end = 0; |
| 521 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte)) | 555 |
| 522 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte))) | 556 /* Turn a 2-char comment sequences into the appropriate syntax. */ |
| 523 code = Scomment; | 557 if (com2end) |
| 524 UPDATE_SYNTAX_TABLE_BACKWARD (from); | 558 code = Sendcomment; |
| 525 } | 559 else if (com2start) |
| 526 else if (code == Scomment && comstyle != SYNTAX_COMMENT_STYLE (c)) | 560 code = Scomment; |
| 527 /* Ignore comment starters of a different style. */ | 561 /* Ignore comment starters of a different style. */ |
| 562 else if (code == Scomment | |
| 563 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax) | |
| 564 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested)) | |
| 528 continue; | 565 continue; |
| 529 | 566 |
| 530 /* Ignore escaped characters, except comment-enders. */ | 567 /* Ignore escaped characters, except comment-enders. */ |
| 531 if (code != Sendcomment && char_quoted (from, from_byte)) | 568 if (code != Sendcomment && char_quoted (from, from_byte)) |
| 532 continue; | 569 continue; |
| 571 strings, else the comment-end itself would be inside a string. */ | 608 strings, else the comment-end itself would be inside a string. */ |
| 572 goto done; | 609 goto done; |
| 573 break; | 610 break; |
| 574 | 611 |
| 575 case Sendcomment: | 612 case Sendcomment: |
| 576 if (SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle) | 613 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle |
| 614 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax) | |
| 615 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested) | |
| 577 /* This is the same style of comment ender as ours. */ | 616 /* This is the same style of comment ender as ours. */ |
| 578 { | 617 { |
| 579 if (comnested) | 618 if (comnested) |
| 580 nesting++; | 619 nesting++; |
| 581 else | 620 else |
