ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Possible to get all script variables without running the script ? #1430

Open Lefty-Lex opened 1 year ago

Lefty-Lex commented 1 year ago

I was wondering if there is any way to get all the variables in a script without running it.

Reason being i am attempting a intellisense - ish feature for my editor and would make life so much easier if i could get the names and type of each variable.

OrfeasZ commented 1 year ago

This is a bit out of the scope of this library, but the way to do what you're asking for probably involves writing a lexer / parser that takes lua code and spits out some representation (eg. some tree like an AST) that you can then use to figure out the declared variables (and other code constructs). Lua publishes the grammar of the language as part of its reference manual, and you can use that and either write a lexer / parser by hand (you can find helpful resources online on how to get started with something like that) or use some existing parser generator tool to do it for you (unfortunately you can't use lua's own parser since that's very deeply tied to its execution engine).

Rochet2 commented 1 year ago

To add to what OrfeasZ said above, LuaSrcDiet is a tool for doing all kinds of things with lua like minifying a source file. It is also MIT licenced, so you might be able to use it or parts of it to achieve what you want. For example, you could reuse the parser and lexer from it possibly. It is also pure lua in case that is what you need. (Like I did)