using CanonSharp.Benchmark.Canon.Core.Abstractions;
using CanonSharp.Benchmark.Canon.Core.Enums;
namespace CanonSharp.Benchmark.Canon.Core.SyntaxNodes;
public class OnExpressionListEventArgs : EventArgs
{
public required ExpressionList ExpressionList { get; init; }
}
public class ExpressionList : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.ExpressionList;
///
/// 子表达式列表
///
public List Expressions { get; } = [];
///
/// 当前ExpressionList中的Expression定义
///
public required Expression Expression { get; init; }
public override void PreVisit(SyntaxNodeVisitor visitor)
{
visitor.PreVisit(this);
RaiseEvent();
}
public override void PostVisit(SyntaxNodeVisitor visitor)
{
visitor.PostVisit(this);
RaiseEvent();
}
///
/// 使用ExpressionList产生式的时间
///
public event EventHandler? OnExpressionList;
public static ExpressionList Create(List children)
{
ExpressionList result;
if (children.Count == 1)
{
result = new ExpressionList { Expression = children[0].Convert(), Children = children };
result.Expressions.Add(children[0].Convert());
}
else
{
result = new ExpressionList { Expression = children[2].Convert(), Children = children };
foreach (Expression expression in children[0].Convert().Expressions)
{
result.Expressions.Add(expression);
}
result.Expressions.Add(children[2].Convert());
}
return result;
}
private void RaiseEvent()
{
if (Children.Count == 3)
{
OnExpressionList?.Invoke(this,
new OnExpressionListEventArgs { ExpressionList = Children[0].Convert() });
}
OnExpressionList = null;
}
}