Skip to content

Read local foox' 1 as defining foo + x's value, not foo, and stop the AST formatter from splitting it to local foo x' 1 - #384

Merged
jbearak merged 2 commits into
mainfrom
macro-built-names
Nov 5, 2026
Merged

jbearak merged 2 commits into
mainfrom
macro-built-names

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026

Copy link
Copy Markdown
Owner

local foo`x' 1 defines foo3 when x is 3, not foo. Sight's parser split the name at the macro, producing a macro_def named foo with value `x' 1. As a result, Sight defined foo, and a later `foo' got no warning. The same split made gen v`i' = 1 define a variable v, scalar sc`x' = 1 a scalar sc, and sight parse --json list args a`x' b as three names.

Formatter fix. The AST formatter printed local foo`x' 1 as local foo `x' 1, which does define foo with value 3 1, so formatting changed what the program did. It now prints the name unchanged in both modes.

What changed

The parser reads a local/global name joined across touching macro references as one name. It uses the rule #382 added in name-words.ts, which moves to src/utils/ so the parser and the analyzer share it. A quote ends the name. Once a name has a macro in it, touching letters and digits continue it (foo`x'1bar; the lexer splits 1bar). The macro_def keeps the whole name (foo`x') and gets name_parts. The analyzer and go-to-definition treat such a name as defining nothing, and a later `foo' is still reported. gen, egen, input, rename, confirm variable, scalar and matrix also read their target names as words.

The parser also accepts a macro as an extended function's name: local s : `ch'(scalars) runs e(scalars) when ch is e, and local h : `what'names m runs rownames. On main, this raised a false "Expected function name after colon" error and broke the rest of the program. Joined names exposed it on 3 more lines, so this PR fixes it. It clears the 425 false warnings in discrim_estat_common.ado.

Names that start with a macro (local `x' 9, local `a'`b' 1) stay command nodes, as before.

sight parse --json

schema_version stays 1. The do2r maintainers agreed this is a bug fix, since name still means the name as written.

Source Before After
local foo`x' 1 name: "foo", value: "x' 1"` name: "foox'", value: "1", name_parts: [text "foo", macro_ref `x']`
local foo 1 name: "foo" name: "foo", name_parts: null
args a`x' b (program args) a, `x', b {name: "ax'", name_parts: [...]}, {name: "b", name_parts: null}`
gen v`i' = 1 unchanged; varlist already had v`i' with parts unchanged
local s : `ch'(scalars) parse error extended: {function: "ch'", args: "(scalars)"}`

name_parts uses the same text and macro_ref objects as a varlist's parts and a macro's value_parts. A non-null name_parts means the name can't be resolved statically. docs/cli.md describes it. The golden fixture has local, global, : fcn, = exp, ++, #delimit ;, args and gen cases.

Corpus: sight check over Stata 18's 3,780 base ados

main (1d10867) this PR
Total 40,724 40,221
UNDEFINED_MACRO 24,100 23,733
OUT_OF_SCOPE_SYMBOL 6,900 6,788
SYNTAX_ERROR 68 51
Wall time (interleaved, 3 runs) 14.1–15.3s 14.7–15.0s
  • Removed (518). These are almost all from the extended-function fix: discrim_estat_common, storedresults, mkassert and ds_util no longer lose the rest of a program to a false parse error.
  • Added (15). Each one checked:
    • 9 are at locations that already had a warning and now have a different one, because a false definition is gone (local trace`nth'opts, local any`i', local opts`i', and align in mkassert).
    • discrim_estat_common:2474: `subcmd' really is undefined in ChkIfEVars. Main never parsed that far.
    • mfp_10:395: h is first set at line 416, later in the loop. Main hid this with the false local h`i' at line 159.
    • dvech:90 and mgarch_dvech:83 come from a separate gap (below). local constant`i' `constant' used to define constant itself.

Found, not fixed

  • syntax [, noCONStant] defines `constant' in Stata, but Sight registers nothing for it, and 525 base-ado syntax lines use a no option.
  • UNDEFINED_VARIABLE is never turned on outside tests: undefinedVariable in sight.toml doesn't reach the analyzer. Item 3's variable fixes show in completion, hover, references and the outline, not in diagnostics.
  • extract_confirm_variable lowercases variable/var before comparing, against the exact-case rule.

Proof: StataMP 18 on voyager defines foo3, foo3bar, foo7 (${g} and $g), gg3, q33 (quote ends the name), a3/b for args, v2/e2/sc3/w2 and no v/sc, foo31bar and t31b, and accepts : `ch'(scalars). 31 of the 40 new tests fail on main (parser, analyzer, providers, and the AST formatter mode); the 9 that pass are guards and source-preserving formatter cases. The golden fixture's expected output differs on main. All four gates pass. The corpus result is above.

Reviewed with sol-loop: 2 rounds with gpt-6.1-sol. Sol verdict: approved

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

…er from splitting it

StataMP 18 defines foo3 for `local foo`x' 1` when x is 3. The parser
split the name at the macro, so it emitted a macro_def named foo with
value "`x' 1": Sight defined foo, and the AST formatter printed
`local foo `x' 1`, which does define foo.

The parser now reads a local/global name, and an extended function
name, joined across touching macro references with the rule
name-words.ts uses (moved to src/utils so the parser shares it). The
macro_def keeps the whole name and gets name_parts; the analyzer and
go-to-definition treat it as defining nothing. gen, egen, input,
rename, confirm variable, scalar and matrix read their target names
as words too, so `gen v`i' = 1` defines no variable v.

sight parse --json: a macro's name is the name as written, with a new
name_parts (null for a plain name); program args are words with
name_parts. schema_version stays 1, agreed with do2r.

Also accept a macro as an extended function's name
(`local x : `ch'(scalars)`), which raised a false syntax error and
broke the rest of the program.
The lexer splits 1bar into a number and a word, and neither is a
macro, so local foo`x'1bar ended the name at foo`x'1, and
tempvar t`x'1b defined b. Once a word has a macro reference, any
touching name text continues it (StataMP 18: foo31bar, t31b).
@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 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available. Your 60 included PR review attempts over the past 7 days set your current allowance at 2 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: dce3445e-f7d3-4d37-bebe-394bf5401743
Commits

Reviewing files that changed from the base of the PR and between 1d10867 and f84e560.

Files selected for processing: 15

  • docs/cli.md
  • docs/configuration.md
  • src/analyzer/c-local-arg-positions.ts
  • src/analyzer/index.ts
  • src/cli/parse-json.ts
  • src/expression-parser/index.ts
  • src/parser/index.ts
  • src/providers/definition.ts
  • src/types/index.ts
  • src/utils/name-words.ts
  • tests/fixtures/cli-json/expected/graph.json
  • tests/fixtures/cli-json/expected/parse.json
  • tests/fixtures/cli-json/project/macro-built-names.do
  • tests/unit/cli/json-golden.test.ts
  • tests/unit/macro-built-names.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@jbearak
jbearak merged commit 53c3d76 into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the macro-built-names branch November 5, 2026 15:48
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