feat: enable Char blocks again, refs NOISSUE

This commit is contained in:
Simon Diesenreiter
2025-12-01 14:04:50 +01:00
parent 1d68b70fa5
commit 8475389b99
4 changed files with 50 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ public class TextParserTests
bca bca
cab"; cab";
private const string testInput11 = @"2 x y 4 x y 6 x y 4 x y 1 x y"; 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] [Fact]
public void LineParser_TestSimpleRepetition() public void LineParser_TestSimpleRepetition()
@@ -443,4 +444,27 @@ public class TextParserTests
Assert.Equal(4, numbers[3]); Assert.Equal(4, numbers[3]);
Assert.Equal(1, numbers[4]); 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<InputSchemaContext>(schema);
var numbers = parser
.SetInputText(testInput12)
.Parse()
.AsSingleStream<string>();
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]);
}
} }

View File

@@ -44,6 +44,6 @@ class CharBlock : BuildingBlockBase
public override BlockType GetBlockType() public override BlockType GetBlockType()
{ {
return BlockType.String; return BlockType.Char;
} }
} }

View File

@@ -17,6 +17,7 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
private string fragmentRegex; private string fragmentRegex;
private List<string> namedGroups = new List<string>(); private List<string> namedGroups = new List<string>();
private List<string> namedLiterals = new List<string>(); private List<string> namedLiterals = new List<string>();
private bool ignoreEmptyMatches = true;
public FragmentSchema(string fragmentRegex, List<string> namedGroups, List<string> namedLiterals) public FragmentSchema(string fragmentRegex, List<string> namedGroups, List<string> namedLiterals)
{ {
@@ -33,6 +34,11 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
// one token per match // one token per match
foreach (Match match in r.Matches(inputs.YieldWord())) foreach (Match match in r.Matches(inputs.YieldWord()))
{ {
if(this.ignoreEmptyMatches && string.IsNullOrEmpty(match.Value))
{
continue;
}
var newToken = new FragmentToken(match.Value); var newToken = new FragmentToken(match.Value);
// token contains data from all included matches // token contains data from all included matches
foreach (var groupName in this.namedGroups) foreach (var groupName in this.namedGroups)
@@ -109,4 +115,9 @@ public class FragmentSchema : ISchema<FragmentSchemaContext>
{ {
return new FragmentSchemaContext(); return new FragmentSchemaContext();
} }
public void ShouldIgnoreEmptyMatches(bool ignoreEmptyMatches)
{
this.ignoreEmptyMatches = ignoreEmptyMatches;
}
} }

View File

@@ -52,12 +52,22 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
case InputType.Integer: case InputType.Integer:
this.fragmentRegex += "(" + groupNamePrefix + "\\d+)"; this.fragmentRegex += "(" + groupNamePrefix + "\\d+)";
break; break;
case InputType.Char:
this.fragmentRegex += "(" + groupNamePrefix + "[a-zA-Z])";
break;
default: default:
throw new Exception("Unrecognized InputType"); throw new Exception("Unrecognized InputType");
} }
return this; return this;
} }
public FragmentSchemaBuilder ShouldIgnoreEmptyMatches(bool ignoreEmptyMatches)
{
this.ShouldIgnoreEmptyMatches(ignoreEmptyMatches);
return this;
}
public FragmentSchemaBuilder Expect(string literal, string name = "") public FragmentSchemaBuilder Expect(string literal, string name = "")
{ {
string groupNamePrefix = ""; string groupNamePrefix = "";
@@ -130,6 +140,10 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
} }
oldSchemaBuilder.fragmentRegex += "(" + currentRegex + ")"; oldSchemaBuilder.fragmentRegex += "(" + currentRegex + ")";
var groupsToAdd = currentBuilder.namedGroups.Where(g => !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; return oldSchemaBuilder;
} }