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();
|
|
|
|
|
|
2025-01-27 18:23:07 -06:00
|
|
|
// https://github.com/apexsharp/apexparser/blob/4eb5983c657b0e6c49ed47bc42a0346e80f9e26d/ApexSharp.ApexParser/Parser/ApexGrammar.cs#L360C9-L367C64
|
|
|
|
|
public static readonly Parser<string> StringLiteral =
|
|
|
|
|
from leading in Parse.WhiteSpace.Many()
|
|
|
|
|
from openQuote in Parse.Char('"')
|
|
|
|
|
from fragments in Parse.Char('\\').Then(_ => Parse.AnyChar.Select(c => $"\\{c}"))
|
|
|
|
|
.Or(Parse.CharExcept("\\\"").Many().Text()).Many()
|
|
|
|
|
from closeQuote in Parse.Char('"')
|
|
|
|
|
from trailing in Parse.WhiteSpace.Many()
|
|
|
|
|
select $"\"{string.Join(string.Empty, fragments)}\"";
|
|
|
|
|
|
2024-01-29 21:42:06 -06:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 23:28:44 -06:00
|
|
|
// Capture generic types
|
|
|
|
|
var genericTypeStartPosition = input.Position;
|
|
|
|
|
var genericTypeMarkerResult = Parse.Char('`')(input);
|
|
|
|
|
input = genericTypeMarkerResult.Remainder;
|
|
|
|
|
if (genericTypeMarkerResult.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
var genericTypeParameterCountResult = Parse.Number(input);
|
|
|
|
|
input = genericTypeParameterCountResult.Remainder;
|
|
|
|
|
if (!genericTypeParameterCountResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<QualifiedTypeName>(input, "Invalid generic type", ["Expected a type parameter count"]);
|
|
|
|
|
|
|
|
|
|
var genericTypeParametersOpenBrace = Parse.Char('[')(input);
|
|
|
|
|
input = genericTypeParametersOpenBrace.Remainder;
|
|
|
|
|
if (genericTypeParametersOpenBrace.WasSuccessful)
|
|
|
|
|
{
|
|
|
|
|
var genericTypeParametersResult = Parse.AnyChar.Except(Parse.Chars('.', ']')).AtLeastOnce().Text()
|
|
|
|
|
.DelimitedBy(Parse.Char('.'))
|
|
|
|
|
.Until(Parse.Char(']'))(input);
|
|
|
|
|
input = genericTypeParametersResult.Remainder;
|
|
|
|
|
if (!genericTypeParametersResult.WasSuccessful)
|
|
|
|
|
return Result.Failure<QualifiedTypeName>(input, "Invalid generic type", ["Expected type parameters"]);
|
|
|
|
|
|
|
|
|
|
var genericTypeEndPosition = input.Position;
|
|
|
|
|
typeName += input.Source[genericTypeStartPosition..genericTypeEndPosition];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2025-01-27 18:23:07 -06:00
|
|
|
|
|
|
|
|
private static Parser<string> ExpressionInBraces(Parser<string> parser, char open = '(', char close = ')') =>
|
|
|
|
|
from openBrace in Parse.Char(open).Token()
|
|
|
|
|
from expression in parser.Optional()
|
|
|
|
|
from closeBrace in Parse.Char(close).Token()
|
|
|
|
|
select expression.GetOrElse(string.Empty).Trim();
|
2024-01-29 21:42:06 -06:00
|
|
|
}
|