* Whether the code from `from` to `to` is a routine's locals and body, the * one conditional the grammar reads right after a routine's header: it ends * with the `end;` of a block that no routine declared in it owns. Code that * closes a block it didn't open (a local class type) counts as a body
(s: string, from: number, to: number)
| 501 | * so it stays the way the grammar reads it. |
| 502 | */ |
| 503 | function holdsBody(s: string, from: number, to: number): boolean { |
| 504 | const blocks: string[] = []; |
| 505 | let pending = 0; // routines declared at the top whose body hasn't come yet |
| 506 | let own = false; // whether the last top-level block is no declared routine's |
| 507 | let last = ''; |
| 508 | let beforeLast = ''; |
| 509 | let i = from; |
| 510 | while (i < to) { |
| 511 | const k = commentOrStringEnd(s, i); |
| 512 | if (k >= 0) { |
| 513 | if (s.charCodeAt(i) === CH_QUOTE) { |
| 514 | beforeLast = last; |
| 515 | last = "'"; |
| 516 | } |
| 517 | i = k; |
| 518 | continue; |
| 519 | } |
| 520 | const c = s.charCodeAt(i); |
| 521 | if (isIdentStart(c)) { |
| 522 | const e = wordEnd(s, i + 1); |
| 523 | const word = e - i <= 11 ? s.slice(i, e).toLowerCase() : 'word'; |
| 524 | if (blocks.length === 0) { |
| 525 | if (ROUTINE_KEYWORDS.has(word) && (last === '' || last === ';')) pending++; |
| 526 | else if ((word === 'forward' || word === 'external') && pending > 0) pending--; |
| 527 | } |
| 528 | if (word === 'end') { |
| 529 | if (blocks.pop() === undefined) return true; |
| 530 | } else if (word === 'begin' || word === 'asm') { |
| 531 | if (blocks.length === 0) { |
| 532 | own = pending === 0; |
| 533 | if (!own) pending--; |
| 534 | } |
| 535 | blocks.push(word); |
| 536 | } else if (word === 'try' || word === 'record' || (word === 'case' && blocks[blocks.length - 1] !== 'record')) { |
| 537 | blocks.push(word); |
| 538 | } |
| 539 | beforeLast = last; |
| 540 | last = word; |
| 541 | i = e; |
| 542 | continue; |
| 543 | } |
| 544 | if (!isSpace(c)) { |
| 545 | beforeLast = last; |
| 546 | last = s[i]!; |
| 547 | } |
| 548 | i++; |
| 549 | } |
| 550 | return blocks.length === 0 && own && beforeLast === 'end' && last === ';'; |
| 551 | } |
| 552 | |
| 553 | /** Whether the code from `from` to `to` holds a `begin` or an `asm`: a body, or a part of one. */ |
| 554 | function holdsBlock(s: string, from: number, to: number): boolean { |
no test coverage detected