Files
ZuneUIXTools/libs/UIX.Asm/Lexer.Body.cs
T

71 lines
2.3 KiB
C#
Raw Normal View History

using Microsoft.Iris.Asm.Models;
using Sprache;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Iris.Asm;
partial class Lexer
{
private static IResult<IBodyItem> ParseBodyItem(IInput input)
{
input = ConsumeWhitespace(input);
2024-02-04 22:09:21 -06:00
var line = input.Line;
var column = input.Column;
2024-02-05 09:23:46 -06:00
var directiveResult = ParseDirective(input);
if (directiveResult.WasSuccessful)
{
input = directiveResult.Remainder;
var directive = directiveResult.Value;
if (directive is not IBodyItem directiveBodyItem)
return Result.Failure<IBodyItem>(input, "Invalid code", [$"The {directive.Identifier} directive cannot be placed in the body of a program"]);
directive.Line = line;
directive.Column = column;
return Result.Success(directiveBodyItem, input);
}
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
if (!identifierResult.WasSuccessful)
2024-02-05 09:23:46 -06:00
return Result.Failure<IBodyItem>(input, "Invalid code", []);
input = identifierResult.Remainder;
var identifier = identifierResult.Value;
IBodyItem bodyItem;
if (!input.AtEnd && input.Current == ':')
{
input = input.Advance();
2024-02-04 22:09:21 -06:00
bodyItem = new Label(identifier)
{
Line = line, Column = column,
};
input = StatementEnd(input).Remainder;
}
else
{
List<Operand> operands = new();
var endOfInstructionResult = StatementEnd(input);
input = endOfInstructionResult.Remainder;
if (!endOfInstructionResult.WasSuccessful)
{
input = ConsumeWhitespace(input);
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
2024-02-04 22:09:21 -06:00
operands.AddRange(operandsResult.Value.Select(s => new Operand(s) { Line = line }));
input = operandsResult.Remainder;
}
2024-02-04 22:09:21 -06:00
bodyItem = new Instruction(identifier, operands)
{
Line = line,
Column = column,
};
}
return Result.Success(bodyItem, input);
}
}