mirror of
https://github.com/ZuneDev/ZuneUIXTools.git
synced 2026-07-27 13:11:59 -07:00
Allow for different kinds of imports
This commit is contained in:
+45
-10
@@ -12,13 +12,7 @@ public static class Lexer
|
|||||||
|
|
||||||
public static readonly Parser<string> StatementEnd = Parse.Char(';').Return(";").Or(Parse.LineTerminator);
|
public static readonly Parser<string> StatementEnd = Parse.Char(';').Return(";").Or(Parse.LineTerminator);
|
||||||
|
|
||||||
public static readonly Parser<Import> Import =
|
public static readonly Parser<IImport> Import = ParseImport;
|
||||||
from _ in Parse.String(".import").Token()
|
|
||||||
from uri in Uri.Token()
|
|
||||||
from __ in Parse.String("as").Token()
|
|
||||||
from name in AlphanumericText
|
|
||||||
from end in StatementEnd
|
|
||||||
select new Import(uri, name);
|
|
||||||
|
|
||||||
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
|
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
|
||||||
|
|
||||||
@@ -29,8 +23,7 @@ public static class Lexer
|
|||||||
|
|
||||||
private static IResult<IBodyItem> ParseBodyItem(IInput input)
|
private static IResult<IBodyItem> ParseBodyItem(IInput input)
|
||||||
{
|
{
|
||||||
var trimWhitespaceResult = Parse.WhiteSpace.Many()(input);
|
input = ConsumeWhitespace(input);
|
||||||
input = trimWhitespaceResult.Remainder;
|
|
||||||
|
|
||||||
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
|
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
|
||||||
if (!identifierResult.WasSuccessful)
|
if (!identifierResult.WasSuccessful)
|
||||||
@@ -54,7 +47,7 @@ public static class Lexer
|
|||||||
|
|
||||||
if (!endOfInstructionResult.WasSuccessful)
|
if (!endOfInstructionResult.WasSuccessful)
|
||||||
{
|
{
|
||||||
input = Parse.WhiteSpace.Many()(input).Remainder;
|
input = ConsumeWhitespace(input);
|
||||||
|
|
||||||
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
|
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
|
||||||
operands.AddRange(operandsResult.Value.Select(s => new Operand(s)));
|
operands.AddRange(operandsResult.Value.Select(s => new Operand(s)));
|
||||||
@@ -66,4 +59,46 @@ public static class Lexer
|
|||||||
|
|
||||||
return Result.Success(bodyItem, input);
|
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", null);
|
||||||
|
|
||||||
|
var importTypeResult = Parse.Letter.AtLeastOnce().Text()(input);
|
||||||
|
input = importTypeResult.Remainder;
|
||||||
|
if (!importTypeResult.WasSuccessful)
|
||||||
|
return Result.Failure<IImport>(input, "Invalid import type", null);
|
||||||
|
|
||||||
|
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", null);
|
||||||
|
|
||||||
|
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", null);
|
||||||
|
|
||||||
|
import = new NamespaceImport(uriResult.Value, nameResult.Value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return Result.Failure<IImport>(input, $"Unknown import type '{importTypeResult.Value}'", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success(import, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IInput ConsumeWhitespace(IInput input) => Parse.WhiteSpace.Many()(input).Remainder;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
namespace Microsoft.Iris.Asm;
|
namespace Microsoft.Iris.Asm;
|
||||||
|
|
||||||
public interface IBodyItem { }
|
public interface IBodyItem { }
|
||||||
|
public interface IImport { }
|
||||||
|
|
||||||
public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : IBodyItem
|
public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : IBodyItem
|
||||||
{
|
{
|
||||||
@@ -24,12 +25,12 @@ public record Operand(string Content)
|
|||||||
public override string ToString() => Content;
|
public override string ToString() => Content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Import(string Uri, string Name)
|
public record NamespaceImport(string Uri, string Name) : IImport
|
||||||
{
|
{
|
||||||
public override string ToString() => $".import {Uri} as {Name}";
|
public override string ToString() => $".import-ns {Uri} as {Name}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public record Program(IEnumerable<Import> Imports, IEnumerable<IBodyItem> Body)
|
public record Program(IEnumerable<IImport> Imports, IEnumerable<IBodyItem> Body)
|
||||||
{
|
{
|
||||||
public override string ToString() => string.Join("\r\n", Imports.Select(i => i.ToString()).Concat(Body.Select(b => b.ToString())));
|
public override string ToString() => string.Join("\r\n", Imports.Select(i => i.ToString()).Concat(Body.Select(b => b.ToString())));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user