예제 #1
0
        public FileStatistics CollectInfo(string fileName)
        {
            var lexer      = new PhpAntlrLexer();
            var sourceFile = new TextFile(File.ReadAllText(fileName))
            {
                Name = fileName
            };
            var tokens = lexer.GetTokens(sourceFile, out _);

            var parser = new PhpAntlrParser();

            parser.Logger     = Logger;
            parser.SourceFile = sourceFile;
            PhpAntlrParseTree ust = (PhpAntlrParseTree)parser.Parse(tokens, out _);

            classUsings.Clear();
            methodInvocations.Clear();
            includes.Clear();

            Visit(ust.SyntaxTree);

            var result = new FileStatistics
            {
                FileName          = fileName,
                ClassUsings       = new Dictionary <string, int>(classUsings),
                MethodInvocations = new Dictionary <string, int>(methodInvocations),
                Includes          = new Dictionary <string, int>(includes)
            };

            return(result);
        }
예제 #2
0
        public void Parse_NewLine_CorrectLineColumn()
        {
            string fileText = File.ReadAllText(Path.Combine(TestHelper.TestsDataPath, "newLine -r-n.php"));
            var    lineEnds = new string[] { "\r", "\n", "\r\n" };

            foreach (var lineEnd in lineEnds)
            {
                var    phpParser      = new PhpAntlrParser();
                string code           = fileText.Replace("\r\n", lineEnd);
                var    sourceCodeFile = new SourceCodeFile
                {
                    Name = "newLine.php",
                    Code = code
                };
                var parseTree = (PhpAntlrParseTree)phpParser.Parse(sourceCodeFile);
                var converter = new PhpAntlrParseTreeConverter();
                Ust ust       = converter.Convert(parseTree);

                UstNode intNode = ust.Root.GetAllDescendants(
                    node => node.NodeType == NodeType.IntLiteral &&
                    ((IntLiteral)node).Value == 42).First();

                int beginLine, beginColumn, endLine, endColumn;
                TextHelper.ToLineColumn(intNode.TextSpan, code, out beginLine, out beginColumn, out endLine, out endColumn);
                Assert.AreEqual(1, beginLine);
                Assert.AreEqual(12, beginColumn);
                Assert.AreEqual(14, endColumn);

                UstNode heredocNode = ust.Root.GetAllDescendants(
                    node => node.NodeType == NodeType.StringLiteral &&
                    ((StringLiteral)node).Text.StartsWith("Heredoc text")).First();

                TextHelper.ToLineColumn(heredocNode.TextSpan, code, out beginLine, out beginColumn, out endLine, out endColumn);
                Assert.AreEqual(3, beginLine);
                Assert.AreEqual(6, endLine);

                UstNode serverAddressNode = ust.Root.GetAllDescendants(
                    node => node.NodeType == NodeType.StringLiteral &&
                    ((StringLiteral)node).Text.Contains("http://127.0.0.1")).First();

                TextHelper.ToLineColumn(serverAddressNode.TextSpan, code, out beginLine, out beginColumn, out endLine, out endColumn);
                Assert.AreEqual(8, beginLine);
                Assert.AreEqual(15, beginColumn);
            }
        }
예제 #3
0
        public void Parse_NewLine_CorrectLineColumn()
        {
            string fileText = File.ReadAllText(Path.Combine(TestUtility.TestsDataPath, "newLine -r-n.php"));
            var    lineEnds = new [] { "\r", "\n", "\r\n" };

            foreach (var lineEnd in lineEnds)
            {
                var    phpParser  = new PhpAntlrParser();
                string code       = fileText.Replace("\r\n", lineEnd);
                var    sourceFile = new TextFile(code)
                {
                    Name = "newLine.php",
                };
                var tokens = Language.Php.CreateAntlrLexer().GetTokens(sourceFile, out TimeSpan _);
                phpParser.SourceFile = sourceFile;
                var     parseTree = (PhpAntlrParseTree)phpParser.Parse(tokens, out TimeSpan _);
                var     converter = new PhpAntlrParseTreeConverter();
                RootUst ust       = converter.Convert(parseTree);

                Ust intNode = ust.WhereDescendantsOrSelf(
                    node => node is IntLiteral intLiteral && intLiteral.Value == 42).First();

                LineColumnTextSpan intNodeSpan = intNode.LineColumnTextSpan;
                Assert.AreEqual(1, intNodeSpan.BeginLine);
                Assert.AreEqual(12, intNodeSpan.BeginColumn);
                Assert.AreEqual(14, intNodeSpan.EndColumn);

                Ust heredocNode = ust.WhereDescendantsOrSelf(
                    node => node is StringLiteral stringLiteral &&
                    stringLiteral.Text.StartsWith("Heredoc text")).First();

                LineColumnTextSpan heredocNodeSpan = heredocNode.LineColumnTextSpan;
                Assert.AreEqual(3, heredocNodeSpan.BeginLine);
                Assert.AreEqual(6, heredocNodeSpan.EndLine);

                Ust serverAddressNode = ust.WhereDescendantsOrSelf(
                    node => node is StringLiteral stringLiteral &&
                    stringLiteral.Text.Contains("http://127.0.0.1")).First();

                LineColumnTextSpan serverAddressNodeSpan = serverAddressNode.LineColumnTextSpan;
                Assert.AreEqual(8, serverAddressNodeSpan.BeginLine);
                Assert.AreEqual(15, serverAddressNodeSpan.BeginColumn);
            }
        }