MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / statementsShape

Function statementsShape

src/extraction/languages/pascal-preparse.ts:695–773  ·  view source on GitHub ↗

* Whether the code from `from` to `to`, a conditional's branch, holds whole statements: its blocks and brackets * close, every `if` has its `then` and every `while`, `for`, `with` and `on` its `do`, and it ends where a * statement can. It must start with a statement's keyword, assign at its top le

(s: string, from: number, to: number, bare: boolean)

Source from the content-addressed store, hash-verified

693 * or SHAPE_NONE.
694 */
695function statementsShape(s: string, from: number, to: number, bare: boolean): number {
696 const blocks: boolean[] = []; // an open block each: true for a `repeat`, which `until` closes
697 let depth = 0;
698 let heads = 0; // `if`, `while`, `for`, `with` and `on` still waiting for their `then` or `do`
699 let tokens = 0;
700 let start = START_NONE;
701 let name = 0; // reading the first dotted name: 1 after a part of it, 2 after its `.`, 0 when done
702 let last = ''; // the last token: a word lowercased ('word' when long), '0' for a number, else its character
703 let i = from;
704 while (i < to) {
705 const k = commentOrStringEnd(s, i);
706 if (k >= 0) {
707 if (s.charCodeAt(i) === CH_QUOTE) {
708 tokens++;
709 name = 0;
710 last = "'";
711 }
712 i = k;
713 continue;
714 }
715 const c = s.charCodeAt(i);
716 if (isSpace(c)) {
717 i++;
718 continue;
719 }
720 tokens++;
721 if (isIdentStart(c)) {
722 const e = wordEnd(s, i + 1);
723 const word = e - i <= 9 ? s.slice(i, e).toLowerCase() : 'word';
724 if (word === 'begin' || word === 'try' || word === 'case' || word === 'asm') blocks.push(false);
725 else if (word === 'repeat') blocks.push(true);
726 else if (word === 'end' && blocks.pop() !== false) return SHAPE_NONE;
727 else if (word === 'until' && blocks.pop() !== true) return SHAPE_NONE;
728 else if (word === 'if' || word === 'while' || word === 'for' || word === 'with' || word === 'on') heads++;
729 else if (word === 'then' || word === 'do') heads--;
730 if (tokens === 1) {
731 if (STATEMENT_KEYWORDS.has(word)) start = START_STATEMENT;
732 else if (!TYPE_KEYWORDS.has(word) && !STATEMENT_CONTINUES.has(word) && word !== 'end') name = 1;
733 } else {
734 name = name === 2 ? 1 : 0;
735 }
736 last = word;
737 i = e;
738 continue;
739 }
740 if (c >= 48 && c <= 57) {
741 name = 0;
742 last = '0';
743 i = wordEnd(s, i + 1);
744 continue;
745 }
746 if (c === CH_COLON && s.charCodeAt(i + 1) === CH_EQ) {
747 if (depth === 0 && start !== START_CALL) start = START_STATEMENT;
748 name = 0;
749 last = ':=';
750 i += 2;
751 continue;
752 }

Callers 1

Calls 6

commentOrStringEndFunction · 0.85
isIdentStartFunction · 0.85
pushMethod · 0.80
hasMethod · 0.80
isSpaceFunction · 0.70
wordEndFunction · 0.70

Tested by

no test coverage detected