using CodePlay.Core.Interfaces;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
namespace CodePlay.Core.Parsers;
///
/// 解析器基类
///
public abstract class BaseParser : IParser
{
///
/// 支持的语言类型
///
public abstract LanguageType SupportedLanguage { get; }
///
/// 解析源代码并生成 AST
///
public abstract Task ParseAsync(string sourceCode, CancellationToken cancellationToken = default);
///
/// 创建语法树
///
protected SyntaxTree CreateSyntaxTree()
{
return new SyntaxTree
{
Language = SupportedLanguage,
Root = new SyntaxNode
{
Type = SyntaxNodeType.CompilationUnit
}
};
}
///
/// 添加注释到语法树
///
protected void AddComment(SyntaxTree tree, string text, CommentType type, int lineNumber)
{
tree.Comments.Add(new SyntaxComment { Text = text, Type = type, LineNumber = lineNumber });
}
///
/// 记录解析日志
///
protected TransformationLogEntry CreateLog(string operation, string details, string level = "Info")
{
return new TransformationLogEntry { Timestamp = DateTime.UtcNow, Operation = operation, Details = details, Level = level };
}
}