Is there a simple way to get a table from a script source?

Hello!

I’m making a plugin right now, and it generates scripts like this:

    BTModule.Root:new(
    { --start of table
    	tree = BTModule.BranchNode:new({
    		nodes = {
    			BTModule.Priority:new({
    				nodes = {
    					BTModule.Random:new({
    						nodes = {
    							nil
    						}
    					})
    				}
    			}),
    			BTModule.ActivePriority:new({
    				nodes = {
    					nil
    				}
    			})
    		}
    	})
    } --end of table
    )

What I want to do is get the source of a script and get a table from it (More specifically the start/end of the table up above). I could make a parser for it, but I want to prevent it if I can. I’m just wondering if there’s an already existing module someone’s created, or if there’s an easy way to do it. I haven’t really found anything about it.

Thanks!

Side note: This is my first post and I have no idea how to format code properly.

1 Like

Is it acceptable to just return the table from a module script?

return {
    -- stuff
};
5 Likes

Could you tell us more about the scripts you want to get the tables from? Do you just need the start and end positions of the table? If so then a simple search for “{” and “}” unless the script has strings or comments with { and } in them, then you’ll need at least a finite automata. If the script has multiple tables mixed with nested tables, then no finite state machine can solve this problem due to memory that must be stored to handle indeterminate nesting depth, it needs to be able to have “infinite” states. The simplest computational model able to solve this problem would be a pushdown automation. Parsers are, when stripped down to their core, pushdown automata. No semantic analysis is required to solve this problem, unless you want to catch what the variables in the table could be set to.

To summarize, you will need something functionally equivalent to a parser. I have a GLR parser which makes writing grammars easy and I have a full Lua grammar for it which I can modify to just include Lua tables. Let me know if you want to go down this route and I’ll put it together for you. You could also write a simple recursive descent leftmost derivation parser for this.

Edit: I’m really curious what you are going to do with this information about the table. If we had more information, I’d bet we could come up with a solution that wouldn’t require a parser (like making BTModule.Root, BranchNode, Priority, ext. keep track of nesting depth for us). Are you trying to provide debugging information? If so, then debug.traceback may be your best friend.

1 Like

@blobbyblob I think that’d be a really good way of doing it, actually. I should’ve thought about it before. Thanks!

@IdiomicLanguage Thanks for the in-depth reply! My plugin just generates a table for a behavior tree library that I’m using. It’s a node-based editor tree. My issue was that I wanted to be able to read a behavior tree table so I could import it into my plugin. For that, the easiest way I could think of would be a table that I could loop through. I think blobbyblob’s solution would be a really simple one, though.

1 Like