I generated a Lua Parser using the Lua.g grammar with antlr in java.
My lua code which i wish to parse basically looks like this
function uniqueid_some_event (e)
if (e:HasString("string1")) then
-- do something
end
if (e:HasString("string2")) then
-- do something
end
end
I have hundred of these events in different files for specific actor bindings.
Now i want to parse these files and gather for each what conditions are checked – in the above case – i want to extract “string1” and “string2” as event triggers. (To be more precise, i want to create a report that will display just the triggers for each file)
I gather i need to somehow modify the Lua.g to add my own logic in there but im lost because i dont find any documentation on this – i looked into LuaEclise which basically does some things but it does not work for me either.
So – is it possible to add to the generated LuaParser some kind of w3c DOM return value? Or something like getFunctions() which returns all functions found, and within each function getHasStringStatements() which would return the conditions?
I don’t recommend using the Lua grammar from the ANTLR wiki. AFAIK, there are quite a few things wrong with it (no proper long-strings and long-comments, invalid number/hex tokens, global backtracking to name just a few).
Here’s a (IMO) better grammar for Lua 5.2:
If you now parse the following input:
you will get the following AST returned from the generated parser:
Now all you need to do is walk through the AST, and when you stumble upon a
ASSIGNMENTnode, check if the right child is an expression list with aFUNCTIONin it. If this happens, walk through this node to look for if-statements that have string-expressions in them.Here’s a start:
When running the class above, you will see the following output: