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

Function statementsShape

src/extraction/languages/pascal-preparse.ts:765–843  ·  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

763 * or SHAPE_NONE.
764 */
765function statementsShape(s: string, from: number, to: number, bare: boolean): number {
766 const blocks: boolean[] = []; // an open block each: true for a `repeat`, which `until` closes
767 let depth = 0;
768 let heads = 0; // `if`, `while`, `for`, `with` and `on` still waiting for their `then` or `do`
769 let tokens = 0;
770 let start = START_NONE;
771 let name = 0; // reading the first dotted name: 1 after a part of it, 2 after its `.`, 0 when done
772 let last = ''; // the last token: a word lowercased ('word' when long), '0' for a number, else its character
773 let i = from;
774 while (i < to) {
775 const k = commentOrStringEnd(s, i);
776 if (k >= 0) {
777 if (s.charCodeAt(i) === CH_QUOTE) {
778 tokens++;
779 name = 0;
780 last = "'";
781 }
782 i = k;
783 continue;
784 }
785 const c = s.charCodeAt(i);
786 if (isSpace(c)) {
787 i++;
788 continue;
789 }
790 tokens++;
791 if (isIdentStart(c)) {
792 const e = wordEnd(s, i + 1);
793 const word = e - i <= 9 ? s.slice(i, e).toLowerCase() : 'word';
794 if (word === 'begin' || word === 'try' || word === 'case' || word === 'asm') blocks.push(false);
795 else if (word === 'repeat') blocks.push(true);
796 else if (word === 'end' && blocks.pop() !== false) return SHAPE_NONE;
797 else if (word === 'until' && blocks.pop() !== true) return SHAPE_NONE;
798 else if (word === 'if' || word === 'while' || word === 'for' || word === 'with' || word === 'on') heads++;
799 else if (word === 'then' || word === 'do') heads--;
800 if (tokens === 1) {
801 if (STATEMENT_KEYWORDS.has(word)) start = START_STATEMENT;
802 else if (!TYPE_KEYWORDS.has(word) && !STATEMENT_CONTINUES.has(word) && word !== 'end') name = 1;
803 } else {
804 name = name === 2 ? 1 : 0;
805 }
806 last = word;
807 i = e;
808 continue;
809 }
810 if (c >= 48 && c <= 57) {
811 name = 0;
812 last = '0';
813 i = wordEnd(s, i + 1);
814 continue;
815 }
816 if (c === CH_COLON && s.charCodeAt(i + 1) === CH_EQ) {
817 if (depth === 0 && start !== START_CALL) start = START_STATEMENT;
818 name = 0;
819 last = ':=';
820 i += 2;
821 continue;
822 }

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