using CodePlay.Core.Interfaces; using CodePlay.Core.Models; 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); foreach (var line in sourceCode.Split('\n')) { if (line.Trim().StartsWith("//")) { AddComment(tree, line.Trim().TrimStart('/').Trim(), CommentType.SingleLine, 0); } } return Task.FromResult(tree); } private SyntaxNode ParseRoot(string sourceCode) { var root = new SyntaxNode { Type = SyntaxNodeType.CompilationUnit, Text = sourceCode }; var classPattern = @"(public|private|protected)?\s*class\s+(\w+)"; var matches = System.Text.RegularExpressions.Regex.Matches(sourceCode, classPattern); foreach (System.Text.RegularExpressions.Match match in matches) { var className = match.Groups[2].Value; var classNode = new SyntaxNode { Type = SyntaxNodeType.Class, Text = match.Value, Metadata = { ["Name"] = className } }; root.Children.Add(classNode); } return root; } }