Skip to content

Add WriteTo method to DelimitedSyntaxList to eliminate manual token iteration patterns #11

@jonathanvdc

Description

@jonathanvdc

We currently duplicate a low-level pattern for writing DelimitedSyntaxList instances across the codebase, both in the MLIR project and in code generated by MLIR.Generators. This pattern manually iterates over elements and separator tokens, handling spacing and delimiter tokens inline.

This leads to:

  • Boilerplate code repeated in multiple places
  • Increased risk of inconsistencies or subtle formatting bugs
  • Reduced readability and higher maintenance cost

Proposed Change

Introduce a WriteTo method directly on DelimitedSyntaxList that encapsulates this logic.

The method should:

  • Handle writing of OpenToken and CloseToken
  • Iterate over elements and interleave SeparatorTokens correctly
  • Apply appropriate spacing (e.g., no leading space for the first element, space before subsequent elements)
  • Delegate to each element’s own WriteTo method

Example of Current Pattern

The following pattern appears in multiple places and should be replaced:

if (Arguments.OpenToken != null)
{
    writer.WriteToken(Arguments.OpenToken.Value, string.Empty);
    for (var i = 0; i < Arguments.Count; i++)
    {
        if (i > 0)
        {
            writer.WriteToken(Arguments.SeparatorTokens[i - 1], string.Empty);
        }

        Arguments[i].WriteTo(writer, i > 0 ? " " : string.Empty);
    }

    writer.WriteToken(Arguments.CloseToken!.Value, string.Empty);
}

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions