/// <summary>
 /// Appends a fragment to the list of fragments.
 /// </summary>
 /// <param name="newFragment">The fragment to append. May not be null.</param>
 public void Append(MixedCodeDocumentFragment newFragment)
 {
     if (newFragment == null)
     {
         throw new ArgumentNullException("newFragment");
     }
     _items.Add(newFragment);
 }
        private void Parse()
        {
            _state = ParseState.Text;
            _index = 0;
            _currentfragment = CreateFragment(MixedCodeDocumentFragmentType.Text);

            while (_index < _text.Length)
            {
                _c = _text[_index];
                IncrementPosition();

                switch (_state)
                {
                    case ParseState.Text:
                        if (_index + TokenCodeStart.Length < _text.Length)
                        {
                            if (_text.Substring(_index - 1, TokenCodeStart.Length) == TokenCodeStart)
                            {
                                _state = ParseState.Code;
                                _currentfragment.Length = _index - 1 - _currentfragment.Index;
                                _currentfragment = CreateFragment(MixedCodeDocumentFragmentType.Code);
                                SetPosition();
                                continue;
                            }
                        }
                        break;

                    case ParseState.Code:
                        if (_index + TokenCodeEnd.Length < _text.Length)
                        {
                            if (_text.Substring(_index - 1, TokenCodeEnd.Length) == TokenCodeEnd)
                            {
                                _state = ParseState.Text;
                                _currentfragment.Length = _index + TokenCodeEnd.Length - _currentfragment.Index;
                                _index += TokenCodeEnd.Length;
                                _lineposition += TokenCodeEnd.Length;
                                _currentfragment = CreateFragment(MixedCodeDocumentFragmentType.Text);
                                SetPosition();
                                continue;
                            }
                        }
                        break;
                }
            }

            _currentfragment.Length = _index - _currentfragment.Index;
        }
 /// <summary>
 /// Prepends a fragment to the list of fragments.
 /// </summary>
 /// <param name="newFragment">The fragment to append. May not be null.</param>
 public void Prepend(MixedCodeDocumentFragment newFragment)
 {
     if (newFragment == null)
     {
         throw new ArgumentNullException("newFragment");
     }
     _items.Insert(0, newFragment);
 }
 internal int GetFragmentIndex(MixedCodeDocumentFragment fragment)
 {
     if (fragment == null)
     {
         throw new ArgumentNullException("fragment");
     }
     for (int i = 0; i < _items.Count; i++)
     {
         if ((_items[i]) == fragment)
         {
             return i;
         }
     }
     return -1;
 }
 /// <summary>
 /// Remove a fragment from the list of fragments. If this fragment was not in the list, an exception will be raised.
 /// </summary>
 /// <param name="fragment">The fragment to remove. May not be null.</param>
 public void Remove(MixedCodeDocumentFragment fragment)
 {
     if (fragment == null)
     {
         throw new ArgumentNullException("fragment");
     }
     int index = GetFragmentIndex(fragment);
     if (index == -1)
     {
         throw new IndexOutOfRangeException();
     }
     RemoveAt(index);
 }