* How many arguments a Pascal call passes: the list in parentheses after the * called name, from the reference's column on and across lines; 0 for a call * written without one. Null when the name is not there, or is indexed.
(ref: UnresolvedRef, context: ResolutionContext)
| 521 | * written without one. Null when the name is not there, or is indexed. |
| 522 | */ |
| 523 | function pascalCallArgumentCount(ref: UnresolvedRef, context: ResolutionContext): number | null { |
| 524 | const lines = fileLines(ref.filePath!, context); |
| 525 | const line = lines[ref.line - 1]; |
| 526 | if (line === undefined) return null; |
| 527 | const name = ref.referenceName.slice(ref.referenceName.lastIndexOf('.') + 1); |
| 528 | if (!/^[A-Za-z_]\w*$/.test(name)) return null; |
| 529 | // The name's first mention from the reference on that a `.` does not |
| 530 | // follow: mORMot's `log.Log(sllWarning, …)` calls `Log` on a local `log`. |
| 531 | const word = new RegExp(`\\b${escapeRegExp(name)}\\b`, 'gi'); |
| 532 | word.lastIndex = Math.max(0, ref.column ?? 0); |
| 533 | let found = word.exec(line); |
| 534 | while (found && /^\s*(?:\^\s*)?\./.test(line.slice(found.index + found[0].length))) found = word.exec(line); |
| 535 | if (!found) return null; |
| 536 | // The text after the name, a line more at a time while the call runs on (40 at most). |
| 537 | let text = line.slice(found.index + found[0].length); |
| 538 | let next = ref.line; |
| 539 | const more = (): boolean => { |
| 540 | if (next >= lines.length || next >= ref.line + 40) return false; |
| 541 | text += '\n' + lines[next++]!; |
| 542 | return true; |
| 543 | }; |
| 544 | const find = (token: string, from: number): number => { |
| 545 | for (;;) { |
| 546 | const at = text.indexOf(token, from); |
| 547 | if (at >= 0 || !more()) return at; |
| 548 | } |
| 549 | }; |
| 550 | let i = 0; |
| 551 | const skipBlanks = (): void => { |
| 552 | do { |
| 553 | while (i < text.length && /\s/.test(text[i]!)) i++; |
| 554 | } while (i >= text.length && more()); |
| 555 | }; |
| 556 | skipBlanks(); |
| 557 | // Explicit generic arguments: `Make<TFoo>(…)`. |
| 558 | if (text[i] === '<') { |
| 559 | const close = find('>', i); |
| 560 | if (close < 0) return null; |
| 561 | i = close + 1; |
| 562 | skipBlanks(); |
| 563 | } |
| 564 | if (text[i] === '[') return null; |
| 565 | if (text[i] !== '(') return 0; |
| 566 | // Inside the call's own parentheses. |
| 567 | i++; |
| 568 | let depth = 1; |
| 569 | let commas = 0; |
| 570 | let any = false; |
| 571 | for (;;) { |
| 572 | if (i >= text.length && !more()) return null; |
| 573 | const c = text[i]!; |
| 574 | if (c === "'") { |
| 575 | // A string, on its line; '' is a quote inside it. |
| 576 | for (i++; i < text.length && !(text[i] === "'" && text[i + 1] !== "'"); i += text[i] === "'" ? 2 : 1); |
| 577 | any = true; |
| 578 | i++; |
| 579 | continue; |
| 580 | } |
no test coverage detected