2024-02-04 21:44:53 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
2025-01-29 08:01:24 -06:00
|
|
|
using System.Collections.Generic;
|
2024-02-04 21:44:53 -06:00
|
|
|
|
|
|
|
|
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-07-07 15:35:44 -05:00
|
|
|
var line = input.Line;
|
|
|
|
|
var col = input.Column;
|
|
|
|
|
|
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)
|
2025-01-29 08:01:24 -06:00
|
|
|
return uriResult.ForType<IImportDirective>();
|
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
|
|
|
|
2024-07-07 15:35:44 -05:00
|
|
|
import = new NamespaceImport(uriResult.Value, nameResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
2024-02-04 21:44:53 -06:00
|
|
|
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)
|
2025-01-29 08:01:24 -06:00
|
|
|
return typeNameResult.ForType<IImportDirective>();
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-07-07 15:35:44 -05:00
|
|
|
import = new TypeImport(typeNameResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
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)
|
2025-01-29 08:01:24 -06:00
|
|
|
return memberTypeNameResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
|
|
|
|
input = Parse.Char('{')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var memberNamesResult = Parse.Ref(() => Identifier).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = memberNamesResult.Remainder;
|
|
|
|
|
if (!memberNamesResult.WasSuccessful)
|
2025-01-29 08:01:24 -06:00
|
|
|
return memberNamesResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
|
|
|
|
input = Parse.Char('}')(input).Remainder;
|
|
|
|
|
|
2024-07-07 15:35:44 -05:00
|
|
|
import = new NamedMemberImport(memberTypeNameResult.Value, memberNamesResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
2024-02-16 20:37:47 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "CTOR":
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var ctorMemberTypeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = ctorMemberTypeNameResult.Remainder;
|
|
|
|
|
if (!ctorMemberTypeNameResult.WasSuccessful)
|
2025-01-29 08:01:24 -06:00
|
|
|
return ctorMemberTypeNameResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
2025-01-28 15:36:23 -06:00
|
|
|
input = Parse.Char('.').Optional()(input).Remainder;
|
2024-02-16 20:37:47 -06:00
|
|
|
input = Parse.Char('(')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var ctorParameterTypesResult = Parse.Ref(() => QualifiedTypeName).DelimitedBy(Parse.Char(',').Token())(input);
|
|
|
|
|
input = ctorParameterTypesResult.Remainder;
|
|
|
|
|
if (!ctorParameterTypesResult.WasSuccessful)
|
2025-01-29 08:01:24 -06:00
|
|
|
return ctorParameterTypesResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
|
|
|
|
input = Parse.Char(')')(input).Remainder;
|
|
|
|
|
|
2024-07-07 15:35:44 -05:00
|
|
|
import = new ConstructorImport(ctorMemberTypeNameResult.Value, ctorParameterTypesResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
2024-02-16 20:37:47 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "MTHD":
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
|
|
|
|
|
var mthdMemberTypeNameResult = ParseQualifiedTypeName(input);
|
|
|
|
|
input = mthdMemberTypeNameResult.Remainder;
|
|
|
|
|
if (!mthdMemberTypeNameResult.WasSuccessful)
|
2025-01-29 08:01:24 -06:00
|
|
|
return mthdMemberTypeNameResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
|
|
|
|
input = Parse.Char('.')(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var mthdNameResult = Identifier(input);
|
|
|
|
|
input = mthdNameResult.Remainder;
|
|
|
|
|
if (!mthdNameResult.WasSuccessful)
|
2025-01-29 08:01:24 -06:00
|
|
|
return mthdNameResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
|
|
|
|
input = Parse.Char('(')(input).Remainder;
|
|
|
|
|
|
2025-01-29 08:01:24 -06:00
|
|
|
IEnumerable<QualifiedTypeName> mthdParameterTypes;
|
|
|
|
|
if (input.Current == ')')
|
|
|
|
|
{
|
|
|
|
|
input = input.Advance();
|
|
|
|
|
mthdParameterTypes = [];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var mthdParameterTypesResult = Parse.Ref(() => QualifiedTypeName)
|
|
|
|
|
.DelimitedBy(Parse.Char(',').Token(), 0, null)(input);
|
|
|
|
|
input = mthdParameterTypesResult.Remainder;
|
|
|
|
|
if (!mthdParameterTypesResult.WasSuccessful)
|
|
|
|
|
return mthdParameterTypesResult.ForType<IImportDirective>();
|
2024-02-16 20:37:47 -06:00
|
|
|
|
2025-01-29 08:01:24 -06:00
|
|
|
input = Parse.Char(')')(input).Remainder;
|
|
|
|
|
mthdParameterTypes = mthdParameterTypesResult.Value;
|
|
|
|
|
}
|
2024-02-16 20:37:47 -06:00
|
|
|
|
2025-01-29 08:01:24 -06:00
|
|
|
import = new MethodImport(mthdMemberTypeNameResult.Value, mthdNameResult.Value, mthdParameterTypes)
|
2024-07-07 15:35:44 -05:00
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
2024-02-16 20:37:47 -06:00
|
|
|
break;
|
|
|
|
|
|
2024-02-04 21:44:53 -06:00
|
|
|
default:
|
2025-01-29 08:01:24 -06:00
|
|
|
return Result.Failure<IImportDirective>(input, $"Unknown import type '{importTypeResult.Value}'", ["Expected 'ns', 'type', 'ctor', 'mbrs', 'mthd'"]);
|
2024-02-04 21:44:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.Success(import, input);
|
|
|
|
|
}
|
|
|
|
|
}
|