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<Import> Import =
|
||||
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<IImport> Import = ParseImport;
|
||||
|
||||
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
|
||||
|
||||
@@ -29,8 +23,7 @@ public static class Lexer
|
||||
|
||||
private static IResult<IBodyItem> ParseBodyItem(IInput input)
|
||||
{
|
||||
var trimWhitespaceResult = Parse.WhiteSpace.Many()(input);
|
||||
input = trimWhitespaceResult.Remainder;
|
||||
input = ConsumeWhitespace(input);
|
||||
|
||||
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
|
||||
if (!identifierResult.WasSuccessful)
|
||||
@@ -54,7 +47,7 @@ public static class Lexer
|
||||
|
||||
if (!endOfInstructionResult.WasSuccessful)
|
||||
{
|
||||
input = Parse.WhiteSpace.Many()(input).Remainder;
|
||||
input = ConsumeWhitespace(input);
|
||||
|
||||
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
|
||||
operands.AddRange(operandsResult.Value.Select(s => new Operand(s)));
|
||||
@@ -66,4 +59,46 @@ public static class Lexer
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user