2024-02-04 21:44:53 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Iris.Asm;
|
|
|
|
|
|
|
|
|
|
partial class Lexer
|
|
|
|
|
{
|
|
|
|
|
private static IResult<IImport> ParseImport(IInput input)
|
|
|
|
|
{
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
var importDirectiveResult = Parse.String(".import")(input);
|
2024-02-04 21:44:53 -06:00
|
|
|
input = importDirectiveResult.Remainder;
|
|
|
|
|
if (!importDirectiveResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImport>(input, "Invalid import directive", ["Expected '.import'"]);
|
|
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
return ParseImportAsDirective(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IResult<IImport> ParseImportAsDirective(IInput input)
|
|
|
|
|
{
|
|
|
|
|
if (input.Current != '-' || input.AtEnd)
|
|
|
|
|
return Result.Failure<IImport>(input, "Invalid import type", ["An import type must be specified"]);
|
|
|
|
|
input = input.Advance();
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
var importTypeResult = WordText(input);
|
2024-02-04 21:44:53 -06:00
|
|
|
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;
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
case "TYPE":
|
2024-02-10 21:37:12 -06:00
|
|
|
var typePrefixResult = Identifier.Token()(input);
|
2024-02-08 10:50:57 -06:00
|
|
|
input = typePrefixResult.Remainder;
|
|
|
|
|
if (!typePrefixResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImport>(input, "Invalid type import", ["Expected a valid namespace prefix"]);
|
|
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
var typeNamespaceDelimitterResult = Parse.Char(':')(input);
|
|
|
|
|
input = typeNamespaceDelimitterResult.Remainder;
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
string typeName, typePrefix;
|
|
|
|
|
if (typeNamespaceDelimitterResult.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
var typeNameResult = Identifier(input);
|
|
|
|
|
input = typeNameResult.Remainder;
|
|
|
|
|
if (!typeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImport>(input, "Invalid type import", ["Expected a valid type name"]);
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
typePrefix = typePrefixResult.Value;
|
|
|
|
|
typeName = typeNameResult.Value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
typePrefix = null;
|
|
|
|
|
typeName = typePrefixResult.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
import = new TypeImport(typePrefix, typeName);
|
2024-02-08 10:50:57 -06:00
|
|
|
break;
|
|
|
|
|
|
2024-02-04 21:44:53 -06:00
|
|
|
default:
|
|
|
|
|
return Result.Failure<IImport>(input, $"Unknown import type '{importTypeResult.Value}'", ["Expected 'ns'"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.Success(import, input);
|
|
|
|
|
}
|
|
|
|
|
}
|