jackfiled
5e3ea6303e
重构语法树的部分,使用单独的类来抽象不同的非终结符节点。 **同时**,将`Pascal`语法的定义从测试项目中移动到核心项目中,在项目中只维护一份对于`Pascal`语法的定义。 Reviewed-on: PostGuard/Canon#23
46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using Canon.Core.Enums;
|
|
using Canon.Core.LexicalParser;
|
|
|
|
namespace Canon.Core.SyntaxNodes;
|
|
|
|
public class SubprogramHead : NonTerminatedSyntaxNode
|
|
{
|
|
public override NonTerminatorType Type => NonTerminatorType.SubprogramHead;
|
|
|
|
public bool IsProcedure { get; private init; }
|
|
|
|
/// <summary>
|
|
/// 子程序的名称
|
|
/// </summary>
|
|
public IdentifierSemanticToken SubprogramName =>
|
|
(IdentifierSemanticToken)Children[1].Convert<TerminatedSyntaxNode>().Token;
|
|
|
|
/// <summary>
|
|
/// 子程序的参数
|
|
/// </summary>
|
|
public IEnumerable<Parameter> Parameters => Children[2].Convert<FormalParameter>().Parameters;
|
|
|
|
public static SubprogramHead Create(List<SyntaxNodeBase> children)
|
|
{
|
|
bool isProcedure;
|
|
|
|
TerminatedSyntaxNode node = children[0].Convert<TerminatedSyntaxNode>();
|
|
KeywordSemanticToken token = (KeywordSemanticToken)node.Token;
|
|
|
|
if (token.KeywordType == KeywordType.Procedure)
|
|
{
|
|
isProcedure = true;
|
|
}
|
|
else if (token.KeywordType == KeywordType.Function)
|
|
{
|
|
isProcedure = false;
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
return new SubprogramHead { Children = children, IsProcedure = isProcedure };
|
|
}
|
|
}
|