diff --git a/TextParser.Tests/TextParserTests.cs b/TextParser.Tests/TextParserTests.cs index 34f45ae..37cdb90 100644 --- a/TextParser.Tests/TextParserTests.cs +++ b/TextParser.Tests/TextParserTests.cs @@ -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]); + } } diff --git a/TextParser/Schema/FragmentSchema.cs b/TextParser/Schema/FragmentSchema.cs index cc47246..af28373 100644 --- a/TextParser/Schema/FragmentSchema.cs +++ b/TextParser/Schema/FragmentSchema.cs @@ -16,11 +16,13 @@ public class FragmentSchema : ISchema { private string fragmentRegex; private List namedGroups = new List(); + private List namedLiterals = new List(); - public FragmentSchema(string fragmentRegex, List namedGroups) + public FragmentSchema(string fragmentRegex, List namedGroups, List namedLiterals) { this.fragmentRegex = fragmentRegex; this.namedGroups = namedGroups; + this.namedLiterals = namedLiterals; } public List ProcessNextWord(FragmentSchemaContext currentContext, InputProvider inputs) @@ -42,6 +44,15 @@ public class FragmentSchema : ISchema } newToken.AddMatch(groupName, captureList); } + foreach (var literalName in this.namedLiterals) + { + var captureList = new List(); + 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); } diff --git a/TextParser/Schema/FragmentSchemaBuilder.cs b/TextParser/Schema/FragmentSchemaBuilder.cs index 99c374b..269d76a 100644 --- a/TextParser/Schema/FragmentSchemaBuilder.cs +++ b/TextParser/Schema/FragmentSchemaBuilder.cs @@ -8,6 +8,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder namedGroups = new List(); + private List namedLiterals = new List(); public FragmentSchemaBuilder() { @@ -57,9 +58,15 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder"; + namedLiterals.Add(name); + } + this.fragmentRegex += "(" + groupNamePrefix + Regex.Escape(literal) + ")"; return this; } @@ -129,7 +136,7 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder