lua-users home
lua-l archive

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



So you want to find AST nodes which are:
I guess that printing the offending line number is good enough. There's a declarative tree exploration library in metalua's head, which we discussed at the last workshop, called metalua.treequery. No proper doc yet, only a blog post. Your problem is addressed as follows (tested with the latest version in the luaeclipse branch):

$ cat find_concats_in_loops.lua
require 'metalua.compiler'
local ast = mlc.luastring_to_ast[[
x = this..is..allowed
for i=1,10 do
    print(nasty..cat)
end
for _ in generator() do
    if not directly_under_for then
        print(nasty..anyway)
    end
end
for i=1, math.huge do
    print "no concat, no problem"
end 
]]
-- predicate to find concat operators: --
local is_op_concat = |x| x.tag=='Op' and x[1]=='concat'
local T = require 'metalua.treequery'
-- Here's the actual query: --
T(ast)
    :under ('Forin', 'Fornum') 
    :filter (is_op_concat)  
    :foreach (function (node)
        local line = node.lineinfo.first.line
        printf ("Offending node on line %i", line)
    end)
$ metalua find_concats_in_loops.lua
Offending node on line 3
Offending node on line 7
$ _