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

111 lines
4.0 KiB
C#
Raw Normal View History

2024-01-31 09:21:30 -06:00
using Microsoft.Iris.Asm.Models;
using Sprache;
2024-01-29 21:42:06 -06:00
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Iris.Asm;
2024-01-29 21:42:06 -06:00
public static class Lexer
{
public static readonly Parser<string> AlphanumericText = Parse.LetterOrDigit.AtLeastOnce().Text();
public static readonly Parser<string> Uri = Parse.LetterOrDigit.Or(Parse.Chars(":/!._-")).AtLeastOnce().Text();
public static readonly Parser<string> StatementEnd = Parse.Char(';').Return(";").Or(Parse.LineTerminator);
public static readonly Parser<string> SectionDirective =
from _ in Parse.String(".section").Token()
from sectionId in Parse.Letter.AtLeastOnce().Text()
select sectionId;
2024-01-30 22:44:48 -06:00
public static readonly Parser<IImport> Import = ParseImport;
2024-01-29 21:42:06 -06:00
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
public static readonly Parser<Program> Program =
from imports in Import.Token().Many()
from body in BodyItem.Many()
select new Program(imports, body);
private static IResult<IBodyItem> ParseBodyItem(IInput input)
{
2024-01-30 22:44:48 -06:00
input = ConsumeWhitespace(input);
2024-01-29 21:42:06 -06:00
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
if (!identifierResult.WasSuccessful)
2024-02-04 18:51:17 -06:00
return Result.Failure<Instruction>(input, "Invalid code", []);
2024-01-29 21:42:06 -06:00
input = identifierResult.Remainder;
var identifier = identifierResult.Value;
IBodyItem bodyItem;
if (!input.AtEnd && input.Current == ':')
{
input = input.Advance();
bodyItem = new Label(identifier);
input = StatementEnd(input).Remainder;
}
else
{
List<Operand> operands = new();
var endOfInstructionResult = StatementEnd(input);
input = endOfInstructionResult.Remainder;
if (!endOfInstructionResult.WasSuccessful)
{
2024-01-30 22:44:48 -06:00
input = ConsumeWhitespace(input);
2024-01-29 21:42:06 -06:00
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
operands.AddRange(operandsResult.Value.Select(s => new Operand(s)));
input = operandsResult.Remainder;
}
bodyItem = new Instruction(identifier, operands);
}
return Result.Success(bodyItem, input);
}
2024-01-30 22:44:48 -06:00
private static IResult<IImport> ParseImport(IInput input)
{
input = ConsumeWhitespace(input);
var importDirectiveResult = Parse.String(".import-")(input);
input = importDirectiveResult.Remainder;
if (!importDirectiveResult.WasSuccessful)
2024-02-04 18:51:17 -06:00
return Result.Failure<IImport>(input, "Invalid import directive", ["Expected '.import'"]);
2024-01-30 22:44:48 -06:00
var importTypeResult = Parse.Letter.AtLeastOnce().Text()(input);
input = importTypeResult.Remainder;
if (!importTypeResult.WasSuccessful)
2024-02-04 18:51:17 -06:00
return Result.Failure<IImport>(input, "Invalid import type", ["Expected 'ns'"]);
2024-01-30 22:44:48 -06:00
IImport import;
switch (importTypeResult.Value.ToUpperInvariant())
{
case "NS":
var uriResult = Uri.Token()(input);
input = uriResult.Remainder;
if (!uriResult.WasSuccessful)
2024-02-04 18:51:17 -06:00
return Result.Failure<IImport>(input, "Invalid URI", ["Expected a valid URI"]);
2024-01-30 22:44:48 -06:00
input = Parse.String("as").Token()(input).Remainder;
var nameResult = AlphanumericText(input);
input = nameResult.Remainder;
if (!nameResult.WasSuccessful)
2024-02-04 18:51:17 -06:00
return Result.Failure<IImport>(input, "Invalid namespace alias", ["Expected a valid namespace alias"]);
2024-01-30 22:44:48 -06:00
import = new NamespaceImport(uriResult.Value, nameResult.Value);
break;
default:
2024-02-04 18:51:17 -06:00
return Result.Failure<IImport>(input, $"Unknown import type '{importTypeResult.Value}'", ["Expected 'ns'"]);
2024-01-30 22:44:48 -06:00
}
return Result.Success(import, input);
}
private static IInput ConsumeWhitespace(IInput input) => Parse.WhiteSpace.Many()(input).Remainder;
2024-01-29 21:42:06 -06:00
}