Skip to content

Commit

Permalink
enhance method SearchConstant in class LuaXClass
Browse files Browse the repository at this point in the history
  • Loading branch information
teo1962 committed May 15, 2023
1 parent 652cfc1 commit 4766faa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
27 changes: 21 additions & 6 deletions Luax.Parser.Test/TreeBuilderTest_ClassTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
8 changes: 4 additions & 4 deletions Luax.Parser/Ast/Builder/LuaXAstTreeCreator_Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions Luax.Parser/Ast/LuaXClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,33 +217,37 @@ 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;
}
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;
Expand Down

0 comments on commit 4766faa

Please sign in to comment.