Skip to content

End mata and python blocks where Stata does, so a mid-line end no longer cuts a block short - #357

Merged
jbearak merged 3 commits into
mainfrom
lexer-embedded-block-end
Nov 5, 2026
Merged

jbearak merged 3 commits into
mainfrom
lexer-embedded-block-end

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026

Copy link
Copy Markdown
Owner

The lexer ended a mata or python block at any end that finished a line, even in the middle of one (y end, a=end, #end, 1end). The context tracker accepted only end alone on its line. So this block closed at line 2, and the rest of the Mata code was read as Stata commands:

mata
x = 1 // end
y end
z = 2
end
di 1

I checked what ends a block in StataMP 18 (on voyager) with about 70 probe do-files. The two languages differ:

  • Mata closes at an end that begins its logical line. Leading blanks and closed /* */ comments may come first. Code earlier on the line (y end, x = 1; end) or a /// join from the line above keeps the block open, as does end inside a comment or string.
  • Python closes only at a line that is exactly end in column 0, optionally followed by blanks. An indented end, end // c, end # c, end python, and /* c */ end are all passed to Python, and the block stays open. This also holds inside if 1 { ... }.

Changes:

  • The lexer follows those two rules.
  • The tracker's check for which end closes a program used its own raw-line rule for where a block ends. It now uses the lexer's ranges, so the two always agree.
  • Both formatter modes, and the indentation depths in compute_expected_depths, used to indent a Python block's end to the enclosing if/loop/program depth. That turned a working nested Python block into a broken one. A Python end now stays in column 0; a Mata end still indents. Neither mode re-indents Python body lines (tested).
  • Updated tests that encoded the old rule:
    • embedded-block-end-indentation and formatter-embedded-block-indentation asserted that a nested Python end sits at the opener's depth. Stata 18 leaves that block open (an indented end inside if 1 { } raised an IndentationError, and the following Stata lines were sent to Python).
    • Three tests treated end python as closing a Python block. In Stata 18 it doesn't close it (Python reports a SyntaxError).
  • Fix three property-test flakes: an expression range outside its input, and generators that emit invalid commands and block content #352 made the block-content generators reject any line containing end. They now reject only lines that can really close a block, plus /// lines, and they splice mid-line end into generated content so the property tests exercise it.

Known gaps, not changed here:

  • In Stata, Mata also closes at end foo, end;, end(1), and end = 1, discarding the rest of the line. Sight still accepts only blanks, comments, or mata/python after end.
  • Python content is still lexed with Stata comment rules, so a /* in Python can hide an end line that Stata would honor.
  • Inside program define, Stata ends the program at the first line whose first word is end, even inside an embedded block.
  • AST formatting adds a } on every pass when a block inside if is never closed. This already happened before this change.

Proof: reproduced the block closing early on main; the new tests in tests/unit/embedded-block-end-rule.test.ts mirror each StataMP 18 probe, and the new mid-line and Python cases fail on main and pass here. Formatter tests in both modes show the blocks are unchanged and that formatting is idempotent.

Reviewed with sol-loop: 3 rounds with gpt-6.1-sol. Sol verdict: approved. After the review, rebasing onto #351 changed how the lexer tracks /// joins; the gates and repeated property-test runs pass on the rebased branch.

Created with Claude Opus 5.5 in Claude Code; reviewed by gpt-6.1-sol (Codex).

@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available. Your 32 included PR review attempts over the past 7 days set your current allowance at 8 reviews per hour.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 5dcf3ffc-2f21-488a-8965-e664b0b75721
Commits

Reviewing files that changed from the base of the PR and between 61ce8bd and 919ab33.

Files selected for processing: 15

  • src/context-tracker/index.ts
  • src/lexer/index.ts
  • src/pretty-printer/index.ts
  • src/providers/formatter.ts
  • src/providers/indentation-diagnostics.ts
  • tests/property/embedded-block-end-indentation.prop.test.ts
  • tests/property/formatter-embedded-block-indentation.prop.test.ts
  • tests/property/generator-validity.test.ts
  • tests/property/generators/primitives.ts
  • tests/property/invalid-end-syntax-tokenization.prop.test.ts
  • tests/property/parser-end-delimiter-handling.prop.test.ts
  • tests/unit/context-tracker-initialize-from-tokens.test.ts
  • tests/unit/context-tracker.test.ts
  • tests/unit/embedded-block-end-rule.test.ts
  • tests/unit/lexer.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

… end in column 0

- The lexer ended a block at any 'end' that finished a line, even
  mid-line ('y end', 'a=end', '#end', '1end'). Stata 18 ends a Mata
  block only at an 'end' that begins its logical line, and a Python
  block only at a line that is exactly 'end' in column 0.
- The context tracker's program/input end scan re-derived block
  extents from raw lines; it now uses the lexer's ranges.
- Both formatter modes and the indentation depths kept a Python end
  at the opener's depth, which leaves the block open in Stata.
- The block-content generators reject only lines that can really end
  a block, and splice in mid-line 'end' so properties exercise it.
@jbearak
jbearak force-pushed the lexer-embedded-block-end branch from 24dba02 to 919ab33 Compare November 5, 2026 00:38
@jbearak
jbearak merged commit 6761245 into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the lexer-embedded-block-end branch November 5, 2026 00:43
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.

1 participant