Canon/Canon.Core/SyntaxNodes/IdentifierVarPart.cs
jackfiled 5e3ea6303e refact: syntax-node (#23)
重构语法树的部分,使用单独的类来抽象不同的非终结符节点。
**同时**,将`Pascal`语法的定义从测试项目中移动到核心项目中,在项目中只维护一份对于`Pascal`语法的定义。

Reviewed-on: PostGuard/Canon#23
2024-04-07 16:47:28 +08:00

52 lines
1.1 KiB
C#

using Canon.Core.Enums;
namespace Canon.Core.SyntaxNodes;
public class IdentifierVarPart : NonTerminatedSyntaxNode
{
public override NonTerminatorType Type => NonTerminatorType.IdVarPart;
/// <summary>
/// 是否声明了索引部分
/// </summary>
public bool Exist { get; private init; }
/// <summary>
/// 索引中的位置声明
/// </summary>
public IEnumerable<Expression> Positions => GetPositions();
private IEnumerable<Expression> GetPositions()
{
if (!Exist)
{
yield break;
}
foreach (Expression expression in Children[1].Convert<ExpressionList>().Expressions)
{
yield return expression;
}
}
public static IdentifierVarPart Create(List<SyntaxNodeBase> children)
{
bool exist;
if (children.Count == 0)
{
exist = false;
}
else if (children.Count == 3)
{
exist = true;
}
else
{
throw new InvalidOperationException();
}
return new IdentifierVarPart { Children = children, Exist = exist };
}
}