コード例 #1
0
 /// <summary>
 /// Загрузить содержимое файла с исходным кодом и получить набор единиц кода
 /// </summary>
 /// <returns></returns>
 public CCodeUnitsCollection Load()
 {
     if (File.Exists(m_args.GetPath()))
     {
         using (FileStream f_stream = new FileStream(m_args.GetPath(), FileMode.Open))
         {
             using (StreamReader sreader = new StreamReader(f_stream, m_args.GetEncoding()))
             {
                 CCodeUnitsCollection collection = new CCodeUnitsCollection();
                 string source_str  = null;
                 long   line_number = CElementPosition.LINE_NUMBER_LOW_BOUND;
                 while ((source_str = sreader.ReadLine()) != null)
                 {
                     CCodeUnit unit = new CCodeUnit(new CElementPosition(line_number, CElementPosition.INDEX_NUMBER_LOW_BOUND, line_number, source_str.Length), source_str);
                     collection.Add(new CExtendedCodeUnit(unit, m_args.GetFileID().SourceFileID));
                     ++line_number;
                 }
                 return(collection);
             }
         }
     }
     else
     {
         throw new FileNotFoundException("File not found", m_args.GetPath());
     }
 }
コード例 #2
0
        public void CCodeUnitsCollectionConstructorTest()
        {
            CCodeUnitsCollection target = new CCodeUnitsCollection();

            Assert.IsNotNull(target);
            Assert.AreEqual(target.Size(), 0);
        }
コード例 #3
0
 /// <summary>
 /// Проинициализировать поля класса
 /// </summary>
 private void InitMembers()
 {
     m_ModifiedCollection   = new CCodeUnitsCollection();
     m_PairCommentStack     = new Stack <CCommentedCodeFragment>();
     m_OnelineCodeFragments = new List <CCommentedCodeFragment>();
     ClearProcessingCommentSymbol();
 }
コード例 #4
0
        public void ClearTest()
        {
            CCodeUnitsCollection target = CreateObj();

            Assert.AreEqual(target.Size(), 3);
            target.Clear();
            Assert.AreEqual(target.Size(), 0);
        }
コード例 #5
0
        public void RemoveRowTest()
        {
            CCodeUnitsCollection target = CreateObj();

            Assert.AreEqual(target.Size(), 3);
            target.RemoveRow(1);
            Assert.AreEqual(target.Size(), 2);
        }
コード例 #6
0
        public void ToArrayTest()
        {
            CCodeUnitsCollection target = CreateObj();

            CExtendedCodeUnit[] actual;
            actual = target.ToArray();
            Assert.AreEqual(actual.Length, 3);
        }
コード例 #7
0
        private CCodeUnitsCollection CreateObj()
        {
            CCodeUnitsCollection target = new CCodeUnitsCollection();

            target.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            target.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(13, 33), ")"), 1, 1));
            target.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(14, 43), ")"), 1, 1));
            return(target);
        }
コード例 #8
0
        public void AddTest()
        {
            CCodeUnitsCollection target = new CCodeUnitsCollection();
            CExtendedCodeUnit    item   = new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1);

            target.Add(item);
            Assert.AreEqual(target.Size(), 1);
            Assert.IsTrue(target[0].EqualsObject(item));
        }
コード例 #9
0
        private CCodeUnitsCollection CreateExpectedCollection1()
        {
            CCodeUnitsCollection collect = new CCodeUnitsCollection();

            for (int counter = 0; counter < processed_code.Length; ++counter)
            {
                collect.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(counter + 1, 0, counter + 1, processed_code[counter].Length), processed_code[counter]), 1, counter + 1));
            }
            return(collect);
        }
コード例 #10
0
        private CCodeUnitsCollection CreateTestCollection1()
        {
            CCodeUnitsCollection collect = new CCodeUnitsCollection();

            for (int counter = 0; counter < original_code.Length; ++counter)
            {
                collect.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(counter + 1, 0, counter + 1, original_code[counter].Length), original_code[counter]), 1, counter + 1));
            }
            return(collect);
        }
コード例 #11
0
ファイル: CCodeSizeDetector.cs プロジェクト: mfvanek/i-clone
        private void IncreaseCountOfLines(string filename)
        {
            // Нам нужно реальное количество строк в файле!
            CSourceFileID            FileID     = new CSourceFileID();
            CTokenizerParms          args       = new CTokenizerParms(filename, FileID);
            ISourceFileContentLoader loader     = new CEntireRowSourceFileContentLoader(args);
            CCodeUnitsCollection     collection = loader.Load();

            Interlocked.Add(ref m_CountOfLines, collection.Size());
        }
コード例 #12
0
        public void ItemTest()
        {
            CCodeUnitsCollection target = CreateObj();

            Assert.IsTrue(target[1].EqualsObject(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(13, 33), ")"), 1, 1)));

            CExtendedCodeUnit expected = new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1);

            target[1] = expected;
            Assert.IsTrue(target[1].EqualsObject(expected));
        }
コード例 #13
0
        public void GetEnumeratorTest()
        {
            CCodeUnitsCollection target = CreateObj();
            string actual = string.Empty;

            foreach (CExtendedCodeUnit unit in target)
            {
                actual += unit.LineStart.ToString();
            }
            Assert.AreEqual(actual, "121314");
        }
コード例 #14
0
        public void LoadTest2()
        {
            string filename = Path.GetFullPath("UnitTestSampleFile2.cs");

            Assert.IsTrue(File.Exists(filename));

            CTokenizerParms args = new CTokenizerParms(filename, new CSourceFileID());
            CEntireRowSourceFileContentLoader target = new CEntireRowSourceFileContentLoader(args);
            CCodeUnitsCollection actual = target.Load();

            Assert.AreEqual(76, actual.Size());
        }
コード例 #15
0
ファイル: CCsICloneExtension.cs プロジェクト: mfvanek/i-clone
        /// <summary>
        /// Представить исходный код, содержащийся в файле, в виде набора токенов
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public override CCodeUnitsCollection Tokenize(CTokenizerParms args)
        {
            CCodeUnitsCollection result_collection = new CCodeUnitsCollection();
            Scanner         lex  = new Scanner(args.GetPath());
            CTokensIterator iter = new CTokensIterator(lex);

            foreach (Token token in iter)
            {
                result_collection.Add(ExtCodeUnitCreator.FromToken(token, args));
            }
            return(result_collection);
        }
コード例 #16
0
        public void LoadTest3()
        {
            string filename = Path.GetFullPath("MimeTypeDetection.cs");

            Assert.IsTrue(File.Exists(filename));

            CTokenizerParms args = new CTokenizerParms(filename, new CSourceFileID());
            CTokenSourceFileContentLoader target = new CTokenSourceFileContentLoader(args, CAvailableExtentions.GetExtention(LANGUAGES.LANGUAGE_C_SHARP));
            CCodeUnitsCollection          actual = target.Load();

            Assert.AreEqual(796, actual.Size());
        }
コード例 #17
0
        public void ContentTest()
        {
            CCodeUnitsCollection _Content = new CCodeUnitsCollection();
            CCodeFragment        target   = new CCodeFragment(_Content);

            Assert.AreEqual(target.Content.Size(), 0);

            _Content = new CCodeUnitsCollection();
            _Content.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            _Content.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 33), ")"), 1, 1));
            target.Content = _Content;
            Assert.AreEqual(target.Content.Size(), 2);
        }
コード例 #18
0
        public void IsBelongOneLineTest1()
        {
            CCodeFragment target = new CCodeFragment(CreateCollection());

            Assert.IsFalse(target.IsBelongOneLine());

            CCodeUnitsCollection _Content = new CCodeUnitsCollection();

            _Content.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            _Content.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 33), ")"), 1, 1));

            target = new CCodeFragment(_Content);
            Assert.IsTrue(target.IsBelongOneLine());
        }
コード例 #19
0
ファイル: CCodeSizeDetector.cs プロジェクト: mfvanek/i-clone
 private void IncreaseCountOfTokens(string filename)
 {
     try
     {
         CSourceFileID            FileID     = new CSourceFileID();
         CTokenizerParms          args       = new CTokenizerParms(filename, FileID);
         ISourceFileContentLoader loader     = new CTokenSourceFileContentLoader(args, m_Extension);
         CCodeUnitsCollection     collection = loader.Load();
         Interlocked.Add(ref m_CountOfTokens, collection.Size());
     }
     catch
     {
         // Если поддержка токенизации отсутствует, то ничего не делаем
     }
 }
コード例 #20
0
        public void EqualsTest()
        {
            CCodeUnitsCollection first  = CreateObj();
            CCodeUnitsCollection second = new CCodeUnitsCollection();

            Assert.IsFalse(first.Equals(second));

            second.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            second.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(13, 33), ")"), 1, 1));
            second.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(14, 43), ")"), 1, 1));
            Assert.IsTrue(first.Equals(second));

            second.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            Assert.IsFalse(first.Equals(second));
        }
コード例 #21
0
        public void SetSourceRowsTest()
        {
            CCodeUnitsCollection target = new CCodeUnitsCollection();

            Assert.AreEqual(target.Size(), 0);

            List <CExtendedCodeUnit> units = new List <CExtendedCodeUnit>();

            units.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            units.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            units.Add(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(12, 23), ")"), 1, 1));
            Assert.AreEqual(units.Count, 3);

            target.SetCodeUnits(units);
            Assert.AreEqual(target.Size(), units.Count);
        }
コード例 #22
0
ファイル: Utils.cs プロジェクト: mfvanek/i-clone
 private void BuildCCodeUnitsCollection(MethodBlock mb)
 {
     code_units_collection = new CCodeUnitsCollection();
     foreach (Token token in iter)
     {
         if (IsTokenBelongMethodBlock(token, mb))
         {
             code_units_collection.Add(ExtCodeUnitCreator.FromToken(token, m_args));
         }
         else
         {
             if (IsTokenAfterMethodBlock(token, mb))
             {
                 const string METHOD_END_TOKEN = "}";
                 if ((token.val == METHOD_END_TOKEN) && (code_units_collection.back().Text != METHOD_END_TOKEN))
                 {
                     code_units_collection.Add(ExtCodeUnitCreator.FromToken(token, m_args));
                 }
                 break;
             }
         }
     }
 }
コード例 #23
0
        public void PreProcessingTest1()
        {
            CSimpleCodePreProcessingAlgorithm target = new CSimpleCodePreProcessingAlgorithm();
            CCodePreProcessingOptions         PreProcessingOptions = new CCodePreProcessingOptions("//;/* */;///");

            PreProcessingOptions.SetDeleteWhiteSpaces(false);

            CCodeUnitsCollection OriginalCodeUnitsCollection = CreateTestCollection1();

            Assert.AreEqual(OriginalCodeUnitsCollection.Size(), 21);

            CCodeUnitsCollection expected = CreateExpectedCollection1();

            Assert.AreEqual(expected.Size(), 11);

            CCodeUnitsCollection actual = target.PreProcessing(PreProcessingOptions, OriginalCodeUnitsCollection);

            Assert.AreEqual(actual.Size(), expected.Size());

            for (int counter = 0; counter < actual.Size(); ++counter)
            {
                Assert.AreEqual(actual[counter].Text, expected[counter].Text);
            }
        }
コード例 #24
0
 /// <summary>
 /// Выполнить предварительную обработку единиц кода
 /// </summary>
 /// <param name="PreProcessingOptions"></param>
 /// <param name="OriginalCodeUnitsCollection"></param>
 /// <returns></returns>
 public abstract CCodeUnitsCollection PreProcessing(CCodePreProcessingOptions PreProcessingOptions, CCodeUnitsCollection OriginalCodeUnitsCollection);
コード例 #25
0
 public void IndexEndTest2()
 {
     CCodeUnitsCollection _Content = new CCodeUnitsCollection();
     CCodeFragment        target   = new CCodeFragment(_Content);
     int actual = target.IndexEnd;
 }
コード例 #26
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="PreProcessingOptions"></param>
        /// <param name="OriginalCodeUnitsCollection"></param>
        /// <returns></returns>
        public override CCodeUnitsCollection PreProcessing(CCodePreProcessingOptions PreProcessingOptions, CCodeUnitsCollection OriginalCodeUnitsCollection)
        {
            ClearMembers();
            m_ModifiedCollection.Clear();

            string ScanningSymbol = string.Empty;

            for (int units_counter = 0; units_counter < OriginalCodeUnitsCollection.Size(); ++units_counter)
            {
                CExtendedCodeUnit current_unit = OriginalCodeUnitsCollection[units_counter];
                CExtendedCodeUnit new_codeunit = new CExtendedCodeUnit(current_unit);

                #region // Удаление комментариев из кода

                if (PreProcessingOptions.DeleteComments)
                {
                    m_OnelineCodeFragments.Clear();
                    ClearProcessingCommentSymbol();

                    // Суть алгоритма в следующем: перебираем все строки; в каждой строке перебираем все символы.
                    // Если текущий символ является одним из возможных символов комментариев, то добавляем его в строку.
                    // Если длина этой строки лежит в пределах от минимальной до максимальной из возможных длин комментариев, то проверяем является ли эта строка комментарием.
                    for (int char_counter = 0; char_counter < current_unit.Text.Length; char_counter++)
                    {
                        char current_char = current_unit.Text[char_counter];

                        if (PreProcessingOptions.CommentSymbols.IsCommentChar(current_char))
                        {
                            m_ProcessingCommentSymbol      += current_char;
                            m_IndexOfCommentSymbolBeginning = (m_IndexOfCommentSymbolBeginning == CElementPosition.INDEX_NUMBER_LOW_BOUND - 1) ? char_counter : m_IndexOfCommentSymbolBeginning;

                            if (m_ProcessingCommentSymbol.Length >= PreProcessingOptions.CommentSymbols.MinCommentLength)
                            {
                                if (m_ProcessingCommentSymbol.Length <= PreProcessingOptions.CommentSymbols.MaxCommentLength)
                                {
                                    if (!String.IsNullOrEmpty(ScanningSymbol))
                                    {
                                        if (m_ProcessingCommentSymbol == ScanningSymbol)
                                        {
                                            AddFragment(GetRealLineNumber(units_counter), char_counter);
                                            ScanningSymbol = string.Empty;
                                            ClearProcessingCommentSymbol();
                                        }
                                        else
                                        {
                                            if (m_ProcessingCommentSymbol.Length >= ScanningSymbol.Length)
                                            {
                                                ClearProcessingCommentSymbol();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        // Попробуем  поискать такой символ комментария, при этом нужно узнать парный ли он.
                                        bool IsNotPair = false;

                                        if (PreProcessingOptions.CommentSymbols.IsCommentSymbol(m_ProcessingCommentSymbol, ref IsNotPair))
                                        {
                                            if (IsNotPair)
                                            {
                                                // Если комментарий непарный, то нужно удалить всё до конца строки
                                                CCommentedCodeFragment new_fragment = new CCommentedCodeFragment(new CElementPosition(GetRealLineNumber(units_counter), m_IndexOfCommentSymbolBeginning, GetRealLineNumber(units_counter), current_unit.Text.Length - 1), new CPair <string>(m_ProcessingCommentSymbol, string.Empty));
                                                m_OnelineCodeFragments.Add(new_fragment);
                                                break;
                                            }
                                            else
                                            {
                                                if (PreProcessingOptions.PairCommentDictionary.ContainsKey(m_ProcessingCommentSymbol))
                                                {
                                                    m_PairCommentStack.Push(new CCommentedCodeFragment(new CElementPosition(GetRealLineNumber(units_counter), m_IndexOfCommentSymbolBeginning), new CPair <string>(m_ProcessingCommentSymbol, PreProcessingOptions.PairCommentDictionary[m_ProcessingCommentSymbol])));
                                                    ClearProcessingCommentSymbol();
                                                }
                                                else
                                                {
                                                    AddFragment(GetRealLineNumber(units_counter), char_counter);
                                                    ClearProcessingCommentSymbol();
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    ClearProcessingCommentSymbol();
                                }
                            }
                        }
                        else
                        {
                            ClearProcessingCommentSymbol();
                        }
                    }


                    if (m_OnelineCodeFragments.Count > 0)
                    {
                        new_codeunit.Text = string.Empty;

                        int starting_index = 0;
                        foreach (CCommentedCodeFragment fragment in m_OnelineCodeFragments)
                        {
                            //try
                            //{
                            new_codeunit.Text = new_codeunit.Text + current_unit.Text.Substring(starting_index, fragment.IndexStart - starting_index);
                            //}
                            //catch (ArgumentOutOfRangeException)
                            //{
                            //   throw;
                            //}
                            starting_index = fragment.IndexEnd + 1;
                        }

                        if (starting_index < current_unit.Text.Length)
                        {
                            //try
                            //{
                            new_codeunit.Text = new_codeunit.Text + current_unit.Text.Substring(starting_index);
                            //}
                            //catch (ArgumentOutOfRangeException)
                            //{
                            //   throw;
                            //}
                        }
                    }

                    if (m_PairCommentStack.Count > 0)
                    {
                        CCommentedCodeFragment open_fragment = m_PairCommentStack.Peek();
                        ScanningSymbol = open_fragment.CommentSymbolPair.Second;

                        if (open_fragment.IsBelongOneLine(GetRealLineNumber(units_counter)))
                        {
                            int index = new_codeunit.Text.IndexOf(open_fragment.CommentSymbolPair.First);
                            new_codeunit.Text = new_codeunit.Text.Substring(0, index);
                        }
                        else
                        {
                            new_codeunit.Text = string.Empty;
                        }
                    }
                }

                #endregion

                if (PreProcessingOptions.DeleteWhiteSpaces)
                {
                    new_codeunit.Text = CStringConverter.RemoveMultipleWhiteSpaces(new_codeunit.Text.Trim());
                }

                if (!PreProcessingOptions.DeleteEmptyLines || !String.IsNullOrEmpty(new_codeunit.Text))
                {
                    m_ModifiedCollection.Add(new_codeunit);
                }
            }
            return(m_ModifiedCollection);
        }
コード例 #27
0
 public void backTest2()
 {
     CCodeUnitsCollection target = new CCodeUnitsCollection();
     CExtendedCodeUnit    unit   = target.back();
 }
コード例 #28
0
        public void backTest1()
        {
            CCodeUnitsCollection target = CreateObj();

            Assert.IsTrue(target.back().EqualsObject(new CExtendedCodeUnit(new CCodeUnit(new CElementPosition(14, 43), ")"), 1, 1)));
        }
コード例 #29
0
 public void frontTest2()
 {
     CCodeUnitsCollection target = new CCodeUnitsCollection();
     CExtendedCodeUnit    unit   = target.front();
 }
コード例 #30
0
        public void ToListTest()
        {
            CCodeUnitsCollection target = CreateObj();

            Assert.AreEqual(target.ToList().Count, 3);
        }