From f42535d282d6e15f581c17bc0c56c76eb0457562 Mon Sep 17 00:00:00 2001 From: GMIKE Date: Sun, 3 Mar 2024 21:59:45 +0000 Subject: [PATCH] body to expression --- .../Configurations/Configuration.cs | 12 +-- .../Configurations/ConfigurationFile.cs | 18 ++-- .../EmptyParsingContext.cs | 32 +++---- .../Extensions/CharParserExtensions.cs | 24 ++---- .../Extensions/EnumerableCharExtensions.cs | 22 ++--- .../Extensions/EnumerableStringExtensions.cs | 17 ---- .../Extensions/IEnumerableExtensions.cs | 6 +- .../IEnumerableSourceMatchExtensions.cs | 17 ---- .../Extensions/ManyParserExtensions.cs | 6 +- .../Extensions/MatchExtensions.cs | 9 -- .../Extensions/ParserExtensions.cs | 85 +++++++------------ .../Extensions/StringExtensions.cs | 6 +- .../Extensions/StringParserExtensions.cs | 50 +++++------ .../Helper/EnumHelper.cs | 56 +++++------- .../Helper/EscapeHelper.cs | 36 +++----- .../Helper/LogicalHelper.cs | 22 +++-- .../Helper/MatchHelper.cs | 16 ++-- .../IFindParser.cs | 6 +- .../Input/FileInput.cs | 6 +- .../Input/StringInput.cs | 12 +-- .../Parsers/Parsers.cs | 68 +++++---------- .../Parsers/PlaceholderParser.cs | 2 +- .../ParsingContext.cs | 6 +- .../Placeholder/Placeholder.cs | 24 +++--- .../Rules/FindRule/BinaryRule.cs | 6 +- .../Rules/FindRule/BinarySubRule.cs | 6 +- .../Rules/FindRule/EmptyRule.cs | 6 +- .../Rules/FindRule/EmptySubRule.cs | 14 ++- .../Rules/FindRule/InSubRule.cs | 6 +- .../Rules/FindRule/IsSubRule.cs | 6 +- .../Rules/FindRule/UnaryRule.cs | 6 +- .../Parameters/ParenthesisedParameter.cs | 14 ++- .../Parameters/PlaceholderColumnParameter.cs | 6 +- .../Parameters/PlaceholderFileParameter.cs | 6 +- .../Parameters/PlaceholderLenghtParameter.cs | 6 +- .../Parameters/PlaceholderLineParameter.cs | 4 +- .../Parameters/PlaceholderOffsetParameter.cs | 6 +- .../Rules/Parameters/PlaceholderParameter.cs | 20 ++--- .../Rules/Parameters/StringFormatParameter.cs | 6 +- .../Rules/Parameters/StringParameter.cs | 9 +- .../ReplaceRule/ChangeBinaryParameter.cs | 6 +- .../Rules/ReplaceRule/ChangeParameter.cs | 6 +- .../Rules/ReplaceRule/ChangeUnaryParameter.cs | 6 +- .../Rules/ReplaceRule/IReplaceRule.cs | 6 +- .../Rules/ReplaceRule/ReplaceRule.cs | 14 ++- .../Rules/ReplaceRule/ReplaceSubRule.cs | 6 +- .../StructuralSearch/CommonParser.cs | 16 ++-- .../StructuralSearch/FindRulesParser.cs | 6 +- .../StructuralSearch/FindTemplateParser.cs | 4 +- .../StructuralSearch/ReplaceRuleParser.cs | 18 ++-- .../StructuralSearch/ReplaceTemplateParser.cs | 20 ++--- .../StructuralSearchParser.cs | 5 +- .../FindTemplate/ConstantFindTemplate.cs | 6 +- .../Templates/FindTemplate/ParserToParser.cs | 30 +++---- .../ReplaceTemplate/IReplaceBuilder.cs | 6 +- .../ReplaceTemplate/ParserToReplace.cs | 11 +-- .../ReplaceTemplate/PlaceholderReplace.cs | 6 +- .../ReplaceTemplate/ReplaceBuilder.cs | 6 +- .../Templates/ReplaceTemplate/TokenReplace.cs | 14 ++- 59 files changed, 296 insertions(+), 581 deletions(-) delete mode 100644 src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableStringExtensions.cs delete mode 100644 src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableSourceMatchExtensions.cs delete mode 100644 src/SimpleStateMachine.StructuralSearch/Extensions/MatchExtensions.cs diff --git a/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs b/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs index fb9c754..5e5f7d4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs +++ b/src/SimpleStateMachine.StructuralSearch/Configurations/Configuration.cs @@ -20,14 +20,10 @@ public bool Equals(Configuration? other) return findTemplateEquals && findRulesEquals && replaceTemplateEquals && replaceRulesEquals; } - public override bool Equals(object? obj) - { - return obj?.GetType() == GetType() && Equals((Configuration)obj); - } + public override bool Equals(object? obj) + => obj?.GetType() == GetType() && Equals((Configuration)obj); - public override int GetHashCode() - { - return HashCode.Combine(FindTemplate, FindRules, ReplaceTemplate, ReplaceRules); - } + public override int GetHashCode() + => HashCode.Combine(FindTemplate, FindRules, ReplaceTemplate, ReplaceRules); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationFile.cs b/src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationFile.cs index ea9ff98..5e67960 100644 --- a/src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationFile.cs +++ b/src/SimpleStateMachine.StructuralSearch/Configurations/ConfigurationFile.cs @@ -19,19 +19,13 @@ public ConfigurationFile(List configurations) public List Configurations { get; init; } - public bool Equals(ConfigurationFile? other) - { - return other?.Configurations != null && Configurations.SequenceEqual(other.Configurations); - } + public bool Equals(ConfigurationFile? other) + => other?.Configurations != null && Configurations.SequenceEqual(other.Configurations); - public override bool Equals(object? obj) - { - return obj?.GetType() == GetType() && Equals((ConfigurationFile)obj); - } + public override bool Equals(object? obj) + => obj?.GetType() == GetType() && Equals((ConfigurationFile)obj); - public override int GetHashCode() - { - return Configurations.GetHashCode(); - } + public override int GetHashCode() + => Configurations.GetHashCode(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs index 8f99a7a..b244486 100644 --- a/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs @@ -12,33 +12,21 @@ public EmptyParsingContext(IInput input) } public bool TryGetPlaceholder(string name, out IPlaceholder value) - { - throw new System.NotImplementedException(); - } + => throw new System.NotImplementedException(); - public void AddPlaceholder(IPlaceholder placeholder) - { - throw new System.NotImplementedException(); - } + public void AddPlaceholder(IPlaceholder placeholder) + => throw new System.NotImplementedException(); - public IPlaceholder GetPlaceholder(string name) - { - return Placeholder.CreateEmpty(this, name, string.Empty); - } + public IPlaceholder GetPlaceholder(string name) + => Placeholder.CreateEmpty(this, name, string.Empty); - public IReadOnlyDictionary SwitchOnNew() - { - throw new System.NotImplementedException(); - } + public IReadOnlyDictionary SwitchOnNew() + => throw new System.NotImplementedException(); - public void Fill(IReadOnlyDictionary placeholders) - { - throw new System.NotImplementedException(); - } + public void Fill(IReadOnlyDictionary placeholders) + => throw new System.NotImplementedException(); public IReadOnlyDictionary Clear() - { - throw new System.NotImplementedException(); - } + => throw new System.NotImplementedException(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/CharParserExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/CharParserExtensions.cs index 469ab5b..eb5ebd4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/CharParserExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/CharParserExtensions.cs @@ -5,21 +5,13 @@ namespace SimpleStateMachine.StructuralSearch.Extensions { public static class CharParserExtensions { - public static Parser AsString(this Parser parser) - { - return parser.Select(x => x.ToString()); - } - - public static Parser AtLeastOnceAsStringUntil( - this Parser parser, Parser terminator) - { - return parser != null ? parser.AtLeastOnceUntil(terminator).AsString() : throw new ArgumentNullException(nameof (parser)); - } - - public static Parser AtLeastOnceAsStringUntilNot(this Parser parser, - Parser terminator) - { - return parser != null ? parser.AtLeastOnceUntilNot(terminator).AsString() : throw new ArgumentNullException(nameof (parser)); - } + public static Parser AsString(this Parser parser) + => parser.Select(x => x.ToString()); + + public static Parser AtLeastOnceAsStringUntil(this Parser parser, Parser terminator) + => parser != null ? parser.AtLeastOnceUntil(terminator).AsString() : throw new ArgumentNullException(nameof (parser)); + + public static Parser AtLeastOnceAsStringUntilNot(this Parser parser, Parser terminator) + => parser != null ? parser.AtLeastOnceUntilNot(terminator).AsString() : throw new ArgumentNullException(nameof (parser)); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableCharExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableCharExtensions.cs index cd1d113..0bf32fd 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableCharExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableCharExtensions.cs @@ -6,21 +6,13 @@ namespace SimpleStateMachine.StructuralSearch.Extensions { public static class EnumerableCharExtensions { - public static Parser AsString(this Parser> parser) - { - return parser.Select(x => new string(x.ToArray())); - } + public static Parser AsString(this Parser> parser) + => parser.Select(x => new string(x.ToArray())); - public static Parser> MergerMany( - this Parser>> parser) - { - return parser.Select(x => x.SelectMany(y => y)); - } - - public static Parser> MergerMany( - this Parser>> parser) - { - return parser.Select(x => x.SelectMany(y => y)); - } + public static Parser> MergerMany(this Parser>> parser) + => parser.Select(x => x.SelectMany(y => y)); + + public static Parser> MergerMany(this Parser>> parser) + => parser.Select(x => x.SelectMany(y => y)); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableStringExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableStringExtensions.cs deleted file mode 100644 index 912f1a3..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/EnumerableStringExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; - -namespace SimpleStateMachine.StructuralSearch.Extensions -{ - public static class EnumerableStringExtensions - { - public static string JoinToString(this IEnumerable enumerable, string? separator = null) - { - return string.Join(separator, enumerable); - } - - public static string JoinToString(this List enumerable, string? separator = null) - { - return string.Join(separator, enumerable); - } - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableExtensions.cs index 1d32e12..c0445a8 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableExtensions.cs @@ -5,9 +5,7 @@ namespace SimpleStateMachine.StructuralSearch.Extensions { public static class IEnumerableExtensions { - public static IEnumerable EmptyIfNull(this IEnumerable? enumerable) - { - return enumerable ?? Enumerable.Empty(); - } + public static IEnumerable EmptyIfNull(this IEnumerable? enumerable) + => enumerable ?? Enumerable.Empty(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableSourceMatchExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableSourceMatchExtensions.cs deleted file mode 100644 index 6b0ce08..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/IEnumerableSourceMatchExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace SimpleStateMachine.StructuralSearch.Extensions -{ - public static class IEnumerableSourceMatchExtensions - { - public static SourceMatch Concatenate(this IEnumerable matches) - { - var sourceMatches = matches as SourceMatch[] ?? matches.ToArray(); - int start = sourceMatches.First().Start; - int end = sourceMatches.Last().End; - var value = string.Join(string.Empty, sourceMatches.Select(x => x.Value)); - return new SourceMatch(value, start, end); - } - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/ManyParserExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/ManyParserExtensions.cs index 0f4e59c..e2afa77 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/ManyParserExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/ManyParserExtensions.cs @@ -5,9 +5,7 @@ namespace SimpleStateMachine.StructuralSearch.Extensions { public static class ManyParserExtensions { - public static Parser> ToIEnumerable(this Parser> parser) - { - return parser.As, IEnumerable>(); - } + public static Parser> ToIEnumerable(this Parser> parser) + => parser.As, IEnumerable>(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/MatchExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/MatchExtensions.cs deleted file mode 100644 index 6efb91b..0000000 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/MatchExtensions.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace SimpleStateMachine.StructuralSearch.Extensions; - -public static class MatchExtensions -{ - public static bool IsEmpty(this Match match) - { - return string.IsNullOrEmpty(match.Value); - } -} \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs index f63889b..9e4ef78 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/ParserExtensions.cs @@ -7,32 +7,22 @@ namespace SimpleStateMachine.StructuralSearch.Extensions { public static class ParserExtensions { - public static Parser Try(this Parser parser) - { - return Parser.Try(parser); - } - - public static Parser> AsMany(this Parser parser) - { - return parser.Select(x => (IEnumerable)new List { x }); - } + public static Parser Try(this Parser parser) + => Parser.Try(parser); - public static Parser> AtLeastOnceUntilNot(this Parser parser, - Parser terminator) - { - return parser != null + public static Parser> AsMany(this Parser parser) + => parser.Select(x => (IEnumerable)new List { x }); + + public static Parser> AtLeastOnceUntilNot(this Parser parser, Parser terminator) => + parser != null ? parser.AtLeastOnceUntil(Not(terminator)) : throw new ArgumentNullException(nameof(parser)); - } - - public static Parser> UntilNot(this Parser parser, - Parser terminator) - { - return parser != null + + public static Parser> UntilNot(this Parser parser, Parser terminator) => + parser != null ? parser.Until(Not(terminator)) : throw new ArgumentNullException(nameof(parser)); - } - + public static bool TryParse(this Parser parser, string value, out string? result) { if (parser is null) @@ -43,25 +33,20 @@ public static bool TryParse(this Parser parser, string value, out return res.Success; } - public static Parser ThenInvoke(this Parser parser, Action action) - { - return parser.Select(x => + public static Parser ThenInvoke(this Parser parser, Action action) + => parser.Select(x => { action.Invoke(x); return x; }); - } - - public static Parser Contains(this Parser parser) - { - return parser != null + + public static Parser Contains(this Parser parser) + => parser != null ? parser.Optional().Select(x => x.HasValue) : throw new ArgumentNullException(nameof(parser)); - } - - public static Parser> Match(this Parser parser) - { - return Map((oldPos, oldOffset, result, newPos, newOffset) => + + public static Parser> Match(this Parser parser) + => Map((oldPos, oldOffset, result, newPos, newOffset) => { var line = new LinePosition(oldPos.Line, newPos.Line); var column = new ColumnPosition(oldPos.Col, newPos.Col); @@ -72,16 +57,13 @@ public static Parser> Match(this Parser parser) Parser.CurrentPos, Parser.CurrentOffset, parser, Parser.CurrentPos, Parser.CurrentOffset); - } - public static Parser WithDebug(this Parser parser, string label) - { - return Map((u, t, v) => + public static Parser WithDebug(this Parser parser, string label) + => Map((u, t, v) => { Console.WriteLine($"{label} ({t.Col}) : {u} "); return u; }, parser, Parser.CurrentPos, Parser.CurrentSourcePosDelta); - } public static Parser WithDebug(this Parser parser) { @@ -100,24 +82,19 @@ public static Parser BetweenAsThen(this Parser(func, parser1, parser, parser2); + + return Map(func, parser1, parser, parser2); } public static Parser As(this Parser parser) - where T: R - { - return parser.Select(x => (R)x); - } - - public static Parser After(this Parser parser, Parser parserAfter) - { - return parserAfter.Then(parser, (u, t) => t); - } - + where T: R + => parser.Select(x => (R)x); + + public static Parser After(this Parser parser, Parser parserAfter) + => parserAfter.Then(parser, (u, t) => t); + // TODO optimization - public static Parser ParenthesisedOptional(this Parser parser, Func> custom) - { - return OneOf(CommonParser.Parenthesised(parser, custom).Try(), parser); - } + public static Parser ParenthesisedOptional(this Parser parser, Func> custom) + => OneOf(CommonParser.Parenthesised(parser, custom).Try(), parser); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/StringExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/StringExtensions.cs index 0fcdc92..323e3f5 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/StringExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/StringExtensions.cs @@ -5,8 +5,6 @@ namespace SimpleStateMachine.StructuralSearch.Extensions; public static class StringExtensions { - public static MemoryStream AsStream(this string str) - { - return new MemoryStream(Encoding.UTF8.GetBytes(str)); - } + public static MemoryStream AsStream(this string str) + => new(Encoding.UTF8.GetBytes(str)); } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Extensions/StringParserExtensions.cs b/src/SimpleStateMachine.StructuralSearch/Extensions/StringParserExtensions.cs index 5446659..042fdbe 100644 --- a/src/SimpleStateMachine.StructuralSearch/Extensions/StringParserExtensions.cs +++ b/src/SimpleStateMachine.StructuralSearch/Extensions/StringParserExtensions.cs @@ -16,7 +16,7 @@ public static Parser BetweenAsThen(this Parser( + return Map( (before, str, after) => before + str + after, parser1, parser, parser2); } @@ -30,16 +30,14 @@ public static Parser BetweenAsThen(this Parser( + return Map( (before, str, after) => before + str + after, parser1, parser, parser2); } - public static Parser> ToMany(this Parser parser) - { - return parser.Select(x => new List() { x }).ToIEnumerable(); - } - + public static Parser> ToMany(this Parser parser) + => parser.Select(x => new List() { x }).ToIEnumerable(); + public static Parser JoinToString(this Parser> parser, string? separator = null) { separator ??= string.Empty; @@ -52,30 +50,20 @@ public static Parser JoinToString(this Parser string.Join(separator, x)); } - public static Parser AsMatch(this Parser parser) - { - return parser.Then(Parser.CurrentOffset, (s, offset) => new SourceMatch(s, offset - s.Length, offset)); - } - - public static Parser TrimEnd(this Parser parser) - { - return parser.Before(SkipWhitespaces); - } - - public static Parser TrimStart(this Parser parser) - { - return parser.After(SkipWhitespaces); - } - - public static Parser Trim(this Parser parser) - { - return parser.Between(SkipWhitespaces); - } - + public static Parser AsMatch(this Parser parser) + => parser.Then(Parser.CurrentOffset, (s, offset) => new SourceMatch(s, offset - s.Length, offset)); + + public static Parser TrimEnd(this Parser parser) + => parser.Before(SkipWhitespaces); + + public static Parser TrimStart(this Parser parser) + => parser.After(SkipWhitespaces); + + public static Parser Trim(this Parser parser) + => parser.Between(SkipWhitespaces); + public static Parser AsEnum(this Parser parser, bool ignoreCase) - where TEnum: struct, Enum - { - return parser.Select(value => System.Enum.Parse(value, ignoreCase)); - } + where TEnum: struct, Enum + => parser.Select(value => System.Enum.Parse(value, ignoreCase)); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs index acf28b0..ac2fcfc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs @@ -6,10 +6,8 @@ namespace SimpleStateMachine.StructuralSearch.Helper { public static class EnumHelper { - public static bool IsEnumType(Type? enumType) - { - return enumType is { IsEnum: true }; - } + public static bool IsEnumType(Type? enumType) + => enumType is { IsEnum: true }; public static bool Contains(Type enumType, string value, bool exception = true) where TEnum : struct, Enum @@ -17,47 +15,34 @@ public static bool Contains(Type enumType, string value, bool exception = if (string.IsNullOrEmpty(value)) return false; - if ((!exception) && (!IsEnumType(enumType))) + if (!exception && !IsEnumType(enumType)) return false; return Enum.IsDefined(enumType, value); } public static string? GetName(TEnum value) - where TEnum : struct, Enum - { - return Enum.GetName(value); - } + where TEnum : struct, Enum + => Enum.GetName(value); - public static string? Name(this Enum enumType) - { - return Enum.GetName(enumType.GetType(), enumType); - } + public static string? Name(this Enum enumType) + => Enum.GetName(enumType.GetType(), enumType); public static IEnumerable? GetNames() - where TEnum : struct, Enum - { - return Enum.GetNames(); - } - + where TEnum : struct, Enum + => Enum.GetNames(); + public static IEnumerable GetNamesExcept(params TEnum [] excludedElements) - where TEnum : struct, Enum - { - return GetValueExcept(excludedElements).Select(x=> x.Name()).OfType(); - } + where TEnum : struct, Enum + => GetValueExcept(excludedElements).Select(x=> x.Name()).OfType(); public static IEnumerable GetValues() - where TEnum : struct, Enum - { - return Enum.GetValues(); - } - - public static IEnumerable GetValueExcept(params TEnum [] excludedElements) - where TEnum : struct, Enum - { - return GetValues().Where(x => !excludedElements.Contains(x)); - } + where TEnum : struct, Enum + => Enum.GetValues(); + public static IEnumerable GetValueExcept(params TEnum [] excludedElements) + where TEnum : struct, Enum + => GetValues().Where(x => !excludedElements.Contains(x)); public static TEnum GetValue(string value, bool ignoreCase = true) where TEnum : struct, Enum @@ -66,11 +51,8 @@ public static TEnum GetValue(string value, bool ignoreCase = true) return result; } - public static bool TryGetValue(string value, bool ignoreCase, out TEnum result) - where TEnum : struct, Enum - { - return Enum.TryParse(value, ignoreCase, out result); - } + where TEnum : struct, Enum + => Enum.TryParse(value, ignoreCase, out result); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs index e1c0fc4..0e6a31f 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/EscapeHelper.cs @@ -5,34 +5,22 @@ namespace SimpleStateMachine.StructuralSearch.Helper { public static class EscapeHelper { - public static string Escape(string str, Func replaceRule) - { - return new string(str.Select(replaceRule).ToArray()); - } + public static string Escape(string str, Func replaceRule) + => new(str.Select(replaceRule).ToArray()); - public static string EscapeChars(string str, Func replaceRule, params char[] filter) - { - return new string(str.Select(c => filter.Contains(c) ? replaceRule(c) : c).ToArray()); - } + public static string EscapeChars(string str, Func replaceRule, params char[] filter) + => new(str.Select(c => filter.Contains(c) ? replaceRule(c) : c).ToArray()); - public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) - { - return new string(str.Select(c => excluded.Contains(c) ? c : replaceRule(c)).ToArray()); - } + public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) + => new(str.Select(c => excluded.Contains(c) ? c : replaceRule(c)).ToArray()); - public static string Escape(string str, Func replaceRule) - { - return string.Join(string.Empty, str.Select(replaceRule)); - } + public static string Escape(string str, Func replaceRule) + => string.Join(string.Empty, str.Select(replaceRule)); - public static string EscapeChars(string str, Func replaceRule, params char[] filter) - { - return string.Join(string.Empty, str.Select(c => filter.Contains(c) ? replaceRule(c) : c.ToString())); - } + public static string EscapeChars(string str, Func replaceRule, params char[] filter) + => string.Join(string.Empty, str.Select(c => filter.Contains(c) ? replaceRule(c) : c.ToString())); - public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) - { - return string.Join(string.Empty, str.Select(c => excluded.Contains(c) ? c.ToString() : replaceRule(c))); - } + public static string EscapeExclude(string str, Func replaceRule, params char[] excluded) + => string.Join(string.Empty, str.Select(c => excluded.Contains(c) ? c.ToString() : replaceRule(c))); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/LogicalHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/LogicalHelper.cs index 2cd92d5..b514359 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/LogicalHelper.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/LogicalHelper.cs @@ -5,18 +5,16 @@ namespace SimpleStateMachine.StructuralSearch.Helper { public static class LogicalHelper { - public static bool Calculate(BinaryRuleType type, bool left, bool right) + public static bool Calculate(BinaryRuleType type, bool left, bool right) + => type switch { - return type switch - { - BinaryRuleType.And => left && right, - BinaryRuleType.Or => left || right, - BinaryRuleType.NAND => !(left && right), - BinaryRuleType.NOR => !(left || right), - BinaryRuleType.XOR => left ^ right, - BinaryRuleType.XNOR => left == right, - _ => throw new ArgumentOutOfRangeException(nameof(type), type, null) - }; - } + BinaryRuleType.And => left && right, + BinaryRuleType.Or => left || right, + BinaryRuleType.NAND => !(left && right), + BinaryRuleType.NOR => !(left || right), + BinaryRuleType.XOR => left ^ right, + BinaryRuleType.XNOR => left == right, + _ => throw new ArgumentOutOfRangeException(nameof(type), type, null) + }; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/MatchHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/MatchHelper.cs index fe56165..9fca7c5 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/MatchHelper.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/MatchHelper.cs @@ -6,15 +6,11 @@ namespace SimpleStateMachine.StructuralSearch.Helper; public static class MatchHelper { public static ParenthesisType GetParenthesisType((char c1, char c2) parenthesis) + => parenthesis switch { - var type = parenthesis switch - { - (Constant.LeftParenthesis, Constant.RightParenthesis) => ParenthesisType.Usual, - (Constant.LeftSquareParenthesis, Constant.RightSquareParenthesis) => ParenthesisType.Square, - (Constant.LeftCurlyParenthesis, Constant.RightCurlyParenthesis) => ParenthesisType.Curly, - _ => throw new ArgumentOutOfRangeException(nameof(parenthesis), parenthesis, null) - }; - - return type; - } + (Constant.LeftParenthesis, Constant.RightParenthesis) => ParenthesisType.Usual, + (Constant.LeftSquareParenthesis, Constant.RightSquareParenthesis) => ParenthesisType.Square, + (Constant.LeftCurlyParenthesis, Constant.RightCurlyParenthesis) => ParenthesisType.Curly, + _ => throw new ArgumentOutOfRangeException(nameof(parenthesis), parenthesis, null) + }; } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs index bbd858c..ff404b9 100644 --- a/src/SimpleStateMachine.StructuralSearch/IFindParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/IFindParser.cs @@ -10,9 +10,7 @@ public interface IFindParser public class EmptyFindParser: IFindParser { - public IEnumerable Parse(ref IParsingContext context) - { - return Array.Empty(); - } + public IEnumerable Parse(ref IParsingContext context) + => Array.Empty(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs index 63384e5..05fc96b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs @@ -12,10 +12,8 @@ public FileInput(FileInfo fileInfo) FileInfo = fileInfo; } - public Result ParseBy(Parser parser) - { - return parser.Parse(FileInfo.OpenText()); - } + public Result ParseBy(Parser parser) + => parser.Parse(FileInfo.OpenText()); public void Replace(Match match, string value) { diff --git a/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs index 7273124..b1928fc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs +++ b/src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs @@ -11,15 +11,11 @@ public StringInput(string input) _input = input; } - public Result ParseBy(Parser parser) - { - return parser.Parse(_input); - } + public Result ParseBy(Parser parser) + => parser.Parse(_input); - public void Replace(Match match, string value) - { - throw new System.NotImplementedException(); - } + public void Replace(Match match, string value) + => throw new System.NotImplementedException(); public string Extension => string.Empty; public string Path => string.Empty; diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs index 1b436ce..95ba14a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/Parsers.cs @@ -9,17 +9,13 @@ namespace SimpleStateMachine.StructuralSearch { public static class Parsers { - public static Parser String(string value, bool ignoreCase) - { - return ignoreCase ? Parser.CIString(value): Parser.String(value); - } + public static Parser String(string value, bool ignoreCase) + => ignoreCase ? CIString(value): Parser.String(value); public static Parser After(TEnum value, bool ignoreCase = false) - where TEnum : struct, Enum - { - return Parsers.String(value.ToString(), ignoreCase).AsEnum(ignoreCase); - } - + where TEnum : struct, Enum + => String(value.ToString(), ignoreCase).AsEnum(ignoreCase); + public static Parser> MapToMany(Parser parser1, Parser parser2, Parser parser3) { @@ -30,8 +26,7 @@ public static Parser> MapToMany(Parser (IEnumerable)new List { arg1, arg2, arg3 }, parser1, parser2, - parser3); + return Map((arg1, arg2, arg3) => (IEnumerable)new List { arg1, arg2, arg3 }, parser1, parser2, parser3); } public static Parser> MapToMany(Parser parser1, @@ -74,45 +69,25 @@ public static Parser> BetweenChars(char left, char right => MapToMany(leftRight(left), expr, leftRight(right)); - public static Parser> BetweenOneOfChars(Func> leftRight, - Parser> expr, params (char, char)[] values) - { - return OneOf(values.Select(x => - MapToMany(leftRight(x.Item1), expr, leftRight(x.Item2))) - ); - } - - public static Parser> BetweenOneOfChars(Func> leftRight, - Parser expr, params (char, char)[] values) - { - return OneOf(values.Select(x => - MapToMany(leftRight(x.Item1), expr, leftRight(x.Item2))) - ); - } - - public static Parser BetweenOneOfChars(Func resultFunc, - Parser expr, params (char, char)[] values) - { - return OneOf(values.Select(x => - Map((c1, res, c2) => (resultFunc(c1, c2), res), - Char(x.Item1).Try(), expr, Char(x.Item2).Try()))); - } + public static Parser> BetweenOneOfChars(Func> leftRight, Parser> expr, params (char, char)[] values) + => OneOf(values.Select(x => MapToMany(leftRight(x.Item1), expr, leftRight(x.Item2)))); + + public static Parser> BetweenOneOfChars(Func> leftRight, Parser expr, params (char, char)[] values) + => OneOf(values.Select(x => MapToMany(leftRight(x.Item1), expr, leftRight(x.Item2)))); + + public static Parser BetweenOneOfChars(Func resultFunc, Parser expr, params (char, char)[] values) + => OneOf(values.Select(x => Map((c1, res, c2) => (resultFunc(c1, c2), res), Char(x.Item1).Try(), expr, Char(x.Item2).Try()))); public static Parser EnumExcept(bool ignoreCase = false, params TEnum[] excluded) - where TEnum : struct, Enum - { - return new EnumParser(ignoreCase, excluded); - } + where TEnum : struct, Enum + => new EnumParser(ignoreCase, excluded); public static Parser EnumValue(TEnum value, bool ignoreCase = false) - where TEnum : struct, Enum - { - return Parsers.String(value.ToString(), ignoreCase).AsEnum(ignoreCase); - } - - public static Parser> Match(Parser parser) - { - return Map((oldPos, oldOffset, result, newPos, newOffset) => + where TEnum : struct, Enum + => String(value.ToString(), ignoreCase).AsEnum(ignoreCase); + + public static Parser> Match(Parser parser) + => Map((oldPos, oldOffset, result, newPos, newOffset) => { var line = new LinePosition(oldPos.Line, newPos.Line); var column = new ColumnPosition(oldPos.Col, newPos.Col); @@ -123,6 +98,5 @@ public static Parser> Match(Parser parser) Parser.CurrentPos, Parser.CurrentOffset, parser, Parser.CurrentPos, Parser.CurrentOffset); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs index d8d5325..35bed3e 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs @@ -60,7 +60,7 @@ protected override Parser BuildParser(Func?> Parser? term = null; var parenthesised = Parsers.BetweenOneOfChars(x => Parser.Char(x).AsString(), - expr: Parser.Rec(() => term!), + expr: Parser.Rec(() => term ?? throw new ArgumentNullException(nameof(term))), Constant.AllParenthesised).JoinToString(); term = Parser.OneOf(parenthesised, token).Many().JoinToString(); diff --git a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs index 0273710..b3ecd60 100644 --- a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs @@ -46,9 +46,7 @@ public IReadOnlyDictionary Clear() return placeholders; } - private void ClearInternal() - { - _placeholders.Clear();; - } + private void ClearInternal() + => _placeholders.Clear(); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs b/src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs index cd17d41..9d34190 100644 --- a/src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs +++ b/src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs @@ -20,16 +20,18 @@ public Placeholder(ref IParsingContext context, string name, Match match public IInput Input => _context.Input; public static Placeholder CreateEmpty(IParsingContext context, string name, string value) - { - return new Placeholder( - context: ref context, - name: name, - new Match( - value, - value.Length, - ColumnPosition.Empty, - LinePosition.Empty, - OffsetPosition.Empty)); - } + => new + ( + context: ref context, + name: name, + match: new Match + ( + Value: value, + Lenght: value.Length, + Column: ColumnPosition.Empty, + Line: LinePosition.Empty, + Offset: OffsetPosition.Empty + ) + ); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs index b17b4a6..112e5f4 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinaryRule.cs @@ -23,9 +23,7 @@ public bool Execute(ref IParsingContext context) return LogicalHelper.Calculate(_type, left, right); } - public override string ToString() - { - return $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; - } + public override string ToString() + => $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs index 8685c41..5bc9e4b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/BinarySubRule.cs @@ -33,9 +33,7 @@ public bool Execute(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; - } + public override string ToString() + => $"{_left}{Constant.Space}{_type}{Constant.Space}{_right}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs index 01712e8..edf0cf2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptyRule.cs @@ -2,10 +2,8 @@ public class EmptyRule: IRule { - public bool Execute(ref IParsingContext context) - { - return true; - } + public bool Execute(ref IParsingContext context) + => true; } public static class Rule diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs index ae0fcb3..261ad4f 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs @@ -2,13 +2,9 @@ public class EmptySubRule : IRule { - public bool Execute(ref IParsingContext context) - { - return true; - } - - public override string ToString() - { - return $"{Constant.Underscore}"; - } + public bool Execute(ref IParsingContext context) + => true; + + public override string ToString() + => $"{Constant.Underscore}"; } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs index 19000c8..f2b556b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/InSubRule.cs @@ -31,9 +31,7 @@ public bool Execute(ref IParsingContext context) return false; } - public override string ToString() - { - return $"{_parameter}{Constant.Space}{SubRuleType.In}{Constant.Space}{string.Join(Constant.Comma, _arguments.Select(x=>x.ToString()))}"; - } + public override string ToString() + => $"{_parameter}{Constant.Space}{SubRuleType.In}{Constant.Space}{string.Join(Constant.Comma, _arguments.Select(x=>x.ToString()))}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs index bbee62e..a504650 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/IsSubRule.cs @@ -30,9 +30,7 @@ public bool Execute(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_parameter}{Constant.Space}{SubRuleType.Is}{Constant.Space}{_argument}"; - } + public override string ToString() + => $"{_parameter}{Constant.Space}{SubRuleType.Is}{Constant.Space}{_argument}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs index 5fdbf33..40aa4e5 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/FindRule/UnaryRule.cs @@ -25,9 +25,7 @@ public bool Execute(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_type}{Constant.Space}{_parameter}"; - } + public override string ToString() + => $"{_type}{Constant.Space}{_parameter}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs index 4e74c12..6e4a096 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/ParenthesisedParameter.cs @@ -27,19 +27,15 @@ public string GetValue(ref IParsingContext context) return string.Format(_template, string.Join(string.Empty, values)); } - public override string ToString() - { - return string.Format(_template, string.Join(string.Empty, _parameters.Select(x=> x.ToString()))); - } - - private static string GetTemplate(ParenthesisType parenthesisType) - { - return parenthesisType switch + public override string ToString() + => string.Format(_template, string.Join(string.Empty, _parameters.Select(x=> x.ToString()))); + + private static string GetTemplate(ParenthesisType parenthesisType) + => parenthesisType switch { ParenthesisType.Usual => "({0})", ParenthesisType.Square => "[{0}]", ParenthesisType.Curly => "{{0}}", _ => throw new ArgumentOutOfRangeException(nameof(parenthesisType), parenthesisType, null) }; - } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs index 42371b3..9cd0f4c 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderColumnParameter.cs @@ -29,9 +29,7 @@ public string GetValue(ref IParsingContext context) return value.ToString(); } - public override string ToString() - { - return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Column}{Constant.Dote}{_property}"; - } + public override string ToString() + => $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Column}{Constant.Dote}{_property}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs index 7389101..7095323 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderFileParameter.cs @@ -30,9 +30,7 @@ public string GetValue(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.File}{Constant.Dote}{_property}"; - } + public override string ToString() + => $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.File}{Constant.Dote}{_property}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs index c5e1a97..d5f6c2d 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLenghtParameter.cs @@ -17,9 +17,7 @@ public string GetValue(ref IParsingContext context) return placeHolder.Lenght.ToString(); } - public override string ToString() - { - return $"{_placeholderParameter}{Constant.Dote}{_property}"; - } + public override string ToString() + => $"{_placeholderParameter}{Constant.Dote}{_property}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs index abe4f1d..5626d84 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderLineParameter.cs @@ -30,8 +30,6 @@ public string GetValue(ref IParsingContext context) } public override string ToString() - { - return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Line}{Constant.Dote}{_property}"; - } + => $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Line}{Constant.Dote}{_property}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs index 0c404d6..232a771 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderOffsetParameter.cs @@ -28,9 +28,7 @@ public string GetValue(ref IParsingContext context) return value.ToString(); } - public override string ToString() - { - return $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Offset}{Constant.Dote}{_property}"; - } + public override string ToString() + => $"{_placeholderParameter}{Constant.Dote}{PlaceholderProperty.Offset}{Constant.Dote}{_property}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs index 10a0862..346ed22 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderParameter.cs @@ -9,19 +9,13 @@ public PlaceholderParameter(string name) public string Name { get; } - public string GetValue(ref IParsingContext context) - { - return context.GetPlaceholder(Name).Value; - } - + public string GetValue(ref IParsingContext context) + => context.GetPlaceholder(Name).Value; + public IPlaceholder GetPlaceholder(ref IParsingContext context) - { - return context.GetPlaceholder(Name); - } - - public override string ToString() - { - return $"{Constant.PlaceholderSeparator}{Name}{Constant.PlaceholderSeparator}"; - } + => context.GetPlaceholder(Name); + + public override string ToString() + => $"{Constant.PlaceholderSeparator}{Name}{Constant.PlaceholderSeparator}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs index 2f93adf..f4537eb 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringFormatParameter.cs @@ -23,9 +23,7 @@ public string GetValue(ref IParsingContext context) return string.Join(string.Empty, values); } - public override string ToString() - { - return $"{Constant.DoubleQuotes}{string.Join(string.Empty, _parameters.Select(x=> x.ToString()))}{Constant.DoubleQuotes}"; - } + public override string ToString() + => $"{Constant.DoubleQuotes}{string.Join(string.Empty, _parameters.Select(x=> x.ToString()))}{Constant.DoubleQuotes}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs index b41ddb1..983409c 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/Parameters/StringParameter.cs @@ -10,15 +10,12 @@ public StringParameter(string value) { _value = value; } - public string GetValue(ref IParsingContext context) - { - return _value; - } - + public string GetValue(ref IParsingContext context) + => _value; + public override string ToString() { var value = EscapeHelper.EscapeChars(_value, c => $"{Constant.BackSlash}{c}", Constant.Parameter.Escape); - return $"{value}"; } } diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs index 3f036b4..25aa22d 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeBinaryParameter.cs @@ -29,8 +29,6 @@ public string GetValue(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_parameter}{Constant.Dote}{_type}"; - } + public override string ToString() + => $"{_parameter}{Constant.Dote}{_type}"; } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs index 3a4c1c2..d63f2ca 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeParameter.cs @@ -29,9 +29,7 @@ public string GetValue(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_parameter}{Constant.Dote}{_type}"; - } + public override string ToString() + => $"{_parameter}{Constant.Dote}{_type}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs index 3a0548b..42e7e2a 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ChangeUnaryParameter.cs @@ -28,8 +28,6 @@ public string GetValue(ref IParsingContext context) }; } - public override string ToString() - { - return $"{_parameter}{Constant.Dote}{_type}{Constant.LeftParenthesis}{_arg}{Constant.RightParenthesis}"; - } + public override string ToString() + => $"{_parameter}{Constant.Dote}{_type}{Constant.LeftParenthesis}{_arg}{Constant.RightParenthesis}"; } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs index e6e9708..92380fc 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs @@ -14,8 +14,6 @@ public class EmptyReplaceRule : IReplaceRule { public IEnumerable Rules { get; } = Array.Empty(); - public bool IsMatch(ref IParsingContext context) - { - return Rule.Empty.Execute(ref context); - } + public bool IsMatch(ref IParsingContext context) + => Rule.Empty.Execute(ref context); } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs index 1cc9451..8d129b2 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceRule.cs @@ -16,14 +16,10 @@ public ReplaceRule(IRule conditionRule, IEnumerable rules) Rules = rules; } - public bool IsMatch(ref IParsingContext context) - { - return ConditionRule.Execute(ref context); - } - - public override string ToString() - { - return $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; - } + public bool IsMatch(ref IParsingContext context) + => ConditionRule.Execute(ref context); + + public override string ToString() + => $"{ConditionRule}{Constant.Space}{Constant.Then}{Constant.Space}{string.Join(Constant.Comma, Rules)}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs index ed3c15a..1afe281 100644 --- a/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs +++ b/src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/ReplaceSubRule.cs @@ -13,8 +13,6 @@ public ReplaceSubRule(PlaceholderParameter placeholder, IRuleParameter parameter Parameter = parameter; } - public override string ToString() - { - return $"{Placeholder}{Constant.Space}{Constant.Should}{Constant.Space}{Parameter}"; - } + public override string ToString() + => $"{Placeholder}{Constant.Space}{Constant.Should}{Constant.Space}{Parameter}"; } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs index 0788510..3e42e1e 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/CommonParser.cs @@ -52,17 +52,13 @@ internal static readonly Parser Underscore = Char(Constant.Underscore); internal static Parser Parenthesised(Parser parser, Func> custom) - { - return parser.Between - ( - custom(Constant.LeftParenthesis), - custom(Constant.RightParenthesis) - ); - } + => parser.Between + ( + custom(Constant.LeftParenthesis), + custom(Constant.RightParenthesis) + ); internal static Parser Escaped(params char [] chars) - { - return Char(Constant.BackSlash).Then(OneOf(chars)); - } + => Char(Constant.BackSlash).Then(OneOf(chars)); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs index 174bc4a..50aa87c 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindRulesParser.cs @@ -75,11 +75,9 @@ internal static readonly Parser> Not ) ); - internal static IRule ParseTemplate(string? str) - { - return string.IsNullOrEmpty(str) + internal static IRule ParseTemplate(string? str) + => string.IsNullOrEmpty(str) ? Rule.Empty : Expr.Before(CommonParser.EOF).ParseOrThrow(str); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs index 2d4b585..1194340 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs @@ -55,10 +55,8 @@ static FindTemplateParser() private static readonly Parser SeriesParser; internal static IFindParser ParseTemplate(string? str) - { - return string.IsNullOrEmpty(str) + => string.IsNullOrEmpty(str) ? FindParser.Empty : SeriesParser.Select(parser => new FindParser(parser)).ParseOrThrow(str); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs index bb72c15..ee3db5d 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceRuleParser.cs @@ -6,12 +6,12 @@ namespace SimpleStateMachine.StructuralSearch { public static class ReplaceRuleParser { - internal static readonly Parser Then = + private static readonly Parser Then = Parser.CIString(Constant.Then) .Try() .TrimStart(); - - internal static readonly Parser ReplaceSubRule = + + private static readonly Parser ReplaceSubRule = Parser.Map((placeholder, _, parameter) => new ReplaceSubRule(placeholder, parameter), ParametersParser.PlaceholderParameter.TrimStart(), CommonTemplateParser.Should.TrimStart(), @@ -19,24 +19,22 @@ public static class ReplaceRuleParser .Try() .TrimStart(); - internal static readonly Parser EmptySubRule = + private static readonly Parser EmptySubRule = CommonParser.Underscore.ThenReturn(new EmptySubRule()) .As() .Try() .TrimStart(); - - internal static readonly Parser ReplaceRule = + + private static readonly Parser ReplaceRule = Parser.Map((rule, subRules) => new ReplaceRule(rule, subRules), Parser.OneOf(EmptySubRule, FindRuleParser.Expr), Then.Then(ReplaceSubRule.SeparatedAtLeastOnce(CommonParser.Comma))) .Try() .TrimStart(); - internal static IReplaceRule ParseTemplate(string? str) - { - return string.IsNullOrEmpty(str) + internal static IReplaceRule ParseTemplate(string? str) + => string.IsNullOrEmpty(str) ? SimpleStateMachine.StructuralSearch.ReplaceRule.Empty : ReplaceRule.Before(CommonParser.EOF).ParseOrThrow(str); - } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs index 4ad8552..792ffbc 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs @@ -41,20 +41,18 @@ static ReplaceTemplateParser() .MergerMany() .Try(); } - - public static readonly Parser ParenthesisedParameter; + + private static readonly Parser ParenthesisedParameter; public static readonly Parser Parameter; - public static readonly Parser> Parenthesised; - public static readonly Parser> ParenthesisedParameters; - public static readonly Parser> Parameters; + private static readonly Parser> Parenthesised; + private static readonly Parser> ParenthesisedParameters; + private static readonly Parser> Parameters; - internal static IReplaceBuilder ParseTemplate(string? str) - { - return string.IsNullOrEmpty(str) + internal static IReplaceBuilder ParseTemplate(string? str) + => string.IsNullOrEmpty(str) ? ReplaceBuilder.Empty : Parameters - .Select(steps => new ReplaceBuilder(steps)) - .ParseOrThrow(str); - } + .Select(steps => new ReplaceBuilder(steps)) + .ParseOrThrow(str); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs index aa2b7ea..8bafc06 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearchParser.cs @@ -32,10 +32,7 @@ public StructuralSearchParser(Configuration configuration) } public IEnumerable Parse(ref IParsingContext context) - { - var matches = _findParser.Parse(ref context); - return matches; - } + => _findParser.Parse(ref context); public IEnumerable ApplyFindRule(ref IParsingContext context, IEnumerable matches) { diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs index 19d6f4a..cdeb0bd 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ConstantFindTemplate.cs @@ -15,10 +15,8 @@ public static class FindTemplate Space ); - public static char[] AllExclude(params char[] excluded) - { - return All.Where(x => !excluded.Contains(x)).ToArray(); - } + public static char[] AllExclude(params char[] excluded) + => All.Where(x => !excluded.Contains(x)).ToArray(); } public static class Parameter diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs index 1f63736..6337fc1 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/FindTemplate/ParserToParser.cs @@ -5,24 +5,16 @@ namespace SimpleStateMachine.StructuralSearch { public static class ParserToParser { - public static Parser> CIChar(char token) - { - return Parser.CIChar(token).Select(Parser.CIChar); - } - - public static Parser> ResultAsParser(Parser parser, bool ignoreCase = false) - { - return parser.Select(value => Parsers.String(value, ignoreCase)); - } - - public static Parser> ParserAsParser(Parser parser) - { - return parser.Select(value => parser); - } - - public static Parser> ResultAsMatch(Parser parser, bool ignoreCase = false) - { - return parser.Select(x=> Parsers.String(x, ignoreCase).AsMatch()); - } + public static Parser> CIChar(char token) + => Parser.CIChar(token).Select(Parser.CIChar); + + public static Parser> ResultAsParser(Parser parser, bool ignoreCase = false) + => parser.Select(value => Parsers.String(value, ignoreCase)); + + public static Parser> ParserAsParser(Parser parser) + => parser.Select(value => parser); + + public static Parser> ResultAsMatch(Parser parser, bool ignoreCase = false) + => parser.Select(x=> Parsers.String(x, ignoreCase).AsMatch()); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs index 0ea12a7..7134068 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/IReplaceBuilder.cs @@ -14,9 +14,7 @@ public class EmptyReplaceBuilder: IReplaceBuilder { public IEnumerable Steps { get; } = Array.Empty(); - public string Build(ref IParsingContext context) - { - return string.Empty; - } + public string Build(ref IParsingContext context) + => string.Empty; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs index 63c8d91..f1f7b49 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ParserToReplace.cs @@ -6,16 +6,13 @@ namespace SimpleStateMachine.StructuralSearch.ReplaceTemplate public static class ParserToReplace { public static Parser ResultAsReplace(Parser parser) - { - return parser.Select(result => new TokenReplace(result)) + => parser.Select(result => new TokenReplace(result)) .As(); - } - + public static Parser Stringc(char token, bool ignoreCase = false) { - var _token = token.ToString(); - - return ResultAsReplace(Parsers.String(_token, ignoreCase)); + var tokenStr = token.ToString(); + return ResultAsReplace(Parsers.String(tokenStr, ignoreCase)); } } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs index ee7eabe..cbc773e 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/PlaceholderReplace.cs @@ -15,9 +15,7 @@ public string GetValue(ref IParsingContext context) return placeHolder.Value; } - public override string ToString() - { - return $"{Constant.PlaceholderSeparator}{_name}{Constant.PlaceholderSeparator}"; - } + public override string ToString() + => $"{Constant.PlaceholderSeparator}{_name}{Constant.PlaceholderSeparator}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs index 645327d..c24dc98 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/ReplaceBuilder.cs @@ -28,9 +28,7 @@ public string Build(ref IParsingContext context) return result; } - public override string ToString() - { - return $"{string.Join(string.Empty, Steps)}"; - } + public override string ToString() + => $"{string.Join(string.Empty, Steps)}"; } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs index c335e4e..05dfa3b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs +++ b/src/SimpleStateMachine.StructuralSearch/Templates/ReplaceTemplate/TokenReplace.cs @@ -9,14 +9,10 @@ public TokenReplace(string token) _token = token; } - public string GetValue(ref IParsingContext context) - { - return _token; - } - - public override string ToString() - { - return $"{_token}"; - } + public string GetValue(ref IParsingContext context) + => _token; + + public override string ToString() + => $"{_token}"; } } \ No newline at end of file