feat: 完成 CodePlay 项目初始化和核心解析器实现
- 创建 CodePlay 解决方案和 4 个项目 (Core, Web, CLI, Tests) - 实现核心数据模型 (ConversionRequest, ConversionResult, Project 等) - 实现核心接口 (IParser, IConverter, ICodeGenerator 等) - 实现基础抽象类 BaseParser - 实现 CSharpParser 解析器 (基于 Roslyn) - 添加 Microsoft.CodeAnalysis.CSharp NuGet 包 - 所有代码编译通过 Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using CodePlay.Core.Interfaces;
|
||||
using CodePlay.Core.Models;
|
||||
using CodePlay.Core.Common;
|
||||
|
||||
namespace CodePlay.Core.Parsers;
|
||||
|
||||
/// <summary>
|
||||
/// 解析器基类
|
||||
/// </summary>
|
||||
public abstract class BaseParser : IParser
|
||||
{
|
||||
/// <summary>
|
||||
/// 支持的语言类型
|
||||
/// </summary>
|
||||
public abstract LanguageType SupportedLanguage { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 解析源代码并生成 AST
|
||||
/// </summary>
|
||||
public abstract Task<SyntaxTree> ParseAsync(string sourceCode, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 创建语法树
|
||||
/// </summary>
|
||||
protected SyntaxTree CreateSyntaxTree()
|
||||
{
|
||||
return new SyntaxTree
|
||||
{
|
||||
Language = SupportedLanguage,
|
||||
Root = new SyntaxNode
|
||||
{
|
||||
Type = SyntaxNodeType.CompilationUnit
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加注释到语法树
|
||||
/// </summary>
|
||||
protected void AddComment(SyntaxTree tree, string text, CommentType type, int lineNumber)
|
||||
{
|
||||
tree.Comments.Add(new SyntaxComment
|
||||
{
|
||||
Text = text,
|
||||
Type = type,
|
||||
LineNumber = lineNumber
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录解析日志
|
||||
/// </summary>
|
||||
protected TransformationLog CreateLog(string operation, string details, LogLevel level = LogLevel.Info)
|
||||
{
|
||||
return new TransformationLog
|
||||
{
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Operation = operation,
|
||||
Details = details,
|
||||
Level = level
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user