| 237 | * `exclude` stays with the Objective-C grammar whatever it holds. |
| 238 | */ |
| 239 | export function splitObjcpp(source: string, exclude?: ReadonlySet<number>): ObjcppParts | null { |
| 240 | // A file with no `::`, no brace after a name and no C++ keyword has nothing |
| 241 | // to hand over; most Objective-C files stop here. |
| 242 | if (!/::|[\w>]\{|\b(?:namespace|template|class|nullptr|constexpr|noexcept|decltype|operator|virtual|explicit|auto|using|new|delete|try|catch|throw|this|\w+_cast)\b/.test(source)) { |
| 243 | return null; |
| 244 | } |
| 245 | const n = source.length; |
| 246 | const items: ObjcppItem[] = []; |
| 247 | let edits: Array<[number, string]> = []; |
| 248 | let spans: Array<[number, number]> = []; |
| 249 | // Each open `#if` group: the brackets open at the `#if`, those open after |
| 250 | // its first branch, and the item that holds the `#if` (-1: none does). |
| 251 | const groups: Array<{ atIf: Frame[]; afterFirst: Frame[] | null; item: number }> = []; |
| 252 | // The namespaces and linkage blocks open around the next item: whether each |
| 253 | // one's own braces went to the C++ grammar. What a namespace holds is C++. |
| 254 | const wrappers: boolean[] = []; |
| 255 | let itemCount = 0; |
| 256 | // The C++ items that can name something. |
| 257 | let named = 0; |
| 258 | let failed = false; |
| 259 | |
| 260 | const frame = (ch: number, at: number): Frame => ({ |
| 261 | ch, at, spanStart: -1, last: Value.None, send: false, weakSend: false, subscript: false, forParen: false, body: Body.None, |
| 262 | }); |
| 263 | |
| 264 | /** The Objective-C block from the `@` at `at` through its `@end`; `#if` groups inside are counted. */ |
| 265 | const atBlockEnd = (at: number): number => { |
| 266 | let lineStart = false; |
| 267 | for (let j = at + 1; j < n; ) { |
| 268 | const c = source.charCodeAt(j); |
| 269 | if (c === NEWLINE) { |
| 270 | lineStart = true; |
| 271 | j++; |
| 272 | } else if (isBlank(c)) j++; |
| 273 | else if (c === SLASH && source.charCodeAt(j + 1) === SLASH) j = lineCommentEnd(source, j + 2); |
| 274 | else if (c === SLASH && source.charCodeAt(j + 1) === STAR) { |
| 275 | const end = source.indexOf('*/', j + 2); |
| 276 | j = end < 0 ? n : end + 2; |
| 277 | } else if (c === HASH && lineStart) { |
| 278 | const name = directiveName(source, j); |
| 279 | if (CONDITIONAL_OPEN.has(name)) groups.push({ atIf: [], afterFirst: null, item: -2 }); |
| 280 | else if (name === 'endif') groups.pop(); |
| 281 | j = directiveEnd(source, j); |
| 282 | } else { |
| 283 | lineStart = false; |
| 284 | if (c === QUOTE || c === APOSTROPHE) j = quotedEnd(source, j); |
| 285 | else if (c === AT && source.startsWith('end', j + 1) && !isWordChar(source.charCodeAt(j + 4))) return j + 4; |
| 286 | else j++; |
| 287 | } |
| 288 | } |
| 289 | return n; |
| 290 | }; |
| 291 | |
| 292 | /** |
| 293 | * Read one item from `start`, its first comment or token; `lineStart` says |
| 294 | * nothing but blanks comes before it on its line. Returns its end. A C++ one |
| 295 | * is added to `items`, with its edits and spans. |
| 296 | */ |