Repository navigation
End mata and python blocks where Stata does, so a mid-line end no longer cuts a block short - #357
Merged
Merged
Conversation
Contributor
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. Next included review available in 1 minute. View limit details
|
… 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
force-pushed
the
lexer-embedded-block-end
branch
from
November 5, 2026 00:38
24dba02 to
919ab33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The lexer ended a
mataorpythonblock at anyendthat finished a line, even in the middle of one (y end,a=end,#end,1end). The context tracker accepted onlyendalone on its line. So this block closed at line 2, and the rest of the Mata code was read as Stata commands:I checked what ends a block in StataMP 18 (on voyager) with about 70 probe do-files. The two languages differ:
endthat 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 doesendinside a comment or string.endin column 0, optionally followed by blanks. An indentedend,end // c,end # c,end python, and/* c */ endare all passed to Python, and the block stays open. This also holds insideif 1 { ... }.Changes:
endcloses 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.compute_expected_depths, used to indent a Python block'sendto the enclosingif/loop/program depth. That turned a working nested Python block into a broken one. A Pythonendnow stays in column 0; a Mataendstill indents. Neither mode re-indents Python body lines (tested).embedded-block-end-indentationandformatter-embedded-block-indentationasserted that a nested Pythonendsits at the opener's depth. Stata 18 leaves that block open (an indentedendinsideif 1 { }raised an IndentationError, and the following Stata lines were sent to Python).end pythonas closing a Python block. In Stata 18 it doesn't close it (Python reports a SyntaxError).end. They now reject only lines that can really close a block, plus///lines, and they splice mid-lineendinto generated content so the property tests exercise it.Known gaps, not changed here:
end foo,end;,end(1), andend = 1, discarding the rest of the line. Sight still accepts only blanks, comments, ormata/pythonafterend./*in Python can hide anendline that Stata would honor.program define, Stata ends the program at the first line whose first word isend, even inside an embedded block.}on every pass when a block insideifis 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.tsmirror 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).