Beispiel #1
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);
        }
Beispiel #2
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);
        }
Beispiel #3
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());
        }
Beispiel #4
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));
        }
 /// <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());
     }
 }
Beispiel #6
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));
        }
        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);
        }
        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);
        }
Beispiel #9
0
        /// <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);
        }
Beispiel #10
0
 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;
             }
         }
     }
 }
Beispiel #11
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);
        }