| 1061 | * the source unchanged when there is nothing to blank. |
| 1062 | */ |
| 1063 | export function preParsePascal(source: string): string { |
| 1064 | if (!MAY_NEED_FIXES.test(source)) return source; |
| 1065 | const directives = source.indexOf('{$') >= 0; |
| 1066 | const ranges: number[] = []; |
| 1067 | const words: Array<[number, number, string]> = []; |
| 1068 | const groups = directives ? conditionalGroups(source) : []; |
| 1069 | const groupAt = new Map<number, ConditionalGroup>(); |
| 1070 | for (const group of groups) groupAt.set(group.bounds[0]!, group); |
| 1071 | // Each conditional's opening directive that comes where one statement goes: whether a case label is before it. |
| 1072 | const label = new Map<number, boolean>(); |
| 1073 | // The end of each routine header whose conditional after it had its directives blanked. |
| 1074 | const blankedAfterHeader: number[] = []; |
| 1075 | // The last token read, start and end, for that; -1 right after a declaration the scan read past. |
| 1076 | let lastAt = -1; |
| 1077 | let lastEnd = -1; |
| 1078 | const n = source.length; |
| 1079 | let i = 0; |
| 1080 | while (i < n) { |
| 1081 | const k = commentOrStringEnd(source, i); |
| 1082 | if (k >= 0) { |
| 1083 | if (source.charCodeAt(i) === CH_QUOTE) { |
| 1084 | lastAt = i; |
| 1085 | lastEnd = k; |
| 1086 | } else if (lastAt >= 0 && source.charCodeAt(i + 1) === CH_DOLLAR && groupAt.has(i)) { |
| 1087 | const position = statementPosition(source, lastAt, lastEnd); |
| 1088 | if (position >= 0) label.set(i, position === 1); |
| 1089 | } |
| 1090 | i = k; |
| 1091 | continue; |
| 1092 | } |
| 1093 | const c = source.charCodeAt(i); |
| 1094 | if (isIdentStart(c)) { |
| 1095 | const e = wordEnd(source, i + 1); |
| 1096 | const len = e - i; |
| 1097 | const word = len >= 6 && len <= 11 ? source.slice(i, e).toLowerCase() : ''; |
| 1098 | // `operator` is no reserved word in Delphi (a call `Operator([1, 2])` is |
| 1099 | // code), so it starts a header only after `class`. |
| 1100 | if (ROUTINE_KEYWORDS.has(word) && (word !== 'operator' || classBefore(source, i) >= 0)) { |
| 1101 | const open = skipTrivia(source, routineNameEnd(source, e)); |
| 1102 | const next = source.charCodeAt(open) === CH_LPAREN ? scanParameterList(source, open, ranges) : e; |
| 1103 | if (groupAt.size > 0) { |
| 1104 | const headerEnd = semicolonEnd(source, next, MAX_HEADER_PART_LENGTH); |
| 1105 | if (headerEnd >= 0) { |
| 1106 | const before = ranges.length; |
| 1107 | blankConditionalsAfterHeader(source, headerEnd, groupAt, ranges); |
| 1108 | if (ranges.length > before) blankedAfterHeader.push(headerEnd); |
| 1109 | } |
| 1110 | } |
| 1111 | i = next; |
| 1112 | lastAt = -1; |
| 1113 | continue; |
| 1114 | } else if (word === 'property') { |
| 1115 | i = scanPropertyOverride(source, i, e, ranges); |
| 1116 | lastAt = -1; |
| 1117 | continue; |
| 1118 | } else if (word === 'record') { |
| 1119 | i = scanAnonymousRecord(source, i, e, ranges, words); |
| 1120 | lastAt = -1; |