Skip to content

Commit ed7e6e3

Browse files
Merge pull request #16 from SimpleStateMachine/context_as_parameter2
context as parameter, remove IContextDependent
2 parents 2194047 + 13691b3 commit ed7e6e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+174
-289
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public static void ReplaceTemplateParsingShouldHaveStepCount(string templatePath
1515
{
1616
var replaceTemplate = File.ReadAllText(templatePath);
1717
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate);
18-
var result = replaceBuilder.Build(ParsingContext.Empty);
18+
IParsingContext context = ParsingContext.Empty;
19+
var result = replaceBuilder.Build(ref context);
1920

2021
Assert.NotNull(replaceTemplate);
2122
Assert.Equal(replaceBuilder.Steps.Count(), stepsCount);
@@ -37,13 +38,13 @@ public static void ReplaceBuildShouldBeSuccess(string templatePath, string resul
3738
var replaceResult = File.ReadAllText(resultPath);
3839
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate);
3940

40-
var parsingContext = new ParsingContext(Input.Empty);
41+
IParsingContext parsingContext = new ParsingContext(Input.Empty);
4142
for (int i = 0; i < keys.Length; i++)
4243
{
4344
parsingContext.AddPlaceholder(Placeholder.CreateEmpty(parsingContext, keys[i], values[i]));
4445
}
4546

46-
var result = replaceBuilder.Build(parsingContext);
47+
var result = replaceBuilder.Build(ref parsingContext);
4748

4849
Assert.NotNull(replaceTemplate);
4950
Assert.NotNull(replaceResult);

src/SimpleStateMachine.StructuralSearch/FindParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public IEnumerable<FindParserResult> Parse(ref IParsingContext context)
2020
{
2121
List<FindParserResult> matches = new();
2222
StringBuilder res = new();
23+
2324
Parser.SetContext(ref context);
2425

2526
var parsingContext = context;

src/SimpleStateMachine.StructuralSearch/Parsers/DebugParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public DebugParser(Parser<TToken, T> parser)
1010
_parser = parser;
1111
}
1212

13-
public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expecteds, out T result)
13+
public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected, out T result)
1414
{
15-
var res = _parser.TryParse(ref state, ref expecteds, out result);
15+
var res = _parser.TryParse(ref state, ref expected, out result);
1616
return res;
1717
}
1818
}

src/SimpleStateMachine.StructuralSearch/Parsers/EmptyStringParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public EmptyStringParser(bool value)
1111
_value = value;
1212
}
1313

14-
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds,
14+
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
1515
out string result)
1616
{
1717
result = string.Empty;

src/SimpleStateMachine.StructuralSearch/Parsers/EnumParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public EnumParser(bool ignoreCase, params TEnum [] excluded)
1919
.AsEnum<TEnum>(ignoreCase);
2020
}
2121

22-
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expecteds,
22+
public override bool TryParse(ref ParseState<char> state, ref PooledList<Expected<char>> expected,
2323
out TEnum result)
2424
{
25-
return _parser.TryParse(ref state, ref expecteds, out result);
25+
return _parser.TryParse(ref state, ref expected, out result);
2626
}
2727
}
2828
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
namespace SimpleStateMachine.StructuralSearch
1+
namespace SimpleStateMachine.StructuralSearch;
2+
3+
public interface IContextDependent
24
{
3-
public interface IContextDependent
4-
{
5-
void SetContext(ref IParsingContext context);
6-
}
5+
void SetContext(ref IParsingContext context);
76
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Pidgin;
2+
3+
namespace SimpleStateMachine.StructuralSearch;
4+
5+
// public abstract class ParserWithContext<TToken, T> : Parser<TToken, T>
6+
// {
7+
// public abstract bool TryParse(ref IParsingContext context, ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expected, out T result);
8+
// }

src/SimpleStateMachine.StructuralSearch/Parsers/ParserWithLookahead.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public override bool TryParse(ref ParseState<TToken> state, ref PooledList<Expec
2828

2929
public class LookaheadResult<TToken, T>
3030
{
31+
public T Result { get; }
32+
public int TokensCount { get; }
33+
public readonly Parser<TToken, T> Parser;
34+
3135
public LookaheadResult(Parser<TToken, T> parser, T result, int tokensCount)
3236
{
3337
Parser = parser;
3438
Result = result;
3539
TokensCount = tokensCount;
3640
}
37-
38-
public T Result { get; }
39-
public int TokensCount { get; }
40-
public Parser<TToken, T> Parser;
4141
}
4242
}

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ namespace SimpleStateMachine.StructuralSearch
77
{
88
public class PlaceholderParser : ParserWithLookahead<char, string>, IContextDependent
99
{
10+
private readonly string _name;
1011
private IParsingContext _context;
11-
1212
public PlaceholderParser(string name)
1313
{
14-
Name = name;
14+
_name = name;
1515
}
1616

17-
public string Name { get; }
18-
1917
public override Parser<char, string> BuildParser(Func<Parser<char, string>?> next,
2018
Func<Parser<char, string>?> nextNext)
2119
{
@@ -79,9 +77,9 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
7977
out string result)
8078
{
8179
bool res;
82-
80+
8381
// No use look-ahead if placeholder is already defined
84-
if (_context.TryGetPlaceholder(Name, out var placeholder))
82+
if (_context.TryGetPlaceholder(_name, out var placeholder))
8583
{
8684
res = Parser.String(placeholder.Value).TryParse(ref state, ref expected, out result);
8785
}
@@ -92,8 +90,8 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
9290
if (res)
9391
{
9492
_context.AddPlaceholder(new Placeholder(
95-
context: _context,
96-
name: Name,
93+
context: ref _context,
94+
name: _name,
9795
match: match));
9896
}
9997
}

src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
private readonly Match<string> _match;
66
private readonly IParsingContext _context;
7-
public Placeholder(IParsingContext context, string name, Match<string> match)
7+
public Placeholder(ref IParsingContext context, string name, Match<string> match)
88
{
99
_context = context;
1010
Name = name;
@@ -22,7 +22,7 @@ public Placeholder(IParsingContext context, string name, Match<string> match)
2222
public static Placeholder CreateEmpty(IParsingContext context, string name, string value)
2323
{
2424
return new Placeholder(
25-
context: context,
25+
context: ref context,
2626
name: name,
2727
new Match<string>(
2828
value,

0 commit comments

Comments
 (0)