diff --git a/TextParser.Tests/TextParserTests.cs b/TextParser.Tests/TextParserTests.cs index 593bb4f..d84c650 100644 --- a/TextParser.Tests/TextParserTests.cs +++ b/TextParser.Tests/TextParserTests.cs @@ -34,6 +34,7 @@ public class TextParserTests bca cab"; private const string testInput11 = @"2 x y 4 x y 6 x y 4 x y 1 x y"; + private const string testInput12 = @"abcd"; [Fact] public void LineParser_TestSimpleRepetition() @@ -443,4 +444,27 @@ public class TextParserTests Assert.Equal(4, numbers[3]); Assert.Equal(1, numbers[4]); } + + [Fact] + public void TextParser_TestCharExplicit() + { + var schemaBuilder = new InputSchemaBuilder(); + var schema = schemaBuilder + .Repeat() + .Expect(InputType.Char) + .EndRepetition() + .Build(); + + var parser = new TextParser(schema); + var numbers = parser + .SetInputText(testInput12) + .Parse() + .AsSingleStream(); + + Assert.Equal(4, numbers.Count); + Assert.Equal("a", numbers[0]); + Assert.Equal("b", numbers[1]); + Assert.Equal("c", numbers[2]); + Assert.Equal("d", numbers[3]); + } } diff --git a/TextParser/Schema/BuildingBlocks/CharBlock.cs b/TextParser/Schema/BuildingBlocks/CharBlock.cs index e8fe029..fb8cddd 100644 --- a/TextParser/Schema/BuildingBlocks/CharBlock.cs +++ b/TextParser/Schema/BuildingBlocks/CharBlock.cs @@ -44,6 +44,6 @@ class CharBlock : BuildingBlockBase public override BlockType GetBlockType() { - return BlockType.String; + return BlockType.Char; } } \ No newline at end of file diff --git a/TextParser/Schema/FragmentSchema.cs b/TextParser/Schema/FragmentSchema.cs index af28373..51faea4 100644 --- a/TextParser/Schema/FragmentSchema.cs +++ b/TextParser/Schema/FragmentSchema.cs @@ -17,6 +17,7 @@ public class FragmentSchema : ISchema private string fragmentRegex; private List namedGroups = new List(); private List namedLiterals = new List(); + private bool ignoreEmptyMatches = true; public FragmentSchema(string fragmentRegex, List namedGroups, List namedLiterals) { @@ -33,6 +34,11 @@ public class FragmentSchema : ISchema // one token per match foreach (Match match in r.Matches(inputs.YieldWord())) { + if(this.ignoreEmptyMatches && string.IsNullOrEmpty(match.Value)) + { + continue; + } + var newToken = new FragmentToken(match.Value); // token contains data from all included matches foreach (var groupName in this.namedGroups) @@ -109,4 +115,9 @@ public class FragmentSchema : ISchema { return new FragmentSchemaContext(); } + + public void ShouldIgnoreEmptyMatches(bool ignoreEmptyMatches) + { + this.ignoreEmptyMatches = ignoreEmptyMatches; + } } diff --git a/TextParser/Schema/FragmentSchemaBuilder.cs b/TextParser/Schema/FragmentSchemaBuilder.cs index 269d76a..3a7ccd5 100644 --- a/TextParser/Schema/FragmentSchemaBuilder.cs +++ b/TextParser/Schema/FragmentSchemaBuilder.cs @@ -52,12 +52,22 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder !oldSchemaBuilder.namedGroups.Contains(g)).ToList(); + var literalsToAdd = currentBuilder.namedLiterals.Where(l => !oldSchemaBuilder.namedLiterals.Contains(l)).ToList(); + oldSchemaBuilder.namedGroups.AddRange(groupsToAdd); + oldSchemaBuilder.namedLiterals.AddRange(literalsToAdd); return oldSchemaBuilder; }