82 lines
2.4 KiB
C#
82 lines
2.4 KiB
C#
namespace Parsing.Schema;
|
|
|
|
using Parsing.Schema;
|
|
using Parsing.Schema.BuildingBlocks;
|
|
using Parsing.Tokenization;
|
|
using System.Collections;
|
|
|
|
public class InputSchemaContext : ISchemaContext
|
|
{
|
|
public int lastProcessedBlockIndex { get; set; } = 0;
|
|
public bool HasFinished { get; set; } = false;
|
|
}
|
|
|
|
public class InputSchema : ISchema<InputSchemaContext>
|
|
{
|
|
private List<IBuildingBlock> buildingBlocks;
|
|
|
|
public InputSchema()
|
|
{
|
|
buildingBlocks = new List<IBuildingBlock>();
|
|
}
|
|
|
|
public void AddBuildingBlock(IBuildingBlock buildingBlock)
|
|
{
|
|
this.buildingBlocks.Add(buildingBlock);
|
|
}
|
|
|
|
public List<IToken> ProcessNextWord(InputSchemaContext currentContext, InputProvider inputs)
|
|
{
|
|
var nextBlock = this.buildingBlocks[currentContext.lastProcessedBlockIndex];
|
|
var tokens = nextBlock.ParseWord(inputs);
|
|
if (!nextBlock.IsRepetitionType() || nextBlock.CheckIsDoneParsingAndReset(inputs))
|
|
{
|
|
currentContext.lastProcessedBlockIndex++;
|
|
currentContext.HasFinished = currentContext.lastProcessedBlockIndex >= this.buildingBlocks.Count;
|
|
}
|
|
return tokens;
|
|
}
|
|
|
|
public bool CanProcessNextWord(InputSchemaContext currentContext, InputProvider inputs)
|
|
{
|
|
using (inputs.GetLookaheadContext())
|
|
{
|
|
if (currentContext.HasFinished)
|
|
{
|
|
return false;
|
|
}
|
|
var nextBlock = this.buildingBlocks[currentContext.lastProcessedBlockIndex];
|
|
return nextBlock.CanParseWord(inputs);
|
|
}
|
|
}
|
|
|
|
public bool CanProcessNextWord(InputSchemaContext currentContext, string word)
|
|
{
|
|
if (currentContext.HasFinished)
|
|
{
|
|
return false;
|
|
}
|
|
var nextBlock = this.buildingBlocks[currentContext.lastProcessedBlockIndex];
|
|
return nextBlock.CanParseWord(word);
|
|
}
|
|
|
|
public List<IToken> ProcessWordList(string[] words)
|
|
{
|
|
List<IToken> tokens = new List<IToken>();
|
|
InputProvider inputs = new InputProvider(words);
|
|
var overallContext = this.CreateContext();
|
|
|
|
while (this.CanProcessNextWord(overallContext, inputs))
|
|
{
|
|
tokens.AddRange(this.ProcessNextWord(overallContext, inputs));
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
public InputSchemaContext CreateContext()
|
|
{
|
|
return new InputSchemaContext();
|
|
}
|
|
}
|