Repository navigation
Read #delimit cr after a Mata end, keep continued mata: lines as written under #delimit ;, end string input at a row with extra fields, and read Unicode program names - #430
Merged
Conversation
…delimit ; as written, and end input data at a row with more fields than variables
…ts own option, and a subscript in assert
…d end a string that nothing closes at its line
…macro names in strings like other names
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 29 minutes. View limit details
|
…sions CodeQL flagged the two first-word patterns and the mata/python tail pattern as polynomial on uncontrolled input. One bounded pass from the row's start to its end reads the same things.
This was referenced Nov 19, 2026
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 #428 found eight more lexer, parser and formatter bugs, all reproducible on main. The worst ones:
#delimit crright after a Mata block'sendwas ignored, so the rest of the file became one statement.//comment line inside a continuedmata:statement to column one, where Stata reads it as code.inputof string variables withoutendswallowed the code after it.This PR fixes them. I checked each language question in StataMP 18 before writing the rule.
Fixed
S2:
#delimitafter a Mataend. Under#delimit ;, a Mata block's bareendends the statement, so#delimit cr(or#d cr) on the next line is a directive. I checked every token that can end a statement. A Python block'send, a program'send,input'sendand a}each need their;, so Stata reads on past them. Sight already did this and still does. One lenient case: a baremataline under;(not an opener in Stata, which reads one statement to the next;) is still read as a block opener, so#delimit crafter itsendis now a directive.S1: formatting a continued
mata:statement. Under#delimit ;the source-preserving formatter now leaves the later lines of a one-linemata:statement as written. A//line inside the statement is a comment when indented and code at column 0, and this holds after a///join too. Under#delimit cr, after a///join, the line is a comment at either indent, and a///inside a string does not join ("mismatched quotes"). So the lines still move with their first line there, as since Move the lines continuing a one-line mata: statement with it once, not one level further on every format #400. Python one-liners already stayed as written, and*lines stay as written too.S3: string
inputwithoutend. A row that doesn't fit the declared variables now ends the data only when the block'sendis accounted for some other way. A row doesn't fit when it has more fields than variables, or text where a numeric variable needs a number (round 2's rule). At such a row the lexer looks ahead to the next row whose first word isend:end(this row included) opens a block that needs its ownend, thatendbelongs to the block. The blocks are: a program definition (checked by the shared program-header recognizer), amataorpythonblock, or anotherinput.endfollows at all, nothing closes the data.In either case the data ended before the row, which is code. Otherwise the
endcloses this data and the row is data, which is how Stata reads it.stringobsandinput-no-end.do: the data ends afterParis(or10), and thelocal, the program and the rest are code.input x/1 2/end,input str20 name/Mary-Ann Smith/Los-Angeles/end, andinput x/1/abc/2/end: 0 issues, and both formatter modes leave the text unchanged on two passes. On main the third block reported a falseUnexpected "end"under round 2's numeric rule; on this PR's first push the first two did too.inputcounts them: a run of non-blanks, a quoted value, or,or/on its own. Treating/as a field is new; round 2 read it as a blank.src/utils/input-data.tsanddocs/cli.mddescribe the rule.inputwithoutendthat declares enough string variables is read as data. And a row that doesn't fit is data whenever anendfollows that no block Sight recognizes claims, even if thatendcloses something opened behind a prefix (quietly program define).S4 and S14: quoted
inputfields. Under#delimit ;a quoted field may span lines, and the record ends at the;after its closing quote. A;inside a compound quote still ends the record, as in Stata. A quote that nothing closes ends at the end of its line. The scanner reads each character once, plus at most one scan to the end of the file per kind of quote.S6: Unicode program names. Stata names may start with any Unicode letter or
_and continue with letters, digits, numbers, combining marks or_.src/utils/stata-name.tsholds that definition. The lexer's word scanner and the program-header recognizer now both use it. Before,caféwas lexed ascaf+é.S12:
plugin. It declares a plugin only as an option of its own after the comma, not inside another option's parentheses.S9 (AST mode): subscripts in
assert.asserttakes an expression, sox[`weight']stays a subscript. In a varlist, a bracket is still a weight.displayalready read its arguments as expressions: AST mode keepsdisplay x[`weight']as written.S15: strings under
#delimit ;. Thecompoundfixture is not valid Stata. A;inside a compound quote ends the statement under#delimit ;, and Stata says "invalid syntax". Sight now matches that: a compound string spans lines (which used to be an error) and stops at a;. A simple string still protects;.badquote: a simple string that no later"closes ends at the end of its line, or at a;that is the last code on that line. The error is still reported. Stata reads such a string to the end of the file. Sight's choice changes only files that are already broken, and it keeps the later statements. In such a file the formatter may reindent lines that Stata would read as part of the open string.#delimit ;line by line, where;in a string is text.Not fixed (outside this PR's files)
src/providers/symbols.ts:678matches program names with\b…\b, which fails for names that start with a Unicode letter.src/console/automation/mode-tracker.ts:30and the TextMate grammar still use ASCII-only names.Stata 18 log lines
Fixture probes (main → this branch)
result' is not defined; one command#delimit cr…`//to column 0; check warnsslashes_mid_statement// add the correctionkept; 0 issuesn' is not defined; input runs throughend`Paris; local, program, display; 0 issuesinput x/1/abc/2/endUnexpected "end"end; 0 issuesUnexpected "end""Long\nlabel" 42; 0 issuesUnexpected "end"assert x == 1 [weight']`assert x[weight'] == 1`Corpus (3,780 base ados)
On the first push, a file-by-file comparison with main found 0 differences in parse shape, source-preserving output and AST output. After the refinement, the corpus check and formatter run above give the same numbers.
Proof
tests/unit/lexer-parser-review-round3.test.tshas 38 tests, with a second formatting pass in both formatter modes. On main (with imports missing on main stubbed), 20 fail and 18 pass, so each fixed item has at least one test that fails there. The 18 that pass are guards. On this PR's first push, the refinedinputtests "is data when nothing else claims the end" and its AST-mode formatting test fail, as does round 2's restoredquietly input x/1 2/endtest. The scaling tests count source reads,indexOfincluded: 4× the input gives under 5× the reads.Send to Stata:
bun test tests/unit/send-to-stata tests/property/send-statement-step-through.prop.test.tsgives 252 pass.Each gate run on its own, all pass:
check:line-endingstypecheckbun test ./tests: 10291 pass, 0 faillinttest:ci: 10303 + 2 passOn the first push, one
test:cirun failedmacro-completion-context.prop.test.ts"cursor inside block comment" with the counterexample["*/","_",7]. The generator can put*/` inside the comment, and main returns the same result for that input. The rerun passed.