// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Dom.CSharp; using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver; using ILSpy.Debugger.AvalonEdit.Editor; namespace ILSpy.Debugger.Services { public class TParser : IParser { ///IParser Interface string[] lexerTags; public string[] LexerTags { get { return lexerTags; } set { lexerTags = value; } } public LanguageProperties Language { get { return LanguageProperties.CSharp; } } public IExpressionFinder CreateExpressionFinder(string fileName) { return new CSharpExpressionFinder(null); } void RetrieveRegions(ICompilationUnit cu, ICSharpCode.NRefactory.Parser.SpecialTracker tracker) { for (int i = 0; i < tracker.CurrentSpecials.Count; ++i) { ICSharpCode.NRefactory.PreprocessingDirective directive = tracker.CurrentSpecials[i] as ICSharpCode.NRefactory.PreprocessingDirective; if (directive != null) { if (directive.Cmd == "#region") { int deep = 1; for (int j = i + 1; j < tracker.CurrentSpecials.Count; ++j) { ICSharpCode.NRefactory.PreprocessingDirective nextDirective = tracker.CurrentSpecials[j] as ICSharpCode.NRefactory.PreprocessingDirective; if (nextDirective != null) { switch (nextDirective.Cmd) { case "#region": ++deep; break; case "#endregion": --deep; if (deep == 0) { cu.FoldingRegions.Add(new FoldingRegion(directive.Arg.Trim(), DomRegion.FromLocation(directive.StartPosition, nextDirective.EndPosition))); goto end; } break; } } } end: ; } } } } public ICompilationUnit Parse(IProjectContent projectContent, string fileName, ITextBuffer fileContent) { using (ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, fileContent.CreateReader())) { return Parse(p, fileName, projectContent); } } ICompilationUnit Parse(ICSharpCode.NRefactory.IParser p, string fileName, IProjectContent projectContent) { p.Lexer.SpecialCommentTags = lexerTags; p.ParseMethodBodies = false; p.Parse(); NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(projectContent, ICSharpCode.NRefactory.SupportedLanguage.CSharp); visitor.Specials = p.Lexer.SpecialTracker.CurrentSpecials; visitor.VisitCompilationUnit(p.CompilationUnit, null); visitor.Cu.FileName = fileName; visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0; RetrieveRegions(visitor.Cu, p.Lexer.SpecialTracker); AddCommentTags(visitor.Cu, p.Lexer.TagComments); return visitor.Cu; } void AddCommentTags(ICompilationUnit cu, System.Collections.Generic.List tagComments) { foreach (ICSharpCode.NRefactory.Parser.TagComment tagComment in tagComments) { DomRegion tagRegion = new DomRegion(tagComment.StartPosition.Y, tagComment.StartPosition.X); var tag = new ICSharpCode.SharpDevelop.Dom.TagComment(tagComment.Tag, tagRegion, tagComment.CommentText); cu.TagComments.Add(tag); } } public IResolver CreateResolver() { return new NRefactoryResolver(LanguageProperties.CSharp); } ///////// IParser Interface END } }