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

Function scanWithStatements

src/resolution/pascal-scope.ts:1292–1391  ·  view source on GitHub ↗

* The `with` statements of a Pascal text, in the order they start. A * statement's body runs from its `do` to the `;` that ends the statement, or * to the `end`, `until`, `except` or `finally` of the block it is in, or to * an `else` that isn't one of its own `if`s'. `begin`, `try`, `case`, `asm`

(text: string, resets: number[])

Source from the content-addressed store, hash-verified

1290 * at the start of a line — every open statement and block ends.
1291 */
1292function scanWithStatements(text: string, resets: number[]): WithStatement[] {
1293 const statements: WithStatement[] = [];
1294 const blocks: string[] = [];
1295 const open: Array<{ index: number; depth: number; parens: number; ifs: number }> = [];
1296 let parens = 0;
1297 const closeWhile = (at: number, ends: (o: { depth: number; parens: number }) => boolean): void => {
1298 while (open.length > 0 && ends(open[open.length - 1]!)) statements[open.pop()!.index]!.end = at;
1299 };
1300 let reset = 0;
1301 const n = text.length;
1302 let i = 0;
1303 while (i < n) {
1304 if (reset < resets.length && i >= resets[reset]!) {
1305 closeWhile(resets[reset]!, () => true);
1306 blocks.length = 0;
1307 parens = 0;
1308 while (reset < resets.length && resets[reset]! <= i) reset++;
1309 }
1310 const c = text.charCodeAt(i);
1311 if (c === CH_SPACE || c === CH_TAB || c === CH_LF || c === CH_CR) {
1312 i++;
1313 continue;
1314 }
1315 const skip = triviaEnd(text, i);
1316 if (skip > i) {
1317 i = skip;
1318 continue;
1319 }
1320 if (!isWordStart(c)) {
1321 if (c === CH_LPAREN || c === CH_LBRACKET) {
1322 parens++;
1323 } else if (c === CH_RPAREN || c === CH_RBRACKET) {
1324 if (parens > 0) parens--;
1325 closeWhile(i, (o) => o.parens > parens);
1326 } else if (c === CH_SEMI) {
1327 closeWhile(i, (o) => o.depth >= blocks.length && o.parens >= parens);
1328 }
1329 i++;
1330 continue;
1331 }
1332 let j = i + 1;
1333 while (j < n && isWordPart(text.charCodeAt(j))) j++;
1334 const top = blocks[blocks.length - 1];
1335 const before = i > 0 ? text.charCodeAt(i - 1) : 0;
1336 // A keyword written as a name (`&begin`, `Foo.End`) is a name, and an
1337 // assembler block is over only at its `end`.
1338 const word = before === CH_AMP || before === CH_DOT ? '' : text.slice(i, j).toLowerCase();
1339 if (top === 'asm' && word !== 'end') {
1340 i = j;
1341 continue;
1342 }
1343 switch (word) {
1344 case 'begin': case 'try': case 'case': case 'asm': case 'repeat':
1345 blocks.push(word);
1346 break;
1347 case 'end':
1348 closeWhile(i, (o) => o.depth >= blocks.length);
1349 blocks.pop();

Callers 1

withOutlineOfFunction · 0.85

Calls 6

closeWhileFunction · 0.85
triviaEndFunction · 0.85
isWordPartFunction · 0.85
withSubjectsAtFunction · 0.85
pushMethod · 0.80
isWordStartFunction · 0.70

Tested by

no test coverage detected