Esempio n. 1
0
        public ParseTree Parse(TextFile sourceFile, out TimeSpan parserTimeSpan)
        {
            if (sourceFile.Data == null)
            {
                return(null);
            }

            try
            {
                var             stopwatch = Stopwatch.StartNew();
                var             parser    = new global::AspxParser.AspxParser(sourceFile.RelativePath);
                var             source    = new AspxSource(sourceFile.FullName, sourceFile.Data);
                AspxParseResult aspxTree  = parser.Parse(source);
                foreach (var error in aspxTree.ParseErrors)
                {
                    Logger.LogError(new ParsingException(sourceFile, message: error.Message)
                    {
                        TextSpan = error.Location.GetTextSpan()
                    });
                }
                var result = new AspxParseTree(aspxTree.RootNode);
                result.SourceFile = sourceFile;
                stopwatch.Stop();

                parserTimeSpan = stopwatch.Elapsed;

                return(result);
            }
            catch (Exception ex) when(!(ex is ThreadAbortException))
            {
                Logger.LogError(new ParsingException(sourceFile, ex));
                return(null);
            }
        }
Esempio n. 2
0
        public void Convert_AspxLineColumnPosition_Correct()
        {
            string          fileName   = Path.Combine(TestUtility.TestsDataPath, "TestAspxParser.aspx");
            string          text       = File.ReadAllText(fileName);
            var             aspxParser = new global::AspxParser.AspxParser(fileName, true);
            var             source     = new AspxSource(fileName, text);
            AspxParseResult result     = aspxParser.Parse(source);
            var             foundNode  = result.RootNode.Descendants <AspxNode.AspxExpressionTag>()
                                         .FirstOrDefault(node => node.Expression.Contains("Expression text"));

            var sourceCode = new TextFile(source.Text);

            sourceCode.GetLineColumnFromLinear(foundNode.Location.Start, out int line, out int column);
            Assert.AreEqual(15, line);
            Assert.AreEqual(13, column);
            Assert.AreEqual(foundNode.Location.Start, sourceCode.GetLinearFromLineColumn(line, column));

            sourceCode.GetLineColumnFromLinear(foundNode.Location.End, out line, out column);
            Assert.AreEqual(15, line);
            Assert.AreEqual(30, column);
            Assert.AreEqual(foundNode.Location.End, sourceCode.GetLinearFromLineColumn(line, column));
        }
Esempio n. 3
0
        public ParseTree Parse(CodeFile sourceCodeFile)
        {
            ParseTree result = null;

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

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

            return(result);
        }