protected override void AddFragment(GenSegBody body, GenFragment frag)
 {
     Contract.Assert(!body.Fragment.Contains(frag), "Fragment added to body again");
     body.Add(frag);
 }
        private void ScanBlockParams(GenSegBody body, GenContainerFragmentBase parentContainer, Function function,
                                     FragmentBody fragmentBody)
        {
            Scan.ScanWhile(ScanReader.WhiteSpace);

            if (Scan.CheckChar(Scan.Delimiter))
            {
                Scan.SkipChar();
            }
            var t = Scan.ScanTokenType();

            while (t != TokenType.Close)
            {
                string s;
                if (t != TokenType.Unknown && t != TokenType.Name)
                {
                    // Parameter starts with a delimiter
                    if (t != TokenType.Close && t != TokenType.Unknown)
                    {
                        // Scan contained block
                        GenTextBlock textBlock = null;
                        var          frag      = ScanFragment(GenDataDef.CurrentClassId, ref t, out s, parentContainer, fragmentBody,
                                                              ref textBlock, true);
                        body.Add(frag ?? textBlock);

                        // Skip blank parameter separators
                        s = s.TrimStart();
                        if (s != "")
                        {
                            body.Add(
                                new GenTextFragment(new GenTextFragmentParams(GenDataDef, parentContainer,
                                                                              s)));
                        }
                        t = Scan.ScanTokenType();
                    }
                }
                else
                {
                    // Parameter starts without a delimiter
                    var block = new GenBlock(new GenFragmentParams(GenDataDef, parentContainer));

                    s = Scan.CheckChar('\'') ? Scan.ScanQuotedString() : Scan.ScanUntil(_parameterSeparator);

                    while (Scan.CheckChar(' '))
                    {
                        Scan.SkipChar();
                    }

                    // Scan for Text and Placeholders
                    var i = s.IndexOf(Scan.Delimiter);
                    while (i != -1)
                    {
                        var w = s.Substring(0, i - 1); // Text up to first delimiter
                        s = s.Substring(i + 1);        // Text after first delimeter
                        if (s != "")
                        {
                            // Some text after the first delimiter
                            i = s.IndexOf(Scan.Delimiter); // Position of next delimiter
                            if (i != -1)
                            {
                                if (w != "")
                                {
                                    // Add Text up to first delimiter
                                    block.Body.Add(
                                        new GenTextFragment(new GenTextFragmentParams(GenDataDef,
                                                                                      parentContainer, w)));
                                }
                                w = s.Substring(0, i - 1); // Text between initial two delimiters
                                block.Body.Add(
                                    new GenPlaceholderFragment(new GenPlaceholderFragmentParams(GenDataDef, parentContainer, GenDataDef.GetId(w))));

                                s = s.Substring(i + 1);
                                i = s.IndexOf(Scan.Delimiter);
                            }
                            else
                            {
                                // No matching delimiter: output delimiter with text
                                block.Body.Add(
                                    new GenTextFragment(new GenTextFragmentParams(GenDataDef,
                                                                                  parentContainer, w + Scan.Delimiter + s)));
                                s = "";
                            }
                        }
                        else
                        {
                            // No text after initial delimiter: output delimiter with text
                            block.Body.Add(
                                new GenTextFragment(new GenTextFragmentParams(GenDataDef, parentContainer,
                                                                              w + Scan.Delimiter)));
                            i = -1;
                        }
                    }

                    if (s != "" || block.Body.Count == 0)
                    {
                        // Text without placeholders
                        block.Body.Add(
                            new GenTextFragment(new GenTextFragmentParams(GenDataDef, block, s)));
                    }

                    body.Add(block);

                    if (Scan.CheckChar(Scan.Delimiter))
                    {
                        Scan.SkipChar();
                    }

                    t = Scan.ScanTokenType();
                }
            }

            if (t == TokenType.Close)
            {
                Scan.SkipChar();
            }
        }