lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The latest improvements in metalua were about enhancing the AST / source correspondance. Here's an untested metalua program that should retrieve all the global function declarations, and store their positions (first char and last char) in a table, indexed by function name:

-{ extension 'match' }
require 'walk'
cfg = { stat = { } }
function cfg.stat.down(x)
   match x with
   | `Set{ { `Id{ funcname} }, { `Function{ lineinfo = li, ... } } } ->
      places [funcname] = { li.first[3], li.last[3] }
   | _ -> -- pass
   end
end

filename = ...
places = { }
src = "">
ast = ast_of_luastring(src, filename)
walk.block(cfg, ast)

for name, place in pairs (places) do
   printf("Function %s's definition goes from char %i to %i:", name, unpack(place))
   print(src:sub(place[1], place[2]))
end

The API is still undocumented and likely to change superficially, don't hesitate to mail for more details.

-- Fabien.