| 991 | * the source unchanged when there is nothing to blank. |
| 992 | */ |
| 993 | export function preParsePascal(source: string): string { |
| 994 | if (!MAY_NEED_FIXES.test(source)) return source; |
| 995 | const directives = source.indexOf('{$') >= 0; |
| 996 | const ranges: number[] = []; |
| 997 | const words: Array<[number, number, string]> = []; |
| 998 | const groups = directives ? conditionalGroups(source) : []; |
| 999 | const groupAt = new Map<number, ConditionalGroup>(); |
| 1000 | for (const group of groups) groupAt.set(group.bounds[0]!, group); |
| 1001 | // Each conditional's opening directive that comes where one statement goes: whether a case label is before it. |
| 1002 | const label = new Map<number, boolean>(); |
| 1003 | // The last token read, start and end, for that; -1 right after a declaration the scan read past. |
| 1004 | let lastAt = -1; |
| 1005 | let lastEnd = -1; |
| 1006 | const n = source.length; |
| 1007 | let i = 0; |
| 1008 | while (i < n) { |
| 1009 | const k = commentOrStringEnd(source, i); |
| 1010 | if (k >= 0) { |
| 1011 | if (source.charCodeAt(i) === CH_QUOTE) { |
| 1012 | lastAt = i; |
| 1013 | lastEnd = k; |
| 1014 | } else if (lastAt >= 0 && source.charCodeAt(i + 1) === CH_DOLLAR && groupAt.has(i)) { |
| 1015 | const position = statementPosition(source, lastAt, lastEnd); |
| 1016 | if (position >= 0) label.set(i, position === 1); |
| 1017 | } |
| 1018 | i = k; |
| 1019 | continue; |
| 1020 | } |
| 1021 | const c = source.charCodeAt(i); |
| 1022 | if (isIdentStart(c)) { |
| 1023 | const e = wordEnd(source, i + 1); |
| 1024 | const len = e - i; |
| 1025 | const word = len >= 6 && len <= 11 ? source.slice(i, e).toLowerCase() : ''; |
| 1026 | // `operator` is no reserved word in Delphi (a call `Operator([1, 2])` is |
| 1027 | // code), so it starts a header only after `class`. |
| 1028 | if (ROUTINE_KEYWORDS.has(word) && (word !== 'operator' || classBefore(source, i) >= 0)) { |
| 1029 | const open = skipTrivia(source, routineNameEnd(source, e)); |
| 1030 | const next = source.charCodeAt(open) === CH_LPAREN ? scanParameterList(source, open, ranges) : e; |
| 1031 | if (groupAt.size > 0) { |
| 1032 | const headerEnd = semicolonEnd(source, next, MAX_HEADER_PART_LENGTH); |
| 1033 | if (headerEnd >= 0) blankConditionalsAfterHeader(source, headerEnd, groupAt, ranges); |
| 1034 | } |
| 1035 | i = next; |
| 1036 | lastAt = -1; |
| 1037 | continue; |
| 1038 | } else if (word === 'property') { |
| 1039 | i = scanPropertyOverride(source, i, e, ranges); |
| 1040 | lastAt = -1; |
| 1041 | continue; |
| 1042 | } else if (word === 'record') { |
| 1043 | i = scanAnonymousRecord(source, i, e, ranges, words); |
| 1044 | lastAt = -1; |
| 1045 | continue; |
| 1046 | } else if (len === 3 && source.slice(i, e).toLowerCase() === 'for') { |
| 1047 | i = scanForInVariable(source, e, ranges); |
| 1048 | lastAt = -1; |
| 1049 | continue; |
| 1050 | } else if (len === 5 && source.slice(i, e).toLowerCase() === 'raise') { |