Пример #1
0
        public ParseTree Parse(SourceCodeFile sourceCodeFile)
        {
            CSharpRoslynParseTree result = null;

            var filePath = Path.Combine(sourceCodeFile.RelativePath, sourceCodeFile.Name);

            if (sourceCodeFile.Code != null)
            {
                SyntaxNode root = null;
                try
                {
                    SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCodeFile.Code, null, filePath);
                    result          = new CSharpRoslynParseTree(syntaxTree);
                    root            = syntaxTree.GetRoot();
                    result.Comments = root.DescendantTrivia().Where(node =>
                    {
                        SyntaxKind kind = node.Kind();
                        return(kind == SyntaxKind.SingleLineCommentTrivia ||
                               kind == SyntaxKind.MultiLineCommentTrivia ||
                               kind == SyntaxKind.SingleLineDocumentationCommentTrivia ||
                               kind == SyntaxKind.MultiLineDocumentationCommentTrivia ||
                               kind == SyntaxKind.DocumentationCommentExteriorTrivia ||
                               kind == SyntaxKind.XmlComment);
                    }).ToArray();

                    IEnumerable <Diagnostic> diagnostics = root.GetDiagnostics();
                    foreach (var diagnostic in diagnostics)
                    {
                        if (diagnostic.Severity == DiagnosticSeverity.Error &&
                            diagnostic.Id != "CS1029")
                        {
                            var textSpan = RoslynHelper.ConvertTextSpan(diagnostic.Location);
                            Logger.LogError(new ParsingException(filePath, message: diagnostic.ToString())
                            {
                                TextSpan = textSpan
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(new ParsingException(filePath, ex));
                    result = new CSharpRoslynParseTree();
                }
            }
            else
            {
                result = new CSharpRoslynParseTree();
            }
            result.FileName = filePath;
            result.FileData = sourceCodeFile.Code;

            return(result);
        }
Пример #2
0
        public ParseTree Parse(TextFile sourceFile, out TimeSpan parserTimeSpan)
        {
            if (sourceFile.Data == null)
            {
                return(null);
            }

            try
            {
                var        stopwatch  = Stopwatch.StartNew();
                var        filePath   = Path.Combine(sourceFile.RelativePath, sourceFile.Name);
                SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceFile.Data, null, filePath);
                var        result     = new CSharpRoslynParseTree(syntaxTree);
                SyntaxNode root       = syntaxTree.GetRoot();
                result.Comments = root.DescendantTrivia().Where(node =>
                {
                    SyntaxKind kind = node.Kind();
                    return(kind == SyntaxKind.SingleLineCommentTrivia ||
                           kind == SyntaxKind.MultiLineCommentTrivia ||
                           kind == SyntaxKind.SingleLineDocumentationCommentTrivia ||
                           kind == SyntaxKind.MultiLineDocumentationCommentTrivia ||
                           kind == SyntaxKind.DocumentationCommentExteriorTrivia ||
                           kind == SyntaxKind.XmlComment);
                }).ToArray();

                IEnumerable <Diagnostic> diagnostics = root.GetDiagnostics();
                foreach (Diagnostic diagnostic in diagnostics)
                {
                    if (diagnostic.Severity == DiagnosticSeverity.Error &&
                        diagnostic.Id != "CS1029")
                    {
                        var textSpan = diagnostic.Location.ToTextSpan();
                        Logger.LogError(new ParsingException(sourceFile, message: diagnostic.ToString())
                        {
                            TextSpan = textSpan
                        });
                    }
                }
                stopwatch.Stop();
                parserTimeSpan = stopwatch.Elapsed;

                result.SourceFile = sourceFile;
                return(result);
            }
            catch (Exception ex) when(!(ex is ThreadAbortException))
            {
                Logger.LogError(new ParsingException(sourceFile, ex));
                return(null);
            }
        }
Пример #3
0
        public ParseTree Parse(SourceCodeFile sourceCodeFile)
        {
            ParseTree result = null;

            var filePath = Path.Combine(sourceCodeFile.RelativePath, sourceCodeFile.Name);

            if (sourceCodeFile.Code != null)
            {
                try
                {
                    var             parser   = new AspxParser.AspxParser(sourceCodeFile.RelativePath);
                    var             source   = new AspxSource(sourceCodeFile.FullPath, sourceCodeFile.Code);
                    AspxParseResult aspxTree = parser.Parse(source);
                    foreach (var error in aspxTree.ParseErrors)
                    {
                        Logger.LogError(new ParsingException(filePath, message: error.Message)
                        {
                            TextSpan = error.Location.GetTextSpan()
                        });
                    }
                    result = new AspxParseTree(aspxTree.RootNode);
                }
                catch (Exception ex)
                {
                    Logger.LogError(new ParsingException(filePath, ex));
                    result = new CSharpRoslynParseTree();
                }
            }
            else
            {
                result = new CSharpRoslynParseTree();
            }
            result.FileName = filePath;
            result.FileData = sourceCodeFile.Code;

            return(result);
        }