fix: some bugfixes with fragment parser logic, ref: NOISSUE

This commit is contained in:
Simon Diesenreiter 2024-12-03 21:32:02 +01:00
parent c1705d9f96
commit 09bbba1293
8 changed files with 25 additions and 25 deletions

View File

@ -17,23 +17,7 @@ on:
workflow_dispatch: workflow_dispatch:
jobs: jobs:
linter:
strategy:
fail-fast: false
matrix:
dotnet-version: [9.0.X]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Run linter
run: make lint
tests_linux: tests_linux:
needs: linter
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:

View File

@ -18,7 +18,7 @@ public class TextParserTests
private const string testInput5 = @"asdfnums(2,5,3)ght private const string testInput5 = @"asdfnums(2,5,3)ght
cv strs(test) jh 4,3,2 cv strs(test) jh 4,3,2
34,54,2nums(2,8) strs(aa,ab,ba,bb)aa,bb"; 34,54,2nums(2,8) strs(aa,ab,ba,bb)aa,bb";
[Fact] [Fact]
@ -230,7 +230,7 @@ public class TextParserTests
.EndOptions() .EndOptions()
.Build(); .Build();
var parser = new TextParser<FragmentSchemaContext>(schema); var parser = TextParser.Create(schema);
var fragmentData = parser var fragmentData = parser
.SetInputText(testInput5) .SetInputText(testInput5)
.Parse() .Parse()

View File

@ -31,7 +31,7 @@ class FixedRepetitionBlock : BuildingBlockBase
this.context = this.inputSchema.CreateContext(); this.context = this.inputSchema.CreateContext();
} }
} }
return result.SingleOrDefault(); return result.Single();
} }
public override bool CanParseWord(InputProvider inputs) public override bool CanParseWord(InputProvider inputs)

View File

@ -22,7 +22,7 @@ class GreedyRepetitionBlock : BuildingBlockBase
{ {
this.context = this.inputSchema.CreateContext(); this.context = this.inputSchema.CreateContext();
} }
return result.SingleOrDefault(); return result.Single();
} }
public override bool CanParseWord(InputProvider inputs) public override bool CanParseWord(InputProvider inputs)

View File

@ -100,6 +100,10 @@ public class FragmentSchemaBuilder : RepetitionSchemaBuilder<FragmentSchemaBuild
throw new Exception("Invalid repetition definitions!"); throw new Exception("Invalid repetition definitions!");
} }
var oldSchemaBuilder = currentBuilder.UpperLayerBuilder; var oldSchemaBuilder = currentBuilder.UpperLayerBuilder;
if (oldSchemaBuilder == null)
{
throw new Exception("Something went terribly wrong!");
}
var currentRegex = "(" + currentBuilder.fragmentRegex + ")"; var currentRegex = "(" + currentBuilder.fragmentRegex + ")";
switch (currentBuilder.RepetitionType) switch (currentBuilder.RepetitionType)

View File

@ -55,22 +55,26 @@ public class InputSchemaBuilder : RepetitionSchemaBuilder<InputSchemaBuilder, In
{ {
throw new Exception("Invalid repetition definitions!"); throw new Exception("Invalid repetition definitions!");
} }
var oldInputSchemaBuilder = currentBuilder.UpperLayerBuilder; var oldSchemaBuilder = currentBuilder.UpperLayerBuilder;
if (oldSchemaBuilder == null)
{
throw new Exception("Something went terribly wrong!");
}
var currentSchema = currentBuilder.Build(); var currentSchema = currentBuilder.Build();
switch (currentBuilder.RepetitionType) switch (currentBuilder.RepetitionType)
{ {
case RepetitionType.FixedRepetition: case RepetitionType.FixedRepetition:
oldInputSchemaBuilder.schema.AddBuildingBlock(new FixedRepetitionBlock(currentSchema, currentBuilder.NumRepetition)); oldSchemaBuilder.schema.AddBuildingBlock(new FixedRepetitionBlock(currentSchema, currentBuilder.NumRepetition));
break; break;
case RepetitionType.GreedyRepetition: case RepetitionType.GreedyRepetition:
oldInputSchemaBuilder.schema.AddBuildingBlock(new GreedyRepetitionBlock(currentSchema)); oldSchemaBuilder.schema.AddBuildingBlock(new GreedyRepetitionBlock(currentSchema));
break; break;
default: default:
throw new Exception("Unrecognized RepetitionType"); throw new Exception("Unrecognized RepetitionType");
} }
return oldInputSchemaBuilder; return oldSchemaBuilder;
} }
public InputSchema Build() public InputSchema Build()

View File

@ -5,6 +5,14 @@ using System.Collections.Generic;
using Parsing.Schema; using Parsing.Schema;
using Parsing.Tokenization; using Parsing.Tokenization;
public static class TextParser
{
public static TextParser<TContext> Create<TContext>(ISchema<TContext> schema, string[]? delimiters = null, bool removeEmptyEntries = true) where TContext : ISchemaContext
{
return new TextParser<TContext>(schema, delimiters, removeEmptyEntries);
}
}
public class TextParser<T> : TokenConverter where T : ISchemaContext public class TextParser<T> : TokenConverter where T : ISchemaContext
{ {
private LineParser<T> lineParser; private LineParser<T> lineParser;

View File

@ -56,7 +56,7 @@ public static class DataManipulationHelpers
{ {
if (data.Count < 2) if (data.Count < 2)
{ {
return data[0] ?? default(TType); return data[0];
} }
TType result = data[0]; TType result = data[0];
for (int i = 1; i < data.Count; i++) for (int i = 1; i < data.Count; i++)