diff --git a/Luax.Parser.Test/TreeBuilderTest_ClassTree.cs b/Luax.Parser.Test/TreeBuilderTest_ClassTree.cs index d1e1f9c..6672883 100644 --- a/Luax.Parser.Test/TreeBuilderTest_ClassTree.cs +++ b/Luax.Parser.Test/TreeBuilderTest_ClassTree.cs @@ -406,23 +406,38 @@ public void SearchProperty() [Fact] public void SearchConstant() { + LuaXClass owner; var metadata = new LuaXClassCollection(); - var a = new LuaXClass("a"); + var a = new LuaXClass("a", null, new LuaXElementLocation("test", 0, 0)); a.Constants.Add(new LuaXConstantVariable() { Name = "pa" }); metadata.Add(a); - var b = new LuaXClass("b", "a", null); + var b = new LuaXClass("b", "a", new LuaXElementLocation("test", 0, 0)); metadata.Add(b); b.Constants.Add(new LuaXConstantVariable() { Name = "pb" }); + var ac = new LuaXClass("a.c", null, new LuaXElementLocation("test", 0, 0)); + metadata.Add(ac); + + var app = new LuaXApplication(); + app.Classes.Add(a); + app.Classes.Add(b); + app.Classes.Add(ac); + app.Pass2(); metadata.Search("a", out _); //force index to be build - b.SearchConstant("pb", out var p).Should().Be(true); + b.SearchConstant("pb", out var p, out _).Should().Be(true); p.Name.Should().Be("pb"); - b.SearchConstant("pa", out p).Should().Be(true); + + b.SearchConstant("pa", out p, out owner).Should().Be(true); + p.Name.Should().Be("pa"); + owner.Name.Should().Be("a"); + + ac.SearchConstant("pa", out p, out owner).Should().Be(true); p.Name.Should().Be("pa"); + owner.Name.Should().Be("a"); - a.SearchConstant("pb", out _).Should().Be(false); - a.SearchConstant("pa", out p).Should().Be(true); + a.SearchConstant("pb", out _, out _).Should().Be(false); + a.SearchConstant("pa", out p, out _).Should().Be(true); p.Name.Should().Be("pa"); } diff --git a/Luax.Parser/Ast/Builder/LuaXAstTreeCreator_Expression.cs b/Luax.Parser/Ast/Builder/LuaXAstTreeCreator_Expression.cs index 05b6088..5be7ff2 100644 --- a/Luax.Parser/Ast/Builder/LuaXAstTreeCreator_Expression.cs +++ b/Luax.Parser/Ast/Builder/LuaXAstTreeCreator_Expression.cs @@ -439,8 +439,8 @@ private LuaXExpression ProcessStaticProperty(IAstNode astNode, LuaXClass current return new LuaXStaticPropertyExpression(ownerClassName, name, property.LuaType, location); } - else if (leftSideClass.SearchConstant(name, out var constant)) - return new LuaXConstantExpression(constant.Value, location, new LuaXConstantExpression.LuaXConstantSource(leftSideClass, null, constant)); + else if (leftSideClass.SearchConstant(name, out var constant, out var owner)) + return new LuaXConstantExpression(constant.Value, location, new LuaXConstantExpression.LuaXConstantSource(owner, null, constant)); throw new LuaXAstGeneratorException(Name, astNode, $"Class {classNameExpression.Name} does not contain property {name}"); } @@ -514,8 +514,8 @@ private LuaXExpression ProcessVariable(IAstNode astNode, LuaXClass currentClass, if (currentClass.SearchProperty(name, out var p1, out string ownerClassName)) return ProcessVariableAsProperty(astNode, ownerClassName, currentMethod, name, p1, location); - if (currentClass.SearchConstant(name, out var c2)) - return new LuaXConstantExpression(c2.Value, location, new LuaXConstantExpression.LuaXConstantSource(currentClass, null, c2)); + if (currentClass.SearchConstant(name, out var c2, out var owner)) + return new LuaXConstantExpression(c2.Value, location, new LuaXConstantExpression.LuaXConstantSource(owner, null, c2)); if (SearchClassByName(name, currentClass, out var realClass)) return new LuaXClassNameExpression(realClass.Name, location); diff --git a/Luax.Parser/Ast/LuaXClass.cs b/Luax.Parser/Ast/LuaXClass.cs index a2d9ff7..5d0c6f1 100644 --- a/Luax.Parser/Ast/LuaXClass.cs +++ b/Luax.Parser/Ast/LuaXClass.cs @@ -217,20 +217,21 @@ public bool SearchProperty(string propertyName, out LuaXProperty property, out s return false; } - public bool SearchConstant(string propertyName, out LuaXConstantVariable constant) + public bool SearchConstant(string propertyName, out LuaXConstantVariable constant, out LuaXClass owner) { LuaXConstantVariableCollection constants = this.Constants; string className = this.Name; + LuaXClass @class = this; while (!string.IsNullOrEmpty(className)) { var constantIndex = constants.Find(propertyName); if (constantIndex < 0) { - if (ParentClass != null && ParentClass.SearchConstant(propertyName, out constant)) + if (ParentClass != null && ParentClass.SearchConstant(propertyName, out constant, out owner)) { return true; } - if (HasOwnerClass(in className, out className, out var @class)) + if (HasOwnerClass(in className, out className, out @class)) { constants = @class.Constants; continue; @@ -238,12 +239,15 @@ public bool SearchConstant(string propertyName, out LuaXConstantVariable constan break; } constant = constants[constantIndex]; + owner = @class; return true; } constant = null; + owner = null; return false; } + public bool SearchMethod(string propertyName, out LuaXMethod method, bool doNotLookInOwnerClasses = false) { LuaXMethodCollection methods = this.Methods;