How to parse any constant Lua table preferably without loading it in the Lua VM?
c#, dynamic, lua, parsing
Solution
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) and `GetTable` to read the table into a `LuaTable` object that you can use the result from c#. The `LuaTable` exposes an `IDictionaryEnumerator` through `GetEnumerator`.
EDIT:
if you had this table constructor:
local t = { os.time() }
print(t[1]);
the function in the constructor would need to be executed to initialize the data.
for constant literals, you can have string constants like so:
local a = [==[
hello
there""\"]==]
with different levels of equal signs
a numeric literal can have the form:
0X1.921FB54442D18P+1
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.
Problem
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?