Files
TextParser/TextParser/Schema/BuildingBlocks/FixedRepetitionBlock.cs
Simon Diesenreiter a4e4ee2b85
Some checks failed
Upload Python Package / Create Release (push) Successful in 1m6s
Upload Python Package / deploy (push) Failing after 1m5s
feat: added initial implementation of TextParser, ref: A24-3
2024-12-01 21:31:50 +01:00

72 lines
1.8 KiB
C#

namespace Parsing.Schema.BuildingBlocks;
using System.IO.Pipelines;
using Parsing.Tokenization;
class FixedRepetitionBlock : BuildingBlockBase
{
private InputSchema inputSchema;
private InputSchemaContext context;
private int repetitionCount;
private int initRepetitionCount;
public FixedRepetitionBlock(InputSchema inputSchema, int repetitionCount)
{
this.inputSchema = inputSchema;
this.repetitionCount = repetitionCount;
this.initRepetitionCount = repetitionCount;
this.context = this.inputSchema.CreateContext();
}
public override IToken ParseWord(InputProvider inputs)
{
var result = inputSchema.ProcessNextWord(context, inputs);
if (context.HasFinished)
{
this.repetitionCount--;
if (this.repetitionCount > 0)
{
this.context = this.inputSchema.CreateContext();
}
}
return result;
}
public override bool CanParseWord(InputProvider inputs)
{
bool result;
if (this.repetitionCount == 0)
{
result = false;
}
else
{
result = inputSchema.CanProcessNextWord(context, inputs);
}
return result;
}
public override BlockType GetBlockType()
{
return BlockType.FixedRepetition;
}
public override bool IsRepetitionType()
{
return true;
}
public override bool CheckIsDoneParsingAndReset()
{
// we are done parsing once all repetitions are exhausted
var result = this.repetitionCount == 0;
if (result)
{
this.repetitionCount = this.initRepetitionCount;
this.context = this.inputSchema.CreateContext();
}
return result;
}
}