Files
ZuneUIXTools/libs/UIX.DecompXml/CodeBlockInfo.cs
T

76 lines
2.4 KiB
C#
Raw Normal View History

2025-09-24 21:52:20 -05:00
using Microsoft.CodeAnalysis.CSharp.Syntax;
2025-09-25 15:52:13 -05:00
using System;
2025-07-18 14:04:06 -05:00
using System.Collections.Generic;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Iris.DecompXml;
2025-09-24 10:34:28 -05:00
internal record CodeBlockInfo
2025-07-18 14:04:06 -05:00
{
2025-09-24 21:52:20 -05:00
public CodeBlockInfo(uint startOffset, uint endOffset, ICodeBlockAdditionalInfo additionalInfo = null)
2025-07-18 14:04:06 -05:00
{
StartOffset = startOffset;
EndOffset = endOffset;
2025-09-24 21:52:20 -05:00
AdditionalInfo = additionalInfo;
2025-07-18 14:04:06 -05:00
}
public uint StartOffset { get; init; }
public uint EndOffset { get; init; }
2025-09-24 21:52:20 -05:00
public ICodeBlockAdditionalInfo AdditionalInfo { get; init; }
2025-07-18 14:04:06 -05:00
public List<StatementSyntax> Statements { get; } = [];
public void FinalizeBlock(CodeBlockInfo parentBlock)
{
var blockBody = Block(Statements);
2025-09-24 21:52:20 -05:00
switch (AdditionalInfo)
2025-07-18 14:04:06 -05:00
{
2025-09-24 21:52:20 -05:00
case IfBlockInfo ifBlockInfo:
var ifStatement = IfStatement(ifBlockInfo.Condition, blockBody);
2025-07-18 14:04:06 -05:00
parentBlock.Statements.Add(ifStatement);
break;
2025-09-24 21:52:20 -05:00
case ElseBlockInfo _:
2025-07-18 14:04:06 -05:00
var elseClause = ElseClause(blockBody);
2025-09-25 15:52:13 -05:00
var parentLastStatement = parentBlock.Statements[^1];
if (parentLastStatement is not IfStatementSyntax ifElseBlock)
throw new InvalidOperationException($"Else block must be preceded by an if block, got '{parentLastStatement.Kind()}'");
2025-07-18 14:04:06 -05:00
parentBlock.Statements[^1] = ifElseBlock.WithElse(elseClause);
break;
2025-09-24 21:52:20 -05:00
case ForEachBlockInfo forEachBlockInfo:
var foreachStatement = ForEachStatement(forEachBlockInfo.Type, forEachBlockInfo.Identifier,
forEachBlockInfo.Source, blockBody);
parentBlock.Statements.Add(foreachStatement);
break;
2025-07-18 14:04:06 -05:00
default:
2025-09-24 21:52:20 -05:00
throw new System.NotImplementedException($"Unrecognized code block kind '{AdditionalInfo.GetType().Name}'");
2025-07-18 14:04:06 -05:00
}
}
}
2025-09-24 21:52:20 -05:00
internal interface ICodeBlockAdditionalInfo;
internal class IfBlockInfo(ExpressionSyntax condition = null) : ICodeBlockAdditionalInfo
{
public ExpressionSyntax Condition { get; set; } = condition;
}
internal class ElseBlockInfo : ICodeBlockAdditionalInfo;
internal class ForEachBlockInfo : ICodeBlockAdditionalInfo
{
public ExpressionSyntax Source { get; set; }
public string Identifier { get; set; }
public TypeSyntax Type { get; set; }
}