From 96cb73ba9844aad7558e4ee1fafccfb6f5ce6951 Mon Sep 17 00:00:00 2001 From: triton Date: Tue, 8 Apr 2014 18:59:19 +0100 Subject: [PATCH] Added comments support to AST converter to match the support in the AST. --- src/Core/Parser/ASTConverter.cs | 89 ++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/src/Core/Parser/ASTConverter.cs b/src/Core/Parser/ASTConverter.cs index 72675e6b..50694104 100644 --- a/src/Core/Parser/ASTConverter.cs +++ b/src/Core/Parser/ASTConverter.cs @@ -237,6 +237,28 @@ namespace CppSharp } } + /// + /// Implements the visitor pattern for the generated comment bindings. + /// + public abstract class CommentsVisitor + { + public abstract TRet VisitFullComment(FullComment comment); + + public virtual TRet Visit(Parser.AST.Comment comment) + { + switch (comment.Kind) + { + case CommentKind.FullComment: + { + var _comment = new FullComment(comment.__Instance); + return VisitFullComment(_comment); + } + } + + throw new ArgumentOutOfRangeException(); + } + } + #endregion #region Parser AST converters @@ -248,14 +270,16 @@ namespace CppSharp public class ASTConverter { ASTContext Context { get; set; } - TypeConverter typeConverter; - DeclConverter declConverter; + readonly TypeConverter typeConverter; + readonly DeclConverter declConverter; + readonly CommentConverter commentConverter; public ASTConverter(ASTContext context) { Context = context; typeConverter = new TypeConverter(); - declConverter = new DeclConverter(typeConverter); + commentConverter = new CommentConverter(); + declConverter = new DeclConverter(typeConverter, commentConverter); typeConverter.declConverter = declConverter; } @@ -551,13 +575,15 @@ namespace CppSharp public unsafe class DeclConverter : DeclVisitor { - TypeConverter typeConverter; + readonly TypeConverter typeConverter; + readonly CommentConverter commentConverter; - Dictionary Declarations; + readonly Dictionary Declarations; - public DeclConverter(TypeConverter converter) + public DeclConverter(TypeConverter type, CommentConverter comment) { - typeConverter = converter; + typeConverter = type; + commentConverter = comment; Declarations = new Dictionary(); } @@ -607,6 +633,45 @@ namespace CppSharp return _base; } + AST.RawComment VisitRawComment(RawComment rawComment) + { + var _rawComment = new AST.RawComment + { + Kind = ConvertRawCommentKind(rawComment.RawCommentKind), + BriefText = rawComment.BriefText, + Text = rawComment.Text, + FullComment = commentConverter.Visit(rawComment.FullComment) + as AST.FullComment + }; + + return _rawComment; + } + + private AST.RawCommentKind ConvertRawCommentKind(RawCommentKind kind) + { + switch (kind) + { + case RawCommentKind.Invalid: + return AST.RawCommentKind.Invalid; + case RawCommentKind.OrdinaryBCPL: + return AST.RawCommentKind.OrdinaryBCPL; + case RawCommentKind.OrdinaryC: + return AST.RawCommentKind.OrdinaryC; + case RawCommentKind.BCPLSlash: + return AST.RawCommentKind.BCPLSlash; + case RawCommentKind.BCPLExcl: + return AST.RawCommentKind.BCPLExcl; + case RawCommentKind.JavaDoc: + return AST.RawCommentKind.JavaDoc; + case RawCommentKind.Qt: + return AST.RawCommentKind.Qt; + case RawCommentKind.Merged: + return AST.RawCommentKind.Merged; + default: + throw new ArgumentOutOfRangeException("kind"); + } + } + void VisitDeclaration(Declaration decl, AST.Declaration _decl) { var originalPtr = new IntPtr(decl.OriginalPtr); @@ -625,6 +690,8 @@ namespace CppSharp _decl.IsIncomplete = decl.IsIncomplete; _decl.IsDependent = decl.IsDependent; _decl.DefinitionOrder = decl.DefinitionOrder; + if (decl.Comment != null) + _decl.Comment = VisitRawComment(decl.Comment); for (uint i = 0; i < decl.PreprocessedEntitiesCount; ++i) { @@ -1258,6 +1325,14 @@ namespace CppSharp } } + public unsafe class CommentConverter : CommentsVisitor + { + public override AST.Comment VisitFullComment(FullComment comment) + { + throw new NotImplementedException(); + } + } + #endregion }