mirror of
https://github.com/ZuneDev/ZuneUIXTools.git
synced 2026-07-27 13:11:59 -07:00
Move body and import lexers to separate files
This commit is contained in:
+1
-85
@@ -1,11 +1,10 @@
|
||||
using Microsoft.Iris.Asm.Models;
|
||||
using Sprache;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.Iris.Asm;
|
||||
|
||||
public static class Lexer
|
||||
public static partial class Lexer
|
||||
{
|
||||
public static readonly Parser<string> AlphanumericText = Parse.LetterOrDigit.AtLeastOnce().Text();
|
||||
|
||||
@@ -18,93 +17,10 @@ public static class Lexer
|
||||
from sectionId in Parse.Letter.AtLeastOnce().Text()
|
||||
select sectionId;
|
||||
|
||||
public static readonly Parser<IImport> Import = ParseImport;
|
||||
|
||||
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)
|
||||
{
|
||||
input = ConsumeWhitespace(input);
|
||||
|
||||
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
|
||||
if (!identifierResult.WasSuccessful)
|
||||
return Result.Failure<Instruction>(input, "Invalid code", []);
|
||||
|
||||
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)
|
||||
{
|
||||
input = ConsumeWhitespace(input);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private static IResult<IImport> ParseImport(IInput input)
|
||||
{
|
||||
input = ConsumeWhitespace(input);
|
||||
|
||||
var importDirectiveResult = Parse.String(".import-")(input);
|
||||
input = importDirectiveResult.Remainder;
|
||||
if (!importDirectiveResult.WasSuccessful)
|
||||
return Result.Failure<IImport>(input, "Invalid import directive", ["Expected '.import'"]);
|
||||
|
||||
var importTypeResult = Parse.Letter.AtLeastOnce().Text()(input);
|
||||
input = importTypeResult.Remainder;
|
||||
if (!importTypeResult.WasSuccessful)
|
||||
return Result.Failure<IImport>(input, "Invalid import type", ["Expected 'ns'"]);
|
||||
|
||||
IImport import;
|
||||
switch (importTypeResult.Value.ToUpperInvariant())
|
||||
{
|
||||
case "NS":
|
||||
var uriResult = Uri.Token()(input);
|
||||
input = uriResult.Remainder;
|
||||
if (!uriResult.WasSuccessful)
|
||||
return Result.Failure<IImport>(input, "Invalid URI", ["Expected a valid URI"]);
|
||||
|
||||
input = Parse.String("as").Token()(input).Remainder;
|
||||
|
||||
var nameResult = AlphanumericText(input);
|
||||
input = nameResult.Remainder;
|
||||
if (!nameResult.WasSuccessful)
|
||||
return Result.Failure<IImport>(input, "Invalid namespace alias", ["Expected a valid namespace alias"]);
|
||||
|
||||
import = new NamespaceImport(uriResult.Value, nameResult.Value);
|
||||
break;
|
||||
|
||||
default:
|
||||
return Result.Failure<IImport>(input, $"Unknown import type '{importTypeResult.Value}'", ["Expected 'ns'"]);
|
||||
}
|
||||
|
||||
return Result.Success(import, input);
|
||||
}
|
||||
|
||||
private static IInput ConsumeWhitespace(IInput input) => Parse.WhiteSpace.Many()(input).Remainder;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user