From 3e9e1fef0f133104a81d0104ca46b5cb4b14a30d Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Mon, 27 Jan 2025 23:28:44 -0600 Subject: [PATCH] Support generic types --- libs/UIX.Asm/Lexer.cs | 27 +++++++++++++++++++++++++++ libs/UIX.Test/Assembly.cs | 7 +++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/libs/UIX.Asm/Lexer.cs b/libs/UIX.Asm/Lexer.cs index 07a236f..3854283 100644 --- a/libs/UIX.Asm/Lexer.cs +++ b/libs/UIX.Asm/Lexer.cs @@ -72,6 +72,33 @@ public static partial class Lexer typeName = typePrefixResult.Value; } + // 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(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(input, "Invalid generic type", ["Expected type parameters"]); + + var genericTypeEndPosition = input.Position; + typeName += input.Source[genericTypeStartPosition..genericTypeEndPosition]; + } + } + QualifiedTypeName qualifiedName = new(typePrefix, typeName) { Line = line, diff --git a/libs/UIX.Test/Assembly.cs b/libs/UIX.Test/Assembly.cs index b5d758e..cc3bc28 100644 --- a/libs/UIX.Test/Assembly.cs +++ b/libs/UIX.Test/Assembly.cs @@ -27,17 +27,20 @@ public class Assembly .export Default 0 UI .export Alt 0 UI +.import-ns assembly://UIX/Microsoft.Iris as iris .import-type UI .import-type Dictionary -.import-ns assembly://UIX/Microsoft.Iris as iris .import-type iris:Command .import-type String .import-type ViewItem .import-type Text .import-type Color .import-type Font +.import-type generic:List`1[System.String] .import-mbrs UI{Locals, Content} .import-mbrs Text{Color, Content, Font} +.import-mbrs SingletonModelItem`1[ZuneUI.TransportControls]{Instance} +.import-mthd List`1[System.Int32].Add(Int32) .constant const0 = Color.str(255, 255, 0, 0) .constant const1 = String.str(Howdy from Microsoft.Iris!) @@ -79,7 +82,7 @@ Alt_locl: _output.WriteLine(ast.ToString()); Assert.NotNull(ast); - Assert.Equal(22, ast.Directives.Count()); + Assert.Equal(25, ast.Directives.Count()); Assert.Equal(24, ast.Code.Count()); }