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

Function scanInterface

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

* Adds the routine names a Pascal file declares while in an interface * section to `names` (see interfaceRoutineNames), starting in one or not; * whether it ends in one, and whether it starts an implementation.

(filePath: string, inInterface: boolean, names: Set<string>, context: ResolutionContext, depth: number)

Source from the content-addressed store, hash-verified

2239 * whether it ends in one, and whether it starts an implementation.
2240 */
2241function scanInterface(filePath: string, inInterface: boolean, names: Set<string>, context: ResolutionContext, depth: number):
2242 { inInterface: boolean; implementation: boolean } {
2243 const text = fileLines(filePath, context).join('\n');
2244 let implementation = false;
2245 // Per open `{$if…}`: the section it opened in, whether a branch of it so
2246 // far ended in the interface, and whether it has an `{$else}`.
2247 const open: { from: boolean; anyInterface: boolean; hasElse: boolean }[] = [];
2248 // After `procedure` or `function` in the interface, the next word is a
2249 // routine's name, whatever comments and directives come between:
2250 // mORMot's `{$ifdef OSLINUXANDROID} function {$endif} CpuThreads: cardinal;`.
2251 let nameDue = false;
2252 for (let i = 0; i < text.length;) {
2253 const c = text.charCodeAt(i);
2254 const end = triviaEnd(text, i);
2255 if (end > i) {
2256 if (c === CH_LBRACE && text.charCodeAt(i + 1) === CH_DOLLAR) {
2257 const directive = text.slice(i, Math.min(end, i + 400));
2258 const word = /^\{\$(\w+)/.exec(directive)?.[1]?.toLowerCase();
2259 const top = open[open.length - 1];
2260 if (word === 'if' || word === 'ifdef' || word === 'ifndef' || word === 'ifopt') {
2261 open.push({ from: inInterface, anyInterface: false, hasElse: false });
2262 } else if ((word === 'else' || word === 'elseif') && top) {
2263 top.anyInterface ||= inInterface;
2264 top.hasElse ||= word === 'else';
2265 inInterface = top.from;
2266 } else if ((word === 'endif' || word === 'ifend') && top) {
2267 inInterface = top.anyInterface || inInterface || (!top.hasElse && top.from);
2268 open.pop();
2269 } else if (inInterface && depth < 3) {
2270 const inc = /^\{\$(?:I|INCLUDE)\s+['"]?([^'"}\s*]+)/i.exec(directive);
2271 if (inc) {
2272 let name = inc[1]!.replace(/\\/g, '/');
2273 name = name.slice(name.lastIndexOf('/') + 1).toLowerCase();
2274 if (!name.includes('.')) name += '.inc';
2275 const file = nearest(projectFiles(context).byName.get(name) ?? [], filePath);
2276 if (file && file !== filePath) {
2277 const after = scanInterface(file, inInterface, names, context, depth + 1);
2278 inInterface = after.inInterface;
2279 implementation ||= after.implementation;
2280 }
2281 }
2282 }
2283 }
2284 i = end;
2285 continue;
2286 }
2287 if (!isWordStart(c)) {
2288 // Blanks and an identifier's `&` keep a name due; anything else ends it.
2289 if (c !== CH_SPACE && c !== CH_TAB && c !== CH_LF && c !== CH_CR && c !== CH_AMP) nameDue = false;
2290 i++;
2291 continue;
2292 }
2293 let j = i + 1;
2294 while (j < text.length && isWordPart(text.charCodeAt(j))) j++;
2295 const word = text.slice(i, j).toLowerCase();
2296 if (nameDue) {
2297 names.add(word);
2298 nameDue = false;

Callers 1

interfaceRoutineNamesFunction · 0.85

Calls 11

fileLinesFunction · 0.85
triviaEndFunction · 0.85
nearestFunction · 0.85
projectFilesFunction · 0.85
isWordPartFunction · 0.85
pushMethod · 0.80
isWordStartFunction · 0.70
execMethod · 0.65
getMethod · 0.65
joinMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected