using CodePlay.Core.Interfaces; using CodePlay.Core.Common; namespace CodePlay.Core.Parsers; /// /// C++ 解析器 (增强版) /// 使用改进的正则表达式和相关性检测 /// public class CppParser : BaseParser { public override LanguageType SupportedLanguage => LanguageType.CPlusPlus; public override Task 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 { ["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 { ["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 { ["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 { ["Parameters"] = match.Groups[1].Value } }; root.Children.Add(templateNode); } } private List ExtractComments(string sourceCode) { var comments = new List(); 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; } }