2024-02-04 21:44:53 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Iris.Asm;
|
|
|
|
|
|
|
|
|
|
partial class Lexer
|
|
|
|
|
{
|
2024-02-10 23:03:57 -06:00
|
|
|
private static IResult<IImportDirective> ParseImport(IInput input)
|
2024-02-04 21:44:53 -06:00
|
|
|
{
|
|
|
|
|
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)
|
2024-02-10 23:03:57 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid import directive", ["Expected '.import'"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
return ParseImportAsDirective(input);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
private static IResult<IImportDirective> ParseImportAsDirective(IInput input)
|
2024-02-05 09:23:46 -06:00
|
|
|
{
|
|
|
|
|
if (input.Current != '-' || input.AtEnd)
|
2024-02-10 23:03:57 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid import type", ["An import type must be specified"]);
|
2024-02-05 09:23:46 -06:00
|
|
|
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)
|
2024-02-10 23:03:57 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid import type", ["Expected 'ns'"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
IImportDirective import;
|
2024-02-04 21:44:53 -06:00
|
|
|
switch (importTypeResult.Value.ToUpperInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "NS":
|
|
|
|
|
var uriResult = Uri.Token()(input);
|
|
|
|
|
input = uriResult.Remainder;
|
|
|
|
|
if (!uriResult.WasSuccessful)
|
2024-02-10 23:03:57 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid URI", ["Expected a valid URI"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
|
|
|
|
|
input = Parse.String("as").Token()(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var nameResult = AlphanumericText(input);
|
|
|
|
|
input = nameResult.Remainder;
|
|
|
|
|
if (!nameResult.WasSuccessful)
|
2024-02-10 23:03:57 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid namespace alias", ["Expected a valid namespace alias"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
|
|
|
|
|
import = new NamespaceImport(uriResult.Value, nameResult.Value);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
case "TYPE":
|
2024-02-11 20:38:19 -06:00
|
|
|
input = ConsumeWhitespace(input);
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
var typeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = typeNameResult.Remainder;
|
|
|
|
|
if (!typeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid type import", ["Expected qualified type name"]);
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
import = new TypeImport(typeNameResult.Value);
|
2024-02-08 10:50:57 -06:00
|
|
|
break;
|
|
|
|
|
|
2024-02-16 20:37:47 -06:00
|
|
|
case "MBRS":
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var memberTypeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = memberTypeNameResult.Remainder;
|
|
|
|
|
if (!memberTypeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid member import", ["Expected qualified type name"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('{')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var memberNamesResult = Parse.Ref(() => Identifier).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = memberNamesResult.Remainder;
|
|
|
|
|
if (!memberNamesResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid member import", ["Expected list of members to import"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('}')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
import = new NamedMemberImport(memberTypeNameResult.Value, memberNamesResult.Value);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "CTOR":
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var ctorMemberTypeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = ctorMemberTypeNameResult.Remainder;
|
|
|
|
|
if (!ctorMemberTypeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid constructor import", ["Expected qualified type name"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('(')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var ctorParameterTypesResult = Parse.Ref(() => QualifiedTypeName).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = ctorParameterTypesResult.Remainder;
|
|
|
|
|
if (!ctorParameterTypesResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid constructor import", ["Expected constructor parameter types"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char(')')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
import = new ConstructorImport(ctorMemberTypeNameResult.Value, ctorParameterTypesResult.Value);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "MTHD":
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var mthdMemberTypeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = mthdMemberTypeNameResult.Remainder;
|
|
|
|
|
if (!mthdMemberTypeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid method import", ["Expected qualified type name"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('.')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var mthdNameResult = Identifier(input);
|
|
|
|
|
input = mthdNameResult.Remainder;
|
|
|
|
|
if (!mthdNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid method import", ["Expected method name"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('(')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var mthdParameterTypesResult = Parse.Ref(() => QualifiedTypeName).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = mthdParameterTypesResult.Remainder;
|
|
|
|
|
if (!mthdParameterTypesResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IImportDirective>(input, "Invalid constructor import", ["Expected constructor parameter types"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char(')')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
import = new MethodImport(mthdMemberTypeNameResult.Value, mthdNameResult.Value, mthdParameterTypesResult.Value);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-02-04 21:44:53 -06:00
|
|
|
default:
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, $"Unknown import type '{importTypeResult.Value}'", ["Expected 'ns', 'type'"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.Success(import, input);
|
|
|
|
|
}
|
|
|
|
|
}
|