2024-01-31 09:21:30 -06:00
|
|
|
using Microsoft.Iris.Asm.Models;
|
|
|
|
|
using Sprache;
|
2024-01-29 21:42:06 -06:00
|
|
|
using System.Linq;
|
|
|
|
|
|
2024-01-29 21:57:00 -06:00
|
|
|
namespace Microsoft.Iris.Asm;
|
2024-01-29 21:42:06 -06:00
|
|
|
|
2024-02-04 21:44:53 -06:00
|
|
|
public static partial class Lexer
|
2024-01-29 21:42:06 -06:00
|
|
|
{
|
|
|
|
|
public static readonly Parser<string> AlphanumericText = Parse.LetterOrDigit.AtLeastOnce().Text();
|
|
|
|
|
|
2024-02-08 10:50:57 -06:00
|
|
|
public static readonly Parser<string> WordText = Parse.Letter.AtLeastOnce().Text();
|
|
|
|
|
|
2024-02-08 13:32:44 -06:00
|
|
|
public static readonly Parser<string> WholeNumber = Parse.Digit.AtLeastOnce().Text();
|
|
|
|
|
|
2024-02-10 21:37:12 -06:00
|
|
|
public static readonly Parser<string> Identifier = Parse.LetterOrDigit.Or(Parse.Chars('_', '-')).AtLeastOnce().Text();
|
|
|
|
|
|
2024-01-29 21:42:06 -06:00
|
|
|
public static readonly Parser<string> Uri = Parse.LetterOrDigit.Or(Parse.Chars(":/!._-")).AtLeastOnce().Text();
|
|
|
|
|
|
|
|
|
|
public static readonly Parser<string> StatementEnd = Parse.Char(';').Return(";").Or(Parse.LineTerminator);
|
|
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
public static readonly Parser<QualifiedTypeName> QualifiedTypeName = ParseQualifiedTypeName;
|
|
|
|
|
|
2024-02-10 23:03:57 -06:00
|
|
|
public static readonly Parser<IImportDirective> Import = ParseImport;
|
2024-02-04 22:09:21 -06:00
|
|
|
|
2024-02-05 09:23:46 -06:00
|
|
|
public static readonly Parser<IDirective> Directive = ParseDirective;
|
|
|
|
|
|
2024-02-04 22:09:21 -06:00
|
|
|
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
|
|
|
|
|
|
2024-01-29 21:42:06 -06:00
|
|
|
public static readonly Parser<Program> Program =
|
|
|
|
|
from body in BodyItem.Many()
|
2024-02-10 23:03:57 -06:00
|
|
|
select new Program(body);
|
2024-01-29 21:42:06 -06:00
|
|
|
|
2024-01-30 22:44:48 -06:00
|
|
|
private static IInput ConsumeWhitespace(IInput input) => Parse.WhiteSpace.Many()(input).Remainder;
|
2024-02-11 20:38:19 -06:00
|
|
|
|
|
|
|
|
private static IResult<QualifiedTypeName> ParseQualifiedTypeName(IInput input)
|
|
|
|
|
{
|
2024-07-07 15:35:44 -05:00
|
|
|
var line = input.Line;
|
|
|
|
|
var col = input.Column;
|
|
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
var typePrefixResult = Identifier(input);
|
|
|
|
|
input = typePrefixResult.Remainder;
|
|
|
|
|
if (!typePrefixResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<QualifiedTypeName>(input, "Invalid type name", ["Expected a valid namespace prefix"]);
|
|
|
|
|
|
|
|
|
|
var typeNamespaceDelimitterResult = Parse.Char(':')(input);
|
|
|
|
|
input = typeNamespaceDelimitterResult.Remainder;
|
|
|
|
|
|
|
|
|
|
string typeName, typePrefix;
|
|
|
|
|
if (typeNamespaceDelimitterResult.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
var typeNameResult = Identifier(input);
|
|
|
|
|
input = typeNameResult.Remainder;
|
|
|
|
|
if (!typeNameResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<QualifiedTypeName>(input, "Invalid type name", ["Expected a valid type name"]);
|
|
|
|
|
|
|
|
|
|
typePrefix = typePrefixResult.Value;
|
|
|
|
|
typeName = typeNameResult.Value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
typePrefix = null;
|
|
|
|
|
typeName = typePrefixResult.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 15:35:44 -05:00
|
|
|
QualifiedTypeName qualifiedName = new(typePrefix, typeName)
|
|
|
|
|
{
|
|
|
|
|
Line = line,
|
|
|
|
|
Column = col,
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-11 20:38:19 -06:00
|
|
|
return Result.Success(qualifiedName, input);
|
|
|
|
|
}
|
2024-01-29 21:42:06 -06:00
|
|
|
}
|