From 66442b088ab8af56dde1664353dc4a00f8a0c828 Mon Sep 17 00:00:00 2001 From: Acedia / Melancholy <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Mon, 7 Nov 2022 06:46:08 +0630 Subject: [PATCH 1/3] Add contained scopes --- .../SyntaxFacts.g.cs | 228 +++++++++--------- ...dVariableManager.ScopeAndVariableWalker.cs | 2 + .../Test/Portable/Scoping/FindScopeTests.cs | 10 + 3 files changed, 126 insertions(+), 114 deletions(-) diff --git a/src/Compilers/Lua/Portable/Generated/Loretta.Generators.SyntaxFactsGenerator/Loretta.Generators.SyntaxFactsGenerator.SyntaxFactsGenerator/SyntaxFacts.g.cs b/src/Compilers/Lua/Portable/Generated/Loretta.Generators.SyntaxFactsGenerator/Loretta.Generators.SyntaxFactsGenerator.SyntaxFactsGenerator/SyntaxFacts.g.cs index 31301004..44040035 100644 --- a/src/Compilers/Lua/Portable/Generated/Loretta.Generators.SyntaxFactsGenerator/Loretta.Generators.SyntaxFactsGenerator.SyntaxFactsGenerator/SyntaxFacts.g.cs +++ b/src/Compilers/Lua/Portable/Generated/Loretta.Generators.SyntaxFactsGenerator/Loretta.Generators.SyntaxFactsGenerator.SyntaxFactsGenerator/SyntaxFacts.g.cs @@ -167,29 +167,29 @@ public static Option GetBinaryExpression(SyntaxKind kind) => /// public static SyntaxKind GetKeywordKind(String text) { - string? __candidate__1 = null; - int __destination__1 = -1; - var __skipCheck__1 = false; + string? __candidate__0 = null; + int __destination__0 = -1; + var __skipCheck__0 = false; switch (text.Length) { case 2: switch (text[1]) { case 'o': - __candidate__1 = "do"; - __destination__1 = 3; + __candidate__0 = "do"; + __destination__0 = 3; break; case 'f': - __candidate__1 = "if"; - __destination__1 = 12; + __candidate__0 = "if"; + __destination__0 = 12; break; case 'n': - __candidate__1 = "in"; - __destination__1 = 13; + __candidate__0 = "in"; + __destination__0 = 13; break; case 'r': - __candidate__1 = "or"; - __destination__1 = 17; + __candidate__0 = "or"; + __destination__0 = 17; break; } break; @@ -197,24 +197,24 @@ public static SyntaxKind GetKeywordKind(String text) switch (System.Runtime.InteropServices.MemoryMarshal.Read(System.Runtime.InteropServices.MemoryMarshal.Cast(text.AsSpan(0, 2)))) { case 0x006e0061: - __candidate__1 = "and"; - __destination__1 = 0; + __candidate__0 = "and"; + __destination__0 = 0; break; case 0x006e0065: - __candidate__1 = "end"; - __destination__1 = 6; + __candidate__0 = "end"; + __destination__0 = 6; break; case 0x006f0066: - __candidate__1 = "for"; - __destination__1 = 9; + __candidate__0 = "for"; + __destination__0 = 9; break; case 0x0069006e: - __candidate__1 = "nil"; - __destination__1 = 15; + __candidate__0 = "nil"; + __destination__0 = 15; break; case 0x006f006e: - __candidate__1 = "not"; - __destination__1 = 16; + __candidate__0 = "not"; + __destination__0 = 16; break; } break; @@ -222,24 +222,24 @@ public static SyntaxKind GetKeywordKind(String text) switch (text[1]) { case 'l': - __candidate__1 = "else"; - __destination__1 = 5; + __candidate__0 = "else"; + __destination__0 = 5; break; case 'o': - __candidate__1 = "goto"; - __destination__1 = 11; + __candidate__0 = "goto"; + __destination__0 = 11; break; case 'h': - __candidate__1 = "then"; - __destination__1 = 20; + __candidate__0 = "then"; + __destination__0 = 20; break; case 'r': - __candidate__1 = "true"; - __destination__1 = 21; + __candidate__0 = "true"; + __destination__0 = 21; break; case 'y': - __candidate__1 = "type"; - __destination__1 = 22; + __candidate__0 = "type"; + __destination__0 = 22; break; } break; @@ -247,24 +247,24 @@ public static SyntaxKind GetKeywordKind(String text) switch (text[0]) { case 'b': - __candidate__1 = "break"; - __destination__1 = 1; + __candidate__0 = "break"; + __destination__0 = 1; break; case 'f': - __candidate__1 = "false"; - __destination__1 = 8; + __candidate__0 = "false"; + __destination__0 = 8; break; case 'l': - __candidate__1 = "local"; - __destination__1 = 14; + __candidate__0 = "local"; + __destination__0 = 14; break; case 'u': - __candidate__1 = "until"; - __destination__1 = 24; + __candidate__0 = "until"; + __destination__0 = 24; break; case 'w': - __candidate__1 = "while"; - __destination__1 = 25; + __candidate__0 = "while"; + __destination__0 = 25; break; } break; @@ -272,24 +272,24 @@ public static SyntaxKind GetKeywordKind(String text) switch (System.Runtime.InteropServices.MemoryMarshal.Read(System.Runtime.InteropServices.MemoryMarshal.Cast(text.AsSpan(1, 2)))) { case 0x0073006c: - __candidate__1 = "elseif"; - __destination__1 = 4; + __candidate__0 = "elseif"; + __destination__0 = 4; break; case 0x00700078: - __candidate__1 = "export"; - __destination__1 = 7; + __candidate__0 = "export"; + __destination__0 = 7; break; case 0x00700065: - __candidate__1 = "repeat"; - __destination__1 = 18; + __candidate__0 = "repeat"; + __destination__0 = 18; break; case 0x00740065: - __candidate__1 = "return"; - __destination__1 = 19; + __candidate__0 = "return"; + __destination__0 = 19; break; case 0x00700079: - __candidate__1 = "typeof"; - __destination__1 = 23; + __candidate__0 = "typeof"; + __destination__0 = 23; break; } break; @@ -297,19 +297,19 @@ public static SyntaxKind GetKeywordKind(String text) switch (text[0]) { case 'c': - __candidate__1 = "continue"; - __destination__1 = 2; + __candidate__0 = "continue"; + __destination__0 = 2; break; case 'f': - __candidate__1 = "function"; - __destination__1 = 10; + __candidate__0 = "function"; + __destination__0 = 10; break; } break; } - if (__destination__1 != -1 && (__skipCheck__1 || string.Equals(text, __candidate__1, StringComparison.Ordinal))) + if (__destination__0 != -1 && (__skipCheck__0 || string.Equals(text, __candidate__0, StringComparison.Ordinal))) { - switch (__destination__1) + switch (__destination__0) { case 0: { @@ -430,29 +430,29 @@ public static SyntaxKind GetKeywordKind(String text) /// public static SyntaxKind GetKeywordKind(ReadOnlySpan span) { - string? __candidate__1 = null; - int __destination__1 = -1; - var __skipCheck__1 = false; + string? __candidate__0 = null; + int __destination__0 = -1; + var __skipCheck__0 = false; switch (span.Length) { case 2: switch (span[1]) { case 'o': - __candidate__1 = "do"; - __destination__1 = 3; + __candidate__0 = "do"; + __destination__0 = 3; break; case 'f': - __candidate__1 = "if"; - __destination__1 = 12; + __candidate__0 = "if"; + __destination__0 = 12; break; case 'n': - __candidate__1 = "in"; - __destination__1 = 13; + __candidate__0 = "in"; + __destination__0 = 13; break; case 'r': - __candidate__1 = "or"; - __destination__1 = 17; + __candidate__0 = "or"; + __destination__0 = 17; break; } break; @@ -460,24 +460,24 @@ public static SyntaxKind GetKeywordKind(ReadOnlySpan span) switch (System.Runtime.InteropServices.MemoryMarshal.Read(System.Runtime.InteropServices.MemoryMarshal.Cast(span.Slice(0, 2)))) { case 0x006e0061: - __candidate__1 = "and"; - __destination__1 = 0; + __candidate__0 = "and"; + __destination__0 = 0; break; case 0x006e0065: - __candidate__1 = "end"; - __destination__1 = 6; + __candidate__0 = "end"; + __destination__0 = 6; break; case 0x006f0066: - __candidate__1 = "for"; - __destination__1 = 9; + __candidate__0 = "for"; + __destination__0 = 9; break; case 0x0069006e: - __candidate__1 = "nil"; - __destination__1 = 15; + __candidate__0 = "nil"; + __destination__0 = 15; break; case 0x006f006e: - __candidate__1 = "not"; - __destination__1 = 16; + __candidate__0 = "not"; + __destination__0 = 16; break; } break; @@ -485,24 +485,24 @@ public static SyntaxKind GetKeywordKind(ReadOnlySpan span) switch (span[1]) { case 'l': - __candidate__1 = "else"; - __destination__1 = 5; + __candidate__0 = "else"; + __destination__0 = 5; break; case 'o': - __candidate__1 = "goto"; - __destination__1 = 11; + __candidate__0 = "goto"; + __destination__0 = 11; break; case 'h': - __candidate__1 = "then"; - __destination__1 = 20; + __candidate__0 = "then"; + __destination__0 = 20; break; case 'r': - __candidate__1 = "true"; - __destination__1 = 21; + __candidate__0 = "true"; + __destination__0 = 21; break; case 'y': - __candidate__1 = "type"; - __destination__1 = 22; + __candidate__0 = "type"; + __destination__0 = 22; break; } break; @@ -510,24 +510,24 @@ public static SyntaxKind GetKeywordKind(ReadOnlySpan span) switch (span[0]) { case 'b': - __candidate__1 = "break"; - __destination__1 = 1; + __candidate__0 = "break"; + __destination__0 = 1; break; case 'f': - __candidate__1 = "false"; - __destination__1 = 8; + __candidate__0 = "false"; + __destination__0 = 8; break; case 'l': - __candidate__1 = "local"; - __destination__1 = 14; + __candidate__0 = "local"; + __destination__0 = 14; break; case 'u': - __candidate__1 = "until"; - __destination__1 = 24; + __candidate__0 = "until"; + __destination__0 = 24; break; case 'w': - __candidate__1 = "while"; - __destination__1 = 25; + __candidate__0 = "while"; + __destination__0 = 25; break; } break; @@ -535,24 +535,24 @@ public static SyntaxKind GetKeywordKind(ReadOnlySpan span) switch (System.Runtime.InteropServices.MemoryMarshal.Read(System.Runtime.InteropServices.MemoryMarshal.Cast(span.Slice(1, 2)))) { case 0x0073006c: - __candidate__1 = "elseif"; - __destination__1 = 4; + __candidate__0 = "elseif"; + __destination__0 = 4; break; case 0x00700078: - __candidate__1 = "export"; - __destination__1 = 7; + __candidate__0 = "export"; + __destination__0 = 7; break; case 0x00700065: - __candidate__1 = "repeat"; - __destination__1 = 18; + __candidate__0 = "repeat"; + __destination__0 = 18; break; case 0x00740065: - __candidate__1 = "return"; - __destination__1 = 19; + __candidate__0 = "return"; + __destination__0 = 19; break; case 0x00700079: - __candidate__1 = "typeof"; - __destination__1 = 23; + __candidate__0 = "typeof"; + __destination__0 = 23; break; } break; @@ -560,19 +560,19 @@ public static SyntaxKind GetKeywordKind(ReadOnlySpan span) switch (span[0]) { case 'c': - __candidate__1 = "continue"; - __destination__1 = 2; + __candidate__0 = "continue"; + __destination__0 = 2; break; case 'f': - __candidate__1 = "function"; - __destination__1 = 10; + __candidate__0 = "function"; + __destination__0 = 10; break; } break; } - if (__destination__1 != -1 && (__skipCheck__1 || System.MemoryExtensions.Equals(span, __candidate__1.AsSpan(), StringComparison.Ordinal))) + if (__destination__0 != -1 && (__skipCheck__0 || System.MemoryExtensions.Equals(span, __candidate__0.AsSpan(), StringComparison.Ordinal))) { - switch (__destination__1) + switch (__destination__0) { case 0: { diff --git a/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs b/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs index ea662747..465b5c0d 100644 --- a/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs +++ b/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs @@ -39,6 +39,7 @@ private IFunctionScopeInternal CreateFunctionScope(SyntaxNode node) { var scope = new FunctionScope(node, Scope); _scopes.Add(node, scope); + _scopeStack.Peek().AddChildScope(scope); _scopeStack.Push(scope); return scope; } @@ -98,6 +99,7 @@ public override void VisitCompilationUnit(CompilationUnitSyntax node) public override void VisitAnonymousFunctionExpression(AnonymousFunctionExpressionSyntax node) { var scope = CreateFunctionScope(node); + try { foreach (var parameter in node.Parameters.Parameters) diff --git a/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs b/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs index 09c62308..3087a29f 100644 --- a/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs +++ b/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs @@ -32,5 +32,15 @@ public void FindScope_OnRootElement_ReturnsRootScope() Assert.Equal(compilationUnitScope, printExpressionScope); } + + [Fact] + [Trait("Category", "Script/FindScope")] + public void FindScope_AnonymousFunctionIsParsed() + { + var (tree, script) = ParseScript("local function a() end"); + var fileScope = script.GetScope(tree.GetRoot()); + + Assert.Equal(1, fileScope?.ContainedScopes.Count()); + } } } From 6d4b43c4b55802c64ce4b08e73665da87e5563f3 Mon Sep 17 00:00:00 2001 From: Acedia / Melancholy <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Mon, 7 Nov 2022 07:17:55 +0630 Subject: [PATCH 2/3] Add remaining scopes and issue test case --- ...peAndVariableManager.ScopeAndVariableWalker.cs | 2 ++ .../Lua/Test/Portable/Scoping/FindScopeTests.cs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs b/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs index 465b5c0d..ff7e31ea 100644 --- a/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs +++ b/src/Compilers/Lua/Portable/Script/ScopeAndVariableManager.ScopeAndVariableWalker.cs @@ -31,6 +31,7 @@ private IFileScopeInternal CreateFileScope(SyntaxNode node) { var scope = new FileScope(node, Scope); _scopes.Add(node, scope); + _scopeStack.Peek().AddChildScope(scope); _scopeStack.Push(scope); return scope; } @@ -48,6 +49,7 @@ private IScopeInternal CreateBlockScope(SyntaxNode node) { var scope = new Scope(ScopeKind.Block, node, Scope); _scopes.Add(node, scope); + _scopeStack.Peek().AddChildScope(scope); _scopeStack.Push(scope); return scope; } diff --git a/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs b/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs index 3087a29f..109f1dd4 100644 --- a/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs +++ b/src/Compilers/Lua/Test/Portable/Scoping/FindScopeTests.cs @@ -1,4 +1,5 @@ using Loretta.CodeAnalysis.Lua.Syntax; +using Loretta.Test.Utilities; using Xunit; namespace Loretta.CodeAnalysis.Lua.UnitTests.Scoping @@ -35,11 +36,23 @@ public void FindScope_OnRootElement_ReturnsRootScope() [Fact] [Trait("Category", "Script/FindScope")] - public void FindScope_AnonymousFunctionIsParsed() + [WorkItem(106, "https://github.com/LorettaDevs/Loretta/issues/106")] + public void FindScope_LocalFunctionIsParsed() { var (tree, script) = ParseScript("local function a() end"); var fileScope = script.GetScope(tree.GetRoot()); + Assert.Equal(1, fileScope?.ContainedScopes.Count()); + } + + [Fact] + [Trait("Category", "Script/FindScope")] + [WorkItem(106, "https://github.com/LorettaDevs/Loretta/issues/106")] + public void FindScope_AnonymousFunctionIsParsed() + { + var (tree, script) = ParseScript("(function(Variable) end)()"); + var fileScope = script.GetScope(tree.GetRoot()); + Assert.Equal(1, fileScope?.ContainedScopes.Count()); } } From 5bceb003348a1198cc40464a01998e8556e82396 Mon Sep 17 00:00:00 2001 From: June <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Mon, 7 Nov 2022 07:29:32 +0630 Subject: [PATCH 3/3] Update CHANGES.md --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 22c3f7d6..7ecc218e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Fixed +- Fixed `ContainedScopes` not being populated. + ## v0.2.10 ### Added - Added the following new `SyntaxFactory` overloads to partially restore compatibility with pre-typed-lua era: