diff --git a/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs b/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs index 303987f..acf28b0 100644 --- a/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs +++ b/src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs @@ -40,10 +40,10 @@ public static bool Contains(Type enumType, string value, bool exception = return Enum.GetNames(); } - public static IEnumerable GetNamesExcept(params TEnum [] excludedElements) + public static IEnumerable GetNamesExcept(params TEnum [] excludedElements) where TEnum : struct, Enum { - return GetValueExcept(excludedElements).Select(x=> x.Name()); + return GetValueExcept(excludedElements).Select(x=> x.Name()).OfType(); } public static IEnumerable GetValues() diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs index 5b8135a..c4d643b 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs @@ -1,4 +1,5 @@ using Pidgin; +#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member. namespace SimpleStateMachine.StructuralSearch { @@ -11,9 +12,6 @@ public DebugParser(Parser parser) } public override bool TryParse(ref ParseState state, ref PooledList> expected, out T result) - { - var res = _parser.TryParse(ref state, ref expected, out result); - return res; - } + => _parser.TryParse(ref state, ref expected, out result!); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs index 6820c87..8a4df3c 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs @@ -1,4 +1,5 @@ using Pidgin; +#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member. namespace SimpleStateMachine.StructuralSearch { @@ -11,8 +12,7 @@ public EmptyStringParser(bool value) _value = value; } - public override bool TryParse(ref ParseState state, ref PooledList> expected, - out string result) + public override bool TryParse(ref ParseState state, ref PooledList> expected, out string result) { result = string.Empty; return _value; diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs index 657bc31..08796e3 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs @@ -3,6 +3,7 @@ using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Helper; +#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member. namespace SimpleStateMachine.StructuralSearch { @@ -18,11 +19,8 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded) .Select(Parser.Try)) .AsEnum(ignoreCase); } - - public override bool TryParse(ref ParseState state, ref PooledList> expected, - out TEnum result) - { - return _parser.TryParse(ref state, ref expected, out result); - } + + public override bool TryParse(ref ParseState state, ref PooledList> expected, out TEnum result) + => _parser.TryParse(ref state, ref expected, out result); } } \ No newline at end of file diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs index 3eecf07..62861a6 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs @@ -2,28 +2,27 @@ using System.Collections.Generic; using Pidgin; +#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member. + namespace SimpleStateMachine.StructuralSearch { public abstract class ParserWithLookahead : Parser { - protected Lazy> parser; - - public Func>> OnLookahead { get; set; } + private Lazy>? _lookaheadParser; + protected Lazy> LookaheadParser => _lookaheadParser ?? throw new ArgumentNullException(nameof(Lookahead)); + + public Func>>? OnLookahead { get; protected set; } - public abstract Parser BuildParser(Func?> next, + protected abstract Parser BuildParser(Func?> next, Func?> nextNext); public void Lookahead(Func?> next, Func?> nextNext) { - parser = new Lazy>(() => BuildParser(next, nextNext)); + _lookaheadParser = new Lazy>(() => BuildParser(next, nextNext)); } - public override bool TryParse(ref ParseState state, ref PooledList> expected, - out T result) - { - var res = parser.Value.TryParse(ref state, ref expected, out result); - return res; - } + public override bool TryParse(ref ParseState state, ref PooledList> expected, out T result) + => LookaheadParser.Value.TryParse(ref state, ref expected, out result!); } public class LookaheadResult diff --git a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs index c7721ad..d8d5325 100644 --- a/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs @@ -8,13 +8,15 @@ namespace SimpleStateMachine.StructuralSearch public class PlaceholderParser : ParserWithLookahead, IContextDependent { private readonly string _name; - private IParsingContext _context; + private IParsingContext? _context; + private IParsingContext Context => _context ?? throw new ArgumentNullException(nameof(_context)); + public PlaceholderParser(string name) { _name = name; } - public override Parser BuildParser(Func?> next, + protected override Parser BuildParser(Func?> next, Func?> nextNext) { var nextParser = next(); @@ -58,7 +60,7 @@ public override Parser BuildParser(Func?> nex Parser? term = null; var parenthesised = Parsers.BetweenOneOfChars(x => Parser.Char(x).AsString(), - expr: Parser.Rec(() => term), + expr: Parser.Rec(() => term!), Constant.AllParenthesised).JoinToString(); term = Parser.OneOf(parenthesised, token).Many().JoinToString(); @@ -73,24 +75,23 @@ public override Parser BuildParser(Func?> nex return parser; } - public override bool TryParse(ref ParseState state, ref PooledList> expected, - out string result) + public override bool TryParse(ref ParseState state, ref PooledList> expected, out string result) { bool res; // No use look-ahead if placeholder is already defined - if (_context.TryGetPlaceholder(_name, out var placeholder)) + if (Context.TryGetPlaceholder(_name, out var placeholder)) { - res = Parser.String(placeholder.Value).TryParse(ref state, ref expected, out result); + res = Parser.String(placeholder.Value).TryParse(ref state, ref expected, out result!); } else { - res = parser.Value.Match().TryParse(ref state, ref expected, out var match); + res = LookaheadParser.Value.Match().TryParse(ref state, ref expected, out var match); result = match.Value; if (res) { - _context.AddPlaceholder(new Placeholder( - context: ref _context, + Context.AddPlaceholder(new Placeholder( + context: ref _context!, name: _name, match: match)); } diff --git a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs index 6b072fa..0273710 100644 --- a/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs +++ b/src/SimpleStateMachine.StructuralSearch/ParsingContext.cs @@ -15,20 +15,16 @@ public ParsingContext(IInput input) } public bool TryGetPlaceholder(string name, out IPlaceholder value) - { - return _placeholders.TryGetValue(name, out value); - } + => _placeholders.TryGetValue(name, out value!); public void AddPlaceholder(IPlaceholder placeholder) { _placeholders[placeholder.Name] = placeholder; } - public IPlaceholder GetPlaceholder(string name) - { - return _placeholders[name]; - } - + public IPlaceholder GetPlaceholder(string name) + => _placeholders[name]; + public void Fill(IReadOnlyDictionary placeholders) { ClearInternal(); diff --git a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs index 6f9fe04..b80608c 100644 --- a/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/SeriesParser.cs @@ -2,6 +2,8 @@ using System.Linq; using Pidgin; +#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member. + namespace SimpleStateMachine.StructuralSearch { public class SeriesParser : Parser>, IContextDependent @@ -55,7 +57,7 @@ void SkipLookedParsers(Parser parser, ref ParseState state) lookaheadParser is { OnLookahead: null }) return; - var lookaheadResults = lookaheadParser.OnLookahead.Invoke(); + var lookaheadResults = lookaheadParser.OnLookahead.Invoke().ToArray(); foreach (var result in lookaheadResults) { diff --git a/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj b/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj index b7c1d80..d98eba3 100644 --- a/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj +++ b/src/SimpleStateMachine.StructuralSearch/SimpleStateMachine.StructuralSearch.csproj @@ -4,6 +4,7 @@ net6.0 10 enable + true diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs index 5a6911d..2d4b585 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/FindTemplateParser.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; @@ -10,7 +11,7 @@ static FindTemplateParser() { Parenthesised = Parsers.BetweenOneOfChars(x => ParserToParser.CIChar(x) .Select(x => x.AsString()), - Parser.Rec(() => Term), + Parser.Rec(() => Term ?? throw new ArgumentNullException(nameof(Term))), Constant.AllParenthesised); Term = Parser.OneOf(Parenthesised, Token) diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs index 795ff7e..84de854 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ParametersParser.cs @@ -13,7 +13,7 @@ static ParametersParser() { // TODO optimize Parenthesised = Parsers.BetweenOneOfChars(x => Parser.Char(x).Try(), - Parser.Rec(() => StringWithParenthesised), + Parser.Rec(() => StringWithParenthesised ?? throw new ArgumentNullException(nameof(StringWithParenthesised))), Constant.AllParenthesised) .Try() .Labelled($"{nameof(ParametersParser)}.{nameof(Parenthesised)}"); @@ -32,7 +32,7 @@ static ParametersParser() .Try() .Labelled($"{nameof(ParametersParser)}.{nameof(StringParameter)}"); - var placeholderOrPropertyRuleParameter = Parser.Rec(() => PlaceholderOrPropertyRuleParameter); + var placeholderOrPropertyRuleParameter = Parser.Rec(() => PlaceholderOrPropertyRuleParameter ?? throw new ArgumentNullException(nameof(PlaceholderOrPropertyRuleParameter))); StringFormatParameter = Parser.OneOf(placeholderOrPropertyRuleParameter, StringParameter) @@ -120,8 +120,6 @@ static ParametersParser() public static readonly Parser> Change; - public static readonly Parser PlaceholderOrProperty; - public static readonly Parser PlaceholderOrPropertyRuleParameter; diff --git a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs index 858e7e2..4ad8552 100644 --- a/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs +++ b/src/SimpleStateMachine.StructuralSearch/StructuralSearch/ReplaceTemplateParser.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Pidgin; using SimpleStateMachine.StructuralSearch.Extensions; using SimpleStateMachine.StructuralSearch.Helper; @@ -14,7 +15,7 @@ static ReplaceTemplateParser() { ParenthesisedParameter = Parsers.BetweenOneOfChars((c1, c2) => MatchHelper.GetParenthesisType((c1, c2)), - Parser.Rec(() => Parameter), + Parser.Rec(() => Parameter ?? throw new ArgumentNullException(nameof(Parameter))), Constant.AllParenthesised) .Select(x => new ParenthesisedParameter(x.Item1, new[] { x.Item2 })) .As()