generated from Templates/Dotnet_Library
feat: enable named literals, ref: NOISSUE
This commit is contained in:
parent
7be09140e2
commit
fab5d2eee7
@ -20,6 +20,12 @@ public class TextParserTests
|
||||
cv strs(test) jh 4,3,2
|
||||
|
||||
34,54,2nums(2,8) strs(aa,ab,ba,bb)aa,bb";
|
||||
private const string testInput6 = @"adfdf1()324ddf3()svsdvs
|
||||
davnsldkvjs2()m23423()
|
||||
mcsodkcn owdjnfj 1() asdfnad 23234 2() sdvsdv";
|
||||
private const string testInput7 = @"adfdf1()324ddf3()()()svsdvs
|
||||
davnsldkvjs2()()m23423()()()
|
||||
mcsodkcn owdjnfj 1() asdfnad 23234 2()() sdvsdv";
|
||||
|
||||
[Fact]
|
||||
public void LineParser_TestSimpleRepetition()
|
||||
@ -257,4 +263,56 @@ public class TextParserTests
|
||||
Assert.Equal(12, convertedData[2]);
|
||||
Assert.Equal(4, convertedData[3]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FragmentParser_LiteralTest()
|
||||
{
|
||||
var schemaBuilder = new FragmentSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.StartOptions()
|
||||
.Option()
|
||||
.Expect("1()", "option1")
|
||||
.Option()
|
||||
.Expect("2()", "option2")
|
||||
.Option()
|
||||
.Expect("3()", "option3")
|
||||
.EndOptions()
|
||||
.Build();
|
||||
|
||||
var parser = TextParser.Create(schema);
|
||||
var fragmentData = parser
|
||||
.SetInputText(testInput6)
|
||||
.Parse()
|
||||
.AsFragments();
|
||||
|
||||
var convertedData = fragmentData
|
||||
.ConvertAll((Fragment f) =>
|
||||
{
|
||||
bool saw1 = f.ContainsKey("option1") ? f["option1"].Count > 0 : false;
|
||||
bool saw2 = f.ContainsKey("option2") ? f["option2"].Count() > 0 : false;
|
||||
bool saw3 = f.ContainsKey("option3") ? f["option3"].Count() > 0 : false;
|
||||
int indicator = 0;
|
||||
if (saw1)
|
||||
{
|
||||
indicator += 1;
|
||||
}
|
||||
if (saw2)
|
||||
{
|
||||
indicator += 2;
|
||||
}
|
||||
if (saw3)
|
||||
{
|
||||
indicator += 4;
|
||||
}
|
||||
return indicator;
|
||||
});
|
||||
|
||||
Assert.Equal(6, convertedData.Count);
|
||||
Assert.Equal(1, convertedData[0]);
|
||||
Assert.Equal(4, convertedData[1]);
|
||||
Assert.Equal(2, convertedData[2]);
|
||||
Assert.Equal(4, convertedData[3]);
|
||||
Assert.Equal(1, convertedData[4]);
|
||||
Assert.Equal(2, convertedData[5]);
|
||||
}
|
||||
}
|
||||
|
@ -16,11 +16,13 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
|
||||
{
|
||||
private string fragmentRegex;
|
||||
private List<string> namedGroups = new List<string>();
|
||||
private List<string> namedLiterals = new List<string>();
|
||||
|
||||
public FragmentSchema(string fragmentRegex, List<string> namedGroups)
|
||||
public FragmentSchema(string fragmentRegex, List<string> namedGroups, List<string> namedLiterals)
|
||||
{
|
||||
this.fragmentRegex = fragmentRegex;
|
||||
this.namedGroups = namedGroups;
|
||||
this.namedLiterals = namedLiterals;
|
||||
}
|
||||
|
||||
public List<IToken> ProcessNextWord(FragmentSchemaContext currentContext, InputProvider inputs)
|
||||
@ -42,6 +44,15 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
|
||||
}
|
||||
newToken.AddMatch(groupName, captureList);
|
||||
}
|
||||
foreach (var literalName in this.namedLiterals)
|
||||
{
|
||||
var captureList = new List<string>();
|
||||
if (match.Groups.Keys.Contains(literalName) && match.Groups[literalName].Length > 0)
|
||||
{
|
||||
captureList.Add(match.Groups[literalName].Length.ToString());
|
||||
}
|
||||
newToken.AddMatch(literalName, captureList);
|
||||
}
|
||||
tokenList.Add(newToken);
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
protected string fragmentRegex = @"";
|
||||
|
||||
private List<string> namedGroups = new List<string>();
|
||||
private List<string> namedLiterals = new List<string>();
|
||||
|
||||
public FragmentSchemaBuilder()
|
||||
{
|
||||
@ -57,9 +58,15 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
return this;
|
||||
}
|
||||
|
||||
public FragmentSchemaBuilder Expect(string literal)
|
||||
public FragmentSchemaBuilder Expect(string literal, string name = "")
|
||||
{
|
||||
this.fragmentRegex += Regex.Escape(literal);
|
||||
string groupNamePrefix = "";
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
groupNamePrefix = "?<" + name + ">";
|
||||
namedLiterals.Add(name);
|
||||
}
|
||||
this.fragmentRegex += "(" + groupNamePrefix + Regex.Escape(literal) + ")";
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -129,7 +136,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
|
||||
|
||||
public FragmentSchema Build()
|
||||
{
|
||||
var schema = new FragmentSchema(this.fragmentRegex, this.namedGroups);
|
||||
var schema = new FragmentSchema(this.fragmentRegex, this.namedGroups, this.namedLiterals);
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user