* PHP before 8.0 also wrote a string offset with braces, `$code{0}` or * `$this->name{0}`. The grammar knows only brackets, and its recovery could * drop the whole function around such an offset (#3299). Outside strings, * comments and heredocs, braces right after a variable or a property name *
(source: string)
| 285 | * `{$x}` and `$obj->{$name}` don't match. |
| 286 | */ |
| 287 | function phpBraceOffsetsAsBrackets(source: string): string { |
| 288 | if (!/(?:\$|->)[A-Za-z_]\w*\{/.test(source)) return source; |
| 289 | const code = phpCodeOnly(source); |
| 290 | const offset = /(?:\$|->)[A-Za-z_]\w*\{/g; |
| 291 | let out: string[] | null = null; |
| 292 | for (let m = offset.exec(code); m; m = offset.exec(code)) { |
| 293 | const open = m.index + m[0].length - 1; |
| 294 | let depth = 0; |
| 295 | let close = -1; |
| 296 | for (let k = open; k < code.length; k++) { |
| 297 | if (code[k] === '{') depth++; |
| 298 | else if (code[k] === '}' && --depth === 0) { close = k; break; } |
| 299 | } |
| 300 | if (close < 0) continue; |
| 301 | out ??= source.split(''); |
| 302 | out[open] = '['; |
| 303 | out[close] = ']'; |
| 304 | } |
| 305 | return out ? out.join('') : source; |
| 306 | } |
| 307 | |
| 308 | export const phpExtractor: LanguageExtractor = { |
| 309 | preParse: phpBraceOffsetsAsBrackets, |
nothing calls this directly
no test coverage detected