Files
TextParser/TextParser/Schema/BuildingBlocks/GreedyRepetitionBlock.cs

58 lines
1.5 KiB
C#

namespace Parsing.Schema.BuildingBlocks;
using System.IO.Pipelines;
using System.Linq;
using Parsing.Tokenization;
class GreedyRepetitionBlock : BuildingBlockBase
{
private InputSchema inputSchema;
private InputSchemaContext context;
public GreedyRepetitionBlock(InputSchema inputSchema)
{
this.inputSchema = inputSchema;
this.context = this.inputSchema.CreateContext();
}
public override List<IToken> ParseWord(InputProvider inputs)
{
var result = inputSchema.ProcessNextWord(context, inputs);
if (!this.CanParseWord(inputs))
{
this.context = this.inputSchema.CreateContext();
}
return result;
}
public override bool CanParseWord(InputProvider inputs)
{
return inputSchema.CanProcessNextWord(context, inputs) && inputs.CanYieldWord();
}
public override bool CanParseWord(string word)
{
return inputSchema.CanProcessNextWord(context, word);
}
public override BlockType GetBlockType()
{
return BlockType.GreedyRepetition;
}
public override bool IsRepetitionType()
{
return true;
}
public override bool CheckIsDoneParsingAndReset(InputProvider inputs)
{
// we are done parsing greedily once the next token doesn't match anymore
var result = !this.CanParseWord(inputs);
if (result)
{
this.context = this.inputSchema.CreateContext();
}
return result;
}
}