Skip to content

Add WriteTo method to DelimitedSyntaxList<T>, IsPresent property, and WriteDelimitedList overloads to SyntaxWriter#15

Merged
jonathanvdc merged 4 commits intomainfrom
copilot/add-writeto-method-delimitedsyntaxlist
Mar 29, 2026
Merged

Add WriteTo method to DelimitedSyntaxList<T>, IsPresent property, and WriteDelimitedList overloads to SyntaxWriter#15
jonathanvdc merged 4 commits intomainfrom
copilot/add-writeto-method-delimitedsyntaxlist

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 29, 2026

The pattern of manually iterating a DelimitedSyntaxList — null-checking OpenToken, looping with separator interleaving, writing CloseToken — was duplicated across the runtime and in code emitted by the generator.

Changes

  • DelimitedSyntaxList<T> — New WriteTo(SyntaxWriter, string openLeadingTrivia, Action<T, SyntaxWriter, string> writeElement) method. No-ops when the list is not present; otherwise writes open/elements/separators/close with correct spacing. New IsPresent property (=> OpenToken.HasValue) that indicates whether the list has delimiters in the source, replacing direct OpenToken == null checks throughout.

  • SyntaxWriter — New WriteDelimitedList overloads for each concrete list type used in the codebase: DelimitedSyntaxList<SyntaxToken>, DelimitedSyntaxList<NamedAttributeSyntax>, and DelimitedSyntaxList<BlockArgumentSyntax>. Each overload delegates to DelimitedSyntaxList<T>.WriteTo with the appropriate element-writing logic, so callers never need to supply a lambda.

  • GenericOperationBodySyntax — Replaced three manual iteration blocks (OperandList, SuccessorList, Attributes) with writer.WriteDelimitedList(...) calls.

  • BlockSyntax — Replaced manual Arguments iteration (and now-redundant OpenToken != null guard) with a writer.WriteDelimitedList(...) call.

  • DialectSourceEmitterGenerateDelimitedNamedAttributeWriteTo / GenerateDelimitedTokenWriteTo now emit a single writer.WriteDelimitedList(...) call instead of a multi-line if/for block.

Usage

// Before
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);
}

// After
writer.WriteDelimitedList(Arguments, string.Empty);

// Checking presence
if (Arguments.IsPresent) { ... }

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…teration patterns

Agent-Logs-Url: https://github.com/jonathanvdc/MLIR.NET/sessions/6bc7d9d3-59a3-4d9e-a98f-2ed26061d21b

Co-authored-by: jonathanvdc <9839946+jonathanvdc@users.noreply.github.com>
Copilot AI changed the title [WIP] Add WriteTo method to DelimitedSyntaxList to improve token iteration Add WriteTo method to DelimitedSyntaxList<T> to eliminate manual iteration boilerplate Mar 29, 2026
Copilot AI requested a review from jonathanvdc March 29, 2026 14:43
Copy link
Copy Markdown
Owner

@jonathanvdc jonathanvdc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Can you add overloaded methods to SyntaxWriter for every type of DelimitedSyntaxList that may need to be written? Then have callers invoke those overloads. The idea is to spare callers from having to directly invoke DelimitedSyntaxList.WriteTo with a lambda, creating cleaner code.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 29, 2026

Looks great. Can you add overloaded methods to SyntaxWriter for every type of DelimitedSyntaxList that may need to be written? Then have callers invoke those overloads. The idea is to spare callers fr...

Done in 03703ff. Added three WriteDelimitedList overloads to SyntaxWriter — one each for DelimitedSyntaxList<SyntaxToken>, DelimitedSyntaxList<NamedAttributeSyntax>, and DelimitedSyntaxList<BlockArgumentSyntax>. All callers in GenericOperationBodySyntax, BlockSyntax, and the generated code from DialectSourceEmitter now use these overloads directly, with no lambdas at the call site.

Copilot AI changed the title Add WriteTo method to DelimitedSyntaxList<T> to eliminate manual iteration boilerplate Add WriteTo method to DelimitedSyntaxList<T> and WriteDelimitedList overloads to SyntaxWriter Mar 29, 2026
Copilot AI requested a review from jonathanvdc March 29, 2026 14:59
@jonathanvdc jonathanvdc marked this pull request as ready for review March 29, 2026 15:17
Copy link
Copy Markdown
Owner

@jonathanvdc jonathanvdc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Also add a property to DelimitedSyntaxList that tells us whether the list exists or not (i.e., whether the opening token is null). Use that property instead of directly checking if the opening token is null. Give it an appropriate name.

… checks with it

Agent-Logs-Url: https://github.com/jonathanvdc/MLIR.NET/sessions/fed83386-d728-477b-ab0a-091f423d1e9d

Co-authored-by: jonathanvdc <9839946+jonathanvdc@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 29, 2026

Nice work. Also add a property to DelimitedSyntaxList that tells us whether the list exists or not (i.e., whether the opening token is null). Use that property instead of directly checking if the open...

Done in 298bdb8. Added IsPresent => OpenToken.HasValue to DelimitedSyntaxList<T>, updated WriteTo to check !IsPresent instead of OpenToken == null, and updated the test to assert IsPresent == false for an absent list.

Copilot AI changed the title Add WriteTo method to DelimitedSyntaxList<T> and WriteDelimitedList overloads to SyntaxWriter Add WriteTo method to DelimitedSyntaxList<T>, IsPresent property, and WriteDelimitedList overloads to SyntaxWriter Mar 29, 2026
Copilot AI requested a review from jonathanvdc March 29, 2026 15:21
Copy link
Copy Markdown
Owner

@jonathanvdc jonathanvdc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@jonathanvdc jonathanvdc merged commit eabb664 into main Mar 29, 2026
1 check passed
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.

Add WriteTo method to DelimitedSyntaxList to eliminate manual token iteration patterns

2 participants