Пример #1
0
        private string GenerateMatchFragmentPartsMultipleMode(MatcherEngineGenerator generator)
        {
            string methodName = $"MatchFragmentPartsMultipleMode{GetSafeMethodName(Name)}";
            string method     = $"{methodName}(ref state, partMatcherData)";

            if (!generator.TryGetMethod(methodName, ref method))
            {
                generator.Add(methodName, method);
                StringBuilder functionText = new StringBuilder();
                for (int partIndex = 0; partIndex < Parts.Count; partIndex++)
                {
                    functionText.AppendLine($@"individualSuccess = {GenerateMatchFragmentPart(generator, Parts[partIndex])};
                    subSuccess |= individualSuccess;
                    if (individualSuccess)
                    {{
                        matchCount++;
                        distinctIndex = state.DistinctIndex;
                        delimiterSuccess = {(PartsDelimiter != null ? string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsDelimiter), "range", PartsDelimiterRequired.ToString().ToLower(), "false") : "true")};
                        goto Break;
                    }}
                    ");
                }

                string code = $@"private bool {methodName}(ref State state, FragmentMatchData matchData)
                {{
                    bool overallSuccess = false;
                    bool subSuccess = false;
                    bool delimiterSuccess = false;
                    StringMatchData range = default;
                    int matchCount = 0;
                    int distinctIndex = state.DistinctIndex;
                    {(PartsPadding != null ? string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsPadding), "_", "false", "false") : null)};

                    do
                    {{
                        subSuccess = false;
                        bool individualSuccess;
                        {functionText.ToString()}

                        Break:
                        overallSuccess |= subSuccess;
                    }}
                    while (subSuccess && delimiterSuccess);
                    if (delimiterSuccess && range != null)
                    {{
                        state.CurrentIndex = range.StartIndex;
                        state.DistinctIndex = distinctIndex;
                    }}
                    {(PartsPadding != null ?
						$@"if (overallSuccess)
                    {{
                        {string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsPadding), "_", "false", "false")};
                    }}" : null)}
            
                    bool thresholdSuccess = {MinMatchedParts ?? 1} <= matchCount;
                    bool success = overallSuccess || thresholdSuccess;
                    if ({(!Negate ? "!" : null)}success)
                    {{
						state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex);
                    }}
                    return success;
                }}";
                method = generator.Add(method, methodName, code);
            }
            return(method);
        }
Пример #2
0
        private string GenerateMatchFragmentPartsOrderedMode(MatcherEngineGenerator generator)
        {
            string methodName = $"MatchFragmentPartsOrderedMode{GetSafeMethodName(Name)}";
            string method     = $"{methodName}(ref state, partMatcherData)";

            if (!generator.TryGetMethod(methodName, ref method))
            {
                generator.Add(methodName, method);
                StringBuilder functionText = new StringBuilder();
                for (int partIndex = 0; partIndex < Parts.Count; partIndex++)
                {
                    functionText.AppendLine($@"{(partIndex > 0 ? $@"distinctIndex = state.DistinctIndex;
                    partSuccess = {(PartsDelimiter != null ? string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsDelimiter), "stringMatchData", PartsDelimiterRequired.ToString().ToLower(), "false") : "true")};
                    success = partSuccess;
                    if (!success)
                    {{
                        goto Break;
                    }}" : null)}

                    success = {GenerateMatchFragmentPart(generator, Parts[partIndex])};
                if (!success)
                {{
                    if (stringMatchData != null)
                    {{
                        state.CurrentIndex = stringMatchData.StartIndex;
                        state.DistinctIndex = distinctIndex;
                    }}
                    goto Break;
                }}
                else
                {{
                    matchCount++;
                }}");
                }

                string code = $@"private bool {methodName}(ref State state, FragmentMatchData matchData)
                {{
                    bool success = true;
                    bool partSuccess;
                    int matchCount = 0;
                    StringMatchData stringMatchData = null;
                    int distinctIndex = state.DistinctIndex;
                    {(PartsPadding != null ? string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsPadding), "_", "false", "false") : null)};

                    {functionText.ToString()}

                    Break:
                    {(PartsPadding != null ?
						$@"if (success)
                    {{
                        {string.Format(generator.LanguageMatcher.GenerateMatchPattern(generator, PartsPadding), "_", "false", "false")};
                    }}" : null)}
            
                    success = success || {(MinMatchedParts ?? Parts.Count)} <= matchCount;
                    if ({(!Negate ? "!" : null)}success)
                    {{
						state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex);
                    }}
                    return success;
                }}";
                method = generator.Add(method, methodName, code);
            }
            return(method);
        }