NArrange.VisualBasic.VBParser.ParseNestedText C# (CSharp) Method

ParseNestedText() private method

Parses nested text.
private ParseNestedText ( char beginChar, char endChar, bool beginExpected, bool trim ) : string
beginChar char The begin char.
endChar char The end char.
beginExpected bool Whether or not the begin char is expected.
trim bool Whether or not the text should be trimmed.
return string
        private string ParseNestedText(char beginChar, char endChar, bool beginExpected, bool trim)
        {
            StringBuilder blockText = new StringBuilder(DefaultBlockLength);

            if (beginChar != EmptyChar && beginExpected)
            {
                while (IsWhiteSpace(NextChar))
                {
                    TryReadChar();
                    if (!trim)
                    {
                        blockText.Append(CurrentChar);
                    }
                }

                EatChar(beginChar);
            }

            int depth = 1;
            char nextChar = NextChar;

            if (nextChar == EmptyChar)
            {
                this.OnParseError("Unexpected end of file. Expected " + endChar);
            }
            else if (nextChar == endChar)
            {
                TryReadChar();
            }
            else
            {
                bool inString = false;

                while (depth > 0)
                {
                    bool charRead = TryReadChar();
                    if (!charRead)
                    {
                        this.OnParseError("Unexpected end of file. Expected " + endChar);
                    }

                    nextChar = NextChar;

                    if (CurrentChar == VBSymbol.BeginString)
                    {
                        inString = !inString;
                    }

                    if (beginChar != EmptyChar && CurrentChar == beginChar &&
                        !inString)
                    {
                        blockText.Append(CurrentChar);
                        depth++;
                    }
                    else
                    {
                        blockText.Append(CurrentChar);
                    }

                    if (nextChar == endChar && !inString)
                    {
                        if (depth == 1)
                        {
                            EatChar(endChar);
                            break;
                        }
                        else
                        {
                            depth--;
                        }
                    }
                }
            }

            if (trim)
            {
                return blockText.ToString().Trim();
            }
            else
            {
                return blockText.ToString();
            }
        }