Skip to content

fix(tokenizer): & in executable code is concatenation, not a reference - #870

Merged
msarson merged 2 commits into
msarson:version-1.0.11from
geircodes:fix/ampersand-concat-in-code
Nov 25, 2026
Merged

msarson merged 2 commits into
msarson:version-1.0.11from
geircodes:fix/ampersand-concat-in-code

Conversation

@geircodes

Copy link
Copy Markdown
Contributor

What happened

Find All References on a local misses every use written after a concatenation & with a space:

Worker       PROCEDURE()
Num            LONG
  CODE
  Num += 1
  MESSAGE('Rows: ' & Num)            ! missed
  MESSAGE('Rows: ' &Num)             ! found
  MESSAGE('Rows: ' & Num & ' rows')  ! missed

And MESSAGE('Rows: ' & Tally.Rows) loses the field access: References on the GROUP label Tally
does not list that line.

Root cause

TokenPatterns.ts:

[TokenType.ReferenceVariable]: /&\s*[A-Za-z_][A-Za-z0-9_]*/i,

PatternMatcher tries this pattern at every &, in data and code alike. In code the operator, the
space and the next name become one token:

source (after CODE) tokens
MESSAGE('Rows: ' & Num) & Num ReferenceVariable
MESSAGE('Rows: ' &Num) &Num ReferenceVariable
MESSAGE('Rows: ' & Tally.Rows) & Tally ReferenceVariable + Rows Variable

ReferencesProvider matches a ReferenceVariable token against '&' + word, so only the no-space form
was found, and & Tally.Rows never produced the Tally.Rows StructureField.

Dropping the \s* is not an option: a space after & is legal in a declaration. Clarion 11.1
compiles and links Ref & LONG, Ref & LONG, Ref & SomeGroupType and Ref & SomeProcType,
each accepting &= and being dereferenced.

The fix

ClarionTokenizer.ts, with the other per-pattern context guards:

// In executable code `&` is string concatenation (or `&=`), never a reference
// declaration: `'Rows: ' & Num` became one `& Num` token, which hid the use of
// Num and split `& Grp.Field`. Skipped here, the `&` is stepped over as before a
// string literal and the name tokenizes normally. Declarations are not in a CODE
// section, and a procedure header line resets it, so `&LONG pX` stays a reference.
if (tokenType === TokenType.ReferenceVariable && inCodeSection) continue;

inCodeSection is the state the tokenizer already keeps (codeSectionSetBy: CODE sets it, DATA,
ROUTINE and PROCEDURE/FUNCTION header lines clear it). An & in code now gets no token, exactly as
an & before a string literal already does today ('a' & 'b'), and the name after it tokenizes as
it would anywhere else in code (Variable, StructureField Tally.Rows, StructurePrefix PRE:Field).
&= and & | are unaffected: the pattern never matched them.

Every consumer of TokenType.ReferenceVariable reads it as a declaration (Label &Type, column 0,
outside code), with one exception: Document highlight matched &Grp in 'x' &Grp.Field through its
ReferenceVariable branch. That token is now a StructureField Grp.Field, which the provider skipped,
so those lines lost their highlight. The second commit adds a StructureField branch to
DocumentHighlightProvider.ts:

} else if (t.type === TokenType.StructureField) {
    // `Grp.Field` — highlight the receiver when it is the word (`Tally` in
    // `'x' & Tally.Rows`). Only the receiver: a bare field name would also match
    // `Other.Field` of an unrelated GROUP, which needs resolution, not text.
    const dot = t.value.indexOf('.');
    if (dot <= 0 || t.value.slice(0, dot).toUpperCase() !== wordUpper) continue;
    isDeclaration = false;
}

This also highlights Grp.Field without &, which was never highlighted before.

Testing

New ClarionTokenizer.AmpersandConcatInCode.test.ts, 17 tests:

  • tokens: & Num, &Num, & Num & ' rows', & Tally.Rows, & in a ROUTINE with and without a DATA
    section, and in a procedure whose header takes &LONG pX;
  • controls that must not move: &LONG / & LONG in a MAP prototype, global, procedure-local and
    ROUTINE DATA declarations, a procedure header parameter, and & | continuation;
  • Find All References on Num (every line above) and on the GROUP label Tally;
  • Document highlight on a GROUP label as the receiver of Tally.Rows (with and without &, any
    case), a control that another GROUP's Other.Rows is not highlighted for Tally, and a plain
    local after & .

Without the tokenizer change 9 fail (all the code-section cases and both References tests) and the
8 controls pass. Without the highlight change its 2 receiver tests fail. With both, 20 pass. Full
server suite: 4639 passing, 0 failing, 3 pending.

Sweeps over a 259-file test corpus, before and after:

  • ReferenceVariable tokens: 15,830 -> 1,261. Every one removed was in a CODE section (14,468 of
    them with a space after &, 101 without); no token appears after that was not there before.
  • document-symbols: 1 entry changed, on a line the outline already reads as data because its
    procedure's CODE is in column 0 (a separate issue): the misread entry's name changes shape.
  • hover-definition-agreement: 0 changed. Its sample holds no position after an &, so this only
    says nothing else moved.
  • cards at 80 positions drawn evenly from the removed tokens (the name after &): hover and F12
    unchanged at all 80. They resolve the word at the cursor without this token.
  • references-snapshot at 12 of those positions: 5 result lists changed, 7 empty before and after.
    Distinct result lines 29,258 -> 30,690. Four positions only gained (+420, +21, +6, +2). The fifth,
    the built-in ERRORCODE, gained 785 and lost 65, and all 65 are a class member called ErrorCode
    after & ('Error: ' & Obj.ErrorCode). Before, & Obj swallowed the receiver and left a bare
    ErrorCode that matched the built-in; now it is a member access, as x = Obj.ErrorCode already was.

Scope

Left out on purpose:

  • No Operator token for & in code. Nothing emits one today ('a' & 'b' has none either), and
    adding it would change what every token-adjacency check sees.
  • The outline misreading code as data when CODE is in column 0 (seen in the document-symbols sweep)
    is unrelated to & and not touched here.
  • Document highlight on a field name (Rows in Tally.Rows) still returns nothing, as before: by
    text alone it cannot tell Tally.Rows from Other.Rows.

geircodes and others added 2 commits November 24, 2026 09:47
The ReferenceVariable pattern /&\s*Name/ was tried at every `&`, so in
code `'Rows: ' & Num` became one `& Num` token. Find All References
matched that token against '&' + word and missed every `& Num` use, and
`& Grp.Field` lost its field access. A space after `&` is legal in a
declaration, so the pattern keeps its \s* and is skipped in a CODE
section only, where `&` now tokenizes like it already did before a
string literal.

New ClarionTokenizer.AmpersandConcatInCode.test.ts: 17 tests, 9 fail
without the fix. Full server suite 4636 passing, 0 failing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ield

Document highlight matched `&Grp` in `'x' &Grp.Field` through the
ReferenceVariable branch. With `&` now tokenized as concatenation in
code, that is a StructureField `Grp.Field`, which the provider skipped,
so those lines lost their highlight. A StructureField branch now
highlights the receiver when it is the word, ignoring case, which also
covers `Grp.Field` without `&`, never highlighted before. The field name
alone is not matched: it would also hit `Other.Field`.

3 new tests, 2 fail without the change. Full suite 4639 passing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@msarson
msarson merged commit d5da138 into msarson:version-1.0.11 Nov 25, 2026
1 check passed
@geircodes
geircodes deleted the fix/ampersand-concat-in-code branch November 25, 2026 13:46
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.

2 participants