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

Function scanForInVariable

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

* After a `for` keyword ending at `forEnd`, recognize `var Name in` or * `var Name: Type in` and push the `var` range and the `: Type` range onto * `ranges`. The type runs to the `in` at nesting depth 0, so generic types * (`TPair >`) and dotted names are whole. Any other *

(s: string, forEnd: number, ranges: number[])

Source from the content-addressed store, hash-verified

366 * nothing. Returns the index to resume scanning from.
367 */
368function scanForInVariable(s: string, forEnd: number, ranges: number[]): number {
369 const varStart = skipTrivia(s, forEnd);
370 if (lowerWordAt(s, varStart) !== 'var') return forEnd;
371 const varEnd = varStart + 3;
372 const nameStart = skipTrivia(s, varEnd);
373 if (!isIdentStart(s.charCodeAt(nameStart))) return forEnd;
374 let i = skipTrivia(s, wordEnd(s, nameStart + 1));
375 let typeStart = -1;
376 if (s.charCodeAt(i) === CH_COLON && s.charCodeAt(i + 1) !== CH_EQ) {
377 typeStart = i;
378 const limit = Math.min(s.length, i + MAX_LOOP_TYPE_LENGTH);
379 let depth = 0;
380 i++;
381 for (;;) {
382 if (i >= limit) return forEnd;
383 const k = commentOrStringEnd(s, i);
384 if (k >= 0) {
385 i = k;
386 continue;
387 }
388 const c = s.charCodeAt(i);
389 if (c === CH_LT || c === CH_LPAREN || c === CH_LBRACKET) {
390 depth++;
391 } else if (c === CH_GT || c === CH_RPAREN || c === CH_RBRACKET) {
392 if (--depth < 0) return forEnd;
393 } else if (c === CH_SEMI || c === CH_EQ) {
394 return forEnd; // `for var i: Integer := 0 to …`, or no loop at all
395 } else if (isIdentStart(c)) {
396 const word = lowerWordAt(s, i);
397 if (depth === 0 && word === 'in') break;
398 if (NOT_A_FOR_IN.has(word)) return forEnd;
399 i = wordEnd(s, i + 1);
400 continue;
401 }
402 i++;
403 }
404 }
405 if (lowerWordAt(s, i) !== 'in') return forEnd;
406 ranges.push(varStart, varEnd);
407 if (typeStart >= 0) ranges.push(typeStart, i);
408 return i;
409}
410
411/**
412 * After a `property` keyword running from `start` to `propertyEnd`,

Callers 1

preParsePascalFunction · 0.85

Calls 7

lowerWordAtFunction · 0.85
isIdentStartFunction · 0.85
commentOrStringEndFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
skipTriviaFunction · 0.70
wordEndFunction · 0.70

Tested by

no test coverage detected