metalua icon indicating copy to clipboard operation
metalua copied to clipboard

treequery/walk.mlua doesn't work if following the walker document

Open wanghc78 opened this issue 11 years ago • 3 comments

If I follow the document http://metalua.luaforge.net/manual004.html#toc9 to write an ast walker, such as

  local cfg      = { expr  = { },
                      stat  = { },
                      block = { } }
  cfg.expr.down = function(x) ... end
  walk.guess(cfg, term)

The walker doesn't work. The function returned by walker_builder() in walk.mlua cannot get the correct cfg up/down functions from the input cfg.

The below changes can fix it.

208c208,209
<       local down, up = cfg.down, cfg.up

---
>       local cfgt = cfg[traverse]
>       local down, up = cfgt and cfgt.down, cfgt and cfgt.up

However, I found the current treequery.mlua will use cfg's first level methods. It seems the issue is still caused by some refactoring inconsistence.

wanghc78 avatar Jun 17 '14 00:06 wanghc78

Thanks, I need to review this more carefully. The generic idea is that walk proved unpracticall to use, so I wish to mostly deprecate it and encourage people to use treequery instead; but that's no reason for it not to work.

fab13n avatar Jun 17 '14 09:06 fab13n

Thanks! The treequery is very nice. I'm using it for some analysis. Regarding the bug, my feeling is the original walk requires a two-level cfg with the first level three children "expr", "stat", "block". The current tree query always construct a one level cfg, and the walk code was changed to support the one level structure. If I pass in a two-level cfg into the walk, the code cannot find the right functions.

wanghc78 avatar Jun 19 '14 01:06 wanghc78

I think this issue is not a bug in the source code. It is from the inconsistency between the 0.5 document http://metalua.luaforge.net/manual004.html#toc9. and the current walk implementation. This form works in the current code.

local cfg = { down = function(x) ... end}
walk.guess(cfg, ast)

wanghc78 avatar Jul 15 '14 20:07 wanghc78