Skip to content

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Dec 25, 2025

This addresses the deep nesting/recursion limits issue documented in
open-problems.md. The solution converts key parsing and compilation
paths from recursive to iterative approaches using explicit stacks.

Parser changes:

  • Handle consecutive parentheses ((((1)))) iteratively
  • Parse nested array literals [[[[1]]]] with a stack-based approach
  • Add _parse_block_statement_iterative() for nested blocks {{{1;}}}
  • Add _parse_nested_arrays() for nested array literals
  • Add _continue_parsing_expression() for operators between parens
  • Add _continue_binary_expression() for binary operators after parens

Compiler changes:

  • Compile MemberExpression chains iteratively (for a[0][0][0]...)
  • Use work stack for ArrayExpression compilation
  • Handle BlockStatement compilation iteratively
  • Make _compile_statement_for_value() drill through nested blocks

The approach was inspired by mquickjs's CPS-style parser, adapted to
Python with a simpler stack-based approach.

Tests now passing:

  • test_deep_nested_parens
  • test_deep_nested_braces
  • test_deep_nested_arrays
  • test_large_eval_parse_stack

Still xfail: test_deep_nested_regex_groups (regex parser)

Claude Code transcript: https://gistpreview.github.io/?3c3e6aec285e3d5c2eeb0e2ae02c3a26/index.html

claude and others added 2 commits December 25, 2025 06:47
This addresses the deep nesting/recursion limits issue documented in
open-problems.md. The solution converts key parsing and compilation
paths from recursive to iterative approaches using explicit stacks.

Parser changes:
- Handle consecutive parentheses ((((1)))) iteratively
- Parse nested array literals [[[[1]]]] with a stack-based approach
- Add _parse_block_statement_iterative() for nested blocks {{{1;}}}
- Add _parse_nested_arrays() for nested array literals
- Add _continue_parsing_expression() for operators between parens
- Add _continue_binary_expression() for binary operators after parens

Compiler changes:
- Compile MemberExpression chains iteratively (for a[0][0][0]...)
- Use work stack for ArrayExpression compilation
- Handle BlockStatement compilation iteratively
- Make _compile_statement_for_value() drill through nested blocks

The approach was inspired by mquickjs's CPS-style parser, adapted to
Python with a simpler stack-based approach.

Tests now passing:
- test_deep_nested_parens
- test_deep_nested_braces
- test_deep_nested_arrays
- test_large_eval_parse_stack

Still xfail: test_deep_nested_regex_groups (regex parser)
@simonw simonw merged commit 02d464e into main Dec 25, 2025
10 checks passed
@simonw simonw deleted the claude/iterative-parsing-nesting-xa6a9 branch December 25, 2025 21:26
simonw added a commit that referenced this pull request Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants