93e5bcb575
P0 任务 (立即执行): ✅ C++ 解析器增强: 改进正则解析,支持模板/命名空间/多重继承 ✅ 输入验证和安全加固: InputValidator 服务,代码大小限制,恶意代码检测 ✅ 缓存机制:CachedConversionService,SHA256 缓存键,60 分钟过期 P1 任务 (短期): ⏸️ 代码格式化集成:deferred (需要外部依赖) ⏸️ Web 界面暗黑模式:deferred (前端任务) ⏸️ 差异对比功能:deferred (前端任务) ✅ 日志增强:RequestLoggingMiddleware 中间件 新增文件: - CodePlay.Core/Parsers/CppParser.cs (增强版) - CodePlay.Core/Services/InputValidator.cs - CodePlay.Core/Services/CachedConversionService.cs - CodePlay.WebAPI/Middleware/RequestLoggingMiddleware.cs - CodePlay.Core/CodePlay.Core.csproj (新增 ClangSharp, MemoryCache 包) 测试结果: 42 个测试 (41 通过,1 跳过) ✅ 延后任务原因: - 代码格式化:需要安装 dotnet-format, google-java-format, clang-format - Web 界面功能:属于前端开发任务,需要 Vue3 开发 - 这些任务可以后续通过前端专项完成 Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
162 lines
5.3 KiB
C#
162 lines
5.3 KiB
C#
using CodePlay.Core.Interfaces;
|
|
using CodePlay.Core.Common;
|
|
|
|
namespace CodePlay.Core.Parsers;
|
|
|
|
/// <summary>
|
|
/// C++ 解析器 (增强版)
|
|
/// 使用改进的正则表达式和相关性检测
|
|
/// </summary>
|
|
public class CppParser : BaseParser
|
|
{
|
|
public override LanguageType SupportedLanguage => LanguageType.CPlusPlus;
|
|
|
|
public override Task<SyntaxTree> ParseAsync(string sourceCode, CancellationToken cancellationToken = default)
|
|
{
|
|
var tree = CreateSyntaxTree();
|
|
tree.SourceCode = sourceCode;
|
|
tree.Root = ParseRoot(sourceCode);
|
|
tree.Comments = ExtractComments(sourceCode);
|
|
|
|
return Task.FromResult(tree);
|
|
}
|
|
|
|
private SyntaxNode ParseRoot(string sourceCode)
|
|
{
|
|
var root = new SyntaxNode { Type = SyntaxNodeType.CompilationUnit, Text = sourceCode };
|
|
|
|
// 提取类/结构体
|
|
ExtractClasses(sourceCode, root);
|
|
|
|
// 提取函数
|
|
ExtractFunctions(sourceCode, root);
|
|
|
|
// 提取命名空间
|
|
ExtractNamespaces(sourceCode, root);
|
|
|
|
// 提取模板
|
|
ExtractTemplates(sourceCode, root);
|
|
|
|
return root;
|
|
}
|
|
|
|
private void ExtractClasses(string code, SyntaxNode root)
|
|
{
|
|
var pattern = @"(public|private|protected)?\s*(class|struct)\s+(\w+)\s*(<[^>]+>)?\s*(:\s*(public|private|protected)?\s*[\w:<>,&\s]+)?\s*\{";
|
|
var matches = System.Text.RegularExpressions.Regex.Matches(code, pattern);
|
|
|
|
foreach (System.Text.RegularExpressions.Match match in matches)
|
|
{
|
|
var classNode = new SyntaxNode
|
|
{
|
|
Type = SyntaxNodeType.Class,
|
|
Text = match.Value,
|
|
Metadata = new Dictionary<string, object?>
|
|
{
|
|
["Name"] = match.Groups[3].Value,
|
|
["Kind"] = match.Groups[2].Value,
|
|
["Template"] = match.Groups[4].Success ? match.Groups[4].Value : null,
|
|
["BaseClass"] = match.Groups[5].Success ? match.Groups[5].Value.Trim() : null
|
|
}
|
|
};
|
|
root.Children.Add(classNode);
|
|
}
|
|
}
|
|
|
|
private void ExtractFunctions(string code, SyntaxNode root)
|
|
{
|
|
var pattern = @"([\w:*&<>,\s]+)\s+(\w+)\s*\(([^)]*)\)\s*(const)?\s*(override)?\s*(final)?\s*\{";
|
|
var matches = System.Text.RegularExpressions.Regex.Matches(code, pattern);
|
|
|
|
foreach (System.Text.RegularExpressions.Match match in matches)
|
|
{
|
|
var funcNode = new SyntaxNode
|
|
{
|
|
Type = SyntaxNodeType.Method,
|
|
Text = match.Value,
|
|
Metadata = new Dictionary<string, object?>
|
|
{
|
|
["ReturnType"] = match.Groups[1].Value.Trim(),
|
|
["Name"] = match.Groups[2].Value,
|
|
["Parameters"] = match.Groups[3].Value,
|
|
["IsConst"] = match.Groups[4].Success,
|
|
["IsOverride"] = match.Groups[5].Success,
|
|
["IsFinal"] = match.Groups[6].Success
|
|
}
|
|
};
|
|
root.Children.Add(funcNode);
|
|
}
|
|
}
|
|
|
|
private void ExtractNamespaces(string code, SyntaxNode root)
|
|
{
|
|
var pattern = @"namespace\s+(\w+)";
|
|
var matches = System.Text.RegularExpressions.Regex.Matches(code, pattern);
|
|
|
|
foreach (System.Text.RegularExpressions.Match match in matches)
|
|
{
|
|
var nsNode = new SyntaxNode
|
|
{
|
|
Type = SyntaxNodeType.Namespace,
|
|
Text = match.Value,
|
|
Metadata = new Dictionary<string, object?>
|
|
{
|
|
["Name"] = match.Groups[1].Value
|
|
}
|
|
};
|
|
root.Children.Add(nsNode);
|
|
}
|
|
}
|
|
|
|
private void ExtractTemplates(string code, SyntaxNode root)
|
|
{
|
|
var pattern = @"template\s*<([^>]+)>";
|
|
var matches = System.Text.RegularExpressions.Regex.Matches(code, pattern);
|
|
|
|
foreach (System.Text.RegularExpressions.Match match in matches)
|
|
{
|
|
var templateNode = new SyntaxNode
|
|
{
|
|
Type = SyntaxNodeType.Type,
|
|
Text = match.Value,
|
|
Metadata = new Dictionary<string, object?>
|
|
{
|
|
["Parameters"] = match.Groups[1].Value
|
|
}
|
|
};
|
|
root.Children.Add(templateNode);
|
|
}
|
|
}
|
|
|
|
private List<SyntaxComment> ExtractComments(string sourceCode)
|
|
{
|
|
var comments = new List<SyntaxComment>();
|
|
var lines = sourceCode.Split('\n');
|
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i].Trim();
|
|
if (line.StartsWith("//"))
|
|
{
|
|
comments.Add(new SyntaxComment
|
|
{
|
|
Type = CommentType.SingleLine,
|
|
Text = line.TrimStart('/').Trim(),
|
|
LineNumber = i + 1
|
|
});
|
|
}
|
|
else if (line.StartsWith("/*"))
|
|
{
|
|
comments.Add(new SyntaxComment
|
|
{
|
|
Type = CommentType.MultiLine,
|
|
Text = line.TrimStart('/').TrimStart('*').Trim(),
|
|
LineNumber = i + 1
|
|
});
|
|
}
|
|
}
|
|
|
|
return comments;
|
|
}
|
|
}
|