Replies: 1 comment 5 replies
-
Hello, thank you for giving our project a shot! For this you could either do a whole-program search through the tree's root for a table has field named For example: using Loretta.CodeAnalysis;
using Loretta.CodeAnalysis.Lua;
using Loretta.CodeAnalysis.Lua.Syntax;
var code = """
RandomName = {
RequestId = 0x7A6,
ResponseId = 0x7A7,
Raw = {
["10 03"] = "50 03 00 28 00 C8",
["3E 00"] = "7E 00",
["22 22 35"] = "62 22 35 47 11",
["2E 22 35 47 11"] = "6E 22 35",
-- Additional entries here
}
}
""";
var tree = LuaSyntaxTree.ParseText(code, new LuaParseOptions(LuaSyntaxOptions.Lua51));
var root = tree.GetCompilationUnitRoot();
var tables = root.DescendantNodes().OfType<TableConstructorExpressionSyntax>();
var toAnalyze = new List<TableConstructorExpressionSyntax>();
foreach (var table in tables)
{
// This checks that the table field is in the form Raw = { ... }
if (table.Fields.Any(x => x is IdentifierKeyedTableFieldSyntax { Identifier.Value: "Raw", Value: TableConstructorExpressionSyntax }))
{
toAnalyze.Add(table);
}
// If you want to handle ["Raw"] = { ... } case as well
else if (table.Fields.Any(x => x is ExpressionKeyedTableFieldSyntax
{
Key: LiteralExpressionSyntax { RawKind: (int)SyntaxKind.StringLiteralExpression, Token.Value: "Raw" },
Value: TableConstructorExpressionSyntax
}))
{
toAnalyze.Add(table);
}
} and then, after you have listed the tables you want to modify, you can then use the But since you also want to add a local table to hold some state before the original one, you'll actually need to work on the To this end, you can either use a Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hello, in my hobby project 'EcuDiagSim,' I aim to modify some Lua files. Initially, I considered using Regex for this task, but then I discovered this project, which seems much more suited for my needs. However, I've realized I lack the knowledge on how to get started, particularly in finding and modifying a specific table named 'Raw' within these files. Could you provide idea on how to use Loretta to locate and manipulate the 'Raw' table in a Lua file? In my Lua scripts, the 'Raw' table is always a first-level sub-element of a top-level table. I hope I've explained that correctly. Here's an example of how the Lua code should look before and after the modifications.
before rework:
Aftter rework. This is what the result should look like.
Please don't get me wrong; I'm not looking for anyone to do my work for me, but I'd really appreciate some tips. I've experimented with but I'm not sure if I'm on the right track. Any advice or pointers would be greatly appreciated.
Thank you in advance for your assistance.
Beta Was this translation helpful? Give feedback.
All reactions