I have a bunch of data in the form of a Lua table and I would like to parse that data into a usable structure in C#.
The problem with Lua tables is that there are optional fields, tables are very dynamic and are not just a dictionary with one type for the key and one type for the value. It’s possible to have one Lua table with both string and integer keys, and values of type integer, string and even table.
Sadly, the data that I’m parsing makes use of the dynamic nature of the language and isn’t really structured in any straight-forward way. This requires a dynamic representation of the data, using for example Dictionary<object, dynamic>.
The format of the data is e.g. (from http://ideone.com/9nzXvt)
local mainNode =
{
[0] =
{
Name = "First element",
Comments = "I have comments!",
Data = { {123, 456}; foo = { "bar" } }
},
[1337] =
{
Name = "Another element",
Data = { {0}; foo = nil }
}
}
Are there any libraries out there to do this? Is there any way to accomplish this without parsing the data character by character?
You can use the luainterface library
There’s some sample code here.
You’ll want to use a combination of
DoFile(to load the file) andGetTableto read the table into aLuaTableobject that you can use the result from c#. TheLuaTableexposes anIDictionaryEnumeratorthroughGetEnumerator.EDIT:
if you had this table constructor:
the function in the constructor would need to be executed to initialize the data.
for constant literals, you can have string constants like so:
with different levels of equal signs
a numeric literal can have the form:
with P as a binary exponent.
faithfully reproducing the lua syntax for constant literals without using the lightweight lua VM would require re-implementing a good chunk of the lua language spec. not much benefit it re-inventing the wheel.