Repository navigation
Read #delimit only at the start of a statement, end input data at a row that doesn't fit its variables, and keep every line when formatting - #428
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 33 minutes. View limit details
|
…ow that doesn't fit its variables, and keep every line when formatting - #d/#delimit is a directive only as the first code of a statement (StataMP 18): not in macro text, after a prefix, or at the start of a line inside a #delimit ; statement; also after a ; on the same line. The TextMate grammar follows as far as it can. - input data ends at end, or before the first row that doesn't fit the declared variables (text where a numeric variable needs a number); rows that fit string variables are data. Replaces the program-header rule, and lexes many input blocks in linear time. - The lexer decides where a program definition starts and marks the program token; the parser follows it. - mi xeq: notes: mata: is a note; summarize x[`weight'`exp'] has a weight; a qualifier glued to a macro in a varlist is the qualifier (statement left as written); char rename keeps its option. - AST formatting keeps continued if ... input data, Mata after a // comment under #delimit ;, an empty ; record, and program de`p'; Python one-liner continuation lines stay as written. - Strings span lines under #delimit ;, as in Stata.
jbearak
force-pushed
the
t3/fix-lexer-parser-printer-regressions
branch
from
November 19, 2026 16:06
719bde2 to
223e872
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.
A cold review of #412, #414, #417, #418 and #419 found that the fix round broke things it was meant to fix.
local tag {#d ;}switched the delimiter and swallowed the rest of the file.inputdata rows such asprogram demowere read as code. AST formatting dropped or moved code: data after a continuedif … input, Mata pulled into a//comment, and a tab in a Python line. This PR fixes those regressions and most of the older items. I checked each language question in StataMP 18 before writing the rule.What Stata does, and what Sight now does
#delimit(H3, L5). It is a directive only as the first code of a statement. That means at the start of a line, after blanks or block comments, or under#delimit ;after a;on the same line (di 1; #d cr). It is not a directive in macro text, after a prefix (quietly #d ;gives r(199)), or after{. It is also not one after a///join or a block comment carried over from a code line. Under#delimit ;it is not one at the start of a line inside a statement:di "L0"then#d crstays part of the statement. The TextMate grammar now has a#delimit ;region, so it also scopes#dafter a;, only under;.Where
inputdata ends (H4, M2, M11). This replaces Read input data, prefixed mata: and python: openers, and #delimit ; star comments as Stata does #414's program-header rule. Stata stops reading data once it has filled the observations of a dataset that already has some, which Sight can't know. So Sight reads the declared variables instead. A row fits when every numeric variable among the first n fields has a field Stata reads as a number. The data ends before the first row that doesn't fit, with noend. That row and the lines after it are code. Stata log lines behind the row rule:1 2underinput x→ x=1. Extra fields are ignored.1underinput x y→'' cannot be read as a number. A missing numeric field fails.1underinput x str5 ais accepted. A missing string field is blank.3,4→',' cannot be read as a number. A comma is its own field."5"→ rejected. A quoted field is never a number.1.0X+003,1d3,.zand1/2are accepted;-.a,1eandinfare rejected.x:yn, labelacceptsyes. Withlabelorautomaticalone, text is rejected.program demounderinput str12 a str12 bis data."a;end";under#delimit ;is one value.set obs 1; input x; 1; program define demo;→ Stata reads1, then runsprogram define demo.Note: the review's "three fields" reason was wrong, because Stata ignores extra fields.
local label scoreendsinput scorebecauselocalis not a number. When the declaration can't be read (no varlist, a macro in it), every row up toendis data. This is documented insrc/utils/input-data.tsanddocs/cli.md.Program headers (M1, M9). The lexer now decides where a definition starts and marks the
programtoken (Token.starts_program_definition). The parser reads that mark. Both use one recognizer,program_header_text+is_program_definition_header, which reads to the;under#delimit ;. It handles a header split over lines (program/define p;), a header after;,program // note,program `p', andprogram .Name.program de`p'now namesdep'`.M3.
mi xeq: notes: mata:gives the notemata:in Stata.mata:now opens Mata only after a real prefix's colon.M6. In a varlist,
summarize x[`weight'`exp']is a weight. Inside an expression,price[`weight']stays a subscript (Parse include using/…, programname', and gluedstem'using as Stata does, and keep label text and program-header comments when formatting #417's case).M7.
summarize price `opts'in 1/2: Stata reads the qualifier whenoptsis empty, andvariable detailin not foundwhen it isdetail. I chose the qualifier reading in a varlist, because it is the reading under which the line runs with an optional, often empty, macro. Because that is a guess, the statement is marked unprintable, so AST formatting leaves it as written. Expressions keep Parse include using/…, programname', and gluedstem'using as Stata does, and keep label text and program-header comments when formatting #417's name reading. The corpus has no glued case either way.M8.
char rename oldvar newvar, replacekeeps its option; Stata runs it.H1, H2, M4, L2. AST formatting keeps:
endafterif 1 input ///;//comment under#delimit ;;;record;///in a Python one-liner.On M4: Stata joins at a
///inside'…'(SyntaxError: EOL while scanning string literal), so Sight's swallowing there was correct. Only the tab change was wrong. In double quotes,;and///are text for both Python and Mata (Stata log), which Sight already did.M12. The context range of a
///-joinedmata:line now runs through the joined line, as the parser's does, so hover there offers no Stata help. Joined Python lines stay byte for byte. Joined Mata lines still move with their first line.Found by the corpus check. Under
#delimit ;a string may span lines. Stata log:local o "a b/c d" ;→[a b c d]. Stata's ownml_searc.adorelies on this.Small fixes. M5: lexing many
inputblocks is now linear; the unclosed/*afterendwas scanned to EOF. L1: an AST withoutkeywordprints#delimit. L4:cd using/datais a path.Not fixed
title(First wave)still printstitle(First wave)in AST mode. Single-space option arguments are a deliberate contract (Parser: #delimit ; reconstructs expression/qualifier/paren strings with internal spacing that diverges from #delimit cr #306), and changing it would changeparse --jsonbroadly.program define. Stata ends the outer program at the firstend. Sight still reads a nested definition, as before, because four analyzer tests depend on it.mata: x = 1 // c ///(cr mode). The parse loses the next line. This is the same on main and out of scope.Fixtures (
probe.ts, origin/main → this branch)=New-York→New -York=1010, then local/program,=1, program nodeMissing "end"displayat top levelUnclosed mata; AST adds a space each pass=weight: null`weight'in: nullin: 1/2, left as written[]replaceend=// comment + 2;+ 2,displaykept=program dep'`=Unexpected "end"=usingclauseusing/dataCorpus (3,780 base ados)
All 6 removals were false positives:
#delimit ;strings inml_searc.adoandqchi_7.ado;program ...in_lstretch_get_width.adohelp text, which is no longer read as a header.The corpus has no real
input … endblocks; every hit was Mata or help text. The row classifier is therefore checked against the Stata logs above, not against the corpus.Proof
tests/unit/lexer-parser-review-round2.test.tshas 47 tests, with a second-pass idempotence check in both formatter modes. On origin/main (1c22bfb), 36 fail and 11 pass; the 11 are guards for behavior main already had right. All 47 pass here.check:line-endingstypecheckbun test ./tests: 10212 pass, 0 fail (after rebasing on 21b95b3)lint: 0 problemstest:ci: 10224 + 2 passExisting tests changed to the new rules:
input-data.test.tsused invalid rows for numeric variables, andstatement-span.test.ts,parser-release-audit-regressions.test.tsand the grammar pattern-order tests (nowcr-content) encoded the old behavior.Created with Claude Opus 5.5 in Claude Code (T3 Code).