* Finds the offset of the '(' that matches the current ')' at offset 0. * Handles escaped characters (backslash followed by any character).
(input: InputStream)
| 302 | * Handles escaped characters (backslash followed by any character). |
| 303 | */ |
| 304 | function findMatchingOpenParenOffset(input: InputStream): number | null { |
| 305 | if (input.next !== CLOSE_PAREN) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | let offset = -1; |
| 310 | let depth = 1; |
| 311 | |
| 312 | while (true) { |
| 313 | const ch = input.peek(offset); |
| 314 | |
| 315 | if (ch === EOF) { |
| 316 | return null; |
| 317 | } |
| 318 | |
| 319 | if (isEscapedAt(input, offset)) { |
| 320 | offset--; |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | if (ch === CLOSE_PAREN) { |
| 325 | depth++; |
| 326 | } else if (ch === OPEN_PAREN) { |
| 327 | depth--; |
| 328 | if (depth === 0) { |
| 329 | return offset; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | offset--; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Checks if we're currently inside a ParenExpr by looking backwards in the input |