Canon/Canon.Core/SyntaxNodes/ExpressionList.cs
ichirinko 03852ed2bf feat:完成语法树结点的类型检查 (#59)
Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Reviewed-on: PostGuard/Canon#59
Co-authored-by: ichirinko <1621543655@qq.com>
Co-committed-by: ichirinko <1621543655@qq.com>
2024-04-28 22:02:29 +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;
}
}