jackfiled
5e3ea6303e
重构语法树的部分,使用单独的类来抽象不同的非终结符节点。 **同时**,将`Pascal`语法的定义从测试项目中移动到核心项目中,在项目中只维护一份对于`Pascal`语法的定义。 Reviewed-on: PostGuard/Canon#23
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using Canon.Core.Enums;
|
|
using Canon.Core.LexicalParser;
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
public class ConstDeclaration : NonTerminatedSyntaxNode
|
|
{
|
|
public override NonTerminatorType Type => NonTerminatorType.ConstDeclaration;
|
|
|
|
/// <summary>
|
|
/// 是否递归的声明下一个ConstDeclaration
|
|
/// </summary>
|
|
public bool IsRecursive { get; private init; }
|
|
|
|
/// <summary>
|
|
/// 获得声明的常量
|
|
/// </summary>
|
|
public (IdentifierSemanticToken, ConstValue) ConstValue => GetConstValue();
|
|
|
|
public static ConstDeclaration Create(List<SyntaxNodeBase> children)
|
|
{
|
|
bool isRecursive;
|
|
if (children.Count == 3)
|
|
{
|
|
isRecursive = false;
|
|
}
|
|
else if (children.Count == 5)
|
|
{
|
|
isRecursive = true;
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
return new ConstDeclaration { Children = children, IsRecursive = isRecursive };
|
|
}
|
|
|
|
private static IdentifierSemanticToken ConvertToIdentifierSemanticToken(SyntaxNodeBase node)
|
|
{
|
|
return (IdentifierSemanticToken)node.Convert<TerminatedSyntaxNode>().Token;
|
|
}
|
|
|
|
private (IdentifierSemanticToken, ConstValue) GetConstValue()
|
|
{
|
|
if (IsRecursive)
|
|
{
|
|
return (ConvertToIdentifierSemanticToken(Children[2]), Children[4].Convert<ConstValue>());
|
|
}
|
|
else
|
|
{
|
|
return (ConvertToIdentifierSemanticToken(Children[0]), Children[2].Convert<ConstValue>());
|
|
}
|
|
}
|
|
}
|