Skip to content

Commit a5e51af

Browse files
committed
Parsing context as ref, dictionary for rules in StructuralSearchParser
1 parent 075354d commit a5e51af

File tree

19 files changed

+97
-41
lines changed

19 files changed

+97
-41
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/FindTemplateTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ public void SourceParsingBeFindTemplateShouldBeSuccess(string templatePath, stri
2929
var findTemplate = File.ReadAllText(templatePath);
3030
var source = File.ReadAllText(sourcePath);
3131
var findParser = StructuralSearch.ParseFindTemplate(findTemplate);
32-
var parsingContext = new ParsingContext();
33-
var result = findParser.Parse(parsingContext, source);
32+
IParsingContext parsingContext = new ParsingContext();
33+
var result = findParser.Parse(ref parsingContext, source);
3434

3535
Assert.NotNull(findParser);
36-
Assert.NotNull(result);
3736
Assert.NotNull(result.Value);
3837
Assert.Equal(result.Lenght, source.Length);
3938
}

src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{
33
public class EmptyParsingContext : IParsingContext
44
{
5+
public FileProperty File { get; } = new FileProperty();
6+
57
public bool TryGetPlaceholder(string name, out Placeholder value)
68
{
79
throw new System.NotImplementedException();

src/SimpleStateMachine.StructuralSearch.Tests/PlaceholderParserTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void FindTemplateShouldBeNotEmpty()
1818
[InlineData("($test$)", "(value (test) )", "value (test) ")]
1919
public void TemplateParsingShouldBeSuccess(string template, string source, string result)
2020
{
21-
var parsingContext = new ParsingContext();
21+
IParsingContext parsingContext = new ParsingContext();
2222
var templateParser = StructuralSearch.ParseFindTemplate(template);
23-
var res = templateParser.Parse(parsingContext, source);
23+
var res = templateParser.Parse(ref parsingContext, source);
2424
var placeholder = parsingContext.GetPlaceholder("test");
2525

2626
Assert.Equal(placeholder.Value, result);
@@ -30,9 +30,9 @@ public void TemplateParsingShouldBeSuccess(string template, string source, strin
3030
[InlineData("$var$;$var2$;", "test;;;test;;;", "value ")]
3131
public void TemplateParsingShouldBeSuccess2(string template, string source, string result)
3232
{
33-
var parsingContext = new ParsingContext();
33+
IParsingContext parsingContext = new ParsingContext();
3434
var templateParser = StructuralSearch.ParseFindTemplate(template);
35-
var res = templateParser.Parse(parsingContext, source);
35+
templateParser.Parse(ref parsingContext, source);
3636

3737

3838
// var templateStr = File.ReadAllText(templatePath);

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceRuleParserTests.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,34 @@ namespace SimpleStateMachine.StructuralSearch.Tests
77
public class ReplaceRuleParserTests
88
{
99
[Theory]
10-
[InlineData("equals $var$", "$var$")]
11-
[InlineData("equals \"\\$\"", "\"\\$\"")]
12-
[InlineData("Not equals $var$.Lenght", "$var$.Lenght")]
13-
[InlineData("Not equals $var$.offset.Start", "$var$.offset.Start")]
14-
[InlineData("equals $var$.Lenght and Not StartsWith \"123\"", "$var$.offset.Start.Trim")]
15-
[InlineData("equals $var$.Lenght and Not StartsWith \"\\\"Test\"", "$var$.offset.Start.ToUpper")]
16-
public void ReplaceRuleParsingShouldBeSuccess(string findRule, string replaceRule)
10+
[InlineData("$var$", "equals $var$", "$var$")]
11+
[InlineData("$var$", "equals \"\\$\"", "\"\\$\"")]
12+
[InlineData("$var$", "Not equals $var$.Lenght", "$var$.Lenght")]
13+
[InlineData("$var$", "Not equals $var$.offset.Start", "$var$.offset.Start")]
14+
[InlineData("$var$", "equals $var$.Lenght and Not StartsWith \"123\"", "$var$.offset.Start.Trim")]
15+
[InlineData("$var$", "equals $var$.Lenght and Not StartsWith \"\\\"Test\"", "$var$.offset.Start.ToUpper")]
16+
public void ReplaceRulePartsParsingShouldBeSuccess(string placeholder, string findRule, string replaceRule)
1717
{
18-
var placeholder = "$var$";
1918
var replaceRuleStr = $"{placeholder} {findRule} => {replaceRule}";
2019
var rule = StructuralSearch.ParseReplaceRule(replaceRuleStr);
21-
var _ruleStr = rule.ToString().ToLower();
20+
var ruleStr = rule.ToString().ToLower();
2221
Assert.NotNull(rule);
23-
Assert.Equal(_ruleStr, replaceRuleStr.ToLower());
22+
Assert.Equal(ruleStr, replaceRuleStr.ToLower());
23+
}
24+
25+
[Theory]
26+
[InlineData("$var1$ equals $var$ => $var2$")]
27+
[InlineData("$var1$ equals \"\\$\" => \"\\$\"")]
28+
[InlineData("$var1$ Not equals $var$.Lenght => $var$.Lenght")]
29+
[InlineData("$var1$ Not equals $var$.offset.Start => $var$.offset.Start")]
30+
[InlineData("$var1$ equals $var$.Lenght and Not StartsWith \"123\" => $var$.offset.Start.Trim")]
31+
[InlineData("$var1$ equals $var$.Lenght and Not StartsWith \"\\\"Test\" => $var$.offset.Start.ToUpper")]
32+
public void ReplaceRuleParsingShouldBeSuccess(string replaceRule)
33+
{
34+
var rule = StructuralSearch.ParseReplaceRule(replaceRule);
35+
var ruleStr = rule.ToString().ToLower();
36+
Assert.NotNull(rule);
37+
Assert.Equal(ruleStr, replaceRule.ToLower());
2438
}
2539
}
2640
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace SimpleStateMachine.StructuralSearch.Extensions
5+
{
6+
public static class IEnumerableExtensions
7+
{
8+
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T>? enumerable)
9+
{
10+
return enumerable ?? Enumerable.Empty<T>();
11+
}
12+
}
13+
}

src/SimpleStateMachine.StructuralSearch/FindParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public FindParser(SeriesParser parser)
1212
Parser = parser;
1313
}
1414

15-
public SourceMatch Parse(IParsingContext context, string input)
15+
public SourceMatch Parse(ref IParsingContext context, string input)
1616
{
17-
Parser.SetContext(context);
17+
Parser.SetContext(ref context);
1818
var result = Parser.Select(x => string.Join(string.Empty, x)).AsMatch().Parse(input);
1919

2020
return result.Success ? result.Value : SourceMatch.Empty;

src/SimpleStateMachine.StructuralSearch/IFindParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace SimpleStateMachine.StructuralSearch
66
{
77
public interface IFindParser
88
{
9-
SourceMatch Parse(IParsingContext context, string input);
9+
SourceMatch Parse(ref IParsingContext context, string input);
1010
}
1111
}

src/SimpleStateMachine.StructuralSearch/IParsingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public interface IParsingContext
44
{
5+
FileProperty File { get; }
56
bool TryGetPlaceholder(string name, out Placeholder value);
67

78
void AddPlaceholder(Placeholder placeholder);

src/SimpleStateMachine.StructuralSearch/Parsers/IContextDependent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IContextDependent
44
{
5-
void SetContext(IParsingContext context);
5+
void SetContext(ref IParsingContext context);
66
}
77
}

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
7979
context: _context,
8080
name: Name,
8181
value: result,
82-
file: null,
8382
line: new LineProperty(oldPos.Line, oldPos.Line + posDelta.Lines),
8483
column: new ColumnProperty(oldPos.Col, oldPos.Col + posDelta.Cols),
8584
offset: new OffsetProperty(oldOffset, newOffset)));
@@ -117,7 +116,7 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
117116
// var parser = term.JoinToString().AsMatch();
118117
// return parser;
119118
// }
120-
public void SetContext(IParsingContext context)
119+
public void SetContext(ref IParsingContext context)
121120
{
122121
_context = context;
123122
}

0 commit comments

Comments
 (0)