Canon/Canon.Core/SyntaxNodes/ExpressionList.cs
Lan_G 160fafef70 misc: 清除冗余的代码
Reviewed-on: PostGuard/Canon#75
Co-authored-by: Lan_G <2911328695@qq.com>
Co-committed-by: Lan_G <2911328695@qq.com>
2024-05-06 00:12:57 +08:00

76 lines
2.0 KiB
C#

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