Skip to content

fix(tokenizer): A statement starting with a variable called Data no longer ends the CODE section - #861

Merged
msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/data-variable-not-section-header
Nov 17, 2026
Merged

msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/data-variable-not-section-header

Conversation

@geircodes

Copy link
Copy Markdown
Contributor

A statement starting with a variable called Data no longer ends the CODE section

What happened

A procedure that assigns a variable called Data at the start of a statement got false 'BREAK' used outside of a LOOP or ACCEPT structure. warnings (and the same for CYCLE) on every BREAK/CYCLE in any LOOP after that line:

Parser.NextItem PROCEDURE(LONG WantType, *STRING data)
i      LONG,AUTO
RetVal LONG(FALSE)
  CODE
  data = ''
  LOOP i = SELF.Cur+1 TO RECORDS(SELF.Q)
    GET(SELF.Q, i)
    IF SELF.Q.Kind = 1
      BREAK            ! warned: 'BREAK' used outside of a LOOP or ACCEPT structure.
    END
  END
  RETURN(RetVal)

The same happens after Data = 'x', Data.Field = 'x', Data[1] = 'x' and data &= NULL, and with a local declared Data STRING(520). In the real class this came from, two methods gave 7 warnings, and across a 153-file solution 10 warnings in 3 files, none of them real.

The warnings are the visible part. The token stream after that line is wrong: LOOP is tokenized as the variable OOP and IF as F, so folding, END matching and anything else that reads the structure tokens is off for the rest of the procedure.

Root cause

ClarionTokenizer.codeSectionSetBy decides when the tokenizer is inside a CODE section, and took any line starting with the word DATA for the routine's DATA header:

if (/^\s*(DATA|ROUTINE)\b/i.test(line)) return false; // DATA/ROUTINE sections can have structures

data = '' matches, so inCodeSection goes false until the next CODE or PROCEDURE line. Outside CODE the Structure pattern skips execution structures (if (!inCodeSection && isExecutionStructure) continue;), the Variable pattern excludes structure keywords, and the next match starts one character in: OOP, F. The LOOP never gets a finishesAt, so validateCycleBreakOutsideLoop finds no loop range around the BREAK.

This is the tokenizer side of #855: that fix made DocumentStructure accept CODE/DATA as the statement only when nothing but a comment follows it (isBareExecutionMarker), and #618 guarded the outline's own DATA check, whose commit notes the same stray leading F. Neither reaches codeSectionSetBy, which the tokenizer and TokenCache (codeSectionAt, isStructureAffectingEdit) read.

Fix

The DATA header counts only alone on its line, optionally followed by a comment, the same form as the CODE check just above it and as the routine-local scan (/^\s*data\s*(!.*)?$/i):

// DATA/ROUTINE sections can have structures. The DATA header stands alone on its line,
// like CODE: a statement starting with a variable called Data (`data = ''`) is code.
if (/^\s*data\s*(!.*)?$/i.test(line)) return false;
if (/^\s*ROUTINE\b/i.test(line)) return false;

Data is a legal label for a procedure-local variable and a parameter: #855 compiler-verified it on Clarion 10 and 12, and the class this was found in compiles on Clarion 11.1 with both a parameter named data and a local Data STRING(520).

Testing

New server/src/test/ControlFlowDiagnostics.DataVariableStatement.test.ts (14 tests), run through the production path (tokenizer + DocumentStructure.process()):

  • No BREAK/CYCLE warning after each of seven statement shapes: data = '', Data = 'x', DATA = 'x' ! comment, data=1, Data.Field = 'x', Data[1] = 'x', data &= NULL.
  • The Structure tokens and their finishesAt are identical to the same procedure with x = '' in place of data = '', and no keyword loses its first character.
  • A local declared Data STRING(520) and assigned in CODE.
  • Bidirectional: a BREAK really outside a LOOP after data = '' is still flagged, and only that one. Before the fix, the CYCLE inside the LOOP was flagged as well.
  • A ROUTINE's DATA section (bare, upper and lower case, with a trailing comment) still opens, its local is still a variable, and its LOOP still owns its BREAK.
  • codeSectionSetBy unit cases: the header forms return false (including a trailing \r), the statement forms return undefined.

Red before the fix: 12 of 14 failing (the two ROUTINE/header guards pass on both sides). 14/14 after. Server suite: 4565 passing, 3 pending.

Corpus check over a 153-file real-world solution, old build against new: the token stream changed in 5 files, every one of them containing a statement that starts with data, and in no other file. The BREAK/CYCLE warnings went from 10 to 0. All 15 real routine DATA headers in that corpus are in the strict form.

Scope

🤖 Generated with Claude Code

…s in CODE

codeSectionSetBy took any line beginning with the word DATA for the
routine DATA header, so `data = ''` in a procedure body left the CODE
section for the rest of the procedure. Every LOOP and IF after it lost
its structure token (LOOP read as `OOP`, IF as `F`), and each BREAK or
CYCLE in such a LOOP was warned as used outside a LOOP or ACCEPT.

The DATA header now counts only alone on its line, like CODE: the rule
msarson#855 applied in DocumentStructure, applied to the tokenizer's section
state. ROUTINE is unchanged; it is reserved and cannot name a variable.

Test: ControlFlowDiagnostics.DataVariableStatement, 12 of 14 failing
before the fix, 14/14 after. Server suite 4565 passing, 3 pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@msarson
msarson merged commit c89e995 into msarson:version-1.0.11 Nov 17, 2026
1 check passed
msarson added a commit that referenced this pull request Nov 17, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geircodes
geircodes deleted the fix/data-variable-not-section-header 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