72 lines
1.8 KiB
C#
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;
|
|
}
|
|
} |