Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SimpleStateMachine.StructuralSearch/Helper/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public static bool Contains<TEnum>(Type enumType, string value, bool exception =
return Enum.GetNames<TEnum>();
}

public static IEnumerable<string?> GetNamesExcept<TEnum>(params TEnum [] excludedElements)
public static IEnumerable<string> GetNamesExcept<TEnum>(params TEnum [] excludedElements)
where TEnum : struct, Enum
{
return GetValueExcept(excludedElements).Select(x=> x.Name());
return GetValueExcept(excludedElements).Select(x=> x.Name()).OfType<string>();
}

public static IEnumerable<TEnum> GetValues<TEnum>()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -11,9 +12,6 @@ public DebugParser(Parser<TToken, T> parser)
}

public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected, out T result)
{
var res = _parser.TryParse(ref state, ref expected, out result);
return res;
}
=> _parser.TryParse(ref state, ref expected, out result!);
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -11,8 +12,7 @@ public EmptyStringParser(bool value)
_value = value;
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
out string result)
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected, out string result)
{
result = string.Empty;
return _value;
Expand Down
10 changes: 4 additions & 6 deletions src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,11 +19,8 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded)
.Select(Parser.Try))
.AsEnum<TEnum>(ignoreCase);
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
out TEnum result)
{
return _parser.TryParse(ref state, ref expected, out result);
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected, out TEnum result)
=> _parser.TryParse(ref state, ref expected, out result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TToken, T> : Parser<TToken, T>
{
protected Lazy<Parser<TToken, T>> parser;

public Func<IEnumerable<LookaheadResult<TToken, T>>> OnLookahead { get; set; }
private Lazy<Parser<TToken, T>>? _lookaheadParser;
protected Lazy<Parser<TToken, T>> LookaheadParser => _lookaheadParser ?? throw new ArgumentNullException(nameof(Lookahead));

public Func<IEnumerable<LookaheadResult<TToken, T>>>? OnLookahead { get; protected set; }

public abstract Parser<TToken, T> BuildParser(Func<Parser<TToken, T>?> next,
protected abstract Parser<TToken, T> BuildParser(Func<Parser<TToken, T>?> next,
Func<Parser<TToken, T>?> nextNext);

public void Lookahead(Func<Parser<TToken, T>?> next, Func<Parser<TToken, T>?> nextNext)
{
parser = new Lazy<Parser<TToken, T>>(() => BuildParser(next, nextNext));
_lookaheadParser = new Lazy<Parser<TToken, T>>(() => BuildParser(next, nextNext));
}

public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected,
out T result)
{
var res = parser.Value.TryParse(ref state, ref expected, out result);
return res;
}
public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected, out T result)
=> LookaheadParser.Value.TryParse(ref state, ref expected, out result!);
}

public class LookaheadResult<TToken, T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ namespace SimpleStateMachine.StructuralSearch
public class PlaceholderParser : ParserWithLookahead<char, string>, 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<char, string> BuildParser(Func<Parser<char, string>?> next,
protected override Parser<char, string> BuildParser(Func<Parser<char, string>?> next,
Func<Parser<char, string>?> nextNext)
{
var nextParser = next();
Expand Down Expand Up @@ -58,7 +60,7 @@ public override Parser<char, string> BuildParser(Func<Parser<char, string>?> nex
Parser<char, string>? 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();
Expand All @@ -73,24 +75,23 @@ public override Parser<char, string> BuildParser(Func<Parser<char, string>?> nex
return parser;
}

public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
out string result)
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> 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));
}
Expand Down
12 changes: 4 additions & 8 deletions src/SimpleStateMachine.StructuralSearch/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IPlaceholder> placeholders)
{
ClearInternal();
Expand Down
4 changes: 3 additions & 1 deletion src/SimpleStateMachine.StructuralSearch/SeriesParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, IEnumerable<string>>, IContextDependent
Expand Down Expand Up @@ -55,7 +57,7 @@ void SkipLookedParsers(Parser<char, string> parser, ref ParseState<char> state)
lookaheadParser is { OnLookahead: null })
return;

var lookaheadResults = lookaheadParser.OnLookahead.Invoke();
var lookaheadResults = lookaheadParser.OnLookahead.Invoke().ToArray();

foreach (var result in lookaheadResults)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Pidgin;
using SimpleStateMachine.StructuralSearch.Extensions;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}");
Expand All @@ -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)
Expand Down Expand Up @@ -120,8 +120,6 @@ static ParametersParser()

public static readonly Parser<char, Func<IRuleParameter, IRuleParameter>> Change;

public static readonly Parser<char, IRuleParameter> PlaceholderOrProperty;

public static readonly Parser<char, IRuleParameter> PlaceholderOrPropertyRuleParameter;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Pidgin;
using SimpleStateMachine.StructuralSearch.Extensions;
using SimpleStateMachine.StructuralSearch.Helper;
Expand All @@ -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<char, ParenthesisedParameter, IRuleParameter>()
Expand Down