Skip to content

Commit 4e08cf6

Browse files
committed
placeholder properties
1 parent 9777ff5 commit 4e08cf6

File tree

10 files changed

+62
-11
lines changed

10 files changed

+62
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public bool TryGetPlaceholder(string name, out Placeholder value)
77
throw new System.NotImplementedException();
88
}
99

10-
public void AddPlaceholder(string name, string value)
10+
public void AddPlaceholder(Placeholder placeholder)
1111
{
1212
throw new System.NotImplementedException();
1313
}
1414

1515
public Placeholder GetPlaceholder(string name)
1616
{
17-
return new Placeholder(this, name, string.Empty);
17+
return Placeholder.CreateEmpty(this, name, string.Empty);
1818
}
1919
}
2020
}

src/SimpleStateMachine.StructuralSearch.Tests/ReplaceTemplateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void ReplaceBuildShouldBeSuccess(string templatePath, string resultPath,
4141
var parsingContext = new ParsingContext();
4242
for (int i = 0; i < keys.Length; i++)
4343
{
44-
parsingContext.AddPlaceholder(keys[i], values[i]);
44+
parsingContext.AddPlaceholder(Placeholder.CreateEmpty(parsingContext, keys[i], values[i]));
4545
}
4646

4747
var result = replaceBuilder.Build(parsingContext);

src/SimpleStateMachine.StructuralSearch/IParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public interface IParsingContext
44
{
55
bool TryGetPlaceholder(string name, out Placeholder value);
66

7-
void AddPlaceholder(string name, string value);
7+
void AddPlaceholder(Placeholder placeholder);
88

99
Placeholder GetPlaceholder(string name);
1010
}

src/SimpleStateMachine.StructuralSearch/Parsers/PlaceholderParser.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,24 @@ public override bool TryParse(ref ParseState<char> state, ref PooledList<Expecte
6666
}
6767
else
6868
{
69+
Parser<char>.CurrentPos.TryParse(ref state, ref expected, out var oldPos);
70+
Parser<char>.CurrentOffset.TryParse(ref state, ref expected, out var oldOffset);
6971
res = base.TryParse(ref state, ref expected, out result);
72+
7073
if (res)
71-
_context.AddPlaceholder(Name, result);
74+
{
75+
Parser<char>.CurrentSourcePosDelta.TryParse(ref state, ref expected, out var posDelta);
76+
Parser<char>.CurrentOffset.TryParse(ref state, ref expected, out var newOffset);
77+
78+
_context.AddPlaceholder(new Placeholder(
79+
context: _context,
80+
name: Name,
81+
value: result,
82+
file: null,
83+
line: new LineProperty(oldPos.Line, oldPos.Line + posDelta.Lines),
84+
column: new ColumnProperty(oldPos.Col, oldPos.Col + posDelta.Cols),
85+
offset: new OffsetProperty(oldOffset, newOffset)));
86+
}
7287
}
7388

7489
return res;

src/SimpleStateMachine.StructuralSearch/ParsingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public bool TryGetPlaceholder(string name, out Placeholder value)
1111
return Placeholders.TryGetValue(name, out value);
1212
}
1313

14-
public void AddPlaceholder(string name, string value)
14+
public void AddPlaceholder(Placeholder placeholder)
1515
{
16-
Placeholders[name] = new Placeholder(this, name, value);
16+
Placeholders[placeholder.Name] = placeholder;
1717
}
1818

1919
public Placeholder GetPlaceholder(string name)

src/SimpleStateMachine.StructuralSearch/Placeholder/Column.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
namespace SimpleStateMachine.StructuralSearch
22
{
3-
public class Column
3+
public class ColumnProperty
44
{
5+
public ColumnProperty(int start, int end)
6+
{
7+
Start = start;
8+
End = end;
9+
}
10+
511
public readonly int Start;
612
public readonly int End;
713
}

src/SimpleStateMachine.StructuralSearch/Placeholder/LineProperty.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
{
33
public class LineProperty
44
{
5+
public LineProperty(int start, int end)
6+
{
7+
Start = start;
8+
End = end;
9+
}
10+
511
public readonly int Start;
612
public readonly int End;
713
}

src/SimpleStateMachine.StructuralSearch/Placeholder/OffsetProperty.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
{
33
public class OffsetProperty
44
{
5+
public OffsetProperty(int start, int end)
6+
{
7+
Start = start;
8+
End = end;
9+
}
10+
511
public readonly int Start;
612
public readonly int End;
713
}

src/SimpleStateMachine.StructuralSearch/Placeholder/Placeholder.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,37 @@
33
public class Placeholder
44
{
55
private IParsingContext _context;
6-
public Placeholder(IParsingContext context, string name, string value)
6+
public Placeholder(IParsingContext context, string name, string value, FileProperty file, LineProperty line, ColumnProperty column, OffsetProperty offset)
77
{
88
_context = context;
99
Name = name;
10+
Lenght = value.Length;
1011
Value = value;
12+
File = file;
13+
Line = line;
14+
Column = column;
15+
Offset = offset;
1116
}
1217

1318
public readonly string Name;
1419
public readonly string Value;
1520
public readonly int Lenght;
1621
public readonly FileProperty File;
1722
public readonly LineProperty Line;
18-
public readonly Column Column;
23+
public readonly ColumnProperty Column;
1924
public readonly OffsetProperty Offset;
25+
26+
27+
public static Placeholder CreateEmpty(IParsingContext context, string name, string value)
28+
{
29+
return new Placeholder(
30+
context: context,
31+
name: name,
32+
value: value,
33+
file: null,
34+
line: null,
35+
column: null,
36+
offset: null);
37+
}
2038
}
2139
}

src/SimpleStateMachine.StructuralSearch/Rules/Parameters/PlaceholderPropertyParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class PlaceholderPropertyParser
1919
public static readonly Parser<char, Func<PlaceholderParameter, IRuleParameter>> Column =
2020
Parsers.EnumValue(PlaceholderProperty.Column, true)
2121
.Then(CommonParser.Dote)
22-
.Then(Parsers.Enum<ColumnProperty>(true))
22+
.Then(Parsers.Enum<Rules.ColumnProperty>(true))
2323
.Select(property => new Func<PlaceholderParameter, IRuleParameter>(placeholder =>
2424
new PlaceholderColumnParameter(placeholder, property)))
2525
.Try();

0 commit comments

Comments
 (0)