private static bool IsInsideSomeStructure(int index, string code)
 {
     var innerStructures = new InnerStructures.InnerStructures();
     for (var i = 0; i < index; i++)
         innerStructures.Handle(code[i]);
     return innerStructures.IsInsideSomeStructure;
 }
        public static IEnumerable<MethodCall> Parse(string code)
        {
            foreach (var match in MethodCallRegex.Matches(code).OfType<Match>())
            {
                var openBracketIndex = match.Groups["open"].Index;
                var parameters = new StringBuilder();
                var innerStructures = new InnerStructures.InnerStructures();
                int j;
                for (j = openBracketIndex; j < code.Length; j++)
                {
                    innerStructures.Handle(code[j]);
                    parameters.Append(code[j]);

                    if (!innerStructures.IsInsideSomeStructure)
                        break;
                }

                var parametersString = parameters.ToString();
                var parametersStringWithoutSurroundingBrackets = parametersString.Substring(1, parametersString.Length - 2);
                var parametersCollection = parametersStringWithoutSurroundingBrackets.SplitByComma().ToArray();

                var methodName = match.Groups["name"].Value;
                var originalText = code.SafeSubstring(match.Index, j - match.Index + 1);
                var methodCall = new MethodCall(methodName, parametersCollection, originalText);

                yield return methodCall;
            }
        }
Example #3
0
        public static IEnumerable <MethodCall> Parse(string code)
        {
            foreach (var match in MethodCallRegex.Matches(code).OfType <Match>())
            {
                var openBracketIndex = match.Groups["open"].Index;
                var parameters       = new StringBuilder();
                var innerStructures  = new InnerStructures.InnerStructures();
                int j;
                for (j = openBracketIndex; j < code.Length; j++)
                {
                    innerStructures.Handle(code[j]);
                    parameters.Append(code[j]);

                    if (!innerStructures.IsInsideSomeStructure)
                    {
                        break;
                    }
                }

                var parametersString = parameters.ToString();
                var parametersStringWithoutSurroundingBrackets = parametersString.Substring(1, parametersString.Length - 2);
                var parametersCollection = parametersStringWithoutSurroundingBrackets.SplitByComma().ToArray();

                var methodName   = match.Groups["name"].Value;
                var originalText = code.SafeSubstring(match.Index, j - match.Index + 1);
                var methodCall   = new MethodCall(methodName, parametersCollection, originalText);

                yield return(methodCall);
            }
        }
Example #4
0
        private static bool IsInsideSomeStructure(int index, string code)
        {
            var innerStructures = new InnerStructures.InnerStructures();

            for (var i = 0; i < index; i++)
            {
                innerStructures.Handle(code[i]);
            }
            return(innerStructures.IsInsideSomeStructure);
        }
        private static string GetParametersPart(string code, int startIndex)
        {
            var result = new StringBuilder();

            var innerStructuresHandler = new InnerStructures.InnerStructures();
            for (var i = startIndex; i < code.Length; i++)
            {
                innerStructuresHandler.Handle(code[i]);

                if (!innerStructuresHandler.IsInsideSomeStructure && code[i] == ';')
                    return result.ToString();

                result.Append(code[i]);
            }

            return result.ToString();
        }
Example #6
0
        private static string GetParametersPart(string code, int startIndex)
        {
            var result = new StringBuilder();

            var innerStructuresHandler = new InnerStructures.InnerStructures();

            for (var i = startIndex; i < code.Length; i++)
            {
                innerStructuresHandler.Handle(code[i]);

                if (!innerStructuresHandler.IsInsideSomeStructure && code[i] == ';')
                {
                    return(result.ToString());
                }

                result.Append(code[i]);
            }

            return(result.ToString());
        }
        public static string RemoveUnusedCode(string template, Words words)
        {
            var result = new StringBuilder();

            var lastIndex = 0;
            foreach (var match in ConditionalRegex.Matches(template).OfType<Match>().ToArray())
            {
                if (match.Index < lastIndex)
                    continue;
                
                var innerStructures = new InnerStructures();
                var currentIndex = template.IndexOf('{', match.Index);
                innerStructures.Handle(template[currentIndex]);
                while (innerStructures.IsInsideSomeStructure)
                {
                    currentIndex++;
                    innerStructures.Handle(template[currentIndex]);
                }
                result.AppendLine(template.SubstringFromTo(lastIndex, match.Index - 1).Trim());
                var wordsGroup = match.Groups["words"].Value;
                var conditionalWords = wordsGroup
                    .SplitByComma()
                    .Select(s => s.Replace("\"", string.Empty))
                    .ToArray();
                
                if (IsRequired(words, conditionalWords))
                {
                    var partToCopy = template.SubstringFromTo(match.Index + match.Length, currentIndex);
                    partToCopy = RemoveUnusedCode(partToCopy, words);
                    result.AppendLine(partToCopy.Trim());
                }
                lastIndex = currentIndex + 1;
            }
            result.AppendLine(template.SubstringFromTo(lastIndex, template.Length - 1).Trim());
            return result.ToString();
        }