2024-02-05 09:23:46 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
2024-02-08 10:50:57 -06:00
|
|
|
using System.Linq;
|
2024-02-05 09:23:46 -06:00
|
|
|
|
|
|
|
|
namespace Microsoft.Iris.Asm;
|
|
|
|
|
|
|
|
|
|
partial class Lexer
|
|
|
|
|
{
|
|
|
|
|
private static IResult<IDirective> ParseDirective(IInput input)
|
|
|
|
|
{
|
|
|
|
|
input = ConsumeWhitespace(input);
|
|
|
|
|
var line = input.Line;
|
|
|
|
|
var column = input.Column;
|
|
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
var directiveBeginResult = Parse.Char('.')(input);
|
2024-02-05 09:23:46 -06:00
|
|
|
input = directiveBeginResult.Remainder;
|
|
|
|
|
if (!directiveBeginResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid directive", ["Expected '.', followed by an identifier"]);
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
var directiveIdResult = WordText(input);
|
2024-02-05 09:23:46 -06:00
|
|
|
input = directiveIdResult.Remainder;
|
|
|
|
|
if (!directiveIdResult.WasSuccessful)
|
2024-02-10 21:37:12 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid directive", ["Expected a valid directive name"]);
|
2024-02-05 09:23:46 -06:00
|
|
|
|
|
|
|
|
IDirective directive;
|
|
|
|
|
switch (directiveIdResult.Value.ToUpperInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "IMPORT":
|
|
|
|
|
return ParseImportAsDirective(input);
|
|
|
|
|
|
|
|
|
|
case "SECTION":
|
|
|
|
|
if (StatementEnd(input).WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid section directive", ["Expected a section name"]);
|
2024-02-05 09:23:46 -06:00
|
|
|
|
|
|
|
|
var sectionNameResult = Parse.Letter.AtLeastOnce().Token().Text()(input);
|
|
|
|
|
input = sectionNameResult.Remainder;
|
|
|
|
|
if (!sectionNameResult.WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid section name", ["Expected a section name containing only letters"]);
|
2024-02-05 09:23:46 -06:00
|
|
|
|
|
|
|
|
directive = new SectionDirective(sectionNameResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = column,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
case "CONSTANT":
|
|
|
|
|
if (StatementEnd(input).WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected a consant declaration"]);
|
|
|
|
|
|
|
|
|
|
var constNameResult = Identifier.Token()(input);
|
|
|
|
|
input = constNameResult.Remainder;
|
|
|
|
|
if (!constNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected a name for the constant"]);
|
|
|
|
|
|
|
|
|
|
input = Parse.Char('=').Token()(input).Remainder;
|
|
|
|
|
|
|
|
|
|
var typeNameResult = QualifiedTypeName.Token()(input);
|
|
|
|
|
input = typeNameResult.Remainder;
|
|
|
|
|
if (!typeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected qualified name of type to construct"]);
|
|
|
|
|
|
|
|
|
|
var openBracketResult = Parse.Char('(').Token()(input);
|
|
|
|
|
input = openBracketResult.Remainder;
|
|
|
|
|
if (!openBracketResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected '('"]);
|
|
|
|
|
|
|
|
|
|
var contentResult = Parse.CharExcept(')').AtLeastOnce().Text().Token()(input);
|
|
|
|
|
input = contentResult.Remainder;
|
|
|
|
|
if (!contentResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected constant value"]);
|
|
|
|
|
|
|
|
|
|
var closeBracketResult = Parse.Char(')').Token()(input);
|
|
|
|
|
input = closeBracketResult.Remainder;
|
|
|
|
|
if (!closeBracketResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<IDirective>(input, "Invalid constant directive", ["Expected ')'"]);
|
|
|
|
|
|
|
|
|
|
directive = new ConstantDirective(constNameResult.Value, typeNameResult.Value, contentResult.Value)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = column,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
case "EXPORT":
|
|
|
|
|
if (StatementEnd(input).WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid export directive", ["Expected export information"]);
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
var labelPrefixResult = Identifier.Token()(input);
|
2024-02-08 10:50:57 -06:00
|
|
|
input = labelPrefixResult.Remainder;
|
|
|
|
|
if (!labelPrefixResult.WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid export directive", ["Expected prefix of labels to export"]);
|
2024-02-08 10:50:57 -06:00
|
|
|
|
2024-02-08 13:32:44 -06:00
|
|
|
var listenerCountResult = WholeNumber.Token()(input);
|
|
|
|
|
input = listenerCountResult.Remainder;
|
|
|
|
|
if (!listenerCountResult.WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid export directive", ["Expected listener count"]);
|
2024-02-08 13:32:44 -06:00
|
|
|
|
|
|
|
|
if (!uint.TryParse(listenerCountResult.Value, out var listenerCount))
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid export directive", ["Expected export listener count to be an unsigned integer"]);
|
2024-02-08 13:32:44 -06:00
|
|
|
|
|
|
|
|
var baseTypeNameResult = AlphanumericText.Token()(input);
|
|
|
|
|
input = baseTypeNameResult.Remainder;
|
|
|
|
|
if (!baseTypeNameResult.WasSuccessful)
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, "Invalid export directive", ["Expected base type name"]);
|
2024-02-08 13:32:44 -06:00
|
|
|
|
|
|
|
|
var labelPrefix = labelPrefixResult.Value;
|
|
|
|
|
var baseTypeName = baseTypeNameResult.Value;
|
|
|
|
|
directive = new ExportDirective(labelPrefix, listenerCount, baseTypeName)
|
2024-02-08 10:50:57 -06:00
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = column,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
default:
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Failure<IDirective>(input, $"Unknown import type '{directiveIdResult.Value}'", ["Expected 'export', 'import', or 'section'"]);
|
2024-02-05 09:23:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.Success(directive, input);
|
|
|
|
|
}
|
|
|
|
|
}
|