src/NFun/Interpretation/StringInterpolationRuntimeBuilder.cs
/// <summary>
/// separates intepolation string to texts and inner scripts
/// </summary>
/// <param name="script">origin interpolation text</param>
/// <param name="texts">texts between scripts</param>
/// <param name="scripts">scripts between texts</param>
/// <exception cref="FunParseException"></exception>
separates intepolation string to texts and inner scripts
separates intepolation string to texts and inner scripts
private static void SeparateInterpolation(string script, out List<string> texts, out List<string> scripts) {
        texts = new List<string>();
        scripts = new List<string>();
        int pos = -1;
        var reader = new Tokenizer();
        while (true)
        {
            var text = "";
            int endOfText = 0;
            if (pos != -1 || script.Length <= 0 || script[0] != '{')
            {
                (text, endOfText) = QuotationReader.ReadQuotation(script, pos, false);
                if (endOfText == -1)
                {
                    texts.Add(script.Substring(pos + 1));
                    break;
                }
            }
            pos = endOfText;
            texts.Add(text);
            var nextSymbol = script[pos];
            if (nextSymbol != '{')
                throw new NFunImpossibleException($"Unexpected symbol '{nextSymbol}'");
            int obrCount = 1;
            pos++;
            while (obrCount != 0)
            {
                var res = reader.TryReadNext(script, pos);
                if (res.Type == TokType.FiObr)
                    obrCount++;
                else if (res.Type == TokType.FiCbr)
                    obrCount--;
                else if (res.Type == TokType.Eof)
                    throw ErrorFactory.ClosingQuoteIsMissed('}', pos, text.Length);
                pos = res.Finish;
            }
            scripts.Add(script.Substring(endOfText + 1, pos - endOfText - 2));
            pos--;
        }
    }
private static void SeparateInterpolation ( string script , out List < string > texts , out List < string > scripts ) { texts = new List < string > ( ) ; scripts = new List < string > ( ) ; int pos = - 1 ; var reader = new Tokenizer ( ) ; while ( true ) { var text = " " ; int endOfText = 0 ; if ( pos != - 1 || script . Length <= 0 || script [ 0 ] != ' ' ) { ( text , endOfText ) = QuotationReader . ReadQuotation ( script , pos , false ) ; if ( endOfText == - 1 ) { texts . Add ( script . Substring ( pos + 1 ) ) ; break ; } } pos = endOfText ; texts . Add ( text ) ; var nextSymbol = script [ pos ] ; if ( nextSymbol != ' ' ) throw new NFunImpossibleException ( $" Unexpected symbol ' { nextSymbol } ' " ) ; int obrCount = 1 ; pos ++ ; while ( obrCount != 0 ) { var res = reader . TryReadNext ( script , pos ) ; if ( res . Type == TokType . FiObr ) obrCount ++ ; else if ( res . Type == TokType . FiCbr ) obrCount -- ; else if ( res . Type == TokType . Eof ) throw ErrorFactory . ClosingQuoteIsMissed ( ' ' , pos , text . Length ) ; pos = res . Finish ; } scripts . Add ( script . Substring ( endOfText + 1 , pos - endOfText - 2 ) ) ; pos -- ; } }
separates intepolation string to texts and inner scripts
separates intepolation string to texts and inner scripts
//search end of quotation //end of script body here!
None
script
string
origin interpolation text
origin interpolation text
List<string>
out
texts
texts between scripts
texts between scripts
scripts
scripts between texts
scripts between texts