Skip to content

Commit c84251a

Browse files
committed
IContextDependent for parsers
1 parent 9b33682 commit c84251a

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

src/SimpleStateMachine.StructuralSearch/FindParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public IEnumerable<FindParserResult> Parse(ref IParsingContext context)
2121
List<FindParserResult> matches = new();
2222
StringBuilder res = new();
2323

24+
Parser.SetContext(ref context);
25+
2426
var parsingContext = context;
2527
var parser = Parser.Select(x => string.Join(string.Empty, x))
2628
.Match()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SimpleStateMachine.StructuralSearch;
2+
3+
public interface IContextDependent
4+
{
5+
void SetContext(ref IParsingContext context);
6+
}

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace SimpleStateMachine.StructuralSearch
77
{
8-
public class PlaceholderParser : ParserWithLookahead<char, string>
8+
public class PlaceholderParser : ParserWithLookahead<char, string>, IContextDependent
99
{
1010
private readonly string _name;
11-
11+
private IParsingContext _context;
1212
public PlaceholderParser(string name)
1313
{
1414
_name = name;
@@ -79,7 +79,7 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
7979
bool res;
8080

8181
// No use look-ahead if placeholder is already defined
82-
if (context.TryGetPlaceholder(_name, out var placeholder))
82+
if (_context.TryGetPlaceholder(_name, out var placeholder))
8383
{
8484
res = Parser.String(placeholder.Value).TryParse(ref state, ref expected, out result);
8585
}
@@ -89,14 +89,19 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
8989
result = match.Value;
9090
if (res)
9191
{
92-
context.AddPlaceholder(new Placeholder(
93-
context: ref context,
92+
_context.AddPlaceholder(new Placeholder(
93+
context: ref _context,
9494
name: _name,
9595
match: match));
9696
}
9797
}
9898

9999
return res;
100100
}
101+
102+
public void SetContext(ref IParsingContext context)
103+
{
104+
_context = context;
105+
}
101106
}
102107
}

src/SimpleStateMachine.StructuralSearch/Rules/FindRule/EmptySubRule.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,4 @@ public override string ToString()
1111
{
1212
return $"{Constant.Underscore}";
1313
}
14-
15-
public void SetContext(ref IParsingContext context)
16-
{
17-
}
1814
}

src/SimpleStateMachine.StructuralSearch/Rules/ReplaceRule/IReplaceRule.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ public class EmptyReplaceRule : IReplaceRule
1414
{
1515
public IEnumerable<ReplaceSubRule> Rules { get; } = Array.Empty<ReplaceSubRule>();
1616
public IRule ConditionRule => Rule.Empty;
17-
18-
public void SetContext(ref IParsingContext context)
19-
{
20-
}
2117
}

src/SimpleStateMachine.StructuralSearch/SeriesParser.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace SimpleStateMachine.StructuralSearch
66
{
7-
public class SeriesParser : Parser<char, IEnumerable<string>>
7+
public class SeriesParser : Parser<char, IEnumerable<string>>, IContextDependent
88
{
99
private readonly IEnumerable<Parser<char, string>> _parsers;
10-
10+
1111
public SeriesParser(IEnumerable<Parser<char, string>> parsers)
1212
{
1313
_parsers = parsers;
@@ -75,5 +75,16 @@ void SkipLookedParsers(Parser<char, string> parser, ref ParseState<char> state)
7575
result = results;
7676
return true;
7777
}
78+
79+
public void SetContext(ref IParsingContext parsingContext)
80+
{
81+
foreach (var parser in _parsers)
82+
{
83+
if (parser is IContextDependent element)
84+
{
85+
element.SetContext(ref parsingContext);
86+
}
87+
}
88+
}
7889
}
7990
}

0 commit comments

Comments
 (0)