Skip to content

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
jbearak merged 2 commits into
mainfrom
t3/fix-lexer-parser-printer-regressions
Nov 19, 2026
Merged

jbearak merged 2 commits into
mainfrom
t3/fix-lexer-parser-printer-regressions

Conversation

@jbearak

@jbearak jbearak commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

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. input data rows such as program demo were read as code. AST formatting dropped or moved code: data after a continued if … 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 cr stays part of the statement. The TextMate grammar now has a #delimit ; region, so it also scopes #d after a ;, only under ;.

  • Where input data 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 no end. That row and the lines after it are code. Stata log lines behind the row rule:

    • 1 2 under input x → x=1. Extra fields are ignored.
    • 1 under input x y → '' cannot be read as a number. A missing numeric field fails.
    • 1 under input x str5 a is 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, .z and 1/2 are accepted; -.a, 1e and inf are rejected.
    • x:yn, label accepts yes. With label or automatic alone, text is rejected.
    • program demo under input str12 a str12 b is data.
    • "a;end"; under #delimit ; is one value.
    • set obs 1; input x; 1; program define demo; → Stata reads 1, then runs program define demo.

    Note: the review's "three fields" reason was wrong, because Stata ignores extra fields. local label score ends input score because local is not a number. When the declaration can't be read (no varlist, a macro in it), every row up to end is data. This is documented in src/utils/input-data.ts and docs/cli.md.

  • Program headers (M1, M9). The lexer now decides where a definition starts and marks the program token (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', and program .Name. program de`p' now names dep'`.

  • M3. mi xeq: notes: mata: gives the note mata: 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/…, program name', and glued stem'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 when opts is empty, and variable detailin not found when it is detail. 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/…, program name', and glued stem'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, replace keeps its option; Stata runs it.

  • H1, H2, M4, L2. AST formatting keeps:

    • the data and end after if 1 input ///;
    • Mata after a // comment under #delimit ;;
    • an empty ; record;
    • a tab before /// 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 ///-joined mata: 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 own ml_searc.ado relies on this.

  • Small fixes. M5: lexing many input blocks is now linear; the unclosed /* after end was scanned to EOF. L1: an AST without keyword prints #delimit. L4: cd using/data is a path.

Not fixed

Fixtures (probe.ts, origin/main → this branch)

fixture main now
braced 1 statement, AST joins all lines 3 statements, =
datarow, data, inputrows/main program node; New-York → New -York input data, no errors, =
input-no-end AST drops 10 data 10, then local/program, =
programsep input data swallows the program data 1, program node
programsw, semi_prog, program_slash, program_macro Missing "end" no errors, display at top level
pref Unclosed mata; AST adds a space each pass notes, local, display; =
wexpr weight: null weight `weight'
macspace in: null in: 1/2, left as written
char options [] replace
notedinput AST drops data and end =
semimat AST // comment + 2; Mata keeps + 2, display kept
single tab → 3 spaces =
ppg program de p'` =
quoted2, elseinput Unexpected "end" data attached
rec empty record dropped =
semifilename using clause path using/data

Corpus (3,780 base ados)

$ bun cdiff.ts chk-final.json chk-r2lp.json 40
chk-final.json 30328 chk-r2lp.json 30322
added {}
removed { "SYNTAX_ERROR/1": 2, "UNBALANCED_QUOTES/1": 4 }

All 6 removals were false positives:

  • the multi-line #delimit ; strings in ml_searc.ado and qchi_7.ado;
  • program ... in _lstretch_get_width.ado help text, which is no longer read as a header.
$ SIGHT_TEST_LOG=0 bun fmt.ts <worktree> r2lp
r2lp source-preserving loss 0 nonidem 0 err 0 slow 0
r2lp ast loss 2366 nonidem 16 err 0 slow 0      (main: loss 2366 nonidem 17 err 0)

The corpus has no real input … end blocks; 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.ts has 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.
  • M5 is a read-count test: 4× the blocks gives about 4× the source reads (main: ~16×). Bench at 4,000 blocks: 4.2 ms (main: 171 ms).
  • Gates, each run on its own, all pass:
    • check:line-endings
    • typecheck
    • bun test ./tests: 10212 pass, 0 fail (after rebasing on 21b95b3)
    • lint: 0 problems
    • test:ci: 10224 + 2 pass

Existing tests changed to the new rules: input-data.test.ts used invalid rows for numeric variables, and statement-span.test.ts, parser-release-audit-regressions.test.ts and the grammar pattern-order tests (now cr-content) encoded the old behavior.

Created with Claude Opus 5.5 in Claude Code (T3 Code).

@coderabbitai

coderabbitai Bot commented Nov 19, 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 33 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 87 included PR review attempts over the past 7 days set your current allowance at 1 review 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: 5ec5c366-109a-478f-a8cf-3365d007535f
Commits

Reviewing files that changed from the base of the PR and between 93d8ec8 and 95a0cc1.

Files selected for processing: 16

  • client/syntaxes/stata.tmLanguage.json
  • docs/cli.md
  • src/lexer/index.ts
  • src/parser/index.ts
  • src/pretty-printer/index.ts
  • src/providers/formatter.ts
  • src/types/index.ts
  • src/utils/input-data.ts
  • src/utils/program-definition.ts
  • src/utils/statement-span.ts
  • tests/unit/input-data.test.ts
  • tests/unit/lexer-parser-review-round2.test.ts
  • tests/unit/parser-release-audit-regressions.test.ts
  • tests/unit/statement-span.test.ts
  • tests/unit/textmate-grammar-star-comments.test.ts
  • tests/unit/textmate-grammar.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

…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
jbearak force-pushed the t3/fix-lexer-parser-printer-regressions branch from 719bde2 to 223e872 Compare November 19, 2026 16:06
@jbearak
jbearak merged commit 9f94055 into main Nov 19, 2026
16 checks passed
@jbearak
jbearak deleted the t3/fix-lexer-parser-printer-regressions branch November 19, 2026 16:17
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