Esempio n. 1
0
        /// <summary>
        /// Generate a String.Format() text from the given text having left and right delimiters.
        /// For example given
        /// "let v[i] = v[i] + Multivector(#E[id]# = 'v[i]c[id]')".ToStringFormatCode("[", "]")
        /// Generates:
        /// "String.Format("let v{0} = v{0} + Multivector(#E{1}# = 'v{0}c{1}')", i, id)"
        /// </summary>
        /// <param name="text"></param>
        /// <param name="leftDel"></param>
        /// <param name="rightDel"></param>
        /// <param name="verbatimFlag"></param>
        /// <returns></returns>
        public static string ToStringFormatCode(this string text, string leftDel, string rightDel, bool verbatimFlag = false)
        {
            var textBuilder = new MappingComposer();

            textBuilder
            .SetDelimitedText(text, leftDel, rightDel)
            .UniqueMarkedSegments
            .TransformByMarkedIndexUsing(index => "{" + index + "}");

            if (textBuilder.HasMarkedSegments == false)
            {
                return(text.ValueToQuotedLiteral(verbatimFlag));
            }

            //Escape all braces in original text to prevent error when String.Format() is called
            textBuilder
            .UniqueUnmarkedSegments
            .Where(s => s.OriginalText.Contains('{') || s.OriginalText.Contains('}'))
            .TransformUsing(
                s => s.Replace("{", "{{").Replace("}", "}}")
                );

            return
                (textBuilder
                 .UniqueMarkedSegments
                 .Select(s => s.InitialText)
                 .Concatenate(
                     ", ",
                     "String.Format(" + textBuilder.FinalText.ValueToQuotedLiteral(verbatimFlag) + ", ",
                     ")"
                     ));
        }
Esempio n. 2
0
        /// <summary>
        /// Map all strings delimited by "| and |" into String.Format() statements
        /// (or string literals if possible)
        /// </summary>
        /// <param name="scriptText"></param>
        /// <returns></returns>
        private static string TranslateFormattedStrings(string scriptText)
        {
            var mappingComposer = new MappingComposer();

            mappingComposer
            .SetDelimitedText(scriptText, "\"|", "|\"")
            .UniqueMarkedSegments
            .TransformUsing(ToStringSubstituteCode);

            return(mappingComposer.FinalText);
        }
Esempio n. 3
0
        private static string TranslateAngleBracketCommands(string scriptText)
        {
            var mappingComposer = new MappingComposer();

            mappingComposer
            .SetDelimitedText(scriptText, "<:", ":>")
            .UniqueMarkedSegments
            .TransformUsing(TranslateAngleBracketCommand);

            return(mappingComposer.FinalText);
        }
Esempio n. 4
0
        /// <summary>
        /// Map all strings identified by a leading @ using the shortcuts dictionary
        /// If the shortcut is not found, leave it as it is
        /// </summary>
        /// <param name="scriptText"></param>
        /// <returns></returns>
        private string TranslateShortcuts(string scriptText)
        {
            var mappingComposer = new MappingComposer();

            mappingComposer
            .SetIdentifiedText(scriptText, "@")
            .UniqueMarkedSegments
            .TransformUsing(
                s =>
            {
                string value;
                return
                (Shortcuts.TryGetMethodName(s.InitialText.ToLower(), out value)
                            ? value
                            : s.OriginalText);
            }
                );

            return(mappingComposer.FinalText);
        }
Esempio n. 5
0
        private static string ToStringSubstituteCode(string text)
        {
            var textBuilder = new MappingComposer();

            textBuilder
            .SetDelimitedText(text, "|{", "}|")
            .UniqueMarkedSegments
            .TransformByMarkedIndexUsing(index => "|{" + index + "}|");

            if (textBuilder.HasMarkedSegments == false)
            {
                return(text.ValueToQuotedLiteral(true));
            }

            return
                (textBuilder
                 .UniqueMarkedSegments
                 .Select(s => s.InitialText)
                 .Concatenate(
                     ", ",
                     "Ipr.Substitute(" + textBuilder.FinalText.ValueToQuotedLiteral(true) + ", ",
                     ")"
                     ));
        }